From 61d255a420c9d977b46670e7fa9e7735d2acf819 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 6 Jan 2021 21:32:48 +0100 Subject: Add CLI with save feature --- src/cli/cmd/mod.rs | 53 ++++++++++++++++++++++++++ src/cli/cmd/save.rs | 42 +++++++++++++++++++++ src/cli/mod.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 src/cli/cmd/mod.rs create mode 100644 src/cli/cmd/save.rs create mode 100644 src/cli/mod.rs (limited to 'src/cli') diff --git a/src/cli/cmd/mod.rs b/src/cli/cmd/mod.rs new file mode 100644 index 0000000..29063e8 --- /dev/null +++ b/src/cli/cmd/mod.rs @@ -0,0 +1,53 @@ +//! The commands that can be performed in the CLI + +pub mod save; + +pub use save::*; + +use crate::Editor; +use std::ops::RangeInclusive; + +/// Errors that can occur when parsing a command. This is for syntax checking, the +/// semantics are checked when trying to execute the command. +#[allow(missing_docs)] +#[derive(thiserror::Error, Debug)] +pub enum CmdParseError { + #[error("no command specified")] + StringEmpty, + #[error("the command {0} is unknown")] + NoSuchCmd(String), + #[error("wrong number of arguments. Expected in range {1:?}, but received {0}")] + WrongNumberOfArgs(usize, RangeInclusive), + #[error("{0} cannot be converted into a {1}, which is required")] + InvalidArgType(String, &'static str), +} + +/// Attempts to parse a command from the given string. If it is unsuccessful, it returns a +/// [CmdParseError]. +pub fn parse_command(string: &str) -> Result, CmdParseError> { + if string.is_empty() { + return Err(CmdParseError::StringEmpty); + } + + let parts: Vec<&str> = string.split_whitespace().collect(); + match parts[0] { + "w" => Ok(Box::new(Save::from_args(&parts[1..])?)), + other => Err(CmdParseError::NoSuchCmd(other.to_owned())), + } +} + +/// Indicates that this entity (command) can be created from arguments. Make sure to check what is +/// expected, to pass the arguments to the correct command. +pub trait FromArgs: Sized { + /// Creates a new instance from the arguments provided. If for whatever reason the syntax of the + /// given arguments is correct an [ArgParseError] is returned. + fn from_args(args: &[&str]) -> Result; +} + +/// A common trait for all commands. +pub trait Command { + /// Process this command on the provided context. Returns either a string with the output of the + /// command when everything went right with it, or an error string explaining what went wrong, + /// which can be displayed to the user. + fn process(&self, editor: &mut Editor) -> Result; +} diff --git a/src/cli/cmd/save.rs b/src/cli/cmd/save.rs new file mode 100644 index 0000000..2c022cf --- /dev/null +++ b/src/cli/cmd/save.rs @@ -0,0 +1,42 @@ +//! Save the contents of the map to disk + +use super::Command; +use super::{CmdParseError, FromArgs}; +use crate::map::MapData; +use crate::Editor; +use std::path::PathBuf; + +/// The save command can take any destination in the filesystem the user can write to. Processing +/// will then save the map contents to that destination, overwriting anything that may be there. +pub struct Save { + destination: PathBuf, +} + +impl FromArgs for Save { + fn from_args(args: &[&str]) -> Result { + if args.len() != 1 { + return Err(CmdParseError::WrongNumberOfArgs(args.len(), 1..=1)); + } + + Ok(Self { + destination: PathBuf::from(args[0]), + }) + } +} + +impl Command for Save { + fn process(&self, editor: &mut Editor) -> Result { + let data = MapData::extract_data(editor.map()); + + match data.write_to_file(&self.destination) { + Ok(_) => Ok(format!( + "Successfully wrote contents to `{:?}`", + &self.destination + )), + Err(e) => Err(format!( + "Unable to write to `{:?}`. Error: {:?}", + &self.destination, e + )), + } + } +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs new file mode 100644 index 0000000..e96070f --- /dev/null +++ b/src/cli/mod.rs @@ -0,0 +1,104 @@ +//! In-window Command line interface. Used for operations that are just easier than with GUI. +//! +//! Sometimes it is nice to have a GUI, for instance when a selection has to be made, things have to +//! be moved etc., however for operations like saving/loading and exporting, no such thing has to be +//! done and the GUI is really just slowing you down (at least in my opinion). For these operations, +//! it is much better to simply have a command do that specific thing. It is also much easier to +//! implement a new command, so features can be tested more quickly. For some things, there should +//! still be a GUI option. With the example of saving/loading, it is much easier to find some hidden +//! folder in a GUI, so that is definitely a consideration for the future. + +pub mod cmd; +pub use self::cmd::*; + +use crate::colours::DEFAULT_COLOURS; +use crate::math::Vec2; +use crate::Editor; +use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; +use raylib::ffi::KeyboardKey; +use raylib::RaylibHandle; + +/// The command line interface. Should be created only once per program instance. +pub struct CLI { + text: String, + active: bool, +} + +impl CLI { + /// Create a CLI for this instance + pub fn new() -> Self { + Self { + text: String::new(), + active: false, + } + } + + /// Activates the CLI, which will now capture keyboard input and execute commands accordingly. + pub fn activate(&mut self) { + if !self.active { + self.text = ";".to_owned(); + self.active = true; + } + } + + /// Handle input for the command line and perform any commands the user may want to run. + pub fn update(&mut self, rl: &mut RaylibHandle, editor: &mut Editor) { + /* Check if the CLI is currently active. If not and it should not be activated according to + * keyboard input, there is nothing to do. + */ + if !self.active { + if rl.is_key_pressed(KeyboardKey::KEY_SEMICOLON) { + // Don't write the keypress again. + rl.get_key_pressed(); + self.activate(); + } else { + return; + } + } + + // The CLI is currently active. Handle input to it. + if let Some(key) = rl.get_key_pressed_number() { + self.text.push(key as u8 as char); + } else if rl.is_key_pressed(KeyboardKey::KEY_BACKSPACE) { + self.text.pop(); + } else if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) { + self.text.clear(); + } + + // When the text is empty, there is also no command marker, so set as inactive and leave. + if self.text.is_empty() { + self.active = false; + return; + } + + // Perform the entered command, when the enter-key is pressed. + if rl.is_key_pressed(KeyboardKey::KEY_ENTER) { + self.active = false; + match cmd::parse_command(&self.text[1..]) { + Ok(cmd) => match cmd.process(editor) { + Ok(res) => self.text = format!("SUCCESS: {}", res), + Err(err) => self.text = format!("ERROR: {}", err), + }, + Err(err) => self.text = format!("SYNTAX ERROR: {}", err), + } + } + } + + /// Draw the command line at the bottom of the window. + pub fn draw(&self, rld: &mut RaylibDrawHandle) { + let pos = Vec2::new(150., rld.get_screen_height() as f32 - 25.); + + rld.draw_rectangle_v( + pos, + Vec2::new(rld.get_screen_width() as f32 - pos.x, 25.), + DEFAULT_COLOURS.cli_background, + ); + rld.draw_text( + &self.text, + 155, + rld.get_screen_height() - 22, + 20, + DEFAULT_COLOURS.cli_foreground, + ); + } +} -- cgit v1.2.3-70-g09d2 From 0eada0bdcb36a9907c6c928aa707ed6bef03c02f Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Wed, 6 Jan 2021 22:47:34 +0100 Subject: Add loading capabilities back --- src/cli/cmd/edit.rs | 35 +++++++++++++++++++++++++++++++++++ src/cli/cmd/mod.rs | 6 ++++++ src/cli/cmd/read.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/map/data.rs | 2 +- src/map/mod.rs | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 src/cli/cmd/edit.rs create mode 100644 src/cli/cmd/read.rs (limited to 'src/cli') diff --git a/src/cli/cmd/edit.rs b/src/cli/cmd/edit.rs new file mode 100644 index 0000000..797edc6 --- /dev/null +++ b/src/cli/cmd/edit.rs @@ -0,0 +1,35 @@ +//! Replace the contents of the currently edited map with contents from a file. + +use super::Command; +use super::{CmdParseError, FromArgs}; +use crate::map::MapData; +use crate::Editor; +use std::path::PathBuf; + +pub struct Edit { + file: PathBuf, +} + +impl FromArgs for Edit { + fn from_args(args: &[&str]) -> Result { + if args.len() != 1 { + return Err(CmdParseError::WrongNumberOfArgs(args.len(), 1..=1)); + } + + Ok(Self { + file: PathBuf::from(args[0]), + }) + } +} + +impl Command for Edit { + fn process(&self, editor: &mut Editor) -> Result { + let data = match MapData::load_from_file(&self.file) { + Ok(data) => data, + Err(err) => return Err(format!("Unable to read file: {:?}", &self.file)), + }; + + editor.map_mut().set_data(data); + Ok(format!("Map data from {:?} loaded.", &self.file)) + } +} diff --git a/src/cli/cmd/mod.rs b/src/cli/cmd/mod.rs index 29063e8..42e865a 100644 --- a/src/cli/cmd/mod.rs +++ b/src/cli/cmd/mod.rs @@ -1,7 +1,11 @@ //! The commands that can be performed in the CLI +pub mod edit; +pub mod read; pub mod save; +pub use edit::*; +pub use read::*; pub use save::*; use crate::Editor; @@ -32,6 +36,8 @@ pub fn parse_command(string: &str) -> Result, CmdParseError> { let parts: Vec<&str> = string.split_whitespace().collect(); match parts[0] { "w" => Ok(Box::new(Save::from_args(&parts[1..])?)), + "e" => Ok(Box::new(Edit::from_args(&parts[1..])?)), + "r" => Ok(Box::new(Read::from_args(&parts[1..])?)), other => Err(CmdParseError::NoSuchCmd(other.to_owned())), } } diff --git a/src/cli/cmd/read.rs b/src/cli/cmd/read.rs new file mode 100644 index 0000000..4ac671c --- /dev/null +++ b/src/cli/cmd/read.rs @@ -0,0 +1,38 @@ +//! Read the contents of a file and add it to the currently edited map. + +use super::Command; +use super::{CmdParseError, FromArgs}; +use crate::map::MapData; +use crate::Editor; +use std::path::PathBuf; + +pub struct Read { + file: PathBuf, +} + +impl FromArgs for Read { + fn from_args(args: &[&str]) -> Result { + if args.len() != 1 { + return Err(CmdParseError::WrongNumberOfArgs(args.len(), 1..=1)); + } + + Ok(Self { + file: PathBuf::from(args[0]), + }) + } +} + +impl Command for Read { + fn process(&self, editor: &mut Editor) -> Result { + let data = match MapData::load_from_file(&self.file) { + Ok(data) => data, + Err(err) => return Err(format!("Unable to read file: {:?}", &self.file)), + }; + + editor.map_mut().add_data(data); + Ok(format!( + "Map data from {:?} read and added to the current buffer.", + &self.file + )) + } +} diff --git a/src/map/data.rs b/src/map/data.rs index b7719cd..f7ec484 100644 --- a/src/map/data.rs +++ b/src/map/data.rs @@ -65,7 +65,7 @@ impl MapData { } /// Load the map data from a file. Fails if the file does not exist or cannot be correctly parsed. - pub fn load_from_file>(&mut self, path: P) -> io::Result { + pub fn load_from_file>(path: P) -> io::Result { let file = File::open(&path)?; let data: Self = match from_reader(file) { Ok(data) => data, diff --git a/src/map/mod.rs b/src/map/mod.rs index 28025ad..70f65b3 100644 --- a/src/map/mod.rs +++ b/src/map/mod.rs @@ -147,19 +147,52 @@ impl Map { .chain(self.icons.iter_mut().map(|i| i as &mut dyn Mappable)) } + /// Get the rectangular rooms of this map. pub fn rect_rooms(&self) -> &Vec { &self.rect_rooms } + /// Get the polygon rooms of this map. pub fn polygon_rooms(&self) -> &Vec { &self.polygon_rooms } + /// Get the walls of this map. pub fn walls(&self) -> &Vec { &self.walls } + /// Get the icons of this map. pub fn icons(&self) -> &Vec { &self.icons } + + /// Replace the internal map data with the data provided. (Load and replace) + pub fn set_data(&mut self, data: MapData) { + // Remove all data. + self.icons.clear(); + self.polygon_rooms.clear(); + self.rect_rooms.clear(); + self.walls.clear(); + + // Add all data from the map data. + self.add_data(data); + } + + /// Add the data provided to the current data on the map. All elements will remain, with the + /// additional elements being pushed also. + pub fn add_data(&mut self, data: MapData) { + for i in data.icons { + self.push_icon(Icon::from_data(i, self.icon_renderer.clone())) + } + for p in data.polygon_rooms { + self.push_polygon_room(p); + } + for r in data.rect_rooms { + self.push_rect_room(r); + } + for w in data.walls { + self.push_wall(w); + } + } } -- cgit v1.2.3-70-g09d2