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-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-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"
|
2013-12-02 16:15:01 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/ui"
|
2013-12-06 18:20:48 +01:00
|
|
|
"image"
|
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-06 18:20:48 +01:00
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
|
|
|
screenScale int
|
|
|
|
graphicsDevice *opengl.Device
|
|
|
|
window unsafe.Pointer
|
|
|
|
initialEventSent bool
|
|
|
|
textureFactory *textureFactory
|
|
|
|
uiEvents
|
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.")
|
|
|
|
}
|
2013-12-02 16:15:01 +01:00
|
|
|
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
|
|
|
C.StartApplication()
|
|
|
|
|
2013-12-06 18:20:48 +01:00
|
|
|
u.textureFactory = runTextureFactory()
|
|
|
|
|
|
|
|
u.textureFactory.UseContext(func() {
|
|
|
|
u.graphicsDevice = opengl.NewDevice(
|
|
|
|
u.screenWidth,
|
|
|
|
u.screenHeight,
|
|
|
|
u.screenScale)
|
|
|
|
})
|
|
|
|
|
|
|
|
u.window = u.textureFactory.CreateWindow(
|
|
|
|
u.screenWidth*u.screenScale,
|
|
|
|
u.screenHeight*u.screenScale,
|
|
|
|
title)
|
|
|
|
|
2013-12-02 16:15:01 +01:00
|
|
|
currentUI = u
|
2013-11-29 19:21:10 +01:00
|
|
|
|
2013-12-02 16:15:01 +01:00
|
|
|
return u
|
2013-10-14 17:49:30 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 16:15:01 +01:00
|
|
|
func (u *UI) PollEvents() {
|
2013-11-23 10:10:15 +01:00
|
|
|
C.PollEvents()
|
2013-12-02 16:15:01 +01:00
|
|
|
if !u.initialEventSent {
|
|
|
|
e := ui.ScreenSizeUpdatedEvent{u.screenWidth, u.screenHeight}
|
2013-12-06 18:20:48 +01:00
|
|
|
u.uiEvents.notifyScreenSizeUpdated(e)
|
2013-12-02 16:15:01 +01:00
|
|
|
u.initialEventSent = true
|
2013-11-29 20:24:52 +01:00
|
|
|
}
|
2013-10-14 17:49:30 +02:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:20:48 +01:00
|
|
|
func (u *UI) CreateRenderTarget(tag string, width, height int) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UI) CreateTexture(tag string, img image.Image) {
|
2013-12-03 14:14:51 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 16:15:01 +01:00
|
|
|
func (u *UI) LoadResources(f func(graphics.TextureFactory)) {
|
2013-12-06 18:20:48 +01:00
|
|
|
// This should be executed on the shared-context context
|
2013-12-04 13:51:39 +01:00
|
|
|
f(u.graphicsDevice)
|
2013-11-22 18:56:18 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 16:15:01 +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-11-29 20:24:52 +01:00
|
|
|
//export ebiten_ScreenSizeUpdated
|
|
|
|
func ebiten_ScreenSizeUpdated(width, height int) {
|
2013-12-02 16:15:01 +01:00
|
|
|
u := currentUI
|
|
|
|
e := ui.ScreenSizeUpdatedEvent{width, height}
|
2013-12-06 18:20:48 +01:00
|
|
|
u.uiEvents.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) {
|
2013-12-02 16:15:01 +01:00
|
|
|
u := currentUI
|
2013-11-23 11:31:10 +01:00
|
|
|
|
2013-10-13 16:33:22 +02:00
|
|
|
if inputType == C.InputTypeMouseUp {
|
2013-12-02 16:15:01 +01:00
|
|
|
e := ui.InputStateUpdatedEvent{-1, -1}
|
2013-12-06 18:20:48 +01:00
|
|
|
u.uiEvents.notifyInputStateUpdated(e)
|
2013-10-13 16:33:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
x, y := int(cx), int(cy)
|
2013-12-02 16:15:01 +01:00
|
|
|
x /= u.screenScale
|
|
|
|
y /= u.screenScale
|
2013-10-13 16:33:22 +02:00
|
|
|
if x < 0 {
|
|
|
|
x = 0
|
2013-12-02 16:15:01 +01:00
|
|
|
} else if u.screenWidth <= x {
|
|
|
|
x = u.screenWidth - 1
|
2013-10-13 16:33:22 +02:00
|
|
|
}
|
|
|
|
if y < 0 {
|
|
|
|
y = 0
|
2013-12-02 16:15:01 +01:00
|
|
|
} else if u.screenHeight <= y {
|
|
|
|
y = u.screenHeight - 1
|
2013-10-13 16:33:22 +02:00
|
|
|
}
|
2013-12-02 16:15:01 +01:00
|
|
|
e := ui.InputStateUpdatedEvent{x, y}
|
2013-12-06 18:20:48 +01:00
|
|
|
u.uiEvents.notifyInputStateUpdated(e)
|
2013-10-13 16:06:05 +02:00
|
|
|
}
|