glfw_notwindows.go (6663B)
1 // Copyright 2018 The Ebiten Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !windows 16 // +build !js 17 18 package glfw 19 20 import ( 21 "image" 22 "sync" 23 24 "github.com/go-gl/glfw/v3.3/glfw" 25 ) 26 27 type windows map[*glfw.Window]*Window 28 29 var ( 30 theWindows = windows{} 31 windowsM sync.Mutex 32 ) 33 34 func (w windows) add(win *glfw.Window) *Window { 35 if win == nil { 36 return nil 37 } 38 ww := &Window{win} 39 windowsM.Lock() 40 w[win] = ww 41 windowsM.Unlock() 42 return ww 43 } 44 45 func (w windows) remove(win *glfw.Window) { 46 windowsM.Lock() 47 delete(w, win) 48 windowsM.Unlock() 49 } 50 51 func (w windows) get(win *glfw.Window) *Window { 52 if win == nil { 53 return nil 54 } 55 windowsM.Lock() 56 ww := w[win] 57 windowsM.Unlock() 58 return ww 59 } 60 61 type Monitor struct { 62 m *glfw.Monitor 63 } 64 65 func (m *Monitor) GetContentScale() (float32, float32) { 66 return m.m.GetContentScale() 67 } 68 69 func (m *Monitor) GetPos() (x, y int) { 70 return m.m.GetPos() 71 } 72 73 func (m *Monitor) GetVideoMode() *VidMode { 74 v := m.m.GetVideoMode() 75 if v == nil { 76 return nil 77 } 78 return &VidMode{ 79 Width: v.Width, 80 Height: v.Height, 81 RedBits: v.RedBits, 82 GreenBits: v.GreenBits, 83 BlueBits: v.BlueBits, 84 RefreshRate: v.RefreshRate, 85 } 86 } 87 88 type Window struct { 89 w *glfw.Window 90 } 91 92 func (w *Window) Destroy() { 93 w.w.Destroy() 94 theWindows.remove(w.w) 95 } 96 97 func (w *Window) GetAttrib(attrib Hint) int { 98 return w.w.GetAttrib(glfw.Hint(attrib)) 99 } 100 101 func (w *Window) SetAttrib(attrib Hint, value int) { 102 w.w.SetAttrib(glfw.Hint(attrib), value) 103 } 104 105 func (w *Window) GetCursorPos() (x, y float64) { 106 return w.w.GetCursorPos() 107 } 108 109 func (w *Window) GetInputMode(mode InputMode) int { 110 return w.w.GetInputMode(glfw.InputMode(mode)) 111 } 112 113 func (w *Window) GetKey(key Key) Action { 114 return Action(w.w.GetKey(glfw.Key(key))) 115 } 116 117 func (w *Window) GetMonitor() *Monitor { 118 m := w.w.GetMonitor() 119 if m == nil { 120 return nil 121 } 122 return &Monitor{m} 123 } 124 125 func (w *Window) GetMouseButton(button MouseButton) Action { 126 return Action(w.w.GetMouseButton(glfw.MouseButton(button))) 127 } 128 129 func (w *Window) GetPos() (x, y int) { 130 return w.w.GetPos() 131 } 132 133 func (w *Window) GetSize() (width, height int) { 134 return w.w.GetSize() 135 } 136 137 func (w *Window) Iconify() { 138 w.w.Iconify() 139 } 140 141 func (w *Window) MakeContextCurrent() { 142 w.w.MakeContextCurrent() 143 } 144 145 func (w *Window) Maximize() { 146 w.w.Maximize() 147 } 148 149 func (w *Window) Restore() { 150 w.w.Restore() 151 } 152 153 func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback) { 154 var gcb glfw.CharModsCallback 155 if cbfun != nil { 156 gcb = func(window *glfw.Window, char rune, mods glfw.ModifierKey) { 157 cbfun(theWindows.get(window), char, ModifierKey(mods)) 158 } 159 } 160 w.w.SetCharModsCallback(gcb) 161 return nil // TODO 162 } 163 164 func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) { 165 var gcb glfw.FramebufferSizeCallback 166 if cbfun != nil { 167 gcb = func(window *glfw.Window, width int, height int) { 168 cbfun(theWindows.get(window), width, height) 169 } 170 } 171 w.w.SetFramebufferSizeCallback(gcb) 172 return nil // TODO 173 } 174 175 func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback) { 176 var gcb glfw.ScrollCallback 177 if cbfun != nil { 178 gcb = func(window *glfw.Window, xoff float64, yoff float64) { 179 cbfun(theWindows.get(window), xoff, yoff) 180 } 181 } 182 w.w.SetScrollCallback(gcb) 183 return nil // TODO 184 } 185 186 func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) { 187 var gcb glfw.SizeCallback 188 if cbfun != nil { 189 gcb = func(window *glfw.Window, width, height int) { 190 cbfun(theWindows.get(window), width, height) 191 } 192 } 193 w.w.SetSizeCallback(gcb) 194 return nil // TODO 195 } 196 197 func (w *Window) SetIcon(images []image.Image) { 198 w.w.SetIcon(images) 199 } 200 201 func (w *Window) SetInputMode(mode InputMode, value int) { 202 w.w.SetInputMode(glfw.InputMode(mode), value) 203 } 204 205 func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) { 206 var m *glfw.Monitor 207 if monitor != nil { 208 m = monitor.m 209 } 210 w.w.SetMonitor(m, xpos, ypos, width, height, refreshRate) 211 } 212 213 func (w *Window) SetPos(xpos, ypos int) { 214 w.w.SetPos(xpos, ypos) 215 } 216 217 func (w *Window) SetSize(width, height int) { 218 w.w.SetSize(width, height) 219 } 220 221 func (w *Window) SetTitle(title string) { 222 w.w.SetTitle(title) 223 } 224 225 func (w *Window) ShouldClose() bool { 226 return w.w.ShouldClose() 227 } 228 229 func (w *Window) Show() { 230 w.w.Show() 231 } 232 233 func (w *Window) SwapBuffers() { 234 w.w.SwapBuffers() 235 } 236 237 func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) { 238 var gm *glfw.Monitor 239 if monitor != nil { 240 gm = monitor.m 241 } 242 var gw *glfw.Window 243 if share != nil { 244 gw = share.w 245 } 246 247 w, err := glfw.CreateWindow(width, height, title, gm, gw) 248 if err != nil { 249 return nil, err 250 } 251 return theWindows.add(w), nil 252 } 253 254 func (j Joystick) GetGUID() string { 255 return glfw.Joystick(j).GetGUID() 256 } 257 258 func (j Joystick) GetName() string { 259 return glfw.Joystick(j).GetName() 260 } 261 262 func (j Joystick) GetAxes() []float32 { 263 return glfw.Joystick(j).GetAxes() 264 } 265 266 func (j Joystick) GetButtons() []Action { 267 var bs []Action 268 for _, b := range glfw.Joystick(j).GetButtons() { 269 bs = append(bs, Action(b)) 270 } 271 return bs 272 } 273 274 func GetMonitors() []*Monitor { 275 ms := []*Monitor{} 276 for _, m := range glfw.GetMonitors() { 277 if m != nil { 278 ms = append(ms, &Monitor{m}) 279 } else { 280 ms = append(ms, nil) 281 } 282 } 283 return ms 284 } 285 286 func GetPrimaryMonitor() *Monitor { 287 m := glfw.GetPrimaryMonitor() 288 if m == nil { 289 return nil 290 } 291 return &Monitor{m} 292 } 293 294 func Init() error { 295 return glfw.Init() 296 } 297 298 func (j Joystick) Present() bool { 299 return glfw.Joystick(j).Present() 300 } 301 302 func PollEvents() { 303 glfw.PollEvents() 304 } 305 306 func SetMonitorCallback(cbfun func(monitor *Monitor, event PeripheralEvent)) { 307 var gcb func(monitor *glfw.Monitor, event glfw.PeripheralEvent) 308 if cbfun != nil { 309 gcb = func(monitor *glfw.Monitor, event glfw.PeripheralEvent) { 310 var m *Monitor 311 if monitor != nil { 312 m = &Monitor{monitor} 313 } 314 cbfun(m, PeripheralEvent(event)) 315 } 316 } 317 glfw.SetMonitorCallback(gcb) 318 } 319 320 func SwapInterval(interval int) { 321 glfw.SwapInterval(interval) 322 } 323 324 func Terminate() { 325 glfw.Terminate() 326 } 327 328 func WindowHint(target Hint, hint int) { 329 glfw.WindowHint(glfw.Hint(target), hint) 330 }