window.go 9.5 KB

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