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::{
Background, Gsa, Rgb, BACKGROUND_MAX_SIZE, EMPTY_TILE, HALF_TILE_SIZE, MAX_BACKGROUNDS,
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) {
let p = tileset[(x as usize + tx) + (y as usize + ty) * (TILESET_SIZE * TILE_SIZE)];
if p != TRANSPARENT {
target[(x as usize + pos.x as usize)
+ (y as usize + pos.y as usize) * screen_size.x as usize] = p;
target[(x + pos.x) as usize + (y + pos.y) as usize * screen_size.x as usize] = p;
}
}
}

View File

@ -51,9 +51,11 @@ pub(crate) fn run_mapedit() {
window.request_redraw();
let mut mouse_pos = IVec2::ZERO;
let mut left_down = false;
let mut middle_down = false;
event_loop.run(move |event, _, control_flow| {
let mouse_pos = &mut mouse_pos;
let left_down = &mut left_down;
let middle_down = &mut middle_down;
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
@ -73,22 +75,37 @@ pub(crate) fn run_mapedit() {
(winit::event::ElementState::Released, winit::event::MouseButton::Left) => {
*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, .. } => {
*mouse_pos = IVec2 {
let new_pos = IVec2 {
x: position.x 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 => {
if *left_down {
let tile_x = mouse_pos.x as usize / TILE_SIZE;
let tile_y = mouse_pos.y as usize / TILE_SIZE;
gsa.bgs[0].tiles[tile_x][tile_y] = 0;
let tile_x = (mouse_pos.x + gsa.bgs[0].scroll.x) 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;
}
}
// render