ui.c 9.1 KB

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