Skip to content

Commit

Permalink
Add moved and pressed-changed callback to TouchArea
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Oct 1, 2021
1 parent dc10916 commit 0aecece
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- New `Dialog` element
- `sixtyfps::Image` has now a `path()` accessor function in Rust and C++ to access the optional path
of the file on disk that's backing the image.
- New `moved` and `pressed-changed` callback in `TouchArea`

### Fixed

Expand Down
6 changes: 4 additions & 2 deletions docs/builtin_elements.md
Expand Up @@ -141,7 +141,7 @@ An Image can be used to represent an image loaded from an image file.
* **`image-rendering`** (*enum*): Specifies how the source image will be scaled. Possible values are:
* `smooth`: The image is scaled with a linear interpolation algorithm.
* `pixelated`: The image is scaled with the nearest neighbor algorithm.

The default value is `smooth`.

* **`colorize`** (*brush*): When set, the image is used as an alpha mask and is drown in the given color (or with the gradient)
Expand Down Expand Up @@ -421,7 +421,9 @@ When not part of a layout, its width or height default to 100% of the parent ele

### Callbacks

* **`clicked`**: Emitted when the mouse is released
* **`clicked`**: Emitted when clicked (the mouse is pressed, then released on this element)
* **`moved`**: The mouse has been moved. This will only be called if the mouse is also pressed.
* **`pressed-changed`**: The value of the `pressed` property changed.

### Example

Expand Down
2 changes: 2 additions & 0 deletions sixtyfps_compiler/builtins.60
Expand Up @@ -109,6 +109,8 @@ export TouchArea := _ {
property <length> pressed_x: native_output;
property <length> pressed_y: native_output;
callback clicked;
callback moved;
callback pressed-changed;
//-default_size_binding:expands_to_parent_geometry
}

Expand Down
24 changes: 19 additions & 5 deletions sixtyfps_runtime/corelib/items.rs
Expand Up @@ -350,6 +350,8 @@ pub struct TouchArea {
pub mouse_x: Property<f32>,
pub mouse_y: Property<f32>,
pub clicked: Callback<VoidArg>,
pub moved: Callback<VoidArg>,
pub pressed_changed: Callback<VoidArg>,
/// FIXME: remove this
pub cached_rendering_data: CachedRenderingData,
}
Expand Down Expand Up @@ -407,21 +409,33 @@ impl Item for TouchArea {
InputEventResult::GrabMouse
};

Self::FIELD_OFFSETS.pressed.apply_pin(self).set(match event {
match event {
MouseEvent::MousePressed { pos } => {
Self::FIELD_OFFSETS.pressed_x.apply_pin(self).set(pos.x);
Self::FIELD_OFFSETS.pressed_y.apply_pin(self).set(pos.y);
true
Self::FIELD_OFFSETS.pressed.apply_pin(self).set(true);
Self::FIELD_OFFSETS.pressed_changed.apply_pin(self).call(&());
}
MouseEvent::MouseExit | MouseEvent::MouseReleased { .. } => {
Self::FIELD_OFFSETS.pressed.apply_pin(self).set(false);
Self::FIELD_OFFSETS.pressed_changed.apply_pin(self).call(&());
}
MouseEvent::MouseExit | MouseEvent::MouseReleased { .. } => false,
MouseEvent::MouseMoved { .. } | MouseEvent::MouseWheel { .. } => {
MouseEvent::MouseMoved { .. } => {
return if self.pressed() {
Self::FIELD_OFFSETS.moved.apply_pin(self).call(&());
InputEventResult::GrabMouse
} else {
InputEventResult::EventAccepted
}
}
});
MouseEvent::MouseWheel { .. } => {
return if self.pressed() {
InputEventResult::GrabMouse
} else {
InputEventResult::EventAccepted
}
}
};
result
}

Expand Down

0 comments on commit 0aecece

Please sign in to comment.