Implement Menu for adding Optimize serial keys
Some checks are pending
CD / ${{ matrix.platform.name }} ${{ matrix.config.name }} (map[flags:-DBUILD_SHARED_LIBS=FALSE name:Static], map[artifact_name:linux32 flags:32 name:Linux GCC 32 os:ubuntu-latest]) (push) Waiting to run
CD / ${{ matrix.platform.name }} ${{ matrix.config.name }} (map[flags:-DBUILD_SHARED_LIBS=FALSE name:Static], map[artifact_name:linux64 flags:64 name:Linux GCC x64 os:ubuntu-latest]) (push) Waiting to run
CD / ${{ matrix.platform.name }} ${{ matrix.config.name }} (map[flags:-DBUILD_SHARED_LIBS=FALSE name:Static], map[artifact_name:linuxarm32 flags:arm32 name:Linux GCC ARM 32 os:ubuntu-latest]) (push) Waiting to run
CD / ${{ matrix.platform.name }} ${{ matrix.config.name }} (map[flags:-DBUILD_SHARED_LIBS=FALSE name:Static], map[artifact_name:linuxarm64 flags:arm64 name:Linux GCC ARM 64 os:ubuntu-latest]) (push) Waiting to run
CD / ${{ matrix.platform.name }} ${{ matrix.config.name }} (map[flags:-DBUILD_SHARED_LIBS=FALSE name:Static], map[artifact_name:macos name:macOS Apple Silicon os:macos-14]) (push) Waiting to run
CD / ${{ matrix.platform.name }} ${{ matrix.config.name }} (map[flags:-DBUILD_SHARED_LIBS=FALSE name:Static], map[artifact_name:win32 flags:-A Win32 -DCMAKE_PARALLEL_MSVC=TRUE name:Windows VS2022 Win32 os:windows-2022]) (push) Waiting to run
CD / ${{ matrix.platform.name }} ${{ matrix.config.name }} (map[flags:-DBUILD_SHARED_LIBS=FALSE name:Static], map[artifact_name:win64 flags:-A x64 -DCMAKE_PARALLEL_MSVC=TRUE name:Windows VS2022 x64 os:windows-2022]) (push) Waiting to run
CD / ${{ matrix.platform.name }} ${{ matrix.config.name }} (map[flags:-DBUILD_SHARED_LIBS=FALSE name:Static], map[artifact_name:winarm64 flags:-A ARM64 -DCMAKE_PARALLEL_MSVC=TRUE name:Windows VS2022 ARM os:windows-2022]) (push) Waiting to run
CD / Create Pi Mono Setup (push) Blocked by required conditions
CD / Publishing (push) Blocked by required conditions

This commit is contained in:
Lorenzooone
2025-11-07 02:27:23 +01:00
parent a6e4348066
commit 4ce35770ac
37 changed files with 1847 additions and 112 deletions

View File

@@ -1,28 +1,56 @@
#include "TextRectangle.hpp"
#include <cmath>
TextRectangle::TextRectangle(bool font_load_success, sf::Font &text_font) : actual_text(text_font) {
#define TEXT_RECTANGLE_POS_APPROX 1.02
TextRectangle::TextRectangle(bool font_load_success, sf::Font *text_font, bool font_mono_load_success, sf::Font *text_font_mono) : actual_text(*text_font) {
this->reset_data(this->future_data);
this->text_font = text_font;
this->text_font_mono = text_font_mono;
this->font_load_success = font_load_success;
this->font_mono_load_success = font_mono_load_success;
this->is_done_showing_text = true;
this->setRealSize(1, 1, false);
this->text_rect.out_rect.setTexture(&this->text_rect.out_tex.getTexture());
if(this->font_load_success)
this->actual_text.setFont(text_font);
this->actual_text.setFont(*this->text_font);
this->time_phase = 0;
this->reset_data(this->future_data);
this->loaded_data.font_kind = this->future_data.font_kind;
}
TextRectangle::~TextRectangle() {
}
void TextRectangle::changeFont(FontKind new_font_kind) {
if(this->future_data.font_kind != new_font_kind) {
this->future_data.render_text = true;
this->future_data.schedule_font_swap = true;
}
this->future_data.font_kind = new_font_kind;
}
void TextRectangle::setSize(int width, int height) {
if((height == this->future_data.height) && (width == this->future_data.width))
return;
this->future_data.width = width;
this->future_data.height = height;
this->future_data.locked = false;
this->future_data.render_text = true;
}
size_text_t TextRectangle::getFinalSize() {
return size_text_t({this->future_data.stored_width, this->future_data.stored_height});
}
size_text_t TextRectangle::getFinalSizeNoMultipliers() {
size_text_t size = this->getFinalSize();
if(this->future_data.tight_and_centered)
size.x = (int)(size.x / TEXT_RECTANGLE_POS_APPROX);
return size;
}
void TextRectangle::setRectangleKind(TextKind kind) {
if(this->future_data.kind != kind) {
this->future_data.render_text = true;
@@ -35,6 +63,7 @@ void TextRectangle::setTextFactor(float size_multiplier) {
if(this->future_data.font_pixel_height == new_pixel_height)
return;
this->future_data.font_pixel_height = new_pixel_height;
this->future_data.locked = false;
this->future_data.render_text = true;
}
@@ -42,23 +71,88 @@ void TextRectangle::setDuration(float on_seconds) {
this->future_data.duration = on_seconds;
}
void TextRectangle::setPosition(int pos_x, int pos_y) {
void TextRectangle::setLineSpacing(float new_line_spacing) {
if(this->future_data.line_spacing != new_line_spacing)
this->future_data.render_text = true;
this->future_data.line_spacing = new_line_spacing;
}
void TextRectangle::setCharacterSpacing(float new_character_spacing) {
if(this->future_data.character_spacing != new_character_spacing)
this->future_data.render_text = true;
this->future_data.character_spacing = new_character_spacing;
}
void TextRectangle::setPosition(int pos_x, int pos_y, TextPosKind kind) {
if((pos_x == this->future_data.pos_x) && (pos_y == this->future_data.pos_y) && (kind == this->future_data.pos_kind))
return;
if(kind != POS_KIND_NORMAL)
this->future_data.render_text = true;
this->future_data.pos_x = pos_x;
this->future_data.pos_y = pos_y;
this->future_data.pos_kind = kind;
this->future_data.locked = false;
}
position_text_t TextRectangle::getFinalPosition() {
return position_text_t({this->future_data.stored_pos_x, this->future_data.stored_pos_y});
}
position_text_t TextRectangle::getFinalPositionNoMultipliers() {
position_text_t pos = this->getFinalPosition();
size_text_t full_size = this->getFinalSize();
size_text_t base_size = this->getFinalSizeNoMultipliers();
pos.x += (full_size.x - base_size.x) / 2;
pos.y += (full_size.y - base_size.y) / 2;
return pos;
}
void TextRectangle::setLocked(bool new_locked) {
this->future_data.locked = new_locked;
}
percentage_pos_text_t TextRectangle::getCoordInRectanglePercentage(int coord_x, int coord_y) {
percentage_pos_text_t percentage;
percentage.x = PERCENTAGE_POS_TEXT_ERROR;
percentage.y = PERCENTAGE_POS_TEXT_ERROR;
if(!(this->isFontLoaded(this->future_data) && this->future_data.show_text))
return percentage;
int left = this->future_data.stored_pos_x;
int right = left + this->future_data.stored_width;
int up = this->future_data.stored_pos_y;
int down = up + this->future_data.stored_height;
if(coord_x < left)
return percentage;
if(coord_y < up)
return percentage;
if(coord_x >= right)
return percentage;
if(coord_y >= down)
return percentage;
percentage.x = ((coord_x - left) * 100) / (right - left);
percentage.y = ((coord_y - up) * 100) / (down - up);
return percentage;
}
bool TextRectangle::isCoordInRectangle(int coord_x, int coord_y) {
if(!(font_load_success && this->future_data.show_text))
return false;
if(coord_x < this->future_data.pos_x)
return false;
if(coord_y < this->future_data.pos_y)
return false;
if(coord_x >= this->future_data.pos_x + this->future_data.width)
return false;
if(coord_y >= this->future_data.pos_y + this->future_data.height)
return false;
return true;
percentage_pos_text_t result = this->getCoordInRectanglePercentage(coord_x, coord_y);
return (result.x != PERCENTAGE_POS_TEXT_ERROR) && (result.y != PERCENTAGE_POS_TEXT_ERROR);
}
bool TextRectangle::isFontLoaded(TextData &reference_data) {
if(reference_data.font_kind == FONT_KIND_NORMAL)
return this->font_load_success;
if(reference_data.font_kind == FONT_KIND_MONO)
return this->font_mono_load_success;
return false;
}
sf::Font* TextRectangle::getFont(TextData &reference_data) {
if(reference_data.font_kind == FONT_KIND_NORMAL)
return this->text_font;
if(reference_data.font_kind == FONT_KIND_MONO)
return this->text_font_mono;
return NULL;
}
void TextRectangle::startTimer(bool do_start) {
@@ -73,14 +167,22 @@ void TextRectangle::setProportionalBox(bool proportional_box) {
this->future_data.proportional_box = proportional_box;
}
void TextRectangle::prepareRenderText() {
if(this->future_data.proportional_box) {
this->future_data.width = this->loaded_data.width;
this->future_data.height = this->loaded_data.height;
void TextRectangle::setTightAndCentered(bool tight_and_centered) {
if(this->future_data.tight_and_centered != tight_and_centered) {
this->future_data.render_text = true;
}
this->future_data.tight_and_centered = tight_and_centered;
}
void TextRectangle::prepareRenderText() {
this->future_data.stored_width = this->loaded_data.stored_width;
this->future_data.stored_height = this->loaded_data.stored_height;
this->future_data.stored_pos_x = this->loaded_data.stored_pos_x;
this->future_data.stored_pos_y = this->loaded_data.stored_pos_y;
this->loaded_data = this->future_data;
this->future_data.render_text = false;
this->future_data.start_timer = false;
this->future_data.schedule_font_swap = false;
}
void TextRectangle::setText(std::string text) {
@@ -97,13 +199,33 @@ void TextRectangle::setShowText(bool show_text) {
this->future_data.render_text = true;
}
bool TextRectangle::getShowText() {
return this->future_data.show_text;
}
void TextRectangle::draw(sf::RenderTarget &window) {
if(this->loaded_data.schedule_font_swap && this->isFontLoaded(this->loaded_data)) {
this->actual_text.setFont(*this->getFont(this->loaded_data));
}
if(this->loaded_data.render_text) {
this->updateText((int)window.getView().getSize().x);
}
if(font_load_success && this->loaded_data.show_text && (!this->is_done_showing_text)) {
if(this->isFontLoaded(this->loaded_data) && this->loaded_data.show_text && (!this->is_done_showing_text)) {
if(this->loaded_data.locked) {
this->loaded_data.width = this->loaded_data.stored_width;
this->loaded_data.height = this->loaded_data.stored_height;
this->loaded_data.pos_x = this->loaded_data.stored_pos_x;
this->loaded_data.pos_y = this->loaded_data.stored_pos_y;
this->pos_x_center_contrib = 0;
this->pos_y_center_contrib = 0;
}
this->loaded_data.pos_x += this->pos_x_center_contrib;
this->loaded_data.pos_y += this->pos_y_center_contrib;
this->text_rect.out_rect.setPosition({(float)this->loaded_data.pos_x, (float)this->loaded_data.pos_y});
if(this->loaded_data.is_timed) {
if(this->loaded_data.stored_height != 0)
this->loaded_data.height = this->loaded_data.stored_height;
float time_seconds[3];
this->updateSlides(time_seconds);
if(this->loaded_data.start_timer) {
@@ -125,7 +247,8 @@ void TextRectangle::draw(sf::RenderTarget &window) {
rect_curr_height = -this->loaded_data.height + ((int)(factor * this->loaded_data.height));
else if(this->time_phase == 2)
rect_curr_height = ((int)(factor * this->loaded_data.height)) * -1;
this->text_rect.out_rect.setPosition({(float)this->loaded_data.pos_x, (float)this->loaded_data.pos_y + rect_curr_height});
this->loaded_data.pos_y += rect_curr_height;
this->text_rect.out_rect.setPosition({(float)this->loaded_data.pos_x, (float)this->loaded_data.pos_y});
if(factor >= 1) {
this->time_phase++;
@@ -134,6 +257,10 @@ void TextRectangle::draw(sf::RenderTarget &window) {
if(this->time_phase >= 3)
this->is_done_showing_text = true;
}
this->loaded_data.stored_pos_x = this->loaded_data.pos_x;
this->loaded_data.stored_pos_y = this->loaded_data.pos_y;
this->loaded_data.stored_width = this->loaded_data.width;
this->loaded_data.stored_height = this->loaded_data.height;
window.draw(this->text_rect.out_rect);
}
}
@@ -145,6 +272,7 @@ void TextRectangle::reset_data(TextData &data) {
data.show_text = false;
data.render_text = false;
data.proportional_box = true;
data.tight_and_centered = false;
data.printed_text = "Sample Text";
data.duration = 2.5;
data.font_pixel_height = BASE_PIXEL_FONT_HEIGHT;
@@ -152,6 +280,16 @@ void TextRectangle::reset_data(TextData &data) {
data.height = 1;
data.pos_x = 0;
data.pos_y = 0;
data.font_kind = FONT_KIND_NORMAL;
data.schedule_font_swap = false;
data.line_spacing = 1.0;
data.character_spacing = 1.0;
data.stored_width = data.width;
data.stored_height = data.height;
data.stored_pos_x = data.pos_x;
data.stored_pos_y = data.pos_y;
data.locked = false;
data.pos_kind = POS_KIND_NORMAL;
}
static sf::String convert_to_utf8(const std::string &str) {
@@ -196,6 +334,8 @@ void TextRectangle::setTextWithLineWrapping(int x_limit) {
void TextRectangle::setRealSize(int width, int height, bool check_previous) {
this->loaded_data.width = width;
this->loaded_data.height = height;
this->pos_x_center_contrib = 0;
this->pos_y_center_contrib = 0;
if(check_previous) {
if((height == this->text_rect.out_rect.getSize().y) && (width == this->text_rect.out_rect.getSize().x))
return;
@@ -205,37 +345,89 @@ void TextRectangle::setRealSize(int width, int height, bool check_previous) {
this->text_rect.out_rect.setTextureRect(sf::IntRect({0, 0}, {this->loaded_data.width, this->loaded_data.height}));
}
float TextRectangle::getNoBlurCharacterSpacing() {
const float whitespaceWidth = this->getFont(this->loaded_data)->getGlyph(U' ', this->actual_text.getCharacterSize(), false).advance;
float pixels_letter_spacing = (float)round((whitespaceWidth / 3.0f) * (this->loaded_data.character_spacing - 1.0));
return (float)((pixels_letter_spacing / (whitespaceWidth / 3.0f)) + 1.0);
}
void TextRectangle::updateText(int x_limit) {
// set the character size
this->actual_text.setCharacterSize((unsigned int)this->loaded_data.font_pixel_height); // in pixels, not points!
// set the color
this->actual_text.setFillColor(sf::Color::White);
// set the text style
//this->actual_text.setStyle(sf::Text::Bold);
this->actual_text.setPosition({0, 0});
this->setTextWithLineWrapping(x_limit);
sf::FloatRect globalBounds;
if(this->loaded_data.proportional_box) {
globalBounds = this->actual_text.getGlobalBounds();
int new_width = this->loaded_data.width;
int new_height = this->loaded_data.height;
int bounds_width = (int)(globalBounds.size.x + (globalBounds.position.x * 2));
int bounds_height = (int)(globalBounds.size.y + (globalBounds.position.y * 2));
if(new_width < bounds_width)
new_width = bounds_width;
if(new_height < bounds_height)
new_height = bounds_height;
this->setRealSize(new_width, new_height);
this->actual_text.setPosition({(float)((int)(5 * (((float)this->loaded_data.font_pixel_height) / BASE_PIXEL_FONT_HEIGHT))), 0});
globalBounds = this->actual_text.getGlobalBounds();
this->setRealSize((int)(globalBounds.size.x + (globalBounds.position.x * 2)), (int)(globalBounds.size.y + (globalBounds.position.y * 2)));
if(!this->loaded_data.locked) {
// set the character size
this->actual_text.setCharacterSize((unsigned int)this->loaded_data.font_pixel_height); // in pixels, not points!
// set the text style
//this->actual_text.setStyle(sf::Text::Bold);
this->actual_text.setPosition({0, 0});
this->setTextWithLineWrapping(x_limit);
sf::FloatRect globalBounds;
this->actual_text.setLineSpacing(this->loaded_data.line_spacing);
this->actual_text.setLetterSpacing(this->getNoBlurCharacterSpacing());
if(this->loaded_data.tight_and_centered) {
globalBounds = this->actual_text.getGlobalBounds();
int new_width = this->loaded_data.width;
int new_height = this->loaded_data.height;
int base_width = (int)(globalBounds.size.x + (globalBounds.position.x * 2));
int bounds_width = (int)(base_width * TEXT_RECTANGLE_POS_APPROX);
int base_height = (int)(globalBounds.size.y + (globalBounds.position.y * 2));
int bounds_height = base_height;
if(new_width < bounds_width) {
bounds_width = new_width;
base_width = new_width;
}
if(new_height < bounds_height) {
bounds_height = new_height;
base_height = new_width;
}
this->setRealSize(bounds_width, bounds_height);
switch(this->loaded_data.pos_kind) {
case POS_KIND_NORMAL:
this->actual_text.setPosition({(float)((int)((this->loaded_data.width - (globalBounds.size.x + (globalBounds.position.x * 2))) / 2)), (float)((int)((this->loaded_data.height - (globalBounds.size.y + (globalBounds.position.y * 2))) / 2))});
this->pos_x_center_contrib = (new_width - this->loaded_data.width) / 2;
this->pos_y_center_contrib = (new_height - this->loaded_data.height) / 2;
break;
case POS_KIND_PRIORITIZE_TOP_LEFT:
this->actual_text.setPosition({0, 0});
this->pos_x_center_contrib = (base_width - this->loaded_data.width) / 2;
this->pos_y_center_contrib = 0;
break;
case POS_KIND_PRIORITIZE_BOTTOM_LEFT:
this->actual_text.setPosition({0, 0});
this->pos_x_center_contrib = (base_width - this->loaded_data.width) / 2;
this->pos_y_center_contrib = -this->loaded_data.height;
break;
default:
break;
}
}
else if(this->loaded_data.proportional_box) {
globalBounds = this->actual_text.getGlobalBounds();
int new_width = this->loaded_data.width;
int new_height = this->loaded_data.height;
int bounds_width = (int)(globalBounds.size.x + (globalBounds.position.x * 2));
int bounds_height = (int)(globalBounds.size.y + (globalBounds.position.y * 2));
if(new_width < bounds_width)
new_width = bounds_width;
if(new_height < bounds_height)
new_height = bounds_height;
this->setRealSize(new_width, new_height);
this->actual_text.setPosition({(float)((int)(5 * (((float)this->loaded_data.font_pixel_height) / BASE_PIXEL_FONT_HEIGHT))), 0});
globalBounds = this->actual_text.getGlobalBounds();
this->setRealSize((int)(globalBounds.size.x + (globalBounds.position.x * 2)), (int)(globalBounds.size.y + (globalBounds.position.y * 2)));
}
else {
this->setRealSize(this->loaded_data.width, this->loaded_data.height);
this->actual_text.setPosition({0, 0});
globalBounds = this->actual_text.getGlobalBounds();
this->actual_text.setPosition({(float)((int)((this->loaded_data.width - (globalBounds.size.x + (globalBounds.position.x * 2))) / 2)), (float)((int)((this->loaded_data.height - (globalBounds.size.y + (globalBounds.position.y * 2))) / 2))});
}
}
else {
this->setRealSize(this->loaded_data.width, this->loaded_data.height);
sf::Vector2f old_actual_text_pos = this->actual_text.getPosition();
this->actual_text.setPosition({0, 0});
globalBounds = this->actual_text.getGlobalBounds();
this->actual_text.setPosition({(float)((int)((this->loaded_data.width - (globalBounds.size.x + (globalBounds.position.x * 2))) / 2)), (float)((int)((this->loaded_data.height - (globalBounds.size.y + (globalBounds.position.y * 2))) / 2))});
this->setTextWithLineWrapping(x_limit);
this->actual_text.setPosition(old_actual_text_pos);
}
sf::Color color_text = sf::Color(255, 255, 255, 255);
switch(this->loaded_data.kind) {
case TEXT_KIND_NORMAL:
this->curr_color = sf::Color(40, 40, 80, 192);
@@ -255,12 +447,21 @@ void TextRectangle::updateText(int x_limit) {
case TEXT_KIND_OPAQUE_ERROR:
this->curr_color = sf::Color(160, 60, 60, 192);
break;
case TEXT_KIND_TEXTBOX:
this->curr_color = sf::Color(255, 255, 255, 255);
color_text = sf::Color(0, 0, 0, 255);
break;
case TEXT_KIND_TEXTBOX_TRANSPARENT:
this->curr_color = sf::Color(255, 255, 255, 0);
color_text = sf::Color(0, 0, 0, 255);
break;
case TEXT_KIND_TITLE:
this->curr_color = sf::Color(30, 30, 60, 0);
break;
default:
break;
}
this->actual_text.setFillColor(color_text);
this->text_rect.out_tex.clear(this->curr_color);
this->text_rect.out_tex.draw(this->actual_text);
this->text_rect.out_tex.display();