Skip to content

Commit

Permalink
Add command and query modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Sep 24, 2023
1 parent cffbb6b commit 104aacf
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions canvas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
orfail = { version = "1.1.0", features = ["serde"] }
pati = { version = "0.2", path = "../pati/" }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
66 changes: 64 additions & 2 deletions canvas/src/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,64 @@
#[derive(Debug)]
pub struct Canvas {}
use crate::{
command::CanvasCommand,
query::{CanvasQuery, CanvasQueryValue},
};
use orfail::OrFail;
use pati::{Color, Image, Point};

#[derive(Debug, Default)]
pub struct Canvas {
image: Image,
cursor: Point,
camera: Point,
brush_color: Color,
background_color: Color,
}

impl Canvas {
pub fn new() -> Self {
Self::default()
}

pub fn image(&self) -> &Image {
&self.image
}

pub fn cursor(&self) -> Point {
self.cursor
}

pub fn camera(&self) -> Point {
self.camera
}

pub fn brush_color(&self) -> Color {
self.brush_color
}

pub fn background_color(&self) -> Color {
self.background_color
}

pub fn query(&self, query: &CanvasQuery) -> CanvasQueryValue {
match query {
CanvasQuery::Cursor => CanvasQueryValue::Cursor(self.cursor),
CanvasQuery::Camera => CanvasQueryValue::Camera(self.camera),
CanvasQuery::BrushColor => CanvasQueryValue::BrushColor(self.brush_color),
CanvasQuery::BackgroundColor => {
CanvasQueryValue::BackgroundColor(self.background_color)
}
}
}

pub fn command(&mut self, command: &CanvasCommand) -> orfail::Result<()> {
match command {
CanvasCommand::Move(c) => self.handle_move(*c).or_fail()?,
}
Ok(())
}

fn handle_move(&mut self, delta: Point) -> orfail::Result<()> {
self.cursor = self.cursor + delta;
Ok(())
}
}
3 changes: 2 additions & 1 deletion canvas/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use pati::Point;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CanvasCommand {
//
Move(Point),
}
5 changes: 3 additions & 2 deletions canvas/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod command;
pub mod query;

mod canvas;
mod command;

pub use canvas::Canvas;
pub use command::CanvasCommand;
20 changes: 20 additions & 0 deletions canvas/src/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use pati::{Color, Point};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CanvasQuery {
Cursor,
Camera,
BrushColor,
BackgroundColor,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CanvasQueryValue {
Cursor(Point),
Camera(Point),
BrushColor(Color),
BackgroundColor(Color),
}

0 comments on commit 104aacf

Please sign in to comment.