Refactoring

This commit is contained in:
Hajime Hoshi 2013-10-13 23:33:22 +09:00
parent 983aeec03b
commit ea6e88d06e
6 changed files with 46 additions and 50 deletions

View File

@ -4,13 +4,13 @@ package cocoa
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL -framework QuartzCore // #cgo LDFLAGS: -framework Cocoa -framework OpenGL -framework QuartzCore
// //
// #include <stdlib.h> // #include <stdlib.h>
// #include "input.h"
// //
// void Run(size_t width, size_t height, size_t scale, const char* title); // void Run(size_t width, size_t height, size_t scale, const char* title);
// //
import "C" import "C"
import ( import (
"github.com/hajimehoshi/go.ebiten" "github.com/hajimehoshi/go.ebiten"
// "github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/opengl" "github.com/hajimehoshi/go.ebiten/graphics/opengl"
"time" "time"
"unsafe" "unsafe"
@ -55,8 +55,26 @@ func ebiten_EbitenOpenGLView_Updating() {
} }
//export ebiten_EbitenOpenGLView_InputUpdated //export ebiten_EbitenOpenGLView_InputUpdated
func ebiten_EbitenOpenGLView_InputUpdated(x, y C.int) { func ebiten_EbitenOpenGLView_InputUpdated(inputType C.int, cx, cy C.int) {
currentUI.input <- ebiten.InputState{int(x), int(y)} if inputType == C.InputTypeMouseUp {
currentUI.input <- ebiten.InputState{-1, -1}
return
}
x, y := int(cx), int(cy)
x /= currentUI.screenScale
y /= currentUI.screenScale
if x < 0 {
x = 0
} else if currentUI.screenWidth <= x {
x = currentUI.screenWidth - 1
}
if y < 0 {
y = 0
} else if currentUI.screenHeight <= y {
y = currentUI.screenHeight - 1
}
currentUI.input <- ebiten.InputState{x, y}
} }
func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int, func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int,

View File

@ -1,10 +1,11 @@
// -*- objc -*- // -*- objc -*-
#include "ebiten_opengl_view.h" #include "ebiten_opengl_view.h"
#include "input.h"
void ebiten_EbitenOpenGLView_Initialized(void); void ebiten_EbitenOpenGLView_Initialized(void);
void ebiten_EbitenOpenGLView_Updating(void); void ebiten_EbitenOpenGLView_Updating(void);
void ebiten_EbitenOpenGLView_InputUpdated(int x, int y); void ebiten_EbitenOpenGLView_InputUpdated(enum InputType inputType, int x, int y);
// Reference: // Reference:
// http://developer.apple.com/library/mac/#qa/qa1385/_index.html // http://developer.apple.com/library/mac/#qa/qa1385/_index.html
@ -32,9 +33,6 @@ EbitenDisplayLinkCallback(CVDisplayLinkRef displayLink,
@implementation EbitenOpenGLView { @implementation EbitenOpenGLView {
@private @private
CVDisplayLinkRef displayLink_; CVDisplayLinkRef displayLink_;
size_t screenWidth_;
size_t screenHeight_;
size_t screenScale_;
} }
- (void)dealloc { - (void)dealloc {
@ -85,50 +83,26 @@ EbitenDisplayLinkCallback(CVDisplayLinkRef displayLink,
- (void)mouseDown:(NSEvent*)theEvent { - (void)mouseDown:(NSEvent*)theEvent {
NSPoint location = [self convertPoint:[theEvent locationInWindow] NSPoint location = [self convertPoint:[theEvent locationInWindow]
fromView:nil]; fromView:nil];
int x = location.x / self->screenScale_; int x = location.x;
int y = location.y / self->screenScale_; int y = location.y;
if (x < 0) { ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseDown, x, y);
x = 0;
} else if (self->screenWidth_<= x) {
x = self->screenWidth_ - 1;
}
if (y < 0) {
y = 0;
} else if (self->screenHeight_<= y) {
y = self->screenHeight_ - 1;
}
ebiten_EbitenOpenGLView_InputUpdated(x, y);
} }
- (void)mouseUp:(NSEvent*)theEvent { - (void)mouseUp:(NSEvent*)theEvent {
(void)theEvent; (void)theEvent;
ebiten_EbitenOpenGLView_InputUpdated(-1, -1); NSPoint location = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
int x = location.x;
int y = location.y;
ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseUp, x, y);
} }
- (void)mouseDragged:(NSEvent*)theEvent { - (void)mouseDragged:(NSEvent*)theEvent {
NSPoint location = [self convertPoint:[theEvent locationInWindow] NSPoint location = [self convertPoint:[theEvent locationInWindow]
fromView:nil]; fromView:nil];
int x = location.x / self->screenScale_; int x = location.x;
int y = location.y / self->screenScale_; int y = location.y;
if (x < 0) { ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseDragged, x, y);
x = 0;
} else if (self->screenWidth_<= x) {
x = self->screenWidth_ - 1;
}
if (y < 0) {
y = 0;
} else if (self->screenHeight_<= y) {
y = self->screenHeight_ - 1;
}
ebiten_EbitenOpenGLView_InputUpdated(x, y);
}
- (void)setScreenWidth:(size_t)screenWidth
screenHeight:(size_t)screenHeight
screenScale:(size_t)screenScale {
self->screenWidth_ = screenWidth;
self->screenHeight_ = screenHeight;
self->screenScale_ = screenScale;
} }
@end @end

View File

@ -9,9 +9,6 @@
@interface EbitenOpenGLView : NSOpenGLView @interface EbitenOpenGLView : NSOpenGLView
- (CVReturn)getFrameForTime:(CVTimeStamp const*)outputTime; - (CVReturn)getFrameForTime:(CVTimeStamp const*)outputTime;
- (void)setScreenWidth:(size_t)screenWidth
screenHeight:(size_t)screenHeight
screenScale:(size_t)screenScale;
@end @end

10
ui/cocoa/input.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef GO_EBITEN_UI_COCOA_INPUT_H_
#define GO_EBITEN_UI_COCOA_INPUT_H_
enum InputType {
InputTypeMouseUp,
InputTypeMouseDragged,
InputTypeMouseDown,
};
#endif

View File

@ -24,9 +24,6 @@ static NSWindow* generateWindow(size_t width, size_t height, size_t scale, const
EbitenOpenGLView* glView = EbitenOpenGLView* glView =
[[EbitenOpenGLView alloc] initWithFrame:rect [[EbitenOpenGLView alloc] initWithFrame:rect
pixelFormat:format]; pixelFormat:format];
[glView setScreenWidth:width
screenHeight:height
screenScale:scale];
[window setContentView:glView]; [window setContentView:glView];
[window setTitle: [[NSString alloc] initWithUTF8String:title]]; [window setTitle: [[NSString alloc] initWithUTF8String:title]];
//[window makeFirstResponder:glView]; //[window makeFirstResponder:glView];