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 "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);
@implementation EbitenContentView {
@ -17,10 +19,11 @@ void ebiten_MouseStateUpdated(void* nativeWindow, InputType inputType, int x, in
}
- (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 {
@ -48,17 +51,4 @@ void ebiten_MouseStateUpdated(void* nativeWindow, InputType inputType, int x, in
ebiten_MouseStateUpdated([self window], InputTypeMouseDragged, x, y);
}
- (void)moveDown:(id)sender {
}
- (void)moveLeft:(id)sender {
}
- (void)moveRight:(id)sender {
}
- (void)moveUp:(id)sender {
}
@end

View File

@ -27,6 +27,7 @@ type Window struct {
screenScale int
closed bool
native unsafe.Pointer
pressedKeys map[ui.Key]struct{}
context *opengl.Context
funcs chan func()
funcsDone chan struct{}
@ -35,13 +36,14 @@ type Window struct {
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{
ui: ui,
ui: cocoaUI,
screenWidth: width,
screenHeight: height,
screenScale: scale,
closed: false,
pressedKeys: map[ui.Key]struct{}{},
funcs: make(chan func()),
funcsDone: make(chan struct{}),
}
@ -63,7 +65,7 @@ func runWindow(ui *cocoaUI, width, height, scale int, title string, sharedContex
}()
<-ch
w.useGLContext(func() {
w.context = ui.graphicsDevice.CreateContext(width, height, scale)
w.context = w.ui.graphicsDevice.CreateContext(width, height, scale)
})
return w
}
@ -102,6 +104,33 @@ func ebiten_ScreenSizeUpdated(nativeWindow unsafe.Pointer, width, height int) {
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
func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType, cx, cy C.int) {
w := windows[nativeWindow]

View File

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