mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 19:28:57 +01:00
Refactoring
This commit is contained in:
parent
983aeec03b
commit
ea6e88d06e
@ -4,13 +4,13 @@ package cocoa
|
||||
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL -framework QuartzCore
|
||||
//
|
||||
// #include <stdlib.h>
|
||||
// #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,
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
10
ui/cocoa/input.h
Normal file
10
ui/cocoa/input.h
Normal 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
|
@ -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];
|
||||
|
Loading…
Reference in New Issue
Block a user