event.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* event handler and events for the Zori module. */
  2. package event
  3. import . "gitlab.com/beoran/ebsgo/zori/types"
  4. // event types, intentionally same integer values as Allegro 5's event types
  5. // apart from the Zori-specific events.
  6. const (
  7. TypeJoystickAxis = 1
  8. TypeJoystickButtonDown = 2
  9. TypeJoystickButtonUp = 3
  10. TypeJoystickConfiguration = 4
  11. TypeKeyDown = 10
  12. TypeKeyChar = 11
  13. TypeKeyUp = 12
  14. TypeMouseAxes = 20
  15. TypeMouseButtonDown = 21
  16. TypeMouseButtonUp = 22
  17. TypeMouseEnterDisplay = 23
  18. TypeMouseLeaveDisplay = 24
  19. TypeMouseWarped = 25
  20. TypeTimer = 30
  21. TypeDisplayExpose = 40
  22. TypeDisplayResize = 41
  23. TypeDisplayClose = 42
  24. TypeDisplayLost = 43
  25. TypeDisplayFound = 44
  26. TypeDisplaySwitchIn = 45
  27. TypeDisplaySwitchOut = 46
  28. TypeDisplayOrientation = 47
  29. TypeDisplayHaltDrawing = 48
  30. TypeDisplayResumeDrawing = 49
  31. TypeTouchBegin = 50
  32. TypeTouchEnd = 51
  33. TypeTouchMove = 52
  34. TypeTouchCancel = 53
  35. TypeDisplayConnected = 60
  36. TypeDisplayDisconnected = 61
  37. TypeZoriUpdate = 70311
  38. TypeZoriResize = 70312
  39. TypeZoriDraw = 70313
  40. TypeZoriDestroy = 70314
  41. TypeZoriAction = 70315
  42. TypeZoriClose = 70316
  43. TypeZoriNewChild = 70317
  44. )
  45. type Basic struct {
  46. Stamp float64
  47. // Cannot be Widget type to avoid dependency loop, but should hold a widget or nil.
  48. EventWidget interface{}
  49. EventData interface{}
  50. }
  51. func (be Basic) Data() interface{} {
  52. return be.EventData
  53. }
  54. func (be Basic) Widget() interface{} {
  55. return be.EventWidget
  56. }
  57. func (be Basic) Timestamp() float64 {
  58. return be.Stamp
  59. }
  60. /* Update event, when UI has to update (animations, etc). */
  61. type Update struct {
  62. Basic
  63. TimePassed float64
  64. }
  65. func (be Update) Type() int {
  66. return TypeZoriUpdate
  67. }
  68. /* Resize event, when the parent of an element has resized. */
  69. type Resize struct {
  70. Basic
  71. NewSize Box
  72. }
  73. func (be Resize) Type() int {
  74. return TypeZoriResize
  75. }
  76. /* Draw event when the UI has to draw itself. */
  77. type Draw struct {
  78. Basic
  79. }
  80. func (be Draw) Type() int {
  81. return TypeZoriDraw
  82. }
  83. /* Cleanup event. */
  84. type Destroy struct {
  85. Basic
  86. }
  87. func (be Destroy) Type() int {
  88. return TypeZoriDestroy
  89. }
  90. type ActionCallback func (data ... interface{}) Result
  91. /* Action event. */
  92. type Action struct {
  93. Basic
  94. ActionCallback
  95. }
  96. func (be Action) Type() int {
  97. return TypeZoriAction
  98. }
  99. /* Close event. */
  100. type Close struct {
  101. Basic
  102. Parent interface {}
  103. }
  104. func (be Close) Type() int {
  105. return TypeZoriClose
  106. }
  107. /* New child event. */
  108. type NewChild struct {
  109. Basic
  110. Child interface {}
  111. }
  112. func (be NewChild) Type() int {
  113. return TypeZoriNewChild
  114. }
  115. type Event interface {
  116. Type() int
  117. Timestamp() float64
  118. }
  119. type Zori interface {
  120. Event
  121. Data() interface{}
  122. Widget() interface{}
  123. }
  124. type Joystick interface {
  125. Event
  126. ID() int
  127. Button() int
  128. Stick() int
  129. Axis() int
  130. Pos() float32
  131. }
  132. type Key interface {
  133. Event
  134. KeyCode() int
  135. Display() Display
  136. Unichar() rune
  137. Modifiers() int
  138. Repeat() bool
  139. }
  140. type Mouse interface {
  141. Event
  142. Display() Display
  143. X() int
  144. Y() int
  145. Z() int
  146. W() int
  147. DX() int
  148. DY() int
  149. DZ() int
  150. DW() int
  151. Button() int
  152. Pressure() float32
  153. }
  154. type Touch interface {
  155. Event
  156. Display() Display
  157. X() int
  158. Y() int
  159. DX() int
  160. DY() int
  161. ID() int
  162. Primary() bool
  163. }
  164. type Result bool
  165. const Done = Result(true)
  166. const Pass = Result(false)
  167. type UIHandler interface {
  168. Update(ev Update) Result
  169. Resize(ev Resize) Result
  170. Draw(ev Draw) Result
  171. Destroy(ev Destroy) Result
  172. Action(ev Action) Result
  173. Close(ev Close) Result
  174. NewChild(ev NewChild) Result
  175. }
  176. type JoystickHandler interface {
  177. JoystickButtonPress(ev Joystick) Result
  178. JoystickButtonRelease(ev Joystick) Result
  179. JoystickAxis(ev Joystick) Result
  180. }
  181. type KeyHandler interface {
  182. KeyPress(ev Key) Result
  183. KeyRelease(ev Key) Result
  184. KeyChar(ev Key) Result
  185. }
  186. type MouseHandler interface {
  187. MouseAxes(ev Mouse) Result
  188. MouseWarped(ev Mouse) Result
  189. MouseButtonPress(ev Mouse) Result
  190. MouseButtonRelease(ev Mouse) Result
  191. MouseEnterDisplay(ev Mouse) Result
  192. MouseLeaveDisplay(ev Mouse) Result
  193. }
  194. type TouchHandler interface {
  195. TouchBegin(ev Touch) Result
  196. TouchEnd(ev Touch) Result
  197. TouchMove(ev Touch) Result
  198. TouchCancel(ev Touch) Result
  199. }
  200. /* Handler is an interface for a type that can handle all
  201. * of Zori's events. */
  202. type Handler interface {
  203. UIHandler
  204. JoystickHandler
  205. KeyHandler
  206. MouseHandler
  207. TouchHandler
  208. Default(ev Event) Result
  209. }
  210. /* Dispatches joystick events. */
  211. func DispatchJoystick(handler JoystickHandler, de Joystick) Result {
  212. switch de.Type() {
  213. case TypeJoystickButtonDown: return handler.JoystickButtonPress(de)
  214. case TypeJoystickButtonUp: return handler.JoystickButtonRelease(de)
  215. case TypeJoystickAxis: return handler.JoystickAxis(de)
  216. default: return Pass
  217. }
  218. }
  219. /* Dispatches key events. */
  220. func DispatchKey(handler KeyHandler, de Key) Result {
  221. switch de.Type() {
  222. case TypeKeyDown: return handler.KeyPress(de)
  223. case TypeKeyUp: return handler.KeyRelease(de)
  224. case TypeKeyChar: return handler.KeyChar(de)
  225. default: return Pass
  226. }
  227. }
  228. /* Dispatches mouse events. */
  229. func DispatchMouse(handler MouseHandler, de Mouse) Result {
  230. switch de.Type() {
  231. case TypeMouseAxes: return handler.MouseAxes(de)
  232. case TypeMouseWarped: return handler.MouseWarped(de)
  233. case TypeMouseButtonDown: return handler.MouseButtonPress(de)
  234. case TypeMouseButtonUp: return handler.MouseButtonRelease(de)
  235. case TypeMouseEnterDisplay: return handler.MouseEnterDisplay(de)
  236. case TypeMouseLeaveDisplay: return handler.MouseLeaveDisplay(de)
  237. default: return Pass
  238. }
  239. }
  240. /* Dispatches touch events. */
  241. func DispatchTouch(handler TouchHandler, de Touch) Result {
  242. switch de.Type() {
  243. case TypeTouchBegin: return handler.TouchBegin(de)
  244. case TypeTouchEnd: return handler.TouchEnd(de)
  245. case TypeTouchMove: return handler.TouchMove(de)
  246. case TypeTouchCancel: return handler.TouchCancel(de)
  247. default: return Pass
  248. }
  249. }
  250. func Dispatch(event Event, handler Handler) Result {
  251. switch de := event.(type) {
  252. case Update: return handler.Update(de)
  253. case Draw: return handler.Draw(de)
  254. case Destroy: return handler.Destroy(de)
  255. case Action: return handler.Action(de)
  256. case Close: return handler.Close(de)
  257. case NewChild: return handler.NewChild(de)
  258. case Joystick: return DispatchJoystick(handler, de)
  259. case Key: return DispatchKey(handler, de)
  260. case Mouse: return DispatchMouse(handler, de)
  261. case Touch: return DispatchTouch(handler, de)
  262. default: return handler.Default(event)
  263. }
  264. }
  265. func String(event Event) string {
  266. switch event.(type) {
  267. case Update: return "Update"
  268. case Draw: return "Draw"
  269. case Destroy: return "Destroy"
  270. case Action: return "Action"
  271. case Close: return "Close"
  272. case NewChild: return "NewChild"
  273. case Joystick: return "Joystick"
  274. case Key: return "Key"
  275. case Mouse: return "Mouse"
  276. case Touch: return "Touch"
  277. default: return "DefaultEvent"
  278. }
  279. }