diff --git a/game/client/gui_helpers/stack_menu.lua b/game/client/gui_helpers/stack_menu.lua index f568bbb4..6ce132f2 100644 --- a/game/client/gui_helpers/stack_menu.lua +++ b/game/client/gui_helpers/stack_menu.lua @@ -2,6 +2,8 @@ local WIDGET_WIDTH = 800 local WIDGET_HEIGHT = 90 local BUTTON_SIZE = GuiDim.new(0, WIDGET_WIDTH, 0, WIDGET_HEIGHT) local widgetTexture = game.getTexture("res/button.png") +local checkboxTexture_unchecked = game.getTexture("res/checkbox-unchecked.png") +local checkboxTexture_checked = game.getTexture("res/checkbox-checked.png") StackMenu = {} StackMenu.__index = StackMenu @@ -69,6 +71,19 @@ function StackMenu:addButton(text) return button end +function StackMenu:addCheckBox(text) + local checkBox = self.overlay:addCheckBox() + self:initBasicWidget(checkBox) + checkBox.position = GuiDim.new(0, (1920 / 2 - (WIDGET_WIDTH - 500) / 2), 0, self.y + self.widgetGap) -- LEFT OFF HERE! + checkBox.size = GuiDim.new(0, WIDGET_WIDTH - 500, 0, WIDGET_HEIGHT) + checkBox.image = checkboxTexture_unchecked + checkBox.text = text + checkBox.textSize = 30 + checkBox.checkedImage = checkboxTexture_checked + checkBox.uncheckedImage = checkboxTexture_unchecked + return checkBox +end + function StackMenu:addLabel(text) local label = self.overlay:addLabel() label.textSize = 100 diff --git a/game/client/menus/main_menu.lua b/game/client/menus/main_menu.lua index ba7ab4ec..0e346389 100644 --- a/game/client/menus/main_menu.lua +++ b/game/client/menus/main_menu.lua @@ -1,6 +1,8 @@ local backgroundTexture = game.getTexture("res/menu_bg.png") local logoTexture = game.getTexture("res/logo.png") +local checkboxTexture_unchecked = game.getTexture("res/checkbox-unchecked.png") +local checkboxTexture_checked = game.getTexture("res/checkbox-checked.png") local function onCreate(overlay) local menu = StackMenu:create(900, overlay, 15) @@ -8,20 +10,20 @@ local function onCreate(overlay) menu:addImage(logoTexture, 1064, 138) menu:pad(150) - local continue = menu:addButton("Continue Game") - local playGame = menu:addButton("Play Game") + local continue = menu:addButton("Continue Game") + local playGame = menu:addButton("Play Game") local settings = menu:addButton("Settings") local exitGame = menu:addButton("Exit Game") continue.onClick = function() - game.gui():change("transition", { message = "Starting Game" } ) + game.gui():change("transition", {message = "Starting Game"}) game.control():loadWorld("Test") - end - + end + playGame.onClick = function() game.gui():push("world_select") end - + settings.onClick = function() game.gui():push("settings_menu") end @@ -34,4 +36,4 @@ end game.gui():addGui{ id = "main_menu", create = onCreate, -} \ No newline at end of file +} diff --git a/game/client/menus/world_select.lua b/game/client/menus/world_select.lua index 3651c288..634bc902 100644 --- a/game/client/menus/world_select.lua +++ b/game/client/menus/world_select.lua @@ -30,7 +30,6 @@ local function onCreate(overlay) createWorldSelectButton(menu, characterSlot) end - local joinWorldButton = overlay:addButton() joinWorldButton.textSize = 50 joinWorldButton.text = "Join World" diff --git a/res/checkbox-checked.png b/res/checkbox-checked.png new file mode 100644 index 00000000..46973abe Binary files /dev/null and b/res/checkbox-checked.png differ diff --git a/res/checkbox-unchecked.png b/res/checkbox-unchecked.png new file mode 100644 index 00000000..4afcb58f Binary files /dev/null and b/res/checkbox-unchecked.png differ diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index f158e422..0da4953a 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -37,6 +37,7 @@ add_library(ob-client gui/widget/button_widget.cpp gui/widget/text_box_widget.cpp gui/widget/widget_helper.cpp + gui/widget/checkbox_widget.cpp window.cpp diff --git a/src/client/gui/overlay.cpp b/src/client/gui/overlay.cpp index 551b6380..a27767c7 100644 --- a/src/client/gui/overlay.cpp +++ b/src/client/gui/overlay.cpp @@ -4,6 +4,7 @@ #include "widget/image_widget.h" #include "widget/label_widget.h" #include "widget/text_box_widget.h" +#include "widget/checkbox_widget.h" #include namespace gui { @@ -38,6 +39,18 @@ namespace gui { return dynamic_cast(widget); } + CheckBoxWidget* Overlay::addCheckBox() + { + auto rectangle = + rectangleComponents.emplace_back(std::make_unique()) + .get(); + auto label = textComponents.emplace_back(std::make_unique()).get(); + auto checkBox = std::make_unique(rectangle, label); + auto widget = m_widgets.emplace_back(std::move(checkBox)).get(); + + return dynamic_cast(widget); + } + ButtonWidget* Overlay::addButton() { auto rectangle = diff --git a/src/client/gui/overlay.h b/src/client/gui/overlay.h index 0b6ea294..05644e19 100644 --- a/src/client/gui/overlay.h +++ b/src/client/gui/overlay.h @@ -12,6 +12,7 @@ namespace gui { class ImageWidget; class LabelWidget; class ButtonWidget; + class CheckBoxWidget; class CenteredLabelWidget; class TextBoxWidget; /** @@ -20,6 +21,7 @@ namespace gui { */ struct OverlayDefinition final { std::string id; + sol::function create; }; @@ -40,6 +42,7 @@ namespace gui { ImageWidget* addImage(); LabelWidget* addLabel(); CenteredLabelWidget* addCenteredLabel(); + CheckBoxWidget* addCheckBox(); ButtonWidget* addButton(); TextBoxWidget* addTextBox(); diff --git a/src/client/gui/widget/checkbox_widget.cpp b/src/client/gui/widget/checkbox_widget.cpp new file mode 100644 index 00000000..105c9c66 --- /dev/null +++ b/src/client/gui/widget/checkbox_widget.cpp @@ -0,0 +1,119 @@ +#include "checkbox_widget.h" + +#include "../component/rectangle_component.h" +#include "../component/text_component.h" +#include "widget_helper.h" + +namespace gui { + + CheckBoxWidget::CheckBoxWidget(RectangleComponent* rectComponent, TextComponent* label) + : mp_rectangle(rectComponent) + , mp_label(label) + , m_checked(false) + { + componentList.push_back(mp_rectangle); + componentList.push_back(mp_label); + + mp_label->setFontSize(40); + } + + void CheckBoxWidget::setPosition(const GuiDimension& position) + { + mp_rectangle->setPosition(position); + } + + void CheckBoxWidget::setSize(const GuiDimension& size) + { + mp_rectangle->setSize(size); + } + + void CheckBoxWidget::setColour(float r, float g, float b) + { + mp_rectangle->colour = {r, g, b}; + } + + void CheckBoxWidget::setImage(int Image) + { + mp_rectangle->setTexture(Image); + } + + void CheckBoxWidget::setUncheckedImage(int Image) + { + uncheckedTexture = Image; + } + + void CheckBoxWidget::setCheckedImage(int Image) + { + checkedTexture = Image; + } + + void CheckBoxWidget::setText(const std::string& text) + { + mp_label->setText(text); + } + + void CheckBoxWidget::setTextSize(unsigned size) + { + mp_label->setFontSize(size); + } + + void CheckBoxWidget::setChecked(bool checked) + { + m_checked = checked; + } + + void CheckBoxWidget::handleClick(sf::Mouse::Button button, float mx, float my) + { + if (mp_rectangle->isInBounds(mx, my) && button == sf::Mouse::Left) { + onClick(); + } + } + + void CheckBoxWidget::handleMouseMove(float mx, float my) + { + if (mp_rectangle->isInBounds(mx, my)) { + if (m_onMoveOver.valid()) { + m_onMoveOver(); + } + } + else { + if (m_onMoveOver.valid()) { + m_onMouseOff(); + } + } + } + + void CheckBoxWidget::onClick() + { + m_checked = !m_checked; + if (m_checked) + setImage(checkedTexture); + else + setImage(uncheckedTexture); + } + + void CheckBoxWidget::setOnMouseOver(sol::function function) + { + m_onMoveOver = function; + } + + void CheckBoxWidget::setOnMouseOff(sol::function function) + { + m_onMouseOff = function; + } + + void CheckBoxWidget::prepareRender() + { + GuiDimension pos = centerText(mp_rectangle->getBounds(), *mp_label); + pos.offset.x -= mp_rectangle->getBounds().width / 6; + + mp_label->setPosition(pos); + } + + bool CheckBoxWidget::getChecked() + { + return m_checked; + } + + +} // namespace gui diff --git a/src/client/gui/widget/checkbox_widget.h b/src/client/gui/widget/checkbox_widget.h new file mode 100644 index 00000000..5d79dfe3 --- /dev/null +++ b/src/client/gui/widget/checkbox_widget.h @@ -0,0 +1,50 @@ +#pragma once + +#include "widget.h" + +#include + +namespace gui { + + class CheckBoxWidget final : public Widget { + public: + CheckBoxWidget(RectangleComponent* mp_rectangle, TextComponent* label); + void setPosition(const GuiDimension& position) final override; + void setSize(const GuiDimension& size) final override; + + void setColour(float r, float g, float b) final override; + void setImage(int Image); + void setUncheckedImage(int Image); + void setCheckedImage(int Image); + + void setText(const std::string& text); + void setTextSize(unsigned size); + + void setChecked(bool check); + + void handleClick(sf::Mouse::Button, float, float) final override; + void handleMouseMove(float, float) final override; + + + void onClick(); + void setOnMouseOver(sol::function function); + void setOnMouseOff(sol::function function); + + void prepareRender() final override; + + bool getChecked(); + + private: + + int checkedTexture; + int uncheckedTexture; + + bool m_checked; + RectangleComponent* mp_rectangle = nullptr; + TextComponent* mp_label = nullptr; + + sol::function m_onMoveOver; + sol::function m_onMouseOff; + }; + +} // namespace gui diff --git a/src/client/lua/gui_widget_api.cpp b/src/client/lua/gui_widget_api.cpp index c7e92488..ab686851 100644 --- a/src/client/lua/gui_widget_api.cpp +++ b/src/client/lua/gui_widget_api.cpp @@ -5,6 +5,7 @@ #include "../gui/widget/image_widget.h" #include "../gui/widget/label_widget.h" #include "../gui/widget/text_box_widget.h" +#include "../gui/widget/checkbox_widget.h" #include namespace { @@ -69,6 +70,23 @@ namespace { addCommonAPI(buttonApi); } + void addGuiCheckBoxApi(ScriptEngine& engine) + { + auto checkBoxApi = engine.lua.new_usertype("CheckBoxWidget"); + checkBoxApi["checkedImage"] = + sol::property(&gui::CheckBoxWidget::setCheckedImage); + checkBoxApi["uncheckedImage"] = + sol::property(&gui::CheckBoxWidget::setUncheckedImage); + checkBoxApi["image"] = sol::property(&gui::CheckBoxWidget::setImage); + checkBoxApi["checked"] = sol::property(&gui::CheckBoxWidget::getChecked, &gui::CheckBoxWidget::setChecked); + + checkBoxApi["onMouseOver"] = sol::property(&gui::CheckBoxWidget::setOnMouseOver); + checkBoxApi["onMouseOff"] = sol::property(&gui::CheckBoxWidget::setOnMouseOff); + + addCommonLabelApi(checkBoxApi); + addCommonAPI(checkBoxApi); + } + void addGuiTextboxApi(ScriptEngine& engine) { auto textboxApi = engine.lua.new_usertype("TextBoxWidget"); @@ -97,6 +115,7 @@ namespace { overlayApi["addButton"] = &gui::Overlay::addButton; overlayApi["addCenteredLabel"] = &gui::Overlay::addCenteredLabel; overlayApi["addTextBox"] = &gui::Overlay::addTextBox; + overlayApi["addCheckBox"] = &gui::Overlay::addCheckBox; overlayApi["hide"] = &gui::Overlay::hide; overlayApi["show"] = &gui::Overlay::show; @@ -114,4 +133,6 @@ void luaInitGuiWidgetApi(ScriptEngine& scriptEngine) addGuiButtonApi(scriptEngine); addGuiTextboxApi(scriptEngine); addGuiCenteredLabelApi(scriptEngine); + addGuiCheckBoxApi(scriptEngine); + }