Skip to content

Commit

Permalink
Fetch team assets from server
Browse files Browse the repository at this point in the history
  • Loading branch information
bigfoodK committed Sep 21, 2024
1 parent 5fd42ed commit 614c621
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 68 deletions.
86 changes: 55 additions & 31 deletions luda-editor/new-client/src/episode_editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,81 @@ mod speaker_selector;
mod text_editor;

use super::*;
use luda_rpc::{EpisodeEditAction, Scene};
use crate::rpc::asset::get_team_asset_docs;
use crate::rpc::episode_editor::join_episode_editor;
use luda_rpc::{AssetDoc, EpisodeEditAction, Scene};
use properties_panel::PropertiesPanel;
use std::{collections::HashMap, sync::Arc};

pub struct EpisodeEditor<'a> {
pub team_id: &'a String,
pub project_id: &'a String,
pub episode_id: &'a String,
}

impl Component for EpisodeEditor<'_> {
fn render(self, ctx: &RenderCtx) {
let Self {
team_id,
project_id,
episode_id,
} = self;

let wh = namui::screen::size().map(|x| x.into_px());

{
use crate::rpc::episode_editor::join_episode_editor::*;
let result = join_episode_editor(
ctx,
|episode_id| Some((RefRequest { episode_id }, ())),
episode_id,
);
let join_result = join_episode_editor::join_episode_editor(
ctx,
|episode_id| Some((join_episode_editor::RefRequest { episode_id }, ())),
episode_id,
);
let asset_result = get_team_asset_docs::get_team_asset_docs(
ctx,
|team_id| Some((get_team_asset_docs::RefRequest { team_id }, ())),
team_id,
);
let asset_docs = ctx.memo({
|| {
let Some(Ok((get_team_asset_docs::Response { asset_docs }, _))) =
asset_result.as_ref()
else {
return HashMap::new();
};
asset_docs
.iter()
.map(|asset_doc| (asset_doc.name.clone(), asset_doc.clone()))
.collect()
}
});

let Some(result) = result.as_ref() else {
let (Some(join_result), Some(asset_result)) = (join_result.as_ref(), asset_result.as_ref())
else {
ctx.add(typography::center_text(
wh,
"로딩중...",
Color::RED,
16.int_px(),
));
return;
};

match (join_result, asset_result) {
(Ok((join_episode_editor::Response { scenes, texts }, _)), Ok(_)) => {
ctx.add(LoadedEpisodeEditor {
project_id,
episode_id,
initial_scenes: scenes,
initial_texts: texts,
asset_docs,
});
}
(join_result, asset_result) => {
let errors = (join_result.as_ref().err(), asset_result.as_ref().err());
ctx.add(typography::center_text(
wh,
"로딩중...",
format!("에러: {:#?}", errors),
Color::RED,
16.int_px(),
));
return;
};

match result {
Ok((Response { scenes, texts }, _)) => {
ctx.add(LoadedEpisodeEditor {
project_id,
episode_id,
initial_scenes: scenes,
initial_texts: texts,
});
}
Err(err) => {
ctx.add(typography::center_text(
wh,
format!("에러: {:?}", err),
Color::RED,
16.int_px(),
));
}
}
}
}
Expand All @@ -69,6 +91,7 @@ struct LoadedEpisodeEditor<'a> {
episode_id: &'a String,
initial_scenes: &'a Vec<Scene>,
initial_texts: &'a HashMap<String, HashMap<String, String>>,
asset_docs: Sig<'a, HashMap<String, AssetDoc>>,
}

impl Component for LoadedEpisodeEditor<'_> {
Expand All @@ -78,6 +101,7 @@ impl Component for LoadedEpisodeEditor<'_> {
episode_id,
initial_scenes,
initial_texts,
asset_docs,
} = self;
let (scenes, set_scenes) = ctx.state(|| initial_scenes.clone());
let (texts, set_texts) = ctx.state(|| initial_texts.clone());
Expand Down Expand Up @@ -304,7 +328,7 @@ impl Component for LoadedEpisodeEditor<'_> {
wh,
scene,
edit_episode: &edit_episode,
sprite_docs: todo!(),
asset_docs,
});
});

Expand Down
8 changes: 4 additions & 4 deletions luda-editor/new-client/src/episode_editor/properties_panel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::scene_sprite_editor::SceneSpriteEditor;
use luda_rpc::{EpisodeEditAction, Scene, SpriteDoc};
use luda_rpc::{AssetDoc, EpisodeEditAction, Scene};
use namui::*;
use namui_prebuilt::{button, table::*};
use std::collections::HashMap;
Expand All @@ -10,14 +10,14 @@ pub struct PropertiesPanel<'a> {
pub wh: Wh<Px>,
pub scene: &'a Scene,
pub edit_episode: &'a dyn Fn(EpisodeEditAction),
pub sprite_docs: Sig<'a, HashMap<String, SpriteDoc>>,
pub asset_docs: Sig<'a, HashMap<String, AssetDoc>>,
}
impl Component for PropertiesPanel<'_> {
fn render(self, ctx: &RenderCtx) {
let Self {
wh,
scene,
sprite_docs,
asset_docs,
edit_episode,
} = self;

Expand Down Expand Up @@ -62,7 +62,7 @@ impl Component for PropertiesPanel<'_> {
wh,
scene,
update_scene,
sprite_docs,
asset_docs,
});
}
PropertiesPanelTab::Background => {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod scene_sprite_list;
mod size_tool;
mod sprite_select_tool;

use luda_rpc::{Circumcircle, Scene, SceneSprite, SpriteDoc};
use luda_rpc::{AssetDoc, Circumcircle, Scene, SceneSprite};
use namui::*;
use namui_prebuilt::*;
use std::collections::HashMap;
Expand All @@ -12,7 +12,7 @@ pub struct SceneSpriteEditor<'a> {
pub wh: Wh<Px>,
pub scene: &'a Scene,
pub update_scene: &'a dyn Fn(Scene),
pub sprite_docs: Sig<'a, HashMap<String, SpriteDoc>>,
pub asset_docs: Sig<'a, HashMap<String, AssetDoc>>,
}

impl Component for SceneSpriteEditor<'_> {
Expand All @@ -21,7 +21,7 @@ impl Component for SceneSpriteEditor<'_> {
wh,
scene,
update_scene,
sprite_docs,
asset_docs,
} = self;

let (selected_scene_sprite_index, set_selected_scene_sprite_index) = ctx.state(|| None);
Expand Down Expand Up @@ -125,19 +125,19 @@ impl Component for SceneSpriteEditor<'_> {
ctx.add(scene_sprite_list::SceneSpriteList {
wh,
scene_sprites,
asset_docs: &asset_docs,
remove_scene_sprite,
add_new_scene_sprite,
move_scene_sprite_up_down,
select_scene_sprite_index,
selected_scene_sprite_index: *selected_scene_sprite_index,
sprite_docs: sprite_docs.as_ref(),
});
}
}),
table::fixed(320.px(), |wh, ctx| {
ctx.add(sprite_select_tool::SpriteSelectTool {
wh,
sprite_docs: sprite_docs.clone(),
asset_docs: asset_docs.clone(),
select_part_option,
});
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::HashMap;
pub struct SceneSpriteList<'a> {
pub wh: Wh<Px>,
pub scene_sprites: &'a [SceneSprite],
pub sprite_docs: &'a HashMap<String, SpriteDoc>,
pub asset_docs: &'a HashMap<String, AssetDoc>,
pub remove_scene_sprite: &'a dyn Fn(usize),
pub add_new_scene_sprite: &'a dyn Fn(),
/// true for up, false for down
Expand All @@ -21,7 +21,7 @@ impl Component for SceneSpriteList<'_> {
let Self {
wh,
scene_sprites,
sprite_docs,
asset_docs,
remove_scene_sprite,
add_new_scene_sprite,
move_scene_sprite_up_down,
Expand Down Expand Up @@ -84,9 +84,9 @@ impl Component for SceneSpriteList<'_> {
.sprite_id
.as_ref()
.map(|sprite_id| {
sprite_docs
asset_docs
.get(sprite_id)
.map(|sprite_doc| sprite_doc.sprite.name.as_str())
.map(|asset_doc| asset_doc.name.as_str())
.unwrap_or("???")
})
.unwrap_or("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

pub struct SpriteSelectTool<'a> {
pub wh: Wh<Px>,
pub sprite_docs: Sig<'a, HashMap<String, SpriteDoc>>,
pub asset_docs: Sig<'a, HashMap<String, AssetDoc>>,
/// fn(part_name, part_option_name)
pub select_part_option: &'a dyn Fn(&str, &str, bool),
}
Expand All @@ -18,13 +18,14 @@ impl Component for SpriteSelectTool<'_> {
fn render(self, ctx: &RenderCtx) {
let Self {
wh,
sprite_docs,
asset_docs,
select_part_option,
} = self;

let (selected_sprite_id, set_selected_sprite_id) = ctx.state::<Option<String>>(|| None);
let (selected_part_name, set_selected_part_name) = ctx.state::<Option<String>>(|| None);
let (selected_tags, set_selected_tags) = ctx.state::<HashSet<SystemTag>>(Default::default);
let (selected_tags, set_selected_tags) =
ctx.state::<HashSet<AssetSystemTag>>(Default::default);
let parts = ctx.memo(|| {
let Some(selected_sprite_id) = selected_sprite_id.deref() else {
return Default::default();
Expand All @@ -36,25 +37,29 @@ impl Component for SpriteSelectTool<'_> {
psd_sprite.parts()
});

let tag_filtered_sprite_docs = ctx.memo(|| {
sprite_docs
let tag_filtered_asset_docs = ctx.memo(|| {
asset_docs
.iter()
.filter(|(_id, sprite_doc)| {
sprite_doc.tags.iter().any(|tag| match tag {
SpriteTag::System { tag } => selected_tags.contains(tag),
SpriteTag::Custom { .. } => false,
.filter(|(_id, asset_tag)| {
if !matches!(asset_tag.asset_kind, AssetKind::Sprite) {
return false;
}
asset_tag.tags.iter().any(|tag| match tag {
AssetTag::System { tag } => selected_tags.contains(tag),
AssetTag::Custom { .. } => false,
})
})
.map(|(id, sprite)| (id.clone(), sprite.clone()))
.collect::<HashMap<String, SpriteDoc>>()
.collect::<HashMap<String, AssetDoc>>()
});

let tag_toggle_button = |tag: SystemTag| {
let is_on = selected_tags.contains(&SystemTag::Character);
let tag_toggle_button = |tag: AssetSystemTag| {
let is_on = selected_tags.contains(&AssetSystemTag::SpriteCharacter);
let text = match tag {
SystemTag::Character => "인물",
SystemTag::Object => "사물",
SystemTag::Background => "배경",
AssetSystemTag::SpriteCharacter => "인물",
AssetSystemTag::SpriteObject => "사물",
AssetSystemTag::SpriteBackground => "배경",
_ => unreachable!(),
};

table::ratio(1, move |wh, ctx| {
Expand All @@ -76,11 +81,11 @@ impl Component for SpriteSelectTool<'_> {
64.px(),
table::horizontal([
table::fixed(64.px(), |_, _| {}),
tag_toggle_button(SystemTag::Character),
tag_toggle_button(AssetSystemTag::SpriteCharacter),
table::fixed(16.px(), |_, _| {}),
tag_toggle_button(SystemTag::Object),
tag_toggle_button(AssetSystemTag::SpriteObject),
table::fixed(16.px(), |_, _| {}),
tag_toggle_button(SystemTag::Background),
tag_toggle_button(AssetSystemTag::SpriteBackground),
table::fixed(64.px(), |_, _| {}),
]),
),
Expand All @@ -90,15 +95,11 @@ impl Component for SpriteSelectTool<'_> {
table::ratio(1, |wh, ctx| {
let sprite_column = Column {
wh,
items: tag_filtered_sprite_docs.iter().map(|(id, sprite)| {
items: tag_filtered_asset_docs.iter().map(|(id, sprite)| {
let on_select = || {
set_selected_sprite_id.set(Some(id.clone()));
};
(
sprite.id.as_str(),
sprite.sprite.name.to_string(),
on_select,
)
(sprite.id.as_str(), sprite.name.to_string(), on_select)
}),
};
ctx.add(sprite_column);
Expand Down

0 comments on commit 614c621

Please sign in to comment.