window.go 9.8 KB

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