Added keydown/keyup

This commit is contained in:
Hajime Hoshi 2013-12-16 01:13:18 +09:00
parent 0a3ec197ff
commit b70884ca40
3 changed files with 50 additions and 18 deletions

View File

@ -3,6 +3,8 @@
#include "ebiten_content_view.h" #include "ebiten_content_view.h"
#include "input.h" #include "input.h"
void ebiten_KeyDown(void* nativeWindow, int keyCode);
void ebiten_KeyUp(void* nativeWindow, int keyCode);
void ebiten_MouseStateUpdated(void* nativeWindow, InputType inputType, int x, int y); void ebiten_MouseStateUpdated(void* nativeWindow, InputType inputType, int x, int y);
@implementation EbitenContentView { @implementation EbitenContentView {
@ -17,10 +19,11 @@ void ebiten_MouseStateUpdated(void* nativeWindow, InputType inputType, int x, in
} }
- (void)keyDown:(NSEvent*)theEvent { - (void)keyDown:(NSEvent*)theEvent {
[self interpretKeyEvents:[NSArray arrayWithObject:theEvent]]; ebiten_KeyDown([self window], [theEvent keyCode]);
} }
- (void)insertText:(id)aString { - (void)keyUp:(NSEvent*)theEvent {
ebiten_KeyUp([self window], [theEvent keyCode]);
} }
- (void)mouseDown:(NSEvent*)theEvent { - (void)mouseDown:(NSEvent*)theEvent {
@ -48,17 +51,4 @@ void ebiten_MouseStateUpdated(void* nativeWindow, InputType inputType, int x, in
ebiten_MouseStateUpdated([self window], InputTypeMouseDragged, x, y); ebiten_MouseStateUpdated([self window], InputTypeMouseDragged, x, y);
} }
- (void)moveDown:(id)sender {
}
- (void)moveLeft:(id)sender {
}
- (void)moveRight:(id)sender {
}
- (void)moveUp:(id)sender {
}
@end @end

View File

@ -27,6 +27,7 @@ type Window struct {
screenScale int screenScale int
closed bool closed bool
native unsafe.Pointer native unsafe.Pointer
pressedKeys map[ui.Key]struct{}
context *opengl.Context context *opengl.Context
funcs chan func() funcs chan func()
funcsDone chan struct{} funcsDone chan struct{}
@ -35,13 +36,14 @@ type Window struct {
var windows = map[unsafe.Pointer]*Window{} var windows = map[unsafe.Pointer]*Window{}
func runWindow(ui *cocoaUI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window { func runWindow(cocoaUI *cocoaUI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window {
w := &Window{ w := &Window{
ui: ui, ui: cocoaUI,
screenWidth: width, screenWidth: width,
screenHeight: height, screenHeight: height,
screenScale: scale, screenScale: scale,
closed: false, closed: false,
pressedKeys: map[ui.Key]struct{}{},
funcs: make(chan func()), funcs: make(chan func()),
funcsDone: make(chan struct{}), funcsDone: make(chan struct{}),
} }
@ -63,7 +65,7 @@ func runWindow(ui *cocoaUI, width, height, scale int, title string, sharedContex
}() }()
<-ch <-ch
w.useGLContext(func() { w.useGLContext(func() {
w.context = ui.graphicsDevice.CreateContext(width, height, scale) w.context = w.ui.graphicsDevice.CreateContext(width, height, scale)
}) })
return w return w
} }
@ -102,6 +104,33 @@ func ebiten_ScreenSizeUpdated(nativeWindow unsafe.Pointer, width, height int) {
u.windowEvents.notifyScreenSizeUpdated(e) u.windowEvents.notifyScreenSizeUpdated(e)
}*/ }*/
var cocoaKeyCodeToKey = map[int]ui.Key{
123: ui.KeyLeft,
124: ui.KeyRight,
125: ui.KeyUp,
126: ui.KeyDown,
}
//export ebiten_KeyDown
func ebiten_KeyDown(nativeWindow unsafe.Pointer, keyCode int) {
key, ok := cocoaKeyCodeToKey[keyCode]
if !ok {
return
}
w := windows[nativeWindow]
w.pressedKeys[key] = struct{}{}
}
//export ebiten_KeyUp
func ebiten_KeyUp(nativeWindow unsafe.Pointer, keyCode int) {
key, ok := cocoaKeyCodeToKey[keyCode]
if !ok {
return
}
w := windows[nativeWindow]
delete(w.pressedKeys, key)
}
//export ebiten_MouseStateUpdated //export ebiten_MouseStateUpdated
func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType, cx, cy C.int) { func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType, cx, cy C.int) {
w := windows[nativeWindow] w := windows[nativeWindow]

View File

@ -4,11 +4,24 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics"
) )
type Key int
const (
KeyUp Key = iota
KeyDown
KeyLeft
KeyRight
)
type ScreenSizeUpdatedEvent struct { type ScreenSizeUpdatedEvent struct {
Width int Width int
Height int Height int
} }
type KeyStateUpdatedEvent struct {
Keys []Key
}
type MouseStateUpdatedEvent struct { type MouseStateUpdatedEvent struct {
X int X int
Y int Y int