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.

496 lines
25KB

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