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};
  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. screen_changed: EventChannel<bool>,
  40. resize: EventChannel<Dimension>,
  41. key_down: EventChannel<(KeyCode, KeyMods, bool)>,
  42. key_up: EventChannel<(KeyCode, KeyMods)>,
  43. chr: EventChannel<(char, KeyMods, bool)>,
  44. mouse_btn_down: EventChannel<(MouseButton, Point)>,
  45. mouse_btn_up: EventChannel<(MouseButton, Point)>,
  46. mouse_move: EventChannel<Point>,
  47. mouse_wheel: EventChannel<Point>,
  48. }
  49. pub type GraphicsEventScreenSub = async_channel::Receiver<bool>;
  50. pub type GraphicsEventResizeSub = async_channel::Receiver<Dimension>;
  51. pub type GraphicsEventKeyDownSub = async_channel::Receiver<(KeyCode, KeyMods, bool)>;
  52. pub type GraphicsEventKeyUpSub = async_channel::Receiver<(KeyCode, KeyMods)>;
  53. pub type GraphicsEventCharSub = async_channel::Receiver<(char, KeyMods, bool)>;
  54. pub type GraphicsEventMouseButtonDownSub = async_channel::Receiver<(MouseButton, Point)>;
  55. pub type GraphicsEventMouseButtonUpSub = async_channel::Receiver<(MouseButton, Point)>;
  56. pub type GraphicsEventMouseMoveSub = async_channel::Receiver<Point>;
  57. pub type GraphicsEventMouseWheelSub = async_channel::Receiver<Point>;
  58. impl GraphicsEventPublisher {
  59. pub fn new() -> Arc<Self> {
  60. Arc::new(Self {
  61. screen_changed: EventChannel::new(),
  62. resize: EventChannel::new(),
  63. key_down: EventChannel::new(),
  64. key_up: EventChannel::new(),
  65. chr: EventChannel::new(),
  66. mouse_btn_down: EventChannel::new(),
  67. mouse_btn_up: EventChannel::new(),
  68. mouse_move: EventChannel::new(),
  69. mouse_wheel: EventChannel::new(),
  70. })
  71. }
  72. pub(super) fn notify_screen_changed(&self, screen_on: bool) {
  73. self.screen_changed.notify(screen_on);
  74. }
  75. pub(super) fn notify_resize(&self, screen_size: Dimension) {
  76. self.resize.notify(screen_size);
  77. }
  78. pub(super) fn notify_key_down(&self, key: KeyCode, mods: KeyMods, repeat: bool) {
  79. let ev = (key, mods, repeat);
  80. self.key_down.notify(ev);
  81. }
  82. pub(super) fn notify_key_up(&self, key: KeyCode, mods: KeyMods) {
  83. let ev = (key, mods);
  84. self.key_up.notify(ev);
  85. }
  86. pub(super) fn notify_char(&self, chr: char, mods: KeyMods, repeat: bool) {
  87. let ev = (chr, mods, repeat);
  88. self.chr.notify(ev);
  89. }
  90. pub(super) fn notify_mouse_btn_down(&self, button: MouseButton, mouse_pos: Point) {
  91. let ev = (button, mouse_pos);
  92. self.mouse_btn_down.notify(ev);
  93. }
  94. pub(super) fn notify_mouse_btn_up(&self, button: MouseButton, mouse_pos: Point) {
  95. let ev = (button, mouse_pos);
  96. self.mouse_btn_up.notify(ev);
  97. }
  98. pub(super) fn notify_mouse_move(&self, mouse_pos: Point) {
  99. self.mouse_move.notify(mouse_pos);
  100. }
  101. pub(super) fn notify_mouse_wheel(&self, wheel_pos: Point) {
  102. self.mouse_wheel.notify(wheel_pos);
  103. }
  104. pub fn subscribe_screen_changed(&self) -> GraphicsEventScreenSub {
  105. self.screen_changed.clone_recvr()
  106. }
  107. pub fn subscribe_resize(&self) -> GraphicsEventResizeSub {
  108. self.resize.clone_recvr()
  109. }
  110. pub fn subscribe_key_down(&self) -> GraphicsEventKeyDownSub {
  111. self.key_down.clone_recvr()
  112. }
  113. pub fn subscribe_key_up(&self) -> GraphicsEventKeyUpSub {
  114. self.key_up.clone_recvr()
  115. }
  116. pub fn subscribe_char(&self) -> GraphicsEventCharSub {
  117. self.chr.clone_recvr()
  118. }
  119. pub fn subscribe_mouse_btn_down(&self) -> GraphicsEventMouseButtonDownSub {
  120. self.mouse_btn_down.clone_recvr()
  121. }
  122. pub fn subscribe_mouse_btn_up(&self) -> GraphicsEventMouseButtonUpSub {
  123. self.mouse_btn_up.clone_recvr()
  124. }
  125. pub fn subscribe_mouse_move(&self) -> GraphicsEventMouseMoveSub {
  126. self.mouse_move.clone_recvr()
  127. }
  128. pub fn subscribe_mouse_wheel(&self) -> GraphicsEventMouseWheelSub {
  129. self.mouse_wheel.clone_recvr()
  130. }
  131. }