Optimize parameter passing to avoid unnecessary copying

This commit is contained in:
Pugemon
2024-06-07 04:00:38 +03:00
parent 46ca1bb04a
commit f25a425cb9
123 changed files with 578 additions and 522 deletions
+9 -8
View File
@@ -19,6 +19,7 @@
#include <iostream>
#include <algorithm>
#include <utility>
using namespace gui;
@@ -221,18 +222,18 @@ bool GUI::isFocusCaught() const {
}
void GUI::add(std::shared_ptr<UINode> node) {
container->add(node);
container->add(std::move(node));
}
void GUI::remove(std::shared_ptr<UINode> node) noexcept {
container->remove(node);
container->remove(std::move(node));
}
void GUI::store(std::string name, std::shared_ptr<UINode> node) {
storage[name] = node;
void GUI::store(const std::string& name, std::shared_ptr<UINode> node) {
storage[name] = std::move(node);
}
std::shared_ptr<UINode> GUI::get(std::string name) noexcept {
std::shared_ptr<UINode> GUI::get(const std::string& name) noexcept {
auto found = storage.find(name);
if (found == storage.end()) {
return nullptr;
@@ -240,7 +241,7 @@ std::shared_ptr<UINode> GUI::get(std::string name) noexcept {
return found->second;
}
void GUI::remove(std::string name) noexcept {
void GUI::remove(const std::string& name) noexcept {
storage.erase(name);
}
@@ -248,7 +249,7 @@ void GUI::setFocus(std::shared_ptr<UINode> node) {
if (focus) {
focus->defocus();
}
focus = node;
focus = std::move(node);
if (focus) {
focus->onFocus(this);
}
@@ -258,7 +259,7 @@ std::shared_ptr<Container> GUI::getContainer() const {
return container;
}
void GUI::postRunnable(runnable callback) {
void GUI::postRunnable(const runnable& callback) {
postRunnables.push(callback);
}
+4 -4
View File
@@ -108,16 +108,16 @@ namespace gui {
/// (does not add node to the main container)
/// @param name node key
/// @param node target node
void store(std::string name, std::shared_ptr<UINode> node);
void store(const std::string& name, std::shared_ptr<UINode> node);
/// @brief Get node from the GUI nodes dictionary
/// @param name node key
/// @return stored node or nullptr
std::shared_ptr<UINode> get(std::string name) noexcept;
std::shared_ptr<UINode> get(const std::string& name) noexcept;
/// @brief Remove node from the GUI nodes dictionary
/// @param name node key
void remove(std::string name) noexcept;
void remove(const std::string& name) noexcept;
/// @brief Set node as focused
/// @param node new focused node or nullptr to remove focus
@@ -129,7 +129,7 @@ namespace gui {
void onAssetsLoad(Assets* assets);
void postRunnable(runnable callback);
void postRunnable(const runnable& callback);
void setDoubleClickDelay(float delay);
float getDoubleClickDelay() const;
+6 -4
View File
@@ -1,12 +1,14 @@
#include "Button.hpp"
#include <utility>
#include "Label.hpp"
#include "../../core/DrawContext.hpp"
#include "../../core/Batch2D.hpp"
using namespace gui;
Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
Button::Button(const std::shared_ptr<UINode>& content, glm::vec4 padding)
: Panel(glm::vec2(), padding, 0) {
glm::vec4 margin = getMargin();
setSize(content->getSize()+
@@ -20,9 +22,9 @@ Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
}
Button::Button(
std::wstring text,
const std::wstring& text,
glm::vec4 padding,
onaction action,
const onaction& action,
glm::vec2 size
) : Panel(size, padding, 0)
{
@@ -63,7 +65,7 @@ std::wstring Button::getText() const {
Button* Button::textSupplier(wstringsupplier supplier) {
if (label) {
label->textSupplier(supplier);
label->textSupplier(std::move(supplier));
}
return this;
}
+3 -3
View File
@@ -10,12 +10,12 @@ namespace gui {
protected:
std::shared_ptr<Label> label = nullptr;
public:
Button(std::shared_ptr<UINode> content,
Button(const std::shared_ptr<UINode>& content,
glm::vec4 padding=glm::vec4(2.0f));
Button(std::wstring text,
Button(const std::wstring& text,
glm::vec4 padding,
onaction action,
const onaction& action,
glm::vec2 size=glm::vec2(-1));
virtual void drawBackground(const DrawContext* pctx, Assets* assets) override;
+5 -3
View File
@@ -1,5 +1,7 @@
#include "CheckBox.hpp"
#include <utility>
#include "../../core/DrawContext.hpp"
#include "../../core/Batch2D.hpp"
#include "Label.hpp"
@@ -30,11 +32,11 @@ void CheckBox::mouseRelease(GUI*, int, int) {
}
void CheckBox::setSupplier(boolsupplier supplier) {
this->supplier = supplier;
this->supplier = std::move(supplier);
}
void CheckBox::setConsumer(boolconsumer consumer) {
this->consumer = consumer;
this->consumer = std::move(consumer);
}
CheckBox* CheckBox::setChecked(bool flag) {
@@ -42,7 +44,7 @@ CheckBox* CheckBox::setChecked(bool flag) {
return this;
}
FullCheckBox::FullCheckBox(std::wstring text, glm::vec2 size, bool checked)
FullCheckBox::FullCheckBox(const std::wstring& text, glm::vec2 size, bool checked)
: Panel(size),
checkbox(std::make_shared<CheckBox>(checked)){
setColor(glm::vec4(0.0f));
+5 -3
View File
@@ -1,6 +1,8 @@
#ifndef GRAPHICS_UI_ELEMENTS_CHECKBOX_HPP_
#define GRAPHICS_UI_ELEMENTS_CHECKBOX_HPP_
#include <utility>
#include "Panel.hpp"
namespace gui {
@@ -33,14 +35,14 @@ namespace gui {
protected:
std::shared_ptr<CheckBox> checkbox;
public:
FullCheckBox(std::wstring text, glm::vec2 size, bool checked=false);
FullCheckBox(const std::wstring& text, glm::vec2 size, bool checked=false);
virtual void setSupplier(boolsupplier supplier) {
checkbox->setSupplier(supplier);
checkbox->setSupplier(std::move(supplier));
}
virtual void setConsumer(boolconsumer consumer) {
checkbox->setConsumer(consumer);
checkbox->setConsumer(std::move(consumer));
}
virtual void setChecked(bool flag) {
+9 -8
View File
@@ -4,6 +4,7 @@
#include "../../core/Batch2D.hpp"
#include <algorithm>
#include <utility>
using namespace gui;
@@ -31,7 +32,7 @@ std::shared_ptr<UINode> Container::getAt(glm::vec2 pos, std::shared_ptr<UINode>
}
void Container::act(float delta) {
for (auto node : nodes) {
for (const auto& node : nodes) {
if (node->isVisible()) {
node->act(delta);
}
@@ -86,7 +87,7 @@ void Container::draw(const DrawContext* pctx, Assets* assets) {
{
DrawContext ctx = pctx->sub();
ctx.setScissors(glm::vec4(pos.x, pos.y, size.x, size.y));
for (auto node : nodes) {
for (const auto& node : nodes) {
if (node->isVisible())
node->draw(pctx, assets);
}
@@ -105,22 +106,22 @@ void Container::drawBackground(const DrawContext* pctx, Assets*) {
batch->rect(pos.x, pos.y, size.x, size.y);
}
void Container::add(std::shared_ptr<UINode> node) {
void Container::add(const std::shared_ptr<UINode> &node) {
nodes.push_back(node);
node->setParent(this);
node->reposition();
refresh();
}
void Container::add(std::shared_ptr<UINode> node, glm::vec2 pos) {
void Container::add(const std::shared_ptr<UINode>& node, glm::vec2 pos) {
node->setPos(pos);
add(node);
}
void Container::remove(std::shared_ptr<UINode> selected) {
void Container::remove(const std::shared_ptr<UINode>& selected) {
selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),
[selected](const std::shared_ptr<UINode> node) {
[selected](const std::shared_ptr<UINode>& node) {
return node == selected;
}
), nodes.end());
@@ -136,7 +137,7 @@ void Container::remove(const std::string& id) {
}
void Container::clear() {
for (auto node : nodes) {
for (const auto& node : nodes) {
node->setParent(nullptr);
}
nodes.clear();
@@ -144,7 +145,7 @@ void Container::clear() {
}
void Container::listenInterval(float interval, ontimeout callback, int repeat) {
intervalEvents.push_back({callback, interval, 0.0f, repeat});
intervalEvents.push_back({std::move(callback), interval, 0.0f, repeat});
}
void Container::setSize(glm::vec2 size) {
+3 -3
View File
@@ -22,10 +22,10 @@ namespace gui {
virtual void drawBackground(const DrawContext* pctx, Assets* assets);
virtual void draw(const DrawContext* pctx, 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(std::shared_ptr<UINode> node, glm::vec2 pos);
virtual void add(const std::shared_ptr<UINode> &node);
virtual void add(const std::shared_ptr<UINode> &node, glm::vec2 pos);
virtual void clear();
virtual void remove(std::shared_ptr<UINode> node);
virtual void remove(const std::shared_ptr<UINode>& node);
virtual void remove(const std::string& id);
virtual void scrolled(int value) override;
virtual void setScrollable(bool flag);
+3 -1
View File
@@ -1,5 +1,7 @@
#include "Image.hpp"
#include <utility>
#include "../../core/DrawContext.hpp"
#include "../../core/Batch2D.hpp"
#include "../../core/Texture.hpp"
@@ -8,7 +10,7 @@
using namespace gui;
Image::Image(std::string texture, glm::vec2 size) : UINode(size), texture(texture) {
Image::Image(std::string texture, glm::vec2 size) : UINode(size), texture(std::move(texture)) {
setInteractive(false);
}
+9 -8
View File
@@ -25,6 +25,7 @@
#include "../GUI.hpp"
#include <glm/glm.hpp>
#include <utility>
using namespace gui;
@@ -40,9 +41,9 @@ SlotLayout::SlotLayout(
position(position),
background(background),
itemSource(itemSource),
updateFunc(updateFunc),
shareFunc(shareFunc),
rightClick(rightClick) {}
updateFunc(std::move(updateFunc)),
shareFunc(std::move(shareFunc)),
rightClick(std::move(rightClick)) {}
InventoryBuilder::InventoryBuilder() {
view = std::make_shared<InventoryView>();
@@ -53,7 +54,7 @@ void InventoryBuilder::addGrid(
glm::vec2 pos,
int padding,
bool addpanel,
SlotLayout slotLayout
const SlotLayout& slotLayout
) {
const int slotSize = InventoryView::SLOT_SIZE;
const int interval = InventoryView::SLOT_INTERVAL;
@@ -95,7 +96,7 @@ void InventoryBuilder::addGrid(
}
}
void InventoryBuilder::add(SlotLayout layout) {
void InventoryBuilder::add(const SlotLayout& layout) {
view->add(view->addSlot(layout), layout.position);
}
@@ -106,7 +107,7 @@ std::shared_ptr<InventoryView> InventoryBuilder::build() {
SlotView::SlotView(
SlotLayout layout
) : UINode(glm::vec2(InventoryView::SLOT_SIZE)),
layout(layout)
layout(std::move(layout))
{
setColor(glm::vec4(0, 0, 0, 0.2f));
setTooltipDelay(0.05f);
@@ -309,7 +310,7 @@ InventoryView::InventoryView() : Container(glm::vec2()) {
InventoryView::~InventoryView() {}
std::shared_ptr<SlotView> InventoryView::addSlot(SlotLayout layout) {
std::shared_ptr<SlotView> InventoryView::addSlot(const SlotLayout& layout) {
uint width = InventoryView::SLOT_SIZE + layout.padding;
uint height = InventoryView::SLOT_SIZE + layout.padding;
@@ -341,7 +342,7 @@ size_t InventoryView::getSlotsCount() const {
}
void InventoryView::bind(
std::shared_ptr<Inventory> inventory,
const std::shared_ptr<Inventory>& inventory,
const Content* content
) {
this->inventory = inventory;
+4 -4
View File
@@ -96,13 +96,13 @@ namespace gui {
void setSelected(int index);
void bind(
std::shared_ptr<Inventory> inventory,
const std::shared_ptr<Inventory>& inventory,
const Content* content
);
void unbind();
std::shared_ptr<SlotView> addSlot(SlotLayout layout);
std::shared_ptr<SlotView> addSlot(const SlotLayout& layout);
std::shared_ptr<Inventory> getInventory() const;
@@ -130,10 +130,10 @@ namespace gui {
glm::vec2 pos,
int padding,
bool addpanel,
SlotLayout slotLayout
const SlotLayout& slotLayout
);
void add(SlotLayout slotLayout);
void add(const SlotLayout& slotLayout);
std::shared_ptr<InventoryView> build();
};
}
+9 -7
View File
@@ -1,4 +1,6 @@
#include "Label.hpp"
#include <utility>
#include "../../core/DrawContext.hpp"
#include "../../core/Batch2D.hpp"
#include "../../core/Font.hpp"
@@ -45,20 +47,20 @@ void LabelCache::update(const std::wstring& text, bool multiline, bool wrap) {
}
}
Label::Label(std::string text, std::string fontName)
Label::Label(const std::string& text, std::string fontName)
: UINode(glm::vec2(text.length() * 8, 15)),
text(util::str2wstr_utf8(text)),
fontName(fontName)
fontName(std::move(fontName))
{
setInteractive(false);
cache.update(this->text, multiline, textWrap);
}
Label::Label(std::wstring text, std::string fontName)
Label::Label(const std::wstring& text, std::string fontName)
: UINode(glm::vec2(text.length() * 8, 15)),
text(text),
fontName(fontName)
fontName(std::move(fontName))
{
setInteractive(false);
cache.update(this->text, multiline, textWrap);
@@ -76,7 +78,7 @@ glm::vec2 Label::calcSize() {
);
}
void Label::setText(std::wstring text) {
void Label::setText(const std::wstring& text) {
if (text == this->text && !cache.resetFlag) {
return;
}
@@ -93,7 +95,7 @@ const std::wstring& Label::getText() const {
}
void Label::setFontName(std::string name) {
this->fontName = name;
this->fontName = std::move(name);
}
const std::string& Label::getFontName() const {
@@ -207,7 +209,7 @@ void Label::draw(const DrawContext* pctx, Assets* assets) {
}
void Label::textSupplier(wstringsupplier supplier) {
this->supplier = supplier;
this->supplier = std::move(supplier);
}
void Label::setAutoResize(bool flag) {
+3 -3
View File
@@ -53,10 +53,10 @@ namespace gui {
/// @brief Auto resize label to fit text
bool autoresize = false;
public:
Label(std::string text, std::string fontName="normal");
Label(std::wstring text, std::string fontName="normal");
Label(const std::string& text, std::string fontName="normal");
Label(const std::wstring& text, std::string fontName="normal");
virtual void setText(std::wstring text);
virtual void setText(const std::wstring& text);
const std::wstring& getText() const;
virtual void setFontName(std::string name);
+6 -5
View File
@@ -1,6 +1,7 @@
#include "Menu.hpp"
#include <stdexcept>
#include <utility>
using namespace gui;
@@ -12,11 +13,11 @@ bool Menu::has(const std::string& name) {
pageSuppliers.find(name) != pageSuppliers.end();
}
void Menu::addPage(std::string name, std::shared_ptr<UINode> panel) {
void Menu::addPage(const std::string& name, const std::shared_ptr<UINode> &panel) {
pages[name] = Page{name, panel};
}
void Menu::addSupplier(std::string name, supplier<std::shared_ptr<UINode>> pageSupplier) {
void Menu::addSupplier(const std::string &name, const supplier<std::shared_ptr<UINode>> &pageSupplier) {
pageSuppliers[name] = pageSupplier;
}
@@ -38,7 +39,7 @@ std::shared_ptr<UINode> Menu::fetchPage(const std::string& name) {
}
}
void Menu::setPage(std::string name, bool history) {
void Menu::setPage(const std::string &name, bool history) {
Page page {name, fetchPage(name)};
if (page.panel == nullptr) {
throw std::runtime_error("no page found");
@@ -53,7 +54,7 @@ void Menu::setPage(Page page, bool history) {
pageStack.push(current);
}
}
current = page;
current = std::move(page);
Container::add(current.panel);
setSize(current.panel->getSize());
}
@@ -73,7 +74,7 @@ void Menu::back() {
}
void Menu::setPageLoader(page_loader_func loader) {
pagesLoader = loader;
pagesLoader = std::move(loader);
}
Page& Menu::getCurrent() {
+3 -3
View File
@@ -30,15 +30,15 @@ namespace gui {
/// @brief Set current page to specified one.
/// @param name page or page supplier name
/// @param history previous page will not be saved in history if false
void setPage(std::string name, bool history=true);
void setPage(const std::string &name, bool history=true);
void setPage(Page page, bool history=true);
void addPage(std::string name, std::shared_ptr<UINode> panel);
void addPage(const std::string& name, const std::shared_ptr<UINode> &panel);
std::shared_ptr<UINode> fetchPage(const std::string& name);
/// @brief Add page supplier used if page is not found
/// @param name page name
/// @param pageSupplier page supplier function
void addSupplier(std::string name, supplier<std::shared_ptr<UINode>> pageSupplier);
void addSupplier(const std::string &name, const supplier<std::shared_ptr<UINode>> &pageSupplier);
/// @brief Page loader is called if accessed page is not found
void setPageLoader(page_loader_func loader);
+1 -1
View File
@@ -46,7 +46,7 @@ void Panel::fullRefresh() {
Container::fullRefresh();
}
void Panel::add(std::shared_ptr<UINode> node) {
void Panel::add(const std::shared_ptr<UINode> &node) {
node->setResizing(true);
Container::add(node);
fullRefresh();
+1 -1
View File
@@ -24,7 +24,7 @@ namespace gui {
virtual void setOrientation(Orientation orientation);
Orientation getOrientation() const;
virtual void add(std::shared_ptr<UINode> node) override;
virtual void add(const std::shared_ptr<UINode> &node) override;
virtual void refresh() override;
virtual void fullRefresh() override;
+9 -7
View File
@@ -1,5 +1,7 @@
#include "TextBox.hpp"
#include <utility>
#include "Label.hpp"
#include "../../core/DrawContext.hpp"
#include "../../core/Batch2D.hpp"
@@ -14,7 +16,7 @@ using namespace gui;
TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
: Panel(glm::vec2(200,32), padding, 0),
input(L""),
placeholder(placeholder)
placeholder(std::move(placeholder))
{
setOnUpPressed(nullptr);
setOnDownPressed(nullptr);
@@ -556,7 +558,7 @@ std::shared_ptr<UINode> TextBox::getAt(glm::vec2 pos, std::shared_ptr<UINode> se
return UINode::getAt(pos, self);
}
void TextBox::setOnUpPressed(runnable callback) {
void TextBox::setOnUpPressed(const runnable &callback) {
if (callback == nullptr) {
onUpPressed = [this]() {
bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT);
@@ -568,7 +570,7 @@ void TextBox::setOnUpPressed(runnable callback) {
}
}
void TextBox::setOnDownPressed(runnable callback) {
void TextBox::setOnDownPressed(const runnable &callback) {
if (callback == nullptr) {
onDownPressed = [this]() {
bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT);
@@ -581,15 +583,15 @@ void TextBox::setOnDownPressed(runnable callback) {
}
void TextBox::setTextSupplier(wstringsupplier supplier) {
this->supplier = supplier;
this->supplier = std::move(supplier);
}
void TextBox::setTextConsumer(wstringconsumer consumer) {
this->consumer = consumer;
this->consumer = std::move(consumer);
}
void TextBox::setTextValidator(wstringchecker validator) {
this->validator = validator;
this->validator = std::move(validator);
}
void TextBox::setFocusedColor(glm::vec4 color) {
@@ -614,7 +616,7 @@ std::wstring TextBox::getText() const {
return input;
}
void TextBox::setText(const std::wstring value) {
void TextBox::setText(const std::wstring& value) {
this->input = value;
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
}
+3 -3
View File
@@ -98,7 +98,7 @@ namespace gui {
virtual std::wstring getText() const;
/// @brief Set TextBox content text
virtual void setText(std::wstring value);
virtual void setText(const std::wstring &value);
/// @brief Get text placeholder
virtual std::wstring getPlaceholder() const;
@@ -169,8 +169,8 @@ namespace gui {
virtual void typed(unsigned int codepoint) override;
virtual void keyPressed(keycode key) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void setOnUpPressed(runnable callback);
virtual void setOnDownPressed(runnable callback);
virtual void setOnUpPressed(const runnable &callback);
virtual void setOnDownPressed(const runnable &callback);
};
}
+4 -2
View File
@@ -1,5 +1,7 @@
#include "TrackBar.hpp"
#include <utility>
#include "../../core/DrawContext.hpp"
#include "../../core/Batch2D.hpp"
#include "../../../assets/Assets.hpp"
@@ -41,11 +43,11 @@ void TrackBar::draw(const DrawContext* pctx, Assets*) {
}
void TrackBar::setSupplier(doublesupplier supplier) {
this->supplier = supplier;
this->supplier = std::move(supplier);
}
void TrackBar::setConsumer(doubleconsumer consumer) {
this->consumer = consumer;
this->consumer = std::move(consumer);
}
void TrackBar::mouseMove(GUI*, int x, int) {
+13 -11
View File
@@ -1,5 +1,7 @@
#include "UINode.hpp"
#include <utility>
#include "Container.hpp"
#include "../../core/Batch2D.hpp"
@@ -62,12 +64,12 @@ UINode* UINode::getParent() const {
return parent;
}
UINode* UINode::listenAction(onaction action) {
UINode* UINode::listenAction(const onaction& action) {
actions.listen(action);
return this;
}
UINode* UINode::listenDoubleClick(onaction action) {
UINode* UINode::listenDoubleClick(const onaction& action) {
doubleClickCallbacks.listen(action);
return this;
}
@@ -113,7 +115,7 @@ std::shared_ptr<UINode> UINode::getAt(glm::vec2 point, std::shared_ptr<UINode> s
if (!isInteractive() || !enabled) {
return nullptr;
}
return isInside(point) ? self : nullptr;
return isInside(point) ? std::move(self) : nullptr;
}
bool UINode::isInteractive() const {
@@ -241,8 +243,8 @@ int UINode::getZIndex() const {
}
void UINode::moveInto(
std::shared_ptr<UINode> node,
std::shared_ptr<Container> dest
const std::shared_ptr<UINode>& node,
const std::shared_ptr<Container>& dest
) {
auto parent = node->getParent();
if (auto container = dynamic_cast<Container*>(parent)) {
@@ -259,7 +261,7 @@ vec2supplier UINode::getPositionFunc() const {
}
void UINode::setPositionFunc(vec2supplier func) {
positionfunc = func;
positionfunc = std::move(func);
}
vec2supplier UINode::getSizeFunc() const {
@@ -267,7 +269,7 @@ vec2supplier UINode::getSizeFunc() const {
}
void UINode::setSizeFunc(vec2supplier func) {
sizefunc = func;
sizefunc = std::move(func);
}
void UINode::setId(const std::string& id) {
@@ -345,7 +347,7 @@ bool UINode::isSubnodeOf(const UINode* node) {
}
void UINode::getIndices(
const std::shared_ptr<UINode> node,
const std::shared_ptr<UINode>& node,
std::unordered_map<std::string, std::shared_ptr<UINode>>& map
) {
const std::string& id = node->getId();
@@ -354,20 +356,20 @@ void UINode::getIndices(
}
auto container = std::dynamic_pointer_cast<gui::Container>(node);
if (container) {
for (auto subnode : container->getNodes()) {
for (const auto& subnode : container->getNodes()) {
getIndices(subnode, map);
}
}
}
std::shared_ptr<UINode> UINode::find(
const std::shared_ptr<UINode> node,
const std::shared_ptr<UINode>& node,
const std::string& id
) {
if (node->getId() == id) {
return node;
}
if (auto container = std::dynamic_pointer_cast<Container>(node)) {
for (auto subnode : container->getNodes()) {
for (const auto& subnode : container->getNodes()) {
if (auto found = UINode::find(subnode, id)) {
return found;
}
+7 -7
View File
@@ -25,7 +25,7 @@ namespace gui {
class ActionsSet {
std::unique_ptr<std::vector<onaction>> callbacks;
public:
void listen(onaction callback) {
void listen(const onaction& callback) {
if (callbacks == nullptr) {
callbacks = std::make_unique<std::vector<onaction>>();
}
@@ -162,8 +162,8 @@ namespace gui {
/// @brief Get element z-index
int getZIndex() const;
virtual UINode* listenAction(onaction action);
virtual UINode* listenDoubleClick(onaction action);
virtual UINode* listenAction(const onaction &action);
virtual UINode* listenDoubleClick(const onaction &action);
virtual void onFocus(GUI*) {focused = true;}
virtual void doubleClick(GUI*, int x, int y);
@@ -227,8 +227,8 @@ namespace gui {
}
};
static void moveInto(
std::shared_ptr<UINode> node,
std::shared_ptr<Container> dest
const std::shared_ptr<UINode>& node,
const std::shared_ptr<Container>& dest
);
virtual vec2supplier getPositionFunc() const;
@@ -249,12 +249,12 @@ namespace gui {
/// @brief collect all nodes having id
static void getIndices(
const std::shared_ptr<UINode> node,
const std::shared_ptr<UINode>& node,
std::unordered_map<std::string, std::shared_ptr<UINode>>& map
);
static std::shared_ptr<UINode> find(
const std::shared_ptr<UINode> node,
const std::shared_ptr<UINode>& node,
const std::string& id
);
};
+2 -2
View File
@@ -22,7 +22,7 @@ std::shared_ptr<gui::UINode> guiutil::create(const std::string& source, scripten
return reader.readXML("<string>", source);
}
void guiutil::alert(GUI* gui, const std::wstring& text, runnable on_hidden) {
void guiutil::alert(GUI* gui, const std::wstring& text, const runnable& on_hidden) {
auto menu = gui->getMenu();
auto panel = std::make_shared<Panel>(glm::vec2(500, 300), glm::vec4(8.0f), 8.0f);
panel->setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));
@@ -48,7 +48,7 @@ void guiutil::alert(GUI* gui, const std::wstring& text, runnable on_hidden) {
void guiutil::confirm(
GUI* gui,
const std::wstring& text,
runnable on_confirm,
const runnable& on_confirm,
std::wstring yestext,
std::wstring notext) {
if (yestext.empty()) yestext = langs::get(L"Yes");
+2 -2
View File
@@ -16,13 +16,13 @@ namespace guiutil {
void alert(
gui::GUI* gui,
const std::wstring& text,
runnable on_hidden=nullptr
const runnable& on_hidden=nullptr
);
void confirm(
gui::GUI* gui,
const std::wstring& text,
runnable on_confirm=nullptr,
const runnable& on_confirm=nullptr,
std::wstring yestext=L"",
std::wstring notext=L"");
}
+23 -22
View File
@@ -19,6 +19,7 @@
#include "../../window/Events.hpp"
#include <stdexcept>
#include <utility>
using namespace gui;
@@ -52,7 +53,7 @@ static Gravity gravity_from_string(const std::string& str) {
static runnable create_runnable(
const UiXmlReader& reader,
xml::xmlelement element,
const xml::xmlelement& element,
const std::string& name
) {
if (element->has(name)) {
@@ -66,7 +67,7 @@ static runnable create_runnable(
return nullptr;
}
static onaction create_action(UiXmlReader& reader, xml::xmlelement element, const std::string& name) {
static onaction create_action(UiXmlReader& reader, const xml::xmlelement& element, const std::string& name) {
auto callback = create_runnable(reader, element, name);
if (callback == nullptr) {
return nullptr;
@@ -75,7 +76,7 @@ static onaction create_action(UiXmlReader& reader, xml::xmlelement element, cons
}
/* Read basic UINode properties */
static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) {
static void _readUINode(UiXmlReader& reader, const xml::xmlelement& element, UINode& node) {
if (element->has("id")) {
node.setId(element->attr("id").getText());
}
@@ -159,7 +160,7 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no
}
}
static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Container& container) {
static void _readContainer(UiXmlReader& reader, const xml::xmlelement& element, Container& container) {
_readUINode(reader, element, container);
if (element->has("scrollable")) {
@@ -175,15 +176,15 @@ static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Contain
}
}
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, Container& container) {
void UiXmlReader::readUINode(UiXmlReader& reader, const xml::xmlelement& element, Container& container) {
_readContainer(reader, element, container);
}
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) {
void UiXmlReader::readUINode(UiXmlReader& reader, const xml::xmlelement& element, UINode& node) {
_readUINode(reader, element, node);
}
static void _readPanel(UiXmlReader& reader, xml::xmlelement element, Panel& panel, bool subnodes=true) {
static void _readPanel(UiXmlReader& reader, const xml::xmlelement& element, Panel& panel, bool subnodes=true) {
_readUINode(reader, element, panel);
if (element->has("padding")) {
@@ -219,7 +220,7 @@ static void _readPanel(UiXmlReader& reader, xml::xmlelement element, Panel& pane
}
}
static std::wstring readAndProcessInnerText(xml::xmlelement element, const std::string& context) {
static std::wstring readAndProcessInnerText(const xml::xmlelement& element, const std::string& context) {
std::wstring text = L"";
if (element->size() == 1) {
std::string source = element->sub(0)->attr("#").getText();
@@ -236,7 +237,7 @@ static std::wstring readAndProcessInnerText(xml::xmlelement element, const std::
return text;
}
static std::shared_ptr<UINode> readLabel(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readLabel(UiXmlReader& reader, const xml::xmlelement& element) {
std::wstring text = readAndProcessInnerText(element, reader.getContext());
auto label = std::make_shared<Label>(text);
_readUINode(reader, element, *label);
@@ -267,20 +268,20 @@ static std::shared_ptr<UINode> readLabel(UiXmlReader& reader, xml::xmlelement el
return label;
}
static std::shared_ptr<UINode> readContainer(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readContainer(UiXmlReader& reader, const xml::xmlelement& element) {
auto container = std::make_shared<Container>(glm::vec2());
_readContainer(reader, element, *container);
return container;
}
static std::shared_ptr<UINode> readPanel(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readPanel(UiXmlReader& reader, const xml::xmlelement& element) {
float interval = element->attr("interval", "2").asFloat();
auto panel = std::make_shared<Panel>(glm::vec2(), glm::vec4(), interval);
_readPanel(reader, element, *panel);
return panel;
}
static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readButton(UiXmlReader& reader, const xml::xmlelement& element) {
glm::vec4 padding = element->attr("padding", "10").asVec4();
std::shared_ptr<Button> button;
@@ -304,7 +305,7 @@ static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement e
return button;
}
static std::shared_ptr<UINode> readCheckBox(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readCheckBox(UiXmlReader& reader, const xml::xmlelement& element) {
auto text = readAndProcessInnerText(element, reader.getContext());
bool checked = element->attr("checked", "false").asBool();
auto checkbox = std::make_shared<FullCheckBox>(text, glm::vec2(32), checked);
@@ -328,7 +329,7 @@ static std::shared_ptr<UINode> readCheckBox(UiXmlReader& reader, xml::xmlelement
return checkbox;
}
static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, const xml::xmlelement& element) {
auto placeholder = util::str2wstr_utf8(element->attr("placeholder", "").getText());
auto text = readAndProcessInnerText(element, reader.getContext());
auto textbox = std::make_shared<TextBox>(placeholder, glm::vec4(0.0f));
@@ -383,14 +384,14 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
return textbox;
}
static std::shared_ptr<UINode> readImage(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readImage(UiXmlReader& reader, const xml::xmlelement& element) {
std::string src = element->attr("src", "").getText();
auto image = std::make_shared<Image>(src);
_readUINode(reader, element, *image);
return image;
}
static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, const xml::xmlelement& element) {
float min = element->attr("min", "0.0").asFloat();
float max = element->attr("max", "1.0").asFloat();
float def = element->attr("value", "0.0").asFloat();
@@ -418,7 +419,7 @@ static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, xml::xmlelement
return bar;
}
static std::shared_ptr<UINode> readInputBindBox(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readInputBindBox(UiXmlReader& reader, const xml::xmlelement& element) {
auto bindname = element->attr("binding").getText();
auto found = Events::bindings.find(bindname);
if (found == Events::bindings.end()) {
@@ -518,7 +519,7 @@ static void readSlotsGrid(InventoryView* view, UiXmlReader& reader, xml::xmlelem
}
}
static std::shared_ptr<UINode> readInventory(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readInventory(UiXmlReader& reader, const xml::xmlelement& element) {
auto view = std::make_shared<InventoryView>();
view->setColor(glm::vec4(0.122f, 0.122f, 0.122f, 0.878f)); // todo: fixme
reader.addIgnore("slot");
@@ -535,7 +536,7 @@ static std::shared_ptr<UINode> readInventory(UiXmlReader& reader, xml::xmlelemen
return view;
}
static std::shared_ptr<UINode> readPageBox(UiXmlReader& reader, xml::xmlelement element) {
static std::shared_ptr<UINode> readPageBox(UiXmlReader& reader, const xml::xmlelement& element) {
auto menu = std::make_shared<Menu>();
// fixme
menu->setPageLoader(menus::create_page_loader(scripting::engine));
@@ -560,7 +561,7 @@ UiXmlReader::UiXmlReader(const scriptenv& env) : env(env) {
}
void UiXmlReader::add(const std::string& tag, uinode_reader reader) {
readers[tag] = reader;
readers[tag] = std::move(reader);
}
bool UiXmlReader::hasReader(const std::string& tag) const {
@@ -571,7 +572,7 @@ void UiXmlReader::addIgnore(const std::string& tag) {
ignored.insert(tag);
}
std::shared_ptr<UINode> UiXmlReader::readUINode(xml::xmlelement element) {
std::shared_ptr<UINode> UiXmlReader::readUINode(const xml::xmlelement& element) {
if (element->has("if")) {
const auto& cond = element->attr("if").getText();
if (cond.empty() || cond == "false" || cond == "nil")
@@ -615,7 +616,7 @@ std::shared_ptr<UINode> UiXmlReader::readXML(
std::shared_ptr<UINode> UiXmlReader::readXML(
const std::string& filename,
xml::xmlelement root
const xml::xmlelement& root
) {
this->filename = filename;
return readUINode(root);
+4 -4
View File
@@ -27,17 +27,17 @@ namespace gui {
bool hasReader(const std::string& tag) const;
void addIgnore(const std::string& tag);
std::shared_ptr<UINode> readUINode(xml::xmlelement element);
std::shared_ptr<UINode> readUINode(const xml::xmlelement& element);
void readUINode(
UiXmlReader& reader,
xml::xmlelement element,
const xml::xmlelement& element,
UINode& node
);
void readUINode(
UiXmlReader& reader,
xml::xmlelement element,
const xml::xmlelement& element,
Container& container
);
@@ -48,7 +48,7 @@ namespace gui {
std::shared_ptr<UINode> readXML(
const std::string& filename,
xml::xmlelement root
const xml::xmlelement& root
);
const std::string& getContext() const;