zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

window.go (10557B)


      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 package ebiten
     16 
     17 import (
     18 	"image"
     19 	"sync/atomic"
     20 )
     21 
     22 const (
     23 	maxInt     = int(^uint(0) >> 1)
     24 	minInt     = -maxInt - 1
     25 	invalidPos = minInt
     26 )
     27 
     28 // IsWindowDecorated reports whether the window is decorated.
     29 //
     30 // IsWindowDecorated is concurrent-safe.
     31 func IsWindowDecorated() bool {
     32 	if w := uiDriver().Window(); w != nil {
     33 		return w.IsDecorated()
     34 	}
     35 	return false
     36 }
     37 
     38 // SetWindowDecorated sets the state if the window is decorated.
     39 //
     40 // The window is decorated by default.
     41 //
     42 // SetWindowDecorated works only on desktops.
     43 // SetWindowDecorated does nothing on other platforms.
     44 //
     45 // SetWindowDecorated does nothing on macOS when the window is fullscreened natively by the macOS desktop
     46 // instead of SetFullscreen(true).
     47 //
     48 // SetWindowDecorated is concurrent-safe.
     49 func SetWindowDecorated(decorated bool) {
     50 	if w := uiDriver().Window(); w != nil {
     51 		w.SetDecorated(decorated)
     52 	}
     53 }
     54 
     55 // IsWindowResizable reports whether the window is resizable by the user's dragging on desktops.
     56 // On the other environments, IsWindowResizable always returns false.
     57 //
     58 // IsWindowResizable is concurrent-safe.
     59 func IsWindowResizable() bool {
     60 	if w := uiDriver().Window(); w != nil {
     61 		return w.IsResizable()
     62 	}
     63 	return false
     64 }
     65 
     66 // SetWindowResizable sets whether the window is resizable by the user's dragging on desktops.
     67 // On the other environments, SetWindowResizable does nothing.
     68 //
     69 // The window is not resizable by default.
     70 //
     71 // If SetWindowResizable is called with true and Run is used, SetWindowResizable panics. Use RunGame instead.
     72 //
     73 // SetWindowResizable does nothing on macOS when the window is fullscreened natively by the macOS desktop
     74 // instead of SetFullscreen(true).
     75 //
     76 // SetWindowResizable is concurrent-safe.
     77 func SetWindowResizable(resizable bool) {
     78 	if w := uiDriver().Window(); w != nil {
     79 		w.SetResizable(resizable)
     80 	}
     81 }
     82 
     83 // SetWindowTitle sets the title of the window.
     84 //
     85 // SetWindowTitle does nothing on browsers or mobiles.
     86 //
     87 // SetWindowTitle is concurrent-safe.
     88 func SetWindowTitle(title string) {
     89 	if w := uiDriver().Window(); w != nil {
     90 		w.SetTitle(title)
     91 	}
     92 }
     93 
     94 // SetWindowIcon sets the icon of the game window.
     95 //
     96 // If len(iconImages) is 0, SetWindowIcon reverts the icon to the default one.
     97 //
     98 // For desktops, see the document of glfwSetWindowIcon of GLFW 3.2:
     99 //
    100 //     This function sets the icon of the specified window.
    101 //     If passed an array of candidate images, those of or closest to the sizes
    102 //     desired by the system are selected.
    103 //     If no images are specified, the window reverts to its default icon.
    104 //
    105 //     The desired image sizes varies depending on platform and system settings.
    106 //     The selected images will be rescaled as needed.
    107 //     Good sizes include 16x16, 32x32 and 48x48.
    108 //
    109 // As macOS windows don't have icons, SetWindowIcon doesn't work on macOS.
    110 //
    111 // SetWindowIcon doesn't work on browsers or mobiles.
    112 //
    113 // SetWindowIcon is concurrent-safe.
    114 func SetWindowIcon(iconImages []image.Image) {
    115 	if w := uiDriver().Window(); w != nil {
    116 		w.SetIcon(iconImages)
    117 	}
    118 }
    119 
    120 // WindowPosition returns the window position.
    121 // The origin position is the left-upper corner of the current monitor.
    122 // The unit is device-independent pixels.
    123 //
    124 // WindowPosition panics if the main loop does not start yet.
    125 //
    126 // WindowPosition returns the last window position in fullscreen mode.
    127 //
    128 // WindowPosition returns (0, 0) on browsers and mobiles.
    129 //
    130 // WindowPosition is concurrent-safe.
    131 func WindowPosition() (x, y int) {
    132 	if w := uiDriver().Window(); w != nil {
    133 		return w.Position()
    134 	}
    135 	return 0, 0
    136 }
    137 
    138 // SetWindowPosition sets the window position.
    139 // The origin position is the left-upper corner of the current monitor.
    140 // The unit is device-independent pixels.
    141 //
    142 // SetWindowPosition does nothing in fullscreen mode.
    143 //
    144 // SetWindowPosition does nothing on browsers and mobiles.
    145 //
    146 // SetWindowPosition is concurrent-safe.
    147 func SetWindowPosition(x, y int) {
    148 	atomic.StoreUint32(&windowPositionSetExplicitly, 1)
    149 	if w := uiDriver().Window(); w != nil {
    150 		w.SetPosition(x, y)
    151 	}
    152 }
    153 
    154 var (
    155 	windowPositionSetExplicitly uint32
    156 )
    157 
    158 func initializeWindowPositionIfNeeded(width, height int) {
    159 	w := uiDriver().Window()
    160 	if w == nil {
    161 		return
    162 	}
    163 
    164 	if atomic.LoadUint32(&windowPositionSetExplicitly) == 0 {
    165 		sw, sh := uiDriver().ScreenSizeInFullscreen()
    166 		x := (sw - width) / 2
    167 		y := (sh - height) / 3
    168 		w.SetPosition(x, y)
    169 	}
    170 }
    171 
    172 // WindowSize returns the window size on desktops.
    173 // WindowSize returns (0, 0) on other environments.
    174 //
    175 // In fullscreen mode, WindowSize returns the original window size.
    176 //
    177 // WindowSize is concurrent-safe.
    178 func WindowSize() (int, int) {
    179 	if w := uiDriver().Window(); w != nil {
    180 		return w.Size()
    181 	}
    182 	return 0, 0
    183 }
    184 
    185 // SetWindowSize sets the window size on desktops.
    186 // SetWindowSize does nothing on other environments.
    187 //
    188 // In fullscreen mode, SetWindowSize sets the original window size.
    189 //
    190 // SetWindowSize panics if width or height is not a positive number.
    191 //
    192 // SetWindowSize is concurrent-safe.
    193 func SetWindowSize(width, height int) {
    194 	if width <= 0 || height <= 0 {
    195 		panic("ebiten: width and height must be positive")
    196 	}
    197 	if w := uiDriver().Window(); w != nil {
    198 		w.SetSize(width, height)
    199 	}
    200 }
    201 
    202 // WindowSizeLimits returns the limitation of the window size on desktops.
    203 // A negative value indicates the size is not limited.
    204 //
    205 // WindowSizeLimits is concurrent-safe.
    206 func WindowSizeLimits() (minw, minh, maxw, maxh int) {
    207 	if w := uiDriver().Window(); w != nil {
    208 		return w.SizeLimits()
    209 	}
    210 	return -1, -1, -1, -1
    211 }
    212 
    213 // SetWindowSizeLimits sets the limitation of the window size on desktops.
    214 // A negative value indicates the size is not limited.
    215 //
    216 // SetWindowSizeLimits is concurrent-safe.
    217 func SetWindowSizeLimits(minw, minh, maxw, maxh int) {
    218 	if w := uiDriver().Window(); w != nil {
    219 		w.SetSizeLimits(minw, minh, maxw, maxh)
    220 	}
    221 }
    222 
    223 // IsWindowFloating reports whether the window is always shown above all the other windows.
    224 //
    225 // IsWindowFloating returns false on browsers and mobiles.
    226 //
    227 // IsWindowFloating is concurrent-safe.
    228 func IsWindowFloating() bool {
    229 	if w := uiDriver().Window(); w != nil {
    230 		return w.IsFloating()
    231 	}
    232 	return false
    233 }
    234 
    235 // SetWindowFloating sets the state whether the window is always shown above all the other windows.
    236 //
    237 // SetWindowFloating does nothing on browsers or mobiles.
    238 //
    239 // SetWindowFloating does nothing on macOS when the window is fullscreened natively by the macOS desktop
    240 // instead of SetFullscreen(true).
    241 //
    242 // SetWindowFloating is concurrent-safe.
    243 func SetWindowFloating(float bool) {
    244 	if w := uiDriver().Window(); w != nil {
    245 		w.SetFloating(float)
    246 	}
    247 }
    248 
    249 // MaximizeWindow maximizes the window.
    250 //
    251 // MaximizeWindow panics when the window is not resizable.
    252 //
    253 // MaximizeWindow does nothing on browsers or mobiles.
    254 //
    255 // MaximizeWindow is concurrent-safe.
    256 func MaximizeWindow() {
    257 	if !IsWindowResizable() {
    258 		panic("ebiten: a window to maximize must be resizable")
    259 	}
    260 	if w := uiDriver().Window(); w != nil {
    261 		w.Maximize()
    262 	}
    263 }
    264 
    265 // IsWindowMaximized reports whether the window is maximized or not.
    266 //
    267 // IsWindowMaximized returns false when the window is not resizable.
    268 //
    269 // IsWindowMaximized always returns false on browsers and mobiles.
    270 //
    271 // IsWindowMaximized is concurrent-safe.
    272 func IsWindowMaximized() bool {
    273 	if !IsWindowResizable() {
    274 		return false
    275 	}
    276 	if w := uiDriver().Window(); w != nil {
    277 		return w.IsMaximized()
    278 	}
    279 	return false
    280 }
    281 
    282 // MinimizeWindow minimizes the window.
    283 //
    284 // If the main loop does not start yet, MinimizeWindow does nothing.
    285 //
    286 // MinimizeWindow does nothing on browsers or mobiles.
    287 //
    288 // MinimizeWindow is concurrent-safe.
    289 func MinimizeWindow() {
    290 	if w := uiDriver().Window(); w != nil {
    291 		w.Minimize()
    292 	}
    293 }
    294 
    295 // IsWindowMinimized reports whether the window is minimized or not.
    296 //
    297 // IsWindowMinimized always returns false on browsers and mobiles.
    298 //
    299 // IsWindowMinimized is concurrent-safe.
    300 func IsWindowMinimized() bool {
    301 	if w := uiDriver().Window(); w != nil {
    302 		return w.IsMinimized()
    303 	}
    304 	return false
    305 }
    306 
    307 // RestoreWindow restores the window from its maximized or minimized state.
    308 //
    309 // RestoreWindow panics when the window is not maximized nor minimized.
    310 //
    311 // RestoreWindow is concurrent-safe.
    312 func RestoreWindow() {
    313 	if !IsWindowMaximized() && !IsWindowMinimized() {
    314 		panic("ebiten: RestoreWindow must be called on a maximized or a minimized window")
    315 	}
    316 	if w := uiDriver().Window(); w != nil {
    317 		w.Restore()
    318 	}
    319 }
    320 
    321 // IsWindowBeingClosed returns true when the user is trying to close the window on desktops.
    322 // As the window is closed immediately by default,
    323 // you might want to call SetWindowClosingHandled(true) to prevent the window is automatically closed.
    324 //
    325 // IsWindowBeingClosed always returns false on other platforms.
    326 //
    327 // IsWindowBeingClosed is concurrent-safe.
    328 func IsWindowBeingClosed() bool {
    329 	if w := uiDriver().Window(); w != nil {
    330 		return w.IsBeingClosed()
    331 	}
    332 	return false
    333 }
    334 
    335 // SetWindowClosingHandled sets whether the window closing is handled or not on desktops. The default state is false.
    336 //
    337 // If the window closing is handled, the window is not closed immediately and
    338 // the game can know whether the window is begin closed or not by IsWindowBeingClosed.
    339 // In this case, the window is not closed automatically.
    340 // To end the game, you have to return an error value at the Game's Update function.
    341 //
    342 // SetWindowClosingHandled works only on desktops.
    343 // SetWindowClosingHandled does nothing on other platforms.
    344 //
    345 // SetWindowClosingHandled is concurrent-safe.
    346 func SetWindowClosingHandled(handled bool) {
    347 	if w := uiDriver().Window(); w != nil {
    348 		w.SetClosingHandled(handled)
    349 	}
    350 }
    351 
    352 // IsWindowClosingHandled reports whether the window closing is handled or not on desktops by SetWindowClosingHandled.
    353 //
    354 // IsWindowClosingHandled always returns false on other platforms.
    355 //
    356 // IsWindowClosingHandled is concurrent-safe.
    357 func IsWindowClosingHandled() bool {
    358 	if w := uiDriver().Window(); w != nil {
    359 		return w.IsClosingHandled()
    360 	}
    361 	return false
    362 }