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
|
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
|
|
|
|
2023-07-21 18:02:55 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/cglfw"
|
2018-12-29 11:20:41 +01:00
|
|
|
)
|
|
|
|
|
2023-07-21 18:02:55 +02:00
|
|
|
type windows map[*cglfw.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
|
|
|
|
2023-07-21 18:02:55 +02:00
|
|
|
func (w windows) add(win *cglfw.Window) *Window {
|
2018-12-29 18:44:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-21 18:02:55 +02:00
|
|
|
func (w windows) remove(win *cglfw.Window) {
|
2018-12-29 18:44:51 +01:00
|
|
|
windowsM.Lock()
|
|
|
|
delete(w, win)
|
|
|
|
windowsM.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-07-21 18:02:55 +02:00
|
|
|
func (w windows) get(win *cglfw.Window) *Window {
|
2018-12-29 18:44:51 +01:00
|
|
|
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 {
|
2023-07-21 18:02:55 +02:00
|
|
|
c *cglfw.Cursor
|
2021-04-11 07:21:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateStandardCursor(shape StandardCursor) *Cursor {
|
2023-07-21 18:02:55 +02:00
|
|
|
c := cglfw.CreateStandardCursor(cglfw.StandardCursor(shape))
|
2021-04-11 07:21:38 +02:00
|
|
|
return &Cursor{c: c}
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
type Monitor struct {
|
2023-07-21 18:02:55 +02:00
|
|
|
m *cglfw.Monitor
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2022-12-08 05:39:15 +01:00
|
|
|
func (m *Monitor) GetContentScale() (float32, float32, error) {
|
|
|
|
x, y := m.m.GetContentScale()
|
|
|
|
return x, y, nil
|
2020-09-18 18:24:16 +02:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:04 +02:00
|
|
|
func (m *Monitor) GetName() string {
|
|
|
|
return m.m.GetName()
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
type Window struct {
|
2023-07-21 18:02:55 +02:00
|
|
|
w *cglfw.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)
|
|
|
|
}
|
|
|
|
|
2022-08-12 15:34:17 +02:00
|
|
|
func (w *Window) Focus() {
|
|
|
|
w.w.Focus()
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func (w *Window) GetAttrib(attrib Hint) int {
|
2023-07-21 18:02:55 +02:00
|
|
|
return w.w.GetAttrib(cglfw.Hint(attrib))
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetCursorPos() (x, y float64) {
|
|
|
|
return w.w.GetCursorPos()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetInputMode(mode InputMode) int {
|
2023-07-21 18:02:55 +02:00
|
|
|
return w.w.GetInputMode(cglfw.InputMode(mode))
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetKey(key Key) Action {
|
2023-07-21 18:02:55 +02:00
|
|
|
return Action(w.w.GetKey(cglfw.Key(key)))
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetMonitor() *Monitor {
|
|
|
|
m := w.w.GetMonitor()
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Monitor{m}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetMouseButton(button MouseButton) Action {
|
2023-07-21 18:02:55 +02:00
|
|
|
return Action(w.w.GetMouseButton(cglfw.MouseButton(button)))
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetPos() (x, y int) {
|
|
|
|
return w.w.GetPos()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) GetSize() (width, height int) {
|
|
|
|
return w.w.GetSize()
|
|
|
|
}
|
|
|
|
|
2021-09-24 19:46:18 +02:00
|
|
|
func (w *Window) Hide() {
|
|
|
|
w.w.Hide()
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-07-21 18:02:55 +02:00
|
|
|
w.w.SetAttrib(cglfw.Hint(attrib), value)
|
2021-04-16 19:27:04 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback) {
|
2022-05-20 10:34:06 +02:00
|
|
|
w.w.SetCharModsCallback(cbfun)
|
2021-06-13 15:43:56 +02:00
|
|
|
return ToCharModsCallback(nil) // TODO
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2023-09-16 21:37:22 +02:00
|
|
|
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
|
|
|
|
w.w.SetCloseCallback(cbfun)
|
|
|
|
return ToCloseCallback(nil) // TODO
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:21:38 +02:00
|
|
|
func (w *Window) SetCursor(cursor *Cursor) {
|
2023-07-21 18:02:55 +02:00
|
|
|
var c *cglfw.Cursor
|
2021-04-11 07:21:38 +02:00
|
|
|
if cursor != nil {
|
|
|
|
c = cursor.c
|
|
|
|
}
|
|
|
|
w.w.SetCursor(c)
|
|
|
|
}
|
|
|
|
|
2023-09-16 21:37:22 +02:00
|
|
|
func (w *Window) SetCursorPos(xpos, ypos float64) {
|
|
|
|
w.w.SetCursorPos(xpos, ypos)
|
2021-06-13 13:52:14 +02:00
|
|
|
}
|
|
|
|
|
2023-01-21 15:34:20 +01:00
|
|
|
func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) {
|
|
|
|
w.w.SetDropCallback(cbfun)
|
|
|
|
return ToDropCallback(nil) // TODO
|
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
|
2022-05-20 10:34:06 +02:00
|
|
|
w.w.SetFramebufferSizeCallback(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) {
|
2022-05-20 10:34:06 +02:00
|
|
|
w.w.SetScrollCallback(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) {
|
2022-05-20 10:34:06 +02:00
|
|
|
w.w.SetSizeCallback(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)
|
|
|
|
}
|
|
|
|
|
2022-02-09 11:36:42 +01:00
|
|
|
func (w *Window) SetAspectRatio(numer, denom int) {
|
|
|
|
w.w.SetAspectRatio(numer, denom)
|
2022-02-09 11:11:25 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2023-07-21 18:02:55 +02:00
|
|
|
w.w.SetInputMode(cglfw.InputMode(mode), value)
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) {
|
2023-07-21 18:02:55 +02:00
|
|
|
var m *cglfw.Monitor
|
2018-12-29 18:44:51 +01:00
|
|
|
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) {
|
2023-07-21 18:02:55 +02:00
|
|
|
var gm *cglfw.Monitor
|
2018-12-29 18:44:51 +01:00
|
|
|
if monitor != nil {
|
|
|
|
gm = monitor.m
|
|
|
|
}
|
2023-07-21 18:02:55 +02:00
|
|
|
var gw *cglfw.Window
|
2018-12-29 18:44:51 +01:00
|
|
|
if share != nil {
|
|
|
|
gw = share.w
|
|
|
|
}
|
|
|
|
|
2023-07-21 18:02:55 +02:00
|
|
|
w, err := cglfw.CreateWindow(width, height, title, gm, gw)
|
2018-12-29 18:44:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return theWindows.add(w), nil
|
|
|
|
}
|
|
|
|
|
2022-08-13 06:33:52 +02:00
|
|
|
func GetKeyName(key Key, scancode int) string {
|
2023-07-21 18:02:55 +02:00
|
|
|
return cglfw.GetKeyName(cglfw.Key(key), scancode)
|
2022-08-13 06:33:52 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func GetMonitors() []*Monitor {
|
|
|
|
ms := []*Monitor{}
|
2023-07-21 18:02:55 +02:00
|
|
|
for _, m := range cglfw.GetMonitors() {
|
2018-12-29 18:44:51 +01:00
|
|
|
if m != nil {
|
|
|
|
ms = append(ms, &Monitor{m})
|
|
|
|
} else {
|
|
|
|
ms = append(ms, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPrimaryMonitor() *Monitor {
|
2023-07-21 18:02:55 +02:00
|
|
|
m := cglfw.GetPrimaryMonitor()
|
2018-12-29 18:44:51 +01:00
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &Monitor{m}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init() error {
|
2023-07-21 18:02:55 +02:00
|
|
|
return cglfw.Init()
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func PollEvents() {
|
2023-07-21 18:02:55 +02:00
|
|
|
cglfw.PollEvents()
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:28 +02:00
|
|
|
func PostEmptyEvent() {
|
2023-07-21 18:02:55 +02:00
|
|
|
cglfw.PostEmptyEvent()
|
2021-07-22 18:07:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-18 18:49:21 +02:00
|
|
|
func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback {
|
2023-07-21 18:02:55 +02:00
|
|
|
cglfw.SetMonitorCallback(cbfun)
|
2022-05-18 18:49:21 +02:00
|
|
|
return ToMonitorCallback(nil)
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func SwapInterval(interval int) {
|
2023-07-21 18:02:55 +02:00
|
|
|
cglfw.SwapInterval(interval)
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Terminate() {
|
2023-07-21 18:02:55 +02:00
|
|
|
cglfw.Terminate()
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2021-07-22 18:07:28 +02:00
|
|
|
func WaitEvents() {
|
2023-07-21 18:02:55 +02:00
|
|
|
cglfw.WaitEvents()
|
2021-07-22 18:07:28 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:21:24 +02:00
|
|
|
func WaitEventsTimeout(timeout float64) {
|
2023-07-21 18:02:55 +02:00
|
|
|
cglfw.WaitEventsTimeout(timeout)
|
2021-09-17 19:21:24 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 18:44:51 +01:00
|
|
|
func WindowHint(target Hint, hint int) {
|
2023-07-21 18:02:55 +02:00
|
|
|
cglfw.WindowHint(cglfw.Hint(target), hint)
|
2018-12-29 18:44:51 +01:00
|
|
|
}
|