doukutsu-rs/src/input/touch_controls.rs

155 lines
4.6 KiB
Rust
Raw Normal View History

2020-11-28 19:25:51 +00:00
use crate::common::Rect;
2020-10-20 20:45:56 +00:00
use crate::engine_constants::EngineConstants;
2021-02-24 08:28:47 +00:00
use crate::framework::context::Context;
use crate::framework::error::GameResult;
use crate::framework::graphics::screen_insets_scaled;
2022-11-19 17:20:03 +00:00
use crate::graphics::texture_set::TextureSet;
2020-10-20 20:45:56 +00:00
2020-12-20 20:57:17 +00:00
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum TouchControlType {
None,
Dialog,
Controls,
}
#[derive(Copy, Clone)]
pub struct TouchPoint {
2021-02-24 08:28:47 +00:00
pub id: u64,
pub touch_id: u64,
pub position: (f64, f64),
pub last_position: (f64, f64),
2020-10-20 20:45:56 +00:00
}
pub struct TouchControls {
2020-12-20 20:57:17 +00:00
pub control_type: TouchControlType,
pub points: Vec<TouchPoint>,
pub interact_icon: bool,
2021-02-24 08:28:47 +00:00
pub touch_id_counter: u64,
pub clicks: Vec<TouchPoint>,
2020-10-20 20:45:56 +00:00
}
impl TouchControls {
pub fn new() -> TouchControls {
TouchControls {
2020-12-20 20:57:17 +00:00
control_type: TouchControlType::None,
2020-10-20 20:45:56 +00:00
points: Vec::with_capacity(8),
2021-02-24 08:28:47 +00:00
interact_icon: false,
touch_id_counter: 0,
2020-12-20 20:57:17 +00:00
clicks: Vec::with_capacity(8),
2020-10-20 20:45:56 +00:00
}
}
2020-11-28 19:25:51 +00:00
2020-12-20 20:57:17 +00:00
pub fn point_in(&self, bounds: Rect) -> Option<u64> {
2022-01-17 22:29:30 +00:00
for point in &self.points {
2020-12-20 20:57:17 +00:00
if (point.position.0 as isize) > bounds.left
&& (point.position.0 as isize) < bounds.right
&& (point.position.1 as isize) > bounds.top
2021-02-24 08:28:47 +00:00
&& (point.position.1 as isize) < bounds.bottom
{
2020-12-20 20:57:17 +00:00
return Some(point.touch_id);
}
}
None
}
pub fn consume_click_in(&mut self, bounds: Rect) -> bool {
self.clicks.retain(|p| p.touch_id != 0);
2022-01-17 22:29:30 +00:00
for point in &mut self.clicks {
2020-12-20 20:57:17 +00:00
if (point.position.0 as isize) > bounds.left
&& (point.position.0 as isize) < bounds.right
&& (point.position.1 as isize) > bounds.top
2021-02-24 08:28:47 +00:00
&& (point.position.1 as isize) < bounds.bottom
{
2020-12-20 20:57:17 +00:00
point.touch_id = 0;
return true;
2020-10-20 20:45:56 +00:00
}
}
2020-12-20 20:57:17 +00:00
false
2020-10-20 20:45:56 +00:00
}
2021-02-24 08:28:47 +00:00
pub fn draw(
&self,
canvas_size: (f32, f32),
scale: f32,
constants: &EngineConstants,
texture_set: &mut TextureSet,
ctx: &mut Context,
) -> GameResult {
2022-05-02 22:54:57 +00:00
let color = (255, 255, 255, 160);
let (left, top, right, bottom) = screen_insets_scaled(ctx, scale);
match self.control_type {
TouchControlType::None => {}
TouchControlType::Dialog => {
2022-05-02 23:34:16 +00:00
let batch = texture_set.get_or_load_batch(ctx, constants, "builtin/touch")?;
2022-05-02 22:54:57 +00:00
// Fast-Forward
batch.add_rect_tinted(
canvas_size.0 - (4.0 + 48.0) + 8.0 - right,
4.0 + 8.0 + top,
color,
&Rect::new_size(2 * 32, 3 * 32, 32, 32),
);
batch.draw(ctx)?;
}
TouchControlType::Controls => {
2022-05-02 23:34:16 +00:00
let batch = texture_set.get_or_load_batch(ctx, constants, "builtin/touch")?;
2022-05-02 22:54:57 +00:00
// Movement
for x in 0..3 {
for y in 0..3 {
let mut icon_x = x;
let icon_y = y;
if self.interact_icon && x == 1 && y == 2 {
icon_x = 3;
}
batch.add_rect_tinted(
4.0 + 48.0 * x as f32 + 8.0 + left,
(canvas_size.1 - 4.0 - 48.0 * 3.0) + 48.0 * y as f32 + 8.0 - bottom,
color,
&Rect::new_size(icon_x * 32, icon_y * 32, 32, 32),
);
2020-12-20 20:57:17 +00:00
}
}
2022-05-02 22:54:57 +00:00
// Jump
batch.add_rect_tinted(
canvas_size.0 - (4.0 + 48.0) + 8.0 - right,
canvas_size.1 - (4.0 + 48.0) + 8.0 - bottom,
color,
&Rect::new_size(3 * 32, 32, 32, 32),
);
// Shoot
batch.add_rect_tinted(
canvas_size.0 - (4.0 + 48.0) + 8.0 - right,
canvas_size.1 - (4.0 + 48.0) * 2.0 + 8.0 - bottom,
color,
&Rect::new_size(3 * 32, 0, 32, 32),
);
// Inventory
batch.add_rect_tinted(
canvas_size.0 - (4.0 + 48.0) + 8.0 - right,
4.0 + 8.0 + top,
color,
&Rect::new_size(0, 3 * 32, 32, 32),
);
// Pause
batch.add_rect_tinted(4.0, 4.0, color, &Rect::new_size(32, 3 * 32, 32, 32));
batch.draw(ctx)?;
}
2020-12-20 20:57:17 +00:00
}
2020-10-20 20:45:56 +00:00
Ok(())
}
}