Skip to content

Commit

Permalink
faster buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Nertsal committed May 30, 2024
1 parent b2331e0 commit d6bfd5b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ impl geng::State for Game {
geng::Event::CursorMove { position } => {
self.ui_context.cursor.cursor_move(position.as_f32());
}
geng::Event::MousePress {
button: geng::MouseButton::Left,
} => self.model.cursor_clicked = true,
geng::Event::TouchStart(touch) if self.active_touch.is_none() => {
self.active_touch = Some(touch.id);
}
Expand Down Expand Up @@ -197,5 +200,6 @@ impl geng::State for Game {
.screen_to_world(game_pos.size(), pos)
.as_r32();
self.model.update(target_pos, delta_time);
self.model.cursor_clicked = false;
}
}
3 changes: 3 additions & 0 deletions src/menu/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@ impl geng::State for LevelMenu {
.play_button
.base_collider
.contains(cursor_world.as_r32());
if hovering && self.ui_context.cursor.was_down {
self.play_button.clicked = true;
}
self.play_button.update(hovering, delta_time);
}
if self.play_button.hover_time.is_max() {
Expand Down
11 changes: 11 additions & 0 deletions src/menu/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct MainMenu {
framebuffer_size: vec2<usize>,
/// Cursor position in screen space.
cursor_pos: vec2<f64>,
/// Cursor clicked last frame.
clicked: bool,
active_touch: Option<u64>,
cursor_world_pos: vec2<Coord>,
camera: Camera2d,
Expand All @@ -39,6 +41,7 @@ impl MainMenu {
cursor_pos: vec2::ZERO,
active_touch: None,
cursor_world_pos: vec2::ZERO,
clicked: false,
camera: Camera2d {
center: vec2::ZERO,
rotation: Angle::ZERO,
Expand Down Expand Up @@ -97,13 +100,18 @@ impl geng::State for MainMenu {
self.player.reset_distance();

let hovering = self.player.collider.check(&self.play_button.base_collider);
if hovering && self.clicked {
self.play_button.clicked = true;
}
self.play_button.update(hovering, delta_time);
self.player
.update_distance_simple(&self.play_button.base_collider);
if self.play_button.hover_time.is_max() {
self.play_button.hover_time.set_ratio(Time::ZERO);
self.play();
}

self.clicked = false;
}

fn fixed_update(&mut self, delta_time: f64) {
Expand All @@ -116,6 +124,9 @@ impl geng::State for MainMenu {
geng::Event::CursorMove { position } => {
self.cursor_pos = position;
}
geng::Event::MousePress {
button: geng::MouseButton::Left,
} => self.clicked = true,
geng::Event::TouchStart(touch) if self.active_touch.is_none() => {
self.active_touch = Some(touch.id);
}
Expand Down
6 changes: 6 additions & 0 deletions src/model/logic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ impl Model {
.restart_button
.base_collider
.check(&self.player.collider);
if hovering && self.cursor_clicked {
self.restart_button.clicked = true;
}
self.restart_button.update(hovering, delta_time);
self.player
.update_distance_simple(&self.restart_button.base_collider);
Expand All @@ -159,6 +162,9 @@ impl Model {

// 1 second before the UI is active
let hovering = self.exit_button.base_collider.check(&self.player.collider);
if hovering && self.cursor_clicked {
self.exit_button.clicked = true;
}
self.exit_button.update(hovering, delta_time);
self.player
.update_distance_simple(&self.exit_button.base_collider);
Expand Down
22 changes: 17 additions & 5 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct HoverButton {
pub base_collider: Collider,
pub hover_time: Lifetime,
pub animation: Movement,
pub clicked: bool,
}

impl HoverButton {
Expand All @@ -36,21 +37,29 @@ impl HoverButton {
key_frames: vec![MoveFrame::scale(0.5, 5.0), MoveFrame::scale(0.25, 75.0)].into(),
fade_out: r32(0.2),
},
clicked: false,
}
}

/// Whether is button is now fading, i.e. going to finish its animation regardless of input.
pub fn is_fading(&self) -> bool {
// TODO: more custom
self.hover_time.get_ratio().as_f32() > 0.5
self.hover_time.get_ratio().as_f32() > 0.6
}

pub fn update(&mut self, hovering: bool, delta_time: Time) {
self.hover_time.change(if self.is_fading() || hovering {
delta_time
let scale = if self.is_fading() {
self.clicked = false;
1.0
} else if self.clicked {
3.0
} else if hovering {
1.0
} else {
-delta_time
});
-1.0
};
let dt = r32(scale) * delta_time;
self.hover_time.change(dt);
}
}

Expand Down Expand Up @@ -94,6 +103,8 @@ pub struct Model {
pub high_score: i32,
pub camera: Camera2d,
pub player: Player,
/// Whether the cursor clicked last frame.
pub cursor_clicked: bool,

pub options: Options,
/// The level being played. Not changed, apart from music being played.
Expand Down Expand Up @@ -163,6 +174,7 @@ impl Model {
),
level.config.health.max,
),
cursor_clicked: false,

level_state: LevelState::default(),
state: State::Starting {
Expand Down

0 comments on commit d6bfd5b

Please sign in to comment.