2018-12-29 11:12:11 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-06-10 18:03:35 +02:00
|
|
|
//go:build !windows && !js
|
|
|
|
// +build !windows,!js
|
2018-12-30 07:35:49 +01:00
|
|
|
|
2018-12-29 11:12:11 +01:00
|
|
|
package glfw
|
|
|
|
|
|
|
|
import (
|
2018-12-29 18:44:51 +01:00
|
|
|
"image"
|
|
|
|
"sync"
|
2018-12-29 11:12:11 +01:00
|
|
|
|
2019-11-25 15:13:44 +01:00
|
|
|
"github.com/go-gl/glfw/v3.3/glfw"
|
2018-12-29 11:20:41 +01:00
|
|
|
)
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
type windows map[*glfw.Window]*Window
|
2018-12-29 11:20:41 +01:00
|
|
|
|
|
|
|
var (
|
2018-12-29 18:44:51 +01:00
|
|
|
theWindows = windows{}
|
|
|
|
windowsM sync.Mutex
|
2018-12-29 11:12:11 +01:00
|
|
|
)
|
2018-12-29 18:44:51 +01:00
|
|
|
|
|
|
|
func (w windows) add(win *glfw.Window) *Window {
|
|
|
|
if win == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-23 08:52:09 +01:00
|
|
|
ww := &Window{w: win}
|
2018-12-29 18:44:51 +01:00
|
|
|
windowsM.Lock()
|
|
|
|
w[win] = ww
|
|
|
|
windowsM.Unlock()
|
|
|
|
return ww
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w windows) remove(win *glfw.Window) {
|
|
|
|
windowsM.Lock()
|
|
|
|
delete(w, win)
|
|
|
|
windowsM.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w windows) get(win *glfw.Window) *Window {
|
|
|
|
if win == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
windowsM.Lock()
|
|
|
|
ww := w[win]
|
|
|
|
windowsM.Unlock()
|
|
|
|
return ww
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:21:38 +02:00
|
|
|
type Cursor struct {
|
|
|
|
c *glfw.Cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateStandardCursor(shape StandardCursor) *Cursor {
|
|
|
|
c := glfw.CreateStandardCursor(glfw.StandardCursor(shape))
|
|
|
|
return &Cursor{c: c}
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
type Monitor struct {
|
|
|
|
m *glfw.Monitor
|
|
|
|
}
|
|
|
|
|
2020-09-18 18:24:16 +02:00
|
|
|
func (m *Monitor) GetContentScale() (float32, float32) {
|
|
|
|
return m.m.GetContentScale()
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func (m *Monitor) GetPos() (x, y int) {
|
|
|
|
return m.m.GetPos()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Monitor) GetVideoMode() *VidMode {
|
|
|
|
v := m.m.GetVideoMode()
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &VidMode{
|
|
|
|
Width: v.Width,
|
|
|
|
Height: v.Height,
|
|
|
|
RedBits: v.RedBits,
|
|
|
|
GreenBits: v.GreenBits,
|
|
|
|
BlueBits: v.BlueBits,
|
|
|
|
RefreshRate: v.RefreshRate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Window struct {
|
|
|
|
w *glfw.Window
|
2021-02-23 08:52:09 +01:00
|
|
|
|
|
|
|
prevSizeCallback SizeCallback
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) Destroy() {
|
|
|
|
w.w.Destroy()
|
|
|
|
theWindows.remove(w.w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetAttrib(attrib Hint) int {
|
|
|
|
return w.w.GetAttrib(glfw.Hint(attrib))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetCursorPos() (x, y float64) {
|
|
|
|
return w.w.GetCursorPos()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetInputMode(mode InputMode) int {
|
|
|
|
return w.w.GetInputMode(glfw.InputMode(mode))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetKey(key Key) Action {
|
|
|
|
return Action(w.w.GetKey(glfw.Key(key)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetMonitor() *Monitor {
|
|
|
|
m := w.w.GetMonitor()
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Monitor{m}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetMouseButton(button MouseButton) Action {
|
|
|
|
return Action(w.w.GetMouseButton(glfw.MouseButton(button)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetPos() (x, y int) {
|
|
|
|
return w.w.GetPos()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetSize() (width, height int) {
|
|
|
|
return w.w.GetSize()
|
|
|
|
}
|
|
|
|
|
2020-03-21 11:26:28 +01:00
|
|
|
func (w *Window) Iconify() {
|
|
|
|
w.w.Iconify()
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func (w *Window) MakeContextCurrent() {
|
|
|
|
w.w.MakeContextCurrent()
|
|
|
|
}
|
|
|
|
|
2020-03-21 11:26:28 +01:00
|
|
|
func (w *Window) Maximize() {
|
|
|
|
w.w.Maximize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) Restore() {
|
|
|
|
w.w.Restore()
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:27:04 +02:00
|
|
|
func (w *Window) SetAttrib(attrib Hint, value int) {
|
|
|
|
w.w.SetAttrib(glfw.Hint(attrib), value)
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback) {
|
2021-06-13 14:43:23 +02:00
|
|
|
w.w.SetCharModsCallback(charModsCallbacks[cbfun])
|
2021-06-13 15:43:56 +02:00
|
|
|
return ToCharModsCallback(nil) // TODO
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2021-04-11 07:21:38 +02:00
|
|
|
func (w *Window) SetCursor(cursor *Cursor) {
|
|
|
|
var c *glfw.Cursor
|
|
|
|
if cursor != nil {
|
|
|
|
c = cursor.c
|
|
|
|
}
|
|
|
|
w.w.SetCursor(c)
|
|
|
|
}
|
|
|
|
|
2021-06-13 13:52:14 +02:00
|
|
|
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
|
|
|
|
w.w.SetCloseCallback(closeCallbacks[cbfun])
|
|
|
|
return ToCloseCallback(nil) // TODO
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
|
2021-06-13 14:43:23 +02:00
|
|
|
w.w.SetFramebufferSizeCallback(framebufferSizeCallbacks[cbfun])
|
2021-06-13 15:43:56 +02:00
|
|
|
return ToFramebufferSizeCallback(nil) // TODO
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback) {
|
2021-06-13 14:43:23 +02:00
|
|
|
w.w.SetScrollCallback(scrollCallbacks[cbfun])
|
2021-06-13 15:43:56 +02:00
|
|
|
return ToScrollCallback(nil) // TODO
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 13:52:14 +02:00
|
|
|
func (w *Window) SetShouldClose(value bool) {
|
|
|
|
w.w.SetShouldClose(value)
|
|
|
|
}
|
|
|
|
|
2019-01-06 16:21:59 +01:00
|
|
|
func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
|
2021-06-13 14:43:23 +02:00
|
|
|
w.w.SetSizeCallback(sizeCallbacks[cbfun])
|
2021-02-23 08:52:09 +01:00
|
|
|
prev := w.prevSizeCallback
|
|
|
|
w.prevSizeCallback = cbfun
|
|
|
|
return prev
|
2019-01-06 16:21:59 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 19:27:04 +02:00
|
|
|
func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
|
|
|
|
w.w.SetSizeLimits(minw, minh, maxw, maxh)
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func (w *Window) SetIcon(images []image.Image) {
|
|
|
|
w.w.SetIcon(images)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetInputMode(mode InputMode, value int) {
|
|
|
|
w.w.SetInputMode(glfw.InputMode(mode), value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) {
|
|
|
|
var m *glfw.Monitor
|
|
|
|
if monitor != nil {
|
|
|
|
m = monitor.m
|
|
|
|
}
|
|
|
|
w.w.SetMonitor(m, xpos, ypos, width, height, refreshRate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetPos(xpos, ypos int) {
|
|
|
|
w.w.SetPos(xpos, ypos)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetSize(width, height int) {
|
|
|
|
w.w.SetSize(width, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetTitle(title string) {
|
|
|
|
w.w.SetTitle(title)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) ShouldClose() bool {
|
|
|
|
return w.w.ShouldClose()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) Show() {
|
|
|
|
w.w.Show()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SwapBuffers() {
|
|
|
|
w.w.SwapBuffers()
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) {
|
|
|
|
var gm *glfw.Monitor
|
|
|
|
if monitor != nil {
|
|
|
|
gm = monitor.m
|
|
|
|
}
|
|
|
|
var gw *glfw.Window
|
|
|
|
if share != nil {
|
|
|
|
gw = share.w
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := glfw.CreateWindow(width, height, title, gm, gw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return theWindows.add(w), nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:11:32 +01:00
|
|
|
func (j Joystick) GetGUID() string {
|
|
|
|
return glfw.Joystick(j).GetGUID()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j Joystick) GetName() string {
|
|
|
|
return glfw.Joystick(j).GetName()
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:13:44 +01:00
|
|
|
func (j Joystick) GetAxes() []float32 {
|
|
|
|
return glfw.Joystick(j).GetAxes()
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 15:13:44 +01:00
|
|
|
func (j Joystick) GetButtons() []Action {
|
|
|
|
var bs []Action
|
|
|
|
for _, b := range glfw.Joystick(j).GetButtons() {
|
|
|
|
bs = append(bs, Action(b))
|
|
|
|
}
|
|
|
|
return bs
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2021-07-18 16:28:26 +02:00
|
|
|
func (j Joystick) GetHats() []JoystickHatState {
|
|
|
|
var hats []JoystickHatState
|
|
|
|
for _, s := range glfw.Joystick(j).GetHats() {
|
|
|
|
hats = append(hats, JoystickHatState(s))
|
|
|
|
}
|
|
|
|
return hats
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func GetMonitors() []*Monitor {
|
|
|
|
ms := []*Monitor{}
|
|
|
|
for _, m := range glfw.GetMonitors() {
|
|
|
|
if m != nil {
|
|
|
|
ms = append(ms, &Monitor{m})
|
|
|
|
} else {
|
|
|
|
ms = append(ms, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPrimaryMonitor() *Monitor {
|
|
|
|
m := glfw.GetPrimaryMonitor()
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Monitor{m}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init() error {
|
|
|
|
return glfw.Init()
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:13:44 +01:00
|
|
|
func (j Joystick) Present() bool {
|
|
|
|
return glfw.Joystick(j).Present()
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func PollEvents() {
|
|
|
|
glfw.PollEvents()
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:28 +02:00
|
|
|
func PostEmptyEvent() {
|
|
|
|
glfw.PostEmptyEvent()
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:13:44 +01:00
|
|
|
func SetMonitorCallback(cbfun func(monitor *Monitor, event PeripheralEvent)) {
|
|
|
|
var gcb func(monitor *glfw.Monitor, event glfw.PeripheralEvent)
|
2018-12-29 18:44:51 +01:00
|
|
|
if cbfun != nil {
|
2019-11-25 15:13:44 +01:00
|
|
|
gcb = func(monitor *glfw.Monitor, event glfw.PeripheralEvent) {
|
2018-12-29 18:44:51 +01:00
|
|
|
var m *Monitor
|
|
|
|
if monitor != nil {
|
|
|
|
m = &Monitor{monitor}
|
|
|
|
}
|
2019-11-25 15:13:44 +01:00
|
|
|
cbfun(m, PeripheralEvent(event))
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
glfw.SetMonitorCallback(gcb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapInterval(interval int) {
|
|
|
|
glfw.SwapInterval(interval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Terminate() {
|
|
|
|
glfw.Terminate()
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:28 +02:00
|
|
|
func WaitEvents() {
|
|
|
|
glfw.WaitEvents()
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func WindowHint(target Hint, hint int) {
|
|
|
|
glfw.WindowHint(glfw.Hint(target), hint)
|
|
|
|
}
|