menu.d 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. module ui.menu;
  2. import raylib;
  3. import variables;
  4. import std.stdio;
  5. import system.abstraction;
  6. import system.config;
  7. import core.time;
  8. import core.thread;
  9. import std.string;
  10. import graphics.engine;
  11. import graphics.playback;
  12. import std.file;
  13. enum
  14. {
  15. MENU_ITEM_START = 0,
  16. MENU_ITEM_SOUND = 1,
  17. MENU_ITEM_SFX = 2,
  18. MENU_ITEM_FULLSCREEN = 3,
  19. MENU_ITEM_EXIT = 4,
  20. FADE_SPEED_IN = 0.02f,
  21. FADE_SPEED_OUT = 0.04f,
  22. INACTIVITY_TIMEOUT = 20.0f
  23. }
  24. void fadeEffect(float alpha, bool fadeIn, void delegate(float alpha) renderer)
  25. {
  26. const float FadeIncrement = 0.02f;
  27. while (fadeIn ? alpha < 2.0f : alpha > 0.0f)
  28. {
  29. alpha += fadeIn ? FadeIncrement : -FadeIncrement;
  30. BeginDrawing();
  31. ClearBackground(Colors.BLACK);
  32. renderer(alpha);
  33. EndDrawing();
  34. }
  35. }
  36. struct MenuState
  37. {
  38. string[] options;
  39. int selectedIndex;
  40. float fadeAlpha;
  41. float inactivityTimer;
  42. Texture2D logoTexture;
  43. Music menuMusic;
  44. bool fromSave;
  45. int logoX, logoY;
  46. }
  47. void renderText(float alpha, immutable(char)* text)
  48. {
  49. DrawTextEx(textFont, text,
  50. Vector2(GetScreenWidth() / 2 - MeasureText(text, 40) / 2,
  51. GetScreenHeight() / 2), 40, 0, Fade(Colors.WHITE, alpha)
  52. );
  53. }
  54. void renderLogo(float alpha, immutable(char)* name, bool fullscreen)
  55. {
  56. Texture2D companyLogo = LoadTexture(name);
  57. if (fullscreen)
  58. {
  59. DrawTexturePro(companyLogo,
  60. Rectangle(0, 0, cast(float) companyLogo.width, cast(float) companyLogo.height),
  61. Rectangle(0, 0, cast(float) GetScreenWidth(), cast(float) GetScreenHeight()),
  62. Vector2(0, 0), 0.0, Fade(Colors.WHITE, alpha));
  63. }
  64. else
  65. {
  66. DrawTexture(companyLogo, GetScreenWidth() / 2, GetScreenHeight() / 2, Colors.WHITE);
  67. }
  68. }
  69. void helloScreen()
  70. {
  71. debug
  72. {
  73. bool play = false;
  74. debugWriteln("hello screen showing");
  75. if (play == false)
  76. {
  77. videoFinished = true;
  78. goto debug_lab;
  79. }
  80. }
  81. else
  82. {
  83. fadeEffect(0.0f, true, (float alpha) {
  84. renderText(alpha, "powered by\n\nHimmel Engine");
  85. });
  86. fadeEffect(2.0f, false, (float alpha) {
  87. renderText(alpha, "powered by\n\nHimmel Engine");
  88. });
  89. /*
  90. fadeEffect(0.0f, true, (float alpha) {
  91. renderLogo(alpha, "atlus_logo.png".toStringz, true);
  92. });
  93. fadeEffect(fadeAlpha, false, (float alpha) {
  94. renderLogo(alpha, "atlus_logo.png".toStringz, true);
  95. });
  96. */
  97. // Play Opening Video
  98. BeginDrawing();
  99. debug debugWriteln("searching for video");
  100. if (std.file.exists(getcwd() ~ "/res/videos/soul_OP.moflex.mp4"))
  101. {
  102. debug debugWriteln("video found, playing");
  103. playVideo("/res/videos/soul_OP.moflex.mp4");
  104. }
  105. else
  106. {
  107. debug debugWriteln("video not found, skipping");
  108. videoFinished = true;
  109. }
  110. }
  111. debug_lab:
  112. }
  113. MenuState initMenuState()
  114. {
  115. MenuState state;
  116. state.fromSave = std.file.exists(getcwd() ~ "/save.txt");
  117. state.options = [
  118. "Start Game",
  119. "Sound: On",
  120. "SFX: On",
  121. "Fullscreen: On",
  122. "Exit Game",
  123. ];
  124. if (state.fromSave)
  125. state.options[MENU_ITEM_START] = "Continue";
  126. if (!audioEnabled)
  127. state.options[MENU_ITEM_SOUND] = "Sound: Off";
  128. state.fadeAlpha = 0.0f;
  129. state.inactivityTimer = 0.0f;
  130. state.selectedIndex = 0;
  131. state.logoTexture = LoadTexture("res/data/menu_logo.png");
  132. state.logoX = (GetScreenWidth() - state.logoTexture.width) / 2;
  133. state.logoY = (GetScreenHeight() - state.logoTexture.height) / 2 - 50;
  134. state.menuMusic = LoadMusicStream("res/data/menu_music.mp3");
  135. if (audioEnabled)
  136. {
  137. PlayMusicStream(state.menuMusic);
  138. }
  139. audio.declineSound = LoadSound("res/sfx/10002.wav");
  140. audio.acceptSound = LoadSound("res/sfx/10003.wav");
  141. audio.menuMoveSound = LoadSound("res/sfx/10004.wav");
  142. audio.menuChangeSound = LoadSound("res/sfx/00152.wav");
  143. audio.nonSound = LoadSound("res/sfx/00154.wav");
  144. return state;
  145. }
  146. void cleanupMenu(ref MenuState state)
  147. {
  148. UnloadTexture(state.logoTexture);
  149. UnloadMusicStream(state.menuMusic);
  150. }
  151. void drawMenu(ref const MenuState state)
  152. {
  153. BeginDrawing();
  154. ClearBackground(Colors.BLACK);
  155. DrawTextureEx(
  156. state.logoTexture,
  157. Vector2(state.logoX, state.logoY),
  158. 0.0f, 1.0f,
  159. Fade(Colors.WHITE, state.fadeAlpha)
  160. );
  161. for (int i = 0; i < state.options.length; i++)
  162. {
  163. Color textColor = (i == state.selectedIndex) ? Colors.LIGHTGRAY : Colors.GRAY;
  164. float textWidth = MeasureTextEx(textFont, toStringz(state.options[i]), 30, 0).x;
  165. float textX = (GetScreenWidth() - textWidth) / 2;
  166. int textY = state.logoY + state.logoTexture.height + 100 + (30 * i);
  167. DrawTextEx(
  168. textFont,
  169. toStringz(state.options[i]),
  170. Vector2(textX, textY),
  171. 30, 0,
  172. Fade(textColor, state.fadeAlpha)
  173. );
  174. }
  175. EndDrawing();
  176. }
  177. void handleInactivity(ref MenuState state)
  178. {
  179. state.inactivityTimer += GetFrameTime();
  180. if (state.inactivityTimer >= INACTIVITY_TIMEOUT)
  181. {
  182. while (state.fadeAlpha > 0.0f)
  183. {
  184. state.fadeAlpha -= FADE_SPEED_OUT;
  185. if (state.fadeAlpha < 0.0f)
  186. state.fadeAlpha = 0.0f;
  187. drawMenu(state);
  188. }
  189. StopMusicStream(state.menuMusic);
  190. playVideo("/res/videos/opening_old.mp4");
  191. if (audioEnabled)
  192. {
  193. PlayMusicStream(state.menuMusic);
  194. }
  195. state.inactivityTimer = 0.0f;
  196. while (state.fadeAlpha < 1.0f)
  197. {
  198. state.fadeAlpha += FADE_SPEED_IN;
  199. if (state.fadeAlpha > 1.0f)
  200. state.fadeAlpha = 1.0f;
  201. drawMenu(state);
  202. }
  203. }
  204. }
  205. void handleMenuNavigation(ref MenuState state)
  206. {
  207. bool moved = false;
  208. if (IsKeyPressed(KeyboardKey.KEY_DOWN))
  209. {
  210. state.selectedIndex = cast(int)((state.selectedIndex + 1) % state.options.length);
  211. state.inactivityTimer = 0;
  212. moved = true;
  213. }
  214. if (IsKeyPressed(KeyboardKey.KEY_UP))
  215. {
  216. state.selectedIndex = cast(int)(
  217. (state.selectedIndex - 1 + state.options.length) % state.options.length);
  218. state.inactivityTimer = 0;
  219. moved = true;
  220. }
  221. if (moved && sfxEnabled)
  222. {
  223. PlaySound(audio.menuMoveSound);
  224. }
  225. }
  226. void handleMenuSettings(ref MenuState state)
  227. {
  228. bool leftPressed = IsKeyPressed(KeyboardKey.KEY_LEFT);
  229. bool rightPressed = IsKeyPressed(KeyboardKey.KEY_RIGHT);
  230. if (!leftPressed && !rightPressed)
  231. return;
  232. state.inactivityTimer = 0;
  233. if (sfxEnabled) PlaySound(audio.menuChangeSound);
  234. switch (state.selectedIndex)
  235. {
  236. case MENU_ITEM_SOUND:
  237. audioEnabled = rightPressed ? false : true;
  238. state.options[MENU_ITEM_SOUND] = audioEnabled ? "Sound: On" : "Sound: Off";
  239. if (audioEnabled)
  240. {
  241. PlayMusicStream(state.menuMusic);
  242. }
  243. else
  244. {
  245. StopMusicStream(state.menuMusic);
  246. }
  247. break;
  248. case MENU_ITEM_SFX:
  249. sfxEnabled = rightPressed ? false : true;
  250. state.options[MENU_ITEM_SFX] = sfxEnabled ? "SFX: On" : "SFX: Off";
  251. break;
  252. case MENU_ITEM_FULLSCREEN:
  253. fullscreenEnabled = rightPressed ? false : true;
  254. state.options[MENU_ITEM_FULLSCREEN] = fullscreenEnabled ? "Fullscreen: On" : "Fullscreen: Off";
  255. if (fullscreenEnabled) {
  256. if (!IsWindowFullscreen()) {
  257. ToggleFullscreen();
  258. HideCursor();
  259. }
  260. } else if (fullscreenEnabled == false) {
  261. if (IsWindowFullscreen()) {
  262. ToggleFullscreen();
  263. ShowCursor();
  264. }
  265. }
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. void showMainMenu(ref GameState currentGameState)
  272. {
  273. MenuState state = initMenuState();
  274. while (state.fadeAlpha < 1.0f)
  275. {
  276. state.fadeAlpha += FADE_SPEED_IN;
  277. if (state.fadeAlpha > 1.0f)
  278. state.fadeAlpha = 1.0f;
  279. drawMenu(state);
  280. }
  281. while (!WindowShouldClose())
  282. {
  283. UpdateMusicStream(state.menuMusic);
  284. handleInactivity(state);
  285. handleMenuNavigation(state);
  286. handleMenuSettings(state);
  287. if (IsKeyPressed(KeyboardKey.KEY_ENTER) ||
  288. IsKeyPressed(KeyboardKey.KEY_SPACE))
  289. {
  290. if (sfxEnabled) PlaySound(audio.acceptSound);
  291. switch (state.selectedIndex)
  292. {
  293. case MENU_ITEM_START:
  294. while (state.fadeAlpha > 0.0f)
  295. {
  296. state.fadeAlpha -= FADE_SPEED_OUT;
  297. if (state.fadeAlpha < 0.0f)
  298. state.fadeAlpha = 0.0f;
  299. drawMenu(state);
  300. }
  301. cleanupMenu(state);
  302. currentGameState = GameState.InGame;
  303. debug debugWriteln("getting into game...");
  304. return;
  305. case MENU_ITEM_EXIT:
  306. cleanupMenu(state);
  307. currentGameState = GameState.Exit;
  308. return;
  309. default:
  310. break;
  311. }
  312. }
  313. drawMenu(state);
  314. }
  315. cleanupMenu(state);
  316. }