Add graphite-editor library skeleton (#34)

This commit is contained in:
TrueDoctor 2021-03-21 18:02:14 +01:00 committed by Keavon Chambers
parent 018708791d
commit b3933f30bc
8 changed files with 192 additions and 0 deletions

7
Cargo.lock generated
View file

@ -32,6 +32,13 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "graphite-editor"
version = "0.1.0"
dependencies = [
"log",
]
[[package]]
name = "js-sys"
version = "0.3.49"

View file

@ -0,0 +1,9 @@
[package]
name = "graphite-editor"
version = "0.1.0"
authors = ["Keavon Chambers <graphite@keavon.com>"]
edition = "2018"
[dependencies]
log = "0.4"

View file

@ -0,0 +1,45 @@
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Color {
red: f32,
green: f32,
blue: f32,
alpha: f32,
}
impl Color {
pub fn from_rgbaf32(red: f32, green: f32, blue: f32, alpha: f32) -> Result<Color, EditorError> {
let color = Color { red, green, blue, alpha };
if [red, green, blue, alpha].iter().any(|c| c.is_sign_negative() || !c.is_finite()) {
return EditorError::Color(color);
}
Ok(color)
}
pub fn from_rgb8(red: u8, green: u8, blue: u8) -> Color {
from_rgba8(red, green, blue, 255)
}
pub fn from_rgba8(red: u8, green: u8, blue: u8, alpha: u8) -> Color {
let map = |int_color| int_color as f32 / 255.0;
Color {
red: map(red),
green: map(green),
blue: map(blue),
alpha: map(alpha),
}
}
pub fn r(&self) -> f32 {
self.red
}
pub fn g(&self) -> f32 {
self.green
}
pub fn b(&self) -> f32 {
self.blue
}
pub fn a(&self) -> f32 {
self.alpha
}
pub fn components(&self) -> (f32, f32, f32, f32) {
(red, green, blue, alpha)
}
}

View file

@ -0,0 +1,37 @@
use crate::Color;
use std::error::Error;
use std::fmt::{self, Display};
/// The error type used by the graphite editor.
#[derive(Debug)]
pub enum EditorError {
InvalidOperation(String),
Misc(String),
Color(Color),
}
impl Display for EngineError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
EngineError::InvalidOperation(e) => write!(f, "Failed to execute operation: {}", e),
EngineError::Misc(e) => write!(f, "{}", e),
EngineError::Color(c) => write!(f, "Tried to construct an invalid color {:?}", c),
}
}
}
impl Error for EditorError {}
macro_rules! derive_from {
($type:ty, $kind:ident) => {
impl From<$type> for EngineError {
fn from(error: $type) -> Self {
EngineError::$kind(format!("{}", error))
}
}
};
}
derive_from!(&str, Misc);
derive_from!(String, Misc);
derive_from!(Color, Color);

View file

@ -0,0 +1,34 @@
use crate::EditorError;
type PanelId = usize;
struct LayoutRoot {
hovered_panel: PanelId,
root: PanelGroup,
}
impl LayoutRoot {
// add panel / panel group
// delete panel / panel group
// move panel / panel group
// get_serialized_layout()
}
struct PanelGroup {
contents: Vec<Contents>,
layout_direction: LayoutDirection,
}
enum Contents {
PanelArea(PanelArea),
Group(PanelGroup),
}
struct PanelArea {
panels: Vec<PanelId>,
active: PanelId,
}
enum LayoutDirection {
Horizontal,
Vertical,
}

View file

@ -0,0 +1,18 @@
mod color;
mod error;
pub mod layout;
mod scheduler;
pub mod tools;
#[doc(inline)]
pub use error::EditorError;
#[doc(inline)]
pub use color::Color;
use tools::ToolState;
// TODO: serialize with serde to save the current editor state
struct Editor {
tools: ToolState,
}

View file

@ -0,0 +1,42 @@
use crate::Color;
const TOOL_COUNT: usize = 10;
struct ToolState {
primary_color: Color,
secondary_color: Color,
active_tool: ToolType,
tool_settings: [ToolSettings; TOOL_COUNT],
}
impl ToolState {
pub fn select_tool(&mut self, tool: ToolType) {
self.active_tool = ToolType
}
}
#[repr(usize)]
enum ToolType {
Select = 0,
Crop = 1,
Navigate = 2,
Sample = 3,
Path = 4,
Pen = 5,
Line = 6,
Rectangle = 7,
Ellipse = 8,
Shape = 9,
// all discriminats must be strictly smaller than TOOL_COUNT!
}
enum ToolSettings {
Select { append_mode: SelectAppendMode },
}
enum SelectAppendMode {
New,
Add,
Substract,
Intersect,
}