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.

318 lines
17KB

  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. sprite : array (style, enumeration) of core.sprite;
  8. glyphs : array (style) of core.font;
  9. ------------------------------------------------------------------------------------------
  10. procedure select_text_box (text : in string; x, y, width, height : in integer) is
  11. begin
  12. if core.cursor.x > x and core.cursor.x < x + width
  13. and core.cursor.y > y and core.cursor.y < y + height then
  14. core.write_text_box (text);
  15. end if;
  16. end select_text_box;
  17. ------------------------------------------------------------------------------------------
  18. procedure draw (index : in enumeration; x, y : in integer) is
  19. begin
  20. core.zoom := 1;
  21. core.draw (sprite (active, index), x, y);
  22. core.zoom := 2;
  23. end draw;
  24. ------------------------------------------------------------------------------------------
  25. procedure crop (index : in enumeration; x, y, u, v, width, height : in integer) is
  26. begin
  27. core.zoom := 1;
  28. core.draw (sprite (active, index), x, y, u, v, width, height);
  29. core.zoom := 2;
  30. end crop;
  31. ------------------------------------------------------------------------------------------
  32. procedure draw_horizontally (index : in enumeration; x, y, width : in integer) is
  33. base : constant integer := sprite (active, index).width;
  34. begin
  35. for move in 0 .. width / base - 1 loop
  36. draw (index, x + move * base, y);
  37. end loop;
  38. --
  39. crop (index, x + (width / base) * base, y, 0, 0, width mod base, sprite (active, index).height);
  40. end draw_horizontally;
  41. ------------------------------------------------------------------------------------------
  42. procedure draw_vertically (index : in enumeration; x, y, height : in integer) is
  43. base : constant integer := sprite (active, index).height;
  44. begin
  45. for move in 0 .. height / base - 1 loop
  46. draw (index, x, y + move * base);
  47. end loop;
  48. --
  49. crop (index, x, y + (height / base) * base, 0, 0, sprite (active, index).width, height mod base);
  50. end draw_vertically;
  51. ------------------------------------------------------------------------------------------
  52. procedure draw_background (index : in enumeration; x, y, width, height : in integer) is
  53. base_width : integer := sprite (active, index).width;
  54. base_height : integer := sprite (active, index).height;
  55. crop_width : integer := width mod base_width;
  56. crop_height : integer := height mod base_height;
  57. begin
  58. for move_y in 0 .. height / base_height - 1 loop
  59. for move_x in 0 .. width / base_width - 1 loop
  60. draw (index, x + move_x * base_width, y + move_y * base_height);
  61. end loop;
  62. --
  63. crop (index, x + width - crop_width, y + move_y * base_height, 0, 0, crop_width, base_height);
  64. end loop;
  65. --
  66. for move_x in 0 .. width / base_width - 1 loop
  67. crop (index, x + move_x * base_width, y + height - crop_height, 0, 0, base_width, crop_height);
  68. end loop;
  69. --
  70. crop (index, x + width - crop_width, y + height - crop_height, 0, 0, crop_width, crop_height);
  71. end draw_background;
  72. ------------------------------------------------------------------------------------------
  73. procedure configure is
  74. procedure load_ui (index : in style; folder_path : in string) is
  75. begin
  76. glyphs (index) := core.import_font ("./sprite/ui/" & folder_path & "/font.png", (if index = default then 16 else 24), 0);
  77. --
  78. for this in enumeration loop
  79. sprite (index, this) := core.import_sprite ("./sprite/ui/" & folder_path & core.lowercase (enumeration'image (this)) & ".png", 1, 1);
  80. end loop;
  81. end load_ui;
  82. begin
  83. core.echo (core.comment, "Configuring UI components...");
  84. --
  85. load_ui (default, "default/");
  86. load_ui (steam, "steam/");
  87. end configure;
  88. ------------------------------------------------------------------------------------------
  89. procedure draw_icon (data : in core.sprite; description : in string; x, y : in integer) is
  90. begin
  91. draw (icon, x, y);
  92. select_text_box (description, x, y, core.icon, core.icon);
  93. --
  94. core.zoom := 1;
  95. core.draw (data, x, y);
  96. core.zoom := 2;
  97. end draw_icon;
  98. ------------------------------------------------------------------------------------------
  99. procedure draw_overicon (description : in string; x, y : in integer) is
  100. begin
  101. draw (overicon, x, y);
  102. select_text_box (description, x, y, core.icon, core.icon);
  103. end draw_overicon;
  104. ------------------------------------------------------------------------------------------
  105. procedure draw_text_box (x, y, width, height : in integer) is
  106. offset : constant integer := sprite (active, text_middle).width;
  107. begin
  108. draw_background (text_middle, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  109. --
  110. draw_horizontally (text_upper, x + offset, y, width - 2 * offset);
  111. draw_horizontally (text_lower, x + offset, y + height - offset, width - 2 * offset);
  112. draw_vertically (text_left, x, y + offset, height - 2 * offset);
  113. draw_vertically (text_right, x + width - offset, y + offset, height - 2 * offset);
  114. --
  115. draw (text_upper_left, x, y);
  116. draw (text_upper_right, x + width - offset, y);
  117. draw (text_lower_left, x, y + height - offset);
  118. draw (text_lower_right, x + width - offset, y + height - offset);
  119. --
  120. core.write (core.read_text_box, x, y, glyphs (active));
  121. --
  122. select_text_box ("", x, y, width, height);
  123. end draw_text_box;
  124. ------------------------------------------------------------------------------------------
  125. procedure draw_frame (description : in string; x, y, width, height : in integer) is
  126. offset_x : constant integer := sprite (active, frame_middle).width;
  127. offset_y : constant integer := sprite (active, frame_middle).height;
  128. begin
  129. if height < core.icon or width < core.icon then
  130. return;
  131. end if;
  132. --
  133. draw_background (frame_middle, x + offset_x, y + offset_y, width - 2 * offset_x, height - 2 * offset_y);
  134. --
  135. draw_horizontally (frame_upper, x + offset_x, y, width - 2 * offset_x);
  136. draw_horizontally (frame_lower, x + offset_x, y + height - offset_y, width - 2 * offset_x);
  137. draw_vertically (frame_left, x, y + offset_y, height - 2 * offset_y);
  138. draw_vertically (frame_right, x + width - offset_x, y + offset_y, height - 2 * offset_y);
  139. --
  140. draw (frame_upper_left, x, y);
  141. draw (frame_upper_right, x + width - sprite (active, frame_upper_right).width, y);
  142. draw (frame_lower_left, x, y + height - sprite (active, frame_lower_left).height);
  143. draw (frame_lower_right, x + width - sprite (active, frame_lower_right).width, y + height - sprite (active, frame_lower_right).height);
  144. --
  145. select_text_box (description, x, y, width, height);
  146. end draw_frame;
  147. ------------------------------------------------------------------------------------------
  148. procedure draw_title_bar (x, y, width : in integer; title : in string) is
  149. middle_width : constant integer := width - sprite (active, title_bar_left).width - sprite (active, title_bar_right).width;
  150. begin
  151. draw (title_bar_left, x, y - sprite (active, title_bar_left).height);
  152. draw (title_bar_right, x + middle_width + sprite (active, title_bar_left).width, y - sprite (active, title_bar_right).height);
  153. --
  154. draw_horizontally (title_bar_middle, x + sprite (active, title_bar_left).width, y - sprite (active, title_bar_middle).height, middle_width);
  155. --
  156. core.write (title, x + sprite (active, title_bar_left).width / 2, y - sprite (active, title_bar_middle).height / 2 - 4, glyphs (active));
  157. --
  158. select_text_box ("", x, y - sprite (active, title_bar_middle).height, width, sprite (active, title_bar_middle).height);
  159. end draw_title_bar;
  160. ------------------------------------------------------------------------------------------
  161. procedure draw_scroll_bar (x, y, height : in integer; offset : in integer) is
  162. middle_height : constant integer := height - sprite (active, scroll_bar_upper).height - sprite (active, scroll_bar_lower).height;
  163. begin
  164. draw (scroll_bar_upper, x, y);
  165. draw (scroll_bar_lower, x, y + middle_height + sprite (active, scroll_bar_upper).height);
  166. --
  167. draw_vertically (scroll_bar_middle, x, y + sprite (active, scroll_bar_upper).height, middle_height);
  168. end draw_scroll_bar;
  169. ------------------------------------------------------------------------------------------
  170. procedure draw_menu (x, y, width, height : in integer; background : in boolean) is
  171. offset : constant integer := sprite (active, none).width;
  172. begin
  173. if width < sprite (active, corner_upper_left).width + sprite (active, corner_upper_right).width
  174. or width < sprite (active, corner_lower_left).width + sprite (active, corner_lower_right).width
  175. or height < sprite (active, corner_upper_left).height + sprite (active, corner_lower_left).height
  176. or height < sprite (active, corner_upper_right).height + sprite (active, corner_lower_right).height
  177. then return;
  178. end if;
  179. --
  180. if background then
  181. draw_background (main_background, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  182. end if;
  183. --
  184. declare upper : constant integer := width - sprite (active, corner_upper_left).width - sprite (active, corner_upper_right).width;
  185. lower : constant integer := width - sprite (active, corner_lower_left).width - sprite (active, corner_lower_right).width;
  186. left : constant integer := height - sprite (active, corner_upper_left).height - sprite (active, corner_lower_left).height;
  187. right : constant integer := height - sprite (active, corner_upper_right).height - sprite (active, corner_lower_right).height;
  188. begin
  189. draw_horizontally (border_upper, x + sprite (active, corner_upper_left).width, y, upper);
  190. draw_horizontally (border_lower, x + sprite (active, corner_lower_left).width, y + height - sprite (active, border_lower).height, lower);
  191. draw_vertically (border_left, x, y + sprite (active, corner_upper_left).height, left);
  192. draw_vertically (border_right, x + width - sprite (active, border_right).width, y + sprite (active, corner_upper_right).height, right);
  193. end;
  194. --
  195. draw (corner_upper_left, x, y);
  196. draw (corner_upper_right, x + width - sprite (active, corner_upper_right).width, y);
  197. draw (corner_lower_left, x, y + height - sprite (active, corner_lower_left).height);
  198. draw (corner_lower_right, x + width - sprite (active, corner_lower_right).width, y + height - sprite (active, corner_lower_right).height);
  199. --
  200. select_text_box ("", x, y, width, height);
  201. end draw_menu;
  202. ------------------------------------------------------------------------------------------
  203. procedure draw_tiny_menu (x, y, width, height : in integer; background : in boolean) is
  204. offset : constant integer := sprite (active, none).width;
  205. begin
  206. if width < sprite (active, tiny_corner_upper_left).width + sprite (active, tiny_corner_upper_right).width
  207. or width < sprite (active, tiny_corner_lower_left).width + sprite (active, tiny_corner_lower_right).width
  208. or height < sprite (active, tiny_corner_upper_left).height + sprite (active, tiny_corner_lower_left).height
  209. or height < sprite (active, tiny_corner_upper_right).height + sprite (active, tiny_corner_lower_right).height
  210. then return;
  211. end if;
  212. --
  213. if background then
  214. draw_background (main_background, x + offset, y + offset, width - 2 * offset, height - 2 * offset);
  215. end if;
  216. --
  217. declare upper : constant integer := width - sprite (active, tiny_corner_upper_left).width - sprite (active, tiny_corner_upper_right).width;
  218. lower : constant integer := width - sprite (active, tiny_corner_lower_left).width - sprite (active, tiny_corner_lower_right).width;
  219. left : constant integer := height - sprite (active, tiny_corner_upper_left).height - sprite (active, tiny_corner_lower_left).height;
  220. right : constant integer := height - sprite (active, tiny_corner_upper_right).height - sprite (active, tiny_corner_lower_right).height;
  221. begin
  222. draw_horizontally (tiny_border_upper, x + sprite (active, tiny_corner_upper_left).width, y, upper);
  223. draw_horizontally (tiny_border_lower, x + sprite (active, tiny_corner_lower_left).width, y + height - sprite (active, tiny_border_lower).height, lower);
  224. draw_vertically (tiny_border_left, x, y + sprite (active, tiny_corner_upper_left).height, left);
  225. draw_vertically (tiny_border_right, x + width - sprite (active, tiny_border_right).width, y + sprite (active, tiny_corner_upper_right).height, right);
  226. end;
  227. --
  228. draw (tiny_corner_upper_left, x, y);
  229. draw (tiny_corner_upper_right, x + width - sprite (active, tiny_corner_upper_right).width, y);
  230. draw (tiny_corner_lower_left, x, y + height - sprite (active, tiny_corner_lower_left).height);
  231. draw (tiny_corner_lower_right, x + width - sprite (active, tiny_corner_lower_right).width, y + height - sprite (active, tiny_corner_lower_right).height);
  232. --
  233. select_text_box ("", x, y, width, height);
  234. end draw_tiny_menu;
  235. ------------------------------------------------------------------------------------------
  236. procedure draw_icon_menu (description : in string; x, y, width, height : in integer) is
  237. offset_x : constant integer := sprite (active, icon_upper_left).width;
  238. offset_y : constant integer := sprite (active, icon_upper_left).height;
  239. begin
  240. if height < 2 * sprite (active, icon_upper_left).height or width < 2 * sprite (active, icon_upper_left).width then
  241. return;
  242. end if;
  243. --
  244. draw_horizontally (icon_upper, x + offset_x, y, width - 2 * offset_x);
  245. draw_horizontally (icon_lower, x + offset_x, y + height - offset_y, width - 2 * offset_x);
  246. draw_vertically (icon_left, x, y + offset_y, height - 2 * offset_y);
  247. draw_vertically (icon_right, x + width - offset_x, y + offset_y, height - 2 * offset_y);
  248. --
  249. draw (icon_upper_left, x, y);
  250. draw (icon_upper_right, x + width - sprite (active, icon_upper_right).width, y);
  251. draw (icon_lower_left, x, y + height - sprite (active, icon_lower_left).height);
  252. draw (icon_lower_right, x + width - sprite (active, icon_lower_right).width, y + height - sprite (active, icon_lower_right).height);
  253. --
  254. select_text_box (description, x, y, width, height);
  255. end draw_icon_menu;
  256. ------------------------------------------------------------------------------------------
  257. procedure draw_state_box (x, y : in integer) is
  258. begin
  259. ui.write ("Cursor X :" & core.cursor.x'image, x, y + 0);
  260. ui.write ("Cursor Y :" & core.cursor.y'image, x, y + 32);
  261. ui.write ("Cursor Mode :" & core.cursor_mode'image, x, y + 64);
  262. ui.write ("Camera X :" & core.camera.x'image, x, y + 96);
  263. ui.write ("Camera Y :" & core.camera.y'image, x, y + 128);
  264. ui.write ("Global Time :" & core.global_time'image, x, y + 160);
  265. ui.write ("Gameplay Time :" & core.gameplay_time'image, x, y + 192);
  266. ui.write ("Animation Time :" & core.animation_time'image, x, y + 224);
  267. ui.write ("Framerate :" & core.framerate'image, x, y + 256);
  268. end draw_state_box;
  269. ------------------------------------------------------------------------------------------
  270. procedure write (text : in string; x, y : in integer) is
  271. begin
  272. core.write (text, x, y, glyphs (active));
  273. end write;
  274. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  275. end ui;