Skip to content

Commit

Permalink
Implement new-file & save-file functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tversteeg committed Feb 16, 2020
1 parent fe6a542 commit 07b9069
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ anyhow = "1.0.26"
druid = "0.4.0"
sprite-gen = { path = "lib", version = "0.1.5" }
lazy_static = "1.4.0"
bincode = "1.2.1"
serde = { version = "1.0.104", features = ["derive"] }

[[example]]
name = "minifb"
Expand Down
96 changes: 94 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use anyhow::Result;
use druid::commands::*;
use druid::kurbo::*;
use druid::lens::{Lens, LensWrap};
use druid::piet::*;
use druid::widget::*;
use druid::*;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use sprite_gen::{gen_sprite, MaskValue, Options};
use std::{convert::From, sync::RwLock};
use std::{convert::From, fs::File, sync::RwLock};

const MAX_GRID_SIZE: usize = 128;
const MAX_SCALE: usize = 32;
Expand Down Expand Up @@ -233,14 +235,15 @@ impl MaskValueEx for MaskValue {
}
}

#[derive(Debug, Clone, PartialEq, Data, Lens)]
#[derive(Debug, Clone, PartialEq, Data, Lens, Serialize, Deserialize)]
struct AppState {
pub fill_type: i8,
pub size_x: f64,
pub size_y: f64,
pub render_scale: f64,
pub mirror_x: bool,
pub mirror_y: bool,
pub file_path: Option<String>,
}

impl AppState {
Expand Down Expand Up @@ -281,11 +284,99 @@ impl Default for AppState {
render_scale: 0.2,
mirror_x: true,
mirror_y: false,
file_path: None,
fill_type: MaskValue::Solid.i8(),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
struct Encoded {
pub state: AppState,
pub grid: Vec<i8>,
}

struct Delegate {}

impl AppDelegate<AppState> for Delegate {
fn event(
&mut self,
event: Event,
data: &mut AppState,
_env: &Env,
_ctx: &mut DelegateCtx,
) -> Option<Event> {
match event {
Event::Command(cmd) => match cmd.selector {
NEW_FILE => {
// Clear the grid
GRID.write()
.unwrap()
.iter_mut()
.for_each(|p| *p = MaskValue::Empty);

// Clear the results
RESULTS.write().unwrap().clear();

None
}
SAVE_FILE => {
// Get the file path from the Save As menu if applicable
if let Some(file_info) = cmd.get_object::<FileInfo>() {
data.file_path = Some(file_info.path().to_str().unwrap().to_string());
}

if data.file_path == None {
// There's no path yet, show the popup
return Some(Event::Command(SHOW_SAVE_PANEL.into()));
}

// Save the current grid to a new file
bincode::serialize_into(
File::create(data.file_path.as_ref().unwrap()).unwrap(),
&Encoded {
state: data.clone(),
// Convert the grid to an array of i8
grid: GRID
.read()
.unwrap()
.iter()
// Only take the size needed instead of the full 1024 * 1024
.take(data.width() * data.height())
.map(|p| p.i8())
.collect::<_>(),
},
)
.unwrap();

None
}
_ => Some(Event::Command(cmd)),
},

other => Some(other),
}
}

fn window_added(
&mut self,
_id: WindowId,
_data: &mut AppState,
_env: &Env,
_ctx: &mut DelegateCtx,
) {
}

fn window_removed(
&mut self,
_id: WindowId,
_data: &mut AppState,
_env: &Env,
_ctx: &mut DelegateCtx,
) {
}
}

fn ui_builder() -> impl Widget<AppState> {
let fill_type = LensWrap::new(
RadioGroup::new(vec![
Expand Down Expand Up @@ -375,6 +466,7 @@ fn main() -> Result<()> {
let data = AppState::default();

AppLauncher::with_window(main_window)
.delegate(Delegate {})
.use_simple_logger()
.launch(data)
.expect("Could not create main window");
Expand Down

0 comments on commit 07b9069

Please sign in to comment.