diff --git a/src/coders/GLSLExtension.cpp b/src/coders/GLSLExtension.cpp index f3f2e29a..1e25c934 100644 --- a/src/coders/GLSLExtension.cpp +++ b/src/coders/GLSLExtension.cpp @@ -42,10 +42,10 @@ const std::string& GLSLExtension::getHeader(const std::string& name) const { return found->second; } -const std::string GLSLExtension::getDefine(const std::string& name) const { +const std::string& GLSLExtension::getDefine(const std::string& name) const { auto found = defines.find(name); if (found == defines.end()) { - return ""; + throw std::runtime_error("name '"+name+"' is not defined"); } return found->second; } @@ -84,7 +84,7 @@ inline void source_line(std::stringstream& ss, uint linenum) { ss << "#line " << linenum << "\n"; } -const std::string GLSLExtension::process(const fs::path& file, const std::string& source) { +std::string GLSLExtension::process(const fs::path& file, const std::string& source) { std::stringstream ss; size_t pos = 0; uint linenum = 1; diff --git a/src/coders/GLSLExtension.hpp b/src/coders/GLSLExtension.hpp index 077c59ea..98884c28 100644 --- a/src/coders/GLSLExtension.hpp +++ b/src/coders/GLSLExtension.hpp @@ -24,12 +24,12 @@ public: void addHeader(const std::string& name, std::string source); const std::string& getHeader(const std::string& name) const; - const std::string getDefine(const std::string& name) const; + const std::string& getDefine(const std::string& name) const; bool hasHeader(const std::string& name) const; bool hasDefine(const std::string& name) const; - const std::string process( + std::string process( const std::filesystem::path& file, const std::string& source ); diff --git a/src/coders/xml.cpp b/src/coders/xml.cpp index e2d9925c..51c3046b 100644 --- a/src/coders/xml.cpp +++ b/src/coders/xml.cpp @@ -120,7 +120,7 @@ const std::string& Node::getTag() const { return tag; } -const xmlattribute Node::attr(const std::string& name) const { +const xmlattribute& Node::attr(const std::string& name) const { auto found = attrs.find(name); if (found == attrs.end()) { throw std::runtime_error("element <"+tag+" ...> missing attribute "+name); @@ -128,7 +128,7 @@ const xmlattribute Node::attr(const std::string& name) const { return found->second; } -const xmlattribute Node::attr(const std::string& name, const std::string& def) const { +xmlattribute Node::attr(const std::string& name, const std::string& def) const { auto found = attrs.find(name); if (found == attrs.end()) { return Attribute(name, def); diff --git a/src/coders/xml.hpp b/src/coders/xml.hpp index 92004f54..c70ce9c2 100644 --- a/src/coders/xml.hpp +++ b/src/coders/xml.hpp @@ -68,14 +68,14 @@ namespace xml { /// @param name attribute name /// @throws std::runtime_error if element has no attribute /// @return xmlattribute - {name, value} - const xmlattribute attr(const std::string& name) const; + const xmlattribute& attr(const std::string& name) const; /// @brief Get attribute by name /// @param name attribute name /// @param def default value will be returned wrapped in xmlattribute /// if element has no attribute /// @return xmlattribute - {name, value} or {name, def} if not found*/ - const xmlattribute attr(const std::string& name, const std::string& def) const; + xmlattribute attr(const std::string& name, const std::string& def) const; /// @brief Check if element has attribute /// @param name attribute name diff --git a/src/frontend/UiDocument.cpp b/src/frontend/UiDocument.cpp index face749f..3951e4e9 100644 --- a/src/frontend/UiDocument.cpp +++ b/src/frontend/UiDocument.cpp @@ -33,11 +33,11 @@ const std::string& UiDocument::getId() const { return id; } -const std::shared_ptr UiDocument::getRoot() const { +std::shared_ptr UiDocument::getRoot() const { return root; } -const std::shared_ptr UiDocument::get(const std::string& id) const { +std::shared_ptr UiDocument::get(const std::string& id) const { auto found = map.find(id); if (found == map.end()) { return nullptr; diff --git a/src/frontend/UiDocument.hpp b/src/frontend/UiDocument.hpp index c571449d..504411a9 100644 --- a/src/frontend/UiDocument.hpp +++ b/src/frontend/UiDocument.hpp @@ -41,8 +41,8 @@ public: const std::string& getId() const; const uinodes_map& getMap() const; uinodes_map& getMapWriteable(); - const std::shared_ptr getRoot() const; - const std::shared_ptr get(const std::string& id) const; + std::shared_ptr getRoot() const; + std::shared_ptr get(const std::string& id) const; const uidocscript& getScript() const; scriptenv getEnvironment() const; diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index d05f028d..b364425f 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -114,8 +114,21 @@ SlotView::SlotView( } void SlotView::draw(const DrawContext* pctx, Assets* assets) { - if (bound == nullptr) + if (bound == nullptr) { return; + } + itemid_t itemid = bound->getItemId(); + if (itemid != prevItem) { + if (itemid) { + auto def = content->getIndices()->getItemDef(itemid); + tooltip = util::pascal_case( + langs::get(util::str2wstr_utf8(def->caption)) + ); + } else { + tooltip = L""; + } + } + prevItem = itemid; const int slotSize = InventoryView::SLOT_SIZE; @@ -274,15 +287,12 @@ void SlotView::onFocus(gui::GUI* gui) { clicked(gui, mousecode::BUTTON_1); } -const std::wstring SlotView::getTooltip() const { - const auto str = UINode::getTooltip(); - if (!str.empty() || bound->isEmpty()) { +const std::wstring& SlotView::getTooltip() const { + const auto& str = UINode::getTooltip(); + if (!str.empty() || tooltip.empty()) { return str; } - auto def = content->getIndices()->getItemDef(bound->getItemId()); - return util::pascal_case( - langs::get(util::str2wstr_utf8(def->caption)) - ); // TODO: cache + return tooltip; } void SlotView::bind( diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index f5762d34..476b4d99 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -53,6 +53,9 @@ namespace gui { int64_t inventoryid = 0; ItemStack* bound = nullptr; + + std::wstring tooltip; + itemid_t prevItem = 0; public: SlotView(SlotLayout layout); @@ -63,7 +66,7 @@ namespace gui { virtual void clicked(gui::GUI*, mousecode) override; virtual void onFocus(gui::GUI*) override; - virtual const std::wstring getTooltip() const override; + virtual const std::wstring& getTooltip() const override; void bind( int64_t inventoryid, diff --git a/src/graphics/ui/elements/UINode.cpp b/src/graphics/ui/elements/UINode.cpp index 113161cf..b2c96721 100644 --- a/src/graphics/ui/elements/UINode.cpp +++ b/src/graphics/ui/elements/UINode.cpp @@ -138,7 +138,7 @@ void UINode::setTooltip(const std::wstring& text) { this->tooltip = text; } -const std::wstring UINode::getTooltip() const { +const std::wstring& UINode::getTooltip() const { return tooltip; } diff --git a/src/graphics/ui/elements/UINode.hpp b/src/graphics/ui/elements/UINode.hpp index c29a4461..056b933f 100644 --- a/src/graphics/ui/elements/UINode.hpp +++ b/src/graphics/ui/elements/UINode.hpp @@ -202,7 +202,7 @@ namespace gui { virtual bool isResizing() const; virtual void setTooltip(const std::wstring& text); - virtual const std::wstring getTooltip() const; + virtual const std::wstring& getTooltip() const; virtual void setTooltipDelay(float delay); virtual float getTooltipDelay() const; diff --git a/src/logic/CommandsInterpreter.hpp b/src/logic/CommandsInterpreter.hpp index 60cdce97..7d7d8508 100644 --- a/src/logic/CommandsInterpreter.hpp +++ b/src/logic/CommandsInterpreter.hpp @@ -120,7 +120,7 @@ namespace cmd { ); Command* get(const std::string& name); - const std::unordered_map getCommands() const { + const std::unordered_map& getCommands() const { return commands; } }; diff --git a/src/logic/scripting/lua/LuaState.cpp b/src/logic/scripting/lua/LuaState.cpp index 49f83439..524e3049 100644 --- a/src/logic/scripting/lua/LuaState.cpp +++ b/src/logic/scripting/lua/LuaState.cpp @@ -68,7 +68,7 @@ LuaState::LuaState() { setglobal(LAMBDAS_TABLE); } -const std::string LuaState::envName(int env) { +std::string LuaState::envName(int env) { return "_ENV"+util::mangleid(env); } diff --git a/src/logic/scripting/lua/LuaState.hpp b/src/logic/scripting/lua/LuaState.hpp index 58c538bd..74c0257d 100644 --- a/src/logic/scripting/lua/LuaState.hpp +++ b/src/logic/scripting/lua/LuaState.hpp @@ -29,7 +29,7 @@ namespace lua { LuaState(); ~LuaState(); - static const std::string envName(int env); + static std::string envName(int env); void loadbuffer(int env, const std::string& src, const std::string& file); int gettop() const; int pushivec3(lua_Integer x, lua_Integer y, lua_Integer z); diff --git a/src/window/input.hpp b/src/window/input.hpp index 50e26446..7c966087 100644 --- a/src/window/input.hpp +++ b/src/window/input.hpp @@ -157,7 +157,7 @@ struct Binding { void reset(keycode); void reset(mousecode); - inline const std::string text() const { + inline std::string text() const { switch (type) { case inputtype::keyboard: { return input_util::to_string(static_cast(code));