window.go 3.0 KB

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