struct DataEditor : UINode
{
struct Item
{
std::string name;
int value;
bool enable;
float value2;
};
struct ItemButton : UINode
{
ItemButton()
{
GetStyle().SetLayout(style::Layout::Stack);
//GetStyle().SetMargin(32);
}
void Render(UIContainer* ctx) override
{
ctx->Push<UIButton>()->GetStyle().SetWidth(style::Coord::Percent(100));
ctx->Text(name);
ctx->Pop();
}
void OnEvent(UIEvent& e) override
{
if (e.type == UIEventType::Activate)
{
callback();
}
}
void Init(const char* txt, const std::function<void()> cb)
{
name = txt;
callback = cb;
}
std::function<void()> callback;
const char* name;
};
struct Property : UIElement
{
Property()
{
GetStyle().SetStackingDirection(style::StackingDirection::LeftToRight);
}
};
struct Checkbox : UINode
{
void Init(bool& val)
{
at = &val;
}
void Render(UIContainer* ctx) override
{
cb = ctx->Make<UICheckbox>()->SetValue(*at);
}
void OnEvent(UIEvent& e) override
{
if (e.type == UIEventType::Change)
{
*at = cb->GetValue();
}
}
UICheckbox* cb;
bool* at;
};
void Render(UIContainer* ctx) override
{
btnGoBack = nullptr;
if (editing == SIZE_MAX)
{
ctx->Text("List");
ctx->Push<UIPanel>();
for (size_t i = 0; i < items.size(); i++)
{
ctx->Make<ItemButton>()->Init(items[i].name.c_str(), [this, i]() { editing = i; Rerender(); });
}
ctx->Pop();
}
else
{
auto* b = ctx->PushBox();
b->GetStyle().SetStackingDirection(style::StackingDirection::LeftToRight);
ctx->Text("Item:")->GetStyle().SetPadding(5);
ctx->Text(items[editing].name.c_str());
btnGoBack = ctx->Push<UIButton>();
ctx->Text("Go back");
ctx->Pop();
ctx->Pop();
ctx->Push<Property>();
auto s = ctx->Text("Name")->GetStyle();
s.SetPadding(5);
s.SetBoxSizing(style::BoxSizing::BorderBox);
s.SetWidth(style::Coord::Percent(40));
ctx->Make<UITextbox>()->text = items[editing].name;
ctx->Pop();
ctx->Push<Property>();
s = ctx->Text("Enable")->GetStyle();
s.SetPadding(5);
s.SetBoxSizing(style::BoxSizing::BorderBox);
s.SetWidth(style::Coord::Percent(40));
ctx->Make<Checkbox>()->Init(items[editing].enable);
ctx->Pop();
}
}
void OnEvent(UIEvent& e) override
{
if (e.type == UIEventType::Activate)
{
if (e.target->IsChildOrSame(btnGoBack))
{
editing = SIZE_MAX;
Rerender();
}
}
}
size_t editing = SIZE_MAX;
std::vector<Item> items =
{
{ "test item 1", 5, true, 36.6f },
{ "another test item", 123, false, 12.3f },
{ "third item", 333, true, 3.0f },
};
UIButton* btnGoBack;
};
Comments