Refactor, GUI::storage (map for direct access to panels)
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include "../../window/input.h"
|
||||
#include "../../window/Camera.h"
|
||||
|
||||
using std::string;
|
||||
using std::shared_ptr;
|
||||
using namespace gui;
|
||||
|
||||
@@ -101,6 +102,22 @@ void GUI::add(shared_ptr<UINode> panel) {
|
||||
container->add(panel);
|
||||
}
|
||||
|
||||
void GUI::remove(std::shared_ptr<UINode> panel) {
|
||||
void GUI::remove(shared_ptr<UINode> panel) {
|
||||
container->remove(panel);
|
||||
}
|
||||
|
||||
void GUI::store(string name, shared_ptr<UINode> node) {
|
||||
storage[name] = node;
|
||||
}
|
||||
|
||||
shared_ptr<UINode> GUI::get(string name) {
|
||||
auto found = storage.find(name);
|
||||
if (found == storage.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
|
||||
void GUI::remove(string name) {
|
||||
storage.erase(name);
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
|
||||
class Batch2D;
|
||||
class Assets;
|
||||
@@ -50,6 +51,8 @@ namespace gui {
|
||||
std::shared_ptr<UINode> hover = nullptr;
|
||||
std::shared_ptr<UINode> pressed = nullptr;
|
||||
std::shared_ptr<UINode> focus = nullptr;
|
||||
std::unordered_map<std::string, std::shared_ptr<UINode>> storage;
|
||||
|
||||
Camera* uicamera;
|
||||
public:
|
||||
GUI();
|
||||
@@ -62,6 +65,9 @@ namespace gui {
|
||||
void draw(Batch2D* batch, Assets* assets);
|
||||
void add(std::shared_ptr<UINode> panel);
|
||||
void remove(std::shared_ptr<UINode> panel);
|
||||
void store(std::string name, std::shared_ptr<UINode> node);
|
||||
std::shared_ptr<UINode> get(std::string name);
|
||||
void remove(std::string name);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -80,8 +80,9 @@ void Button::mouseRelease(GUI* gui, int x, int y) {
|
||||
}
|
||||
}
|
||||
|
||||
void Button::listenAction(onaction action) {
|
||||
Button* Button::listenAction(onaction action) {
|
||||
actions.push_back(action);
|
||||
return this;
|
||||
}
|
||||
|
||||
TextBox::TextBox(wstring placeholder, vec4 padding)
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace gui {
|
||||
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
|
||||
|
||||
virtual void mouseRelease(GUI*, int x, int y) override;
|
||||
virtual void listenAction(onaction action);
|
||||
virtual Button* listenAction(onaction action);
|
||||
};
|
||||
|
||||
class TextBox : public Panel {
|
||||
|
||||
@@ -75,6 +75,10 @@ void Container::add(shared_ptr<UINode> node) {
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Container::add(UINode* node) {
|
||||
add(shared_ptr<UINode>(node));
|
||||
}
|
||||
|
||||
void Container::remove(shared_ptr<UINode> selected) {
|
||||
selected->setParent(nullptr);
|
||||
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),
|
||||
|
||||
@@ -33,6 +33,7 @@ namespace gui {
|
||||
virtual void draw(Batch2D* batch, Assets* assets) override;
|
||||
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
|
||||
virtual void add(std::shared_ptr<UINode> node);
|
||||
virtual void add(UINode* node);
|
||||
virtual void remove(std::shared_ptr<UINode> node);
|
||||
void listenInterval(float interval, ontimeout callback, int repeat=-1);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user