DungeonCrawl
Loading...
Searching...
No Matches
output_handler.c
Go to the documentation of this file.
1
5#include "output_handler.h"
6
7#include "../../../common.h"
11#include "../../io_handler.h"// Include this to access global nc and stdplane
12
13#include <notcurses/nckeys.h>
14#include <stdlib.h>
15#include <string.h>
16
17#ifndef _WIN32
18 #include <unistd.h>// for usleep
19#else
20 #include <windows.h>
21#endif
22
23// Using the global variables from io_handler.h
24// No need to redeclare them here as they're already included through io_handler.h
25
27 if (!nc) {
28 log_msg(ERROR, "output_handler", "Null Notcurses instance provided");
29 return false;
30 }
31
32 if (!stdplane) {
33 log_msg(ERROR, "output_handler", "Null standard plane provided");
34 return false;
35 }
36 return true;
37}
38
39void clear_screen(void) {
40 if (!stdplane) {
41 log_msg(ERROR, "output_handler", "Output handler not initialized");
42 return;
43 }
44
45 // Clear the plane with the default colors
46 ncplane_set_base(stdplane, " ", 0, DEFAULT_COLORS);
47 ncplane_erase(stdplane);
48}
49
50void print_text(int y, int x, const char* text, uint64_t ncchannel) {
51 if (!stdplane || !text) {
52 log_msg(ERROR, "output_handler", "Output handler not initialized or null text");
53 return;
54 }
55
56 // Set the channels and print the text
57 ncplane_set_channels(stdplane, ncchannel);
58 ncplane_putstr_yx(stdplane, y, x, text);
59}
60
61void print_text_default(int y, int x, const char* text) {
62 print_text(y, x, text, DEFAULT_COLORS);
63}
64
65void print_text_multi_line(int y, int x, const char* text, int max_width, uint64_t ncchannel) {
66 if (!stdplane || !text || max_width <= 0) {
67 log_msg(ERROR, "output_handler", "Invalid parameters for print_text_multi_line");
68 return;
69 }
70
71 // Set the channels
72 ncplane_set_channels(stdplane, ncchannel);
73
74 // Handle line wrapping
75 const char* ptr = text;
76 int current_y = y;
77 int line_len = 0;
78 char line_buffer[256];// Assuming max line length
79
80 while (*ptr) {
81 // Copy characters until max_width or newline
82 line_len = 0;
83 while (*ptr && *ptr != '\n' && line_len < max_width) {
84 line_buffer[line_len++] = *ptr++;
85 }
86 line_buffer[line_len] = '\0';
87
88 // Print this line
89 ncplane_putstr_yx(stdplane, current_y, x, line_buffer);
90 current_y++;
91
92 // Handle newline character
93 if (*ptr == '\n') {
94 ptr++;
95 }
96 }
97}
98
99void print_text_multi_line_default(int y, int x, const char* text, int max_width) {
100 print_text_multi_line(y, x, text, max_width, DEFAULT_COLORS);
101}
102
103void print_text_multi_strings(int y, int x, const char* text[], int count, uint64_t ncchannel) {
104 if (!stdplane || !text || count <= 0) {
105 log_msg(ERROR, "output_handler", "Invalid parameters for print_text_multi_strings");
106 return;
107 }
108
109 // Set the channels
110 ncplane_set_channels(stdplane, ncchannel);
111
112 // Print each string on a new line
113 for (int i = 0; i < count; i++) {
114 if (text[i]) {
115 ncplane_putstr_yx(stdplane, y + i, x, text[i]);
116 }
117 }
118}
119
120void print_text_multi_strings_default(int y, int x, const char* text[], int count) {
121 print_text_multi_strings(y, x, text, count, DEFAULT_COLORS);
122}
123
124void print_menu(const char* title, const char** options, int option_count,
125 int selected_index, int y, int x,
126 uint64_t title_channel,
127 uint64_t option_channel,
128 uint64_t selected_channel) {
129 if (!stdplane || !title || !options || option_count <= 0) {
130 log_msg(ERROR, "output_handler", "Invalid menu parameters");
131 return;
132 }
133
134 // Print title
135 ncplane_set_channels(stdplane, title_channel);
136 ncplane_putstr_yx(stdplane, y, x, title);
137
138 // Print options
139 for (int i = 0; i < option_count; i++) {
140 if (i == selected_index) {
141 // Highlight selected option
142 ncplane_set_channels(stdplane, selected_channel);
143 } else {
144 ncplane_set_channels(stdplane, option_channel);
145 }
146 ncplane_putstr_yx(stdplane, y + i + 1, x, options[i]);
147 }
148}
149
150void print_menu_default(const char* title, const char** options, int option_count,
151 int selected_index, int y, int x) {
152 print_menu(title, options, option_count, selected_index, y, x,
153 DEFAULT_COLORS, DEFAULT_COLORS, INVERTED_COLORS);
154}
155
156bool get_text_input(const char* prompt, char* buffer, int buffer_size,
157 const char* confirm_msg, int y, int x) {
158 if (!stdplane || !buffer || buffer_size <= 0) {
159 log_msg(ERROR, "output_handler", "Invalid parameters for get_text_input");
160 return false;
161 }
162
163 // Clear the buffer
164 memset(buffer, 0, buffer_size);
165 int text_length = 0;
166 bool input_active = true;
167 bool confirmed = false;
168
169 while (input_active) {
170 // Clear screen
171 clear_screen();
172
173 // Display prompt
174 print_text_default(y, x, prompt);
175
176 // Display current input
177 print_text_default(y + 2, x, buffer);
178
179 // Display confirm message if provided
180 if (confirm_msg) {
181 print_text_default(y + 4, x, confirm_msg);
182 }
183
184 // Render the frame
185 render_frame();
186
187 // Get input
188 input_event_t input_event;
189 if (!get_input_blocking(&input_event)) {
190 continue;
191 }
192
193 // Process input
194 uint32_t key_id = input_event.raw_input.id;
195
196 if (input_event.type == INPUT_CONFIRM && text_length > 0) {
197 // Enter was pressed and we have input
198 input_active = false;
199 confirmed = true;
200 } else if (input_event.type == INPUT_CANCEL) {
201 // Escape was pressed
202 input_active = false;
203 confirmed = false;
204 } else if (key_id == NCKEY_BACKSPACE && text_length > 0) {
205 // Backspace was pressed and we have characters to delete
206 buffer[--text_length] = '\0';
207 } else if (key_id != 0 && text_length < buffer_size - 1 &&
208 !(input_event.type == INPUT_UP ||
209 input_event.type == INPUT_DOWN ||
210 input_event.type == INPUT_LEFT ||
211 input_event.type == INPUT_RIGHT)) {
212 // A printable character was typed
213 buffer[text_length++] = key_id;
214 buffer[text_length] = '\0';
215 }
216 }
217
218 return confirmed;
219}
220
221void show_message_screen(const char* message, const char* continue_message, int y, int x) {
222 if (!stdplane || !message) {
223 log_msg(ERROR, "output_handler", "Invalid parameters for show_message_screen");
224 return;
225 }
226
227 // Clear screen
228 clear_screen();
229
230 // Display message
231 print_text_default(y, x, message);
232
233 // Display continue message if provided
234 if (continue_message) {
235 print_text_default(y + 2, x, continue_message);
236 }
237
238 // Render the frame
239 render_frame();
240
241 // Wait for any input to continue
242 input_event_t input_event;
243 while (!get_input_blocking(&input_event));
244}
245
246bool render_frame(void) {
247 if (!nc) {
248 log_msg(ERROR, "output_handler", "Output handler not initialized");
249 return false;
250 }
251
252 // Render all changes directly
253 int ret = notcurses_render(nc);
254 return ret >= 0;
255}
256
257bool get_screen_dimensions(int* width, int* height) {
258 if (!stdplane || !width || !height) {
259 log_msg(ERROR, "output_handler", "Invalid parameters for get_screen_dimensions");
260 return false;
261 }
262
263 // Get the dimensions of the standard plane
264 *width = ncplane_dim_x(stdplane);
265 *height = ncplane_dim_y(stdplane);
266
267 return true;
268}
269
271 // Reset the globals
272 nc = NULL;
273 stdplane = NULL;
274}
Defines common macros, types, and global variables for color schemes and utilities.
bool get_input_blocking(input_event_t *event)
Get the next input event (blocking)
Exposes functions for working with input.
Declares structs for possible input types.
Exposes functions for the IO-Handler.
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Definition logger.c:246
Header file for logging functionality of the game.
void print_text(int y, int x, const char *text, uint64_t ncchannel)
Print text at a specific position.
bool get_text_input(const char *prompt, char *buffer, int buffer_size, const char *confirm_msg, int y, int x)
Get a text input from the user.
void print_text_default(int y, int x, const char *text)
Print text at a specific position with default colors.
void print_text_multi_strings_default(int y, int x, const char *text[], int count)
Print multiple strings on consecutive lines with default colors.
void shutdown_output_handler(void)
Shutdown the output handler.
bool render_frame(void)
Render the current frame.
bool init_output_handler()
Initialize the output handler.
bool get_screen_dimensions(int *width, int *height)
Get the dimensions of the standard plane.
void print_menu(const char *title, const char **options, int option_count, int selected_index, int y, int x, uint64_t title_channel, uint64_t option_channel, uint64_t selected_channel)
Print a menu with selection highlighting.
void clear_screen(void)
Clear the screen.
void print_text_multi_line_default(int y, int x, const char *text, int max_width)
Print multi-line text with word wrapping and default colors.
void print_text_multi_strings(int y, int x, const char *text[], int count, uint64_t ncchannel)
Print multiple strings on consecutive lines.
void show_message_screen(const char *message, const char *continue_message, int y, int x)
Show a message screen.
void print_menu_default(const char *title, const char **options, int option_count, int selected_index, int y, int x)
Print a menu with selection highlighting using default colors.
Exposes functions for outputting to the console.
Structure for advanced input events with context data.
Definition input_types.h:43