From e482b29fc69bee8c0ffee6fda3aaec8c689595cb Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Sun, 24 Sep 2023 19:16:47 +0100 Subject: [PATCH] reimpl farlook (tooltips NYI) --- src/camera.rs | 2 +- src/gui/farlook.rs | 66 ++++++++++++++++++++++++--------------------- src/main.rs | 25 +++++++++++------ src/map/themes.rs | 7 ++--- src/states/state.rs | 14 ++++++++-- 5 files changed, 70 insertions(+), 44 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 09ff97b..11f9c19 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -90,7 +90,7 @@ pub fn render_camera(ecs: &World, ctx: &mut BTerm) { 16 ), 0, - tint, + RGBA::named(WHITE), 0 // Ya ); ctx.set_active_console(TILE_LAYER); diff --git a/src/gui/farlook.rs b/src/gui/farlook.rs index b346e0f..5d303fe 100644 --- a/src/gui/farlook.rs +++ b/src/gui/farlook.rs @@ -1,5 +1,6 @@ use super::{ State, RunState, tooltip::draw_tooltips, camera::get_offset, VIEWPORT_H, VIEWPORT_W }; use bracket_lib::prelude::*; +use notan::prelude::*; #[derive(PartialEq, Copy, Clone)] pub enum FarlookResult { @@ -10,44 +11,49 @@ pub enum FarlookResult { Cancel, } -pub fn show_farlook(gs: &mut State, ctx: &mut BTerm) -> FarlookResult { +pub fn show_farlook(gs: &mut State, ctx: &mut App) -> FarlookResult { let runstate = gs.ecs.fetch::(); let offsets = get_offset(); - - ctx.print_color( - 1 + offsets.x, - 1 + offsets.y, - RGB::named(WHITE), - RGB::named(BLACK), - "Look at what? [move keys][Esc.]" - ); - if let RunState::Farlook { x, y } = *runstate { let x = x.clamp(offsets.x, offsets.x - 1 + VIEWPORT_W); let y = y.clamp(offsets.y, offsets.y - 1 + VIEWPORT_H); - - ctx.set(x, y, RGB::named(WHITE), RGB::named(BLACK), to_cp437('X')); - draw_tooltips(&gs.ecs, ctx, Some((x, y))); - - return match ctx.key { - None => FarlookResult::NoResponse { x, y }, - Some(key) => - match key { - VirtualKeyCode::Escape | VirtualKeyCode::X => FarlookResult::Cancel, - VirtualKeyCode::Numpad9 => FarlookResult::NoResponse { x: x + 1, y: y - 1 }, - VirtualKeyCode::Numpad8 => FarlookResult::NoResponse { x, y: y - 1 }, - VirtualKeyCode::Numpad7 => FarlookResult::NoResponse { x: x - 1, y: y - 1 }, - VirtualKeyCode::Numpad6 => FarlookResult::NoResponse { x: x + 1, y }, - VirtualKeyCode::Numpad4 => FarlookResult::NoResponse { x: x - 1, y }, - VirtualKeyCode::Numpad3 => FarlookResult::NoResponse { x: x + 1, y: y + 1 }, - VirtualKeyCode::Numpad2 => FarlookResult::NoResponse { x, y: y + 1 }, - VirtualKeyCode::Numpad1 => FarlookResult::NoResponse { x: x - 1, y: y + 1 }, - _ => FarlookResult::NoResponse { x, y }, + let key = &ctx.keyboard; + // Movement + for keycode in key.pressed.iter() { + match *keycode { + KeyCode::Escape | KeyCode::X => { + return FarlookResult::Cancel; } - }; + KeyCode::Numpad1 => { + return FarlookResult::NoResponse { x: x - 1, y: y + 1 }; + } + KeyCode::Numpad2 => { + return FarlookResult::NoResponse { x, y: y + 1 }; + } + KeyCode::Numpad3 => { + return FarlookResult::NoResponse { x: x + 1, y: y + 1 }; + } + KeyCode::Numpad4 => { + return FarlookResult::NoResponse { x: x - 1, y }; + } + KeyCode::Numpad6 => { + return FarlookResult::NoResponse { x: x + 1, y }; + } + KeyCode::Numpad7 => { + return FarlookResult::NoResponse { x: x - 1, y: y - 1 }; + } + KeyCode::Numpad8 => { + return FarlookResult::NoResponse { x, y: y - 1 }; + } + KeyCode::Numpad9 => { + return FarlookResult::NoResponse { x: x + 1, y: y - 1 }; + } + _ => {} + } + } + return FarlookResult::NoResponse { x, y }; } else { let ppos = gs.ecs.fetch::(); - // TODO: PPOS + offsets (should get these from screen_bounds()) return FarlookResult::NoResponse { x: ppos.x + offsets.x, y: ppos.x + offsets.y }; } } diff --git a/src/main.rs b/src/main.rs index 821e281..cb033a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -180,7 +180,7 @@ fn draw_camera(ecs: &World, draw: &mut Draw, atlas: &HashMap) { let minds = ecs.read_storage::(); let pools = ecs.read_storage::(); let entities = ecs.entities(); - let mut data = (&positions, &renderables, &entities, !&hidden).join().collect::>(); + let data = (&positions, &renderables, &entities, !&hidden).join().collect::>(); let mut to_draw: HashMap = HashMap::new(); for (pos, render, e, _h) in data.iter() { let idx = map.xy_idx(pos.x, pos.y); @@ -255,6 +255,7 @@ fn draw_camera(ecs: &World, draw: &mut Draw, atlas: &HashMap) { (entry.0.x as f32) * TILESIZE, (entry.0.y as f32) * TILESIZE ); + // TODO: Update map memory. } _ => {} } @@ -287,7 +288,7 @@ fn render_map_in_view(map: &Map, ecs: &World, draw: &mut Draw, atlas: &HashMap) { + draw.image(atlas.get("ui_select_a1").unwrap()).position( + (x as f32) * TILESIZE, + (y as f32) * TILESIZE + ); +} + fn draw(app: &mut App, gfx: &mut Graphics, gs: &mut State) { let mut draw = gfx.create_draw(); draw.clear(Color::BLACK); @@ -308,12 +316,13 @@ fn draw(app: &mut App, gfx: &mut Graphics, gs: &mut State) { draw_camera(&gs.ecs, &mut draw, &gs.atlas); } } - // Draw player (replace this with draw entities). - let map = gs.ecs.fetch::(); - let ppos = gs.ecs.fetch::(); - let offsets = crate::camera::get_offset(); - let px = idx_to_px(map.xy_idx(ppos.x + offsets.x, ppos.y + offsets.y), &map); - draw.image(gs.atlas.get("ui_heart_full").unwrap()).position(px.0, px.1); + match *gs.ecs.fetch::() { + RunState::Farlook { x, y } => { + draw_farlook(x, y, &mut draw, &gs.atlas); + //draw_tooltips(&gs.ecs, ctx, Some((x, y))); TODO: Put this in draw loop + } + _ => {} + } // Render batch gfx.render(&draw); } diff --git a/src/map/themes.rs b/src/map/themes.rs index 92ee89b..e11ee2b 100644 --- a/src/map/themes.rs +++ b/src/map/themes.rs @@ -4,8 +4,9 @@ use crate::config::CONFIG; use crate::data::ids::*; use bracket_lib::prelude::*; use std::ops::{ Add, Mul }; +use notan::prelude::*; -pub fn get_sprite_for_id(idx: usize, map: &Map, other_pos: Option) -> (&str, RGBA) { +pub fn get_sprite_for_id(idx: usize, map: &Map, other_pos: Option) -> (&str, Color) { let x = (idx as i32) % map.width; let y = (idx as i32) / map.width; let sprite = map.tiles[idx].sprite(); @@ -15,9 +16,9 @@ pub fn get_sprite_for_id(idx: usize, map: &Map, other_pos: Option) -> (&s }; let sprite_id = pick_variant(base, tile.variants(), idx, map);*/ let tint = if !map.visible_tiles[idx] { - RGBA::from_f32(0.75, 0.75, 0.75, 1.0) + Color::from_rgb(0.75, 0.75, 0.75) } else { - RGBA::named(WHITE) + Color::WHITE }; return (sprite, tint); } diff --git a/src/states/state.rs b/src/states/state.rs index 9185e0d..8f46415 100644 --- a/src/states/state.rs +++ b/src/states/state.rs @@ -229,7 +229,17 @@ impl State { } } } - // RunState::Farlook + RunState::Farlook { .. } => { + let result = gui::show_farlook(self, ctx); + match result { + gui::FarlookResult::NoResponse { x, y } => { + new_runstate = RunState::Farlook { x, y }; + } + gui::FarlookResult::Cancel => { + new_runstate = RunState::AwaitingInput; + } + } + } // RunState::ShowCheatMenu // RunState::ShowInventory // RunState::ShowDropItem @@ -379,7 +389,7 @@ impl State { } } RunState::Farlook { .. } => { - let result = gui::show_farlook(self, ctx); + let result = gui::FarlookResult::Cancel; //gui::show_farlook(self, ctx); match result { gui::FarlookResult::NoResponse { x, y } => { new_runstate = RunState::Farlook { x, y };