mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-20 11:28:30 +08:00
Rename project structure with /core and /client
This commit is contained in:
47
core/editor/src/color.rs
Normal file
47
core/editor/src/color.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use crate::EditorError;
|
||||
|
||||
#[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()) {
|
||||
Err(color)?
|
||||
}
|
||||
Ok(color)
|
||||
}
|
||||
pub fn from_rgb8(red: u8, green: u8, blue: u8) -> Color {
|
||||
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) {
|
||||
(self.red, self.green, self.blue, self.alpha)
|
||||
}
|
||||
}
|
||||
37
core/editor/src/error.rs
Normal file
37
core/editor/src/error.rs
Normal 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(Clone, Debug)]
|
||||
pub enum EditorError {
|
||||
InvalidOperation(String),
|
||||
Misc(String),
|
||||
Color(String),
|
||||
}
|
||||
|
||||
impl Display for EditorError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
EditorError::InvalidOperation(e) => write!(f, "Failed to execute operation: {}", e),
|
||||
EditorError::Misc(e) => write!(f, "{}", e),
|
||||
EditorError::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 EditorError {
|
||||
fn from(error: $type) -> Self {
|
||||
EditorError::$kind(format!("{:?}", error))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
derive_from!(&str, Misc);
|
||||
derive_from!(String, Misc);
|
||||
derive_from!(Color, Color);
|
||||
20
core/editor/src/lib.rs
Normal file
20
core/editor/src/lib.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
mod color;
|
||||
mod error;
|
||||
mod scheduler;
|
||||
pub mod tools;
|
||||
pub mod workspace;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use error::EditorError;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use color::Color;
|
||||
|
||||
use tools::ToolState;
|
||||
use workspace::Workspace;
|
||||
|
||||
// TODO: serialize with serde to save the current editor state
|
||||
struct Editor {
|
||||
tools: ToolState,
|
||||
workspace: Workspace,
|
||||
}
|
||||
1
core/editor/src/scheduler/mod.rs
Normal file
1
core/editor/src/scheduler/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
42
core/editor/src/tools/mod.rs
Normal file
42
core/editor/src/tools/mod.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use crate::Color;
|
||||
|
||||
const TOOL_COUNT: usize = 10;
|
||||
|
||||
pub 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 = tool
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(usize)]
|
||||
pub 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!
|
||||
}
|
||||
|
||||
pub enum ToolSettings {
|
||||
Select { append_mode: SelectAppendMode },
|
||||
}
|
||||
|
||||
pub enum SelectAppendMode {
|
||||
New,
|
||||
Add,
|
||||
Subtract,
|
||||
Intersect,
|
||||
}
|
||||
34
core/editor/src/workspace/mod.rs
Normal file
34
core/editor/src/workspace/mod.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use crate::EditorError;
|
||||
pub type PanelId = usize;
|
||||
|
||||
pub struct Workspace {
|
||||
hovered_panel: PanelId,
|
||||
root: PanelGroup,
|
||||
}
|
||||
|
||||
impl Workspace {
|
||||
// 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,
|
||||
}
|
||||
Reference in New Issue
Block a user