Skip to content

Commit

Permalink
feat: move scene when cursor in border
Browse files Browse the repository at this point in the history
  • Loading branch information
buxx committed Aug 23, 2023
1 parent 00fe266 commit 3b4950a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
15 changes: 15 additions & 0 deletions battle_gui/src/engine/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ggez::{event::MouseButton, input::keyboard::KeyInput, winit::event::VirtualK
use crate::{
debug::DebugPhysics,
engine::{event::UIEvent, message::GuiStateMessage},
ui::BORDER_MOVE_FACTOR,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -59,6 +60,20 @@ impl Engine {
UIEvent::ImmobileCursorSince(cursor_immobile_since),
)));

if !self.gui_state.pending_order().is_empty() || self.gui_state.dragged_squad().is_some() {
if let Some(border) =
self.point_in_border(ctx, self.gui_state.current_cursor_window_point())
{
let (x, y) = border.modifier();
messages.push(EngineMessage::GuiState(
GuiStateMessage::ApplyOnDisplaySceneOffset(Offset::new(
-x as f32 * BORDER_MOVE_FACTOR,
-y as f32 * BORDER_MOVE_FACTOR,
)),
));
}
}

messages
}

Expand Down
36 changes: 35 additions & 1 deletion battle_gui/src/engine/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ use battle_core::{
game::{cover::CoverFinder, health::SoldierHealthBuilder},
graphics::vehicle::VehicleGraphicInfos,
order::{Order, PendingOrder},
physics::path::Direction,
state::battle::message::{BattleStateMessage, SoldierMessage, VehicleMessage},
types::*,
utils::DebugPoint,
};

use crate::{
engine::event::UIEvent,
ui::{color::Colorized, menu::squad_menu_sprite_info},
ui::{color::Colorized, hud::HUD_HEIGHT, menu::squad_menu_sprite_info, BORDER_SIZE},
utils::GREEN,
};

Expand Down Expand Up @@ -579,4 +580,37 @@ impl Engine {

messages
}

pub fn point_in_border(&self, ctx: &Context, point: &WindowPoint) -> Option<Direction> {
let (width, height) = ctx.gfx.drawable_size();
let height = height - HUD_HEIGHT;

if point.x <= BORDER_SIZE && point.y <= BORDER_SIZE {
return Some(Direction::NorthWest);
}
if point.x >= width - BORDER_SIZE && point.y <= BORDER_SIZE {
return Some(Direction::NorthEst);
}
if point.x >= width - BORDER_SIZE && point.y >= height - BORDER_SIZE {
return Some(Direction::SouthEst);
}
if point.x <= BORDER_SIZE && point.y >= height - BORDER_SIZE {
return Some(Direction::SouthWest);
}

if point.y <= BORDER_SIZE {
return Some(Direction::North);
}
if point.y >= height - BORDER_SIZE {
return Some(Direction::South);
}
if point.x <= BORDER_SIZE {
return Some(Direction::West);
}
if point.x >= width - BORDER_SIZE {
return Some(Direction::Est);
}

None
}
}
3 changes: 3 additions & 0 deletions battle_gui/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ pub mod component;
pub mod health;
pub mod hud;
pub mod menu;

pub const BORDER_SIZE: f32 = 30.;
pub const BORDER_MOVE_FACTOR: f32 = 3.;

0 comments on commit 3b4950a

Please sign in to comment.