Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hightlight a tile under the cursor #469

Merged
merged 3 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ impl event::EventHandler for MainState {
.expect("Can't handle click event");
}

fn mouse_motion_event(&mut self, context: &mut Context, x: f32, y: f32, dx: f32, dy: f32) {
if dx.abs() < 1.0 && dy.abs() < 1.0 {
// Don't do anything on touch devices
return;
}
let window_pos = Point2::new(x, y);
let pos = ui::window_to_screen(context, window_pos);
self.screens
.move_mouse(context, pos)
.expect("Can't move the mouse");
}

// This functions just overrides the default implementation,
// because we don't want to quit from the game on `Esc`.
#[cfg(not(target_arch = "wasm32"))] // <- we cant quit in wasm anyway
Expand Down
8 changes: 8 additions & 0 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub trait Screen: Debug {
fn draw(&self, context: &mut Context) -> ZResult;
fn click(&mut self, context: &mut Context, pos: Point2<f32>) -> ZResult<Transition>;
fn resize(&mut self, aspect_ratio: f32);

fn move_mouse(&mut self, _context: &mut Context, _pos: Point2<f32>) -> ZResult {
Ok(())
}
}

const ERR_MSG: &str = "Screen stack is empty";
Expand Down Expand Up @@ -60,6 +64,10 @@ impl Screens {
self.handle_command(context, command)
}

pub fn move_mouse(&mut self, context: &mut Context, pos: Point2<f32>) -> ZResult {
self.screen_mut().move_mouse(context, pos)
}

pub fn resize(&mut self, aspect_ratio: f32) {
for screen in &mut self.screens {
screen.resize(aspect_ratio);
Expand Down
10 changes: 10 additions & 0 deletions src/screen/battle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,14 @@ impl Screen for Battle {
}
Ok(Transition::None)
}

fn move_mouse(&mut self, _context: &mut Context, point: Point2<f32>) -> ZResult {
let pos = geom::point_to_hex(self.view.tile_size(), point);
if self.state.map().is_inboard(pos) {
self.view.show_current_tile_marker(pos);
} else {
self.view.hide_current_tile_marker();
}
Ok(())
}
}
40 changes: 33 additions & 7 deletions src/screen/battle/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Layers {
pub grass: Layer,
pub highlighted_tiles: Layer,
pub selection_marker: Layer,
pub current_tile_marker: Layer,
pub particles: Layer,
pub objects: Layer,
pub dots: Layer,
Expand All @@ -55,6 +56,7 @@ impl Layers {
self.grass,
self.highlighted_tiles,
self.selection_marker,
self.current_tile_marker,
self.particles,
self.objects,
self.dots,
Expand All @@ -81,6 +83,7 @@ struct DisappearingSprite {
#[derive(Debug)]
struct Sprites {
selection_marker: Sprite,
current_tile_marker: Sprite,
highlighted_tiles: Vec<Sprite>,
labels: Vec<Sprite>,
id_to_sprite_map: HashMap<ObjId, Sprite>,
Expand Down Expand Up @@ -142,15 +145,18 @@ impl BattleView {
let scene = Scene::new(layers.clone().sorted());
let map_diameter = map::radius_to_diameter(map_radius);
let tile_size = tile_size(map_diameter);
let mut selection_marker = Sprite::from_image(
context,
images.selection.clone(),
tile_size * 2.0 * geom::FLATNESS_COEFFICIENT,
)?;
selection_marker.set_centered(true);
selection_marker.set_color([0.0, 0.0, 1.0, 0.8].into());
let mut make_marker_sprite = |color: Color| -> ZResult<Sprite> {
let h = tile_size * 2.0 * geom::FLATNESS_COEFFICIENT;
let mut sprite = Sprite::from_image(context, images.selection.clone(), h)?;
sprite.set_centered(true);
sprite.set_color(color);
Ok(sprite)
};
let selection_marker = make_marker_sprite([0.0, 0.0, 1.0, 0.8].into())?;
let current_tile_marker = make_marker_sprite([0.0, 0.0, 0.0, 0.5].into())?;
let sprites = Sprites {
selection_marker,
current_tile_marker,
highlighted_tiles: Vec::new(),
labels: Vec::new(),
id_to_sprite_map: HashMap::new(),
Expand Down Expand Up @@ -317,6 +323,26 @@ impl BattleView {
}
}

pub fn hide_current_tile_marker(&mut self) {
let layer = &self.layers.current_tile_marker;
let sprite = &mut self.sprites.current_tile_marker;
if layer.has_sprite(sprite) {
let hide_marker = action::Hide::new(layer, sprite).boxed();
self.scene.add_action(hide_marker);
}
}

pub fn show_current_tile_marker(&mut self, pos: PosHex) {
let point = hex_to_point(self.tile_size(), pos);
let layer = &self.layers.current_tile_marker;
let sprite = &mut self.sprites.current_tile_marker;
sprite.set_pos(point);
if !layer.has_sprite(sprite) {
let action = action::Show::new(layer, sprite).boxed();
self.scene.add_action(action);
}
}

pub fn deselect(&mut self) {
self.hide_selection_marker();
self.remove_highlights();
Expand Down
2 changes: 1 addition & 1 deletion utils/wasm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

cp -r assets static
cp utils/wasm/index.html static
ls static > static/index.txt
ls static | sed 's:^:/:' > static/index.txt
cargo web build