ev.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use miniquad::{KeyCode, KeyMods, MouseButton, TouchPhase};
  19. use std::sync::Arc;
  20. use super::{Dimension, Point};
  21. struct EventChannel<T> {
  22. sender: async_channel::Sender<T>,
  23. recvr: async_channel::Receiver<T>,
  24. }
  25. impl<T> EventChannel<T> {
  26. fn new() -> Self {
  27. let (sender, recvr) = async_channel::unbounded();
  28. Self { sender, recvr }
  29. }
  30. fn notify(&self, ev: T) {
  31. self.sender.try_send(ev).unwrap();
  32. }
  33. fn clone_recvr(&self) -> async_channel::Receiver<T> {
  34. self.recvr.clone()
  35. }
  36. }
  37. pub type GraphicsEventPublisherPtr = Arc<GraphicsEventPublisher>;
  38. pub struct GraphicsEventPublisher {
  39. resize: EventChannel<Dimension>,
  40. key_down: EventChannel<(KeyCode, KeyMods, bool)>,
  41. key_up: EventChannel<(KeyCode, KeyMods)>,
  42. chr: EventChannel<(char, KeyMods, bool)>,
  43. mouse_btn_down: EventChannel<(MouseButton, Point)>,
  44. mouse_btn_up: EventChannel<(MouseButton, Point)>,
  45. mouse_move: EventChannel<Point>,
  46. mouse_wheel: EventChannel<Point>,
  47. touch: EventChannel<(TouchPhase, u64, Point)>,
  48. }
  49. pub type GraphicsEventResizeSub = async_channel::Receiver<Dimension>;
  50. pub type GraphicsEventKeyDownSub = async_channel::Receiver<(KeyCode, KeyMods, bool)>;
  51. pub type GraphicsEventKeyUpSub = async_channel::Receiver<(KeyCode, KeyMods)>;
  52. pub type GraphicsEventCharSub = async_channel::Receiver<(char, KeyMods, bool)>;
  53. pub type GraphicsEventMouseButtonDownSub = async_channel::Receiver<(MouseButton, Point)>;
  54. pub type GraphicsEventMouseButtonUpSub = async_channel::Receiver<(MouseButton, Point)>;
  55. pub type GraphicsEventMouseMoveSub = async_channel::Receiver<Point>;
  56. pub type GraphicsEventMouseWheelSub = async_channel::Receiver<Point>;
  57. pub type GraphicsEventTouchSub = async_channel::Receiver<(TouchPhase, u64, Point)>;
  58. impl GraphicsEventPublisher {
  59. pub fn new() -> Arc<Self> {
  60. Arc::new(Self {
  61. resize: EventChannel::new(),
  62. key_down: EventChannel::new(),
  63. key_up: EventChannel::new(),
  64. chr: EventChannel::new(),
  65. mouse_btn_down: EventChannel::new(),
  66. mouse_btn_up: EventChannel::new(),
  67. mouse_move: EventChannel::new(),
  68. mouse_wheel: EventChannel::new(),
  69. touch: EventChannel::new(),
  70. })
  71. }
  72. pub(super) fn notify_resize(&self, screen_size: Dimension) {
  73. self.resize.notify(screen_size);
  74. }
  75. pub(super) fn notify_key_down(&self, key: KeyCode, mods: KeyMods, repeat: bool) {
  76. let ev = (key, mods, repeat);
  77. self.key_down.notify(ev);
  78. }
  79. pub(super) fn notify_key_up(&self, key: KeyCode, mods: KeyMods) {
  80. let ev = (key, mods);
  81. self.key_up.notify(ev);
  82. }
  83. pub(super) fn notify_char(&self, chr: char, mods: KeyMods, repeat: bool) {
  84. let ev = (chr, mods, repeat);
  85. self.chr.notify(ev);
  86. }
  87. pub(super) fn notify_mouse_btn_down(&self, button: MouseButton, mouse_pos: Point) {
  88. let ev = (button, mouse_pos);
  89. self.mouse_btn_down.notify(ev);
  90. }
  91. pub(super) fn notify_mouse_btn_up(&self, button: MouseButton, mouse_pos: Point) {
  92. let ev = (button, mouse_pos);
  93. self.mouse_btn_up.notify(ev);
  94. }
  95. pub(super) fn notify_mouse_move(&self, mouse_pos: Point) {
  96. self.mouse_move.notify(mouse_pos);
  97. }
  98. pub(super) fn notify_mouse_wheel(&self, wheel_pos: Point) {
  99. self.mouse_wheel.notify(wheel_pos);
  100. }
  101. pub(super) fn notify_touch(&self, phase: TouchPhase, id: u64, touch_pos: Point) {
  102. let ev = (phase, id, touch_pos);
  103. self.touch.notify(ev);
  104. }
  105. pub fn subscribe_resize(&self) -> GraphicsEventResizeSub {
  106. self.resize.clone_recvr()
  107. }
  108. pub fn subscribe_key_down(&self) -> GraphicsEventKeyDownSub {
  109. self.key_down.clone_recvr()
  110. }
  111. pub fn subscribe_key_up(&self) -> GraphicsEventKeyUpSub {
  112. self.key_up.clone_recvr()
  113. }
  114. pub fn subscribe_char(&self) -> GraphicsEventCharSub {
  115. self.chr.clone_recvr()
  116. }
  117. pub fn subscribe_mouse_btn_down(&self) -> GraphicsEventMouseButtonDownSub {
  118. self.mouse_btn_down.clone_recvr()
  119. }
  120. pub fn subscribe_mouse_btn_up(&self) -> GraphicsEventMouseButtonUpSub {
  121. self.mouse_btn_up.clone_recvr()
  122. }
  123. pub fn subscribe_mouse_move(&self) -> GraphicsEventMouseMoveSub {
  124. self.mouse_move.clone_recvr()
  125. }
  126. pub fn subscribe_mouse_wheel(&self) -> GraphicsEventMouseWheelSub {
  127. self.mouse_wheel.clone_recvr()
  128. }
  129. pub fn subscribe_touch(&self) -> GraphicsEventTouchSub {
  130. self.touch.clone_recvr()
  131. }
  132. }