window.d 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. module three.window;
  2. import three.common;
  3. struct Window {
  4. private:
  5. GLFWwindow* _glfwWindow = null;
  6. uint _x, _y, _width, _height;
  7. KeyAction[int] _keyStates;
  8. ButtonAction[int] _buttonStates;
  9. public:
  10. void construct(string title, uint width, uint height) nothrow {
  11. this._x = 0;
  12. this._y = 0;
  13. this._width = width;
  14. this._height = height;
  15. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  16. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  17. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
  18. glfwDefaultWindowHints();
  19. glfwWindowHint(GLFW_RED_BITS, 8);
  20. glfwWindowHint(GLFW_GREEN_BITS, 8);
  21. glfwWindowHint(GLFW_BLUE_BITS, 8);
  22. glfwWindowHint(GLFW_ALPHA_BITS, 0);
  23. glfwWindowHint(GLFW_DEPTH_BITS, 24);
  24. glfwWindowHint(GLFW_STENCIL_BITS, 8);
  25. import std.string : toStringz;
  26. this._glfwWindow = glfwCreateWindow(width, height, title.toStringz(), null, null);
  27. assert(this._glfwWindow !is null);
  28. glfwSetWindowUserPointer(this._glfwWindow, cast(void*)&this);
  29. glfwSetWindowPosCallback(this._glfwWindow, cast(GLFWwindowposfun)&_GLFWwindowposfun);
  30. glfwSetWindowSizeCallback(this._glfwWindow, cast(GLFWwindowsizefun)&_GLFWwindowsizefun);
  31. glfwSetWindowCloseCallback(this._glfwWindow, cast(GLFWwindowclosefun)&_GLFWwindowclosefun);
  32. glfwSetWindowRefreshCallback(this._glfwWindow, cast(GLFWwindowrefreshfun)&_GLFWwindowrefreshfun);
  33. glfwSetWindowIconifyCallback(this._glfwWindow, cast(GLFWwindowiconifyfun)&_GLFWwindowiconifyfun);
  34. glfwSetMouseButtonCallback(this._glfwWindow, cast(GLFWmousebuttonfun)&_GLFWmousebuttonfun);
  35. glfwSetCursorPosCallback(this._glfwWindow, cast(GLFWcursorposfun)&_GLFWcursorposfun);
  36. //glfwSetCursorEnterCallback(this._glfwWindow, cast(GLFWcursorenterfunfun)&_GLFWcursorenterfunfun);
  37. glfwSetScrollCallback(this._glfwWindow, cast(GLFWscrollfun)&_GLFWscrollfun);
  38. glfwSetKeyCallback(this._glfwWindow, cast(GLFWkeyfun)&_GLFWkeyfun);
  39. glfwSetCharCallback(this._glfwWindow, cast(GLFWcharfun)&_GLFWcharfun);
  40. glfwMakeContextCurrent(this._glfwWindow);
  41. }
  42. void destruct() {
  43. // glfwSetWindowPosCallback(this._glfwWindow, null);
  44. // glfwSetWindowSizeCallback(this._glfwWindow, null);
  45. // glfwSetWindowCloseCallback(this._glfwWindow, null);
  46. // glfwSetWindowRefreshCallback(this._glfwWindow, null);
  47. // glfwSetWindowIconifyCallback(this._glfwWindow, null);
  48. // glfwSetMouseButtonCallback(this._glfwWindow, null);
  49. // glfwSetCursorPosCallback(this._glfwWindow, null);
  50. // //glfwSetCursorEnterCallback(this._glfwWindow, null);
  51. // glfwSetScrollCallback(this._glfwWindow, null);
  52. // glfwSetKeyCallback(this._glfwWindow, null);
  53. // glfwSetCharCallback(this._glfwWindow, null);
  54. glfwDestroyWindow(this._glfwWindow);
  55. }
  56. void makeAktiveRenderWindow() nothrow @nogc {
  57. glfwMakeContextCurrent(this._glfwWindow);
  58. }
  59. void swapBuffers() nothrow @nogc {
  60. glfwSwapBuffers(this._glfwWindow);
  61. }
  62. void pollEvents() nothrow @nogc {
  63. glfwPollEvents();
  64. }
  65. @property void setTitle(string title) nothrow {
  66. import std.string : toStringz;
  67. glfwSetWindowTitle(this._glfwWindow, title.toStringz());
  68. }
  69. KeyAction keyState(int key) pure @safe nothrow {
  70. try {
  71. return this._keyStates.get(key, KeyAction.Released);
  72. }
  73. catch(Exception) {
  74. return KeyAction.Released;
  75. }
  76. }
  77. ButtonAction buttonState(int button) pure @safe nothrow {
  78. try {
  79. return this._buttonStates.get(button, ButtonAction.Released);
  80. }
  81. catch(Exception) {
  82. return ButtonAction.Released;
  83. }
  84. }
  85. uint x() {
  86. return this._x;
  87. }
  88. uint y() {
  89. return this._y;
  90. }
  91. uint width() {
  92. return this._width;
  93. }
  94. uint height() {
  95. return this._height;
  96. }
  97. public:
  98. void delegate(ref Window, int, int) onPosition;
  99. void delegate(ref Window, int, int) onSize;
  100. void delegate(ref Window) onClose;
  101. void delegate(ref Window) onRefresh;
  102. void delegate(ref Window, FocusAction) onFocus;
  103. void delegate(ref Window, IconifyAction) onIconify;
  104. void delegate(ref Window, bool) onCursorEnter;
  105. void delegate(ref Window, int, ButtonAction) onButton;
  106. void delegate(ref Window, double, double) onCursorPos;
  107. void delegate(ref Window, double, double) onScroll;
  108. void delegate(ref Window, Key, ScanCode, KeyAction, KeyMod) onKey;
  109. void delegate(ref Window, int) onChar;
  110. }
  111. alias ScanCode = int;
  112. enum IconifyAction {
  113. Iconified,
  114. Restored
  115. }
  116. enum FocusAction {
  117. Focused,
  118. Defocused
  119. }
  120. enum CursorAction {
  121. Entered,
  122. Leaved
  123. }
  124. enum ButtonAction {
  125. Pressed,
  126. Released
  127. }
  128. enum KeyAction {
  129. Pressed,
  130. Released,
  131. Repeated
  132. }
  133. enum KeyMod {
  134. Shift = 0x0001,
  135. Control = 0x0002,
  136. Alt = 0x0004,
  137. Super = 0x0008,
  138. }
  139. enum Key {
  140. Unknown = -1,
  141. Space = 32,
  142. Apostrophe = 39,
  143. Comma = 44,
  144. Minus = 45,
  145. Period = 46,
  146. Slash = 47,
  147. Key0 = 48,
  148. Key1 = 49,
  149. Key2 = 50,
  150. Key3 = 51,
  151. Key4 = 52,
  152. Key5 = 53,
  153. Key6 = 54,
  154. Key7 = 55,
  155. Key8 = 56,
  156. Key9 = 57,
  157. Semicolon = 59,
  158. Equal = 61,
  159. KeyA = 65,
  160. KeyB = 66,
  161. KeyC = 67,
  162. KeyD = 68,
  163. KeyE = 69,
  164. KeyF = 70,
  165. KeyG = 71,
  166. KeyH = 72,
  167. KeyI = 73,
  168. KeyJ = 74,
  169. KeyK = 75,
  170. Keyl = 76,
  171. KeyM = 77,
  172. KeyN = 78,
  173. KeyO = 79,
  174. KeyP = 80,
  175. KeyQ = 81,
  176. KeyR = 82,
  177. KeyS = 83,
  178. KeyT = 84,
  179. KeyU = 85,
  180. KeyV = 86,
  181. KeyW = 87,
  182. KeyX = 88,
  183. KeyY = 89,
  184. KeyZ = 90,
  185. LeftBracket = 91,
  186. Backslash = 92,
  187. RightBracket = 93,
  188. GraveAccent = 96,
  189. World1 = 161,
  190. World2 = 162,
  191. Escape = 256,
  192. Enter = 257,
  193. Tab = 258,
  194. Backspace = 259,
  195. Insert = 260,
  196. Delete = 261,
  197. Right = 262,
  198. Left = 263,
  199. Down = 264,
  200. Up = 265,
  201. PageUp = 266,
  202. PageDown = 267,
  203. Home = 268,
  204. End = 269,
  205. CapsLock = 280,
  206. ScrollLock = 281,
  207. NumLock = 282,
  208. PrintScreen = 283,
  209. Pause = 284,
  210. F1 = 290,
  211. F2 = 291,
  212. F3 = 292,
  213. F4 = 293,
  214. F5 = 294,
  215. F6 = 295,
  216. F7 = 296,
  217. F8 = 297,
  218. F9 = 298,
  219. F10 = 299,
  220. F11 = 300,
  221. F12 = 301,
  222. F13 = 302,
  223. F14 = 303,
  224. F15 = 304,
  225. F16 = 305,
  226. F17 = 306,
  227. F18 = 307,
  228. F19 = 308,
  229. F20 = 309,
  230. F21 = 310,
  231. F22 = 311,
  232. F23 = 312,
  233. F24 = 313,
  234. F25 = 314,
  235. NumBlock0 = 320,
  236. NumBlock1 = 321,
  237. NumBlock2 = 322,
  238. NumBlock3 = 323,
  239. NumBlock4 = 324,
  240. NumBlock5 = 325,
  241. NumBlock6 = 326,
  242. NumBlock7 = 327,
  243. NumBlock8 = 328,
  244. NumBlock9 = 329,
  245. KpDecimal = 330,
  246. KpDivide = 331,
  247. KpMultiply = 332,
  248. KpSubtract = 333,
  249. KpAdd = 334,
  250. KpEnter = 335,
  251. KpEqual = 336,
  252. LeftShift = 340,
  253. LeftControl = 341,
  254. LeftAlt = 342,
  255. LeftSuper = 343,
  256. RightShift = 344,
  257. RightControl = 345,
  258. RightAlt = 346,
  259. RightSuper = 347,
  260. Menu = 348
  261. }
  262. private Window* _castWindow(GLFWwindow* window)
  263. out (result) { assert(result !is null, "glfwGetWindowUserPointer returned null"); }
  264. body {
  265. void* user_ptr = glfwGetWindowUserPointer(window);
  266. return cast(Window*)user_ptr;
  267. }
  268. private extern(C) void _GLFWwindowposfun(GLFWwindow* glfwWindow, int x, int y) {
  269. Window* window = _castWindow(glfwWindow);
  270. auto monitor = glfwGetPrimaryMonitor();
  271. if(monitor is null) return;
  272. auto videoMode = glfwGetVideoMode(monitor);
  273. y = videoMode.height - y;
  274. int w,h;
  275. glfwGetWindowSize(glfwWindow, &w, &h);
  276. window._x = x;
  277. window._y = y;
  278. window._width = w;
  279. window._height = h;
  280. if(window.onPosition) window.onPosition(*window, x, y);
  281. }
  282. private extern(C) void _GLFWwindowsizefun(GLFWwindow* glfwWindow, int width, int height) {
  283. Window* window = _castWindow(glfwWindow);
  284. window._width = width;
  285. window._height = height;
  286. if(window.onSize) window.onSize(*window, width, height);
  287. }
  288. private extern(C) void _GLFWwindowclosefun(GLFWwindow* glfwWindow) {
  289. Window* window = _castWindow(glfwWindow);
  290. if(window.onClose) window.onClose(*window);
  291. }
  292. private extern(C) void _GLFWwindowrefreshfun(GLFWwindow* glfwWindow) {
  293. Window* window = _castWindow(glfwWindow);
  294. if(window.onRefresh) window.onRefresh(*window);
  295. }
  296. private extern(C) void _GLFWwindowfocusfun(GLFWwindow* glfwWindow, int focused) {
  297. Window* window = _castWindow(glfwWindow);
  298. if(window.onFocus) window.onFocus(*window, (focused == GL_TRUE) ? FocusAction.Focused : FocusAction.Defocused);
  299. }
  300. private extern(C) void _GLFWwindowiconifyfun(GLFWwindow* glfwWindow, int iconified) {
  301. Window* window = _castWindow(glfwWindow);
  302. if(window.onIconify) window.onIconify(*window, (iconified == GL_TRUE) ? IconifyAction.Iconified : IconifyAction.Restored);
  303. }
  304. private extern(C) void _GLFWcursorenterfun(GLFWwindow* glfwWindow, int entered) {
  305. Window* window = _castWindow(glfwWindow);
  306. if(window.onCursorEnter) window.onCursorEnter(*window, (entered == GL_TRUE) ? CursorAction.Entered : CursorAction.Leaved);
  307. }
  308. private extern(C) void _GLFWmousebuttonfun(GLFWwindow* glfwWindow, int button, int action) {
  309. Window* window = _castWindow(glfwWindow);
  310. window._buttonStates[button] = (action == GLFW_PRESS) ? ButtonAction.Pressed : ButtonAction.Released;
  311. if(window.onButton) window.onButton(*window, button, (action == GLFW_PRESS) ? ButtonAction.Pressed : ButtonAction.Released);
  312. }
  313. private extern(C) void _GLFWcursorposfun(GLFWwindow* glfwWindow, double x, double y) {
  314. Window* window = _castWindow(glfwWindow);
  315. if(window.onCursorPos) window.onCursorPos(*window, x, window._height - y);
  316. }
  317. private extern(C) void _GLFWscrollfun(GLFWwindow* glfwWindow, double x, double y) {
  318. Window* window = _castWindow(glfwWindow);
  319. if(window.onScroll) window.onScroll(*window, x, y);
  320. }
  321. private extern(C) void _GLFWkeyfun(GLFWwindow* glfwWindow, int key, int scancode, int action, int mods) {
  322. Window* window = _castWindow(glfwWindow);
  323. window._keyStates[key] = (action == GLFW_PRESS || action == GLFW_REPEAT) ? KeyAction.Pressed : KeyAction.Released;
  324. if(window.onKey) window.onKey(*window, cast(Key)key, cast(ScanCode)scancode, cast(KeyAction)action, cast(KeyMod)mods);
  325. }
  326. private extern(C) void _GLFWcharfun(GLFWwindow* glfwWindow, uint character) {
  327. Window* window = _castWindow(glfwWindow);
  328. if(window.onChar) window.onChar(*window, character);
  329. }