ebiten/ui/cocoa/cocoa.go

162 lines
3.6 KiB
Go
Raw Normal View History

2013-10-12 20:17:51 +02:00
package cocoa
2013-11-22 18:56:18 +01:00
// #cgo CFLAGS: -x objective-c
2013-11-23 10:10:15 +01:00
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL
//
2013-10-12 20:17:51 +02:00
// #include <stdlib.h>
2013-10-13 16:33:22 +02:00
// #include "input.h"
2013-10-12 20:17:51 +02:00
//
2013-11-23 17:17:22 +01:00
// void StartApplication(void);
2013-11-24 18:12:07 +01:00
// void* CreateGLContext(void* sharedGLContext);
// void SetCurrentGLContext(void* glContext);
2013-11-25 14:27:59 +01:00
// void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext);
2013-11-23 10:10:15 +01:00
// void PollEvents(void);
2013-11-23 17:17:22 +01:00
// void BeginDrawing(void* window);
// void EndDrawing(void* window);
2013-10-12 20:17:51 +02:00
//
import "C"
import (
2013-11-22 18:56:18 +01:00
"github.com/hajimehoshi/go-ebiten/graphics"
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
"github.com/hajimehoshi/go-ebiten/ui"
2013-10-13 10:36:18 +02:00
"unsafe"
2013-10-12 20:17:51 +02:00
)
2013-10-13 16:06:05 +02:00
type UI struct {
2013-12-03 14:14:51 +01:00
screenWidth int
screenHeight int
screenScale int
graphicsDevice *opengl.Device
window unsafe.Pointer
initialEventSent bool
screenSizeUpdated chan ui.ScreenSizeUpdatedEvent // initialized lazily
inputStateUpdated chan ui.InputStateUpdatedEvent // initialized lazily
2013-10-13 16:06:05 +02:00
}
var currentUI *UI
2013-10-14 17:49:30 +02:00
func New(screenWidth, screenHeight, screenScale int, title string) *UI {
if currentUI != nil {
panic("UI can't be duplicated.")
}
u := &UI{
2013-12-03 14:14:51 +01:00
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
initialEventSent: false,
2013-10-14 17:49:30 +02:00
}
2013-11-23 17:17:22 +01:00
cTitle := C.CString(title)
2013-10-14 17:49:30 +02:00
defer C.free(unsafe.Pointer(cTitle))
2013-11-23 17:17:22 +01:00
C.StartApplication()
2013-11-24 18:12:07 +01:00
context := C.CreateGLContext(unsafe.Pointer(nil))
2013-11-26 17:46:07 +01:00
C.SetCurrentGLContext(context)
u.graphicsDevice = opengl.NewDevice(
u.screenWidth,
u.screenHeight,
u.screenScale)
2013-11-23 17:17:22 +01:00
u.window = C.CreateWindow(C.size_t(u.screenWidth*u.screenScale),
C.size_t(u.screenHeight*u.screenScale),
2013-11-24 18:12:07 +01:00
cTitle,
context)
currentUI = u
2013-11-29 19:21:10 +01:00
return u
2013-10-14 17:49:30 +02:00
}
func (u *UI) PollEvents() {
2013-11-23 10:10:15 +01:00
C.PollEvents()
if !u.initialEventSent {
e := ui.ScreenSizeUpdatedEvent{u.screenWidth, u.screenHeight}
u.notifyScreenSizeUpdated(e)
u.initialEventSent = true
2013-11-29 20:24:52 +01:00
}
2013-10-14 17:49:30 +02:00
}
2013-12-03 14:14:51 +01:00
func (u *UI) LoadTextures(map[int]string) {
// TODO: Implement
}
func (u *UI) LoadResources(f func(graphics.TextureFactory)) {
C.BeginDrawing(u.window)
2013-12-04 13:51:39 +01:00
f(u.graphicsDevice)
C.EndDrawing(u.window)
2013-11-22 18:56:18 +01:00
}
func (u *UI) Draw(f func(graphics.Canvas)) {
C.BeginDrawing(u.window)
u.graphicsDevice.Update(f)
C.EndDrawing(u.window)
2013-10-14 17:49:30 +02:00
}
2013-12-03 14:14:51 +01:00
func (u *UI) ScreenSizeUpdated() <-chan ui.ScreenSizeUpdatedEvent {
if u.screenSizeUpdated != nil {
return u.screenSizeUpdated
}
u.screenSizeUpdated = make(chan ui.ScreenSizeUpdatedEvent)
return u.screenSizeUpdated
2013-11-29 19:21:10 +01:00
}
2013-12-03 14:14:51 +01:00
func (u *UI) notifyScreenSizeUpdated(e ui.ScreenSizeUpdatedEvent) {
if u.screenSizeUpdated == nil {
return
}
2013-11-29 19:21:10 +01:00
go func() {
2013-12-03 14:14:51 +01:00
u.screenSizeUpdated <- e
2013-11-29 19:21:10 +01:00
}()
}
2013-12-03 14:14:51 +01:00
func (u *UI) InputStateUpdated() <-chan ui.InputStateUpdatedEvent {
if u.inputStateUpdated != nil {
return u.inputStateUpdated
}
u.inputStateUpdated = make(chan ui.InputStateUpdatedEvent)
return u.inputStateUpdated
2013-11-29 20:24:52 +01:00
}
2013-12-03 14:14:51 +01:00
func (u *UI) notifyInputStateUpdated(e ui.InputStateUpdatedEvent) {
if u.inputStateUpdated == nil {
return
}
2013-11-29 20:24:52 +01:00
go func() {
2013-12-03 14:14:51 +01:00
u.inputStateUpdated <- e
2013-11-29 20:24:52 +01:00
}()
}
//export ebiten_ScreenSizeUpdated
func ebiten_ScreenSizeUpdated(width, height int) {
u := currentUI
e := ui.ScreenSizeUpdatedEvent{width, height}
u.notifyScreenSizeUpdated(e)
2013-11-29 20:24:52 +01:00
}
2013-11-23 10:15:39 +01:00
//export ebiten_InputUpdated
func ebiten_InputUpdated(inputType C.InputType, cx, cy C.int) {
u := currentUI
2013-11-23 11:31:10 +01:00
2013-10-13 16:33:22 +02:00
if inputType == C.InputTypeMouseUp {
e := ui.InputStateUpdatedEvent{-1, -1}
u.notifyInputStateUpdated(e)
2013-10-13 16:33:22 +02:00
return
}
x, y := int(cx), int(cy)
x /= u.screenScale
y /= u.screenScale
2013-10-13 16:33:22 +02:00
if x < 0 {
x = 0
} else if u.screenWidth <= x {
x = u.screenWidth - 1
2013-10-13 16:33:22 +02:00
}
if y < 0 {
y = 0
} else if u.screenHeight <= y {
y = u.screenHeight - 1
2013-10-13 16:33:22 +02:00
}
e := ui.InputStateUpdatedEvent{x, y}
u.notifyInputStateUpdated(e)
2013-10-13 16:06:05 +02:00
}