ebiten/ui/cocoa/cocoa.go

152 lines
3.2 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-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-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-12-06 18:20:48 +01:00
"image"
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
2013-12-08 14:13:48 +01:00
window *window
2013-12-06 18:20:48 +01:00
initialEventSent bool
textureFactory *textureFactory
2013-12-07 17:35:24 +01:00
graphicsDevice *opengl.Device
2013-12-06 18:20:48 +01:00
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.")
}
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)
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}
2013-12-06 18:20:48 +01:00
u.uiEvents.notifyScreenSizeUpdated(e)
u.initialEventSent = true
2013-11-29 20:24:52 +01:00
}
2013-10-14 17:49:30 +02:00
}
2013-12-08 08:19:24 +01:00
func (u *UI) CreateTexture(tag interface{}, img image.Image) {
2013-12-07 17:35:24 +01:00
go func() {
var id graphics.TextureId
var err error
u.textureFactory.UseContext(func() {
2013-12-09 01:45:40 +01:00
id, err = u.graphicsDevice.CreateTexture(img)
2013-12-07 17:35:24 +01:00
})
e := graphics.TextureCreatedEvent{
Tag: tag,
Id: id,
Error: err,
}
u.textureFactory.notifyTextureCreated(e)
}()
}
2013-12-08 08:19:24 +01:00
func (u *UI) CreateRenderTarget(tag interface{}, width, height int) {
2013-12-07 17:35:24 +01:00
go func() {
var id graphics.RenderTargetId
var err error
u.textureFactory.UseContext(func() {
2013-12-09 01:45:40 +01:00
id, err = u.graphicsDevice.CreateRenderTarget(width, height)
2013-12-07 17:35:24 +01:00
})
e := graphics.RenderTargetCreatedEvent{
Tag: tag,
Id: id,
Error: err,
}
u.textureFactory.notifyRenderTargetCreated(e)
}()
2013-12-06 18:20:48 +01:00
}
2013-12-07 17:35:24 +01:00
func (u *UI) TextureCreated() <-chan graphics.TextureCreatedEvent {
return u.textureFactory.TextureCreated()
}
func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent {
return u.textureFactory.RenderTargetCreated()
2013-12-03 14:14:51 +01:00
}
func (u *UI) Draw(f func(graphics.Canvas)) {
2013-12-08 14:13:48 +01:00
u.window.UseContext(func() {
u.graphicsDevice.Update(f)
})
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) {
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) {
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}
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)
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}
2013-12-06 18:20:48 +01:00
u.uiEvents.notifyInputStateUpdated(e)
2013-10-13 16:06:05 +02:00
}