middle click scrolling in map editor

This commit is contained in:
dani 2023-08-04 23:52:04 +00:00
parent 546fcef57a
commit 6d6d03db37
2 changed files with 24 additions and 6 deletions

View File

@ -1,3 +1,5 @@
use std::usize;
use crate::{ use crate::{
Background, Gsa, Rgb, BACKGROUND_MAX_SIZE, EMPTY_TILE, HALF_TILE_SIZE, MAX_BACKGROUNDS, Background, Gsa, Rgb, BACKGROUND_MAX_SIZE, EMPTY_TILE, HALF_TILE_SIZE, MAX_BACKGROUNDS,
SCREEN_HEIGHT, SCREEN_WIDTH, TILESET_SIZE, TILE_SIZE, TRANSPARENT, SCREEN_HEIGHT, SCREEN_WIDTH, TILESET_SIZE, TILE_SIZE, TRANSPARENT,
@ -24,8 +26,7 @@ fn draw_tile(
for x in (startx - pos.x)..(endx - pos.x) { for x in (startx - pos.x)..(endx - pos.x) {
let p = tileset[(x as usize + tx) + (y as usize + ty) * (TILESET_SIZE * TILE_SIZE)]; let p = tileset[(x as usize + tx) + (y as usize + ty) * (TILESET_SIZE * TILE_SIZE)];
if p != TRANSPARENT { if p != TRANSPARENT {
target[(x as usize + pos.x as usize) target[(x + pos.x) as usize + (y + pos.y) as usize * screen_size.x as usize] = p;
+ (y as usize + pos.y as usize) * screen_size.x as usize] = p;
} }
} }
} }

View File

@ -51,9 +51,11 @@ pub(crate) fn run_mapedit() {
window.request_redraw(); window.request_redraw();
let mut mouse_pos = IVec2::ZERO; let mut mouse_pos = IVec2::ZERO;
let mut left_down = false; let mut left_down = false;
let mut middle_down = false;
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
let mouse_pos = &mut mouse_pos; let mouse_pos = &mut mouse_pos;
let left_down = &mut left_down; let left_down = &mut left_down;
let middle_down = &mut middle_down;
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
@ -73,23 +75,38 @@ pub(crate) fn run_mapedit() {
(winit::event::ElementState::Released, winit::event::MouseButton::Left) => { (winit::event::ElementState::Released, winit::event::MouseButton::Left) => {
*left_down = false; *left_down = false;
} }
(winit::event::ElementState::Pressed, winit::event::MouseButton::Middle) => {
*middle_down = true;
}
(winit::event::ElementState::Released, winit::event::MouseButton::Middle) => {
*middle_down = false;
}
_ => {} _ => {}
}, },
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
*mouse_pos = IVec2 { let new_pos = IVec2 {
x: position.x as i32, x: position.x as i32,
y: position.y as i32, y: position.y as i32,
}; };
let delta = new_pos - *mouse_pos;
if *middle_down {
gsa.bgs[0].scroll -= delta;
gsa.bgs[1].scroll -= delta;
gsa.bgs[2].scroll -= delta;
}
*mouse_pos = new_pos;
} }
_ => {} _ => {}
}, },
Event::MainEventsCleared => { Event::MainEventsCleared => {
if *left_down { if *left_down {
let tile_x = mouse_pos.x as usize / TILE_SIZE; let tile_x = (mouse_pos.x + gsa.bgs[0].scroll.x) as usize / TILE_SIZE;
let tile_y = mouse_pos.y as usize / TILE_SIZE; let tile_y = (mouse_pos.y + gsa.bgs[0].scroll.y) as usize / TILE_SIZE;
if tile_x < BACKGROUND_MAX_SIZE && tile_y < BACKGROUND_MAX_SIZE {
gsa.bgs[0].tiles[tile_x][tile_y] = 0; gsa.bgs[0].tiles[tile_x][tile_y] = 0;
} }
}
// render // render
let size = window.inner_size(); let size = window.inner_size();