diff --git a/ui/cocoa/cocoa.go b/ui/cocoa/cocoa.go index e10c9432a..673dec3d5 100644 --- a/ui/cocoa/cocoa.go +++ b/ui/cocoa/cocoa.go @@ -4,13 +4,13 @@ package cocoa // #cgo LDFLAGS: -framework Cocoa -framework OpenGL -framework QuartzCore // // #include +// #include "input.h" // // void Run(size_t width, size_t height, size_t scale, const char* title); // import "C" import ( "github.com/hajimehoshi/go.ebiten" - // "github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics/opengl" "time" "unsafe" @@ -55,8 +55,26 @@ func ebiten_EbitenOpenGLView_Updating() { } //export ebiten_EbitenOpenGLView_InputUpdated -func ebiten_EbitenOpenGLView_InputUpdated(x, y C.int) { - currentUI.input <- ebiten.InputState{int(x), int(y)} +func ebiten_EbitenOpenGLView_InputUpdated(inputType C.int, cx, cy C.int) { + 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, diff --git a/ui/cocoa/ebiten_opengl_view.c b/ui/cocoa/ebiten_opengl_view.c index 616448b07..6e429fa8d 100644 --- a/ui/cocoa/ebiten_opengl_view.c +++ b/ui/cocoa/ebiten_opengl_view.c @@ -1,10 +1,11 @@ // -*- objc -*- #include "ebiten_opengl_view.h" +#include "input.h" void ebiten_EbitenOpenGLView_Initialized(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: // http://developer.apple.com/library/mac/#qa/qa1385/_index.html @@ -32,9 +33,6 @@ EbitenDisplayLinkCallback(CVDisplayLinkRef displayLink, @implementation EbitenOpenGLView { @private CVDisplayLinkRef displayLink_; - size_t screenWidth_; - size_t screenHeight_; - size_t screenScale_; } - (void)dealloc { @@ -85,50 +83,26 @@ EbitenDisplayLinkCallback(CVDisplayLinkRef displayLink, - (void)mouseDown:(NSEvent*)theEvent { NSPoint location = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - int x = location.x / self->screenScale_; - int y = location.y / self->screenScale_; - if (x < 0) { - 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); + int x = location.x; + int y = location.y; + ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseDown, x, y); } - (void)mouseUp:(NSEvent*)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 { NSPoint location = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - int x = location.x / self->screenScale_; - int y = location.y / self->screenScale_; - if (x < 0) { - 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; + int x = location.x; + int y = location.y; + ebiten_EbitenOpenGLView_InputUpdated(InputTypeMouseDragged, x, y); } @end diff --git a/ui/cocoa/ebiten_opengl_view.h b/ui/cocoa/ebiten_opengl_view.h index a4f6b98da..802065c0a 100644 --- a/ui/cocoa/ebiten_opengl_view.h +++ b/ui/cocoa/ebiten_opengl_view.h @@ -9,9 +9,6 @@ @interface EbitenOpenGLView : NSOpenGLView - (CVReturn)getFrameForTime:(CVTimeStamp const*)outputTime; -- (void)setScreenWidth:(size_t)screenWidth - screenHeight:(size_t)screenHeight - screenScale:(size_t)screenScale; @end diff --git a/ui/cocoa/input.h b/ui/cocoa/input.h new file mode 100644 index 000000000..3042a5bca --- /dev/null +++ b/ui/cocoa/input.h @@ -0,0 +1,10 @@ +#ifndef GO_EBITEN_UI_COCOA_INPUT_H_ +#define GO_EBITEN_UI_COCOA_INPUT_H_ + +enum InputType { + InputTypeMouseUp, + InputTypeMouseDragged, + InputTypeMouseDown, +}; + +#endif diff --git a/ui/cocoa/run.c b/ui/cocoa/run.c index 9689b2697..cc8dd34ba 100644 --- a/ui/cocoa/run.c +++ b/ui/cocoa/run.c @@ -24,9 +24,6 @@ static NSWindow* generateWindow(size_t width, size_t height, size_t scale, const EbitenOpenGLView* glView = [[EbitenOpenGLView alloc] initWithFrame:rect pixelFormat:format]; - [glView setScreenWidth:width - screenHeight:height - screenScale:scale]; [window setContentView:glView]; [window setTitle: [[NSString alloc] initWithUTF8String:title]]; //[window makeFirstResponder:glView]; diff --git a/ui/glut/glut.go b/ui/glut/glut.go index d5cab77f7..6384fe583 100644 --- a/ui/glut/glut.go +++ b/ui/glut/glut.go @@ -117,7 +117,7 @@ func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int, title str screenWidth, screenHeight, screenScale) ui.graphicsDevice = graphicsDevice graphicsDevice.Init() - + game.Init(ui.graphicsDevice.TextureFactory()) input := make(chan ebiten.InputState) @@ -169,7 +169,7 @@ func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int, title str // Set the callbacks C.setGlutFuncs() - + C.glutMainLoop() }