window.go 9.1 KB

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