From 83732aed1a41a713cd8790fcebae90aabe78b789 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 17 Mar 2021 17:52:11 +0530 Subject: read and write to .obi files --- src/app.rs | 34 ++++++++++++++++++++++++---------- src/bitmap.rs | 3 +-- src/symmetry.rs | 8 ++++---- src/utils.rs | 2 +- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index 9a1155e..5b27422 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,17 +1,19 @@ use crate::{ bitmap::{MapPoint, Pixmap}, + command::CommandBox, consts::colors::*, dither, rect, symmetry::Symmetry, undo::{ModifyRecord, OpKind, Operation, UndoStack}, - utils::draw_text, + utils::{draw_text, is_copy_event, is_paste_event}, }; -use std::convert::From; +use std::{convert::From, fs::File, io::prelude::*}; +use obi::Image; use sdl2::{ event::Event, - keyboard::Keycode, + keyboard::{Keycode, Mod}, mouse::MouseButton, pixels::Color, rect::{Point, Rect}, @@ -286,15 +288,17 @@ impl<'ctx> AppState<'ctx> { )) .unwrap(); let mouse_coords = if let Some((x, y)) = self.idx_at_coord(self.mouse) { - format!("{:3}, {:3}", x, y) + format!("{:3}, {:3}", x + 1, y + 1) } else { format!("---, ---") }; let status_text = format!( - "[BRUSH {}] [SYM {}] {}", + "[DITHER {}][BRUSH {}][SYM {}][PT {}][ACTIVE {}]", + self.dither_level, self.brush_size + 1, self.symmetry, - mouse_coords + mouse_coords, + if self.active_color { "WHT" } else { "BLK" } ); draw_text( &mut self.canvas, @@ -383,6 +387,7 @@ impl<'ctx> AppState<'ctx> { height: u32, context: &'ctx Sdl, ttf_context: &'ctx Sdl2TtfContext, + start_data: Option>, ) -> Self { let video_subsystem = context.video().unwrap(); @@ -401,26 +406,35 @@ impl<'ctx> AppState<'ctx> { .map_err(|e| e.to_string()) .unwrap(); - let pixmap = Pixmap::new_with(width, height, false); + let data = start_data.unwrap_or(vec![false; (width * height) as usize]); + let pixmap = Pixmap::new_with(width, height, data); Self { active_color: true, brush_size: 0, - dither_level: 16, canvas, context, - ttf_context, - mouse: (0, 0), + command_box: CommandBox::new(), current_operation: Vec::new(), + dither_level: 16, grid: Grid::new(), last_point: None, + mode: Mode::Draw, + mouse: (0, 0), pixmap, start: Point::new(60, 60), symmetry: Default::default(), + ttf_context, undo_stack: UndoStack::new(), zoom: 5, } } + pub fn export(&self) -> Image { + let mut image = Image::new(self.width(), self.height()); + image.data = self.pixmap.data.clone(); + image + } + pub fn run(&mut self) { self.canvas.set_draw_color(BLACK); self.canvas.clear(); diff --git a/src/bitmap.rs b/src/bitmap.rs index 128a14e..d02cafb 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -113,8 +113,7 @@ where } } - pub fn new_with(width: u32, height: u32, start: T) -> Self { - let data = vec![start; (width * height) as usize]; + pub fn new_with(width: u32, height: u32, data: Vec) -> Self { Pixmap { width, height, diff --git a/src/symmetry.rs b/src/symmetry.rs index 7517317..60ff791 100644 --- a/src/symmetry.rs +++ b/src/symmetry.rs @@ -33,10 +33,10 @@ impl fmt::Display for Symmetry { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Symmetry { x, y } = self; match (x, y) { - (None, None) => write!(f, " "), - (Some(_), None) => write!(f, "-"), - (None, Some(_)) => write!(f, "|"), - (Some(_), Some(_)) => write!(f, "+"), + (None, None) => write!(f, "OFF"), + (Some(_), None) => write!(f, "HOR"), + (None, Some(_)) => write!(f, "VER"), + (Some(_), Some(_)) => write!(f, "RAD"), } } } diff --git a/src/utils.rs b/src/utils.rs index e36a8cc..a1b3624 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -14,7 +14,7 @@ pub fn draw_text>( text: S, color: Color, (x, y): (u32, u32), -) { +) -> u32 { let text = text.as_ref(); let texture_creator = canvas.texture_creator(); let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap(); -- cgit v1.2.3