Skip to content

Commit

Permalink
Atom & Sequence Document Split (#557)
Browse files Browse the repository at this point in the history
Co-authored-by: bigfood <[email protected]>
  • Loading branch information
namse and bigfoodK authored Jul 2, 2023
1 parent 6274330 commit aed5871
Show file tree
Hide file tree
Showing 91 changed files with 2,735 additions and 1,827 deletions.
95 changes: 85 additions & 10 deletions luda-editor/client/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 luda-editor/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ futures = "0.3.17"
wasm-bindgen = "0.2.84"
unicode-normalization = "0.1"
crc32fast = "1.3"
tokio = { version = "1.28.2", features = ["sync"] }

[dev-dependencies]
wasm-bindgen-test = "0.3.13"
Expand Down
78 changes: 78 additions & 0 deletions luda-editor/client/src/atom/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::sync::{Arc, Mutex, MutexGuard};

#[allow(dead_code)]
pub struct Atom<Atom: Atomic> {
atom: Mutex<Atom>,
}

#[allow(dead_code)]
impl<TAtom: Atomic> Atom<TAtom> {
pub fn get(&self) -> MutexGuard<TAtom> {
self.atom.lock().unwrap()
}

pub fn update<F>(&self, f: F)
where
F: FnOnce(&mut TAtom),
{
let mut atom = self.atom.lock().unwrap();
f(&mut atom);
atom.on_update();
namui::event::send(namui::NamuiEvent::NoUpdateJustRender);
}

pub(crate) fn new(atom: TAtom) -> Self {
Self {
atom: Mutex::new(atom),
}
}
}

pub struct OptionAtom<Atom: Atomic> {
inner: Mutex<Option<Arc<Atom>>>,
}

impl<TAtom: Atomic> OptionAtom<TAtom> {
pub fn get_unwrap(&self) -> Arc<TAtom> {
let inner = self.inner.lock().unwrap();
inner.as_ref().unwrap().clone()
}

pub fn set(&self, atom: TAtom) {
let mut inner = self.inner.lock().unwrap();
*inner = Some(Arc::new(atom));
namui::event::send(namui::NamuiEvent::NoUpdateJustRender);
}

pub fn update<F>(&self, f: F)
where
F: FnOnce(&mut TAtom),
{
let mut inner = self.inner.lock().unwrap();
let Some(atom) = inner.as_mut() else {
return;
};
let atom = Arc::get_mut(atom).unwrap();
f(&mut *atom);
atom.on_update();
namui::event::send(namui::NamuiEvent::NoUpdateJustRender);
}

pub(crate) const fn new() -> Self {
Self {
inner: Mutex::new(None),
}
}
}

pub trait Atomic {
fn on_update(&self);
}

impl<T: Atomic> Atomic for Option<T> {
fn on_update(&self) {
if let Some(value) = self {
value.on_update();
}
}
}
7 changes: 7 additions & 0 deletions luda-editor/client/src/color.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use namui::Color;

#[allow(dead_code)]
pub const BACKGROUND: Color = Color::grayscale_u8(32);
#[allow(dead_code)]
pub const STROKE_NORMAL: Color = Color::grayscale_u8(204);
#[allow(dead_code)]
pub const STROKE_DISABLED: Color = Color::grayscale_u8(128);
#[allow(dead_code)]
pub const STROKE_HOVER: Color = Color::grayscale_u8(230);
#[allow(dead_code)]
pub const STROKE_SELECTED: Color = Color::grayscale_u8(255);

#[allow(dead_code)]
pub const STROKE_FOCUS: Color = Color::from_u8(155, 109, 255, 255);

#[allow(dead_code)]
pub const fn stroke_color(is_selected: bool, is_focused: bool) -> Color {
if is_selected && is_focused {
STROKE_FOCUS
Expand Down
74 changes: 74 additions & 0 deletions luda-editor/client/src/components/cg_render.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::storage::get_project_cg_part_variant_image_url;
use namui::prelude::*;
use rpc::data::*;

pub struct CgRenderProps {
pub rect: Rect<Px>,
pub project_id: Uuid,
pub cg_id: Uuid,
}

pub fn render_cg(props: CgRenderProps, screen_cg: &ScreenCg, cg_file: &CgFile) -> RenderingTree {
render(screen_cg.parts.iter().map(|part| {
try_render(|| {
let cg_part = cg_file.parts.iter().find(|part| part.name == part.name)?;
Some(render_cg_part(&props, part, cg_part))
})
}))
}

fn render_cg_part(
props: &CgRenderProps,
part: &rpc::data::ScreenCgPart,
cg_part: &CgPart,
) -> RenderingTree {
match part {
rpc::data::ScreenCgPart::Single { variant_name, .. } => try_render(|| {
let variant_name = variant_name.as_ref()?;

let variant = cg_part
.variants
.iter()
.find(|variant| &variant.name == variant_name)?;

Some(render_cg_variant(props, variant))
}),
rpc::data::ScreenCgPart::Multi { variant_names, .. } => render(
cg_part
.variants
.iter()
.filter(|variant| variant_names.contains(&variant.name))
.map(|variant| render_cg_variant(props, variant)),
),
rpc::data::ScreenCgPart::AlwaysOn { .. } => render(
cg_part
.variants
.iter()
.map(|variant| render_cg_variant(props, variant)),
),
}
}

fn render_cg_variant(props: &CgRenderProps, variant: &rpc::data::CgPartVariant) -> RenderingTree {
try_render(|| {
let rect = Rect::Xywh {
x: props.rect.x() + props.rect.width() * variant.rect.x(),
y: props.rect.y() + props.rect.height() * variant.rect.y(),
width: props.rect.width() * variant.rect.width(),
height: props.rect.height() * variant.rect.height(),
};

let url = get_project_cg_part_variant_image_url(props.project_id, props.cg_id, variant.id)
.unwrap();

let image = namui::image::try_load_url(&url)?;
Some(namui::image(ImageParam {
rect,
source: ImageSource::Image(image),
style: ImageStyle {
fit: ImageFit::Fill,
paint_builder: None,
},
}))
})
}
2 changes: 2 additions & 0 deletions luda-editor/client/src/components/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub enum Item {
text: String,
on_click: ClosurePtr<(), ()>,
},

#[allow(dead_code)]
Divider,
}

Expand Down
Loading

0 comments on commit aed5871

Please sign in to comment.