window.go 9.3 KB

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