Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize size tool #972

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions luda-editor/new-client/src/episode_editor/scene_preview.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::psd_sprite_util::render_psd_sprite;
use super::{psd_sprite_util::render_psd_sprite, scene_sprite_editor::SIZE_TOOL_DRAGGING_ATOM};
use luda_rpc::Scene;
use namui::*;
use namui_prebuilt::*;
use std::ops::Deref;

pub struct ScenePreview<'a> {
pub wh: Wh<Px>,
Expand Down Expand Up @@ -51,8 +52,24 @@ struct ScenePreviewScreen<'a> {
impl Component for ScenePreviewScreen<'_> {
fn render(self, ctx: &RenderCtx) {
let Self { scene, screen_wh } = self;
let (size_tool_dragging, _) = ctx.init_atom(&SIZE_TOOL_DRAGGING_ATOM, || None);

for scene_sprite in &scene.scene_sprites {
let size_tool_dragging = size_tool_dragging.deref().as_ref().and_then(|dragging| {
if dragging.scene_id != scene.id {
return None;
}
Some(dragging)
});

for (sprite_index, scene_sprite) in scene.scene_sprites.iter().enumerate() {
if let Some(size_tool_dragging) = size_tool_dragging {
if size_tool_dragging.sprite_index == sprite_index {
let mut scene_sprite = scene_sprite.clone();
scene_sprite.circumcircle.radius = size_tool_dragging.radius;
render_psd_sprite(ctx, &scene_sprite, screen_wh);
continue;
}
}
render_psd_sprite(ctx, scene_sprite, screen_wh);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use luda_rpc::{AssetDoc, Circumcircle, Scene, SceneSprite};
use math::num::Zero;
use namui::*;
use namui_prebuilt::*;
pub use size_tool::SIZE_TOOL_DRAGGING_ATOM;
use std::collections::{HashMap, HashSet};

pub struct SceneSpriteEditor<'a> {
Expand All @@ -26,6 +27,8 @@ impl Component for SceneSpriteEditor<'_> {
} = self;

let (selected_scene_sprite_index, set_selected_scene_sprite_index) = ctx.state(|| None);
let (_size_tool_dragging, set_size_tool_dragging) =
ctx.init_atom(&SIZE_TOOL_DRAGGING_ATOM, || None);

let scene_sprites = &scene.scene_sprites;
let selected_scene_sprite = selected_scene_sprite_index
Expand Down Expand Up @@ -121,10 +124,18 @@ impl Component for SceneSpriteEditor<'_> {
update_scene(scene);
};

let on_change_size_radius = &|size_radius: Percent| {
let on_change_size_radius = &|size_radius: Percent, is_dragging: bool| {
let Some(index) = *selected_scene_sprite_index else {
return;
};
if is_dragging {
set_size_tool_dragging.set(Some(size_tool::SizeToolDragging {
scene_id: scene.id.clone(),
sprite_index: index,
radius: size_radius,
}));
return;
}
let mut scene = scene.clone();
let Some(scene_sprite) = scene.scene_sprites.get_mut(index) else {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::ops::Deref;

use crate::*;

pub static SIZE_TOOL_DRAGGING_ATOM: Atom<Option<SizeToolDragging>> = Atom::uninitialized();

pub struct SizeTool<'a> {
pub wh: Wh<Px>,
pub size_radius: Percent,
pub on_change_size_radius: &'a dyn Fn(Percent),
/// Fn(Percent, is_dragging)
pub on_change_size_radius: &'a dyn Fn(Percent, bool),
}

impl Component for SizeTool<'_> {
Expand All @@ -14,14 +19,20 @@ impl Component for SizeTool<'_> {
on_change_size_radius,
} = self;

let (is_dragging, set_is_dragging) = ctx.state(|| false);
let (dragging, set_dragging) = ctx.init_atom(&SIZE_TOOL_DRAGGING_ATOM, || None);

let size_radius = dragging
.deref()
.as_ref()
.map(|dragging| dragging.radius)
.unwrap_or(size_radius);

ctx.compose(|ctx| {
table::vertical([
table::fixed(64.px(), |wh, ctx| {
ctx.add(typography::body::left(
wh.height,
format!("크기 - {}", size_radius.round()),
format!("크기 - {}", size_radius),
Color::WHITE,
));
}),
Expand All @@ -40,34 +51,39 @@ impl Component for SizeTool<'_> {
},
})
.attach_event(|event| {
let mouse_event = match event {
let mouse_event = match &event {
Event::MouseDown { event } => {
if !event.is_local_xy_in() {
return;
}
set_is_dragging.set(true);
event
}
Event::MouseMove { event } => {
if !*is_dragging {
Event::MouseMove { event } | Event::MouseUp { event } => {
if dragging.is_none() {
return;
}
event
}
Event::MouseUp { event } => {
if !*is_dragging {
return;
Event::VisibilityChange => {
if dragging.is_some() {
set_dragging.set(None);
}
set_is_dragging.set(false);
event
return;
}
_ => {
return;
}
};
let radius = {
let x = (mouse_event.local_xy().x / wh.width).clamp(0.0, 1.0);
100.percent() * x
};
on_change_size_radius(radius, true);

let x = (mouse_event.local_xy().x / wh.width).clamp(0.0, 1.0);
on_change_size_radius(100.percent() * x);
if let Event::MouseUp { .. } = &event {
set_dragging.set(None);
on_change_size_radius(radius, false);
}
}),
);

Expand All @@ -86,3 +102,9 @@ impl Component for SizeTool<'_> {
});
}
}

pub struct SizeToolDragging {
pub scene_id: String,
pub sprite_index: usize,
pub radius: Percent,
}
Loading