Просмотр исходного кода

wallet: input events are now relative to the layer which emits them

darkfi 1 год назад
Родитель
Сommit
9e8da9b3a6
2 измененных файлов с 23 добавлено и 6 удалено
  1. 13 1
      bin/darkwallet/src/gfx/linalg.rs
  2. 10 5
      bin/darkwallet/src/ui/layer.rs

+ 13 - 1
bin/darkwallet/src/gfx/linalg.rs

@@ -16,7 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use std::ops::{Add, Sub};
+use std::ops::{Add, AddAssign, Sub, SubAssign};
 
 #[derive(Clone, Copy, Debug)]
 pub struct Point {
@@ -68,6 +68,18 @@ impl Sub for Point {
     }
 }
 
+impl AddAssign for Point {
+    fn add_assign(&mut self, other: Self) {
+        *self = Self { x: self.x + other.x, y: self.y + other.y };
+    }
+}
+
+impl SubAssign for Point {
+    fn sub_assign(&mut self, other: Self) {
+        *self = Self { x: self.x - other.x, y: self.y - other.y };
+    }
+}
+
 #[derive(Debug, Clone)]
 pub struct Rectangle {
     pub x: f32,

+ 10 - 5
bin/darkwallet/src/ui/layer.rs

@@ -221,8 +221,9 @@ impl UIObject for Layer {
         &self,
         sg: &SceneGraph,
         btn: MouseButton,
-        mouse_pos: Point,
+        mut mouse_pos: Point,
     ) -> bool {
+        mouse_pos -= self.rect.get().pos();
         for child_id in get_child_nodes_ordered(&sg, self.node_id) {
             let node = sg.get_node(child_id).unwrap();
             let obj = get_ui_object(node);
@@ -236,8 +237,9 @@ impl UIObject for Layer {
         &self,
         sg: &SceneGraph,
         btn: MouseButton,
-        mouse_pos: Point,
+        mut mouse_pos: Point,
     ) -> bool {
+        mouse_pos -= self.rect.get().pos();
         for child_id in get_child_nodes_ordered(&sg, self.node_id) {
             let node = sg.get_node(child_id).unwrap();
             let obj = get_ui_object(node);
@@ -247,7 +249,8 @@ impl UIObject for Layer {
         }
         false
     }
-    async fn handle_mouse_move(&self, sg: &SceneGraph, mouse_pos: Point) -> bool {
+    async fn handle_mouse_move(&self, sg: &SceneGraph, mut mouse_pos: Point) -> bool {
+        mouse_pos -= self.rect.get().pos();
         for child_id in get_child_nodes_ordered(&sg, self.node_id) {
             let node = sg.get_node(child_id).unwrap();
             let obj = get_ui_object(node);
@@ -257,7 +260,8 @@ impl UIObject for Layer {
         }
         false
     }
-    async fn handle_mouse_wheel(&self, sg: &SceneGraph, wheel_pos: Point) -> bool {
+    async fn handle_mouse_wheel(&self, sg: &SceneGraph, mut wheel_pos: Point) -> bool {
+        wheel_pos -= self.rect.get().pos();
         for child_id in get_child_nodes_ordered(&sg, self.node_id) {
             let node = sg.get_node(child_id).unwrap();
             let obj = get_ui_object(node);
@@ -272,8 +276,9 @@ impl UIObject for Layer {
         sg: &SceneGraph,
         phase: TouchPhase,
         id: u64,
-        touch_pos: Point,
+        mut touch_pos: Point,
     ) -> bool {
+        touch_pos -= self.rect.get().pos();
         for child_id in get_child_nodes_ordered(&sg, self.node_id) {
             let node = sg.get_node(child_id).unwrap();
             let obj = get_ui_object(node);