window.go 9.4 KB

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