2
0

Window.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "Window.h"
  2. #include "App.h"
  3. #include "AppGLFW.h"
  4. #include "Input.h"
  5. #include "Monitor.h"
  6. namespace gameplay
  7. {
  8. Window::Window()
  9. {
  10. }
  11. Window::~Window()
  12. {
  13. if (handle->glfwWindow)
  14. {
  15. glfwDestroyWindow(handle->glfwWindow);
  16. }
  17. }
  18. void Window::set_title(const char* title)
  19. {
  20. glfwSetWindowTitle(handle->glfwWindow, title);
  21. }
  22. void Window::set_size(const Int2& windowSize)
  23. {
  24. glfwSetWindowSize(handle->glfwWindow, windowSize.x, windowSize.y);
  25. }
  26. Int2 Window::get_size() const
  27. {
  28. Int2 size;
  29. glfwGetWindowSize(handle->glfwWindow, &size.x, &size.y);
  30. return size;
  31. }
  32. void Window::set_pos(const Int2& pos)
  33. {
  34. glfwSetWindowPos(handle->glfwWindow, pos.x, pos.y);
  35. }
  36. Int2 Window::get_pos() const
  37. {
  38. Int2 pos;
  39. glfwGetWindowSize(handle->glfwWindow, &pos.x, &pos.y);
  40. return pos;
  41. }
  42. void Window::set_fullscreen(bool fullscreen)
  43. {
  44. if (handle->fullscreen == fullscreen)
  45. {
  46. return;
  47. }
  48. if (fullscreen)
  49. {
  50. glfwGetWindowPos(handle->glfwWindow, &handle->pos.x, &handle->pos.y);
  51. glfwGetWindowSize(handle->glfwWindow, &handle->size.x, &handle->size.y);
  52. Monitor* monitor = App::get_app()->get_monitor(this);
  53. const GLFWvidmode* mode = glfwGetVideoMode(monitor->handle->glfwMonitor);
  54. glfwSetWindowMonitor(handle->glfwWindow, monitor->handle->glfwMonitor, 0, 0, mode->width, mode->height, mode->refreshRate);
  55. glfwSetWindowSize(handle->glfwWindow, mode->width, mode->height);
  56. handle->fullscreen = true;
  57. }
  58. else
  59. {
  60. glfwSetWindowMonitor(handle->glfwWindow, nullptr, handle->pos.x, handle->pos.y, handle->size.x, handle->size.y, GLFW_DONT_CARE);
  61. glfwSetWindowSize(handle->glfwWindow, handle->size.x, handle->size.y);
  62. handle->fullscreen = false;
  63. }
  64. }
  65. bool Window::is_fullscreen() const
  66. {
  67. return handle->fullscreen;
  68. }
  69. void Window::get_frame_size(int* left, int* top, int* right, int* bottom) const
  70. {
  71. glfwGetWindowFrameSize(handle->glfwWindow, left, top, right, bottom);
  72. }
  73. void Window::set_opacity(float opacity)
  74. {
  75. glfwSetWindowOpacity(handle->glfwWindow, opacity);
  76. }
  77. float Window::get_opacity() const
  78. {
  79. return glfwGetWindowOpacity(handle->glfwWindow);
  80. }
  81. void Window::set_icon(const Pixmap* images, size_t imageCount)
  82. {
  83. glfwSetWindowIcon(handle->glfwWindow, (int)imageCount, (const GLFWimage*)images);
  84. }
  85. bool Window::should_close()
  86. {
  87. return glfwWindowShouldClose(handle->glfwWindow);
  88. }
  89. void Window::close()
  90. {
  91. glfwWindowShouldClose(handle->glfwWindow);
  92. }
  93. void Window::show()
  94. {
  95. glfwShowWindow(handle->glfwWindow);
  96. glfwFocusWindow(handle->glfwWindow);
  97. }
  98. void Window::hide()
  99. {
  100. glfwHideWindow(handle->glfwWindow);
  101. }
  102. bool Window::is_visible() const
  103. {
  104. return glfwGetWindowAttrib(handle->glfwWindow, GLFW_VISIBLE) == GLFW_TRUE;
  105. }
  106. void Window::focus()
  107. {
  108. glfwFocusWindow(handle->glfwWindow);
  109. }
  110. bool Window::is_focused() const
  111. {
  112. return glfwGetWindowAttrib(handle->glfwWindow, GLFW_FOCUSED) == GLFW_TRUE;
  113. }
  114. void Window::minimize()
  115. {
  116. glfwIconifyWindow(handle->glfwWindow);
  117. }
  118. bool Window::is_minimized() const
  119. {
  120. return glfwGetWindowAttrib(handle->glfwWindow, GLFW_ICONIFIED) == GLFW_TRUE;
  121. }
  122. void Window::maximize()
  123. {
  124. glfwMaximizeWindow(handle->glfwWindow);
  125. }
  126. bool Window::is_maximized() const
  127. {
  128. return glfwGetWindowAttrib(handle->glfwWindow, GLFW_MAXIMIZED) == GLFW_TRUE;
  129. }
  130. void Window::restore()
  131. {
  132. glfwRestoreWindow(handle->glfwWindow);
  133. }
  134. Float2 Window::get_content_scale() const
  135. {
  136. Float2 scale;
  137. glfwGetWindowContentScale(handle->glfwWindow, &scale.x, &scale.y);
  138. return scale;
  139. }
  140. void Window::set_cursor(Cursor* cursor)
  141. {
  142. glfwSetCursor(handle->glfwWindow, cursor->glfwCursor);
  143. }
  144. void Window::set_cursor_pos(const Double2& pos)
  145. {
  146. glfwSetCursorPos(handle->glfwWindow, pos.x, pos.y);
  147. }
  148. Double2 Window::get_cursor_pos() const
  149. {
  150. Double2 pos;
  151. glfwGetCursorPos(handle->glfwWindow, &pos.x, &pos.y);
  152. return pos;
  153. }
  154. void Window::set_cursor_mode(CursorMode mode)
  155. {
  156. int32_t glfwValue = 0;
  157. switch (mode)
  158. {
  159. case CursorMode::NORMAL:
  160. glfwValue = GLFW_CURSOR_NORMAL;
  161. break;
  162. case CursorMode::HIDDEN:
  163. glfwValue = GLFW_CURSOR_HIDDEN;
  164. break;
  165. case CursorMode::DISABLED:
  166. glfwValue = GLFW_CURSOR_DISABLED;
  167. break;
  168. }
  169. glfwSetInputMode(handle->glfwWindow, GLFW_CURSOR, glfwValue);
  170. }
  171. CursorMode Window::get_cursor_mode() const
  172. {
  173. CursorMode mode = CursorMode::NORMAL;
  174. int32_t glfwModeValue = glfwGetInputMode(handle->glfwWindow, GLFW_CURSOR);
  175. switch (glfwModeValue)
  176. {
  177. case GLFW_CURSOR_NORMAL:
  178. mode = CursorMode::NORMAL;
  179. break;
  180. case GLFW_CURSOR_HIDDEN:
  181. mode = CursorMode::HIDDEN;
  182. break;
  183. case GLFW_CURSOR_DISABLED:
  184. mode = CursorMode::DISABLED;
  185. break;
  186. }
  187. return mode;
  188. }
  189. void Window::set_input_mode_enabled(gameplay::InputMode mode, bool enabled)
  190. {
  191. int32_t setMode = 0;
  192. switch (mode)
  193. {
  194. case gameplay::InputMode::STICKY_KEYS:
  195. setMode = GLFW_STICKY_KEYS;
  196. break;
  197. case gameplay::InputMode::STICKY_MOUSE_BUTTONS:
  198. setMode = GLFW_STICKY_MOUSE_BUTTONS;
  199. break;
  200. case gameplay::InputMode::LOCK_KEY_MODS:
  201. setMode = GLFW_LOCK_KEY_MODS;
  202. break;
  203. case gameplay::InputMode::RAW_MOUSE_MOTION:
  204. setMode = GLFW_RAW_MOUSE_MOTION;
  205. break;
  206. default:
  207. GP_ASSERT(false);
  208. }
  209. glfwSetInputMode(handle->glfwWindow, setMode, enabled);
  210. }
  211. bool Window::is_input_mode_enabled(InputMode mode) const
  212. {
  213. int32_t setMode = 0;
  214. switch (mode)
  215. {
  216. case gameplay::InputMode::STICKY_KEYS:
  217. setMode = GLFW_STICKY_KEYS;
  218. break;
  219. case gameplay::InputMode::STICKY_MOUSE_BUTTONS:
  220. setMode = GLFW_STICKY_MOUSE_BUTTONS;
  221. break;
  222. case gameplay::InputMode::LOCK_KEY_MODS:
  223. setMode = GLFW_LOCK_KEY_MODS;
  224. break;
  225. case gameplay::InputMode::RAW_MOUSE_MOTION:
  226. setMode = GLFW_RAW_MOUSE_MOTION;
  227. break;
  228. default:
  229. GP_ASSERT(false);
  230. }
  231. return glfwGetInputMode(handle->glfwWindow, setMode) == GLFW_TRUE;
  232. }
  233. ButtonAction Window::get_mouse_button_action(MouseButton button)
  234. {
  235. int glfwAction = glfwGetMouseButton(handle->glfwWindow, GLFWUtils::to_glfw_button(button));
  236. switch(glfwAction)
  237. {
  238. case GLFW_PRESS:
  239. return ButtonAction::PRESS;
  240. case GLFW_RELEASE:
  241. return ButtonAction::RELEASE;
  242. }
  243. return ButtonAction::RELEASE;
  244. }
  245. KeyAction Window::get_key_action(Key key)
  246. {
  247. int glfwAction = glfwGetKey(handle->glfwWindow, GLFWUtils::to_glfw_key(key));
  248. switch(glfwAction)
  249. {
  250. case GLFW_PRESS:
  251. return KeyAction::PRESS;
  252. case GLFW_RELEASE:
  253. return KeyAction::RELEASE;
  254. case GLFW_REPEAT:
  255. return KeyAction::REPEAT;
  256. }
  257. return KeyAction::RELEASE;
  258. }
  259. void Window::set_user_ptr(void* userPtr)
  260. {
  261. handle->userPtr = userPtr;
  262. }
  263. void* Window::get_user_ptr() const
  264. {
  265. return handle->userPtr;
  266. }
  267. }