window.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 a system window.
  5. // Depending on the build tags it can be a GLFW desktop window or a browser WebGlCanvas.
  6. package window
  7. import (
  8. "fmt"
  9. "github.com/g3n/engine/core"
  10. "github.com/g3n/engine/gls"
  11. )
  12. // IWindow singleton
  13. var win IWindow
  14. // Get returns the IWindow singleton.
  15. func Get() IWindow {
  16. // Return singleton if already created
  17. if win != nil {
  18. return win
  19. }
  20. panic(fmt.Errorf("need to call window.Init() first"))
  21. }
  22. // IWindow is the interface for all windows
  23. type IWindow interface {
  24. core.IDispatcher
  25. Gls() *gls.GLS
  26. GetFramebufferSize() (width int, height int)
  27. GetSize() (width int, height int)
  28. GetScale() (x float64, y float64)
  29. CreateCursor(imgFile string, xhot, yhot int) (Cursor, error)
  30. SetCursor(cursor Cursor)
  31. DisposeAllCustomCursors()
  32. Destroy()
  33. }
  34. // Key corresponds to a keyboard key.
  35. type Key int
  36. // ModifierKey corresponds to a set of modifier keys (bitmask).
  37. type ModifierKey int
  38. // MouseButton corresponds to a mouse button.
  39. type MouseButton int
  40. // Action corresponds to a key or button action.
  41. type Action int
  42. // InputMode corresponds to an input mode.
  43. type InputMode int
  44. // InputMode corresponds to an input mode.
  45. type CursorMode int
  46. // Cursor corresponds to a g3n standard or user-created cursor icon.
  47. type Cursor int
  48. // Standard cursors for G3N.
  49. const (
  50. ArrowCursor = Cursor(iota)
  51. IBeamCursor
  52. CrosshairCursor
  53. HandCursor
  54. HResizeCursor
  55. VResizeCursor
  56. DiagResize1Cursor
  57. DiagResize2Cursor
  58. CursorLast = DiagResize2Cursor
  59. )
  60. //
  61. // Window event names used for dispatch and subscribe
  62. //
  63. const (
  64. OnWindowPos = "w.OnWindowPos"
  65. OnWindowSize = "w.OnWindowSize"
  66. OnKeyUp = "w.OnKeyUp"
  67. OnKeyDown = "w.OnKeyDown"
  68. OnKeyRepeat = "w.OnKeyRepeat"
  69. OnChar = "w.OnChar"
  70. OnCursor = "w.OnCursor"
  71. OnMouseUp = "w.OnMouseUp"
  72. OnMouseDown = "w.OnMouseDown"
  73. OnScroll = "w.OnScroll"
  74. )
  75. // PosEvent describes a windows position changed event
  76. type PosEvent struct {
  77. Xpos int
  78. Ypos int
  79. }
  80. // SizeEvent describers a window size changed event
  81. type SizeEvent struct {
  82. Width int
  83. Height int
  84. }
  85. // KeyEvent describes a window key event
  86. type KeyEvent struct {
  87. Keycode Key
  88. Action Action
  89. Mods ModifierKey
  90. }
  91. // CharEvent describes a window char event
  92. type CharEvent struct {
  93. Char rune
  94. Mods ModifierKey
  95. }
  96. // MouseEvent describes a mouse event over the window
  97. type MouseEvent struct {
  98. Xpos float32
  99. Ypos float32
  100. Button MouseButton
  101. Action Action
  102. Mods ModifierKey
  103. }
  104. // CursorEvent describes a cursor position changed event
  105. type CursorEvent struct {
  106. Xpos float32
  107. Ypos float32
  108. Mods ModifierKey
  109. }
  110. // ScrollEvent describes a scroll event
  111. type ScrollEvent struct {
  112. Xoffset float32
  113. Yoffset float32
  114. Mods ModifierKey
  115. }