Skip to content

Commit

Permalink
Fixed an issue where mouse input was not properly triggering lua even…
Browse files Browse the repository at this point in the history
…ts for the UI

Added Checkbox.luau
Changed Slider.luau to be consistent with Checkbox.luau
  • Loading branch information
NixAJ committed Sep 17, 2024
1 parent d40f536 commit bd22f57
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 24 deletions.
26 changes: 18 additions & 8 deletions Source/Game-Lib/Game-Lib/ECS/Systems/UI/HandleInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,23 @@ namespace ECS::Systems::UI
}

u32 templateHash = eventInputInfo->onClickTemplateHash;
i32 inputEvent = eventInputInfo->onMouseDownEvent;
i32 inputDownEvent = eventInputInfo->onMouseDownEvent;
i32 inputUpEvent = eventInputInfo->onMouseUpEvent;
i32 heldEvent = eventInputInfo->onMouseHeldEvent;

if (templateHash != 0 || inputEvent != -1 || heldEvent != -1)
if (templateHash != 0 || inputDownEvent != -1 || inputUpEvent != -1 || heldEvent != -1)
{
eventInputInfo->isClicked = true;
ECS::Util::UI::RefreshTemplate(&registry, entity, *eventInputInfo);

if (inputEvent != -1)
if (templateHash != 0)
{
ECS::Util::UI::RefreshTemplate(&registry, entity, *eventInputInfo);
}

if (inputDownEvent != -1)
{
auto& widget = registry.get<Components::UI::Widget>(entity);
CallLuaEvent(inputEvent, Scripting::UI::UIInputEvents::MouseDown, widget.scriptWidget);
CallLuaEvent(inputDownEvent, Scripting::UI::UIInputEvents::MouseDown, widget.scriptWidget);
}

uiSingleton.clickedEntity = entity;
Expand All @@ -127,10 +132,15 @@ namespace ECS::Systems::UI
eventInputInfo->isClicked = false;
ECS::Util::UI::RefreshTemplate(&registry, uiSingleton.clickedEntity, *eventInputInfo);

if (eventInputInfo->onMouseUpEvent != -1 && uiSingleton.clickedEntity == uiSingleton.hoveredEntity)
if (eventInputInfo->onMouseUpEvent != -1)
{
auto& widget = registry.get<Components::UI::Widget>(uiSingleton.clickedEntity);
CallLuaEvent(eventInputInfo->onMouseUpEvent, Scripting::UI::UIInputEvents::MouseUp, widget.scriptWidget);
auto& rect = registry.get<Components::UI::BoundingRect>(uiSingleton.clickedEntity);
bool isWithin = IsWithin(mousePos, rect.min, rect.max);
if (isWithin)
{
auto& widget = registry.get<Components::UI::Widget>(uiSingleton.clickedEntity);
CallLuaEvent(eventInputInfo->onMouseUpEvent, Scripting::UI::UIInputEvents::MouseUp, widget.scriptWidget);
}
}
}

Expand Down
78 changes: 78 additions & 0 deletions Source/Scripts/API/UI/Checkbox.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
local Module = { }

-- TODO : FIX ME
--[[
usage:
.NewSlider(panel, 0, 0, 200, 25, 0, {
backgroundTemplate = "DefaultSliderBackground",
fillTemplate = "DefaultSliderFill",
})
]]--

local function SetupCheckboxMethods(checkboxTable)
checkboxTable.IsChecked = function(table)
return table.isChecked
end

checkboxTable.SetChecked = function(table, state)
if (checkboxTable.isChecked == state) then
return
end

local callback = table.onValueChanged
if (callback ~= nil) then
local result = callback(table, state)
if (result == false) then
return
end
end

checkboxTable.isChecked = state
checkboxTable.fill:SetVisible(state)
end

checkboxTable.SetOnMouseDown = function(table, func)
table.background:SetOnMouseDown(func)
table.fill:SetOnMouseDown(func)
end

checkboxTable.SetOnMouseUp = function(table, func)
table.background:SetOnMouseUp(func)
table.fill:SetOnMouseUp(func)
end

checkboxTable.Click = function(...)
local isChecked = not checkboxTable.isChecked
checkboxTable:SetChecked(isChecked)
end

checkboxTable.SetOnValueChanged = function(table, callback)
table.onValueChanged = callback
end

checkboxTable:SetOnMouseUp(checkboxTable.Click)

return checkboxTable
end

function Module.NewCheckbox(parent, posX, posY, sizeX, sizeY, layer, checkboxTemplateTable)
local checkboxTable = {}

checkboxTable.checkbox = parent:NewWidget(posX, posY, layer);
checkboxTable.isChecked = false
checkboxTable.onValueChanged = nil

local backgroundTemplate = checkboxTemplateTable["backgroundTemplate"];
local fillTemplate = checkboxTemplateTable["fillTemplate"];

checkboxTable.background = checkboxTable.checkbox:NewPanel(0, 0, sizeX, sizeY, 0, backgroundTemplate);
checkboxTable.fill = checkboxTable.checkbox:NewPanel(0, -2, sizeX, sizeY, 0, fillTemplate);
checkboxTable.fill:SetAnchor(0.5, 0.5)
checkboxTable.fill:SetRelativePoint(0.5, 1.0)
checkboxTable.fill:SetVisible(false)
checkboxTable = SetupCheckboxMethods(checkboxTable);

return checkboxTable;
end

return Module
21 changes: 10 additions & 11 deletions Source/Scripts/API/UI/Slider.luau
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,39 @@ usage:
})
]]--

local function SetupSliderMethods(slider)

slider.SetProgress = function(table, progress)
local function SetupSliderMethods(sliderTable)
sliderTable.SetProgress = function(table, progress)
local sizeX = table.fillSizeX * progress;
table.fill:SetWidth(sizeX);
end

slider.SetOnMouseDown = function(table, func)
sliderTable.SetOnMouseDown = function(table, func)
table.background:SetOnMouseDown(func);
table.fill:SetOnMouseDown(func);
end

slider.SetOnMouseUp = function(table, func)
sliderTable.SetOnMouseUp = function(table, func)
table.background:SetOnMouseUp(func);
table.fill:SetOnMouseUp(func);
end

slider.SetOnMouseHeld = function(table, func)
sliderTable.SetOnMouseHeld = function(table, func)
table.background:SetOnMouseHeld(func);
table.fill:SetOnMouseHeld(func);
end

slider:SetOnMouseHeld(function(eventID, widget, mouseX, mouseY)
local fillPosX = slider.fill:GetWorldPosX();
sliderTable:SetOnMouseHeld(function(eventID, widget, mouseX, mouseY)
local fillPosX = sliderTable.fill:GetWorldPosX();

local fillDiff = mouseX - fillPosX;
local fillSize = slider.fillSizeX;
local fillSize = sliderTable.fillSizeX;
local progress = fillDiff / fillSize;

progress = math.clamp(progress, 0.0, 1.0);
slider:SetProgress(progress);
sliderTable:SetProgress(progress);
end);

return slider
return sliderTable
end

function Module.NewSlider(parent, posX, posY, sizeX, sizeY, layer, sliderTemplateTable)
Expand Down
71 changes: 71 additions & 0 deletions Source/Scripts/API/UI/Templates.luau
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,45 @@ function Module.RegisterTemplates()
textTemplate = "DefaultButtonText",
});

-- Minimzie Button

local minimizeButtonMinX, minimizeButtonMinY = UI.PixelsToTexCoord(6, 7, 32, 32);
local minimizeButtonMaxX, minimizeButtonMaxY = UI.PixelsToTexCoord(24, 24, 32, 32);

UI.RegisterPanelTemplate("DefaultButtonMinimizePanel",
{
background = "Data/Texture/interface/buttons/ui-panel-minimizebutton-up.dds",
color = vec3.new(1.0, 1.0, 1.0), -- TODO: Color class
cornerRadius = 0.0,
texCoords = Box.new(minimizeButtonMinX, minimizeButtonMinY, minimizeButtonMaxX, minimizeButtonMaxY),

-- These get applied additively on this template
onClickTemplate = "DefaultButtonMinimizeDownPanel",
onHoverTemplate = "DefaultButtonMinimizeHoveredPanel",
onUninteractableTemplate = "DefaultButtonMinimizeDisabledPanel",
});

UI.RegisterPanelTemplate("DefaultButtonMinimizeDownPanel",
{
background = "Data/Texture/interface/buttons/ui-panel-minimizebutton-down.dds",
});

UI.RegisterPanelTemplate("DefaultButtonMinimizeHoveredPanel",
{
foreground = "Data/Texture/interface/buttons/ui-panel-minimizebutton-highlight.dds",
});

UI.RegisterPanelTemplate("DefaultButtonMinimizeDisabledPanel",
{
background = "Data/Texture/interface/buttons/ui-panel-minimizebutton-disabled.dds",
});

UI.RegisterButtonTemplate("DefaultButtonMinimize",
{
panelTemplate = "DefaultButtonMinimizePanel",
textTemplate = "DefaultButtonText",
});

-- Slider
local sliderMinX, sliderMinY = UI.PixelsToTexCoord(11, 11, 64, 64);
local sliderMaxX, sliderMaxY = UI.PixelsToTexCoord(51, 51, 64, 64);
Expand All @@ -125,6 +164,38 @@ function Module.RegisterTemplates()
cornerRadius = 2.0,
});

-- Checkbox
local checkboxMinX, checkboxMinY = UI.PixelsToTexCoord(0, 0, 64, 32);
local checkboxMaxX, checkboxMaxY = UI.PixelsToTexCoord(63, 22, 64, 32);

local checkboxNineSliceMinX, checkboxSliceMinY = UI.PixelsToTexCoord(3, 3, 64, 32);
local checkboxNineSliceMaxX, checkboxSliceMaxY = UI.PixelsToTexCoord(61, 20, 64, 32);

UI.RegisterPanelTemplate("DefaultCheckboxBackground",
{
background = "Data/Texture/interface/common/common-checkbox-background.dds",
texCoords = Box.new(checkboxMinX, checkboxMinY, checkboxMaxX, checkboxMaxY),
nineSliceCoords = Box.new(checkboxNineSliceMinX, checkboxSliceMinY, checkboxNineSliceMaxX, checkboxSliceMaxY),
});

UI.RegisterPanelTemplate("DefaultCheckboxFill",
{
background = "Data/Texture/interface/buttons/ui-checkbox-check.dds",
color = vec3.new(1.0, 1.0, 1.0),
cornerRadius = 2.0,

onUninteractableTemplate = "DefaultCheckboxDisabledFill",
});

UI.RegisterPanelTemplate("DefaultCheckboxHoveredPanel",
{
foreground = "Data/Texture/interface/buttons/checkbuttonhilight.dds",
});

UI.RegisterPanelTemplate("DefaultCheckboxDisabledFill",
{
background = "Data/Texture/interface/buttons/ui-checkbox-check-disabled.dds",
});

-- Menu Panel

Expand Down
20 changes: 16 additions & 4 deletions Source/Scripts/UI/Demo.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local UITemplates = require("API/UI/Templates")
local UIStack = require("API/UI/UIStack")
local UISlider = require("API/UI/Slider")
local UICheckbox = require("API/UI/Checkbox")

-- TODO: Borders :(

Expand Down Expand Up @@ -128,10 +129,10 @@ function CreateOptionsMenu(stack)
headerText:SetRelativePoint(0.5, 1.0);

-- Create buttons
local exitButton = panel:NewButton(2, 2, 40, 40, 0, "DefaultButton");
local exitButton = panel:NewButton(2, 2, 24, 24, 0, "DefaultButtonMinimize");
exitButton:SetAnchor(1.0, 1.0);
exitButton:SetRelativePoint(1.0, 1.0);
exitButton:SetText("X");
exitButton:SetText(".");
exitButton:SetOnMouseUp(function(eventID, widget, ...)
local stack = UIStack.Get("Menu");
stack:Push("GameMenu");
Expand All @@ -151,9 +152,20 @@ function CreateOptionsMenu(stack)
--volumeSliderText:SetRelativePoint(1.0, 0.5);
--volumeSliderText:SetPos(-20, 0);


local testCheckbox = UICheckbox.NewCheckbox(panel, 0, -80, 24, 24, 0,
{
backgroundTemplate = "DefaultCheckboxBackground",
fillTemplate = "DefaultCheckboxFill",
});
testCheckbox.checkbox:SetAnchor(0.5, 1.0);
testCheckbox.background:SetRelativePoint(0.5, 1.0);

testCheckbox:SetOnValueChanged(function(checkboxTable, state)
return true
end)

stack:Register("OptionsMenu", panel, 0); -- Name, widget, slot, onPush, onPop
--stack:Push("OptionsMenu")
stack:Push("OptionsMenu")
end

local function OnGameLoaded(eventID : number, motd : string)
Expand Down
2 changes: 1 addition & 1 deletion Submodules/Engine

0 comments on commit bd22f57

Please sign in to comment.