small refactor for ui things
This commit is contained in:
parent
7f37153056
commit
139350d5e1
|
@ -13,6 +13,7 @@ use gilrs;
|
|||
use image;
|
||||
use lyon;
|
||||
use toml;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
/// An enum containing all kinds of game framework errors.
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -127,6 +128,13 @@ impl From<gfx::mapping::Error> for GameError {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<std::string::FromUtf8Error> for GameError {
|
||||
fn from(e: FromUtf8Error) -> Self {
|
||||
let errstr = format!("UTF-8 decoding error: {:?}", e);
|
||||
GameError::ConfigError(errstr)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, D> From<gfx::CopyError<S, D>> for GameError
|
||||
where
|
||||
S: fmt::Debug,
|
||||
|
|
|
@ -8,6 +8,7 @@ use crate::SharedGameState;
|
|||
pub struct LiveDebugger {
|
||||
selected_item: i32,
|
||||
map_selector_visible: bool,
|
||||
hacks_visible: bool,
|
||||
stages: Vec<ImString>,
|
||||
error: Option<ImString>,
|
||||
}
|
||||
|
@ -17,15 +18,16 @@ impl LiveDebugger {
|
|||
Self {
|
||||
selected_item: -1,
|
||||
map_selector_visible: false,
|
||||
hacks_visible: false,
|
||||
stages: vec![],
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_ingame(&mut self, game_scene: &mut GameScene, state: &mut SharedGameState, ctx: &mut Context, ui: &mut imgui::Ui) -> GameResult {
|
||||
Window::new(im_str!("Live Debugger"))
|
||||
Window::new(im_str!("Debugger"))
|
||||
.position([5.0, 5.0], Condition::FirstUseEver)
|
||||
.size([300.0, 100.0], Condition::FirstUseEver)
|
||||
.size([300.0, 120.0], Condition::FirstUseEver)
|
||||
.build(ui, || {
|
||||
ui.text(format!(
|
||||
"Player position: ({:.1},{:.1})",
|
||||
|
@ -46,6 +48,10 @@ impl LiveDebugger {
|
|||
if ui.button(im_str!("Map Selector"), [0.0, 0.0]) {
|
||||
self.map_selector_visible = true;
|
||||
}
|
||||
|
||||
if ui.button(im_str!("Hacks"), [0.0, 0.0]) {
|
||||
self.hacks_visible = true;
|
||||
}
|
||||
});
|
||||
|
||||
if self.error.is_some() {
|
||||
|
|
31
src/main.rs
31
src/main.rs
|
@ -1,6 +1,3 @@
|
|||
extern crate strum;
|
||||
#[macro_use]
|
||||
extern crate strum_macros;
|
||||
#[macro_use]
|
||||
extern crate bitflags;
|
||||
#[macro_use]
|
||||
|
@ -11,14 +8,20 @@ extern crate log;
|
|||
extern crate serde_derive;
|
||||
#[macro_use]
|
||||
extern crate smart_default;
|
||||
extern crate strum;
|
||||
#[macro_use]
|
||||
extern crate strum_macros;
|
||||
|
||||
use std::{env, mem};
|
||||
use std::path;
|
||||
use std::time::Instant;
|
||||
|
||||
use log::*;
|
||||
use pretty_env_logger::env_logger::Env;
|
||||
use winit::{ElementState, Event, KeyboardInput, WindowEvent};
|
||||
|
||||
use crate::caret::{Caret, CaretType};
|
||||
use crate::common::Direction;
|
||||
use crate::engine_constants::EngineConstants;
|
||||
use crate::ggez::{Context, ContextBuilder, event, filesystem, GameResult};
|
||||
use crate::ggez::conf::{WindowMode, WindowSetup};
|
||||
|
@ -29,16 +32,13 @@ use crate::ggez::input::keyboard;
|
|||
use crate::ggez::mint::ColumnMatrix4;
|
||||
use crate::ggez::nalgebra::Vector2;
|
||||
use crate::live_debugger::LiveDebugger;
|
||||
use crate::rng::RNG;
|
||||
use crate::scene::loading_scene::LoadingScene;
|
||||
use crate::scene::Scene;
|
||||
use crate::sound::SoundManager;
|
||||
use crate::stage::StageData;
|
||||
use crate::texture_set::TextureSet;
|
||||
use crate::ui::UI;
|
||||
use crate::caret::{Caret, CaretType};
|
||||
use crate::common::Direction;
|
||||
use crate::rng::RNG;
|
||||
use std::time::Instant;
|
||||
|
||||
mod caret;
|
||||
mod common;
|
||||
|
@ -85,7 +85,6 @@ bitfield! {
|
|||
struct Game {
|
||||
scene: Option<Box<dyn Scene>>,
|
||||
state: SharedGameState,
|
||||
debugger: LiveDebugger,
|
||||
ui: UI,
|
||||
scaled_matrix: ColumnMatrix4<f32>,
|
||||
def_matrix: ColumnMatrix4<f32>,
|
||||
|
@ -154,19 +153,18 @@ impl Game {
|
|||
scaled_matrix: DrawParam::new()
|
||||
.scale(Vector2::new(scale, scale))
|
||||
.to_matrix(),
|
||||
debugger: LiveDebugger::new(),
|
||||
ui: UI::new(ctx)?,
|
||||
def_matrix: DrawParam::new().to_matrix(),
|
||||
state: SharedGameState {
|
||||
flags: GameFlags(0),
|
||||
game_rng: RNG::new(0),
|
||||
effect_rng: RNG::new(Instant::now().elapsed().as_nanos() as i32),
|
||||
carets: Vec::new(),
|
||||
carets: Vec::with_capacity(32),
|
||||
key_state: KeyState(0),
|
||||
key_trigger: KeyState(0),
|
||||
texture_set: TextureSet::new(base_path),
|
||||
base_path: str!(base_path),
|
||||
stages: Vec::new(),
|
||||
stages: Vec::with_capacity(96),
|
||||
sound_manager: SoundManager::new(ctx),
|
||||
constants,
|
||||
scale,
|
||||
|
@ -181,8 +179,8 @@ impl Game {
|
|||
}
|
||||
|
||||
fn update(&mut self, ctx: &mut Context) -> GameResult {
|
||||
if self.scene.is_some() {
|
||||
self.scene.as_mut().unwrap().tick(&mut self.state, ctx)?;
|
||||
if let Some(scene) = self.scene.as_mut() {
|
||||
scene.tick(&mut self.state, ctx)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -192,12 +190,12 @@ impl Game {
|
|||
graphics::set_transform(ctx, self.scaled_matrix);
|
||||
graphics::apply_transformations(ctx)?;
|
||||
|
||||
if self.scene.is_some() {
|
||||
self.scene.as_ref().unwrap().draw(&mut self.state, ctx)?;
|
||||
if let Some(scene) = self.scene.as_mut() {
|
||||
scene.draw(&mut self.state, ctx)?;
|
||||
|
||||
graphics::set_transform(ctx, self.def_matrix);
|
||||
graphics::apply_transformations(ctx)?;
|
||||
self.ui.draw(&mut self.debugger, &mut self.state, ctx, self.scene.as_mut().unwrap())?;
|
||||
self.ui.draw(&mut self.state, ctx, scene)?;
|
||||
}
|
||||
|
||||
graphics::present(ctx)?;
|
||||
|
@ -225,6 +223,7 @@ impl Game {
|
|||
|
||||
fn key_up_event(&mut self, _ctx: &mut Context, key_code: KeyCode, _key_mod: KeyMods) {
|
||||
let state = &mut self.state;
|
||||
|
||||
match key_code {
|
||||
KeyCode::Left => { state.key_state.set_left(false) }
|
||||
KeyCode::Right => { state.key_state.set_right(false) }
|
||||
|
|
|
@ -20,7 +20,7 @@ impl Map {
|
|||
return Err(Error::new(ErrorKind::InvalidData, "Invalid magic"));
|
||||
}
|
||||
|
||||
map_data.read_i8()?; // reserved, alignment?
|
||||
map_data.read_i8()?; // unused
|
||||
|
||||
let width = map_data.read_u16::<LE>()? as usize;
|
||||
let height = map_data.read_u16::<LE>()? as usize;
|
||||
|
|
|
@ -11,6 +11,7 @@ use crate::scene::Scene;
|
|||
use crate::SharedGameState;
|
||||
use crate::stage::{BackgroundType, Stage};
|
||||
use crate::str;
|
||||
use crate::ui::{UI, Components};
|
||||
|
||||
pub struct GameScene {
|
||||
pub tick: usize,
|
||||
|
@ -196,6 +197,11 @@ impl GameScene {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_black_bars(&self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_tiles(&self, state: &mut SharedGameState, ctx: &mut Context, layer: TileLayer) -> GameResult {
|
||||
let tex = match layer {
|
||||
TileLayer::Snack => &self.tex_npcsym_name,
|
||||
|
@ -263,7 +269,6 @@ impl GameScene {
|
|||
|
||||
impl Scene for GameScene {
|
||||
fn init(&mut self, state: &mut SharedGameState, ctx: &mut Context) -> GameResult {
|
||||
state.sound_manager.play_song(ctx)?;
|
||||
//self.player.x = 700 * 0x200;
|
||||
//self.player.y = 1000 * 0x200;
|
||||
self.player.equip.set_booster_2_0(true);
|
||||
|
@ -313,6 +318,7 @@ impl Scene for GameScene {
|
|||
self.draw_tiles(state, ctx, TileLayer::Foreground)?;
|
||||
self.draw_tiles(state, ctx, TileLayer::Snack)?;
|
||||
self.draw_carets(state, ctx)?;
|
||||
self.draw_black_bars(state, ctx)?;
|
||||
|
||||
self.draw_hud(state, ctx)?;
|
||||
self.draw_number(state.canvas_size.0 - 8.0, 8.0, timer::fps(ctx) as usize, Alignment::Right, state, ctx)?;
|
||||
|
@ -320,8 +326,8 @@ impl Scene for GameScene {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn debug_overlay_draw(&mut self, dbg: &mut LiveDebugger, state: &mut SharedGameState, ctx: &mut Context, ui: &mut imgui::Ui) -> GameResult {
|
||||
dbg.run_ingame(self, state, ctx, ui)?;
|
||||
fn debug_overlay_draw(&mut self, components: &mut Components, state: &mut SharedGameState, ctx: &mut Context, ui: &mut imgui::Ui) -> GameResult {
|
||||
components.live_debugger.run_ingame(self, state, ctx, ui)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::ggez::{Context, GameResult};
|
||||
|
||||
use crate::live_debugger::LiveDebugger;
|
||||
use crate::SharedGameState;
|
||||
use crate::ui::Components;
|
||||
|
||||
pub mod game_scene;
|
||||
pub mod loading_scene;
|
||||
|
@ -13,5 +13,5 @@ pub trait Scene {
|
|||
|
||||
fn draw(&self, _state: &mut SharedGameState, _ctx: &mut Context) -> GameResult { Ok(()) }
|
||||
|
||||
fn debug_overlay_draw(&mut self, dbg: &mut LiveDebugger, _state: &mut SharedGameState, _ctx: &mut Context, ui: &mut imgui::Ui) -> GameResult { Ok(()) }
|
||||
fn debug_overlay_draw(&mut self, _game_ui: &mut Components, _state: &mut SharedGameState, _ctx: &mut Context, _frame: &mut imgui::Ui) -> GameResult { Ok(()) }
|
||||
}
|
||||
|
|
|
@ -91,7 +91,7 @@ impl TextureSet {
|
|||
pub fn new(base_path: &str) -> TextureSet {
|
||||
TextureSet {
|
||||
tex_map: HashMap::new(),
|
||||
base_path: str!(base_path),
|
||||
base_path: base_path.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,7 +153,7 @@ impl TextureSet {
|
|||
|
||||
pub fn get_or_load_batch(&mut self, ctx: &mut Context, constants: &EngineConstants, name: &str) -> GameResult<&mut SizedBatch> {
|
||||
if !self.tex_map.contains_key(name) {
|
||||
let mut batch = self.load_texture(ctx, constants, name)?;
|
||||
let batch = self.load_texture(ctx, constants, name)?;
|
||||
self.tex_map.insert(str!(name), batch);
|
||||
}
|
||||
|
||||
|
|
12
src/ui.rs
12
src/ui.rs
|
@ -24,10 +24,15 @@ pub struct UI {
|
|||
pub imgui: imgui::Context,
|
||||
pub platform: WinitPlatform,
|
||||
pub renderer: Renderer<Rgba8, types::Resources>,
|
||||
pub components: Components,
|
||||
main_color: RenderTargetView<types::Resources, Rgba8>,
|
||||
last_frame: Instant,
|
||||
}
|
||||
|
||||
pub struct Components {
|
||||
pub live_debugger: LiveDebugger,
|
||||
}
|
||||
|
||||
impl UI {
|
||||
pub fn new(ctx: &mut Context) -> GameResult<Self> {
|
||||
let mut imgui = imgui::Context::create();
|
||||
|
@ -144,6 +149,9 @@ impl UI {
|
|||
imgui,
|
||||
platform,
|
||||
renderer,
|
||||
components: Components {
|
||||
live_debugger: LiveDebugger::new(),
|
||||
},
|
||||
main_color: RenderTargetView::new(color),
|
||||
last_frame: Instant::now(),
|
||||
})
|
||||
|
@ -153,7 +161,7 @@ impl UI {
|
|||
self.platform.handle_event(self.imgui.io_mut(), graphics::window(ctx), &event);
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, dbg: &mut LiveDebugger, state: &mut SharedGameState, ctx: &mut Context, scene: &mut Box<dyn Scene>) -> GameResult {
|
||||
pub fn draw(&mut self, state: &mut SharedGameState, ctx: &mut Context, scene: &mut Box<dyn Scene>) -> GameResult {
|
||||
{
|
||||
let io = self.imgui.io_mut();
|
||||
self.platform.prepare_frame(io, graphics::window(ctx)).map_err(|e| RenderError(e))?;
|
||||
|
@ -163,7 +171,7 @@ impl UI {
|
|||
}
|
||||
let mut ui = self.imgui.frame();
|
||||
|
||||
scene.debug_overlay_draw(dbg, state, ctx, &mut ui)?;
|
||||
scene.debug_overlay_draw(&mut self.components, state, ctx, &mut ui)?;
|
||||
|
||||
self.platform.prepare_render(&ui, graphics::window(ctx));
|
||||
let draw_data = ui.render();
|
||||
|
|
Loading…
Reference in New Issue