Skip to content

Commit

Permalink
[Window]
Browse files Browse the repository at this point in the history
Support for displaying a close button on windows, similar to behavior found in Dear ImGui. This will address Issue 13 found at:
#13.

The background hover color and foreground color have been added to the Style table. SlabTest has also been modified to reflect this new change for the main window. Changes will be propagated to SlabDebug in upcoming commits.
  • Loading branch information
coding-jackalope committed Apr 12, 2020
1 parent 87231b8 commit c864673
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
2 changes: 2 additions & 0 deletions API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ end
be: NW, NE, SW, SE, N, S, E, W
CanObstruct: [Boolean] Sets whether this window is considered for obstruction of other windows and their controls. The default value is true.
Rounding: [Number] Amount of rounding to apply to the corners of the window.
IsOpen: [Boolean] Determines if the window is open. If this value exists within the options, a close button will appear in
the corner of the window and is updated when this button is pressed to reflect the new open state of this window.
Return: None
--]]
Expand Down
8 changes: 7 additions & 1 deletion Internal/Core/DrawCommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,14 @@ function DrawCommands.Begin(Options)
table.insert(PendingBatches, 1, ActiveBatch)
end

function DrawCommands.End()
function DrawCommands.End(ClearElements)
ClearElements = ClearElements == nil and false or ClearElements

if ActiveBatch ~= nil then
if ClearElements then
ActiveBatch.Elements = {}
end

love.graphics.setScissor()
table.remove(PendingBatches, 1)

Expand Down
47 changes: 46 additions & 1 deletion Internal/UI/Window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ local function NewInstance(Id)
Instance.LastCursorY = 0
Instance.StatHandle = nil
Instance.IsAppearing = false
Instance.IsOpen = true
return Instance
end

Expand Down Expand Up @@ -339,6 +340,10 @@ function Window.IsObstructed(X, Y, SkipScrollCheck)
if ActiveInstance ~= nil then
local FoundStackLock = false

if not ActiveInstance.IsOpen then
return true
end

for I, V in ipairs(Stack) do
if V.Id == StackLockId then
FoundStackLock = true
Expand Down Expand Up @@ -473,6 +478,12 @@ function Window.Begin(Id, Options)
ActiveInstance.FrameNumber = CurrentFrameNumber
ActiveInstance.StatHandle = StatHandle

local ShowClose = false
if Options.IsOpen ~= nil and type(Options.IsOpen) == 'boolean' then
ActiveInstance.IsOpen = Options.IsOpen
ShowClose = true
end

if ActiveInstance.StackIndex == 0 then
table.insert(Stack, 1, ActiveInstance)
UpdateStackIndex()
Expand Down Expand Up @@ -532,6 +543,40 @@ function Window.Begin(Id, Options)
MouseY = MouseY
})
DrawCommands.Print(ActiveInstance.Title, TitleX, math.floor(ActiveInstance.Y - OffsetY), Style.TextColor, Style.Font)

if ShowClose then
local CloseBgRadius = OffsetY * 0.4
local CloseSize = CloseBgRadius * 0.5
local CloseX = ActiveInstance.X + ActiveInstance.W - ActiveInstance.Border - CloseBgRadius
local CloseY = ActiveInstance.Y - OffsetY * 0.5
local IsCloseHovered =
CloseX - CloseBgRadius <= MouseX and MouseX <= CloseX + CloseBgRadius and
CloseY - OffsetY * 0.5 <= MouseY and MouseY <= CloseY + CloseBgRadius and
not IsObstructed

if IsCloseHovered then
DrawCommands.Circle(
'fill',
CloseX,
CloseY,
CloseBgRadius,
Style.WindowCloseBgColor
)

if Mouse.IsClicked(1) then
ActiveInstance.IsOpen = false
Options.IsOpen = false
end
end

DrawCommands.Cross(
CloseX,
CloseY,
CloseSize,
Style.WindowCloseColor
)
end

Region.End()
end

Expand Down Expand Up @@ -566,7 +611,7 @@ function Window.End()
if ActiveInstance ~= nil then
local Handle = ActiveInstance.StatHandle
Region.End()
DrawCommands.End()
DrawCommands.End(not ActiveInstance.IsOpen)
table.remove(PendingStack, 1)

Cursor.SetPosition(ActiveInstance.LastCursorX, ActiveInstance.LastCursorY)
Expand Down
34 changes: 16 additions & 18 deletions SlabTest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1978,13 +1978,13 @@ local function DrawFonts()
Slab.Text("This text control is using the default font.")
end

local DrawSlabTest = true
local SlabTest_Options = {Title = "Slab", AutoSizeWindow = false, W = 800.0, H = 600.0, IsOpen = true}

function SlabTest.MainMenuBar()
if Slab.BeginMainMenuBar() then
if Slab.BeginMenu("File") then
if Slab.MenuItemChecked("Slab Test", DrawSlabTest) then
DrawSlabTest = not DrawSlabTest
if Slab.MenuItemChecked("Show Test Window", SlabTest_Options.IsOpen) then
SlabTest_Options.IsOpen = not SlabTest_Options.IsOpen
end

if Slab.MenuItem("Quit") then
Expand Down Expand Up @@ -2034,30 +2034,28 @@ function SlabTest.Begin()
Selected = Categories[1]
end

if DrawSlabTest then
Slab.BeginWindow('SlabTest', {Title = "Slab", AutoSizeWindow = false, W = 800.0, H = 600.0})
Slab.BeginWindow('SlabTest', SlabTest_Options)

local W, H = Slab.GetWindowActiveSize()
local W, H = Slab.GetWindowActiveSize()

if Slab.BeginComboBox('Categories', {Selected = Selected[1], W = W}) then
for I, V in ipairs(Categories) do
if Slab.TextSelectable(V[1]) then
Selected = Categories[I]
end
if Slab.BeginComboBox('Categories', {Selected = Selected[1], W = W}) then
for I, V in ipairs(Categories) do
if Slab.TextSelectable(V[1]) then
Selected = Categories[I]
end

Slab.EndComboBox()
end

Slab.Separator()
Slab.EndComboBox()
end

if Selected ~= nil and Selected[2] ~= nil then
Selected[2]()
end
Slab.Separator()

Slab.EndWindow()
if Selected ~= nil and Selected[2] ~= nil then
Selected[2]()
end

Slab.EndWindow()

SlabDebug.Begin()

Slab.EndStat(StatHandle)
Expand Down
2 changes: 2 additions & 0 deletions Style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ local Style =
SeparatorColor = {0.5, 0.5, 0.5, 0.7},
WindowBackgroundColor = {0.2, 0.2, 0.2, 1.0},
WindowTitleFocusedColor = {0.26, 0.53, 0.96, 1.0},
WindowCloseBgColor = {0.64, 0.64, 0.64, 1.0},
WindowCloseColor = {0.0, 0.0, 0.0, 1.0},
ButtonColor = {0.55, 0.55, 0.55, 1.0},
RadioButtonSelectedColor = {0.2, 0.2, 0.2, 1.0},
ButtonHoveredColor = {0.7, 0.7, 0.7, 1.0},
Expand Down
11 changes: 11 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
v0.6.3 RELEASE
==========
[API]: Update version number.
[API]: Investigate IsVoidHovered within hidden windows.
[FileSystem]: Test file system on Linux.
[FileSystem]: Investigate crash when selecting an existing file in Save Dialog.
[Optimization]: Investigate Pull request and integrate.
[ScrollBar]: Allow modifying wheel scroll speed through the API.
[Style]: Update style documentation for Close button.
[Window]: Update Wiki with IsOpen documentation.

v0.7.0 RELEASE
==========
[API]: Update version number.
Expand Down

0 comments on commit c864673

Please sign in to comment.