button.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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::{PropertyBool, PropertyPtr, PropertyRect, PropertyUint32, Role},
  27. pubsub::Subscription,
  28. scene::{Pimpl, SceneNodePtr, SceneNodeWeak},
  29. ExecutorPtr,
  30. };
  31. use super::{DrawUpdate, UIObject};
  32. pub type ButtonPtr = Arc<Button>;
  33. pub struct Button {
  34. node: SceneNodeWeak,
  35. is_active: PropertyBool,
  36. rect: PropertyRect,
  37. z_index: PropertyUint32,
  38. priority: PropertyUint32,
  39. mouse_btn_held: AtomicBool,
  40. }
  41. impl Button {
  42. pub async fn new(node: SceneNodeWeak, ex: ExecutorPtr) -> Pimpl {
  43. debug!(target: "ui::button", "Button::new()");
  44. let node_ref = &node.upgrade().unwrap();
  45. let is_active = PropertyBool::wrap(node_ref, Role::Internal, "is_active", 0).unwrap();
  46. let rect = PropertyRect::wrap(node_ref, Role::Internal, "rect").unwrap();
  47. let z_index = PropertyUint32::wrap(node_ref, Role::Internal, "z_index", 0).unwrap();
  48. let priority = PropertyUint32::wrap(node_ref, Role::Internal, "priority", 0).unwrap();
  49. let self_ = Arc::new(Self {
  50. node,
  51. is_active,
  52. rect,
  53. z_index,
  54. priority,
  55. mouse_btn_held: AtomicBool::new(false),
  56. });
  57. Pimpl::Button(self_)
  58. }
  59. }
  60. #[async_trait]
  61. impl UIObject for Button {
  62. fn priority(&self) -> u32 {
  63. self.priority.get()
  64. }
  65. async fn draw(&self, parent_rect: Rectangle) -> Option<DrawUpdate> {
  66. let _ = self.rect.eval(&parent_rect);
  67. None
  68. }
  69. async fn handle_mouse_btn_down(&self, btn: MouseButton, mouse_pos: Point) -> bool {
  70. if !self.is_active.get() {
  71. return false
  72. }
  73. if btn != MouseButton::Left {
  74. return false
  75. }
  76. let rect = self.rect.get();
  77. if !rect.contains(mouse_pos) {
  78. return false
  79. }
  80. self.mouse_btn_held.store(true, Ordering::Relaxed);
  81. true
  82. }
  83. async fn handle_mouse_btn_up(&self, btn: MouseButton, mouse_pos: Point) -> bool {
  84. if !self.is_active.get() {
  85. return false
  86. }
  87. if btn != MouseButton::Left {
  88. return false
  89. }
  90. // Did we start the click inside the button?
  91. let btn_held = self.mouse_btn_held.swap(false, Ordering::Relaxed);
  92. if !btn_held {
  93. return false
  94. }
  95. // Are we releasing the click inside the button?
  96. let rect = self.rect.get();
  97. if !rect.contains(mouse_pos) {
  98. return false
  99. }
  100. debug!(target: "ui::button", "Button clicked!");
  101. let node = self.node.upgrade().unwrap();
  102. node.trigger("click", vec![]).await.unwrap();
  103. true
  104. }
  105. async fn handle_touch(&self, phase: TouchPhase, id: u64, touch_pos: Point) -> bool {
  106. if !self.is_active.get() {
  107. return false
  108. }
  109. // Ignore multi-touch
  110. if id != 0 {
  111. return false
  112. }
  113. let rect = self.rect.get();
  114. if !rect.contains(touch_pos) {
  115. //debug!(target: "ui::chatview", "not inside rect");
  116. return false
  117. }
  118. // Simulate mouse events
  119. match phase {
  120. TouchPhase::Started => self.handle_mouse_btn_down(MouseButton::Left, touch_pos).await,
  121. TouchPhase::Moved => false,
  122. TouchPhase::Ended => self.handle_mouse_btn_up(MouseButton::Left, touch_pos).await,
  123. TouchPhase::Cancelled => false,
  124. }
  125. }
  126. }