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

View File

@ -94,21 +94,21 @@ bool assetload::font(Assets* assets,
const ResPaths* paths,
const std::string filename,
const std::string name) {
std::vector<Texture*> pages;
std::vector<std::unique_ptr<Texture>> pages;
for (size_t i = 0; i <= 4; i++) {
std::string name = filename + "_" + std::to_string(i) + ".png";
name = paths->find(name).string();
Texture* texture = png::load_texture(name);
std::unique_ptr<Texture> texture (png::load_texture(name));
if (texture == nullptr) {
std::cerr << "failed to load bitmap font '" << name;
std::cerr << "' (missing page " << std::to_string(i) << ")";
std::cerr << std::endl;
return false;
}
pages.push_back(texture);
pages.push_back(std::move(texture));
}
Font* font = new Font(pages, pages[0]->height / 16);
assets->store(font, name);
int res = pages[0]->height / 16;
assets->store(new Font(std::move(pages), res, 4), name);
return true;
}

View File

@ -48,7 +48,10 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
batch->color = getColor();
Font* font = assets->getFont(fontName_);
vec2 size = getSize();
vec2 newsize = vec2(font->calcWidth(text), font->lineHeight());
vec2 newsize = vec2(
font->calcWidth(text),
font->getLineHeight()+font->getYOffset()
);
vec2 coord = calcCoord();
switch (align) {
@ -61,7 +64,7 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
coord.x += size.x-newsize.x;
break;
}
coord.y += (size.y-newsize.y)*0.5f;
font->draw(batch, text, coord.x, coord.y);
}
@ -103,10 +106,12 @@ Button::Button(
vec2 size
) : Panel(size, padding, 0)
{
size = vec2(
glm::max(padding.x + padding.z + text.length()*8, size.x),
glm::max(padding.y + padding.w + 16, size.y)
);
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) {
@ -141,6 +146,13 @@ Button* Button::textSupplier(wstringsupplier supplier) {
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) {
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();

View File

@ -81,6 +81,8 @@ namespace gui {
virtual std::wstring getText() const;
virtual Button* textSupplier(wstringsupplier supplier);
virtual void setSize(glm::vec2 size) override;
};
class RichButton : public Container {

View File

@ -4,16 +4,19 @@
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(){
for (Texture* texture : pages)
delete texture;
}
int Font::lineHeight() const {
return lineHeight_;
int Font::getYOffset() const {
return yoffset;
}
int Font::getLineHeight() const {
return lineHeight;
}
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)){
int charpage = c >> 8;
if (charpage == page){
Texture* texture = pages[charpage];
Texture* texture = pages[charpage].get();
if (texture == nullptr){
texture = pages[0];
texture = pages[0].get();
}
batch->texture(pages[charpage]);
batch->texture(texture);
switch (style){
case STYLE_SHADOW:

View File

@ -1,6 +1,7 @@
#ifndef GRAPHICS_FONT_H_
#define GRAPHICS_FONT_H_
#include <memory>
#include <string>
#include <vector>
#include "../typedefs.h"
@ -13,13 +14,15 @@ const uint STYLE_SHADOW = 1;
const uint STYLE_OUTLINE = 2;
class Font {
int lineHeight_;
int lineHeight;
int yoffset;
public:
std::vector<Texture*> pages;
Font(std::vector<Texture*> pages, int lineHeight);
std::vector<std::unique_ptr<Texture>> pages;
Font(std::vector<std::unique_ptr<Texture>> pages, int lineHeight, int yoffset);
~Font();
int lineHeight() const;
int getLineHeight() const;
int getYOffset() const;
int calcWidth(std::wstring text);
// int getGlyphWidth(char c);
bool isPrintableChar(int c);