window.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. OnWindowFocus = "w.OnWindowFocus" // x | x |
  66. OnWindowPos = "w.OnWindowPos" // x | |
  67. OnWindowSize = "w.OnWindowSize" // x | |
  68. OnKeyUp = "w.OnKeyUp" // x | x |
  69. OnKeyDown = "w.OnKeyDown" // x | x |
  70. OnKeyRepeat = "w.OnKeyRepeat" // x | |
  71. OnChar = "w.OnChar" // x | x |
  72. OnCursor = "w.OnCursor" // x | x |
  73. OnMouseUp = "w.OnMouseUp" // x | x |
  74. OnMouseDown = "w.OnMouseDown" // x | x |
  75. OnScroll = "w.OnScroll" // x | x |
  76. )
  77. // PosEvent describes a windows position changed event
  78. type PosEvent struct {
  79. Xpos int
  80. Ypos int
  81. }
  82. // SizeEvent describers a window size changed event
  83. type SizeEvent struct {
  84. Width int
  85. Height int
  86. }
  87. // KeyEvent describes a window key event
  88. type KeyEvent struct {
  89. Key Key
  90. Mods ModifierKey
  91. }
  92. // CharEvent describes a window char event
  93. type CharEvent struct {
  94. Char rune
  95. Mods ModifierKey
  96. }
  97. // MouseEvent describes a mouse event over the window
  98. type MouseEvent struct {
  99. Xpos float32
  100. Ypos float32
  101. Button MouseButton
  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. }
  116. // FocusEvent describes a focus event
  117. type FocusEvent struct {
  118. Focused bool
  119. }