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.

484 lines
25KB

  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 320 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, data.gui_list (x).info, at_x, at_y);
  148. draw_frame (data.gui_list (x).info, at_x + core.icon + 2, at_y, new_width - 2 * offset - core.icon - 2, core.icon);
  149. write (data.gui_list (x).text, at_x + core.icon + 6, at_y + 2);
  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 a is begin core.echo (core.warning, "Heyo world!"); end a;
  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. core.block_queue ((x, y, width, height, 2, a'access));
  263. --
  264. select_text_box ("", x, y, width, height);
  265. end draw_help_box;
  266. ------------------------------------------------------------------------------------------
  267. procedure draw_frame (description : in string; x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  268. offset_x : constant integer := sprite (active, frame_middle).width;
  269. offset_y : constant integer := sprite (active, frame_middle).height;
  270. begin
  271. if height < core.icon or width < core.icon then
  272. return;
  273. end if;
  274. --
  275. draw_background (frame_middle, x + offset_x, y + offset_y, width - 2 * offset_x, height - 2 * offset_y);
  276. --
  277. draw_horizontally (frame_upper, x + offset_x, y, width - 2 * offset_x);
  278. draw_horizontally (frame_lower, x + offset_x, y + height - offset_y, width - 2 * offset_x);
  279. draw_vertically (frame_left, x, y + offset_y, height - 2 * offset_y);
  280. draw_vertically (frame_right, x + width - offset_x, y + offset_y, height - 2 * offset_y);
  281. --
  282. draw (frame_upper_left, x, y);
  283. draw (frame_upper_right, x + width - sprite (active, frame_upper_right).width, y);
  284. draw (frame_lower_left, x, y + height - sprite (active, frame_lower_left).height);
  285. draw (frame_lower_right, x + width - sprite (active, frame_lower_right).width, y + height - sprite (active, frame_lower_right).height);
  286. --
  287. select_text_box (description, x, y, width, height);
  288. end draw_frame;
  289. ------------------------------------------------------------------------------------------
  290. procedure draw_title_bar (x, y, width : in integer; title : in string) is
  291. middle_width : constant integer := width - sprite (active, title_bar_left).width - sprite (active, title_bar_right).width;
  292. begin
  293. draw (title_bar_left, x, y - sprite (active, title_bar_left).height);
  294. draw (title_bar_right, x + middle_width + sprite (active, title_bar_left).width, y - sprite (active, title_bar_right).height);
  295. --
  296. draw_horizontally (title_bar_middle, x + sprite (active, title_bar_left).width, y - sprite (active, title_bar_middle).height, middle_width);
  297. --
  298. core.write (title, x + sprite (active, title_bar_left).width / 2 + 20, y - sprite (active, title_bar_middle).height / 2 - 6, font (active));
  299. --
  300. select_text_box (title, x, y - sprite (active, title_bar_middle).height, width, sprite (active, title_bar_middle).height);
  301. end draw_title_bar;
  302. ------------------------------------------------------------------------------------------
  303. procedure draw_fill_bar (x, y, width : in integer; fill : in float) is
  304. middle_width : constant integer := width - sprite (active, fill_bar_left).width - sprite (active, fill_bar_right).width;
  305. fill_width : constant integer := integer (float (middle_width) * fill);
  306. begin
  307. draw (fill_bar_left, x, y - sprite (active, fill_bar_left).height);
  308. draw (fill_bar_right, x + middle_width + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_right).height);
  309. --
  310. draw_horizontally (fill_bar_horizontal, x + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_horizontal).height, middle_width);
  311. draw_horizontally (fill_horizontal, x + sprite (active, fill_bar_left).width, y - sprite (active, fill_bar_horizontal).height, fill_width);
  312. --
  313. select_text_box ("", x, y, width, sprite (active, fill_bar_horizontal).height);
  314. end draw_fill_bar;
  315. ------------------------------------------------------------------------------------------
  316. procedure draw_scroll_bar (x, y, height : in integer; offset : in integer) is
  317. middle_height : constant integer := height - sprite (active, scroll_bar_upper).height - sprite (active, scroll_bar_lower).height;
  318. begin
  319. draw (scroll_bar_upper, x, y);
  320. draw (scroll_bar_lower, x, y + middle_height + sprite (active, scroll_bar_upper).height);
  321. --
  322. draw_vertically (scroll_bar_middle, x, y + sprite (active, scroll_bar_upper).height, middle_height);
  323. end draw_scroll_bar;
  324. ------------------------------------------------------------------------------------------
  325. procedure draw_menu (x, y, width, height : in integer) is
  326. offset : constant integer := sprite (active, none).width;
  327. begin
  328. declare upper : constant integer := width - sprite (active, corner_upper_left).width - sprite (active, corner_upper_right).width;
  329. lower : constant integer := width - sprite (active, corner_lower_left).width - sprite (active, corner_lower_right).width;
  330. left : constant integer := height - sprite (active, corner_upper_left).height - sprite (active, corner_lower_left).height;
  331. right : constant integer := height - sprite (active, corner_upper_right).height - sprite (active, corner_lower_right).height;
  332. begin
  333. draw_horizontally (border_upper, x + sprite (active, corner_upper_left).width, y, upper, action => core.move_camera_up'access);
  334. 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);
  335. draw_vertically (border_left, x, y + sprite (active, corner_upper_left).height, left, action => core.move_camera_left'access);
  336. 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);
  337. end;
  338. --
  339. draw (corner_upper_left, x, y);
  340. draw (corner_upper_right, x + width - sprite (active, corner_upper_right).width, y);
  341. draw (corner_lower_left, x, y + height - sprite (active, corner_lower_left).height);
  342. draw (corner_lower_right, x + width - sprite (active, corner_lower_right).width, y + height - sprite (active, corner_lower_right).height);
  343. --
  344. select_text_box ("", x, y, width, height);
  345. end draw_menu;
  346. ------------------------------------------------------------------------------------------
  347. procedure draw_tiny_menu (x, y, width, height : in integer) is
  348. offset : constant integer := sprite (active, none).width;
  349. begin
  350. draw_background (main_background, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  351. --
  352. declare upper : constant integer := width - sprite (active, tiny_corner_upper_left).width - sprite (active, tiny_corner_upper_right).width;
  353. lower : constant integer := width - sprite (active, tiny_corner_lower_left).width - sprite (active, tiny_corner_lower_right).width;
  354. left : constant integer := height - sprite (active, tiny_corner_upper_left).height - sprite (active, tiny_corner_lower_left).height;
  355. right : constant integer := height - sprite (active, tiny_corner_upper_right).height - sprite (active, tiny_corner_lower_right).height;
  356. begin
  357. draw_horizontally (tiny_border_upper, x + sprite (active, tiny_corner_upper_left).width, y, upper);
  358. draw_horizontally (tiny_border_lower, x + sprite (active, tiny_corner_lower_left).width, y + height - sprite (active, tiny_border_lower).height, lower);
  359. draw_vertically (tiny_border_left, x, y + sprite (active, tiny_corner_upper_left).height, left);
  360. draw_vertically (tiny_border_right, x + width - sprite (active, tiny_border_right).width, y + sprite (active, tiny_corner_upper_right).height, right);
  361. end;
  362. --
  363. draw (tiny_corner_upper_left, x, y);
  364. draw (tiny_corner_upper_right, x + width - sprite (active, tiny_corner_upper_right).width, y);
  365. draw (tiny_corner_lower_left, x, y + height - sprite (active, tiny_corner_lower_left).height);
  366. draw (tiny_corner_lower_right, x + width - sprite (active, tiny_corner_lower_right).width, y + height - sprite (active, tiny_corner_lower_right).height);
  367. --
  368. select_text_box ("", x, y, width, height);
  369. end draw_tiny_menu;
  370. ------------------------------------------------------------------------------------------
  371. procedure draw_icon_menu (description : in string; x, y, width, height : in integer; action : core.pointer := core.idle'access) is
  372. offset_x : constant integer := sprite (active, icon_upper_left).width;
  373. offset_y : constant integer := sprite (active, icon_upper_left).height;
  374. begin
  375. if height < 2 * sprite (active, icon_upper_left).height
  376. or width < 2 * sprite (active, icon_upper_left).width then
  377. return;
  378. end if;
  379. --
  380. draw_horizontally (icon_upper, x + offset_x, y, width - 2 * offset_x);
  381. draw_horizontally (icon_lower, x + offset_x, y + height - offset_y, width - 2 * offset_x);
  382. draw_vertically (icon_left, x, y + offset_y, height - 2 * offset_y);
  383. draw_vertically (icon_right, x + width - offset_x, y + offset_y, height - 2 * offset_y);
  384. --
  385. draw (icon_upper_left, x, y);
  386. draw (icon_upper_right, x + width - sprite (active, icon_upper_right).width, y);
  387. draw (icon_lower_left, x, y + height - sprite (active, icon_lower_left).height);
  388. draw (icon_lower_right, x + width - sprite (active, icon_lower_right).width, y + height - sprite (active, icon_lower_right).height);
  389. --
  390. select_text_box (description, x, y, width, height);
  391. end draw_icon_menu;
  392. ------------------------------------------------------------------------------------------
  393. procedure draw_state_box (x, y : in integer) is
  394. begin
  395. ui.write ("Cursor X:" & core.cursor.x'image, x, y + 0);
  396. ui.write ("Cursor Y:" & core.cursor.y'image, x, y + 32);
  397. ui.write ("Cursor Mode:" & core.cursor_mode'image, x, y + 64);
  398. ui.write ("Camera X:" & core.camera.x'image, x, y + 96);
  399. ui.write ("Camera Y:" & core.camera.y'image, x, y + 128);
  400. ui.write ("Global Time:" & core.global_time'image, x, y + 160);
  401. ui.write ("Gameplay Time:" & core.gameplay_time'image, x, y + 192);
  402. ui.write ("Animation Time:" & core.animation_time'image, x, y + 224);
  403. ui.write ("Framerate:" & core.framerate'image, x, y + 256);
  404. end draw_state_box;
  405. ------------------------------------------------------------------------------------------
  406. procedure add_structure (data : in structure) is
  407. begin
  408. structure_array (structure_count) := data;
  409. structure_array (structure_count).gui_list := new gui_array (0 .. structure_array (structure_count).gui_n - 1);
  410. structure_array (structure_count).gui_n := 0;
  411. --
  412. core.increment (structure_count);
  413. end add_structure;
  414. ------------------------------------------------------------------------------------------
  415. procedure add_structure_button (icon : in core.sprite; text : in core.short_string; description : in core.long_string := "") is
  416. begin
  417. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).kind := gui_button;
  418. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).text := text;
  419. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).info := description;
  420. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).number := 0;
  421. structure_array (structure_count - 1).gui_list (structure_array (structure_count - 1).gui_n).image := icon;
  422. --
  423. core.increment (structure_array (structure_count - 1).gui_n);
  424. end add_structure_button;
  425. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  426. end ui;