Skip to content

Commit b00661d

Browse files
committed
hook up hover events
1 parent 0d9e297 commit b00661d

File tree

3 files changed

+71
-17
lines changed

3 files changed

+71
-17
lines changed

crates/unavi-player/src/input.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use avian3d::prelude::*;
22
use bevy::input::keyboard::KeyCode;
33
use bevy::prelude::*;
4-
use unavi_scripting::api::wired::input::input_handler::{InputHandlerSender, ScriptInputEvent};
4+
use unavi_scripting::api::wired::input::{
5+
bindings::types::InputAction,
6+
input_handler::{InputHandlerSender, ScriptInputEvent},
7+
};
58

69
use crate::{
710
layers::{OTHER_PLAYER_LAYER, WORLD_LAYER},
@@ -95,7 +98,7 @@ pub fn handle_raycast_input(
9598
},
9699
) {
97100
// Crosshair.
98-
// TODO: Showing double on non-60hz monitors
101+
// TODO: Fix showing double on non-60hz monitors
99102
if let Ok(normal) = Dir3::from_xyz(hit.normal.x, hit.normal.y, hit.normal.z) {
100103
gizmos.circle(
101104
translation + direction * (hit.time_of_impact - 0.001),
@@ -106,19 +109,24 @@ pub fn handle_raycast_input(
106109
}
107110

108111
// Script input.
109-
if mouse.just_pressed(MouseButton::Left) {
110-
for (ent, handler) in input_handlers.iter() {
111-
// TODO: Recursive check if children were hit.
112+
for (ent, handler) in input_handlers.iter() {
113+
// TODO: Recursive check if children were hit.
114+
115+
let action = if mouse.just_pressed(MouseButton::Left) {
116+
InputAction::Collision
117+
} else {
118+
InputAction::Hover
119+
};
112120

113-
if hit.entity == ent {
114-
if let Err(e) = handler.send(ScriptInputEvent::Raycast {
115-
origin: translation,
116-
orientation: rotation,
117-
}) {
118-
error!("Failed to send script input event: {}", e);
119-
};
120-
break;
121-
}
121+
if hit.entity == ent {
122+
if let Err(e) = handler.send(ScriptInputEvent::Raycast {
123+
action,
124+
origin: translation,
125+
orientation: rotation,
126+
}) {
127+
error!("Failed to send script input event: {}", e);
128+
};
129+
break;
122130
}
123131
}
124132
};

crates/unavi-scripting/src/api/wired/input/input_handler.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use super::bindings::{
1616

1717
pub enum ScriptInputEvent {
1818
Raycast {
19+
action: InputAction,
1920
origin: bevy::math::Vec3,
2021
orientation: bevy::math::Quat,
2122
},
@@ -71,11 +72,12 @@ impl HostInputHandler for StoreState {
7172
if let Ok(event) = data.receiver.try_recv() {
7273
let e = match event {
7374
ScriptInputEvent::Raycast {
75+
action,
7476
origin,
7577
orientation,
7678
} => InputEvent {
77-
id: 0,
78-
action: InputAction::Collision,
79+
id: 0, // TODO: Set id
80+
action,
7981
data: InputData::Ray(Ray {
8082
origin: origin.into(),
8183
orientation: orientation.into(),

wasm/unavi-ui/src/button.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use crate::{
44
bindings::{
55
exports::unavi::ui::button::{Guest, GuestButton},
66
unavi::{layout::container::Container, shapes::api::Cuboid},
7-
wired::input::{handler::InputHandler, types::InputAction},
7+
wired::{
8+
input::{handler::InputHandler, types::InputAction},
9+
scene::material::{Color, Material},
10+
},
811
},
912
GuestImpl, Updatable, ELEMENTS, ELEMENT_ID,
1013
};
@@ -23,8 +26,14 @@ impl Drop for Button {
2326

2427
pub struct ButtonData {
2528
id: usize,
29+
30+
color: Color,
31+
color_hover: Color,
32+
2633
input: InputHandler,
34+
material: Material,
2735
root: Container,
36+
2837
hovered: Cell<bool>,
2938
pressed: Cell<bool>,
3039
}
@@ -35,6 +44,7 @@ impl Updatable for ButtonData {
3544
}
3645

3746
fn update(&self, _delta: f32) {
47+
// Handle input.
3848
self.hovered.set(false);
3949
self.pressed.set(false);
4050

@@ -44,9 +54,18 @@ impl Updatable for ButtonData {
4454
InputAction::Collision => self.pressed.set(true),
4555
}
4656
}
57+
58+
// Animation.
59+
if self.hovered.get() {
60+
self.material.set_color(self.color_hover);
61+
} else {
62+
self.material.set_color(self.color);
63+
}
4764
}
4865
}
4966

67+
const DARKEN_PERCENT: f32 = 0.5;
68+
5069
impl GuestButton for Button {
5170
fn new(root: Container) -> Self {
5271
let size = root.size();
@@ -55,12 +74,37 @@ impl GuestButton for Button {
5574
let input = InputHandler::new();
5675
node.set_input_handler(Some(&input));
5776

77+
let material = Material::new();
78+
for primitive in node.mesh().unwrap().list_primitives() {
79+
primitive.set_material(Some(&material));
80+
}
81+
5882
root.inner().add_child(&node);
5983

84+
let color = Color {
85+
r: 0.2,
86+
g: 0.7,
87+
b: 1.0,
88+
a: 1.0,
89+
};
90+
91+
let color_hover = Color {
92+
r: color.r * DARKEN_PERCENT,
93+
g: color.g * DARKEN_PERCENT,
94+
b: color.b * DARKEN_PERCENT,
95+
a: color.a,
96+
};
97+
6098
let data = Rc::new(ButtonData {
6199
id: ELEMENT_ID.fetch_add(1, Ordering::Relaxed),
100+
101+
color,
102+
color_hover,
103+
62104
input,
105+
material,
63106
root,
107+
64108
hovered: Cell::default(),
65109
pressed: Cell::default(),
66110
});

0 commit comments

Comments
 (0)