DungeonCrawl
Loading...
Searching...
No Matches
language_menu.c
Go to the documentation of this file.
1
5#include "language_menu.h"
6
7#include "../common.h"
12
14 language_menu_strings = (char**) malloc(sizeof(char*) * MAX_LANGUAGE_MENU_STRINGS);
15 RETURN_WHEN_NULL(language_menu_strings, 1, "Language Menu", "Failed to allocate memory for language menu strings.");
16
17 for (int i = 0; i < MAX_LANGUAGE_MENU_STRINGS; i++) {
18 language_menu_strings[i] = NULL;
19 }
20
21 // update local once, so the strings are initialized
23 // add update local function to the observer list
25 return 0;
26}
27
28
29menu_result_t show_language_menu() {
30 const char* menu_options[2];
31
32 menu_options[0] = language_menu_strings[LANGUAGE_ENGLISH];
33 menu_options[1] = language_menu_strings[LANGUAGE_GERMAN];
34
35 menu_result_t res = MENU_CHANGE_LANGUAGE;
36
37 int selected_index = 0;
38 bool selection_active = true;
39 while (selection_active) {
40 const int menu_count = 2;
41 draw_menu(menu_options, menu_count, selected_index);
42
43 input_event_t input_event;
44 if (!get_input_blocking(&input_event)) {
45 continue;
46 }
47
48 // Use our logical input types
49 switch (input_event.type) {
50 case INPUT_UP:
51 selected_index = (selected_index - 1 + menu_count) % menu_count;
52 break;
53 case INPUT_DOWN:
54 selected_index = (selected_index + 1) % menu_count;
55 break;
56 case INPUT_CONFIRM: {
57 if (selected_index == 0) {
58 // English selected
59 set_language(LANGE_EN);
60 } else if (selected_index == 1) {
61 // German selected
62 set_language(LANGE_DE);
63 }
64 selection_active = false;
65 break;
66 }
67 case INPUT_QUIT:
68 res = MENU_EXIT;
69 selection_active = false;
70 break;
71 case INPUT_CANCEL:
72 res = MENU_CONTINUE;
73 selection_active = false;
74 break;
75 default:
76 // do nothing for other keys
77 break;
78 }
79 }
80
81 return res;
82}
83
85 if (language_menu_strings != NULL) {
86 for (int i = 0; i < MAX_LANGUAGE_MENU_STRINGS; i++) {
87 if (language_menu_strings[i] != NULL) {
88 free(language_menu_strings[i]);
89 }
90 }
91 free(language_menu_strings);
92 }
93}
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.
int init_language_menu()
Initializes the language menu with the needed data.
void shutdown_language_menu()
Shuts down the language menu and frees all associated data.
menu_result_t show_language_menu()
Draws the language menu to the screen.
Exposes functions for the language menu.
void update_language_menu_local(void)
Updates the localized strings for the language menu.
Exposes functionality to manage the localization of the language menu.
int set_language(const local_lang_t lang)
Sets the current language for the local handler and updates all registered observers.
void observe_local(void(*update_func)(void))
Registers an observer function to be notified of updates from the local handler.
Exposes public functions for the localization handler.
void draw_menu(const char **menu_options, int menu_count, int selected_index)
Draws a menu with the given options.
Definition menu.c:15
Exposes functions for outputting to the console.
Structure for advanced input events with context data.
Definition input_types.h:43