Window_controls.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. --[[
  2. Basic window controls by hot-keys.
  3. Functions:
  4. - Go back to main menu
  5. - Toggle full-screen mode
  6. - Toggle mouse capture mode
  7. - Toggle v-sync mode
  8. ]]--
  9. function init ()
  10. -- Create needed variables
  11. create(Types.EngineVariables, 'engineVariables');
  12. create(Types.InputVariables, 'inputVariables');
  13. create(Types.WindowVariables, 'windowVariables');
  14. -- Create key commands, used to track pressed keys
  15. create(Types.KeyCommand, 'closeKey')
  16. create(Types.KeyCommand, 'fullscreenKey')
  17. create(Types.KeyCommand, 'mouseCaptureKey')
  18. create(Types.KeyCommand, 'vsyncKey')
  19. -- Bind keys to their corresponding buttons on the keyboard
  20. closeKey:bind(inputVariables.close_window_key)
  21. fullscreenKey:bind(inputVariables.fullscreen_key)
  22. mouseCaptureKey:bind(inputVariables.clip_mouse_key)
  23. vsyncKey:bind(inputVariables.vsync_key)
  24. -- Get current variables
  25. fullscreenBool = windowVariables.fullscreen
  26. mouseCaptureBool = windowVariables.mouse_captured
  27. vsyncBool = windowVariables.vertical_sync
  28. ErrHandlerLoc.logErrorCode(ErrorCode.Initialize_success, getLuaFilename())
  29. end
  30. function update (p_deltaTime)
  31. if closeKey:isActivated() then
  32. -- If the current engine state is PlayState, unload it and return to main menu
  33. if getEngineState() == EngineStateType.Play then
  34. sendEngineChange(EngineChangeType.StateChange, EngineStateType.MainMenu)
  35. sendEngineChange(EngineChangeType.SceneUnload, EngineStateType.Play)
  36. end
  37. end
  38. if fullscreenKey:isActivated() then
  39. -- Invert the corresponding bool
  40. fullscreenBool = not fullscreenBool
  41. -- Make the window fullscreen/windowed
  42. setFullscreen(fullscreenBool)
  43. -- Deactivate the button, so it's not triggered the next frame, unless pressed again
  44. fullscreenKey:deactivate()
  45. end
  46. if mouseCaptureKey:isActivated() then
  47. -- Invert the corresponding bool
  48. mouseCaptureBool = not mouseCaptureBool
  49. -- Capture/release the mouse
  50. setMouseCapture(mouseCaptureBool)
  51. -- Deactivate the button, so it's not triggered the next frame, unless pressed again
  52. mouseCaptureKey:deactivate()
  53. end
  54. if vsyncKey:isActivated() then
  55. -- Invert the corresponding bool
  56. vsyncBool = not vsyncBool
  57. -- Enable/disable vertical synchronization
  58. setVerticalSync(vsyncBool)
  59. -- Deactivate the button, so it's not triggered the next frame, unless pressed again
  60. vsyncKey:deactivate()
  61. end
  62. end