Prototype game engine for Heroes of Might & Magic, featuring a gameplay plot-twist...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

478 lines
24KB

  1. -- Copyright (c) 2024 - Ognjen 'xolatile' Milan Robovic
  2. --
  3. -- GNU General Public Licence (version 3 or later)
  4. with core, ui;
  5. package body ui is
  6. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  7. type element is (
  8. none,
  9. main_background,
  10. --
  11. corner_upper_left, border_upper, corner_upper_right,
  12. border_left, border_right,
  13. corner_lower_left, border_lower, corner_lower_right,
  14. --
  15. tiny_corner_upper_left, tiny_border_upper, tiny_corner_upper_right,
  16. tiny_border_left, tiny_border_right,
  17. tiny_corner_lower_left, tiny_border_lower, tiny_corner_lower_right,
  18. --
  19. frame_upper_left, frame_upper, frame_upper_right,
  20. frame_left, frame_middle, frame_right,
  21. frame_lower_left, frame_lower, frame_lower_right,
  22. --
  23. icon_upper_left, icon_upper, icon_upper_right,
  24. icon_left, icon_right,
  25. icon_lower_left, icon_lower, icon_lower_right,
  26. --
  27. text_upper_left, text_upper, text_upper_right,
  28. text_left, text_middle, text_right,
  29. text_lower_left, text_lower, text_lower_right,
  30. --
  31. cursor, icon, overicon,
  32. --
  33. fill_bar_left, fill_bar_horizontal, fill_bar_right, fill_horizontal,
  34. --
  35. scroll_bar_lower, scroll_bar_middle, scroll_bar_upper,
  36. --
  37. title_bar_left, title_bar_middle, title_bar_right
  38. );
  39. ------------------------------------------------------------------------------------------
  40. structure_limit : constant natural := 12;
  41. sprite : array (style, element) of core.sprite;
  42. font : array (style) of core.font;
  43. structure_array : array (0 .. structure_limit) of structure;
  44. structure_count : natural := 0;
  45. ------------------------------------------------------------------------------------------
  46. procedure select_text_box (text : in string; x, y, width, height : in integer) is
  47. begin
  48. if core.cursor.x > x and core.cursor.x < x + width
  49. and core.cursor.y > y and core.cursor.y < y + height then
  50. core.write_text_box (text);
  51. end if;
  52. end select_text_box;
  53. ------------------------------------------------------------------------------------------
  54. procedure draw (index : in element := none;
  55. x : in integer := 0;
  56. y : in integer := 0;
  57. width : in integer := 0;
  58. height : in integer := 0) is
  59. save_zoom : natural := core.zoom;
  60. begin
  61. core.zoom := 1;
  62. core.draw (sprite (active, index), x, y, 0, 0, width, height);
  63. core.zoom := save_zoom;
  64. end draw;
  65. ------------------------------------------------------------------------------------------
  66. procedure draw_horizontally (index : in element; x, y, width : in integer; action : core.pointer := core.idle'access) is
  67. step : constant integer := sprite (active, index).width;
  68. begin
  69. for move in 0 .. width / step - 1 loop
  70. draw (index, x + move * step,y);
  71. end loop;
  72. --
  73. if width mod step > 0 then
  74. draw (index, x + (width / step) * step, y, width mod step, sprite (active, index).height);
  75. end if;
  76. --
  77. --~if core.cursor.x > x and core.cursor.x < x + width
  78. --~and core.cursor.y > y and core.cursor.y < y + sprite (active, index).height
  79. --~and core.cursor_mode = 1 then
  80. --~action.all;
  81. --~core.cursor_mode := 0;
  82. --~end if;
  83. end draw_horizontally;
  84. ------------------------------------------------------------------------------------------
  85. procedure draw_vertically (index : in element; x, y, height : in integer; action : core.pointer := core.idle'access) is
  86. step : constant integer := sprite (active, index).height;
  87. begin
  88. for move in 0 .. height / step - 1 loop
  89. draw (index, x, y + move * step);
  90. end loop;
  91. --
  92. if height mod step > 0 then
  93. draw (index, x, y + (height / step) * step, sprite (active, index).width, height mod step);
  94. end if;
  95. --
  96. --~if core.cursor.x > x and core.cursor.x < x + sprite (active, index).width
  97. --~and core.cursor.y > y and core.cursor.y < y + height
  98. --~and core.cursor_mode = 1 then
  99. --~action.all;
  100. --~core.cursor_mode := 0;
  101. --~end if;
  102. end draw_vertically;
  103. ------------------------------------------------------------------------------------------
  104. procedure draw_background (index : in element; x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  105. base_width : integer := sprite (active, index).width;
  106. base_height : integer := sprite (active, index).height;
  107. crop_width : integer := width mod base_width;
  108. crop_height : integer := height mod base_height;
  109. begin
  110. for move_y in 0 .. height / base_height - 1 loop
  111. for move_x in 0 .. width / base_width - 1 loop
  112. draw (index, x + move_x * base_width, y + move_y * base_height);
  113. end loop;
  114. --
  115. if width mod base_width > 0 then
  116. draw (index, x + width - crop_width, y + move_y * base_height, crop_width, base_height);
  117. end if;
  118. end loop;
  119. --
  120. for move_x in 0 .. width / base_width - 1 loop
  121. if height mod base_height > 0 then
  122. draw (index, x + move_x * base_width, y + height - crop_height, base_width, crop_height);
  123. end if;
  124. end loop;
  125. --
  126. if width mod base_width > 0 and height mod base_height > 0 then
  127. draw (index, x + width - crop_width, y + height - crop_height, crop_width, crop_height);
  128. end if;
  129. end draw_background;
  130. ------------------------------------------------------------------------------------------
  131. procedure draw_structure (data : in structure) is
  132. offset : constant integer := core.icon;
  133. new_width : constant integer := (if data.resize then 300 else data.width);
  134. new_height : constant integer := (if data.resize then data.gui_n * core.icon + 2 * offset else data.height);
  135. new_x : constant integer := (if data.center then (core.window_width - new_width) / 2 else data.x);
  136. new_y : constant integer := (if data.center then (core.window_height - new_height) / 2 else data.y);
  137. --
  138. at_x : integer := new_x + offset;
  139. at_y : integer := new_y + offset;
  140. begin
  141. draw_tiny_menu (new_x, new_y, new_width, new_height);
  142. draw_title_bar (new_x, new_y, new_width, data.title);
  143. --
  144. for x in 0 .. data.gui_n - 1 loop
  145. case data.gui_list (x).kind is
  146. when gui_button =>
  147. draw_icon (data.gui_list (x).image, "", at_x, at_y);
  148. draw_frame ("", at_x + core.icon, at_y, new_width - 2 * offset - core.icon, core.icon);
  149. write (data.gui_list (x).text, at_x + core.icon, at_y);
  150. at_y := at_y + core.icon;
  151. when others => null;
  152. end case;
  153. end loop;
  154. end draw_structure;
  155. ------------------------------------------------------------------------------------------
  156. procedure configure is
  157. procedure load_ui (index : in style; folder_path : in string) is
  158. begin
  159. font (index) := core.import_font ("./sprite/ui/" & folder_path & "/font.png", 24, 0);
  160. --
  161. for this in element loop
  162. sprite (index, this) := core.import_sprite ("./sprite/ui/" & folder_path & "/" & core.lowercase (element'image (this)) & ".png", 1, 1);
  163. end loop;
  164. end load_ui;
  165. begin
  166. core.echo (core.comment, "Configuring UI components...");
  167. --
  168. for index in style loop
  169. load_ui (index, core.lowercase (style'image (index)));
  170. end loop;
  171. end configure;
  172. ------------------------------------------------------------------------------------------
  173. procedure synchronize is
  174. begin
  175. for index in 0 .. structure_count - 1 loop
  176. if core.signal_mode = core.signal_code'pos (structure_array (index).toggle) then
  177. structure_array (index).show := (if structure_array (index).show then false else true);
  178. end if;
  179. --
  180. if structure_array (index).show then
  181. draw_structure (structure_array (index));
  182. end if;
  183. end loop;
  184. end synchronize;
  185. ------------------------------------------------------------------------------------------
  186. procedure write (text : in string; x, y : in integer) is
  187. begin
  188. core.write (text, x, y, font (active));
  189. end write;
  190. ------------------------------------------------------------------------------------------
  191. procedure draw_icon (data : in core.sprite; description : in string; x, y : in integer; action : core.pointer := core.idle'access) is
  192. save_zoom : natural := core.zoom;
  193. begin
  194. select_text_box (description, x, y, core.icon, core.icon);
  195. --
  196. draw (icon, x, y);
  197. --
  198. core.zoom := 1;
  199. core.draw (data, x, y);
  200. core.zoom := save_zoom;
  201. --
  202. if core.cursor.x > x and core.cursor.x < x + core.icon
  203. and core.cursor.y > y and core.cursor.y < y + core.icon
  204. and core.cursor_mode = 1 then
  205. action.all;
  206. core.cursor_mode := 0;
  207. end if;
  208. end draw_icon;
  209. ------------------------------------------------------------------------------------------
  210. procedure draw_overicon (data : in core.sprite; description : in string; x, y : in integer; action : core.pointer := core.idle'access) is
  211. save_zoom : natural := core.zoom;
  212. begin
  213. select_text_box (description, x, y, core.icon, core.icon);
  214. --
  215. core.zoom := 1;
  216. core.draw (data, x, y);
  217. core.zoom := save_zoom;
  218. --
  219. draw (overicon, x, y);
  220. end draw_overicon;
  221. ------------------------------------------------------------------------------------------
  222. procedure draw_text_box (text : in string) is
  223. width : constant integer := 144;
  224. height : constant integer := 72;
  225. x : constant integer := (core.window_width - width) / 2;
  226. y : constant integer := (core.window_height - height) / 2;
  227. offset : constant integer := sprite (active, text_middle).width;
  228. begin
  229. draw_background (text_middle, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  230. --
  231. draw_horizontally (text_upper, x + offset, y, width - 2 * offset);
  232. draw_horizontally (text_lower, x + offset, y + height - offset, width - 2 * offset);
  233. draw_vertically (text_left, x, y + offset, height - 2 * offset);
  234. draw_vertically (text_right, x + width - offset, y + offset, height - 2 * offset);
  235. --
  236. draw (text_upper_left, x, y);
  237. draw (text_upper_right, x + width - offset, y);
  238. draw (text_lower_left, x, y + height - offset);
  239. draw (text_lower_right, x + width - offset, y + height - offset);
  240. --
  241. write (text, x, y);
  242. end draw_text_box;
  243. ------------------------------------------------------------------------------------------
  244. procedure draw_help_box (x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  245. offset : constant integer := sprite (active, text_middle).width;
  246. begin
  247. draw_background (text_middle, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  248. --
  249. draw_horizontally (text_upper, x + offset, y, width - 2 * offset);
  250. draw_horizontally (text_lower, x + offset, y + height - offset, width - 2 * offset);
  251. draw_vertically (text_left, x, y + offset, height - 2 * offset);
  252. draw_vertically (text_right, x + width - offset, y + offset, height - 2 * offset);
  253. --
  254. draw (text_upper_left, x, y);
  255. draw (text_upper_right, x + width - offset, y);
  256. draw (text_lower_left, x, y + height - offset);
  257. draw (text_lower_right, x + width - offset, y + height - offset);
  258. --
  259. core.write (core.read_text_box, x, y, font (active));
  260. --
  261. select_text_box ("", x, y, width, height);
  262. end draw_help_box;
  263. ------------------------------------------------------------------------------------------
  264. procedure draw_frame (description : in string; x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  265. offset_x : constant integer := sprite (active, frame_middle).width;
  266. offset_y : constant integer := sprite (active, frame_middle).height;
  267. begin
  268. if height < core.icon or width < core.icon then
  269. return;
  270. end if;
  271. --
  272. draw_background (frame_middle, x + offset_x, y + offset_y, width - 2 * offset_x, height - 2 * offset_y);
  273. --
  274. draw_horizontally (frame_upper, x + offset_x, y, width - 2 * offset_x);
  275. draw_horizontally (frame_lower, x + offset_x, y + height - offset_y, width - 2 * offset_x);
  276. draw_vertically (frame_left, x, y + offset_y, height - 2 * offset_y);
  277. draw_vertically (frame_right, x + width - offset_x, y + offset_y, height - 2 * offset_y);
  278. --
  279. draw (frame_upper_left, x, y);
  280. draw (frame_upper_right, x + width - sprite (active, frame_upper_right).width, y);
  281. draw (frame_lower_left, x, y + height - sprite (active, frame_lower_left).height);
  282. draw (frame_lower_right, x + width - sprite (active, frame_lower_right).width, y + height - sprite (active, frame_lower_right).height);
  283. --
  284. select_text_box (description, x, y, width, height);
  285. end draw_frame;
  286. ------------------------------------------------------------------------------------------
  287. procedure draw_title_bar (x, y, width : in integer; title : in string) is
  288. middle_width : constant integer := width - sprite (active, title_bar_left).width - sprite (active, title_bar_right).width;
  289. begin
  290. draw (title_bar_left, x, y - sprite (active, title_bar_left).height);
  291. draw (title_bar_right, x + middle_width + sprite (active, title_bar_left).width, y - sprite (active, title_bar_right).height);
  292. --
  293. draw_horizontally (title_bar_middle, x + sprite (active, title_bar_left).width, y - sprite (active, title_bar_middle).height, middle_width);
  294. --
  295. core.write (title, x + sprite (active, title_bar_left).width / 2 + 16, y - sprite (active, title_bar_middle).height / 2 - 8, font (active));
  296. --
  297. select_text_box (title, x, y - sprite (active, title_bar_middle).height, width, sprite (active, title_bar_middle).height);
  298. end draw_title_bar;
  299. ------------------------------------------------------------------------------------------
  300. procedure draw_fill_bar (x, y, width : in integer; fill : in float) is
  301. middle_width : constant integer := width - sprite (active, fill_bar_left).width - sprite (active, fill_bar_right).width;
  302. fill_width : constant integer := integer (float (middle_width) * fill);
  303. begin
  304. draw (fill_bar_left, x, y - sprite (active, fill_bar_left).height);
  305. draw (fill_bar_right, x + middle_width + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_right).height);
  306. --
  307. draw_horizontally (fill_bar_horizontal, x + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_horizontal).height, middle_width);
  308. draw_horizontally (fill_horizontal, x + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_horizontal).height, fill_width);
  309. --
  310. select_text_box ("", x, y, width, sprite (active, fill_bar_horizontal).height);
  311. end draw_fill_bar;
  312. ------------------------------------------------------------------------------------------
  313. procedure draw_scroll_bar (x, y, height : in integer; offset : in integer) is
  314. middle_height : constant integer := height - sprite (active, scroll_bar_upper).height - sprite (active, scroll_bar_lower).height;
  315. begin
  316. draw (scroll_bar_upper, x, y);
  317. draw (scroll_bar_lower, x, y + middle_height + sprite (active, scroll_bar_upper).height);
  318. --
  319. draw_vertically (scroll_bar_middle, x, y + sprite (active, scroll_bar_upper).height, middle_height);
  320. end draw_scroll_bar;
  321. ------------------------------------------------------------------------------------------
  322. procedure draw_menu (x, y, width, height : in integer) is
  323. offset : constant integer := sprite (active, none).width;
  324. begin
  325. declare upper : constant integer := width - sprite (active, corner_upper_left).width - sprite (active, corner_upper_right).width;
  326. lower : constant integer := width - sprite (active, corner_lower_left).width - sprite (active, corner_lower_right).width;
  327. left : constant integer := height - sprite (active, corner_upper_left).height - sprite (active, corner_lower_left).height;
  328. right : constant integer := height - sprite (active, corner_upper_right).height - sprite (active, corner_lower_right).height;
  329. begin
  330. draw_horizontally (border_upper, x + sprite (active, corner_upper_left).width, y, upper, action => core.move_camera_up'access);
  331. draw_horizontally (border_lower, x + sprite (active, corner_lower_left).width, y + height - sprite (active, border_lower).height, lower, action => core.move_camera_down'access);
  332. draw_vertically (border_left, x, y + sprite (active, corner_upper_left).height, left, action => core.move_camera_left'access);
  333. draw_vertically (border_right, x + width - sprite (active, border_right).width, y + sprite (active, corner_upper_right).height, right, action => core.move_camera_right'access);
  334. end;
  335. --
  336. draw (corner_upper_left, x, y);
  337. draw (corner_upper_right, x + width - sprite (active, corner_upper_right).width, y);
  338. draw (corner_lower_left, x, y + height - sprite (active, corner_lower_left).height);
  339. draw (corner_lower_right, x + width - sprite (active, corner_lower_right).width, y + height - sprite (active, corner_lower_right).height);
  340. --
  341. select_text_box ("", x, y, width, height);
  342. end draw_menu;
  343. ------------------------------------------------------------------------------------------
  344. procedure draw_tiny_menu (x, y, width, height : in integer) is
  345. offset : constant integer := sprite (active, none).width;
  346. begin
  347. draw_background (main_background, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  348. --
  349. declare upper : constant integer := width - sprite (active, tiny_corner_upper_left).width - sprite (active, tiny_corner_upper_right).width;
  350. lower : constant integer := width - sprite (active, tiny_corner_lower_left).width - sprite (active, tiny_corner_lower_right).width;
  351. left : constant integer := height - sprite (active, tiny_corner_upper_left).height - sprite (active, tiny_corner_lower_left).height;
  352. right : constant integer := height - sprite (active, tiny_corner_upper_right).height - sprite (active, tiny_corner_lower_right).height;
  353. begin
  354. draw_horizontally (tiny_border_upper, x + sprite (active, tiny_corner_upper_left).width, y, upper);
  355. draw_horizontally (tiny_border_lower, x + sprite (active, tiny_corner_lower_left).width, y + height - sprite (active, tiny_border_lower).height, lower);
  356. draw_vertically (tiny_border_left, x, y + sprite (active, tiny_corner_upper_left).height, left);
  357. draw_vertically (tiny_border_right, x + width - sprite (active, tiny_border_right).width, y + sprite (active, tiny_corner_upper_right).height, right);
  358. end;
  359. --
  360. draw (tiny_corner_upper_left, x, y);
  361. draw (tiny_corner_upper_right, x + width - sprite (active, tiny_corner_upper_right).width, y);
  362. draw (tiny_corner_lower_left, x, y + height - sprite (active, tiny_corner_lower_left).height);
  363. draw (tiny_corner_lower_right, x + width - sprite (active, tiny_corner_lower_right).width, y + height - sprite (active, tiny_corner_lower_right).height);
  364. --
  365. select_text_box ("", x, y, width, height);
  366. end draw_tiny_menu;
  367. ------------------------------------------------------------------------------------------
  368. procedure draw_icon_menu (description : in string; x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  369. offset_x : constant integer := sprite (active, icon_upper_left).width;
  370. offset_y : constant integer := sprite (active, icon_upper_left).height;
  371. begin
  372. if height < 2 * sprite (active, icon_upper_left).height
  373. or width < 2 * sprite (active, icon_upper_left).width then
  374. return;
  375. end if;
  376. --
  377. draw_horizontally (icon_upper, x + offset_x, y, width - 2 * offset_x);
  378. draw_horizontally (icon_lower, x + offset_x, y + height - offset_y, width - 2 * offset_x);
  379. draw_vertically (icon_left, x, y + offset_y, height - 2 * offset_y);
  380. draw_vertically (icon_right, x + width - offset_x, y + offset_y, height - 2 * offset_y);
  381. --
  382. draw (icon_upper_left, x, y);
  383. draw (icon_upper_right, x + width - sprite (active, icon_upper_right).width, y);
  384. draw (icon_lower_left, x, y + height - sprite (active, icon_lower_left).height);
  385. draw (icon_lower_right, x + width - sprite (active, icon_lower_right).width, y + height - sprite (active, icon_lower_right).height);
  386. --
  387. select_text_box (description, x, y, width, height);
  388. end draw_icon_menu;
  389. ------------------------------------------------------------------------------------------
  390. procedure draw_state_box (x, y : in integer) is -- TODO: Delete this at some point.
  391. begin
  392. ui.write ("Cursor X:" & core.cursor.x'image, x, y + 0);
  393. ui.write ("Cursor Y:" & core.cursor.y'image, x, y + 32);
  394. ui.write ("Cursor Mode:" & core.cursor_mode'image, x, y + 64);
  395. ui.write ("Camera X:" & core.camera.x'image, x, y + 96);
  396. ui.write ("Camera Y:" & core.camera.y'image, x, y + 128);
  397. ui.write ("Global Time:" & core.global_time'image, x, y + 160);
  398. ui.write ("Gameplay Time:" & core.gameplay_time'image, x, y + 192);
  399. ui.write ("Animation Time:" & core.animation_time'image, x, y + 224);
  400. ui.write ("Framerate:" & core.framerate'image, x, y + 256);
  401. end draw_state_box;
  402. ------------------------------------------------------------------------------------------
  403. procedure add_structure (data : in structure) is -- TODO: This is dumb, tho less error-prone...
  404. begin
  405. structure_array (structure_count) := data;
  406. --
  407. structure_count := structure_count + 1;
  408. end add_structure;
  409. ------------------------------------------------------------------------------------------
  410. procedure add_structure_button (icon : in core.sprite; text : in core.short_string) is
  411. begin
  412. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).kind := gui_button;
  413. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).text := text;
  414. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).number := 0;
  415. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).image := icon;
  416. --
  417. structure_array (structure_count - 1).gui_n := structure_array (structure_count - 1).gui_n + 1;
  418. end add_structure_button;
  419. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  420. end ui;