button.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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 async_trait::async_trait;
  19. use miniquad::{MouseButton, TouchPhase};
  20. use std::sync::{
  21. atomic::{AtomicBool, Ordering},
  22. Arc, Weak,
  23. };
  24. use crate::{
  25. gfx::{GraphicsEventPublisherPtr, Point, Rectangle},
  26. prop::{PropertyAtomicGuard, PropertyBool, PropertyPtr, PropertyRect, PropertyUint32, Role},
  27. pubsub::Subscription,
  28. scene::{Pimpl, SceneNodePtr, SceneNodeWeak},
  29. ExecutorPtr,
  30. };
  31. use super::{DrawUpdate, UIObject};
  32. macro_rules! d { ($($arg:tt)*) => { debug!(target: "app", $($arg)*); } }
  33. macro_rules! t { ($($arg:tt)*) => { trace!(target: "app", $($arg)*); } }
  34. pub type ButtonPtr = Arc<Button>;
  35. pub struct Button {
  36. node: SceneNodeWeak,
  37. is_active: PropertyBool,
  38. rect: PropertyRect,
  39. z_index: PropertyUint32,
  40. priority: PropertyUint32,
  41. mouse_btn_held: AtomicBool,
  42. }
  43. impl Button {
  44. pub async fn new(node: SceneNodeWeak, ex: ExecutorPtr) -> Pimpl {
  45. t!("Button::new()");
  46. let node_ref = &node.upgrade().unwrap();
  47. let is_active = PropertyBool::wrap(node_ref, Role::Internal, "is_active", 0).unwrap();
  48. let rect = PropertyRect::wrap(node_ref, Role::Internal, "rect").unwrap();
  49. let z_index = PropertyUint32::wrap(node_ref, Role::Internal, "z_index", 0).unwrap();
  50. let priority = PropertyUint32::wrap(node_ref, Role::Internal, "priority", 0).unwrap();
  51. let self_ = Arc::new(Self {
  52. node,
  53. is_active,
  54. rect,
  55. z_index,
  56. priority,
  57. mouse_btn_held: AtomicBool::new(false),
  58. });
  59. Pimpl::Button(self_)
  60. }
  61. }
  62. #[async_trait]
  63. impl UIObject for Button {
  64. fn priority(&self) -> u32 {
  65. self.priority.get()
  66. }
  67. async fn draw(
  68. &self,
  69. parent_rect: Rectangle,
  70. trace_id: u32,
  71. atom: &mut PropertyAtomicGuard,
  72. ) -> Option<DrawUpdate> {
  73. let _ = self.rect.eval(&parent_rect);
  74. None
  75. }
  76. async fn handle_mouse_btn_down(&self, btn: MouseButton, mouse_pos: Point) -> bool {
  77. if !self.is_active.get() {
  78. return false
  79. }
  80. if btn != MouseButton::Left {
  81. return false
  82. }
  83. let rect = self.rect.get();
  84. if !rect.contains(mouse_pos) {
  85. return false
  86. }
  87. self.mouse_btn_held.store(true, Ordering::Relaxed);
  88. true
  89. }
  90. async fn handle_mouse_btn_up(&self, btn: MouseButton, mouse_pos: Point) -> bool {
  91. t!("handle_mouse_btn_up({btn:?}, {mouse_pos:?})");
  92. if !self.is_active.get() {
  93. return false
  94. }
  95. if btn != MouseButton::Left {
  96. return false
  97. }
  98. // Did we start the click inside the button?
  99. let btn_held = self.mouse_btn_held.swap(false, Ordering::Relaxed);
  100. if !btn_held {
  101. return false
  102. }
  103. // Are we releasing the click inside the button?
  104. let rect = self.rect.get();
  105. if !rect.contains(mouse_pos) {
  106. return false
  107. }
  108. d!("Button clicked!");
  109. let node = self.node.upgrade().unwrap();
  110. node.trigger("click", vec![]).await.unwrap();
  111. true
  112. }
  113. async fn handle_touch(&self, phase: TouchPhase, id: u64, touch_pos: Point) -> bool {
  114. t!("handle_touch({phase:?}, {id}, {touch_pos:?})");
  115. if !self.is_active.get() {
  116. return false
  117. }
  118. // Ignore multi-touch
  119. if id != 0 {
  120. return false
  121. }
  122. let rect = self.rect.get();
  123. if !rect.contains(touch_pos) {
  124. t!("not inside rect");
  125. return false
  126. }
  127. // Simulate mouse events
  128. match phase {
  129. TouchPhase::Started => self.handle_mouse_btn_down(MouseButton::Left, touch_pos).await,
  130. TouchPhase::Moved => false,
  131. TouchPhase::Ended => self.handle_mouse_btn_up(MouseButton::Left, touch_pos).await,
  132. TouchPhase::Cancelled => false,
  133. }
  134. }
  135. }