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