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