window.go 9.4 KB

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