Add adaptive text size

This commit is contained in:
Nikola Petrov 2025-01-20 01:57:11 +01:00
parent d32c2fafb3
commit a0756a152f
4 changed files with 58 additions and 6 deletions

View File

@ -37,4 +37,7 @@ private:
std::array<Unit, 2> unit = {0};
DnaManager manager;
Rectangle likedTextBox;
Rectangle genTextBox;
};

View File

@ -1,4 +1,5 @@
#include <raylib.h>
Color ColorAdd(Color c1, Color c2);
Color ColorAddValue(Color c, int add);
Color ColorAddValue(Color c, int add);
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin);

View File

@ -7,9 +7,14 @@
#include <raylib.h>
#include <raymath.h>
#define TOP 1 - pos
#define TOP (1 - pos)
#define BOTTOM pos
// Dimentions for font size 20
// DISLIKE 83
// LIKE 46
// GEN 9999: 999/999 -> 196
void App::init(int screenWidth, int screenHeight)
{
this->screenWidth = screenWidth;
@ -37,10 +42,22 @@ void App::init(int screenWidth, int screenHeight)
float posY = (screenHeight - screenWidth) / 2.0f;
likedTextBox = TextInSpace({0,
0,
(float)screenWidth,
posY},
20.0f, 83.0f, 0.02f);
genTextBox = TextInSpace({0,
posY + screenWidth,
(float)screenWidth,
posY},
20.0f, 196.0f, 0.02f);
destB = {0, posY, (float)screenWidth, (float)screenWidth};
destA = destB;
float recPosX = screenWidth * 0.2f;
float recPosX = screenWidth * 0.3f;
disLikeBox = {0, posY, (float)recPosX, (float)screenWidth};
likeBox = {screenWidth - recPosX, posY, (float)recPosX, (float)screenWidth};
}
@ -116,6 +133,7 @@ void App::update()
}
rotation = 0.0f;
destA = destB;
topLiked = Liked::tbd;
}
}
@ -131,15 +149,15 @@ void App::draw()
DrawTexturePro(canvasTexure[TOP].texture, source, destA, origin, 360 - rotation, WHITE);
const char *text = TextFormat("GEN %d: %d / %d", manager.generation, unit[TOP].index + 1, NUM_PER_GEN);
DrawText(text, destB.x + 10, destB.y - 30, 20, BLACK);
DrawText(text, genTextBox.x, genTextBox.y, genTextBox.height, BLACK);
switch (topLiked)
{
case Liked::yes:
DrawText("LIKED", destB.x + 10, destB.y - 50, 20, BLACK);
DrawText("LIKED", likedTextBox.x, likedTextBox.y, likedTextBox.height, BLACK);
break;
case Liked::no:
DrawText("DISLIKE", destB.x + 10, destB.y - 50, 20, BLACK);
DrawText("DISLIKE", likedTextBox.x, likedTextBox.y, likedTextBox.height, BLACK);
break;
default:
break;

View File

@ -23,4 +23,34 @@ Color ColorAdd(Color c1, Color c2)
int a = std::clamp(c1.a + c2.a, 0, 255);
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a};
}
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin)
{
float br = box.width / box.height;
float tr = textW / textH;
Rectangle ret = {0, 0, 0, 0};
float hm = box.height * margin;
float wm = box.width * margin;
ret.y = box.y + hm;
ret.x = box.x + wm;
ret.height = box.height - hm;
ret.width = box.width - wm;
if (br < tr)
{
// bolj kvadrat izracunaj visino iz sirine
ret.height = ret.width / tr;
}
else
{
// bolj podolgovat izracunaj sirino iz visine
ret.width = ret.height * tr;
}
return ret;
}