MainMenu_image_buttons.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. function init ()
  2. -- Create needed variables
  3. create(Types.EngineVariables, 'engineVariables');
  4. create(Types.GraphicsVariables, 'graphicsVariables');
  5. create(Types.InputVariables, 'inputVariables');
  6. create(Types.PathsVariables, 'pathsVariables');
  7. create(Types.WindowVariables, 'windowVariables');
  8. create(Types.Conditional, 'loadButtonPressed')
  9. create(Types.Conditional, 'loadButtonHover')
  10. create(Types.Conditional, 'playButtonPressed')
  11. create(Types.Conditional, 'playButtonHover')
  12. create(Types.Conditional, 'editorButtonPressed')
  13. create(Types.Conditional, 'editorButtonHover')
  14. create(Types.Conditional, 'optionsButtonPressed')
  15. create(Types.Conditional, 'optionsButtonHover')
  16. create(Types.Conditional, 'aboutButtonPressed')
  17. create(Types.Conditional, 'aboutButtonHover')
  18. create(Types.Conditional, 'exitButtonPressed')
  19. create(Types.Conditional, 'exitButtonHover')
  20. -- Load button textures
  21. buttonLoadTexture = loadTexture2D('buttons\\button_load_0.png')
  22. buttonLoadTexture:loadToMemory()
  23. buttonLoadTexture:loadToVideoMemory()
  24. buttonLoadPressedTexture = loadTexture2D('buttons\\button_load_1.png')
  25. buttonLoadPressedTexture:loadToMemory()
  26. buttonLoadPressedTexture:loadToVideoMemory()
  27. -- Play button textures
  28. buttonPlayTexture = loadTexture2D('buttons\\button_play_0.png')
  29. buttonPlayTexture:loadToMemory()
  30. buttonPlayTexture:loadToVideoMemory()
  31. buttonPlayPressedTexture = loadTexture2D('buttons\\button_play_1.png')
  32. buttonPlayPressedTexture:loadToMemory()
  33. buttonPlayPressedTexture:loadToVideoMemory()
  34. -- Editor button textures
  35. buttonEditorTexture = loadTexture2D('buttons\\button_editor_0.png')
  36. buttonEditorTexture:loadToMemory()
  37. buttonEditorTexture:loadToVideoMemory()
  38. buttonEditorPressedTexture = loadTexture2D('buttons\\button_editor_1.png')
  39. buttonEditorPressedTexture:loadToMemory()
  40. buttonEditorPressedTexture:loadToVideoMemory()
  41. -- Options button textures
  42. buttonOptionsTexture = loadTexture2D('buttons\\button_options_0.png')
  43. buttonOptionsTexture:loadToMemory()
  44. buttonOptionsTexture:loadToVideoMemory()
  45. buttonOptionsPressedTexture = loadTexture2D('buttons\\button_options_1.png')
  46. buttonOptionsPressedTexture:loadToMemory()
  47. buttonOptionsPressedTexture:loadToVideoMemory()
  48. -- About button textures
  49. buttonAboutTexture = loadTexture2D('buttons\\button_about_0.png')
  50. buttonAboutTexture:loadToMemory()
  51. buttonAboutTexture:loadToVideoMemory()
  52. buttonAboutPressedTexture = loadTexture2D('buttons\\button_about_1.png')
  53. buttonAboutPressedTexture:loadToMemory()
  54. buttonAboutPressedTexture:loadToVideoMemory()
  55. -- Exit button textures
  56. buttonExitTexture = loadTexture2D('buttons\\button_exit_0.png')
  57. buttonExitTexture:loadToMemory()
  58. buttonExitTexture:loadToVideoMemory()
  59. buttonExitPressedTexture = loadTexture2D('buttons\\button_exit_1.png')
  60. buttonExitPressedTexture:loadToMemory()
  61. buttonExitPressedTexture:loadToVideoMemory()
  62. -- Engine logo texture
  63. praxisLogoTexture = loadTexture2D('logo1.png')
  64. praxisLogoTexture:loadToMemory()
  65. praxisLogoTexture:loadToVideoMemory()
  66. -- Fmod logo texture
  67. fmodLogoTexture = loadTexture2D('FMOD Logo White - Black Background.png')
  68. fmodLogoTexture:loadToMemory()
  69. fmodLogoTexture:loadToVideoMemory()
  70. praxisLogoScale = 1
  71. fmodLogoScale = 0.05
  72. fmodLogoOffsetMultX = 0.05
  73. fmodLogoOffsetMultY = 1.95
  74. --buttonSizeX = 100.0
  75. --buttonSizeY = 25.0
  76. buttonSizeX = buttonExitTexture:getTextureWidth()
  77. buttonSizeY = buttonExitTexture:getTextureHeight()
  78. buttonHalfSizeX = buttonSizeX / 2
  79. buttonHalfSizeY = buttonSizeY / 2
  80. buttonOffsetMultX = 1.75
  81. buttonOffsetMultY = 1.75
  82. buttonSpacing = 5
  83. fileBrowserLoadMap = FileBrowserDialog.new()
  84. fileBrowserLoadMap.m_name = 'fileBrowserLoadMap'
  85. fileBrowserLoadMap.m_title = 'Load Map File'
  86. fileBrowserLoadMap.m_filter = '.pmap,*.*'
  87. fileBrowserLoadMap.m_rootPath = pathsVariables.map_path
  88. fileBrowserLoadMapOpened = false
  89. ErrHandlerLoc.logErrorCode(ErrorCode.Initialize_success, getLuaFilename())
  90. end
  91. function update (p_deltaTime)
  92. -- Make sure the mouse is released, so the buttons can be pressed
  93. setMouseCapture(false)
  94. -- Calculate the position of the middle of the screen
  95. halfScreenSizeX = graphicsVariables.current_resolution_x / 2.0
  96. halfScreenSizeY = graphicsVariables.current_resolution_y / 2.0
  97. -- Calculate the starting position for the buttons (relative to window size, not absolute, so the buttons are always in the right place)
  98. buttonPositionX = halfScreenSizeX * buttonOffsetMultX
  99. buttonPositionY = halfScreenSizeY * buttonOffsetMultY
  100. -- Calculate engine logo size based based on texture and window size (try to fit it on screen vertically)
  101. engineLogoAdjustedSizeX = praxisLogoTexture:getTextureWidth() / (praxisLogoTexture:getTextureWidth() / graphicsVariables.current_resolution_x) * praxisLogoScale
  102. engineLogoAdjustedSizeY = praxisLogoTexture:getTextureHeight() / (praxisLogoTexture:getTextureWidth() / graphicsVariables.current_resolution_x) * praxisLogoScale
  103. -- Calculate fmod logo size based on texture and window size (so it scaled with the screen size)
  104. fmodLogoAdjustedSizeMultAverage = ((fmodLogoTexture:getTextureHeight() / graphicsVariables.current_resolution_y) + (fmodLogoTexture:getTextureWidth() / graphicsVariables.current_resolution_x)) / 2
  105. fmodLogoAdjustedSizeX = fmodLogoTexture:getTextureWidth() / fmodLogoAdjustedSizeMultAverage * fmodLogoScale
  106. fmodLogoAdjustedSizeY = fmodLogoTexture:getTextureHeight() / fmodLogoAdjustedSizeMultAverage * fmodLogoScale
  107. -- Remove window padding and border size, so the content inside the window fills the whole window
  108. GUI.PushStyleVar(ImGuiStyleVar.WindowPadding, 0.0, 0.0)
  109. GUI.PushStyleVar(ImGuiStyleVar.WindowBorderSize, 0.0)
  110. -- Draw the background color
  111. GUI.PushStyleColor(ImGuiCol.WindowBg, 0.102, 0.102, 0.102, 255.0)
  112. GUI.SetNextWindowPos(0, 0)
  113. GUI.SetNextWindowSize(graphicsVariables.current_resolution_x, graphicsVariables.current_resolution_y)
  114. GUI.Begin('BACKGROUND', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoMouseInputs, ImGuiWindowFlags.NoFocusOnAppearing))
  115. GUI.End()
  116. GUI.PopStyleColor()
  117. -- Draw the engine logo
  118. GUI.SetNextWindowPos((graphicsVariables.current_resolution_x - engineLogoAdjustedSizeX) / 2, (graphicsVariables.current_resolution_y - engineLogoAdjustedSizeY) / 2)
  119. GUI.SetNextWindowSize(engineLogoAdjustedSizeX, engineLogoAdjustedSizeY)
  120. GUI.Begin('PRAXIS LOGO', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoBackground, ImGuiWindowFlags.NoMouseInputs))
  121. GUI.Image(praxisLogoTexture, engineLogoAdjustedSizeX, engineLogoAdjustedSizeY)
  122. GUI.End()
  123. -- Draw the fmod logo
  124. GUI.SetNextWindowPos((halfScreenSizeX - (fmodLogoAdjustedSizeX / 2)) * fmodLogoOffsetMultX, (halfScreenSizeY - (fmodLogoAdjustedSizeY / 2)) * fmodLogoOffsetMultY)
  125. GUI.SetNextWindowSize(fmodLogoAdjustedSizeX, fmodLogoAdjustedSizeY)
  126. GUI.Begin('FMOD LOGO', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoBackground, ImGuiWindowFlags.NoMouseInputs))
  127. GUI.Image(fmodLogoTexture, fmodLogoAdjustedSizeX, fmodLogoAdjustedSizeY)
  128. GUI.End()
  129. -- Push transparent background colors for all buttons
  130. GUI.PushStyleColor(ImGuiCol.Button, 0.0, 0.0, 0.0, 0.0);
  131. GUI.PushStyleColor(ImGuiCol.ButtonActive, 0.0, 0.0, 0.0, 0.0);
  132. GUI.PushStyleColor(ImGuiCol.ButtonHovered, 0.0, 0.0, 0.0, 0.0);
  133. -- Draw the EXIT button
  134. GUI.SetNextWindowPos(buttonPositionX - buttonHalfSizeX, buttonPositionY - buttonHalfSizeY)
  135. GUI.Begin('EXIT BTN', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoBackground))
  136. if exitButtonHover:isChecked() then
  137. GUI.ImageButton(buttonExitPressedTexture, exitButtonPressed)
  138. else
  139. GUI.ImageButton(buttonExitTexture, exitButtonPressed)
  140. end
  141. GUI.IsItemHovered(exitButtonHover)
  142. GUI.End()
  143. -- Draw the ABOUT button
  144. GUI.SetNextWindowPos(buttonPositionX - buttonHalfSizeX, buttonPositionY - buttonHalfSizeY - (buttonSpacing + buttonSizeY))
  145. GUI.Begin('ABOUT BTN', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoBackground))
  146. if aboutButtonHover:isChecked() then
  147. GUI.ImageButton(buttonAboutPressedTexture, aboutButtonPressed)
  148. else
  149. GUI.ImageButton(buttonAboutTexture, aboutButtonPressed)
  150. end
  151. GUI.IsItemHovered(aboutButtonHover)
  152. GUI.End()
  153. -- Draw the OPTIONS button
  154. GUI.SetNextWindowPos(buttonPositionX - buttonHalfSizeX, buttonPositionY - buttonHalfSizeY - ((buttonSpacing + buttonSizeY) * 2))
  155. GUI.Begin('OPTIONS BTN', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoBackground))
  156. if optionsButtonHover:isChecked() then
  157. GUI.ImageButton(buttonOptionsPressedTexture, optionsButtonPressed)
  158. else
  159. GUI.ImageButton(buttonOptionsTexture, optionsButtonPressed)
  160. end
  161. GUI.IsItemHovered(optionsButtonHover)
  162. GUI.End()
  163. -- Draw the EDIT button
  164. GUI.SetNextWindowPos(buttonPositionX - buttonHalfSizeX, buttonPositionY - buttonHalfSizeY - ((buttonSpacing + buttonSizeY) * 3))
  165. GUI.Begin('EDITOR BTN', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoBackground))
  166. if editorButtonHover:isChecked() then
  167. GUI.ImageButton(buttonEditorPressedTexture, editorButtonPressed)
  168. else
  169. GUI.ImageButton(buttonEditorTexture, editorButtonPressed)
  170. end
  171. GUI.IsItemHovered(editorButtonHover)
  172. GUI.End()
  173. -- Draw the PLAY button
  174. GUI.SetNextWindowPos(buttonPositionX - buttonHalfSizeX, buttonPositionY - buttonHalfSizeY - ((buttonSpacing + buttonSizeY) * 4))
  175. GUI.Begin('PLAY BTN', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoBackground))
  176. if playButtonHover:isChecked() then
  177. GUI.ImageButton(buttonPlayPressedTexture, playButtonPressed)
  178. else
  179. GUI.ImageButton(buttonPlayTexture, playButtonPressed)
  180. end
  181. GUI.IsItemHovered(playButtonHover)
  182. GUI.End()
  183. -- Draw the LOAD MAP button
  184. GUI.SetNextWindowPos(buttonPositionX - buttonHalfSizeX, buttonPositionY - buttonHalfSizeY - ((buttonSpacing + buttonSizeY) * 5))
  185. GUI.Begin('LOAD BTN', bitwiseOr(ImGuiWindowFlags.NoDecoration, ImGuiWindowFlags.NoMove, ImGuiWindowFlags.NoSavedSettings, ImGuiWindowFlags.NoBackground))
  186. if loadButtonHover:isChecked() then
  187. GUI.ImageButton(buttonLoadPressedTexture, loadButtonPressed)
  188. else
  189. GUI.ImageButton(buttonLoadTexture, loadButtonPressed)
  190. end
  191. GUI.IsItemHovered(loadButtonHover)
  192. GUI.End()
  193. -- Pop the transparent background colors for all buttons
  194. GUI.PopStyleColor(3)
  195. -- Pop the removal of frame padding and border size
  196. GUI.PopStyleVar(2)
  197. -- Check if the EXIT button is pressed; close the engine if it is
  198. if exitButtonPressed:isChecked() then
  199. ErrHandlerLoc.logErrorType(ErrorType.Info, 'Exit called from MainMenu.lua')
  200. setEngineRunning(false)
  201. end
  202. -- Check if the OPTIONS button is pressed
  203. if aboutButtonPressed:isChecked() then
  204. print('ABOUT')
  205. end
  206. -- Check if the OPTIONS button is pressed
  207. if optionsButtonPressed:isChecked() then
  208. print('OPTIONS')
  209. end
  210. -- Check if the EDITOR button is pressed; change the current engine state to EDITOR, if it is
  211. if editorButtonPressed:isChecked() then
  212. sendEngineChange(EngineChangeType.StateChange, EngineStateType.Editor)
  213. --setMouseCapture(true)
  214. end
  215. -- Check if the PLAY button is pressed; change the current engine state to PLAY, if it is
  216. if playButtonPressed:isChecked() then
  217. sendEngineChange(EngineChangeType.StateChange, EngineStateType.Play)
  218. setMouseCapture(true)
  219. end
  220. -- Check if the LOAD button is pressed
  221. if loadButtonPressed:isChecked() then
  222. if not fileBrowserLoadMapOpened then
  223. fileBrowserLoadMapOpened = true
  224. GUI.FileDialog(fileBrowserLoadMap)
  225. end
  226. end
  227. if fileBrowserLoadMap.m_closed then
  228. if fileBrowserLoadMap.m_success then
  229. infoText = 'Loading map: "' .. fileBrowserLoadMap.m_filename .. '"'
  230. ErrHandlerLoc.logErrorType(ErrorType.Info, infoText)
  231. sendEngineChange(EngineChangeType.SceneFilename, EngineStateType.Play, fileBrowserLoadMap.m_filename)
  232. sendEngineChange(EngineChangeType.StateChange, EngineStateType.Play)
  233. setMouseCapture(true)
  234. end
  235. fileBrowserLoadMapOpened = false
  236. fileBrowserLoadMap:reset()
  237. end
  238. end