dialogue: count dimensions correctly

This commit is contained in:
2025-12-03 12:14:38 +02:00
parent 32b9628ff2
commit 24f51eb22b
11 changed files with 73 additions and 43 deletions
+42 -15
View File
@@ -1,30 +1,58 @@
#include "dialogue.h"
#include "../app.h"
#include "group.h"
#include <ncurses.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
dialogue_t *init_dialogue(const char title[], const char text[],
const uint32_t width, const uint32_t height) {
#include "../app.h"
#include "dialogue.h"
#include "group.h"
dialogue_t *init_dialogue(const char title[], const char text[]) {
dialogue_t *dialogue = malloc(sizeof(dialogue_t));
dialogue->win = 0;
dialogue->ch_group = NULL;
dialogue->w.x = width;
dialogue->w.y = height;
dialogue->w.x = 0;
dialogue->w.y = 0;
strcpy(dialogue->w.title, title);
strcpy(dialogue->text, text);
return dialogue;
}
int32_t draw_dialogue(void *app, dialogue_t *d, uint32_t cur_x, uint32_t cur_y) {
app_t *_app = (app_t *) app;
d->w.m_x = (cur_x - d->w.x) / 2;
d->w.m_y = (cur_y - d->w.y) / 2;
int32_t draw_dialogue(void *_app, dialogue_t *d) {
app_t *app = (app_t *)_app;
d->win = newwin(d->w.y, d->w.x, d->w.m_y, d->w.m_x);
/* count dimensions */
uint32_t x = 1; /* when uses box */
uint32_t y = 1; /* when uses box */
uint32_t line_count = 0;
uint32_t line_max_len = 0;
line_max_len = get_max_line_len(d->text, &line_count);
y += line_count;
y += 2; /* margin for text */
if (d->ch_group) {
d->ch_group->w.m_y = y;
if (line_max_len < d->ch_group->w.x) {
line_max_len = d->ch_group->w.x;
d->ch_group->w.m_x = 1; /* when uses box */
} else {
/* TODO: centering */
}
y += d->ch_group->w.y;
}
x += line_max_len;
x += 1; /* when uses box */
y += 1; /* when uses box */
d->w.x = x;
d->w.y = y;
d->w.m_x = (app->cur_x - x) / 2;
d->w.m_y = (app->cur_y - y) / 2;
/* render window */
d->win = newwin(y, x, d->w.m_y, d->w.m_x);
wattrset(d->win, COLOR_PAIR(0) | A_BOLD | A_REVERSE);
/* background */
@@ -42,10 +70,9 @@ int32_t draw_dialogue(void *app, dialogue_t *d, uint32_t cur_x, uint32_t cur_y)
mvwprintw(d->win, 0, title_mx_pos + strlen(d->w.title), " ");
/* text */
print_multiline_text(d->win, d->text, d->w.x, 1, 1, PMT_ALIGN_CENTER);
wattroff(d->win, A_BOLD);
print_multiline_text(d->win, d->text, d->w.x, 2, 1, PMT_ALIGN_CENTER);
wattroff(d->win, A_REVERSE);
d->ch_group->w.m_x = (d->w.x - d->ch_group->w.x) / 2;
d->ch_group->w.m_y = 4;
draw_group(d->win, d->ch_group);
wrefresh(d->win);