window.go 9.1 KB

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