#include "controls.h" #include #include "../../window/Events.h" #include "../../assets/Assets.h" #include "../../graphics/Batch2D.h" #include "../../graphics/Font.h" #include "../../graphics/GfxContext.h" #include "../../util/stringutil.h" #include "GUI.h" using glm::vec2; using glm::vec3; using glm::vec4; using namespace gui; Label::Label(std::string text, std::string fontName) : UINode(vec2(), vec2(text.length() * 8, 15)), text(util::str2wstr_utf8(text)), fontName_(fontName) { setInteractive(false); } Label::Label(std::wstring text, std::string fontName) : UINode(vec2(), vec2(text.length() * 8, 15)), text(text), fontName_(fontName) { setInteractive(false); } void Label::setText(std::wstring text) { this->text = text; } std::wstring Label::getText() const { return text; } void Label::draw(const GfxContext* pctx, Assets* assets) { if (supplier) { setText(supplier()); } auto batch = pctx->getBatch2D(); batch->color = getColor(); Font* font = assets->getFont(fontName_); vec2 size = getSize(); vec2 newsize = vec2( font->calcWidth(text), font->getLineHeight()+font->getYOffset() ); vec2 coord = calcCoord(); switch (align) { case Align::left: break; case Align::center: coord.x += (size.x-newsize.x)*0.5f; break; case Align::right: coord.x += size.x-newsize.x; break; } coord.y += (size.y-newsize.y)*0.5f; font->draw(batch, text, coord.x, coord.y); } Label* Label::textSupplier(wstringsupplier supplier) { this->supplier = supplier; return this; } // ================================= Image ==================================== Image::Image(std::string texture, vec2 size) : UINode(vec2(), size), texture(texture) { setInteractive(false); } void Image::draw(const GfxContext* pctx, Assets* assets) { vec2 coord = calcCoord(); vec4 color = getColor(); auto batch = pctx->getBatch2D(); batch->texture(assets->getTexture(texture)); batch->color = color; batch->rect(coord.x, coord.y, size.x, size.y, 0, 0, 0, UVRegion(), false, true, color); } // ================================= Button =================================== Button::Button(std::shared_ptr content, glm::vec4 padding) : Panel(vec2(), padding, 0) { vec4 margin = getMargin(); setSize(content->getSize()+vec2(padding[0]+padding[2]+margin[0]+margin[2], padding[1]+padding[3]+margin[1]+margin[3])); add(content); setScrollable(false); setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f)); content->setInteractive(false); } Button::Button( std::wstring text, vec4 padding, onaction action, vec2 size ) : Panel(size, padding, 0) { if (size.y < 0.0f) { size = vec2( glm::max(padding.x + padding.z + text.length()*8, size.x), glm::max(padding.y + padding.w + 16, size.y) ); } setSize(size); if (action) { listenAction(action); } setScrollable(false); label = std::make_shared