#include "controls.h" #include #include "../../window/Events.h" #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" using namespace gui; Label::Label(std::string text, std::string fontName) : UINode(glm::vec2(), glm::vec2(text.length() * 8, 15)), text(util::str2wstr_utf8(text)), fontName(fontName) { setInteractive(false); } Label::Label(std::wstring text, std::string fontName) : UINode(glm::vec2(), glm::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); glm::vec2 size = getSize(); glm::vec2 newsize ( font->calcWidth(text), font->getLineHeight()+font->getYOffset() ); glm::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); } void Label::textSupplier(wstringsupplier supplier) { this->supplier = supplier; } // ================================= Image ==================================== Image::Image(std::string texture, glm::vec2 size) : UINode(glm::vec2(), size), texture(texture) { setInteractive(false); } void Image::draw(const GfxContext* pctx, Assets* assets) { glm::vec2 coord = calcCoord(); glm::vec4 color = getColor(); auto batch = pctx->getBatch2D(); 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 content, glm::vec4 padding) : Panel(glm::vec2(), padding, 0) { glm::vec4 margin = getMargin(); setSize(content->getSize()+ glm::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, glm::vec4 padding, onaction action, glm::vec2 size ) : Panel(size, padding, 0) { if (size.y < 0.0f) { size = glm::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