window.go 9.4 KB

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