Advertisement
snake5

some ui code 2

Jul 28th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. struct DataEditor : UINode
  2. {
  3.     struct Item
  4.     {
  5.         std::string name;
  6.         int value;
  7.         bool enable;
  8.         float value2;
  9.     };
  10.  
  11.     struct ItemButton : UINode
  12.     {
  13.         ItemButton()
  14.         {
  15.             GetStyle().SetLayout(style::Layout::Stack);
  16.             //GetStyle().SetMargin(32);
  17.         }
  18.         void Render(UIContainer* ctx) override
  19.         {
  20.             ctx->Push<UIButton>()->GetStyle().SetWidth(style::Coord::Percent(100));
  21.             ctx->Text(name);
  22.             ctx->Pop();
  23.         }
  24.         void OnEvent(UIEvent& e) override
  25.         {
  26.             if (e.type == UIEventType::Activate)
  27.             {
  28.                 callback();
  29.             }
  30.         }
  31.         void Init(const char* txt, const std::function<void()> cb)
  32.         {
  33.             name = txt;
  34.             callback = cb;
  35.         }
  36.         std::function<void()> callback;
  37.         const char* name;
  38.     };
  39.  
  40.     struct Property : UIElement
  41.     {
  42.         Property()
  43.         {
  44.             GetStyle().SetStackingDirection(style::StackingDirection::LeftToRight);
  45.         }
  46.     };
  47.  
  48.     struct Checkbox : UINode
  49.     {
  50.         void Init(bool& val)
  51.         {
  52.             at = &val;
  53.         }
  54.         void Render(UIContainer* ctx) override
  55.         {
  56.             cb = ctx->Make<UICheckbox>()->SetValue(*at);
  57.         }
  58.         void OnEvent(UIEvent& e) override
  59.         {
  60.             if (e.type == UIEventType::Change)
  61.             {
  62.                 *at = cb->GetValue();
  63.             }
  64.         }
  65.         UICheckbox* cb;
  66.         bool* at;
  67.     };
  68.  
  69.     void Render(UIContainer* ctx) override
  70.     {
  71.         btnGoBack = nullptr;
  72.         if (editing == SIZE_MAX)
  73.         {
  74.             ctx->Text("List");
  75.             ctx->Push<UIPanel>();
  76.             for (size_t i = 0; i < items.size(); i++)
  77.             {
  78.                 ctx->Make<ItemButton>()->Init(items[i].name.c_str(), [this, i]() { editing = i; Rerender(); });
  79.             }
  80.             ctx->Pop();
  81.         }
  82.         else
  83.         {
  84.             auto* b = ctx->PushBox();
  85.             b->GetStyle().SetStackingDirection(style::StackingDirection::LeftToRight);
  86.             ctx->Text("Item:")->GetStyle().SetPadding(5);
  87.             ctx->Text(items[editing].name.c_str());
  88.             btnGoBack = ctx->Push<UIButton>();
  89.             ctx->Text("Go back");
  90.             ctx->Pop();
  91.             ctx->Pop();
  92.  
  93.             ctx->Push<Property>();
  94.             auto s = ctx->Text("Name")->GetStyle();
  95.             s.SetPadding(5);
  96.             s.SetBoxSizing(style::BoxSizing::BorderBox);
  97.             s.SetWidth(style::Coord::Percent(40));
  98.             ctx->Make<UITextbox>()->text = items[editing].name;
  99.             ctx->Pop();
  100.  
  101.             ctx->Push<Property>();
  102.             s = ctx->Text("Enable")->GetStyle();
  103.             s.SetPadding(5);
  104.             s.SetBoxSizing(style::BoxSizing::BorderBox);
  105.             s.SetWidth(style::Coord::Percent(40));
  106.             ctx->Make<Checkbox>()->Init(items[editing].enable);
  107.             ctx->Pop();
  108.         }
  109.     }
  110.     void OnEvent(UIEvent& e) override
  111.     {
  112.         if (e.type == UIEventType::Activate)
  113.         {
  114.             if (e.target->IsChildOrSame(btnGoBack))
  115.             {
  116.                 editing = SIZE_MAX;
  117.                 Rerender();
  118.             }
  119.         }
  120.     }
  121.  
  122.     size_t editing = SIZE_MAX;
  123.     std::vector<Item> items =
  124.     {
  125.         { "test item 1", 5, true, 36.6f },
  126.         { "another test item", 123, false, 12.3f },
  127.         { "third item", 333, true, 3.0f },
  128.     };
  129.     UIButton* btnGoBack;
  130. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement