window.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 platform-specific 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. "github.com/g3n/engine/util/logger"
  12. )
  13. // Package logger
  14. var log = logger.New("WIN", logger.Default)
  15. // IWindow singleton
  16. var win IWindow
  17. // Get returns the IWindow singleton.
  18. func Get() IWindow {
  19. // Return singleton if already created
  20. if win != nil {
  21. return win
  22. }
  23. panic(fmt.Errorf("need to call window.Init() first"))
  24. }
  25. // IWindow is the interface for all windows
  26. type IWindow interface {
  27. core.IDispatcher
  28. Gls() *gls.GLS
  29. GetFramebufferSize() (width int, height int)
  30. GetSize() (width int, height int)
  31. GetScale() (x float64, y float64)
  32. CreateCursor(imgFile string, xhot, yhot int) (Cursor, error)
  33. SetCursor(cursor Cursor)
  34. DisposeAllCustomCursors()
  35. Destroy()
  36. SetCursorMode(cm CursorMode)
  37. }
  38. // Key corresponds to a keyboard key.
  39. type Key int
  40. // ModifierKey corresponds to a set of modifier keys (bitmask).
  41. type ModifierKey int
  42. // MouseButton corresponds to a mouse button.
  43. type MouseButton int
  44. // InputMode corresponds to an input mode.
  45. type InputMode int
  46. // InputMode corresponds to an input mode.
  47. type CursorMode int
  48. // Cursor corresponds to a g3n standard or user-created cursor icon.
  49. type Cursor int
  50. // Standard cursors for G3N.
  51. const (
  52. ArrowCursor = Cursor(iota)
  53. IBeamCursor
  54. CrosshairCursor
  55. HandCursor
  56. HResizeCursor
  57. VResizeCursor
  58. DiagResize1Cursor
  59. DiagResize2Cursor
  60. CursorLast = DiagResize2Cursor
  61. )
  62. // Window event names. See availability per platform below ("x" indicates available).
  63. const ( // Desktop | Browser |
  64. OnWindowPos = "w.OnWindowPos" // x | |
  65. OnWindowSize = "w.OnWindowSize" // x | |
  66. OnKeyUp = "w.OnKeyUp" // x | x |
  67. OnKeyDown = "w.OnKeyDown" // x | x |
  68. OnKeyRepeat = "w.OnKeyRepeat" // x | |
  69. OnChar = "w.OnChar" // x | x |
  70. OnCursor = "w.OnCursor" // x | x |
  71. OnMouseUp = "w.OnMouseUp" // x | x |
  72. OnMouseDown = "w.OnMouseDown" // x | x |
  73. OnScroll = "w.OnScroll" // x | x |
  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. Key Key
  88. Mods ModifierKey
  89. }
  90. // CharEvent describes a window char event
  91. type CharEvent struct {
  92. Char rune
  93. Mods ModifierKey
  94. }
  95. // MouseEvent describes a mouse event over the window
  96. type MouseEvent struct {
  97. Xpos float32
  98. Ypos float32
  99. Button MouseButton
  100. Mods ModifierKey
  101. }
  102. // CursorEvent describes a cursor position changed event
  103. type CursorEvent struct {
  104. Xpos float32
  105. Ypos float32
  106. Mods ModifierKey
  107. }
  108. // ScrollEvent describes a scroll event
  109. type ScrollEvent struct {
  110. Xoffset float32
  111. Yoffset float32
  112. Mods ModifierKey
  113. }