window.go (5472B)
1 // Copyright 2019 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 darwin freebsd linux windows 16 // +build !android 17 // +build !ios 18 19 package glfw 20 21 import ( 22 "image" 23 24 "github.com/hajimehoshi/ebiten/v2/internal/glfw" 25 ) 26 27 type window struct { 28 ui *UserInterface 29 setPositionCalled bool 30 } 31 32 func (w *window) IsDecorated() bool { 33 if !w.ui.isRunning() { 34 return w.ui.isInitWindowDecorated() 35 } 36 v := false 37 _ = w.ui.t.Call(func() error { 38 v = w.ui.window.GetAttrib(glfw.Decorated) == glfw.True 39 return nil 40 }) 41 return v 42 } 43 44 func (w *window) SetDecorated(decorated bool) { 45 if !w.ui.isRunning() { 46 w.ui.setInitWindowDecorated(decorated) 47 return 48 } 49 50 _ = w.ui.t.Call(func() error { 51 v := glfw.False 52 if decorated { 53 v = glfw.True 54 } 55 w.ui.window.SetAttrib(glfw.Decorated, v) 56 57 // The title can be lost when the decoration is gone. Recover this. 58 if v == glfw.True { 59 w.ui.window.SetTitle(w.ui.title) 60 } 61 return nil 62 }) 63 } 64 65 func (w *window) IsResizable() bool { 66 if !w.ui.isRunning() { 67 return w.ui.isInitWindowResizable() 68 } 69 v := false 70 _ = w.ui.t.Call(func() error { 71 v = w.ui.window.GetAttrib(glfw.Resizable) == glfw.True 72 return nil 73 }) 74 return v 75 } 76 77 func (w *window) SetResizable(resizable bool) { 78 if !w.ui.isRunning() { 79 w.ui.setInitWindowResizable(resizable) 80 return 81 } 82 _ = w.ui.t.Call(func() error { 83 v := glfw.False 84 if resizable { 85 v = glfw.True 86 } 87 w.ui.window.SetAttrib(glfw.Resizable, v) 88 return nil 89 }) 90 } 91 92 func (w *window) IsFloating() bool { 93 if !w.ui.isRunning() { 94 return w.ui.isInitWindowFloating() 95 } 96 var v bool 97 _ = w.ui.t.Call(func() error { 98 v = w.ui.window.GetAttrib(glfw.Floating) == glfw.True 99 return nil 100 }) 101 return v 102 } 103 104 func (w *window) SetFloating(floating bool) { 105 if !w.ui.isRunning() { 106 w.ui.setInitWindowFloating(floating) 107 return 108 } 109 _ = w.ui.t.Call(func() error { 110 v := glfw.False 111 if floating { 112 v = glfw.True 113 } 114 w.ui.window.SetAttrib(glfw.Floating, v) 115 return nil 116 }) 117 } 118 119 func (w *window) IsMaximized() bool { 120 if !w.ui.isRunning() { 121 return w.ui.isInitWindowMaximized() 122 } 123 var v bool 124 _ = w.ui.t.Call(func() error { 125 v = w.ui.window.GetAttrib(glfw.Maximized) == glfw.True 126 return nil 127 }) 128 return v 129 } 130 131 func (w *window) Maximize() { 132 if !w.IsResizable() { 133 panic("glfw: a window to maximize must be resizable") 134 } 135 if !w.ui.isRunning() { 136 w.ui.setInitWindowMaximized(true) 137 return 138 } 139 _ = w.ui.t.Call(func() error { 140 w.ui.window.Maximize() 141 return nil 142 }) 143 } 144 145 func (w *window) IsMinimized() bool { 146 if !w.ui.isRunning() { 147 return false 148 } 149 var v bool 150 _ = w.ui.t.Call(func() error { 151 v = w.ui.window.GetAttrib(glfw.Iconified) == glfw.True 152 return nil 153 }) 154 return v 155 } 156 157 func (w *window) Minimize() { 158 if !w.ui.isRunning() { 159 // Do nothing 160 return 161 } 162 _ = w.ui.t.Call(func() error { 163 w.ui.window.Iconify() 164 return nil 165 }) 166 } 167 168 func (w *window) Restore() { 169 if !w.ui.isRunning() { 170 // Do nothing 171 return 172 } 173 _ = w.ui.t.Call(func() error { 174 w.ui.window.Restore() 175 return nil 176 }) 177 } 178 179 func (w *window) Position() (int, int) { 180 if !w.ui.isRunning() { 181 panic("glfw: WindowPosition can't be called before the main loop starts") 182 } 183 x, y := 0, 0 184 _ = w.ui.t.Call(func() error { 185 var wx, wy int 186 if w.ui.isFullscreen() { 187 wx, wy = w.ui.origPosX, w.ui.origPosY 188 } else { 189 wx, wy = w.ui.window.GetPos() 190 } 191 mx, my := currentMonitor(w.ui.window).GetPos() 192 wx -= mx 193 wy -= my 194 xf := w.ui.fromGLFWPixel(float64(wx)) 195 yf := w.ui.fromGLFWPixel(float64(wy)) 196 x, y = int(xf), int(yf) 197 return nil 198 }) 199 return x, y 200 } 201 202 func (w *window) SetPosition(x, y int) { 203 if !w.ui.isRunning() { 204 w.ui.setInitWindowPosition(x, y) 205 return 206 } 207 _ = w.ui.t.Call(func() error { 208 defer func() { 209 w.setPositionCalled = true 210 }() 211 mx, my := currentMonitor(w.ui.window).GetPos() 212 xf := w.ui.toGLFWPixel(float64(x)) 213 yf := w.ui.toGLFWPixel(float64(y)) 214 x, y := w.ui.adjustWindowPosition(mx+int(xf), my+int(yf)) 215 if w.ui.isFullscreen() { 216 w.ui.origPosX, w.ui.origPosY = x, y 217 } else { 218 w.ui.window.SetPos(x, y) 219 } 220 return nil 221 }) 222 } 223 224 func (w *window) Size() (int, int) { 225 if !w.ui.isRunning() { 226 return w.ui.getInitWindowSize() 227 } 228 ww := int(w.ui.fromGLFWPixel(float64(w.ui.windowWidth))) 229 wh := int(w.ui.fromGLFWPixel(float64(w.ui.windowHeight))) 230 return ww, wh 231 } 232 233 func (w *window) SetSize(width, height int) { 234 if !w.ui.isRunning() { 235 w.ui.setInitWindowSize(width, height) 236 return 237 } 238 ww := int(w.ui.toGLFWPixel(float64(width))) 239 wh := int(w.ui.toGLFWPixel(float64(height))) 240 w.ui.setWindowSize(ww, wh, w.ui.isFullscreen()) 241 } 242 243 func (w *window) SetIcon(iconImages []image.Image) { 244 if !w.ui.isRunning() { 245 w.ui.setInitIconImages(iconImages) 246 return 247 } 248 _ = w.ui.t.Call(func() error { 249 w.ui.window.SetIcon(iconImages) 250 return nil 251 }) 252 } 253 254 func (w *window) SetTitle(title string) { 255 if !w.ui.isRunning() { 256 w.ui.setInitTitle(title) 257 return 258 } 259 w.ui.title = title 260 _ = w.ui.t.Call(func() error { 261 w.ui.window.SetTitle(title) 262 return nil 263 }) 264 }