flamendless / Slab

An immediate mode GUI for the Love2D framework.
MIT License
289 stars 25 forks source link

Programmatically move a window #72

Closed sharpobject closed 3 years ago

sharpobject commented 3 years ago

Hello,

I tried out the example and noticed that it was easy for the window to end up out of the viewport. So I wanted to make it so that the window was clamped inside a viewport, and to do that I wanted to programmatically move the window. Is there a recommended way to do this?

coding-jackalope commented 3 years ago

Hi @sharpobject, you do have control over the placement of the window programmatically through the 'X' and 'Y' options you pass into the 'BeginWindow' function. Passing NoMove = true with the options will disable movement of the window. More information can be found here.

local X, Y = 50, 50
Slab.BeginWindow('MyWindow', {Title = "My Window", X = X, Y = Y, NoMove = true})
Slab.EndWindow()

Unfortunately, Slab does not support constraining the window to the viewport at the moment. I looked into how it can be done quickly, but you may need to dive into the Slab internals to get this to work. I can add an option to constrain the window to the viewport in the next update.

The basic gist of how window movement works in Slab is that it records any delta changes when a window is dragged. This delta is then applied to the requested window position on the next frame. The main logic for how a window position is updated is done in the UpdateTitleBar function in the Window.lua module. I would start there if you decide to jump in and try to look into this.