Deprecated keyvalues pt1

This commit is contained in:
Rebekah 2022-04-11 11:57:36 -04:00
parent c05ca050e7
commit 20b344c3b6
Signed by: oneechanhax
GPG Key ID: 183EB7902964DAE5
14 changed files with 103 additions and 79 deletions

View File

@ -56,14 +56,14 @@ class TestWindow : public CBaseWindow {
public: public:
TestWindow() TestWindow()
: CBaseWindow("root_test", nullptr) { : CBaseWindow("root_test", nullptr) {
Props()->SetBool("always_visible", false); this->always_visible = false;
Props()->SetBool("hover", false); this->hover = false;
SetMaxSize(1270, 1000); SetMaxSize(1270, 1000);
SetPositionMode(PositionMode::FLOATING); SetPositionMode(PositionMode::FLOATING);
auto* titlebar = new CTitleBar(this, "Test uwu~"); auto* titlebar = new CTitleBar(this, "Test uwu~");
AddChild(titlebar); AddChild(titlebar);
Props()->SetBool("visable", true); // this->visible = true;
AddChild(new CTextLabel("button_label", this, "Button widget:")); AddChild(new CTextLabel("button_label", this, "Button widget:"));
AddChild(new CBaseButton("button", this, "I'm Clickable!", [this](CBaseButton*) { AddChild(new CBaseButton("button", this, "I'm Clickable!", [this](CBaseButton*) {
@ -155,7 +155,12 @@ int main() {
// auto* list_menu = List::FromString(menu_list); // auto* list_menu = List::FromString(menu_list);
auto* list_menu = new List(); auto* list_menu = new List();
list_menu->Fill(ui::BaseVar::GetList()); list_menu->Fill(ui::BaseVar::GetList());
list_menu->Props()->SetBool("brackets3", true); ItemTitle* find = nullptr;
for (auto* i : list_menu->m_children)
if ((find = dynamic_cast<ItemTitle*>(i)))
break;
if (find)
find->brackets = true;
list_menu->SetMaxSize(1000, 1000); list_menu->SetMaxSize(1000, 1000);
list_menu->Show(); list_menu->Show();
g_pGUI->m_pRootWindow->AddChild(list_menu); g_pGUI->m_pRootWindow->AddChild(list_menu);

View File

@ -33,6 +33,7 @@ public:
public: public:
const std::string title; const std::string title;
bool brackets;
}; };
} }

View File

@ -22,6 +22,7 @@
//#include <memory> //#include <memory>
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <optional>
#include <vector> #include <vector>
#include "iwidget.hpp" #include "iwidget.hpp"
@ -40,27 +41,27 @@ public:
return m_KeyValues; return m_KeyValues;
} }
inline virtual void OnMouseEnter() { m_KeyValues->SetBool("hover", true); } inline virtual void OnMouseEnter() { this->hover = true; }
inline virtual void OnMouseLeave() { m_KeyValues->SetBool("hover", false); } inline virtual void OnMouseLeave() { this->hover = false; }
inline virtual void OnMousePress() { m_KeyValues->SetBool("press", true); } inline virtual void OnMousePress() { this->press = true; }
inline virtual void OnMouseRelease() { m_KeyValues->SetBool("press", false); } inline virtual void OnMouseRelease() { this->press = false; }
inline virtual void OnKeyPress(CatKey key, bool repeat) {}; inline virtual void OnKeyPress(CatKey key, bool repeat) {};
inline virtual void OnKeyRelease(CatKey key) {}; inline virtual void OnKeyRelease(CatKey key) {};
inline virtual void OnFocusGain() { m_KeyValues->SetBool("focus", true); } inline virtual void OnFocusGain() { this->focus = true; }
inline virtual void OnFocusLose() { m_KeyValues->SetBool("focus", false); } inline virtual void OnFocusLose() { this->focus = false; }
inline virtual void HandleCustomEvent(std::string_view event) {}; inline virtual void HandleCustomEvent(std::string_view event) {};
inline virtual bool ConsumesKey(CatKey key) { return false; } inline virtual bool ConsumesKey(CatKey key) { return false; }
inline virtual bool AlwaysVisible() { return m_KeyValues->GetBool("always_visible"); } inline virtual bool AlwaysVisible() { return this->always_visible; }
inline virtual void Show() { m_KeyValues->SetBool("visible", true); } inline virtual void Show() { this->visible = true; }
inline virtual void Hide() { m_KeyValues->SetBool("visible", false); } inline virtual void Hide() { this->visible = false; }
inline virtual bool IsVisible() { inline virtual bool IsVisible() {
if (GetParent()) if (GetParent())
return GetParent()->IsVisible() && m_KeyValues->GetBool("visible"); return GetParent()->IsVisible() && this->visible;
return m_KeyValues->GetBool("visible"); return this->visible;
} }
virtual bool IsHovered(); virtual bool IsHovered();
@ -71,45 +72,58 @@ public:
inline virtual void SetOffset(int x, int y) { inline virtual void SetOffset(int x, int y) {
if (x >= 0) if (x >= 0)
m_KeyValues->SetInt("offset_x", x); this->offset.first = x;
if (y >= 0) if (y >= 0)
m_KeyValues->SetInt("offset_y", y); this->offset.second = y;
} }
inline virtual void SetMaxSize(int x, int y) { inline virtual void SetMaxSize(int x, int y) {
if (x >= 0) if (x >= 0)
m_KeyValues->SetInt("max_x", x); this->max_size.first = x;
if (y >= 0) if (y >= 0)
m_KeyValues->SetInt("max_y", y); this->max_size.second = y;
} }
inline virtual std::pair<int, int> GetOffset() { inline virtual std::pair<int, int> GetOffset() {
return std::make_pair(m_KeyValues->GetInt("offset_x"), m_KeyValues->GetInt("offset_y")); return this->offset;
} }
inline virtual std::pair<int, int> GetSize() { inline virtual std::pair<int, int> GetSize() {
return std::make_pair(m_KeyValues->GetInt("size_x"), m_KeyValues->GetInt("size_y")); return this->size;
} }
inline virtual std::pair<int, int> GetMaxSize() { inline virtual std::pair<int, int> GetMaxSize() {
return std::make_pair(m_KeyValues->GetInt("max_x"), m_KeyValues->GetInt("max_y")); return this->max_size;
} }
inline virtual int GetZIndex() { return m_KeyValues->GetInt("zindex"); } inline virtual int GetZIndex() { return this->zindex; }
inline virtual void SetZIndex(int idx) { m_KeyValues->SetInt("zindex", idx); } inline virtual void SetZIndex(int idx) { this->zindex = idx; }
inline virtual std::string GetTooltip() { return std::string(m_KeyValues->GetString("tooltip")); } inline virtual std::string GetTooltip() { return this->tooltip; }
inline virtual PositionMode GetPositionMode() { return (PositionMode)m_KeyValues->GetInt("positionmode"); } inline virtual PositionMode GetPositionMode() { return (PositionMode)m_KeyValues->GetInt("positionmode"); }
inline virtual void SetPositionMode(PositionMode mode) { m_KeyValues->SetInt("positionmode", mode); }; inline virtual void SetPositionMode(PositionMode mode) { m_KeyValues->SetInt("positionmode", mode); };
inline virtual IWidget* GetParent() { return m_pParent; } inline virtual IWidget* GetParent() { return m_pParent; }
inline virtual void SetParent(IWidget* parent) { m_pParent = parent; } inline virtual void SetParent(IWidget* parent) { m_pParent = parent; }
inline virtual std::string GetName() { return std::string(m_KeyValues->GetString("name")); } inline virtual std::string GetName() { return this->name; }
std::pair<int, int> AbsolutePosition(); std::pair<int, int> AbsolutePosition();
inline void SetSize(int x, int y) { inline void SetSize(int x, int y) {
if (x >= 0) if (x >= 0)
m_KeyValues->SetInt("size_x", x); this->size.first = x;
if (y >= 0) if (y >= 0)
m_KeyValues->SetInt("size_y", y); this->size.second = y;
} }
KeyValues* m_KeyValues; KeyValues* m_KeyValues;
IWidget* m_pParent; IWidget* m_pParent;
std::pair<int, int> offset;
std::pair<int, int> size;
std::pair<int, int> max_size;
std::string name;
std::string tooltip;
std::optional<glez::rgba> bounds_color;
int zindex;
bool visible;
bool always_visible;
bool hover;
bool press;
bool focus;
}; };

View File

@ -30,12 +30,14 @@ public:
CCheckbox(std::string name = "unnamed", IWidget* parent = nullptr, bool checked = false); CCheckbox(std::string name = "unnamed", IWidget* parent = nullptr, bool checked = false);
void SetWidth(int width); void SetWidth(int width);
inline bool Value() { return Props()->GetBool("checked"); } inline bool Value() { return this->checked; }
inline void SetValue(bool value) { Props()->SetBool("checked", value); } inline void SetValue(bool value) { this->checked = value; }
void SetCallback(CheckboxCallbackFn_t callback); void SetCallback(CheckboxCallbackFn_t callback);
virtual void OnMousePress(); virtual void OnMousePress();
virtual void Draw(int x, int y); virtual void Draw(int x, int y);
CheckboxCallbackFn_t m_pCallback; CheckboxCallbackFn_t m_pCallback;
bool checked;
int width;
}; };

View File

@ -40,4 +40,5 @@ public:
virtual void OnMousePress() override; virtual void OnMousePress() override;
virtual void OnFocusLose() override; virtual void OnFocusLose() override;
virtual bool ConsumesKey(CatKey key) override; virtual bool ConsumesKey(CatKey key) override;
bool capturing;
}; };

View File

@ -33,8 +33,8 @@ public:
std::unordered_map<std::string, float> stored_floats; std::unordered_map<std::string, float> stored_floats;
std::unordered_map<std::string, std::string> stored_strings; std::unordered_map<std::string, std::string> stored_strings;
std::unordered_map<std::string, glez::rgba> stored_colors; std::unordered_map<std::string, glez::rgba> stored_colors;
bool GetBool(const std::string& s) { return this->stored_bools.at(s); } [[deprecated]] bool GetBool(const std::string& s) { return this->stored_bools.at(s); }
void SetBool(const std::string& s, int v) { this->stored_bools[s] = v; } [[deprecated]] void SetBool(const std::string& s, int v) { this->stored_bools[s] = v; }
int GetInt(const std::string& s) { return this->stored_ints.at(s); } int GetInt(const std::string& s) { return this->stored_ints.at(s); }
void SetInt(const std::string& s, int v) { this->stored_ints[s] = v; } void SetInt(const std::string& s, int v) { this->stored_ints[s] = v; }
float GetFloat(const std::string& s) { return this->stored_floats.at(s); } float GetFloat(const std::string& s) { return this->stored_floats.at(s); }

View File

@ -32,4 +32,8 @@ public:
void SetCentered(bool centered); void SetCentered(bool centered);
virtual void Draw(int x, int y); virtual void Draw(int x, int y);
private:
bool autosize;
bool centered;
}; };

View File

@ -48,11 +48,11 @@ Canvas::Canvas()
// Crashes associated with no set root in globals // Crashes associated with no set root in globals
void Canvas::Setup() { void Canvas::Setup() {
gui_visible.Callback = [this](bool v) { gui_visible.Callback = [this](bool v) {
this->Props()->SetBool("visible", v); this->visible = v;
}; };
this->gui_color = glez::rgba(255, 105, 180); this->gui_color = glez::rgba(255, 105, 180);
this->Props()->SetBool("always_visible", true); this->always_visible = true;
this->Props()->SetBool("hover", true); this->hover = true;
// this->font = new glez::font("../res/opensans.ttf", 10); // this->font = new glez::font("../res/opensans.ttf", 10);
// this->font->load(); // this->font->load();
@ -121,7 +121,7 @@ void Canvas::Update() {
} }
} else { } else {
if ((i == CatKey::CATKEY_INSERT || i == CatKey::CATKEY_F11) && down) { if ((i == CatKey::CATKEY_INSERT || i == CatKey::CATKEY_F11) && down) {
this->Props()->SetBool("visible", !this->Props()->GetBool("visible")); this->visible = !this->visible;
} }
if (this->IsVisible()) { if (this->IsVisible()) {
if (down) if (down)

View File

@ -30,13 +30,13 @@ namespace ncc {
ItemTitle::ItemTitle(std::string title) ItemTitle::ItemTitle(std::string title)
: Item("ncc_list_title") : Item("ncc_list_title")
, title(title) { , title(title) {
Props()->SetBool("brackets3", false); this->brackets = false;
} }
void ItemTitle::Draw(int x, int y) { void ItemTitle::Draw(int x, int y) {
Item::Draw(x, y); Item::Draw(x, y);
// nailed it // nailed it
bool brackets3 = Props()->GetBool("brackets3"); bool brackets3 = this->brackets;
std::string str = (brackets3 ? ">>> " : ">> ") + title + (brackets3 ? " <<<" : " <<"); std::string str = (brackets3 ? ">>> " : ">> ") + title + (brackets3 ? " <<<" : " <<");
std::pair<float, float> size; std::pair<float, float> size;
g_pGUI->GetRootWindow()->GetFont().stringSize(str, &size.first, &size.second); g_pGUI->GetRootWindow()->GetFont().stringSize(str, &size.first, &size.second);

View File

@ -25,46 +25,43 @@
#include "gui/gui.hpp" #include "gui/gui.hpp"
void CBaseWidget::DrawBounds(int x, int y) { void CBaseWidget::DrawBounds(int x, int y) {
if (m_KeyValues->IsEmpty("bounds_color")) { if (!this->bounds_color)
m_KeyValues->SetColor("bounds_color", glez::rgba(rand() % 255, rand() % 255, rand() % 255, 255)); this->bounds_color = glez::rgba(rand() % 255, rand() % 255, rand() % 255, 255);
}
auto size = GetSize(); auto size = GetSize();
glez::draw::rect(x, y, size.first, size.second, Transparent(m_KeyValues->GetColor("bounds_color"), 0.25f)); glez::draw::rect(x, y, size.first, size.second, Transparent(*this->bounds_color, 0.25f));
glez::draw::rect_outline(x, y, size.first, size.second, m_KeyValues->GetColor("bounds_color"), 1); glez::draw::rect_outline(x, y, size.first, size.second, *this->bounds_color, 1);
} }
bool CBaseWidget::IsHovered() { bool CBaseWidget::IsHovered() {
return g_pGUI->GetRootWindow()->IsVisible() && m_KeyValues->GetBool("hover"); return g_pGUI->GetRootWindow()->IsVisible() && this->hover;
} }
bool CBaseWidget::IsFocused() { bool CBaseWidget::IsFocused() {
return g_pGUI->GetRootWindow()->IsVisible() && m_KeyValues->GetBool("focus"); return g_pGUI->GetRootWindow()->IsVisible() && this->focus;
} }
bool CBaseWidget::IsPressed() { bool CBaseWidget::IsPressed() {
return g_pGUI->GetRootWindow()->IsVisible() && m_KeyValues->GetBool("press"); return g_pGUI->GetRootWindow()->IsVisible() && this->press;
} }
CBaseWidget::CBaseWidget(std::string name, IWidget* parent) CBaseWidget::CBaseWidget(std::string _name, IWidget* parent)
: m_KeyValues(new KeyValues(std::string(name + "_kv").c_str())) { : m_KeyValues(new KeyValues(std::string(_name + "_kv").c_str())) {
m_pParent = parent; m_pParent = parent;
Props()->SetString("name", name.c_str()); this->name = _name;
SetPositionMode(INLINE); SetPositionMode(INLINE);
Show(); Show();
SetMaxSize(-1, -1); this->max_size = { -1, -1 };
this->SetOffset(0, 0); this->SetOffset(0, 0);
this->Props()->SetBool("hover", false); this->hover = false;
this->Props()->SetBool("press", false); this->press = false;
this->Props()->SetBool("focus", false); this->focus = false;
this->Props()->SetBool("always_visible", false); this->always_visible = false;
this->Props()->SetInt("size_y", 0);
this->Props()->SetInt("size_x", 0);
this->SetZIndex(-1); this->SetZIndex(-1);
} }
void CBaseWidget::Update() { void CBaseWidget::Update() {
if (IsHovered() && IsVisible() && Props()->FindKey("tooltip")) { if (IsHovered() && IsVisible() && !this->tooltip.empty()) {
g_pGUI->m_pRootWindow->ShowTooltip(Props()->GetString("tooltip")); g_pGUI->m_pRootWindow->ShowTooltip(tooltip);
} }
} }

View File

@ -29,9 +29,9 @@ CCheckbox::CCheckbox(std::string name, IWidget* parent, bool checked)
SetValue(checked); SetValue(checked);
} }
void CCheckbox::SetWidth(int width) { void CCheckbox::SetWidth(int _width) {
Props()->SetInt("width", width); this->width = _width;
SetSize(width, width); SetSize(_width, _width);
} }
void CCheckbox::Draw(int x, int y) { void CCheckbox::Draw(int x, int y) {

View File

@ -26,8 +26,8 @@
CKeyInput::CKeyInput(std::string name, IWidget* parent) CKeyInput::CKeyInput(std::string name, IWidget* parent)
: CBaseWidget(name, parent) { : CBaseWidget(name, parent) {
Props()->SetInt("value", 0); Props()->SetInt("value", 0);
Props()->SetBool("capturing", false); this->capturing = false;
Props()->SetBool("focus", false); this->focus = false;
} }
CatKey CKeyInput::Value() { CatKey CKeyInput::Value() {
@ -41,7 +41,7 @@ void CKeyInput::SetValue(int value) {
void CKeyInput::Draw(int x, int y) { void CKeyInput::Draw(int x, int y) {
std::string key = ""; std::string key = "";
glez::rgba color = glez::color::white; glez::rgba color = glez::color::white;
if (Props()->GetBool("capturing")) { if (this->capturing) {
key = "< PRESS >"; key = "< PRESS >";
color = g_pGUI->GetRootWindow()->GetColor(); color = g_pGUI->GetRootWindow()->GetColor();
} else { } else {
@ -64,25 +64,25 @@ void CKeyInput::SetCallback(KeyInputCallbackFn_t callback) {
} }
void CKeyInput::OnMousePress() { void CKeyInput::OnMousePress() {
if (!Props()->GetBool("capturing")) if (!this->capturing)
Props()->SetBool("capturing", true); this->capturing = true;
} }
void CKeyInput::OnFocusLose() { void CKeyInput::OnFocusLose() {
Props()->SetBool("capturing", false); this->capturing = false;
} }
bool CKeyInput::ConsumesKey(CatKey key) { bool CKeyInput::ConsumesKey(CatKey key) {
return key != CatKey::CATKEY_MOUSE_1 && Props()->GetBool("capturing"); return key != CatKey::CATKEY_MOUSE_1 && this->capturing;
} }
void CKeyInput::OnKeyPress(CatKey key, bool repeat) { void CKeyInput::OnKeyPress(CatKey key, bool repeat) {
if (Props()->GetBool("capturing")) { if (this->capturing) {
if (key == CATKEY_ESCAPE) if (key == CATKEY_ESCAPE)
key = (CatKey)0; key = (CatKey)0;
SetValue(key); SetValue(key);
if (m_pCallback) if (m_pCallback)
m_pCallback(this, key); m_pCallback(this, key);
Props()->SetBool("capturing", false); this->capturing = false;
} }
} }

View File

@ -27,7 +27,7 @@
CTextInput::CTextInput(std::string name, IWidget* parent) CTextInput::CTextInput(std::string name, IWidget* parent)
: CBaseWidget(name, parent) { : CBaseWidget(name, parent) {
this->Props()->SetString("value", ""); this->Props()->SetString("value", "");
this->Props()->SetBool("focus", false); this->focus = false;
this->SetMaxWidth(8); this->SetMaxWidth(8);
} }

View File

@ -62,7 +62,7 @@ static std::string WordWrap(std::string& in, int max, glez::font& font) {
CTextLabel::CTextLabel(std::string name, IWidget* parent, std::string text, bool centered) CTextLabel::CTextLabel(std::string name, IWidget* parent, std::string text, bool centered)
: CBaseWidget(name, parent) { : CBaseWidget(name, parent) {
Props()->SetInt("max_x", 50); this->max_size.first = 50;
this->SetPadding(3, 3); this->SetPadding(3, 3);
if (centered) { if (centered) {
SetAutoSize(false); SetAutoSize(false);
@ -74,12 +74,12 @@ CTextLabel::CTextLabel(std::string name, IWidget* parent, std::string text, bool
SetText(text); SetText(text);
} }
void CTextLabel::SetAutoSize(bool autosize) { void CTextLabel::SetAutoSize(bool _autosize) {
Props()->SetBool("autosize", autosize); this->autosize = _autosize;
} }
void CTextLabel::SetCentered(bool centered) { void CTextLabel::SetCentered(bool _centered) {
Props()->SetBool("centered", centered); this->centered = _centered;
} }
void CTextLabel::SetPadding(int x, int y) { void CTextLabel::SetPadding(int x, int y) {
@ -93,11 +93,11 @@ void CTextLabel::SetText(std::string text) {
auto padding = std::make_pair(Props()->GetInt("padding_x"), Props()->GetInt("padding_y")); auto padding = std::make_pair(Props()->GetInt("padding_x"), Props()->GetInt("padding_y"));
std::pair<float, float> size; std::pair<float, float> size;
g_pGUI->GetRootWindow()->GetFont().stringSize(text, &size.first, &size.second); g_pGUI->GetRootWindow()->GetFont().stringSize(text, &size.first, &size.second);
if (Props()->GetBool("autosize")) { if (this->autosize) {
SetSize(size.first + padding.first * 2, size.second + padding.second * 2); SetSize(size.first + padding.first * 2, size.second + padding.second * 2);
} else { } else {
// auto ms = GetMaxSize(); // auto ms = GetMaxSize();
auto ms = Props()->GetInt("max_x"); auto ms = this->max_size.first;
SetSize(-1, size.second + padding.second * 2); SetSize(-1, size.second + padding.second * 2);
if (ms /*.first*/ > 0) { if (ms /*.first*/ > 0) {
std::string txt = WordWrap(text, ms /*.first*/ - 2 * padding.first, g_pGUI->GetRootWindow()->GetFont()); std::string txt = WordWrap(text, ms /*.first*/ - 2 * padding.first, g_pGUI->GetRootWindow()->GetFont());
@ -115,7 +115,7 @@ std::string CTextLabel::GetText() {
} }
void CTextLabel::Draw(int x, int y) { void CTextLabel::Draw(int x, int y) {
if (Props()->GetBool("centered")) { if (this->centered) {
auto size = GetSize(); auto size = GetSize();
std::pair<float, float> ssize; std::pair<float, float> ssize;
g_pGUI->GetRootWindow()->GetFont().stringSize(GetText(), &ssize.first, &ssize.second); g_pGUI->GetRootWindow()->GetFont().stringSize(GetText(), &ssize.first, &ssize.second);