Skip to content

Commit

Permalink
Rename: s/canvas/image/
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Sep 24, 2023
1 parent 32f792b commit 7a10d2c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 68 deletions.
72 changes: 36 additions & 36 deletions pati/src/canvas.rs → pati/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,61 @@ use std::{
ops::{Bound, RangeBounds},
};

/// [`Canvas`] with a log of applied [`Command`]s.
/// [`Image`] with a log of applied [`Command`]s.
#[derive(Debug, Default, Clone)]
pub struct VersionedCanvas {
canvas: Canvas,
pub struct VersionedImage {
image: Image,
log: Log,
}

impl VersionedCanvas {
/// Makes a new [`VersionedCanvas`] instance.
impl VersionedImage {
/// Makes a new [`VersionedImage`] instance.
pub fn new() -> Self {
Self::default()
}

/// Gets the current version of this canvas.
/// Gets the current version of this image.
pub fn version(&self) -> Version {
self.log.latest_canvas_version()
self.log.latest_image_version()
}

/// Gets the color of the pixel at the given point.
pub fn get_pixel(&self, point: Point) -> Option<Color> {
self.canvas.get_pixel(point)
self.image.get_pixel(point)
}

/// Gets an iterator over the pixels in the given range.
pub fn range_pixels<R>(&self, range: R) -> impl '_ + Iterator<Item = (Point, Color)>
where
R: RangeBounds<Point>,
{
self.canvas.range_pixels(range)
self.image.range_pixels(range)
}

/// Gets the all pixels in this canvas.
/// Gets the all pixels in this image.
pub fn pixels(&self) -> &BTreeMap<Point, Color> {
self.canvas.pixels()
self.image.pixels()
}

/// Gets the all anchors in this canvas.
/// Gets the all anchors in this image.
pub fn anchors(&self) -> &BTreeMap<String, Point> {
self.canvas.anchors()
self.image.anchors()
}

/// Gets the all metadata in this canvas.
/// Gets the all metadata in this image.
pub fn metadata(&self) -> &BTreeMap<String, serde_json::Value> {
self.canvas.metadata()
self.image.metadata()
}

/// Applies the given command to this canvas.
/// Applies the given command to this image.
///
/// Returns `true` if the canvas is changed, otherwise `false`.
/// Returns `true` if the image is changed, otherwise `false`.
/// If the command is applied, it is appended to the log.
pub fn apply(&mut self, command: &Command) -> bool {
let applied = self.canvas.apply(command);
let applied = self.image.apply(command);
if applied {
self.log
.append_applied_command(command.clone(), &self.canvas);
.append_applied_command(command.clone(), &self.image);
}
applied
}
Expand All @@ -70,23 +70,23 @@ impl VersionedCanvas {
&self.log.commands()[i..]
}

/// Calculates the diff between the current canvas and the canvas at the given version.
/// Calculates the diff between the current image and the image at the given version.
pub fn diff(&self, version: Version) -> Option<PatchCommand> {
let canvas = self.log.restore_canvas(version)?;
Some(self.canvas.diff(&canvas))
let image = self.log.restore_image(version)?;
Some(self.image.diff(&image))
}
}

/// Raster image canvas.
/// Raster image.
#[derive(Debug, Default, Clone)]
pub struct Canvas {
pub struct Image {
pixels: BTreeMap<Point, Color>,
anchors: BTreeMap<String, Point>,
metadata: BTreeMap<String, serde_json::Value>,
}

impl Canvas {
/// Makes a new [`Canvas`] instance.
impl Image {
/// Makes a new [`Image`] instance.
pub fn new() -> Self {
Self::default()
}
Expand All @@ -104,24 +104,24 @@ impl Canvas {
RangePixels::new(self, range)
}

/// Gets the all pixels in this canvas.
/// Gets the all pixels in this image.
pub fn pixels(&self) -> &BTreeMap<Point, Color> {
&self.pixels
}

/// Gets the all anchors in this canvas.
/// Gets the all anchors in this image.
pub fn anchors(&self) -> &BTreeMap<String, Point> {
&self.anchors
}

/// Gets the all metadata in this canvas.
/// Gets the all metadata in this image.
pub fn metadata(&self) -> &BTreeMap<String, serde_json::Value> {
&self.metadata
}

/// Applies the given command to this canvas.
/// Applies the given command to this image.
///
/// Returns `true` if the canvas is changed, otherwise `false`.
/// Returns `true` if the image is changed, otherwise `false`.
pub fn apply(&mut self, command: &Command) -> bool {
match command {
Command::Patch(c) => self.handle_patch_command(c),
Expand Down Expand Up @@ -217,14 +217,14 @@ impl Canvas {

#[derive(Debug)]
struct RangePixels<'a> {
canvas: &'a Canvas,
image: &'a Image,
start: Point,
end: Point,
row: std::collections::btree_map::Range<'a, Point, Color>,
}

impl<'a> RangePixels<'a> {
fn new<R>(canvas: &'a Canvas, range: R) -> Self
fn new<R>(image: &'a Image, range: R) -> Self
where
R: RangeBounds<Point>,
{
Expand All @@ -238,9 +238,9 @@ impl<'a> RangePixels<'a> {
Bound::Excluded(&p) => Point::new(p.x - 1, p.y - 1),
Bound::Unbounded => Point::new(i16::MAX, i16::MAX),
};
let row = canvas.pixels.range(start..=end);
let row = image.pixels.range(start..=end);
Self {
canvas,
image,
start,
end,
row,
Expand All @@ -261,7 +261,7 @@ impl<'a> Iterator for RangePixels<'a> {
} else {
return Some((*point, *color));
}
self.row = self.canvas.pixels.range(self.start..=self.end);
self.row = self.image.pixels.range(self.start..=self.end);
}
}
}
4 changes: 2 additions & 2 deletions pati/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
//!
//! - [patica](https://github.com/sile/patica): Terminal based pixel art editor using this crate.
#![warn(missing_docs)]
mod canvas;
mod command;
mod image;
mod log;
mod pixel;

pub use self::canvas::{Canvas, VersionedCanvas};
pub use self::command::{Command, CommandReader, CommandWriter, PatchCommand, PatchEntry};
pub use self::image::{Image, VersionedImage};
pub use self::log::Version;
pub use self::pixel::{Color, Point};
38 changes: 19 additions & 19 deletions pati/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Canvas;
use crate::Command;
use crate::Image;
use serde::{Deserialize, Serialize};

/// Number of applied commands.
Expand Down Expand Up @@ -31,16 +31,16 @@ pub struct Log {
}

impl Log {
pub fn latest_canvas_version(&self) -> Version {
pub fn latest_image_version(&self) -> Version {
Version(self.commands.len() as u32)
}

pub fn append_applied_command(&mut self, command: Command, canvas: &Canvas) {
pub fn append_applied_command(&mut self, command: Command, image: &Image) {
self.commands.push(command);
if self.commands.len() % 1000 == 0 {
self.snapshots.push(Snapshot {
version: Version(self.commands.len() as u32),
canvas: canvas.clone(),
image: image.clone(),
});
}
}
Expand All @@ -49,19 +49,19 @@ impl Log {
&self.commands
}

pub fn restore_canvas(&self, version: Version) -> Option<Canvas> {
if self.latest_canvas_version() < version {
pub fn restore_image(&self, version: Version) -> Option<Image> {
if self.latest_image_version() < version {
return None;
}

match self.snapshots.binary_search_by_key(&version, |s| s.version) {
Ok(i) => Some(self.snapshots[i].canvas.clone()),
Ok(i) => Some(self.snapshots[i].image.clone()),
Err(i) => {
let mut snapshot = self.snapshots[i - 1].clone();
for i in snapshot.version.0..version.0 {
snapshot.canvas.apply(&self.commands[i as usize]);
snapshot.image.apply(&self.commands[i as usize]);
}
Some(snapshot.canvas)
Some(snapshot.image)
}
}
}
Expand All @@ -79,31 +79,31 @@ impl Default for Log {
#[derive(Debug, Default, Clone)]
struct Snapshot {
version: Version,
canvas: Canvas,
image: Image,
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{Canvas, Color, PatchCommand, PatchEntry, Point};
use crate::{Color, Image, PatchCommand, PatchEntry, Point};

#[test]
fn restore_canvas_works() {
let mut canvas = Canvas::new();
fn restore_image_works() {
let mut image = Image::new();
let mut log = Log::default();
assert_eq!(log.latest_canvas_version(), Version(0));
assert_eq!(log.latest_image_version(), Version(0));

let color = Color::rgb(100, 0, 0);
let entry = PatchEntry {
color: Some(color),
points: vec![Point::new(1, 3)],
};
let command = Command::Patch(PatchCommand::new(vec![entry]));
assert!(canvas.apply(&command));
log.append_applied_command(command, &canvas);
assert_eq!(log.latest_canvas_version(), Version(1));
assert!(image.apply(&command));
log.append_applied_command(command, &image);
assert_eq!(log.latest_image_version(), Version(1));

let old_canvas = log.restore_canvas(Version(0)).unwrap();
assert_ne!(old_canvas.pixels().len(), canvas.pixels().len());
let old_image = log.restore_image(Version(0)).unwrap();
assert_ne!(old_image.pixels().len(), image.pixels().len());
}
}
10 changes: 5 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};
use pagurus::{failure::OrFail, Game};
use pagurus_tui::{TuiSystem, TuiSystemOptions};
use pati::{CommandReader, CommandWriter, Point, VersionedCanvas};
use pati::{CommandReader, CommandWriter, Point, VersionedImage};
use std::io::Write;
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -151,15 +151,15 @@ impl OpenCommand {
#[derive(Debug)]
struct EmbeddedCanvas {
path: PathBuf,
canvas: VersionedCanvas,
canvas: VersionedImage,
reader: CommandReader<BufReader<std::fs::File>>,
}

impl EmbeddedCanvas {
fn new(path: &PathBuf) -> orfail::Result<Self> {
let file = std::fs::File::open(path).or_fail()?;
let reader = CommandReader::new(BufReader::new(file));
let canvas = VersionedCanvas::default(); // TODO: use Canvas
let canvas = VersionedImage::default(); // TODO: use Canvas
Ok(Self {
path: path.clone(),
canvas,
Expand Down Expand Up @@ -344,10 +344,10 @@ impl ExportCommand {
}
}

fn load_canvas<P: AsRef<Path>>(path: &P) -> orfail::Result<pati::Canvas> {
fn load_canvas<P: AsRef<Path>>(path: &P) -> orfail::Result<pati::Image> {
let file = std::fs::File::open(path).or_fail()?;
let mut reader = CommandReader::new(BufReader::new(file));
let mut canvas = pati::Canvas::new();
let mut canvas = pati::Image::new();
while let Some(command) = reader.read_command().or_fail()? {
canvas.apply(&command);
}
Expand Down
4 changes: 2 additions & 2 deletions src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::clock::Ticks;
use orfail::OrFail;
use pati::{Color, Point, Version, VersionedCanvas};
use pati::{Color, Point, Version, VersionedImage};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, path::PathBuf};

Expand Down Expand Up @@ -38,7 +38,7 @@ impl EmbeddedFrame {
}
}

pub fn sync(&mut self, canvas: &VersionedCanvas) -> orfail::Result<()> {
pub fn sync(&mut self, canvas: &VersionedImage) -> orfail::Result<()> {
let start = canvas
.anchors()
.get(&self.frame.top_left_anchor)
Expand Down
8 changes: 4 additions & 4 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const METADATA_BRUSH_COLOR: &str = "patica.brush_color";

#[derive(Debug, Default)]
pub struct Model {
canvas: pati::VersionedCanvas,
canvas: pati::VersionedImage,
cursor: Point,
camera: Point,
brush_color: Color,
Expand Down Expand Up @@ -132,11 +132,11 @@ impl Model {
}
}

pub fn canvas(&self) -> &pati::VersionedCanvas {
pub fn canvas(&self) -> &pati::VersionedImage {
&self.canvas
}

pub fn canvas_mut(&mut self) -> &mut pati::VersionedCanvas {
pub fn canvas_mut(&mut self) -> &mut pati::VersionedImage {
&mut self.canvas
}

Expand Down Expand Up @@ -440,7 +440,7 @@ enum Fsm {
}

impl Fsm {
fn draw(&mut self, canvas: &mut pati::VersionedCanvas, brush_color: Color, cursor: Point) {
fn draw(&mut self, canvas: &mut pati::VersionedImage, brush_color: Color, cursor: Point) {
match self {
Fsm::Neutral(fsm) => {
let command =
Expand Down

0 comments on commit 7a10d2c

Please sign in to comment.