-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |