render: implement dynamic box sizing & line count calculation in render_message
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ibb30f7ff6fbff55f253397619e2208c76a6a6964
This commit is contained in:
parent
4f0a85df19
commit
22a2da75a9
1 changed files with 63 additions and 11 deletions
72
src/render.c
72
src/render.c
|
|
@ -450,18 +450,70 @@ void render_message(const char *message) {
|
|||
if (message == NULL)
|
||||
return;
|
||||
|
||||
int msg_len = strlen(message);
|
||||
float msg_ratio = 13.5;
|
||||
const int font_size = 20;
|
||||
const int line_height = font_size + 4;
|
||||
const int padding_x = 20;
|
||||
const int padding_y = 15;
|
||||
const int max_box_width = (int)(SCREEN_WIDTH * 0.75f);
|
||||
const int max_line_width = max_box_width - (padding_x * 2);
|
||||
|
||||
// Draw message box
|
||||
// TODO: Separate out the calculation of the x/y and width/height so that if a message takes up more than, say,
|
||||
// 75% of the screen width, we add a line break and increase the height. That would then require calculating the
|
||||
// width based on the longest line.
|
||||
Rectangle msg_bg = {(float)(SCREEN_WIDTH / 2.0f - ((msg_ratio / 2.03f) * msg_len)),
|
||||
(float)(SCREEN_HEIGHT / 2.0f - 30.0f), msg_ratio * msg_len, 60};
|
||||
// Calculate line breaks by iterating through message
|
||||
int line_count = 1;
|
||||
int current_line_width = 0;
|
||||
int longest_line_width = 0;
|
||||
|
||||
const char *msg_ptr = message;
|
||||
while (*msg_ptr && line_count <= 10) {
|
||||
// Estimate character width (average ~10px for 20pt font)
|
||||
int char_width = 10;
|
||||
current_line_width += char_width;
|
||||
|
||||
if (current_line_width > max_line_width && *msg_ptr == ' ') {
|
||||
if (current_line_width > longest_line_width)
|
||||
longest_line_width = current_line_width;
|
||||
line_count++;
|
||||
current_line_width = 0;
|
||||
}
|
||||
msg_ptr++;
|
||||
}
|
||||
|
||||
if (current_line_width > longest_line_width)
|
||||
longest_line_width = current_line_width;
|
||||
|
||||
// Measure full message
|
||||
int total_msg_width = MeasureText(message, font_size);
|
||||
int box_width = total_msg_width + (padding_x * 2);
|
||||
|
||||
// If message is too long, use wrapped width
|
||||
if (box_width > max_box_width) {
|
||||
box_width = max_box_width;
|
||||
}
|
||||
|
||||
// Ensure minimum width
|
||||
if (box_width < 200)
|
||||
box_width = 200;
|
||||
|
||||
// Calculate box height based on line count
|
||||
int box_height = (line_count * line_height) + (padding_y * 2);
|
||||
|
||||
// Center the box
|
||||
float box_x = (SCREEN_WIDTH - box_width) / 2.0f;
|
||||
float box_y = (SCREEN_HEIGHT - box_height) / 2.0f;
|
||||
|
||||
// Draw message box background
|
||||
Rectangle msg_bg = {box_x, box_y, (float)box_width, (float)box_height};
|
||||
DrawRectangleRec(msg_bg, (Color){45, 45, 45, 235});
|
||||
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, (Color){180, 180, 180, 255});
|
||||
|
||||
int msg_width = MeasureText(message, 20);
|
||||
DrawText(message, (SCREEN_WIDTH - msg_width) / 2, SCREEN_HEIGHT / 2 - 10, 20, WHITE);
|
||||
// Draw text centered
|
||||
int text_x = (SCREEN_WIDTH - total_msg_width) / 2;
|
||||
int text_y = (SCREEN_HEIGHT - font_size) / 2;
|
||||
|
||||
// For wrapped text, draw at box center with padding
|
||||
if (line_count > 1) {
|
||||
text_x = (int)box_x + padding_x;
|
||||
text_y = (int)box_y + padding_y;
|
||||
}
|
||||
|
||||
DrawText(message, text_x, text_y, font_size, WHITE);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue