Skip to content

Commit

Permalink
Merge pull request #174 from edunad/feature/ui-tabs
Browse files Browse the repository at this point in the history
[UI] Tabs
  • Loading branch information
edunad authored Jul 10, 2024
2 parents d42db81 + d5e12cf commit 8587799
Show file tree
Hide file tree
Showing 32 changed files with 335 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This engine started as a C++ training project, with hopes of being applied in up
| 001-stencil<br/><a href='/samples/001-stencil'><img src="https://i.rawr.dev/sample1-min-3.gif" width="240" /></a> | 002-generated-models<br/><a href='/samples/002-generated-models'><img src="https://i.rawr.dev/sample2-min-4.gif" width="240" /></a> | 003-light<br/><a href='/samples/003-light'><img src="https://i.rawr.dev/sample3-min-4.gif" width="240" /></a> |
| :-----------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------: |
| 004-instancing<br/><a href='/samples/004-instancing'><img src="https://i.rawr.dev/sample4-min-2.gif" width="240" /></a> | 005-post-process<br/><a href='/samples/005-post-process'><img src="https://i.rawr.dev/sample5-min-2.gif" width="240" /></a> | 006-decals<br/><a href='/samples/006-decals'><img src="https://i.rawr.dev/sample6-min-2.gif" width="240" /></a> |
| 007-particle-system<br/><a href='/samples/007-particle-system'><img src="https://i.rawr.dev/sample7-min.gif" width="240" /></a> | 008-ui<br/><a href='/samples/008-ui'><img src="https://i.rawr.dev/sample8-min-2.gif" width="240" /></a> | 009-assimp<br/><a href='/samples/009-assimp'><img src="https://i.rawr.dev/sample9-min-3.gif" width="240" /></a> |
| 007-particle-system<br/><a href='/samples/007-particle-system'><img src="https://i.rawr.dev/sample7-min.gif" width="240" /></a> | 008-ui<br/><a href='/samples/008-ui'><img src="https://i.rawr.dev/sample8-min-3.gif" width="240" /></a> | 009-assimp<br/><a href='/samples/009-assimp'><img src="https://i.rawr.dev/sample9-min-3.gif" width="240" /></a> |
| 010-bass-audio<br/><a href='/samples/010-bass-audio'><img src="https://i.rawr.dev/sample10-min.gif" width="240" /></a> | 011-physics-3D<br/><a href='/samples/011-physics-3D'><img src="https://i.rawr.dev/sample11-min-2.gif" width="240" /></a> | 012-physics-2D<br/><a href='/samples/012-physics-2D'><img src="https://i.rawr.dev/sample12-min-2.gif" width="240" /></a> |
| 013-webm<br/><a href='/samples/013-webm'><img src="https://i.rawr.dev/sample13-min-2.gif" width="240" /></a> | 014-scripting<br/><a href='/samples/014-scripting'><img src="https://i.rawr.dev/sample14-min-2.gif" width="240" /></a> | 015-gpu-picking<br/><a href='/samples/015-gpu-picking'><img src="https://i.rawr.dev/sample15-min-2.gif" width="240" /></a> |

Expand Down
8 changes: 2 additions & 6 deletions rawrbox.ui/include/rawrbox/ui/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ namespace rawrbox {
public:
virtual ~UIContainer() = default;

UIContainer() = default;
UIContainer(rawrbox::UIRoot* root);
UIContainer(const UIContainer&) = default;
UIContainer(UIContainer&&) noexcept;
UIContainer& operator=(const UIContainer&) = default;
UIContainer& operator=(UIContainer&&) = delete;

virtual void initialize();

// UTILS ---
virtual void setPos(const rawrbox::Vector2f& pos);
[[nodiscard]] virtual const rawrbox::Vector2f& getPos() const;
Expand Down Expand Up @@ -73,10 +71,8 @@ namespace rawrbox {
template <class T, typename... CallbackArgs>
requires(std::derived_from<T, rawrbox::UIContainer>)
T* createChild(CallbackArgs&&... args) {
auto elm = std::make_shared<T>(std::forward<CallbackArgs>(args)...);
elm->setRoot(this->_root);
auto elm = std::make_shared<T>(this->_root, std::forward<CallbackArgs>(args)...);
elm->setParent(this);
elm->initialize();

auto& childn = this->getChildren();
if (elm->alwaysOnTop()) {
Expand Down
4 changes: 1 addition & 3 deletions rawrbox.ui/include/rawrbox/ui/elements/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace rawrbox {
public:
rawrbox::Event<> onClick;

UIButton() = default;
UIButton(rawrbox::UIRoot* root);
UIButton(const UIButton&) = default;
UIButton(UIButton&&) = delete;
UIButton& operator=(const UIButton&) = default;
Expand All @@ -42,8 +42,6 @@ namespace rawrbox {
this->_regular = nullptr;
}

void initialize() override;

// UTILS -----
virtual void setTextureSize(const rawrbox::Vector2& size);
virtual void setTextureColor(const rawrbox::Color& color);
Expand Down
3 changes: 1 addition & 2 deletions rawrbox.ui/include/rawrbox/ui/elements/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ namespace rawrbox {
virtual void drawText(rawrbox::Stencil& stencil, const std::vector<ConsoleEntry>& entries);

public:
UIConsole(rawrbox::Console& console);
UIConsole(rawrbox::UIRoot* root, rawrbox::Console& console);
UIConsole(const UIConsole&) = delete;
UIConsole(UIConsole&&) = delete;
UIConsole& operator=(const UIConsole&) = delete;
UIConsole& operator=(UIConsole&&) = delete;
~UIConsole() override;

void initialize() override;
void draw(rawrbox::Stencil& stencil) override;
void afterDraw(rawrbox::Stencil& stencil) override;

Expand Down
9 changes: 4 additions & 5 deletions rawrbox.ui/include/rawrbox/ui/elements/frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ namespace rawrbox {
// -----------------

public:
~UIFrame() override = default;
UIFrame() = default;
rawrbox::Event<> onClose;

UIFrame(rawrbox::UIRoot* root);
UIFrame(const UIFrame&) = default;
UIFrame(UIFrame&&) = delete;
UIFrame& operator=(const UIFrame&) = default;
UIFrame& operator=(UIFrame&&) = delete;
~UIFrame() override = default;

rawrbox::Event<> onClose;

void initialize() override;
[[nodiscard]] rawrbox::Vector2f getDrawOffset() const override;

// UTILS -----
Expand Down
6 changes: 2 additions & 4 deletions rawrbox.ui/include/rawrbox/ui/elements/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ namespace rawrbox {
std::vector<std::pair<std::string, float>> texts = {};

public:
~UIGraph() override = default;
UIGraph() = default;
UIGraph(rawrbox::UIRoot* root);
UIGraph(const UIGraph&) = default;
UIGraph(UIGraph&&) = delete;
UIGraph& operator=(const UIGraph&) = default;
UIGraph& operator=(UIGraph&&) = delete;

void initialize() override;
~UIGraph() override;

// CATEGORY --
virtual rawrbox::UIGraphCategory& getCategory(size_t id);
Expand Down
2 changes: 1 addition & 1 deletion rawrbox.ui/include/rawrbox/ui/elements/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace rawrbox {
float _border = 0.F;

public:
UIGroup(rawrbox::UIRoot* root);
~UIGroup() override = default;
UIGroup() = default;
UIGroup(const UIGroup&) = default;
UIGroup(UIGroup&&) = delete;
UIGroup& operator=(const UIGroup&) = default;
Expand Down
6 changes: 2 additions & 4 deletions rawrbox.ui/include/rawrbox/ui/elements/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ namespace rawrbox {
rawrbox::Color _color = rawrbox::Colors::White();

public:
UIImage() = default;
UIImage(rawrbox::UIRoot* root);
UIImage(const UIImage&) = default;
UIImage(UIImage&&) = delete;
UIImage& operator=(const UIImage&) = default;
UIImage& operator=(UIImage&&) = delete;
~UIImage() override {
this->_texture = nullptr;
}
~UIImage() override;

// UTILS ----
[[nodiscard]] virtual rawrbox::TextureBase* getTexture() const;
Expand Down
6 changes: 2 additions & 4 deletions rawrbox.ui/include/rawrbox/ui/elements/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@ namespace rawrbox {
void moveCharet(bool forward);
// ---------------
public:
~UIInput() override = default;
UIInput() = default;
UIInput(rawrbox::UIRoot* root);
UIInput(const UIInput&) = default;
UIInput(UIInput&&) = delete;
UIInput& operator=(const UIInput&) = default;
UIInput& operator=(UIInput&&) = delete;
~UIInput() override = default;

rawrbox::Event<uint32_t> onKey;
rawrbox::Event<> onTextUpdate;
rawrbox::Event<> onEnter;

void initialize() override;

// UTILS ----
virtual void setHints(const std::vector<std::string>& hints);

Expand Down
8 changes: 2 additions & 6 deletions rawrbox.ui/include/rawrbox/ui/elements/label.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ namespace rawrbox {
rawrbox::Vector2f _shadow = {1, 1};

public:
UILabel() = default;
UILabel(rawrbox::UIRoot* root);
UILabel(const UILabel&) = default;
UILabel(UILabel&&) = delete;
UILabel& operator=(const UILabel&) = default;
UILabel& operator=(UILabel&&) = delete;
~UILabel() override {
this->_font = nullptr;
}
~UILabel() override;

// FOCUS HANDLE ---
[[nodiscard]] bool hitTest(const rawrbox::Vector2f& point) const override;
// -----

void initialize() override;

// UTILS ----
virtual void setColor(const rawrbox::Color& col);
[[nodiscard]] virtual const rawrbox::Color& getColor() const;
Expand Down
9 changes: 7 additions & 2 deletions rawrbox.ui/include/rawrbox/ui/elements/progress_bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ namespace rawrbox {
public:
rawrbox::Event<float> onValueChange;

// UTILS ----
void initialize() override;
UIProgressBar(rawrbox::UIRoot* root);
UIProgressBar(const UIProgressBar&) = default;
UIProgressBar(UIProgressBar&&) = delete;
UIProgressBar& operator=(const UIProgressBar&) = default;
UIProgressBar& operator=(UIProgressBar&&) = delete;
~UIProgressBar() override;

// UTILS ----
virtual void showPercent(bool show);
[[nodiscard]] virtual bool isPercentVisible() const;

Expand Down
54 changes: 54 additions & 0 deletions rawrbox.ui/include/rawrbox/ui/elements/tabs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <rawrbox/ui/container.hpp>
#include <rawrbox/ui/elements/button.hpp>
#include <rawrbox/ui/elements/group.hpp>

#include <utility>

namespace rawrbox {
struct UITab {
public:
std::string name;
std::string id;

rawrbox::UIGroup* group = nullptr;
rawrbox::UIButton* button = nullptr;

UITab(std::string id, std::string name, rawrbox::UIGroup* groupUI) : name(std::move(name)), id(std::move(id)), group(groupUI) {}
};

class UITabs : public rawrbox::UIContainer {

protected:
std::vector<rawrbox::UITab> _tabs = {};

rawrbox::UITab* _activeTab = nullptr;
rawrbox::UIGroup* _buttonGroup = nullptr;

virtual void generate();

public:
UITabs(rawrbox::UIRoot* root, const std::vector<rawrbox::UITab>& tabs);
UITabs(const UITabs&) = default;
UITabs(UITabs&&) = delete;
UITabs& operator=(const UITabs&) = default;
UITabs& operator=(UITabs&&) = delete;
~UITabs() override = default;

void setSize(const rawrbox::Vector2f& size) override;

// TABS ----
virtual void setActive(size_t index);
virtual void setEnabled(size_t index, bool enabled);

[[nodiscard]] virtual float getTabHeight() const;
[[nodiscard]] virtual float getButtonWidth() const;
[[nodiscard]] virtual uint16_t getButtonFontSize() const;
// ---------

// DRAW ----
void draw(rawrbox::Stencil& stencil) override;
// -------
};
} // namespace rawrbox
2 changes: 1 addition & 1 deletion rawrbox.ui/include/rawrbox/ui/elements/virtual_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace rawrbox {
rawrbox::Color _backgroundColor = rawrbox::Colors::Transparent();

public:
UIVirtualList() = default;
UIVirtualList(rawrbox::UIRoot* root) : rawrbox::UIContainer(root){};
UIVirtualList(const UIVirtualList&) = delete;
UIVirtualList(UIVirtualList&&) = delete;
UIVirtualList& operator=(const UIVirtualList&) = delete;
Expand Down
4 changes: 1 addition & 3 deletions rawrbox.ui/include/rawrbox/ui/root.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ namespace rawrbox {
template <class T, typename... CallbackArgs>
requires(std::derived_from<T, rawrbox::UIContainer>)
T* createChild(CallbackArgs&&... args) {
auto elm = std::make_shared<T>(std::forward<CallbackArgs>(args)...);
elm->setRoot(this);
elm->initialize();
auto elm = std::make_shared<T>(this, std::forward<CallbackArgs>(args)...);

auto& childn = this->getChildren();
if (elm->alwaysOnTop()) {
Expand Down
4 changes: 4 additions & 0 deletions rawrbox.ui/include/rawrbox/ui/scripting/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <rawrbox/ui/scripting/wrappers/input.hpp>
#include <rawrbox/ui/scripting/wrappers/label.hpp>
#include <rawrbox/ui/scripting/wrappers/progress_bar.hpp>
#include <rawrbox/ui/scripting/wrappers/tab.hpp>
#include <rawrbox/ui/scripting/wrappers/tabs.hpp>
#include <rawrbox/ui/scripting/wrappers/ui.hpp>

namespace rawrbox {
Expand All @@ -35,6 +37,8 @@ namespace rawrbox {
rawrbox::UIProgressBarWrapper::registerLua(L);
rawrbox::UIGraphCatWrapper::registerLua(L);
rawrbox::UIGraphWrapper::registerLua(L);
rawrbox::UITabWrapper::registerLua(L);
rawrbox::UITabsWrapper::registerLua(L);
// ------

rawrbox::UIWrapper::registerLua(L, _root);
Expand Down
10 changes: 10 additions & 0 deletions rawrbox.ui/include/rawrbox/ui/scripting/wrappers/tab.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <rawrbox/scripting/utils/lua.hpp>

namespace rawrbox {
class UITabWrapper {

public:
static void registerLua(lua_State* L);
};
} // namespace rawrbox
10 changes: 10 additions & 0 deletions rawrbox.ui/include/rawrbox/ui/scripting/wrappers/tabs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <rawrbox/scripting/utils/lua.hpp>

namespace rawrbox {
class UITabsWrapper {

public:
static void registerLua(lua_State* L);
};
} // namespace rawrbox
5 changes: 4 additions & 1 deletion rawrbox.ui/src/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
#include <rawrbox/ui/root.hpp>

namespace rawrbox {
UIContainer::UIContainer(rawrbox::UIRoot* root) : _root(root) {
if (_root == nullptr) throw rawrbox::Logger::err("UIContainer", "UI root cannot be null!");
}

UIContainer::UIContainer(rawrbox::UIContainer&& other) noexcept : _parent(other._parent), _children(std::move(other._children)), _alwaysOnTop(other._alwaysOnTop), _aabb(other._aabb) {}
void UIContainer::initialize() {}

// UTILS ---
rawrbox::Vector2f UIContainer::getDrawOffset() const { return {}; };
Expand Down
2 changes: 1 addition & 1 deletion rawrbox.ui/src/elements/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <rawrbox/utils/keys.hpp>

namespace rawrbox {
void UIButton::initialize() {
UIButton::UIButton(rawrbox::UIRoot* root) : rawrbox::UIContainer(root) {
this->_overlay = rawrbox::RESOURCES::getFile<rawrbox::ResourceTexture>("assets/textures/ui/overlay/overlay.png")->get();
}

Expand Down
15 changes: 7 additions & 8 deletions rawrbox.ui/src/elements/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@
#include <rawrbox/utils/string.hpp>

namespace rawrbox {
UIConsole::UIConsole(rawrbox::Console& console) : _console(&console) {}
UIConsole::~UIConsole() {
if (this->_console == nullptr) return;
this->_console->onPrint.clear();
}

void UIConsole::initialize() {
if (this->_console == nullptr) throw std::runtime_error("[RawrBox-UIConsole] Invalid console reference");
UIConsole::UIConsole(rawrbox::UIRoot* root, rawrbox::Console& console) : rawrbox::UIContainer(root), _console(&console) {
if (this->_console == nullptr) throw rawrbox::Logger::err("UIConsole", "Invalid console reference");

this->_overlay = rawrbox::RESOURCES::getFile<rawrbox::ResourceTexture>("./assets/textures/ui/overlay/overlay.png")->get();

Expand Down Expand Up @@ -112,6 +106,11 @@ namespace rawrbox {
this->updateEntries();
}

UIConsole::~UIConsole() {
if (this->_console == nullptr) return;
this->_console->onPrint.clear();
}

void UIConsole::setVisible(bool visible) {
rawrbox::UIContainer::setVisible(visible);
if (visible && this->_input != nullptr) {
Expand Down
4 changes: 1 addition & 3 deletions rawrbox.ui/src/elements/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
#include <rawrbox/ui/root.hpp>

namespace rawrbox {
void UIFrame::initialize() {
UIFrame::UIFrame(rawrbox::UIRoot* root) : rawrbox::UIContainer(root), _closeButton(this->createChild<rawrbox::UIButton>()) {
this->_stripes = rawrbox::RESOURCES::getFile<rawrbox::ResourceTexture>("./assets/textures/ui/stripe.png")->get();
this->_overlay = rawrbox::RESOURCES::getFile<rawrbox::ResourceTexture>("./assets/textures/ui/overlay/overlay.png")->get();

// Build close button ---
auto size = this->getSize();

this->_closeButton = this->createChild<rawrbox::UIButton>();
this->_closeButton->setTexture("./assets/textures/ui/icons/close.png");
this->_closeButton->setSize({30, this->_titleSize - 1});
this->_closeButton->setPos({size.x - 30, -this->_titleSize});
Expand All @@ -23,7 +22,6 @@ namespace rawrbox {
this->_closeButton->setBorder(false);
this->_closeButton->setBackgroundColor(Colors::Transparent());
this->_closeButton->setVisible(this->_closable);
this->_closeButton->initialize();

this->_closeButton->onClick += [this]() {
this->onClose();
Expand Down
Loading

0 comments on commit 8587799

Please sign in to comment.