Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / config.c
blobcaa699c2deae69a63b811b4ec62eaf9bdca57364
1 #include <stdlib.h>
2 #include <fxlib.h>
4 #include "config.h"
5 #include "console.h"
7 #define WIDTH 128
8 #define HEIGHT 64
10 #define menu_free_item(item) if(item) free(item->str)
12 extern int* get_tex_flag_address();
14 int test;
16 void draw_menu(Menu* menu)
18 int x, y, i, key;
19 int longest = 0, tmp;
20 int border = 1, margin = 2, check_box_width = 15, item_height = 7, letter_width = 4;// inter_line = 1;
21 int cursor_y = 0;
22 DISPBOX box;
24 for(i=0; i < menu->items_number; i++) {
25 tmp = strlen(menu->items[i]->str) * letter_width;
26 if(menu->items[i]->type == CHECK_BOX) tmp += check_box_width;
28 if(tmp > longest) longest = tmp;
31 box.left = (WIDTH/2) - (longest/2) - margin - border;
32 box.right = box.left + longest + 2*margin + 2*border;
33 box.bottom = (HEIGHT/2) + (menu->items_number * item_height)/2 /*+ margin*/ + border;
34 box.top = HEIGHT - box.bottom;
36 Bdisp_AreaClr_VRAM(&box);
38 Bdisp_AreaReverseVRAM(box.left, box.top, box.right, box.bottom);
40 Bdisp_AreaReverseVRAM(box.left + border, box.top + border, box.right - border, box.bottom - border);
42 while (key != KEY_CTRL_EXIT) {
43 for(i=0; i < menu->items_number; i++) {
44 PrintMini(box.left + border + margin, box.top + margin + item_height*i, (unsigned char*)menu->items[i]->str, MINI_OVER);
45 if(menu->items[i]->type == CHECK_BOX) {
46 if((menu->items[i]->action.val.value)) {
47 if(*(menu->items[i]->action.val.value)) PrintMini(box.right - border - margin - (check_box_width/2) , box.top + margin + item_height*i, (unsigned char*)"[x]", MINI_OVER);
48 else PrintMini(box.right - border - margin - (check_box_width/2) , box.top + margin + item_height*i, (unsigned char*)"[ ]", MINI_OVER);
52 PrintMini(box.left + border + margin, box.top + margin + item_height*cursor_y, (unsigned char*)menu->items[cursor_y]->str, MINI_REV);
53 if(menu->items[cursor_y]->type == CHECK_BOX) {
54 if((menu->items[cursor_y]->action.val.value)) {
55 if(*(menu->items[cursor_y]->action.val.value)) PrintMini(box.right - border - margin - (check_box_width/2) , box.top + margin + item_height*cursor_y, (unsigned char*)"[x]", MINI_OVER);
56 else PrintMini(box.right - border - margin - (check_box_width/2) , box.top + margin + item_height*cursor_y, (unsigned char*)"[ ]", MINI_OVER);
59 GetKey(&key);
61 if (key == KEY_CTRL_DOWN && cursor_y < menu->items_number-1) cursor_y++;
62 if (key == KEY_CTRL_UP && cursor_y > 0) cursor_y--;
64 if (key == KEY_CTRL_EXE) {
65 //menu_test();
66 if(menu->items[cursor_y]->type == CHECK_BOX) {
67 if(menu->items[cursor_y]->action.val.value != NULL) { //Changing the state of the variable, as a boolean
68 if(*(menu->items[cursor_y]->action.val.value)) *(menu->items[cursor_y]->action.val.value) = 0;
69 else *(menu->items[cursor_y]->action.val.value) = 1;
71 if (menu->items[cursor_y]->action.val.save_function != NULL) (*menu->items[cursor_y]->action.val.save_function)();
74 else if(menu->items[cursor_y]->type == FUNCTION_CALL) {
75 if(menu->items[cursor_y]->action.function != NULL) {
76 (*(menu->items[cursor_y]->action.function))(); //Calling the function
77 break;
84 void menu_setup()
86 int number = 2;
87 Menu menu;
88 menu.items = malloc(sizeof(Menu_Item*) * number);
90 menu.items[0] = menu_create_item("About", FUNCTION_CALL, menu_about, (void*)NULL);
91 menu.items[1] = menu_create_item("Pretty Print", CHECK_BOX, get_tex_flag_address(), save_config);
92 if(menu.items[0] && menu.items[1]) menu.items_number = number;
93 else {
94 menu_free_item(menu.items[0]);
95 menu_free_item(menu.items[1]);
98 draw_menu(&menu);
100 menu_free_item(menu.items[0]);
101 menu_free_item(menu.items[1]);
102 free(menu.items);
105 void menu_about()
107 int key;
108 PopUpWin(6);
109 PrintMini(12, 6, (unsigned char*)"Eigenmath symbolic maths", MINI_OVER);
110 PrintMini(51, 14, (unsigned char*)"engine", MINI_OVER);
111 PrintMini(12, 22, (unsigned char*)"Ported by Mike.", MINI_OVER);
112 PrintMini(12, 30, (unsigned char*)"Enhanced by Nemh and the", MINI_OVER);
113 PrintMini(38, 38, (unsigned char*)"PC community.", MINI_OVER);
114 PrintMini(40, 46, (unsigned char*)"Early build", MINI_OVER);
115 PrintMini(12, 54, (unsigned char*)"See : http://huit.re/eigen", MINI_OVER);
116 GetKey(&key);
117 return;
120 Menu_Item* menu_create_item(const char* str, Menu_Item_Type type, void* other, void* save_func)
122 Menu_Item* item = malloc(sizeof(Menu_Item));
123 if(item == NULL) return NULL;
125 item->str = malloc(sizeof(char)*(strlen(str) + 1));
126 if(item->str == NULL) {
127 free(item);
128 return NULL;
130 else strcpy(item->str, str);
132 item->type = type;
134 if(type == CHECK_BOX) {
135 item->action.val.value = other;
136 item->action.val.save_function = save_func;
138 else if(type == FUNCTION_CALL) item->action.function = other;
140 return item;
143 void save_config()
145 memory_save(CONFIG_FILE, (int*)get_tex_flag_address(), 4);
148 void load_config()
150 if(memory_exists(CONFIG_FILE)) *(int*)(get_tex_flag_address()) = *((int*)memory_load(CONFIG_FILE));