Fixed window resizing after switching back from fullscreen mode

This commit is contained in:
IgorTimofeev 2022-09-20 16:08:32 +03:00
parent 0b1c83bc85
commit 6c0b108fa9

View File

@ -32,8 +32,6 @@ object UiHandler extends Logging {
private val hierarchy = mutable.ArrayBuffer(root: Widget)
private var shouldUpdateHierarchy = true
private val fpsCalculator = new FPSCalculator
private var _windowTitle = "Ocelot Desktop"
private var windowSize: Size2D = Size2D(0, 0)
private val ticker = new Ticker
private var _audioDisabled: Boolean = false
@ -86,42 +84,42 @@ object UiHandler extends Logging {
def isFullScreen: Boolean = Display.isFullscreen
def isFullScreen_=(value: Boolean): Unit = {
Settings.get.windowFullscreen = value
def isFullScreen_=(value: Boolean): Any = {
if (value) {
Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode)
}
else {
val settingsSize = Settings.get.windowSize
root.size = if (settingsSize.isSet) Size2D(settingsSize.x, settingsSize.y) else Size2D(800, 600)
Display.setDisplayMode(new DisplayMode(root.size.width.toInt, root.size.height.toInt))
if (Settings.get.windowPosition.isSet)
Display.setLocation(Settings.get.windowPosition.x, Settings.get.windowPosition.y)
}
Display.setFullscreen(value)
Display.setResizable(!value)
Settings.get.windowFullscreen = value
}
def init(root: RootWidget): Unit = {
this.root = root
root.relayout()
val previousSize = Settings.get.windowSize
root.size = if (!previousSize.isSet) Size2D(800, 600) else Size2D(previousSize.x, previousSize.y)
loadLibraries()
isFullScreen = Settings.get.windowFullscreen
windowTitle = "Ocelot Desktop"
if (Settings.get.windowPosition.isSet)
Display.setLocation(Settings.get.windowPosition.x, Settings.get.windowPosition.y)
Display.setTitle("Ocelot Desktop")
loadIcons()
Display.setResizable(true)
Display.setVSyncEnabled(true)
Display.create()
KeyEvents.init()
MouseEvents.init()
windowSize = root.size.copy()
logger.info(s"Created window with $windowSize")
logger.info(s"Created window with ${root.size}")
logger.info(s"OpenGL vendor: ${GL11.glGetString(GL11.GL_VENDOR)}")
logger.info(s"OpenGL renderer: ${GL11.glGetString(GL11.GL_RENDERER)}")
@ -169,9 +167,13 @@ object UiHandler extends Logging {
logger.debug("Unpacking native libraries to: " + nativeLibrariesDir)
for (lib <- libs) {
val dest = new File(Paths.get(nativeLibrariesDir, lib).toString)
if (!dest.exists()) {
val source = getClass.getResourceAsStream("/" + lib)
if (!dest.getParentFile.exists()) dest.getParentFile.mkdirs()
if (!dest.getParentFile.exists())
dest.getParentFile.mkdirs()
val output = new FileOutputStream(dest)
output.getChannel.transferFrom(Channels.newChannel(source), 0, Long.MaxValue)
output.flush()
@ -212,12 +214,9 @@ object UiHandler extends Logging {
logger.info(s"Loaded window icons of sizes ${sizes.mkString(", ")}")
}
def windowTitle: String = _windowTitle
def windowTitle: String = Display.getTitle
def windowTitle_=(title: String): Unit = {
Display.setTitle(title)
_windowTitle = title
}
def windowTitle_=(title: String): Unit = Display.setTitle(title)
private var exitRequested = false
@ -235,6 +234,7 @@ object UiHandler extends Logging {
OcelotDesktop.withTickLockAcquired(() => {
updateWindowSizeAndPosition()
KeyEvents.update()
MouseEvents.update()
@ -271,7 +271,7 @@ object UiHandler extends Logging {
if (shouldUpdateHierarchy) _updateHierarchy()
val mousePos = mousePosition
if (mousePos.x < 0 || mousePos.y < 0 || mousePos.x > windowSize.width || mousePos.y > windowSize.height) {
if (mousePos.x < 0 || mousePos.y < 0 || mousePos.x > root.width || mousePos.y > root.height) {
KeyEvents.releaseKeys()
MouseEvents.releaseButtons()
}
@ -313,15 +313,20 @@ object UiHandler extends Logging {
private def updateWindowSizeAndPosition(): Unit = {
val width = Display.getWidth
val height = Display.getHeight
windowSize = Size2D(width, height)
root.size = windowSize.copy()
graphics.resize(width, height)
root.size = Size2D(width, height)
// Settings fields should be updated only in non-fullscreen mode
if (isFullScreen)
return
Settings.get.windowSize.set(width, height)
Settings.get.windowPosition.set(Display.getX, Display.getY)
}
private def draw(): Unit = {
graphics.setViewport(windowSize.width.asInstanceOf[Int], windowSize.height.asInstanceOf[Int])
graphics.setViewport(root.width.asInstanceOf[Int], root.height.asInstanceOf[Int])
graphics.clear()
root.draw(graphics)