ui.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "eruta.h"
  2. #include "image.h"
  3. #include "store.h"
  4. #include "zori.h"
  5. #include "zori_style.h"
  6. #include "zori_screen.h"
  7. #include "zori_console.h"
  8. #include "zori_button.h"
  9. #include "zori_page.h"
  10. #include "zori_menu.h"
  11. #include "draw.h"
  12. #include "monolog.h"
  13. #include "ui.h"
  14. /* This file contains the concrete implementation of the Eruta GUI
  15. * and it's various GUI screens and pages. */
  16. static struct ui_state the_ui_state;
  17. char POEM_TEXT[] = "After mankind's summer, winter suddenly came"
  18. "Most reached for the stars, but some remained"
  19. "The earth scarred by the Ancients unhostpitable"
  20. "The hopes low, and so much wisdom forgotten"
  21. "Yet when it seemed the last hour struck"
  22. "Our hope returned, a savior arose"
  23. "Lord Kei, who by Creator's grace"
  24. "Restored the Trees that guard us all from harm"
  25. "Thus ushered in a new millennial spring"
  26. "Lord Kei, watch over us and protect us"
  27. "Until the Travellers return with bounty of the stars";
  28. char INTRO_TEXT[] = "Millennia have passed since mankind first traveled to the moon. "
  29. "Civilizations rose as never before, yet to fall again. "
  30. "When all hope seemed lost, the 21 trees sprouted from the earth. "
  31. "They brought mysterious powers that protected and strengthened mankind. "
  32. "Hi!\n\n"
  33. "Hello µ world, this is me, 無 life should be\nfun for everyone!";
  34. char INTRO_TEXT2[] = "Something happened, and I was changed!";
  35. zori_id ui_state_make_sub_menu(zori_id parent, int x, int y, int w, int bh);
  36. /* Set up HUD. */
  37. void ui_state_init_hud(struct ui_state * ui,
  38. zori_display * display, zori_font * font) {
  39. Rebox box;
  40. ui->hud.page = zori_new_page(-1, ui->screen);
  41. LOG_NOTE("HUD page: %d\n", ui->hud.page);
  42. box = rebox_make(10, 300, 620, 200);
  43. ui->hud.dialog = zori_new_longtext(-1, ui->hud.page, box, INTRO_TEXT);
  44. }
  45. /* Set up Zori GUI. */
  46. void ui_state_init(struct ui_state * ui,
  47. zori_display * display,
  48. zori_font * font
  49. ) {
  50. struct zori_style style;
  51. Rebox box;
  52. memset(&style, 0, sizeof(style));
  53. style.text.font = font;
  54. style.text.color = color_rgb(255,255,255);
  55. style.text.flags = ZORI_FONT_ALIGN_CENTRE;
  56. style.back.color = color_rgba(0, 16, 64, 128);
  57. if ( !ZORI_ID_OK_P(zori_start(&style)) ) {
  58. LOG_ERROR( "Out of memory when allocating GUI.");
  59. return;
  60. }
  61. if (zori_start(&style) != ZORI_ID_OK) {
  62. LOG_ERROR( "Cannot set up ZORI UI screen.");
  63. return;
  64. }
  65. ui->main.background_image = store_load_bitmap_id(1000, "/image/background/eruta_mainmenu.png");
  66. ui->mouse_image = store_load_bitmap_id(1001, "image/gin/fountain-pen_32.png");
  67. ui->keyjoy_image = store_load_bitmap_id(1002, "image/gin/fountain-pen_32.png");
  68. if (ui->main.background_image < 0) {
  69. LOG_ERROR( "Cannot load main menu background.");
  70. }
  71. if (ui->mouse_image < 0) {
  72. LOG_ERROR( "Cannot load mouse cursor image.");
  73. }
  74. if (ui->keyjoy_image < 0) {
  75. LOG_ERROR( "Cannot load keyjoy cursor image.");
  76. }
  77. draw_convert_average_to_alpha(store_get_bitmap(ui->mouse_image), al_map_rgb(255,255,255));
  78. draw_convert_average_to_alpha(store_get_bitmap(ui->keyjoy_image), al_map_rgb(255,255,255));
  79. ui->screen = zori_new_screen(-1, display);
  80. if (!ZORI_ID_OK_P(ui->screen)) {
  81. LOG_ERROR( "Cannot set up main screen.");
  82. return;
  83. }
  84. ui->main.page = zori_new_page(-1, ui->screen);
  85. LOG_NOTE("Main page: %d\n", ui->main.page);
  86. box = rebox_make(280, 140, 140, 240);
  87. ui->main.menu = zori_new_menu(-1, ui->main.page, &box);
  88. zori_set_background_bitmap(ui->main.page,
  89. store_get_bitmap(ui->main.background_image));
  90. LOG_NOTE("Main menu: %d\n", ui->main.menu);
  91. zori_set_text_font_flags(ui->main.menu, ZORI_FONT_ALIGN_CENTRE);
  92. {
  93. Rebox box = rebox_make(300, 160, 100, 60);
  94. ui->main.button.resume = zori_new_button(-1, ui->main.menu, &box, "Continue");
  95. // zori_set_text_font_flags(ui->main.button.resume, ZORI_FONT_ALIGN_CENTRE);
  96. LOG_NOTE("Button: %d\n", ui->main.button.resume);
  97. }
  98. {
  99. Rebox box = rebox_make(300, 260, 100, 60);
  100. ui->main.button.new = zori_new_button(-1, ui->main.menu, &box, "New");
  101. zori_set_text_font_flags(ui->main.button.new, ZORI_FONT_ALIGN_RIGHT);
  102. LOG_NOTE("Button: %d\n", ui->main.button.new);
  103. }
  104. ui_state_init_hud(ui, display, font);
  105. }
  106. void ui_handle_main_menu(struct ui_state * ui, int selected) {
  107. LOG_NOTE("Main menu selected: %d\n", selected);
  108. if (selected == ui->main.button.resume) {
  109. LOG_NOTE("Resume");
  110. } else if (selected == ui->main.button.new) {
  111. LOG_NOTE("New");
  112. }
  113. }
  114. void ui_state_update(struct ui_state * ui,
  115. zori_display * display, zori_font * font) {
  116. int value = 0;
  117. if ((value = zori_result(ui->main.menu))) {
  118. ui_handle_main_menu(ui, value);
  119. }
  120. }
  121. #ifdef COMMENT_
  122. /* Direct mode operation? */
  123. INTRO_TEXT2 =
  124. def make_sub_menu(parent, x, y, w, h, bh)
  125. sub_menu = parent.make_menu(x, y, w, h, nil)
  126. sub_menu.make_button(x, y + 20, w - 20, bh, "Sub choice 1") do
  127. puts "choice 1"
  128. end
  129. sub_menu.make_button(x, y + 30 + bh, w - 20, bh, "Sub choice 2") do
  130. puts "choice 2"
  131. end
  132. sub_menu.make_button(x, y + 40 + 2* bh, w - 20, bh, "Sub choice 3") do
  133. puts "choice 3"
  134. end
  135. sub_menu.fit_children
  136. sub_menu.hide
  137. return sub_menu
  138. end
  139. def do_main_menu
  140. main_music = Music.load(:main, '/music/nethis-the_writer.ogg')
  141. $lote = nil
  142. $lobe = nil
  143. if PLAY_MUSIC
  144. res = main_music.play!
  145. end
  146. # res = nil
  147. # $main_menu = MainMenu.new
  148. # $main_menu.active = true
  149. Zori.make_page(:default) do |m|
  150. State.talk_box = m.make_longtext(10, 310, 620, 160, "Testing 1 2 3")
  151. State.talk_box.graph.each { |g| g.font = Eruta::Zori.font.id }
  152. State.talk_box.delay = 0.1
  153. State.talk_box.page_lines = 5
  154. State.talk_box.page = 0
  155. State.talk_box.hide
  156. end
  157. Zori.make_page(:main_menu) do |m|
  158. if MAIN_BACKGROUND
  159. main_back = Bitmap.load(:main_back,
  160. '/image/background/eruta_mainmenu.png')
  161. p main_back, main_back.width, main_back.height, main_back.name, main_back.id
  162. main_back = Bitmap.load(:main_back,
  163. '/image/background/eruta_mainmenu.png')
  164. p main_back, main_back.width, main_back.height, main_back.name, main_back.id
  165. p Bitmap[:main_back]
  166. p Store[:bitmap_main_back]
  167. m.graph_image(0, 0, main_back.id)
  168. end
  169. main_menu = m.make_menu(250, 190, 120, 440, nil)
  170. ma = main_menu
  171. main_button_1 = ma.make_button(260, 200, 100, 30, "Continue")
  172. sub_menu = make_sub_menu(main_button_1, 50, 190, 120, 440, 30)
  173. sub_menu.hide
  174. main_button_2 = ma.make_button(260, 240, 100, 30, "New") do
  175. do_start_test_map
  176. Zori.go(:default)
  177. end
  178. main_button_3 = ma.make_button(260, 280, 100, 30, "Settings") do
  179. Zori.go(:settings)
  180. end
  181. main_button_4 = ma.make_button(260, 320, 100, 30, "Instructions")
  182. main_button_5 = ma.make_button(260, 360, 100, 30, "µ£éè")
  183. main_button_5 << sub_menu
  184. main_button_5= ma.make_button(260, 400, 100, 30, "Quit") do
  185. Eruta.quit
  186. end
  187. main_menu.fit_children
  188. end
  189. Zori.make_page(:settings) do |se|
  190. lote2 = se.make_longtext(100, 10, 160, 100, INTRO_TEXT)
  191. lote2.delay = 1
  192. lote2.page = 0
  193. lote2.page_lines = 3
  194. # $lote2.line_stop = 999990
  195. settings_menu = se.make_menu(480, 380, 120, 440, nil)
  196. sm = settings_menu
  197. settings_ok_button = sm.make_button(500, 300, 100, 30, "Font 1") do
  198. if lote2
  199. lote2.graph.each { |g| g.font = 0 }
  200. end
  201. end
  202. settings_ok_button = sm.make_button(500, 350, 100, 30, "Font 2") do
  203. if lote2
  204. lote2.graph.each { |g| g.font = Eruta::Zori.font.id }
  205. end
  206. lote2.text = INTRO_TEXT2
  207. end
  208. settings_ok_button = sm.make_button(500, 400, 100, 30, "OK") do
  209. Zori.go(:main_menu)
  210. if lote
  211. lote.close
  212. lote = nil
  213. end
  214. end
  215. sm.fit_children
  216. end
  217. Zori[:main_menu].hide
  218. Zori[:settings].hide
  219. Zori.go(:main_menu)
  220. end
  221. #endif