window.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. /*
  5. Package window abstracts the OpenGL Window manager
  6. Currently only glfw is supported
  7. */
  8. package window
  9. import (
  10. "github.com/g3n/engine/core"
  11. "github.com/go-gl/glfw/v3.2/glfw"
  12. )
  13. //
  14. // Interface for all window managers
  15. //
  16. type IWindow interface {
  17. core.IDispatcher
  18. SwapInterval(interval int)
  19. MakeContextCurrent()
  20. GetSize() (width int, height int)
  21. SetSize(width int, height int)
  22. GetPos() (xpos, ypos int)
  23. SetPos(xpos, ypos int)
  24. SetTitle(title string)
  25. SetStandardCursor(cursor StandardCursor)
  26. SwapBuffers()
  27. ShouldClose() bool
  28. SetShouldClose(bool)
  29. PollEvents()
  30. GetTime() float64
  31. }
  32. // Key corresponds to a keyboard key.
  33. type Key int
  34. //
  35. // Keycodes (from glfw)
  36. //
  37. const (
  38. KeyUnknown = Key(glfw.KeyUnknown)
  39. KeySpace = Key(glfw.KeySpace)
  40. KeyApostrophe = Key(glfw.KeyApostrophe)
  41. KeyComma = Key(glfw.KeyComma)
  42. KeyMinus = Key(glfw.KeyMinus)
  43. KeyPeriod = Key(glfw.KeyPeriod)
  44. KeySlash = Key(glfw.KeySlash)
  45. Key0 = Key(glfw.Key0)
  46. Key1 = Key(glfw.Key1)
  47. Key2 = Key(glfw.Key2)
  48. Key3 = Key(glfw.Key3)
  49. Key4 = Key(glfw.Key4)
  50. Key5 = Key(glfw.Key5)
  51. Key6 = Key(glfw.Key6)
  52. Key7 = Key(glfw.Key7)
  53. Key8 = Key(glfw.Key8)
  54. Key9 = Key(glfw.Key8)
  55. KeySemicolon = Key(glfw.KeySemicolon)
  56. KeyEqual = Key(glfw.KeyEqual)
  57. KeyA = Key(glfw.KeyA)
  58. KeyB = Key(glfw.KeyB)
  59. KeyC = Key(glfw.KeyC)
  60. KeyD = Key(glfw.KeyD)
  61. KeyE = Key(glfw.KeyE)
  62. KeyF = Key(glfw.KeyF)
  63. KeyG = Key(glfw.KeyG)
  64. KeyH = Key(glfw.KeyH)
  65. KeyI = Key(glfw.KeyI)
  66. KeyJ = Key(glfw.KeyJ)
  67. KeyK = Key(glfw.KeyK)
  68. KeyL = Key(glfw.KeyL)
  69. KeyM = Key(glfw.KeyM)
  70. KeyN = Key(glfw.KeyN)
  71. KeyO = Key(glfw.KeyO)
  72. KeyP = Key(glfw.KeyP)
  73. KeyQ = Key(glfw.KeyQ)
  74. KeyR = Key(glfw.KeyR)
  75. KeyS = Key(glfw.KeyS)
  76. KeyT = Key(glfw.KeyT)
  77. KeyU = Key(glfw.KeyU)
  78. KeyV = Key(glfw.KeyV)
  79. KeyW = Key(glfw.KeyW)
  80. KeyX = Key(glfw.KeyX)
  81. KeyY = Key(glfw.KeyY)
  82. KeyZ = Key(glfw.KeyZ)
  83. KeyLeftBracket = Key(glfw.KeyLeftBracket)
  84. KeyBackslash = Key(glfw.KeyBackslash)
  85. KeyRightBracket = Key(glfw.KeyRightBracket)
  86. KeyGraveAccent = Key(glfw.KeyGraveAccent)
  87. KeyWorld1 = Key(glfw.KeyWorld1)
  88. KeyWorld2 = Key(glfw.KeyWorld2)
  89. KeyEscape = Key(glfw.KeyEscape)
  90. KeyEnter = Key(glfw.KeyEnter)
  91. KeyTab = Key(glfw.KeyTab)
  92. KeyBackspace = Key(glfw.KeyBackspace)
  93. KeyInsert = Key(glfw.KeyInsert)
  94. KeyDelete = Key(glfw.KeyDelete)
  95. KeyRight = Key(glfw.KeyRight)
  96. KeyLeft = Key(glfw.KeyLeft)
  97. KeyDown = Key(glfw.KeyDown)
  98. KeyUp = Key(glfw.KeyUp)
  99. KeyPageUp = Key(glfw.KeyPageUp)
  100. KeyPageDown = Key(glfw.KeyPageDown)
  101. KeyHome = Key(glfw.KeyHome)
  102. KeyEnd = Key(glfw.KeyEnd)
  103. KeyCapsLock = Key(glfw.KeyCapsLock)
  104. KeyScrollLock = Key(glfw.KeyScrollLock)
  105. KeyNumLock = Key(glfw.KeyNumLock)
  106. KeyPrintScreen = Key(glfw.KeyPrintScreen)
  107. KeyPause = Key(glfw.KeyPause)
  108. KeyF1 = Key(glfw.KeyF1)
  109. KeyF2 = Key(glfw.KeyF2)
  110. KeyF3 = Key(glfw.KeyF3)
  111. KeyF4 = Key(glfw.KeyF4)
  112. KeyF5 = Key(glfw.KeyF5)
  113. KeyF6 = Key(glfw.KeyF6)
  114. KeyF7 = Key(glfw.KeyF7)
  115. KeyF8 = Key(glfw.KeyF8)
  116. KeyF9 = Key(glfw.KeyF9)
  117. KeyF10 = Key(glfw.KeyF10)
  118. KeyF11 = Key(glfw.KeyF11)
  119. KeyF12 = Key(glfw.KeyF12)
  120. KeyF13 = Key(glfw.KeyF13)
  121. KeyF14 = Key(glfw.KeyF14)
  122. KeyF15 = Key(glfw.KeyF15)
  123. KeyF16 = Key(glfw.KeyF16)
  124. KeyF17 = Key(glfw.KeyF17)
  125. KeyF18 = Key(glfw.KeyF18)
  126. KeyF19 = Key(glfw.KeyF19)
  127. KeyF20 = Key(glfw.KeyF20)
  128. KeyF21 = Key(glfw.KeyF21)
  129. KeyF22 = Key(glfw.KeyF22)
  130. KeyF23 = Key(glfw.KeyF23)
  131. KeyF24 = Key(glfw.KeyF24)
  132. KeyF25 = Key(glfw.KeyF25)
  133. KeyKP0 = Key(glfw.KeyKP0)
  134. KeyKP1 = Key(glfw.KeyKP1)
  135. KeyKP2 = Key(glfw.KeyKP2)
  136. KeyKP3 = Key(glfw.KeyKP3)
  137. KeyKP4 = Key(glfw.KeyKP4)
  138. KeyKP5 = Key(glfw.KeyKP5)
  139. KeyKP6 = Key(glfw.KeyKP6)
  140. KeyKP7 = Key(glfw.KeyKP7)
  141. KeyKP8 = Key(glfw.KeyKP8)
  142. KeyKP9 = Key(glfw.KeyKP9)
  143. KeyKPDecimal = Key(glfw.KeyKPDecimal)
  144. KeyKPDivide = Key(glfw.KeyKPDivide)
  145. KeyKPMultiply = Key(glfw.KeyKPMultiply)
  146. KeyKPSubtract = Key(glfw.KeyKPSubtract)
  147. KeyKPAdd = Key(glfw.KeyKPAdd)
  148. KeyKPEnter = Key(glfw.KeyKPEnter)
  149. KeyKPEqual = Key(glfw.KeyKPEqual)
  150. KeyLeftShift = Key(glfw.KeyLeftShift)
  151. KeyLeftControl = Key(glfw.KeyLeftControl)
  152. KeyLeftAlt = Key(glfw.KeyLeftAlt)
  153. KeyLeftSuper = Key(glfw.KeyLeftSuper)
  154. KeyRightShift = Key(glfw.KeyRightShift)
  155. KeyRightControl = Key(glfw.KeyRightControl)
  156. KeyRightAlt = Key(glfw.KeyRightAlt)
  157. KeyRightSuper = Key(glfw.KeyRightSuper)
  158. KeyMenu = Key(glfw.KeyMenu)
  159. KeyLast = Key(glfw.KeyLast)
  160. )
  161. // ModifierKey corresponds to a modifier key.
  162. type ModifierKey int
  163. // Modifier keys
  164. const (
  165. ModShift = ModifierKey(glfw.ModShift)
  166. ModControl = ModifierKey(glfw.ModControl)
  167. ModAlt = ModifierKey(glfw.ModAlt)
  168. ModSuper = ModifierKey(glfw.ModSuper)
  169. )
  170. // MouseButton corresponds to a mouse button.
  171. type MouseButton int
  172. // Mouse buttons
  173. const (
  174. MouseButton1 = MouseButton(glfw.MouseButton1)
  175. MouseButton2 = MouseButton(glfw.MouseButton2)
  176. MouseButton3 = MouseButton(glfw.MouseButton3)
  177. MouseButton4 = MouseButton(glfw.MouseButton4)
  178. MouseButton5 = MouseButton(glfw.MouseButton5)
  179. MouseButton6 = MouseButton(glfw.MouseButton6)
  180. MouseButton7 = MouseButton(glfw.MouseButton7)
  181. MouseButton8 = MouseButton(glfw.MouseButton8)
  182. MouseButtonLast = MouseButton(glfw.MouseButtonLast)
  183. MouseButtonLeft = MouseButton(glfw.MouseButtonLeft)
  184. MouseButtonRight = MouseButton(glfw.MouseButtonRight)
  185. MouseButtonMiddle = MouseButton(glfw.MouseButtonMiddle)
  186. )
  187. // StandardCursor corresponds to a standard cursor icon.
  188. type StandardCursor int
  189. // Standard cursors
  190. const (
  191. ArrowCursor = StandardCursor(glfw.ArrowCursor)
  192. IBeamCursor = StandardCursor(glfw.IBeamCursor)
  193. CrosshairCursor = StandardCursor(glfw.CrosshairCursor)
  194. HandCursor = StandardCursor(glfw.HandCursor)
  195. HResizeCursor = StandardCursor(glfw.HResizeCursor)
  196. VResizeCursor = StandardCursor(glfw.VResizeCursor)
  197. )
  198. // Action corresponds to a key or button action.
  199. type Action int
  200. const (
  201. Release = Action(glfw.Release) // The key or button was released.
  202. Press = Action(glfw.Press) // The key or button was pressed.
  203. Repeat = Action(glfw.Repeat) // The key was held down until it repeated.
  204. )
  205. // InputMode corresponds to an input mode.
  206. type InputMode int
  207. // Input modes
  208. const (
  209. CursorMode = InputMode(glfw.CursorMode) // See Cursor mode values
  210. StickyKeysMode = InputMode(glfw.StickyKeysMode) // Value can be either 1 or 0
  211. StickyMouseButtonsMode = InputMode(glfw.StickyMouseButtonsMode) // Value can be either 1 or 0
  212. )
  213. // Cursor mode values
  214. const (
  215. CursorNormal = glfw.CursorNormal
  216. CursorHidden = glfw.CursorHidden
  217. CursorDisabled = glfw.CursorDisabled
  218. )
  219. //
  220. // Window event names using for dispatch and subscribe
  221. //
  222. const (
  223. OnWindowPos = "win.OnWindowPos"
  224. OnWindowSize = "win.OnWindowSize"
  225. OnKeyUp = "win.OnKeyUp"
  226. OnKeyDown = "win.OnKeyDown"
  227. OnChar = "win.OnChar"
  228. OnCursor = "win.OnCursor"
  229. OnMouseUp = "win.OnMouseUp"
  230. OnMouseDown = "win.OnMouseDown"
  231. OnScroll = "win.OnScroll"
  232. OnFrame = "win.OnFrame"
  233. )
  234. // Window position changed event
  235. type PosEvent struct {
  236. W IWindow
  237. Xpos int
  238. Ypos int
  239. }
  240. // Window size changed
  241. type SizeEvent struct {
  242. W IWindow
  243. Width int
  244. Height int
  245. }
  246. // Key pressed in window
  247. type KeyEvent struct {
  248. W IWindow
  249. Keycode Key
  250. Scancode int
  251. Action Action
  252. Mods ModifierKey
  253. }
  254. // Char pressed in window
  255. type CharEvent struct {
  256. W IWindow
  257. Char rune
  258. Mods ModifierKey
  259. }
  260. // Mouse button event
  261. type MouseEvent struct {
  262. W IWindow
  263. Xpos float32
  264. Ypos float32
  265. Button MouseButton
  266. Action Action
  267. Mods ModifierKey
  268. }
  269. // Cursor position changed
  270. type CursorEvent struct {
  271. W IWindow
  272. Xpos float32
  273. Ypos float32
  274. }
  275. // Scroll event
  276. type ScrollEvent struct {
  277. W IWindow
  278. Xoffset float32
  279. Yoffset float32
  280. }
  281. func New(wtype string, width, height int, title string, full bool) (IWindow, error) {
  282. if wtype != "glfw" {
  283. panic("Unsupported window type")
  284. }
  285. return newGLFW(width, height, title, full)
  286. }