window.go 9.1 KB

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