select tiles in map edit

This commit is contained in:
dani 2023-08-05 14:14:18 +00:00
parent 365f30e893
commit aeab78e365
4 changed files with 123 additions and 29 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View File

@ -9,6 +9,8 @@ pub struct Background {
pub scroll: IVec2, pub scroll: IVec2,
/// Are tiles indices half-tile indices? /// Are tiles indices half-tile indices?
pub half_tile: bool, pub half_tile: bool,
/// Is this background being displayed?
pub active: bool,
} }
impl Default for Background { impl Default for Background {
@ -19,6 +21,7 @@ impl Default for Background {
tiles, tiles,
scroll: IVec2::ZERO, scroll: IVec2::ZERO,
half_tile: false, half_tile: false,
active: true,
} }
} }
} }

View File

@ -69,7 +69,9 @@ fn render_map(target: &mut [u8], map: &Background, tileset: &[u8], screen_size:
pub(crate) fn render_to_screen(target: &mut [u8], gsa: &Gsa, tileset: &[u8], screen_size: IVec2) { pub(crate) fn render_to_screen(target: &mut [u8], gsa: &Gsa, tileset: &[u8], screen_size: IVec2) {
for i in 0..MAX_BACKGROUNDS { for i in 0..MAX_BACKGROUNDS {
render_map(target, &gsa.bgs[i], tileset, screen_size); if gsa.bgs[i].active {
render_map(target, &gsa.bgs[i], tileset, screen_size);
}
for sprite in &gsa.sprites { for sprite in &gsa.sprites {
if sprite.tile != EMPTY_TILE && sprite.priority == i as u8 { if sprite.tile != EMPTY_TILE && sprite.priority == i as u8 {
draw_tile(target, sprite.pos, sprite.tile, tileset, false, screen_size); draw_tile(target, sprite.pos, sprite.tile, tileset, false, screen_size);

View File

@ -17,6 +17,8 @@ use winit::{
}; };
const SPR_CURSOR: usize = 0x01; const SPR_CURSOR: usize = 0x01;
const TILE_CURSOR: u16 = 0x7308;
const TILE_MARKER: u16 = 0x7309;
pub(crate) fn run_mapedit() { pub(crate) fn run_mapedit() {
println!("running map edit"); println!("running map edit");
@ -36,7 +38,27 @@ pub(crate) fn run_mapedit() {
gsa.reset_bgs(); gsa.reset_bgs();
gsa.reset_sprites(); gsa.reset_sprites();
gsa.sprites[SPR_CURSOR].tile = 0x7308;
gsa.sprites[SPR_CURSOR].tile = TILE_CURSOR;
let mut gsa2 = Gsa {
sprites: [Sprite::default(); MAX_SPRITES],
palette,
bgs: Default::default(),
font: FONT_BOLD,
pressed: 0,
released: 0,
down: 0,
};
gsa2.reset_bgs();
gsa2.reset_sprites();
for y in 0..TILESET_SIZE {
for x in 0..TILESET_SIZE {
gsa2.bgs[0].tiles[x][y] = (x + (y << 8)) as u16;
}
}
gsa2.bgs[0].active = false;
gsa2.bgs[1].active = false;
let event_loop = EventLoop::new(); let event_loop = EventLoop::new();
let size = LogicalSize::new(1280, 720); let size = LogicalSize::new(1280, 720);
@ -54,26 +76,48 @@ 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 tile_pos = IVec2::ZERO; let mut tile_pos = IVec2::ZERO;
let mut tile_pos2 = IVec2::ZERO;
let mut left_down = false; let mut left_down = false;
let mut middle_down = false; let mut middle_down = false;
let mut right_down = false; let mut right_down = false;
let mut selected_tile = IVec2::ZERO;
gsa2.bgs[1].tiles[0][0] = TILE_MARKER;
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 tile_pos = &mut tile_pos; let tile_pos = &mut tile_pos;
let tile_pos2 = &mut tile_pos2;
let left_down = &mut left_down; let left_down = &mut left_down;
let middle_down = &mut middle_down; let middle_down = &mut middle_down;
let right_down = &mut right_down; let right_down = &mut right_down;
let selected_tile = &mut selected_tile;
match event { match event {
Event::WindowEvent { event, .. } => match event { Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput { input, .. } => {
input: match (input.state, input.virtual_keycode.unwrap()) {
KeyboardInput { (winit::event::ElementState::Pressed, VirtualKeyCode::Escape) => {
virtual_keycode: Some(VirtualKeyCode::Escape, ..), *control_flow = ControlFlow::Exit;
.. }
}, (winit::event::ElementState::Pressed, VirtualKeyCode::LShift) => {
.. gsa.bgs[0].active = false;
} => { gsa.bgs[1].active = false;
*control_flow = ControlFlow::Exit; gsa.bgs[2].active = false;
gsa2.bgs[0].active = true;
gsa2.bgs[1].active = true;
gsa.sprites[SPR_CURSOR].tile = EMPTY_TILE;
gsa2.sprites[SPR_CURSOR].tile = TILE_CURSOR;
}
(winit::event::ElementState::Released, VirtualKeyCode::LShift) => {
gsa.bgs[0].active = true;
gsa.bgs[1].active = true;
gsa.bgs[2].active = true;
gsa2.bgs[0].active = false;
gsa2.bgs[1].active = false;
gsa.sprites[SPR_CURSOR].tile = TILE_CURSOR;
gsa2.sprites[SPR_CURSOR].tile = EMPTY_TILE;
}
_ => {}
}
} }
WindowEvent::MouseInput { state, button, .. } => match (state, button) { WindowEvent::MouseInput { state, button, .. } => match (state, button) {
(winit::event::ElementState::Pressed, winit::event::MouseButton::Left) => { (winit::event::ElementState::Pressed, winit::event::MouseButton::Left) => {
@ -103,34 +147,70 @@ pub(crate) fn run_mapedit() {
}; };
let delta = new_pos - *mouse_pos; let delta = new_pos - *mouse_pos;
if *middle_down { if *middle_down {
gsa.bgs[0].scroll -= delta; if gsa.bgs[0].active {
gsa.bgs[1].scroll -= delta; // normal mode
gsa.bgs[2].scroll -= delta; gsa.bgs[0].scroll -= delta;
gsa.bgs[1].scroll -= delta;
gsa.bgs[2].scroll -= delta;
} else {
// tile select mode
gsa2.bgs[0].scroll -= delta;
gsa2.bgs[1].scroll -= delta;
gsa2.bgs[2].scroll -= delta;
}
} }
*mouse_pos = new_pos; *mouse_pos = new_pos;
*tile_pos = (new_pos + gsa.bgs[0].scroll) / TILE_SIZE as i32; *tile_pos = IVec2 {
x: (new_pos.x + gsa.bgs[0].scroll.x).div_euclid(TILE_SIZE as i32),
y: (new_pos.y + gsa.bgs[0].scroll.y).div_euclid(TILE_SIZE as i32),
};
*tile_pos2 = IVec2 {
x: (new_pos.x + gsa2.bgs[0].scroll.x).div_euclid(TILE_SIZE as i32),
y: (new_pos.y + gsa2.bgs[0].scroll.y).div_euclid(TILE_SIZE as i32),
};
let cursor_pos = *tile_pos * TILE_SIZE as i32 - gsa.bgs[0].scroll; let cursor_pos = *tile_pos * TILE_SIZE as i32 - gsa.bgs[0].scroll;
let cursor_pos2 = *tile_pos2 * TILE_SIZE as i32 - gsa2.bgs[0].scroll;
gsa.sprites[SPR_CURSOR].pos = cursor_pos; gsa.sprites[SPR_CURSOR].pos = cursor_pos;
gsa2.sprites[SPR_CURSOR].pos = cursor_pos2;
} }
_ => {} _ => {}
}, },
Event::MainEventsCleared => { Event::MainEventsCleared => {
if *left_down { if gsa.bgs[0].active {
if tile_pos.x >= 0 // normal mode
&& tile_pos.y >= 0 if *left_down {
&& tile_pos.x < BACKGROUND_MAX_SIZE as i32 if tile_pos.x >= 0
&& tile_pos.y < BACKGROUND_MAX_SIZE as i32 && tile_pos.y >= 0
{ && tile_pos.x < BACKGROUND_MAX_SIZE as i32
gsa.bgs[0].tiles[tile_pos.x as usize][tile_pos.y as usize] = 0; && tile_pos.y < BACKGROUND_MAX_SIZE as i32
{
let tile = (selected_tile.x + (selected_tile.y << 8)) as u16;
gsa.bgs[0].tiles[tile_pos.x as usize][tile_pos.y as usize] = tile;
}
} else if *right_down {
if tile_pos.x >= 0
&& tile_pos.y >= 0
&& tile_pos.x < BACKGROUND_MAX_SIZE as i32
&& tile_pos.y < BACKGROUND_MAX_SIZE as i32
{
gsa.bgs[0].tiles[tile_pos.x as usize][tile_pos.y as usize] = EMPTY_TILE;
}
} }
} else if *right_down { } else {
if tile_pos.x >= 0 // tile select mode
&& tile_pos.y >= 0 if *left_down {
&& tile_pos.x < BACKGROUND_MAX_SIZE as i32 if tile_pos2.x >= 0
&& tile_pos.y < BACKGROUND_MAX_SIZE as i32 && tile_pos2.y >= 0
{ && tile_pos2.x < BACKGROUND_MAX_SIZE as i32
gsa.bgs[0].tiles[tile_pos.x as usize][tile_pos.y as usize] = EMPTY_TILE; && tile_pos2.y < BACKGROUND_MAX_SIZE as i32
{
gsa2.bgs[1].tiles[selected_tile.x as usize][selected_tile.y as usize] =
EMPTY_TILE;
*selected_tile = *tile_pos2;
gsa2.bgs[1].tiles[selected_tile.x as usize][selected_tile.y as usize] =
TILE_MARKER;
}
} }
} }
@ -154,6 +234,15 @@ pub(crate) fn run_mapedit() {
y: size.height as i32, y: size.height as i32,
}, },
); );
render_to_screen(
&mut screen_buffer,
&gsa2,
&tileset,
IVec2 {
x: size.width as i32,
y: size.height as i32,
},
);
render_to_window( render_to_window(
&mut window_buffer, &mut window_buffer,
&mut screen_buffer, &mut screen_buffer,