misc hud-related updates
This commit is contained in:
@@ -170,3 +170,7 @@ void GUI::setFocus(std::shared_ptr<UINode> node) {
|
||||
focus->focus(this);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Container> GUI::getContainer() const {
|
||||
return container;
|
||||
}
|
||||
|
||||
@@ -76,6 +76,8 @@ namespace gui {
|
||||
std::shared_ptr<UINode> get(std::string name);
|
||||
void remove(std::string name);
|
||||
void setFocus(std::shared_ptr<UINode> node);
|
||||
|
||||
std::shared_ptr<Container> getContainer() const;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -119,7 +119,18 @@ glm::vec2 UINode::getSize() const {
|
||||
}
|
||||
|
||||
void UINode::setSize(glm::vec2 size) {
|
||||
this->size = size;
|
||||
this->size = glm::vec2(
|
||||
glm::max(minSize.x, size.x), glm::max(minSize.y, size.y)
|
||||
);
|
||||
}
|
||||
|
||||
glm::vec2 UINode::getMinSize() const {
|
||||
return minSize;
|
||||
}
|
||||
|
||||
void UINode::setMinSize(glm::vec2 minSize) {
|
||||
this->minSize = minSize;
|
||||
setSize(getSize());
|
||||
}
|
||||
|
||||
void UINode::setColor(glm::vec4 color) {
|
||||
@@ -158,6 +169,14 @@ int UINode::getZIndex() const {
|
||||
void UINode::lock() {
|
||||
}
|
||||
|
||||
vec2supplier UINode::getPositionFunc() const {
|
||||
return positionfunc;
|
||||
}
|
||||
|
||||
void UINode::setPositionFunc(vec2supplier func) {
|
||||
positionfunc = func;
|
||||
}
|
||||
|
||||
void UINode::setId(const std::string& id) {
|
||||
this->id = id;
|
||||
}
|
||||
@@ -165,3 +184,9 @@ void UINode::setId(const std::string& id) {
|
||||
const std::string& UINode::getId() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
void UINode::reposition() {
|
||||
if (positionfunc) {
|
||||
setCoord(positionfunc());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include "../../delegates.h"
|
||||
|
||||
class GfxContext;
|
||||
class Assets;
|
||||
@@ -26,6 +27,7 @@ namespace gui {
|
||||
protected:
|
||||
glm::vec2 coord;
|
||||
glm::vec2 size;
|
||||
glm::vec2 minSize {1.0f};
|
||||
glm::vec4 color {1.0f};
|
||||
glm::vec4 hoverColor {1.0f};
|
||||
glm::vec4 margin {1.0f};
|
||||
@@ -38,6 +40,7 @@ namespace gui {
|
||||
int zindex = 0;
|
||||
Align align = Align::left;
|
||||
UINode* parent = nullptr;
|
||||
vec2supplier positionfunc = nullptr;
|
||||
UINode(glm::vec2 coord, glm::vec2 size);
|
||||
public:
|
||||
virtual ~UINode();
|
||||
@@ -112,14 +115,22 @@ namespace gui {
|
||||
/* Calculate screen position of the element */
|
||||
virtual glm::vec2 calcCoord() const;
|
||||
virtual void setCoord(glm::vec2 coord);
|
||||
glm::vec2 getCoord() const;
|
||||
virtual glm::vec2 getCoord() const;
|
||||
virtual glm::vec2 getSize() const;
|
||||
virtual void setSize(glm::vec2 size);
|
||||
virtual glm::vec2 getMinSize() const;
|
||||
virtual void setMinSize(glm::vec2 size);
|
||||
virtual void refresh() {};
|
||||
virtual void lock();
|
||||
|
||||
virtual vec2supplier getPositionFunc() const;
|
||||
virtual void setPositionFunc(vec2supplier);
|
||||
|
||||
void setId(const std::string& id);
|
||||
const std::string& getId() const;
|
||||
|
||||
/* Fetch coord from positionfunc if assigned */
|
||||
void reposition();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "../../assets/Assets.h"
|
||||
#include "../../graphics/Batch2D.h"
|
||||
#include "../../graphics/Font.h"
|
||||
#include "../../graphics/Texture.h"
|
||||
#include "../../graphics/GfxContext.h"
|
||||
#include "../../util/stringutil.h"
|
||||
#include "GUI.h"
|
||||
@@ -64,9 +65,8 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
|
||||
font->draw(batch, text, coord.x, coord.y);
|
||||
}
|
||||
|
||||
Label* Label::textSupplier(wstringsupplier supplier) {
|
||||
void Label::textSupplier(wstringsupplier supplier) {
|
||||
this->supplier = supplier;
|
||||
return this;
|
||||
}
|
||||
|
||||
// ================================= Image ====================================
|
||||
@@ -78,12 +78,24 @@ void Image::draw(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec2 coord = calcCoord();
|
||||
glm::vec4 color = getColor();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(assets->getTexture(texture));
|
||||
|
||||
auto texture = assets->getTexture(this->texture);
|
||||
if (texture && autoresize) {
|
||||
setSize(glm::vec2(texture->width, texture->height));
|
||||
}
|
||||
batch->texture(texture);
|
||||
batch->color = color;
|
||||
batch->rect(coord.x, coord.y, size.x, size.y,
|
||||
0, 0, 0, UVRegion(), false, true, color);
|
||||
}
|
||||
|
||||
void Image::setAutoResize(bool flag) {
|
||||
autoresize = flag;
|
||||
}
|
||||
bool Image::isAutoResize() const {
|
||||
return autoresize;
|
||||
}
|
||||
|
||||
// ================================= Button ===================================
|
||||
Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
|
||||
: Panel(glm::vec2(), padding, 0) {
|
||||
|
||||
@@ -31,16 +31,20 @@ namespace gui {
|
||||
|
||||
virtual void draw(const GfxContext* pctx, Assets* assets) override;
|
||||
|
||||
virtual Label* textSupplier(wstringsupplier supplier);
|
||||
virtual void textSupplier(wstringsupplier supplier);
|
||||
};
|
||||
|
||||
class Image : public UINode {
|
||||
protected:
|
||||
std::string texture;
|
||||
bool autoresize = false;
|
||||
public:
|
||||
Image(std::string texture, glm::vec2 size=glm::vec2(32,32));
|
||||
|
||||
virtual void draw(const GfxContext* pctx, Assets* assets) override;
|
||||
|
||||
virtual void setAutoResize(bool flag);
|
||||
virtual bool isAutoResize() const;
|
||||
};
|
||||
|
||||
class Button : public Panel {
|
||||
|
||||
@@ -21,7 +21,7 @@ static Align align_from_string(const std::string& str, Align def) {
|
||||
}
|
||||
|
||||
/* Read basic UINode properties */
|
||||
static void _readUINode(xml::xmlelement element, UINode& node) {
|
||||
static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) {
|
||||
if (element->has("id")) {
|
||||
node.setId(element->attr("id").getText());
|
||||
}
|
||||
@@ -40,13 +40,21 @@ static void _readUINode(xml::xmlelement element, UINode& node) {
|
||||
if (element->has("z-index")) {
|
||||
node.setZIndex(element->attr("z-index").asInt());
|
||||
}
|
||||
if (element->has("position-func")) {
|
||||
auto supplier = scripting::create_vec2_supplier(
|
||||
reader.getEnvironment().getId(),
|
||||
element->attr("position-func").getText(),
|
||||
reader.getFilename()+".lua"
|
||||
);
|
||||
node.setPositionFunc(supplier);
|
||||
}
|
||||
std::string alignName = element->attr("align", "").getText();
|
||||
node.setAlign(align_from_string(alignName, node.getAlign()));
|
||||
}
|
||||
|
||||
|
||||
static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Container& container) {
|
||||
_readUINode(element, container);
|
||||
_readUINode(reader, element, container);
|
||||
|
||||
if (element->has("scrollable")) {
|
||||
container.setScrollable(element->attr("scrollable").asBool());
|
||||
@@ -66,11 +74,11 @@ void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, Conta
|
||||
}
|
||||
|
||||
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) {
|
||||
_readUINode(element, node);
|
||||
_readUINode(reader, element, node);
|
||||
}
|
||||
|
||||
static void _readPanel(UiXmlReader& reader, xml::xmlelement element, Panel& panel) {
|
||||
_readUINode(element, panel);
|
||||
_readUINode(reader, element, panel);
|
||||
|
||||
if (element->has("padding")) {
|
||||
glm::vec4 padding = element->attr("padding").asVec4();
|
||||
@@ -114,7 +122,7 @@ static std::wstring readAndProcessInnerText(xml::xmlelement element) {
|
||||
static std::shared_ptr<UINode> readLabel(UiXmlReader& reader, xml::xmlelement element) {
|
||||
std::wstring text = readAndProcessInnerText(element);
|
||||
auto label = std::make_shared<Label>(text);
|
||||
_readUINode(element, *label);
|
||||
_readUINode(reader, element, *label);
|
||||
return label;
|
||||
}
|
||||
|
||||
@@ -173,7 +181,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
|
||||
static std::shared_ptr<UINode> readImage(UiXmlReader& reader, xml::xmlelement element) {
|
||||
std::string src = element->attr("src", "").getText();
|
||||
auto image = std::make_shared<Image>(src);
|
||||
_readUINode(element, *image);
|
||||
_readUINode(reader, element, *image);
|
||||
reader.getAssetsLoader().add(ASSET_TEXTURE, "textures/"+src+".png", src, nullptr);
|
||||
return image;
|
||||
}
|
||||
@@ -185,7 +193,7 @@ static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, xml::xmlelement
|
||||
float step = element->attr("step", "1.0").asFloat();
|
||||
int trackWidth = element->attr("track-width", "1.0").asInt();
|
||||
auto bar = std::make_shared<TrackBar>(min, max, def, step, trackWidth);
|
||||
_readUINode(element, *bar);
|
||||
_readUINode(reader, element, *bar);
|
||||
if (element->has("consumer")) {
|
||||
auto consumer = scripting::create_number_consumer(
|
||||
reader.getEnvironment().getId(),
|
||||
|
||||
@@ -115,6 +115,7 @@ void Container::addBack(std::shared_ptr<UINode> node) {
|
||||
void Container::add(std::shared_ptr<UINode> node) {
|
||||
nodes.push_back(node);
|
||||
node->setParent(this);
|
||||
node->reposition();
|
||||
refresh();
|
||||
}
|
||||
|
||||
@@ -138,8 +139,15 @@ void Container::listenInterval(float interval, ontimeout callback, int repeat) {
|
||||
}
|
||||
|
||||
void Container::setSize(glm::vec2 size) {
|
||||
if (size == getSize()) {
|
||||
refresh();
|
||||
return;
|
||||
}
|
||||
UINode::setSize(size);
|
||||
refresh();
|
||||
for (auto& node : nodes) {
|
||||
node->reposition();
|
||||
}
|
||||
}
|
||||
|
||||
void Container::refresh() {
|
||||
|
||||
Reference in New Issue
Block a user