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:
TestWindow()
: CBaseWindow("root_test", nullptr) {
Props()->SetBool("always_visible", false);
Props()->SetBool("hover", false);
this->always_visible = false;
this->hover = false;
SetMaxSize(1270, 1000);
SetPositionMode(PositionMode::FLOATING);
auto* titlebar = new CTitleBar(this, "Test uwu~");
AddChild(titlebar);
Props()->SetBool("visable", true);
// this->visible = true;
AddChild(new CTextLabel("button_label", this, "Button widget:"));
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 = new List();
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->Show();
g_pGUI->m_pRootWindow->AddChild(list_menu);

View File

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

View File

@ -22,6 +22,7 @@
//#include <memory>
#include <algorithm>
#include <cstring>
#include <optional>
#include <vector>
#include "iwidget.hpp"
@ -40,27 +41,27 @@ public:
return m_KeyValues;
}
inline virtual void OnMouseEnter() { m_KeyValues->SetBool("hover", true); }
inline virtual void OnMouseLeave() { m_KeyValues->SetBool("hover", false); }
inline virtual void OnMousePress() { m_KeyValues->SetBool("press", true); }
inline virtual void OnMouseRelease() { m_KeyValues->SetBool("press", false); }
inline virtual void OnMouseEnter() { this->hover = true; }
inline virtual void OnMouseLeave() { this->hover = false; }
inline virtual void OnMousePress() { this->press = true; }
inline virtual void OnMouseRelease() { this->press = false; }
inline virtual void OnKeyPress(CatKey key, bool repeat) {};
inline virtual void OnKeyRelease(CatKey key) {};
inline virtual void OnFocusGain() { m_KeyValues->SetBool("focus", true); }
inline virtual void OnFocusLose() { m_KeyValues->SetBool("focus", false); }
inline virtual void OnFocusGain() { this->focus = true; }
inline virtual void OnFocusLose() { this->focus = false; }
inline virtual void HandleCustomEvent(std::string_view event) {};
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 Hide() { m_KeyValues->SetBool("visible", false); }
inline virtual void Show() { this->visible = true; }
inline virtual void Hide() { this->visible = false; }
inline virtual bool IsVisible() {
if (GetParent())
return GetParent()->IsVisible() && m_KeyValues->GetBool("visible");
return m_KeyValues->GetBool("visible");
return GetParent()->IsVisible() && this->visible;
return this->visible;
}
virtual bool IsHovered();
@ -71,45 +72,58 @@ public:
inline virtual void SetOffset(int x, int y) {
if (x >= 0)
m_KeyValues->SetInt("offset_x", x);
this->offset.first = x;
if (y >= 0)
m_KeyValues->SetInt("offset_y", y);
this->offset.second = y;
}
inline virtual void SetMaxSize(int x, int y) {
if (x >= 0)
m_KeyValues->SetInt("max_x", x);
this->max_size.first = x;
if (y >= 0)
m_KeyValues->SetInt("max_y", y);
this->max_size.second = y;
}
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() {
return std::make_pair(m_KeyValues->GetInt("size_x"), m_KeyValues->GetInt("size_y"));
return this->size;
}
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 void SetZIndex(int idx) { m_KeyValues->SetInt("zindex", idx); }
inline virtual int GetZIndex() { return this->zindex; }
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 void SetPositionMode(PositionMode mode) { m_KeyValues->SetInt("positionmode", mode); };
inline virtual IWidget* GetParent() { return m_pParent; }
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();
inline void SetSize(int x, int y) {
if (x >= 0)
m_KeyValues->SetInt("size_x", x);
this->size.first = x;
if (y >= 0)
m_KeyValues->SetInt("size_y", y);
this->size.second = y;
}
KeyValues* m_KeyValues;
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);
void SetWidth(int width);
inline bool Value() { return Props()->GetBool("checked"); }
inline void SetValue(bool value) { Props()->SetBool("checked", value); }
inline bool Value() { return this->checked; }
inline void SetValue(bool value) { this->checked = value; }
void SetCallback(CheckboxCallbackFn_t callback);
virtual void OnMousePress();
virtual void Draw(int x, int y);
CheckboxCallbackFn_t m_pCallback;
bool checked;
int width;
};

View File

@ -40,4 +40,5 @@ public:
virtual void OnMousePress() override;
virtual void OnFocusLose() 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, std::string> stored_strings;
std::unordered_map<std::string, glez::rgba> stored_colors;
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]] bool GetBool(const std::string& s) { return this->stored_bools.at(s); }
[[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); }
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); }

View File

@ -32,4 +32,8 @@ public:
void SetCentered(bool centered);
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
void Canvas::Setup() {
gui_visible.Callback = [this](bool v) {
this->Props()->SetBool("visible", v);
this->visible = v;
};
this->gui_color = glez::rgba(255, 105, 180);
this->Props()->SetBool("always_visible", true);
this->Props()->SetBool("hover", true);
this->always_visible = true;
this->hover = true;
// this->font = new glez::font("../res/opensans.ttf", 10);
// this->font->load();
@ -121,7 +121,7 @@ void Canvas::Update() {
}
} else {
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 (down)

View File

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

View File

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

View File

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

View File

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

View File

@ -27,7 +27,7 @@
CTextInput::CTextInput(std::string name, IWidget* parent)
: CBaseWidget(name, parent) {
this->Props()->SetString("value", "");
this->Props()->SetBool("focus", false);
this->focus = false;
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)
: CBaseWidget(name, parent) {
Props()->SetInt("max_x", 50);
this->max_size.first = 50;
this->SetPadding(3, 3);
if (centered) {
SetAutoSize(false);
@ -74,12 +74,12 @@ CTextLabel::CTextLabel(std::string name, IWidget* parent, std::string text, bool
SetText(text);
}
void CTextLabel::SetAutoSize(bool autosize) {
Props()->SetBool("autosize", autosize);
void CTextLabel::SetAutoSize(bool _autosize) {
this->autosize = _autosize;
}
void CTextLabel::SetCentered(bool centered) {
Props()->SetBool("centered", centered);
void CTextLabel::SetCentered(bool _centered) {
this->centered = _centered;
}
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"));
std::pair<float, float> size;
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);
} else {
// auto ms = GetMaxSize();
auto ms = Props()->GetInt("max_x");
auto ms = this->max_size.first;
SetSize(-1, size.second + padding.second * 2);
if (ms /*.first*/ > 0) {
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) {
if (Props()->GetBool("centered")) {
if (this->centered) {
auto size = GetSize();
std::pair<float, float> ssize;
g_pGUI->GetRootWindow()->GetFont().stringSize(GetText(), &ssize.first, &ssize.second);