Handle multiple-devices selection

This commit is contained in:
Lorenzooone
2024-04-28 19:09:16 +02:00
parent f087e02b1b
commit ae3200cc08
13 changed files with 745 additions and 130 deletions

View File

@@ -1,6 +1,4 @@
#include "frontend.hpp"
#define BASE_PIXEL_FONT_HEIGHT 24
#include "TextRectangle.hpp"
TextRectangle::TextRectangle(bool font_load_success, sf::Font &text_font) {
this->font_load_success = font_load_success;
@@ -26,11 +24,11 @@ TextRectangle::~TextRectangle() {
}
void TextRectangle::setSize(int width, int height) {
this->width = width;
this->height = height;
this->text_rect.out_rect.setSize(sf::Vector2f(this->width, this->height));
this->text_rect.out_tex.create(this->width, this->height);
this->text_rect.out_rect.setTextureRect(sf::IntRect(0, 0, this->width, this->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.render_text = true;
}
void TextRectangle::setRectangleKind(TextKind kind) {
@@ -42,13 +40,35 @@ void TextRectangle::setRectangleKind(TextKind kind) {
void TextRectangle::setTextFactor(float size_multiplier) {
int new_pixel_height = BASE_PIXEL_FONT_HEIGHT * 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.render_text = true;
}
void TextRectangle::setDuration(float on_seconds) {
this->future_data.duration = on_seconds;
}
void TextRectangle::setPosition(int pos_x, int pos_y) {
this->future_data.pos_x = pos_x;
this->future_data.pos_y = pos_y;
}
bool TextRectangle::isCoordInRectangle(int coord_x, int coord_y) {
if(!(font_load_success && this->future_data.show_text && (!this->is_done_showing_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;
}
void TextRectangle::startTimer(bool do_start) {
this->future_data.start_timer = do_start;
this->future_data.is_timed = do_start;
@@ -62,6 +82,10 @@ void TextRectangle::setProportionalBox(bool proportional_box) {
}
void TextRectangle::prepareRenderText() {
if(this->loaded_data.proportional_box) {
this->future_data.width = this->loaded_data.width;
this->future_data.height = this->loaded_data.height;
}
this->loaded_data = this->future_data;
this->future_data.render_text = false;
this->future_data.start_timer = false;
@@ -83,6 +107,7 @@ void TextRectangle::draw(sf::RenderTarget &window) {
this->updateText(window.getView().getSize().x);
}
if(font_load_success && this->loaded_data.show_text && (!this->is_done_showing_text)) {
this->text_rect.out_rect.setPosition(this->loaded_data.pos_x, this->loaded_data.pos_y);
if(this->loaded_data.is_timed) {
float time_seconds[3];
this->updateSlides(time_seconds);
@@ -102,10 +127,10 @@ void TextRectangle::draw(sf::RenderTarget &window) {
int rect_curr_height = 0;
if(this->time_phase == 0)
rect_curr_height = -this->height + ((int)(factor * this->height));
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->height)) * -1;
this->text_rect.out_rect.setPosition(0, rect_curr_height);
rect_curr_height = ((int)(factor * this->loaded_data.height)) * -1;
this->text_rect.out_rect.setPosition(this->loaded_data.pos_x, this->loaded_data.pos_y + rect_curr_height);
if(factor >= 1) {
this->time_phase++;
@@ -128,6 +153,8 @@ void TextRectangle::reset_data(TextData &data) {
data.printed_text = "Sample Text";
data.duration = 2.5;
data.font_pixel_height = BASE_PIXEL_FONT_HEIGHT;
data.pos_x = 0;
data.pos_y = 0;
}
void TextRectangle::setTextWithLineWrapping(int x_limit) {
@@ -165,6 +192,16 @@ void TextRectangle::setTextWithLineWrapping(int x_limit) {
this->actual_text.setString(new_text);
}
void TextRectangle::setRealSize(int width, int height) {
this->loaded_data.width = width;
this->loaded_data.height = height;
if((height == this->text_rect.out_rect.getSize().y) && (width == this->text_rect.out_rect.getSize().x))
return;
this->text_rect.out_rect.setSize(sf::Vector2f(this->loaded_data.width, this->loaded_data.height));
this->text_rect.out_tex.create(this->loaded_data.width, this->loaded_data.height);
this->text_rect.out_rect.setTextureRect(sf::IntRect(0, 0, this->loaded_data.width, this->loaded_data.height));
}
void TextRectangle::updateText(int x_limit) {
// set the character size
this->actual_text.setCharacterSize(this->loaded_data.font_pixel_height); // in pixels, not points!
@@ -173,26 +210,27 @@ void TextRectangle::updateText(int x_limit) {
// set the text style
//this->actual_text.setStyle(sf::Text::Bold);
this->setTextWithLineWrapping(x_limit);
sf::FloatRect globalBounds = this->actual_text.getGlobalBounds();
int new_width = this->width;
int new_height = this->height;
int bounds_width = globalBounds.width + (globalBounds.left * 2);
int bounds_height = globalBounds.height + (globalBounds.top * 2);
if(new_width < bounds_width)
new_width = bounds_width;
if(new_height < bounds_height)
new_height = bounds_height;
if((new_width != this->width) || (new_height != this->height))
this->setSize(new_width, new_height);
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 = globalBounds.width + (globalBounds.left * 2);
int bounds_height = globalBounds.height + (globalBounds.top * 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((int)(5 * (((float)this->loaded_data.font_pixel_height) / BASE_PIXEL_FONT_HEIGHT)), 0);
globalBounds = this->actual_text.getGlobalBounds();
this->setSize(globalBounds.width + (globalBounds.left * 2), globalBounds.height + (globalBounds.top * 2));
this->setRealSize(globalBounds.width + (globalBounds.left * 2), globalBounds.height + (globalBounds.top * 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((new_width - (globalBounds.width + (globalBounds.left * 4))) / 2, (new_height - (globalBounds.height + (globalBounds.top * 4))) / 2);
this->actual_text.setPosition((int)((this->loaded_data.width - (globalBounds.width + (globalBounds.left * 2))) / 2), (int)((this->loaded_data.height - (globalBounds.height + (globalBounds.top * 2))) / 2));
}
sf::Color *used_color = this->base_bg_color;
switch(this->loaded_data.kind) {
@@ -219,7 +257,7 @@ void TextRectangle::updateText(int x_limit) {
}
void TextRectangle::updateSlides(float* time_seconds) {
time_seconds[0] = base_time_slide_factor * (this->height / (loaded_data.font_pixel_height * base_pixel_slide_factor));
time_seconds[0] = base_time_slide_factor * (this->loaded_data.height / (loaded_data.font_pixel_height * base_pixel_slide_factor));
time_seconds[1] = loaded_data.duration;
time_seconds[2] = base_time_slide_factor * (this->height / (loaded_data.font_pixel_height * base_pixel_slide_factor));
time_seconds[2] = base_time_slide_factor * (this->loaded_data.height / (loaded_data.font_pixel_height * base_pixel_slide_factor));
}