glfw.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package window
  5. import (
  6. "github.com/g3n/engine/core"
  7. "github.com/go-gl/glfw/v3.2/glfw"
  8. )
  9. type GLFW struct {
  10. core.Dispatcher
  11. win *glfw.Window
  12. keyEv KeyEvent
  13. charEv CharEvent
  14. mouseEv MouseEvent
  15. posEv PosEvent
  16. sizeEv SizeEvent
  17. cursorEv CursorEvent
  18. scrollEv ScrollEvent
  19. arrowCursor *glfw.Cursor
  20. ibeamCursor *glfw.Cursor
  21. crosshairCursor *glfw.Cursor
  22. handCursor *glfw.Cursor
  23. hresizeCursor *glfw.Cursor
  24. vresizeCursor *glfw.Cursor
  25. fullScreen bool
  26. lastX int
  27. lastY int
  28. lastWidth int
  29. lastHeight int
  30. }
  31. // Global GLFW initialization flag
  32. // is initialized when the first window is created
  33. var initialized bool = false
  34. func newGLFW(width, height int, title string, full bool) (*GLFW, error) {
  35. // Initialize GLFW once before the first window is created
  36. if !initialized {
  37. err := glfw.Init()
  38. if err != nil {
  39. return nil, err
  40. }
  41. // Sets window hints
  42. glfw.WindowHint(glfw.ContextVersionMajor, 3)
  43. glfw.WindowHint(glfw.ContextVersionMinor, 3)
  44. glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
  45. glfw.WindowHint(glfw.Samples, 8)
  46. initialized = true
  47. }
  48. // Creates window and sets it as the current context.
  49. // The window is created always as not full screen because if it is
  50. // created as full screen it not possible to revert it to windowed mode.
  51. // At the end of this function, the window will be set to full screen if requested.
  52. win, err := glfw.CreateWindow(width, height, title, nil, nil)
  53. if err != nil {
  54. return nil, err
  55. }
  56. win.MakeContextCurrent()
  57. // Create wrapper window with dispacher
  58. w := new(GLFW)
  59. w.win = win
  60. w.Dispatcher.Initialize()
  61. // Set key callback to dispatch event
  62. win.SetKeyCallback(func(x *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
  63. w.keyEv.W = w
  64. w.keyEv.Keycode = Key(key)
  65. w.keyEv.Scancode = scancode
  66. w.keyEv.Action = Action(action)
  67. w.keyEv.Mods = ModifierKey(mods)
  68. if action == glfw.Press {
  69. w.Dispatch(OnKeyDown, &w.keyEv)
  70. return
  71. }
  72. if action == glfw.Release {
  73. w.Dispatch(OnKeyUp, &w.keyEv)
  74. return
  75. }
  76. if action == glfw.Repeat {
  77. w.Dispatch(OnKeyRepeat, &w.keyEv)
  78. return
  79. }
  80. })
  81. // Set char callback
  82. win.SetCharModsCallback(func(x *glfw.Window, char rune, mods glfw.ModifierKey) {
  83. w.charEv.W = w
  84. w.charEv.Char = char
  85. w.charEv.Mods = ModifierKey(mods)
  86. w.Dispatch(OnChar, &w.charEv)
  87. })
  88. // Set mouse button callback to dispatch event
  89. win.SetMouseButtonCallback(func(x *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
  90. xpos, ypos := x.GetCursorPos()
  91. w.mouseEv.W = w
  92. w.mouseEv.Button = MouseButton(button)
  93. w.mouseEv.Action = Action(action)
  94. w.mouseEv.Mods = ModifierKey(mods)
  95. w.mouseEv.Xpos = float32(xpos)
  96. w.mouseEv.Ypos = float32(ypos)
  97. if action == glfw.Press {
  98. w.Dispatch(OnMouseDown, &w.mouseEv)
  99. return
  100. }
  101. if action == glfw.Release {
  102. w.Dispatch(OnMouseUp, &w.mouseEv)
  103. return
  104. }
  105. })
  106. // Set window size callback to dispatch event
  107. win.SetSizeCallback(func(x *glfw.Window, width int, height int) {
  108. w.sizeEv.W = w
  109. w.sizeEv.Width = width
  110. w.sizeEv.Height = height
  111. w.Dispatch(OnWindowSize, &w.sizeEv)
  112. })
  113. // Set window position event callback to dispatch event
  114. win.SetPosCallback(func(x *glfw.Window, xpos int, ypos int) {
  115. w.posEv.W = w
  116. w.posEv.Xpos = xpos
  117. w.posEv.Ypos = ypos
  118. w.Dispatch(OnWindowPos, &w.posEv)
  119. })
  120. // Set window cursor position event callback to dispatch event
  121. win.SetCursorPosCallback(func(x *glfw.Window, xpos float64, ypos float64) {
  122. w.cursorEv.W = w
  123. w.cursorEv.Xpos = float32(xpos)
  124. w.cursorEv.Ypos = float32(ypos)
  125. w.Dispatch(OnCursor, &w.cursorEv)
  126. })
  127. // Set mouse wheel scroll event callback to dispatch event
  128. win.SetScrollCallback(func(x *glfw.Window, xoff float64, yoff float64) {
  129. w.scrollEv.W = w
  130. w.scrollEv.Xoffset = float32(xoff)
  131. w.scrollEv.Yoffset = float32(yoff)
  132. w.Dispatch(OnScroll, &w.scrollEv)
  133. })
  134. // Preallocate standard cursors
  135. w.arrowCursor = glfw.CreateStandardCursor(glfw.ArrowCursor)
  136. w.ibeamCursor = glfw.CreateStandardCursor(glfw.IBeamCursor)
  137. w.crosshairCursor = glfw.CreateStandardCursor(glfw.CrosshairCursor)
  138. w.handCursor = glfw.CreateStandardCursor(glfw.HandCursor)
  139. w.hresizeCursor = glfw.CreateStandardCursor(glfw.HResizeCursor)
  140. w.vresizeCursor = glfw.CreateStandardCursor(glfw.VResizeCursor)
  141. // Sets full screen if requested
  142. if full {
  143. w.SetFullScreen(true)
  144. }
  145. return w, nil
  146. }
  147. // GetScreenResolution returns the resolution of the primary screen in pixels.
  148. // The parameter is currently ignored
  149. func (w *GLFW) GetScreenResolution(p interface{}) (width, height int) {
  150. mon := glfw.GetPrimaryMonitor()
  151. vmode := mon.GetVideoMode()
  152. return vmode.Width, vmode.Height
  153. }
  154. func (w *GLFW) SwapInterval(interval int) {
  155. glfw.SwapInterval(interval)
  156. }
  157. func (w *GLFW) MakeContextCurrent() {
  158. w.win.MakeContextCurrent()
  159. }
  160. func (w *GLFW) GetSize() (width int, height int) {
  161. return w.win.GetSize()
  162. }
  163. func (w *GLFW) SetSize(width int, height int) {
  164. w.win.SetSize(width, height)
  165. }
  166. func (w *GLFW) GetPos() (xpos, ypos int) {
  167. return w.win.GetPos()
  168. }
  169. func (w *GLFW) SetPos(xpos, ypos int) {
  170. w.win.SetPos(xpos, ypos)
  171. }
  172. func (w *GLFW) SetTitle(title string) {
  173. w.win.SetTitle(title)
  174. }
  175. func (w *GLFW) SetStandardCursor(cursor StandardCursor) {
  176. switch cursor {
  177. case ArrowCursor:
  178. w.win.SetCursor(w.arrowCursor)
  179. case IBeamCursor:
  180. w.win.SetCursor(w.ibeamCursor)
  181. case CrosshairCursor:
  182. w.win.SetCursor(w.crosshairCursor)
  183. case HandCursor:
  184. w.win.SetCursor(w.handCursor)
  185. case HResizeCursor:
  186. w.win.SetCursor(w.hresizeCursor)
  187. case VResizeCursor:
  188. w.win.SetCursor(w.vresizeCursor)
  189. default:
  190. panic("Invalid cursor")
  191. }
  192. }
  193. // FullScreen returns this window full screen state for the primary monitor
  194. func (w *GLFW) FullScreen() bool {
  195. return w.fullScreen
  196. }
  197. // SetFullScreen sets this window full screen state for the primary monitor
  198. func (w *GLFW) SetFullScreen(full bool) {
  199. // If already in the desired state, nothing to do
  200. if w.fullScreen == full {
  201. return
  202. }
  203. // Sets this window full screen for the primary monitor
  204. if full {
  205. // Get primary monitor
  206. mon := glfw.GetPrimaryMonitor()
  207. vmode := mon.GetVideoMode()
  208. width := vmode.Width
  209. height := vmode.Height
  210. // Saves current position and size of the window
  211. w.lastX, w.lastY = w.win.GetPos()
  212. w.lastWidth, w.lastHeight = w.win.GetSize()
  213. // Sets monitor for full screen
  214. w.win.SetMonitor(mon, 0, 0, width, height, vmode.RefreshRate)
  215. w.fullScreen = true
  216. } else {
  217. // Restore window to previous position and size
  218. w.win.SetMonitor(nil, w.lastX, w.lastY, w.lastWidth, w.lastHeight, glfw.DontCare)
  219. w.fullScreen = false
  220. }
  221. }
  222. // ShouldClose returns the current state of this window should close flag
  223. func (w *GLFW) ShouldClose() bool {
  224. return w.win.ShouldClose()
  225. }
  226. // SetShouldClose sets the state of this windows should close flag
  227. func (w *GLFW) SetShouldClose(v bool) {
  228. w.win.SetShouldClose(v)
  229. }
  230. func (w *GLFW) SwapBuffers() {
  231. w.win.SwapBuffers()
  232. }
  233. func (w *GLFW) Destroy() {
  234. w.win.Destroy()
  235. w.win = nil
  236. }
  237. func (w *GLFW) PollEvents() {
  238. glfw.PollEvents()
  239. }
  240. func (w *GLFW) GetTime() float64 {
  241. return glfw.GetTime()
  242. }