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.

509 lines
26KB

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