added tooltips
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#include "InventoryView.hpp"
|
||||
|
||||
#include "../../../assets/Assets.hpp"
|
||||
#include "../../../content/Content.hpp"
|
||||
#include "../../../frontend/LevelFrontend.hpp"
|
||||
#include "../../../frontend/locale.hpp"
|
||||
#include "../../../items/Inventories.hpp"
|
||||
#include "../../../items/Inventory.hpp"
|
||||
#include "../../../items/ItemDef.hpp"
|
||||
@@ -22,7 +24,6 @@
|
||||
#include "../../render/BlocksPreview.hpp"
|
||||
#include "../GUI.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
using namespace gui;
|
||||
@@ -270,6 +271,17 @@ 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()) {
|
||||
return str;
|
||||
}
|
||||
auto def = content->getIndices()->getItemDef(bound->getItemId());
|
||||
return util::capitalized(
|
||||
langs::get(util::str2wstr_utf8(def->caption))
|
||||
); // TODO: cache
|
||||
}
|
||||
|
||||
void SlotView::bind(
|
||||
int64_t inventoryid,
|
||||
ItemStack& stack,
|
||||
|
||||
@@ -63,6 +63,7 @@ namespace gui {
|
||||
|
||||
virtual void clicked(gui::GUI*, mousecode) override;
|
||||
virtual void onFocus(gui::GUI*) override;
|
||||
virtual const std::wstring getTooltip() const override;
|
||||
|
||||
void bind(
|
||||
int64_t inventoryid,
|
||||
|
||||
@@ -64,12 +64,28 @@ Label::Label(std::wstring text, std::string fontName)
|
||||
cache.update(this->text, multiline, textWrap);
|
||||
}
|
||||
|
||||
glm::vec2 Label::calcSize() {
|
||||
auto font = cache.font;
|
||||
uint lineHeight = font->getLineHeight();
|
||||
if (cache.lines.size() > 1) {
|
||||
lineHeight *= lineInterval;
|
||||
}
|
||||
return glm::vec2 (
|
||||
cache.font->calcWidth(text),
|
||||
lineHeight * cache.lines.size() + font->getYOffset()
|
||||
);
|
||||
}
|
||||
|
||||
void Label::setText(std::wstring text) {
|
||||
if (text == this->text && !cache.resetFlag) {
|
||||
return;
|
||||
}
|
||||
this->text = text;
|
||||
cache.update(this->text, multiline, textWrap);
|
||||
|
||||
if (cache.font) {
|
||||
setSize(calcSize());
|
||||
}
|
||||
}
|
||||
|
||||
const std::wstring& Label::getText() const {
|
||||
@@ -156,10 +172,10 @@ void Label::draw(const DrawContext* pctx, Assets* assets) {
|
||||
lineHeight *= lineInterval;
|
||||
}
|
||||
glm::vec2 size = getSize();
|
||||
glm::vec2 newsize (
|
||||
font->calcWidth(text),
|
||||
lineHeight * cache.lines.size() + font->getYOffset()
|
||||
);
|
||||
glm::vec2 newsize = calcSize();
|
||||
if (autoresize) {
|
||||
setSize(newsize);
|
||||
}
|
||||
|
||||
glm::vec2 pos = calcPos();
|
||||
switch (align) {
|
||||
@@ -194,6 +210,13 @@ void Label::textSupplier(wstringsupplier supplier) {
|
||||
this->supplier = supplier;
|
||||
}
|
||||
|
||||
void Label::setAutoResize(bool flag) {
|
||||
this->autoresize = flag;
|
||||
}
|
||||
|
||||
bool Label::isAutoResize() const {
|
||||
return autoresize;
|
||||
}
|
||||
|
||||
void Label::setMultiline(bool multiline) {
|
||||
if (multiline != this->multiline) {
|
||||
|
||||
@@ -24,6 +24,8 @@ namespace gui {
|
||||
|
||||
class Label : public UINode {
|
||||
LabelCache cache;
|
||||
|
||||
glm::vec2 calcSize();
|
||||
protected:
|
||||
std::wstring text;
|
||||
std::string fontName;
|
||||
@@ -47,6 +49,9 @@ namespace gui {
|
||||
|
||||
/// @brief Text line height multiplied by line interval
|
||||
int totalLineHeight = 1;
|
||||
|
||||
/// @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");
|
||||
@@ -95,6 +100,9 @@ namespace gui {
|
||||
|
||||
virtual void textSupplier(wstringsupplier supplier);
|
||||
|
||||
virtual void setAutoResize(bool flag);
|
||||
virtual bool isAutoResize() const;
|
||||
|
||||
virtual void setMultiline(bool multiline);
|
||||
virtual bool isMultiline() const;
|
||||
|
||||
|
||||
@@ -129,6 +129,14 @@ bool UINode::isResizing() const {
|
||||
return resizing;
|
||||
}
|
||||
|
||||
void UINode::setTooltip(const std::wstring& text) {
|
||||
this->tooltip = text;
|
||||
}
|
||||
|
||||
const std::wstring UINode::getTooltip() const {
|
||||
return tooltip;
|
||||
}
|
||||
|
||||
glm::vec2 UINode::calcPos() const {
|
||||
if (parent) {
|
||||
return pos + parent->calcPos() + parent->contentOffset();
|
||||
@@ -316,7 +324,7 @@ void UINode::setGravity(Gravity gravity) {
|
||||
}
|
||||
|
||||
void UINode::getIndices(
|
||||
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();
|
||||
@@ -330,3 +338,19 @@ void UINode::getIndices(
|
||||
}
|
||||
}
|
||||
}
|
||||
std::shared_ptr<UINode> UINode::find(
|
||||
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()) {
|
||||
if (auto found = UINode::find(subnode, id)) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -109,6 +109,8 @@ namespace gui {
|
||||
ActionsSet actions;
|
||||
/// @brief 'ondoubleclick' callbacks
|
||||
ActionsSet doubleClickCallbacks;
|
||||
/// @brief element tooltip text
|
||||
std::wstring tooltip;
|
||||
|
||||
UINode(glm::vec2 size);
|
||||
public:
|
||||
@@ -197,6 +199,9 @@ namespace gui {
|
||||
virtual void setResizing(bool flag);
|
||||
virtual bool isResizing() const;
|
||||
|
||||
virtual void setTooltip(const std::wstring& text);
|
||||
virtual const std::wstring getTooltip() const;
|
||||
|
||||
virtual glm::vec4 calcColor() const;
|
||||
|
||||
/// @brief Get inner content offset. Used for scroll
|
||||
@@ -237,9 +242,14 @@ namespace gui {
|
||||
|
||||
/// @brief collect all nodes having id
|
||||
static void getIndices(
|
||||
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::string& id
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user