gui and Font update

This commit is contained in:
MihailRis
2024-02-03 05:56:10 +03:00
parent 6cdb45485b
commit 9cb3923442
5 changed files with 43 additions and 23 deletions
+5 -5
View File
@@ -94,21 +94,21 @@ bool assetload::font(Assets* assets,
const ResPaths* paths, const ResPaths* paths,
const std::string filename, const std::string filename,
const std::string name) { const std::string name) {
std::vector<Texture*> pages; std::vector<std::unique_ptr<Texture>> pages;
for (size_t i = 0; i <= 4; i++) { for (size_t i = 0; i <= 4; i++) {
std::string name = filename + "_" + std::to_string(i) + ".png"; std::string name = filename + "_" + std::to_string(i) + ".png";
name = paths->find(name).string(); name = paths->find(name).string();
Texture* texture = png::load_texture(name); std::unique_ptr<Texture> texture (png::load_texture(name));
if (texture == nullptr) { if (texture == nullptr) {
std::cerr << "failed to load bitmap font '" << name; std::cerr << "failed to load bitmap font '" << name;
std::cerr << "' (missing page " << std::to_string(i) << ")"; std::cerr << "' (missing page " << std::to_string(i) << ")";
std::cerr << std::endl; std::cerr << std::endl;
return false; return false;
} }
pages.push_back(texture); pages.push_back(std::move(texture));
} }
Font* font = new Font(pages, pages[0]->height / 16); int res = pages[0]->height / 16;
assets->store(font, name); assets->store(new Font(std::move(pages), res, 4), name);
return true; return true;
} }
+18 -6
View File
@@ -48,7 +48,10 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
batch->color = getColor(); batch->color = getColor();
Font* font = assets->getFont(fontName_); Font* font = assets->getFont(fontName_);
vec2 size = getSize(); vec2 size = getSize();
vec2 newsize = vec2(font->calcWidth(text), font->lineHeight()); vec2 newsize = vec2(
font->calcWidth(text),
font->getLineHeight()+font->getYOffset()
);
vec2 coord = calcCoord(); vec2 coord = calcCoord();
switch (align) { switch (align) {
@@ -61,7 +64,7 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
coord.x += size.x-newsize.x; coord.x += size.x-newsize.x;
break; break;
} }
coord.y += (size.y-newsize.y)*0.5f;
font->draw(batch, text, coord.x, coord.y); font->draw(batch, text, coord.x, coord.y);
} }
@@ -103,10 +106,12 @@ Button::Button(
vec2 size vec2 size
) : Panel(size, padding, 0) ) : Panel(size, padding, 0)
{ {
size = vec2( if (size.y < 0.0f) {
glm::max(padding.x + padding.z + text.length()*8, size.x), size = vec2(
glm::max(padding.y + padding.w + 16, size.y) glm::max(padding.x + padding.z + text.length()*8, size.x),
); glm::max(padding.y + padding.w + 16, size.y)
);
}
setSize(size); setSize(size);
if (action) { if (action) {
@@ -141,6 +146,13 @@ Button* Button::textSupplier(wstringsupplier supplier) {
return this; return this;
} }
void Button::setSize(glm::vec2 size) {
Panel::setSize(size);
if (label) {
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y));
}
}
void Button::drawBackground(const GfxContext* pctx, Assets* assets) { void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
+2
View File
@@ -81,6 +81,8 @@ namespace gui {
virtual std::wstring getText() const; virtual std::wstring getText() const;
virtual Button* textSupplier(wstringsupplier supplier); virtual Button* textSupplier(wstringsupplier supplier);
virtual void setSize(glm::vec2 size) override;
}; };
class RichButton : public Container { class RichButton : public Container {
+11 -8
View File
@@ -4,16 +4,19 @@
using glm::vec4; using glm::vec4;
Font::Font(std::vector<Texture*> pages, int lineHeight) : lineHeight_(lineHeight), pages(pages) { Font::Font(std::vector<std::unique_ptr<Texture>> pages, int lineHeight, int yoffset)
: lineHeight(lineHeight), yoffset(yoffset), pages(std::move(pages)) {
} }
Font::~Font(){ Font::~Font(){
for (Texture* texture : pages)
delete texture;
} }
int Font::lineHeight() const { int Font::getYOffset() const {
return lineHeight_; return yoffset;
}
int Font::getLineHeight() const {
return lineHeight;
} }
bool Font::isPrintableChar(int c) { bool Font::isPrintableChar(int c) {
@@ -48,11 +51,11 @@ void Font::draw(Batch2D* batch, std::wstring text, int x, int y, int style) {
if (isPrintableChar(c)){ if (isPrintableChar(c)){
int charpage = c >> 8; int charpage = c >> 8;
if (charpage == page){ if (charpage == page){
Texture* texture = pages[charpage]; Texture* texture = pages[charpage].get();
if (texture == nullptr){ if (texture == nullptr){
texture = pages[0]; texture = pages[0].get();
} }
batch->texture(pages[charpage]); batch->texture(texture);
switch (style){ switch (style){
case STYLE_SHADOW: case STYLE_SHADOW:
+7 -4
View File
@@ -1,6 +1,7 @@
#ifndef GRAPHICS_FONT_H_ #ifndef GRAPHICS_FONT_H_
#define GRAPHICS_FONT_H_ #define GRAPHICS_FONT_H_
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "../typedefs.h" #include "../typedefs.h"
@@ -13,13 +14,15 @@ const uint STYLE_SHADOW = 1;
const uint STYLE_OUTLINE = 2; const uint STYLE_OUTLINE = 2;
class Font { class Font {
int lineHeight_; int lineHeight;
int yoffset;
public: public:
std::vector<Texture*> pages; std::vector<std::unique_ptr<Texture>> pages;
Font(std::vector<Texture*> pages, int lineHeight); Font(std::vector<std::unique_ptr<Texture>> pages, int lineHeight, int yoffset);
~Font(); ~Font();
int lineHeight() const; int getLineHeight() const;
int getYOffset() const;
int calcWidth(std::wstring text); int calcWidth(std::wstring text);
// int getGlyphWidth(char c); // int getGlyphWidth(char c);
bool isPrintableChar(int c); bool isPrintableChar(int c);