window.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. }
  37. // Key corresponds to a keyboard key.
  38. type Key int
  39. // ModifierKey corresponds to a set of modifier keys (bitmask).
  40. type ModifierKey int
  41. // MouseButton corresponds to a mouse button.
  42. type MouseButton int
  43. // InputMode corresponds to an input mode.
  44. type InputMode int
  45. // InputMode corresponds to an input mode.
  46. type CursorMode int
  47. // Cursor corresponds to a g3n standard or user-created cursor icon.
  48. type Cursor int
  49. // Standard cursors for G3N.
  50. const (
  51. ArrowCursor = Cursor(iota)
  52. IBeamCursor
  53. CrosshairCursor
  54. HandCursor
  55. HResizeCursor
  56. VResizeCursor
  57. DiagResize1Cursor
  58. DiagResize2Cursor
  59. CursorLast = DiagResize2Cursor
  60. )
  61. // Window event names. See availability per platform below ("x" indicates available).
  62. const ( // Desktop | Browser |
  63. OnWindowPos = "w.OnWindowPos" // x | |
  64. OnWindowSize = "w.OnWindowSize" // x | |
  65. OnKeyUp = "w.OnKeyUp" // x | x |
  66. OnKeyDown = "w.OnKeyDown" // x | x |
  67. OnKeyRepeat = "w.OnKeyRepeat" // x | |
  68. OnChar = "w.OnChar" // x | x |
  69. OnCursor = "w.OnCursor" // x | x |
  70. OnMouseUp = "w.OnMouseUp" // x | x |
  71. OnMouseDown = "w.OnMouseDown" // x | x |
  72. OnScroll = "w.OnScroll" // x | x |
  73. )
  74. // PosEvent describes a windows position changed event
  75. type PosEvent struct {
  76. Xpos int
  77. Ypos int
  78. }
  79. // SizeEvent describers a window size changed event
  80. type SizeEvent struct {
  81. Width int
  82. Height int
  83. }
  84. // KeyEvent describes a window key event
  85. type KeyEvent struct {
  86. Key Key
  87. Mods ModifierKey
  88. }
  89. // CharEvent describes a window char event
  90. type CharEvent struct {
  91. Char rune
  92. Mods ModifierKey
  93. }
  94. // MouseEvent describes a mouse event over the window
  95. type MouseEvent struct {
  96. Xpos float32
  97. Ypos float32
  98. Button MouseButton
  99. Mods ModifierKey
  100. }
  101. // CursorEvent describes a cursor position changed event
  102. type CursorEvent struct {
  103. Xpos float32
  104. Ypos float32
  105. Mods ModifierKey
  106. }
  107. // ScrollEvent describes a scroll event
  108. type ScrollEvent struct {
  109. Xoffset float32
  110. Yoffset float32
  111. Mods ModifierKey
  112. }