Skip to content

Commit

Permalink
Refactor UI buttons to be implemented in Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Pursche committed Oct 24, 2024
1 parent 2c4eb98 commit 1bf078f
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 360 deletions.
4 changes: 0 additions & 4 deletions Source/Game-Lib/Game-Lib/ECS/Singletons/UISingleton.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "Game-Lib/ECS/Components/UI/PanelTemplate.h"
#include "Game-Lib/ECS/Components/UI/TextTemplate.h"
#include "Game-Lib/UI/Templates.h"

#include <Base/Types.h>
#include <Base/Memory/StackAllocator.h>
Expand All @@ -20,9 +19,6 @@ namespace ECS::Singletons
robin_hood::unordered_map<u32, entt::entity> nameHashToCanvasEntity;

// Templates
std::vector <UI::ButtonTemplate> buttonTemplates;
robin_hood::unordered_map<u32, u32> templateHashToButtonTemplateIndex;

std::vector <ECS::Components::UI::PanelTemplate> panelTemplates;
robin_hood::unordered_map<u32, u32> templateHashToPanelTemplateIndex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ void CanvasRenderer::UpdateTextVertices(ECS::Components::Transform2D& transform,
{
textWidth = std::max(textWidth, penPos.x);
penPos.x = 0.0f;
penPos.y -= fontSize * metrics.lineHeight;
penPos.y -= fontSize * static_cast<f32>(metrics.lineHeight);
continue;
}

Expand Down
37 changes: 0 additions & 37 deletions Source/Game-Lib/Game-Lib/Scripting/Handlers/UIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "Game-Lib/Scripting/LuaState.h"
#include "Game-Lib/Scripting/LuaManager.h"
#include "Game-Lib/Scripting/Systems/LuaSystemBase.h"
#include "Game-Lib/Scripting/UI/Button.h"
#include "Game-Lib/Scripting/UI/Box.h"
#include "Game-Lib/Scripting/UI/Canvas.h"
#include "Game-Lib/Scripting/UI/Panel.h"
Expand All @@ -27,7 +26,6 @@ namespace Scripting::UI
{
static LuaMethod uiMethods[] =
{
{ "RegisterButtonTemplate", UIHandler::RegisterButtonTemplate },
{ "RegisterPanelTemplate", UIHandler::RegisterPanelTemplate },
{ "RegisterTextTemplate", UIHandler::RegisterTextTemplate },

Expand Down Expand Up @@ -55,7 +53,6 @@ namespace Scripting::UI

// Widgets
Widget::Register(state);
Button::Register(state);
Canvas::Register(state);
Panel::Register(state);
Text::Register(state);
Expand All @@ -76,40 +73,6 @@ namespace Scripting::UI
}

// UI
i32 UIHandler::RegisterButtonTemplate(lua_State* state)
{
LuaState ctx(state);

const char* templateName = ctx.Get(nullptr, 1);

const char* panelTemplate = nullptr;
if (ctx.GetTableField("panelTemplate", 2))
{
panelTemplate = ctx.Get(nullptr, 3);
ctx.Pop(1);
}

const char* textTemplate = nullptr;
if (ctx.GetTableField("textTemplate", 2))
{
textTemplate = ctx.Get(nullptr, 3);
ctx.Pop(1);
}

entt::registry* registry = ServiceLocator::GetEnttRegistries()->uiRegistry;
ECS::Singletons::UISingleton& uiSingleton = registry->ctx().get<ECS::Singletons::UISingleton>();

u32 buttonTemplateIndex = static_cast<u32>(uiSingleton.buttonTemplates.size());
auto& buttonTemplate = uiSingleton.buttonTemplates.emplace_back();
buttonTemplate.panelTemplate = panelTemplate;
buttonTemplate.textTemplate = textTemplate;

u32 templateNameHash = StringUtils::fnv1a_32(templateName, strlen(templateName));
uiSingleton.templateHashToButtonTemplateIndex[templateNameHash] = buttonTemplateIndex;

return 0;
}

i32 UIHandler::RegisterPanelTemplate(lua_State* state)
{
entt::registry* registry = ServiceLocator::GetEnttRegistries()->uiRegistry;
Expand Down
1 change: 0 additions & 1 deletion Source/Game-Lib/Game-Lib/Scripting/Handlers/UIHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ namespace Scripting::UI
public:
// Registered Functions
// Register templates
static i32 RegisterButtonTemplate(lua_State* state);
static i32 RegisterPanelTemplate(lua_State* state);
static i32 RegisterTextTemplate(lua_State* state);

Expand Down
94 changes: 0 additions & 94 deletions Source/Game-Lib/Game-Lib/Scripting/UI/Button.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions Source/Game-Lib/Game-Lib/Scripting/UI/Button.h

This file was deleted.

76 changes: 0 additions & 76 deletions Source/Game-Lib/Game-Lib/Scripting/UI/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "Game-Lib/ECS/Util/Transform2D.h"
#include "Game-Lib/ECS/Util/UIUtil.h"
#include "Game-Lib/Scripting/LuaState.h"
#include "Game-Lib/Scripting/UI/Button.h"
#include "Game-Lib/Scripting/UI/Panel.h"
#include "Game-Lib/Scripting/UI/Text.h"
#include "Game-Lib/Util/ServiceLocator.h"
Expand All @@ -19,81 +18,6 @@ namespace Scripting::UI
{
namespace WidgetCreationMethods
{
i32 CreateButton(lua_State* state)
{
LuaState ctx(state);

Widget* parent = ctx.GetUserData<Widget>(nullptr, 1);
if (parent == nullptr)
{
luaL_error(state, "Parent is null");
}

i32 posX = ctx.Get(0, 2);
i32 posY = ctx.Get(0, 3);

i32 sizeX = ctx.Get(100, 4);
i32 sizeY = ctx.Get(100, 5);

i32 layer = ctx.Get(0, 6);

const char* templateName = ctx.Get(nullptr, 7);
if (templateName == nullptr)
{
luaL_error(state, "Template name is null");
}

u32 templateNameHash = StringUtils::fnv1a_32(templateName, strlen(templateName));

entt::registry* registry = ServiceLocator::GetEnttRegistries()->uiRegistry;
ECS::Singletons::UISingleton& uiSingleton = registry->ctx().get<ECS::Singletons::UISingleton>();

u32 buttonTemplateIndex = uiSingleton.templateHashToButtonTemplateIndex[templateNameHash];
::UI::ButtonTemplate& buttonTemplate = uiSingleton.buttonTemplates[buttonTemplateIndex];

const std::string& panelTemplateName = buttonTemplate.panelTemplate;
u32 panelTemplateNameHash = StringUtils::fnv1a_32(panelTemplateName.data(), panelTemplateName.size());
if (!uiSingleton.templateHashToPanelTemplateIndex.contains(panelTemplateNameHash))
{
luaL_error(state, "Tried to use template name '%s' but no panel template with that name has been registered", panelTemplateName.c_str());
}

const std::string& textTemplateName = buttonTemplate.textTemplate;
u32 textTemplateNameHash = StringUtils::fnv1a_32(textTemplateName.data(), textTemplateName.size());
if (!uiSingleton.templateHashToTextTemplateIndex.contains(textTemplateNameHash))
{
luaL_error(state, "Tried to use template name '%s' but no text template with that name has been registered", textTemplateName.c_str());
}

Button* button = ctx.PushUserData<Button>([](void* x)
{

});

entt::entity panelEntity = ECS::Util::UI::CreatePanel(&button->panelWidget, registry, vec2(posX, posY), ivec2(sizeX, sizeY), layer, panelTemplateName.c_str(), parent->entity);
button->panelWidget.type = WidgetType::Panel;
button->panelWidget.entity = panelEntity;
button->panelWidget.metaTableName = "PanelMetaTable";

entt::entity textEntity = ECS::Util::UI::CreateText(&button->textWidget, registry, "", vec2(0, 3), layer, textTemplateName.c_str(), panelEntity);
button->textWidget.type = WidgetType::Text;
button->textWidget.entity = textEntity;
button->textWidget.metaTableName = "TextMetaTable";

ECS::Transform2DSystem& ts = ECS::Transform2DSystem::Get(*registry);
ts.SetAnchor(textEntity, vec2(0.5, 0.5));
ts.SetRelativePoint(textEntity, vec2(0.5, 0.5));

button->type = WidgetType::Button;
button->entity = panelEntity;

button->metaTableName = "ButtonMetaTable";
luaL_getmetatable(state, "ButtonMetaTable");
lua_setmetatable(state, -2);

return 1;
}

i32 CreatePanel(lua_State* state)
{
LuaState ctx(state);
Expand Down
3 changes: 0 additions & 3 deletions Source/Game-Lib/Game-Lib/Scripting/UI/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Scripting::UI
{
enum class WidgetType : u8
{
Button,
Canvas,
Panel,
Text,
Expand All @@ -31,15 +30,13 @@ namespace Scripting::UI

namespace WidgetCreationMethods
{
i32 CreateButton(lua_State* state);
i32 CreatePanel(lua_State* state);
i32 CreateText(lua_State* state);
i32 CreateWidget(lua_State* state);
};

static LuaMethod widgetCreationMethods[] =
{
{ "NewButton", WidgetCreationMethods::CreateButton },
{ "NewPanel", WidgetCreationMethods::CreatePanel },
{ "NewText", WidgetCreationMethods::CreateText },
{ "NewWidget", WidgetCreationMethods::CreateWidget },
Expand Down
15 changes: 0 additions & 15 deletions Source/Game-Lib/Game-Lib/UI/Templates.h

This file was deleted.

Loading

0 comments on commit 1bf078f

Please sign in to comment.