window.go 3.5 KB

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