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.

479 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 out structure) is
  132. offset : constant integer := 16;
  133. new_width : constant integer := (if data.resize then 640 else data.width);
  134. new_height : constant integer := (if data.resize then 480 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;
  139. at_y : integer := new_y;
  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 loop
  145. case data.gui_list (x).kind is
  146. when gui_button =>
  147. draw_frame ("", at_x + offset, at_y, new_width - 2 * offset, core.icon + offset);
  148. draw_icon (data.gui_list (x).image, "", at_x + 3 * offset / 2, at_y + offset / 2);
  149. write (data.gui_list (x).text, at_x + 3 * offset / 2 + core.icon, at_y + offset / 2);
  150. when others => null;
  151. end case;
  152. end loop;
  153. end draw_structure;
  154. ------------------------------------------------------------------------------------------
  155. procedure configure is
  156. procedure load_ui (index : in style; folder_path : in string) is
  157. begin
  158. font (index) := core.import_font ("./sprite/ui/" & folder_path & "/font.png", 24, 0);
  159. --
  160. for this in element loop
  161. sprite (index, this) := core.import_sprite ("./sprite/ui/" & folder_path & "/" & core.lowercase (element'image (this)) & ".png", 1, 1);
  162. end loop;
  163. end load_ui;
  164. begin
  165. core.echo (core.comment, "Configuring UI components...");
  166. --
  167. for index in style loop
  168. load_ui (index, core.lowercase (style'image (index)));
  169. end loop;
  170. end configure;
  171. ------------------------------------------------------------------------------------------
  172. procedure synchronize is
  173. begin
  174. for index in 0 .. structure_limit loop
  175. exit when index = structure_count;
  176. --
  177. if core.signal_mode = core.signal_code'pos (structure_array (index).toggle) then
  178. structure_array (index).show := (if structure_array (index).show then false else true);
  179. end if;
  180. --
  181. if structure_array (index).show then
  182. draw_structure (structure_array (index));
  183. end if;
  184. end loop;
  185. end synchronize;
  186. ------------------------------------------------------------------------------------------
  187. procedure write (text : in string; x, y : in integer) is
  188. begin
  189. core.write (text, x, y, font (active));
  190. end write;
  191. ------------------------------------------------------------------------------------------
  192. procedure draw_icon (data : in core.sprite; description : in string; x, y : in integer; action : core.pointer := core.idle'access) is
  193. save_zoom : natural := core.zoom;
  194. begin
  195. select_text_box (description, x, y, core.icon, core.icon);
  196. --
  197. draw (icon, x, y);
  198. --
  199. core.zoom := 1;
  200. core.draw (data, x, y);
  201. core.zoom := save_zoom;
  202. --
  203. if core.cursor.x > x and core.cursor.x < x + core.icon
  204. and core.cursor.y > y and core.cursor.y < y + core.icon
  205. and core.cursor_mode = 1 then
  206. action.all;
  207. core.cursor_mode := 0;
  208. end if;
  209. end draw_icon;
  210. ------------------------------------------------------------------------------------------
  211. procedure draw_overicon (data : in core.sprite; description : in string; x, y : in integer; action : core.pointer := core.idle'access) is
  212. save_zoom : natural := core.zoom;
  213. begin
  214. select_text_box (description, x, y, core.icon, core.icon);
  215. --
  216. core.zoom := 1;
  217. core.draw (data, x, y);
  218. core.zoom := save_zoom;
  219. --
  220. draw (overicon, x, y);
  221. end draw_overicon;
  222. ------------------------------------------------------------------------------------------
  223. procedure draw_text_box (text : in string) is
  224. width : constant integer := 144;
  225. height : constant integer := 72;
  226. x : constant integer := (core.window_width - width) / 2;
  227. y : constant integer := (core.window_height - height) / 2;
  228. offset : constant integer := sprite (active, text_middle).width;
  229. begin
  230. draw_background (text_middle, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  231. --
  232. draw_horizontally (text_upper, x + offset, y, width - 2 * offset);
  233. draw_horizontally (text_lower, x + offset, y + height - offset, width - 2 * offset);
  234. draw_vertically (text_left, x, y + offset, height - 2 * offset);
  235. draw_vertically (text_right, x + width - offset, y + offset, height - 2 * offset);
  236. --
  237. draw (text_upper_left, x, y);
  238. draw (text_upper_right, x + width - offset, y);
  239. draw (text_lower_left, x, y + height - offset);
  240. draw (text_lower_right, x + width - offset, y + height - offset);
  241. --
  242. write (text, x, y);
  243. end draw_text_box;
  244. ------------------------------------------------------------------------------------------
  245. procedure draw_help_box (x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  246. offset : constant integer := sprite (active, text_middle).width;
  247. begin
  248. draw_background (text_middle, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  249. --
  250. draw_horizontally (text_upper, x + offset, y, width - 2 * offset);
  251. draw_horizontally (text_lower, x + offset, y + height - offset, width - 2 * offset);
  252. draw_vertically (text_left, x, y + offset, height - 2 * offset);
  253. draw_vertically (text_right, x + width - offset, y + offset, height - 2 * offset);
  254. --
  255. draw (text_upper_left, x, y);
  256. draw (text_upper_right, x + width - offset, y);
  257. draw (text_lower_left, x, y + height - offset);
  258. draw (text_lower_right, x + width - offset, y + height - offset);
  259. --
  260. core.write (core.read_text_box, x, y, font (active));
  261. --
  262. select_text_box ("", x, y, width, height);
  263. end draw_help_box;
  264. ------------------------------------------------------------------------------------------
  265. procedure draw_frame (description : in string; x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  266. offset_x : constant integer := sprite (active, frame_middle).width;
  267. offset_y : constant integer := sprite (active, frame_middle).height;
  268. begin
  269. if height < core.icon or width < core.icon then
  270. return;
  271. end if;
  272. --
  273. draw_background (frame_middle, x + offset_x, y + offset_y, width - 2 * offset_x, height - 2 * offset_y);
  274. --
  275. draw_horizontally (frame_upper, x + offset_x, y, width - 2 * offset_x);
  276. draw_horizontally (frame_lower, x + offset_x, y + height - offset_y, width - 2 * offset_x);
  277. draw_vertically (frame_left, x, y + offset_y, height - 2 * offset_y);
  278. draw_vertically (frame_right, x + width - offset_x, y + offset_y, height - 2 * offset_y);
  279. --
  280. draw (frame_upper_left, x, y);
  281. draw (frame_upper_right, x + width - sprite (active, frame_upper_right).width, y);
  282. draw (frame_lower_left, x, y + height - sprite (active, frame_lower_left).height);
  283. draw (frame_lower_right, x + width - sprite (active, frame_lower_right).width, y + height - sprite (active, frame_lower_right).height);
  284. --
  285. select_text_box (description, x, y, width, height);
  286. end draw_frame;
  287. ------------------------------------------------------------------------------------------
  288. procedure draw_title_bar (x, y, width : in integer; title : in string) is
  289. middle_width : constant integer := width - sprite (active, title_bar_left).width - sprite (active, title_bar_right).width;
  290. begin
  291. draw (title_bar_left, x, y - sprite (active, title_bar_left).height);
  292. draw (title_bar_right, x + middle_width + sprite (active, title_bar_left).width, y - sprite (active, title_bar_right).height);
  293. --
  294. draw_horizontally (title_bar_middle, x + sprite (active, title_bar_left).width, y - sprite (active, title_bar_middle).height, middle_width);
  295. --
  296. core.write (title, x + sprite (active, title_bar_left).width / 2 + 16, y - sprite (active, title_bar_middle).height / 2 - 8, font (active));
  297. --
  298. select_text_box (title, x, y - sprite (active, title_bar_middle).height, width, sprite (active, title_bar_middle).height);
  299. end draw_title_bar;
  300. ------------------------------------------------------------------------------------------
  301. procedure draw_fill_bar (x, y, width : in integer; fill : in float) is
  302. middle_width : constant integer := width - sprite (active, fill_bar_left).width - sprite (active, fill_bar_right).width;
  303. fill_width : constant integer := integer (float (middle_width) * fill);
  304. begin
  305. draw (fill_bar_left, x, y - sprite (active, fill_bar_left).height);
  306. draw (fill_bar_right, x + middle_width + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_right).height);
  307. --
  308. draw_horizontally (fill_bar_horizontal, x + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_horizontal).height, middle_width);
  309. draw_horizontally (fill_horizontal, x + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_horizontal).height, fill_width);
  310. --
  311. select_text_box ("", x, y, width, sprite (active, fill_bar_horizontal).height);
  312. end draw_fill_bar;
  313. ------------------------------------------------------------------------------------------
  314. procedure draw_scroll_bar (x, y, height : in integer; offset : in integer) is
  315. middle_height : constant integer := height - sprite (active, scroll_bar_upper).height - sprite (active, scroll_bar_lower).height;
  316. begin
  317. draw (scroll_bar_upper, x, y);
  318. draw (scroll_bar_lower, x, y + middle_height + sprite (active, scroll_bar_upper).height);
  319. --
  320. draw_vertically (scroll_bar_middle, x, y + sprite (active, scroll_bar_upper).height, middle_height);
  321. end draw_scroll_bar;
  322. ------------------------------------------------------------------------------------------
  323. procedure draw_menu (x, y, width, height : in integer) is
  324. offset : constant integer := sprite (active, none).width;
  325. begin
  326. declare upper : constant integer := width - sprite (active, corner_upper_left).width - sprite (active, corner_upper_right).width;
  327. lower : constant integer := width - sprite (active, corner_lower_left).width - sprite (active, corner_lower_right).width;
  328. left : constant integer := height - sprite (active, corner_upper_left).height - sprite (active, corner_lower_left).height;
  329. right : constant integer := height - sprite (active, corner_upper_right).height - sprite (active, corner_lower_right).height;
  330. begin
  331. draw_horizontally (border_upper, x + sprite (active, corner_upper_left).width, y, upper, action => core.move_camera_up'access);
  332. 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);
  333. draw_vertically (border_left, x, y + sprite (active, corner_upper_left).height, left, action => core.move_camera_left'access);
  334. 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);
  335. end;
  336. --
  337. draw (corner_upper_left, x, y);
  338. draw (corner_upper_right, x + width - sprite (active, corner_upper_right).width, y);
  339. draw (corner_lower_left, x, y + height - sprite (active, corner_lower_left).height);
  340. draw (corner_lower_right, x + width - sprite (active, corner_lower_right).width, y + height - sprite (active, corner_lower_right).height);
  341. --
  342. select_text_box ("", x, y, width, height);
  343. end draw_menu;
  344. ------------------------------------------------------------------------------------------
  345. procedure draw_tiny_menu (x, y, width, height : in integer) is
  346. offset : constant integer := sprite (active, none).width;
  347. begin
  348. draw_background (main_background, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  349. --
  350. declare upper : constant integer := width - sprite (active, tiny_corner_upper_left).width - sprite (active, tiny_corner_upper_right).width;
  351. lower : constant integer := width - sprite (active, tiny_corner_lower_left).width - sprite (active, tiny_corner_lower_right).width;
  352. left : constant integer := height - sprite (active, tiny_corner_upper_left).height - sprite (active, tiny_corner_lower_left).height;
  353. right : constant integer := height - sprite (active, tiny_corner_upper_right).height - sprite (active, tiny_corner_lower_right).height;
  354. begin
  355. draw_horizontally (tiny_border_upper, x + sprite (active, tiny_corner_upper_left).width, y, upper);
  356. draw_horizontally (tiny_border_lower, x + sprite (active, tiny_corner_lower_left).width, y + height - sprite (active, tiny_border_lower).height, lower);
  357. draw_vertically (tiny_border_left, x, y + sprite (active, tiny_corner_upper_left).height, left);
  358. draw_vertically (tiny_border_right, x + width - sprite (active, tiny_border_right).width, y + sprite (active, tiny_corner_upper_right).height, right);
  359. end;
  360. --
  361. draw (tiny_corner_upper_left, x, y);
  362. draw (tiny_corner_upper_right, x + width - sprite (active, tiny_corner_upper_right).width, y);
  363. draw (tiny_corner_lower_left, x, y + height - sprite (active, tiny_corner_lower_left).height);
  364. draw (tiny_corner_lower_right, x + width - sprite (active, tiny_corner_lower_right).width, y + height - sprite (active, tiny_corner_lower_right).height);
  365. --
  366. select_text_box ("", x, y, width, height);
  367. end draw_tiny_menu;
  368. ------------------------------------------------------------------------------------------
  369. procedure draw_icon_menu (description : in string; x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  370. offset_x : constant integer := sprite (active, icon_upper_left).width;
  371. offset_y : constant integer := sprite (active, icon_upper_left).height;
  372. begin
  373. if height < 2 * sprite (active, icon_upper_left).height
  374. or width < 2 * sprite (active, icon_upper_left).width then
  375. return;
  376. end if;
  377. --
  378. draw_horizontally (icon_upper, x + offset_x, y, width - 2 * offset_x);
  379. draw_horizontally (icon_lower, x + offset_x, y + height - offset_y, width - 2 * offset_x);
  380. draw_vertically (icon_left, x, y + offset_y, height - 2 * offset_y);
  381. draw_vertically (icon_right, x + width - offset_x, y + offset_y, height - 2 * offset_y);
  382. --
  383. draw (icon_upper_left, x, y);
  384. draw (icon_upper_right, x + width - sprite (active, icon_upper_right).width, y);
  385. draw (icon_lower_left, x, y + height - sprite (active, icon_lower_left).height);
  386. draw (icon_lower_right, x + width - sprite (active, icon_lower_right).width, y + height - sprite (active, icon_lower_right).height);
  387. --
  388. select_text_box (description, x, y, width, height);
  389. end draw_icon_menu;
  390. ------------------------------------------------------------------------------------------
  391. procedure draw_state_box (x, y : in integer) is -- TODO: Delete this at some point.
  392. begin
  393. ui.write ("Cursor X:" & core.cursor.x'image, x, y + 0);
  394. ui.write ("Cursor Y:" & core.cursor.y'image, x, y + 32);
  395. ui.write ("Cursor Mode:" & core.cursor_mode'image, x, y + 64);
  396. ui.write ("Camera X:" & core.camera.x'image, x, y + 96);
  397. ui.write ("Camera Y:" & core.camera.y'image, x, y + 128);
  398. ui.write ("Global Time:" & core.global_time'image, x, y + 160);
  399. ui.write ("Gameplay Time:" & core.gameplay_time'image, x, y + 192);
  400. ui.write ("Animation Time:" & core.animation_time'image, x, y + 224);
  401. ui.write ("Framerate:" & core.framerate'image, x, y + 256);
  402. end draw_state_box;
  403. ------------------------------------------------------------------------------------------
  404. procedure add_structure (data : in structure) is -- TODO: This is dumb, tho less error-prone...
  405. begin
  406. structure_array (structure_count) := data;
  407. --
  408. structure_count := structure_count + 1;
  409. end add_structure;
  410. ------------------------------------------------------------------------------------------
  411. procedure add_structure_button (data : in out structure; icon : in core.sprite; text, description : in string) is
  412. begin
  413. data.gui_list (data.gui_n).kind := gui_button;
  414. data.gui_list (data.gui_n).text := text;
  415. data.gui_list (data.gui_n).number := 0;
  416. data.gui_list (data.gui_n).image := icon;
  417. --
  418. data.gui_n := data.gui_n + 1;
  419. end add_structure_button;
  420. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  421. end ui;