~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 7aa0ada94624cf7cf1d81d2b5d0d7d43770b5757 CTU-FEE-B0B35APO-Semestral-project/lib-gui/src/dialog.c -rw-r--r-- 4.7 KiB
7aa0ada9 — Rutherther chore: fix image in readme 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "dialog.h"
#include "display_utils.h"
#include "font.h"
#include "gui.h"
#include "gui_component_text.h"
#include "gui_container_info.h"
#include "gui_component_line.h"
#include "gui_window_info.h"
#include "input.h"
#include "logger.h"

#define DIALOG_WIDTH 350
#define DIALOG_HEIGHT 233
#define DIALOG_PADDING 10

typedef struct {
  bool running;
  gui_t *gui;

  window_t *dialog_window;
  component_t *title_component;
  component_t *dialog_component;

  text_t title_text;
  text_t dialog_text;

  font_t *font;

  const char *title;
  const char *text;
} dialog_state_t;

static void *dialog_construct(window_t *window, void *state);
static bool dialog_running(void *state);
static void dialog_job(void *state);

gui_container_info_t dialog_container_info[] = {
    {.type = CONT_GROUP, .payload.group.components_count = 3},
    {.type = CONT_ONE},
};

window_info_t dialog_info = {.containers_count = 2,
                             .containers = dialog_container_info,
                             .construct = dialog_construct};

bool dialog_info_show(gui_t *gui, font_t *font, const char *title, const char *text) {
  logger_info(gui->logger, __FILE__, __FUNCTION__, __LINE__, "Opening dialog");

  dialog_state_t state = {
      .running = true,
      .title = title,
      .text = text,
      .gui = gui,
      .font = font,
  };

  uint16_t commands_state = commands_save_state(gui->commands);
  bool success = gui_window_init_and_loop(gui, &state, dialog_info,
                                          dialog_running, dialog_job);
  commands_restore_state(gui->commands, commands_state);
  logger_info(gui->logger, __FILE__, __FUNCTION__, __LINE__, "Closing dialog");
  return success;
}

static void command_handler_close(void *state, int amount) {
  dialog_state_t *dstate = (dialog_state_t *)state;
  dstate->running = false;
}

static void dialog_construct_group(gui_t *gui, container_t *group_container, font_t *font, dialog_state_t *dstate) {
  int16_t begin_x = gui->size.x / 2 - DIALOG_WIDTH / 2;
  int16_t begin_y = gui->size.y / 2 - DIALOG_HEIGHT / 2;

  // construct line, rectangle and text
  gui_container_info_init(group_container, NULL, 0, begin_x, begin_y);

  gui_group_container_add_component(
      group_container,
      gui_line_create(&WHITE_PIXEL, 0, 0, DIALOG_WIDTH, DIALOG_HEIGHT));

  gui_group_container_add_component(
      group_container,
      gui_line_create(&WHITE_PIXEL, 10, font->size + 3, DIALOG_WIDTH - 20, 1));

  uint16_t title_center_x =
      DIALOG_WIDTH / 2 - font_measure_text(font, dstate->title).x / 2;

  dstate->title_component = gui_group_container_add_component(
      group_container,
      gui_text_create(&dstate->title_text, title_center_x, 2, 0, 0));
}
static void dialog_construct_one(gui_t *gui, container_t *one_container,
                                 font_t *font, dialog_state_t *dstate) {
  int16_t begin_x = gui->size.x / 2 - DIALOG_WIDTH / 2;
  int16_t begin_y = gui->size.y / 2 - DIALOG_HEIGHT / 2;
  // construct message
  gui_container_info_init(one_container, NULL, 0, begin_x,
                          begin_y + font->size + 5);
  one_container->width = DIALOG_WIDTH;
  one_container->height = DIALOG_HEIGHT - font->size - 5;

  dstate->dialog_component = gui_one_container_set_component(
      one_container,
      gui_text_create(&dstate->dialog_text, DIALOG_PADDING, DIALOG_PADDING,
                      DIALOG_WIDTH - 2 * DIALOG_PADDING, DIALOG_HEIGHT));
}

static void *dialog_construct(window_t *window, void *state) {
  dialog_state_t *dstate = (dialog_state_t *)state;
  gui_t *gui = dstate->gui;
  font_t *font = dstate->font;
  dstate->dialog_window = window;

  dstate->title_text.font = font;
  dstate->title_text.color = WHITE_PIXEL;
  dstate->title_text.line = dstate->title;

  dstate->dialog_text.font = font;
  dstate->dialog_text.color = WHITE_PIXEL;
  dstate->dialog_text.line = dstate->text;

  container_t *one_container = &window->containers[1];
  container_t *group_container = &window->containers[0];


  dialog_construct_group(gui, group_container, font, dstate);
  dialog_construct_one(gui, one_container, font, dstate);

  // register commands
  commands_register(gui->commands, IN_KEYBOARD, 0, command_handler_close, dstate);

  return state;
}

static bool dialog_running(void *state) {
  dialog_state_t *dstate = (dialog_state_t*)state;
  return dstate->running;
}

static void dialog_job(void *state) {
  dialog_state_t *dstate = (dialog_state_t *)state;
  //gui_t *gui = dstate->gui;
  font_t *font = dstate->font;

  uint16_t title_center_x =
      DIALOG_WIDTH / 2 - font_measure_text(font, dstate->title).x / 2;
  dstate->title_component->x = title_center_x;

  //int16_t begin_y = gui->size.y / 2 - DIALOG_HEIGHT / 2;
  //dstate->dialog_window->containers[0].y = begin_y + font->size + 5;
}
Do not follow this link