config.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <stdlib.h>
  2. #include <fxlib.h>
  3. #include "config.h"
  4. #include "console.h"
  5. #define WIDTH 128
  6. #define HEIGHT 64
  7. #define menu_free_item(item) if(item) free(item->str)
  8. extern int* get_tex_flag_address();
  9. int test;
  10. void draw_menu(Menu* menu)
  11. {
  12. int x, y, i, key;
  13. int longest = 0, tmp;
  14. int border = 1, margin = 2, check_box_width = 15, item_height = 7, letter_width = 4;// inter_line = 1;
  15. int cursor_y = 0;
  16. DISPBOX box;
  17. for(i=0; i < menu->items_number; i++) {
  18. tmp = strlen(menu->items[i]->str) * letter_width;
  19. if(menu->items[i]->type == CHECK_BOX) tmp += check_box_width;
  20. if(tmp > longest) longest = tmp;
  21. }
  22. box.left = (WIDTH/2) - (longest/2) - margin - border;
  23. box.right = box.left + longest + 2*margin + 2*border;
  24. box.bottom = (HEIGHT/2) + (menu->items_number * item_height)/2 /*+ margin*/ + border;
  25. box.top = HEIGHT - box.bottom;
  26. Bdisp_AreaClr_VRAM(&box);
  27. Bdisp_AreaReverseVRAM(box.left, box.top, box.right, box.bottom);
  28. Bdisp_AreaReverseVRAM(box.left + border, box.top + border, box.right - border, box.bottom - border);
  29. while (key != KEY_CTRL_EXIT) {
  30. for(i=0; i < menu->items_number; i++) {
  31. PrintMini(box.left + border + margin, box.top + margin + item_height*i, (unsigned char*)menu->items[i]->str, MINI_OVER);
  32. if(menu->items[i]->type == CHECK_BOX) {
  33. if((menu->items[i]->action.val.value)) {
  34. 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);
  35. else PrintMini(box.right - border - margin - (check_box_width/2) , box.top + margin + item_height*i, (unsigned char*)"[ ]", MINI_OVER);
  36. }
  37. }
  38. }
  39. PrintMini(box.left + border + margin, box.top + margin + item_height*cursor_y, (unsigned char*)menu->items[cursor_y]->str, MINI_REV);
  40. if(menu->items[cursor_y]->type == CHECK_BOX) {
  41. if((menu->items[cursor_y]->action.val.value)) {
  42. 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);
  43. else PrintMini(box.right - border - margin - (check_box_width/2) , box.top + margin + item_height*cursor_y, (unsigned char*)"[ ]", MINI_OVER);
  44. }
  45. }
  46. GetKey(&key);
  47. if (key == KEY_CTRL_DOWN && cursor_y < menu->items_number-1) cursor_y++;
  48. if (key == KEY_CTRL_UP && cursor_y > 0) cursor_y--;
  49. if (key == KEY_CTRL_EXE) {
  50. //menu_test();
  51. if(menu->items[cursor_y]->type == CHECK_BOX) {
  52. if(menu->items[cursor_y]->action.val.value != NULL) { //Changing the state of the variable, as a boolean
  53. if(*(menu->items[cursor_y]->action.val.value)) *(menu->items[cursor_y]->action.val.value) = 0;
  54. else *(menu->items[cursor_y]->action.val.value) = 1;
  55. if (menu->items[cursor_y]->action.val.save_function != NULL) (*menu->items[cursor_y]->action.val.save_function)();
  56. }
  57. }
  58. else if(menu->items[cursor_y]->type == FUNCTION_CALL) {
  59. if(menu->items[cursor_y]->action.function != NULL) {
  60. (*(menu->items[cursor_y]->action.function))(); //Calling the function
  61. break;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. void menu_setup()
  68. {
  69. int number = 2;
  70. Menu menu;
  71. menu.items = malloc(sizeof(Menu_Item*) * number);
  72. menu.items[0] = menu_create_item("About", FUNCTION_CALL, menu_about, (void*)NULL);
  73. menu.items[1] = menu_create_item("Pretty Print", CHECK_BOX, get_tex_flag_address(), save_config);
  74. if(menu.items[0] && menu.items[1]) menu.items_number = number;
  75. else {
  76. menu_free_item(menu.items[0]);
  77. menu_free_item(menu.items[1]);
  78. }
  79. draw_menu(&menu);
  80. menu_free_item(menu.items[0]);
  81. menu_free_item(menu.items[1]);
  82. free(menu.items);
  83. }
  84. void menu_about()
  85. {
  86. int key;
  87. PopUpWin(6);
  88. PrintMini(12, 6, (unsigned char*)"Eigenmath symbolic maths", MINI_OVER);
  89. PrintMini(51, 14, (unsigned char*)"engine", MINI_OVER);
  90. PrintMini(12, 22, (unsigned char*)"Ported by Mike.", MINI_OVER);
  91. PrintMini(12, 30, (unsigned char*)"Enhanced by Nemh and the", MINI_OVER);
  92. PrintMini(38, 38, (unsigned char*)"PC community.", MINI_OVER);
  93. PrintMini(40, 46, (unsigned char*)"Early build", MINI_OVER);
  94. PrintMini(12, 54, (unsigned char*)"See : http://huit.re/eigen", MINI_OVER);
  95. GetKey(&key);
  96. return;
  97. }
  98. Menu_Item* menu_create_item(const char* str, Menu_Item_Type type, void* other, void* save_func)
  99. {
  100. Menu_Item* item = malloc(sizeof(Menu_Item));
  101. if(item == NULL) return NULL;
  102. item->str = malloc(sizeof(char)*(strlen(str) + 1));
  103. if(item->str == NULL) {
  104. free(item);
  105. return NULL;
  106. }
  107. else strcpy(item->str, str);
  108. item->type = type;
  109. if(type == CHECK_BOX) {
  110. item->action.val.value = other;
  111. item->action.val.save_function = save_func;
  112. }
  113. else if(type == FUNCTION_CALL) item->action.function = other;
  114. return item;
  115. }
  116. void save_config()
  117. {
  118. memory_save(CONFIG_FILE, (int*)get_tex_flag_address(), 4);
  119. }
  120. void load_config()
  121. {
  122. if(memory_exists(CONFIG_FILE)) *(int*)(get_tex_flag_address()) = *((int*)memory_load(CONFIG_FILE));
  123. }