page suppliers

This commit is contained in:
MihailRis 2024-03-21 10:23:33 +03:00
parent c7bc4bb463
commit 2fba78a5f5
4 changed files with 27 additions and 3 deletions

View File

@ -0,0 +1,7 @@
<panel size='400' color='0' interval='1' context='menu'>
<button onclick='menu.page="new-world"' padding='10'>@New World</button>
<panel id='worlds' size='390,1' padding='5' color='#FFFFFF11' max-length='400'>
</panel>
<button onclick='menu.page="settings"' padding='10'>@Settings</button>
<button onclick='core.quit()' padding='10'>@Quit</button>
</panel>

View File

@ -6,6 +6,7 @@
#include <string>
using runnable = std::function<void()>;
template<class T> using supplier = std::function<T()>;
// data sources
using wstringsupplier = std::function<std::wstring()>;

View File

@ -257,10 +257,22 @@ void PagesControl::addPage(std::string name, std::shared_ptr<UINode> panel) {
pages[name] = Page{panel};
}
void PagesControl::addSupplier(std::string name, supplier<std::shared_ptr<UINode>> pageSupplier) {
pageSuppliers[name] = pageSupplier;
}
void PagesControl::setPage(std::string name, bool history) {
auto found = pages.find(name);
Page page;
if (found == pages.end()) {
throw std::runtime_error("no page found");
auto supplier = pageSuppliers.find(name);
if (supplier == pageSuppliers.end()) {
throw std::runtime_error("no page found");
} else {
page.panel = supplier->second();
}
} else {
page = found->second;
}
if (current.panel) {
Container::remove(current.panel);
@ -269,7 +281,7 @@ void PagesControl::setPage(std::string name, bool history) {
pageStack.push(curname);
}
curname = name;
current = found->second;
current = page;
Container::add(current.panel);
setSize(current.panel->getSize());
}

View File

@ -1,12 +1,14 @@
#ifndef GRAPHICS_UI_ELEMENTS_CONTAINERS_H_
#define GRAPHICS_UI_ELEMENTS_CONTAINERS_H_
#include "UINode.h"
#include "../../../delegates.h"
#include <glm/glm.hpp>
#include <vector>
#include <stack>
#include <string>
#include <memory>
#include "UINode.h"
class Batch2D;
class Assets;
@ -96,12 +98,14 @@ namespace gui {
std::stack<std::string> pageStack;
Page current;
std::string curname = "";
std::unordered_map<std::string, supplier<std::shared_ptr<UINode>>> pageSuppliers;
public:
PagesControl();
bool has(const std::string& name);
void setPage(std::string name, bool history=true);
void addPage(std::string name, std::shared_ptr<UINode> panel);
void addSupplier(std::string name, supplier<std::shared_ptr<UINode>> pageSupplier);
void back();
void clearHistory();
void reset();