@ -62,6 +62,7 @@ fn main() {
) )
. init_resource ::< AssetRegistry > ( )
. insert_resource ( AssetsDir ::new ( "assets/scratch" ) )
. init_resource ::< FontInfo > ( )
. add_event ::< ImportAsset > ( )
. add_asset ::< Monologue > ( )
. init_asset_loader ::< MonologueLoader > ( )
@ -86,13 +87,14 @@ fn main() {
Update ,
(
gltf_ui ,
fonts_ui ,
texts_ui ,
control_active_gltf ,
show_preview_text ,
sync_monologue_font ,
) ,
)
. add_systems ( Update , ( fonts_ui , set_active_font ) )
. add_systems ( Startup , reload_assets )
. add_systems (
Update ,
(
@ -100,8 +102,8 @@ fn main() {
import_assets ,
import_file ,
import_folder ,
reload_assets ,
clear_assets ,
reload_assets .run_if ( ui ::event ::< ImportAsset > ) ,
clear_assets .run_if ( ui ::activated ::< ClearAssets > ) ,
) ,
)
. add_systems (
@ -570,17 +572,20 @@ mod assets {
let newpath = PathBuf ::from ( assets_dir . root . clone ( ) ) . join ( fname ) ;
IoTaskPool ::get ( )
. spawn ( async move {
if path = = newpath {
info ! ( "Skipping copying {:?} to itself {:?}" , path , newpath ) ;
} else {
match copy ( path , newpath ) {
Ok ( _ ) = > info ! ( "Created assets directory" ) ,
Err ( e ) = > warn ! ( "Error creating assets directory {:?}" , e ) ,
}
}
} )
. detach ( ) ;
} ) ;
}
fn copy_dir ( from : PathBuf , to : PathBuf ) -> std ::io ::Result < ( ) > {
info ! ( "Copy dir {:?} -> {:?}" , from , to ) ;
if let Ok ( entries ) = read_dir ( from ) {
for entry in entries . filter_map ( | e | e . ok ( ) ) {
if entry . path ( ) . is_file ( ) {
@ -611,28 +616,28 @@ mod assets {
info ! ( "copying folder {:?} to {:?}" , path . clone ( ) , newpath ) ;
IoTaskPool ::get ( )
. spawn ( async move {
if path . clone ( ) = = newpath {
info ! ( "Skipping copying {:?} to itself {:?}" , path , newpath ) ;
} else {
match copy_dir ( path . clone ( ) , newpath ) {
Ok ( _ ) = > info ! ( "Created assets directory" ) ,
Err ( e ) = > warn ! ( "Error creating assets directory {:?}" , e ) ,
}
}
} )
. detach ( ) ;
} ) ;
}
pub fn reload_assets (
mut events : EventReader < ImportAsset > ,
server : Res < AssetServer > ,
mut registry : ResMut < AssetRegistry > ,
assets_dir : Res < AssetsDir > ,
) {
// Reduce all import events in a frame to one "load_folder" action
if events . iter ( ) . len ( ) > 0 {
registry . 0 = server
. load_folder ( assets_dir . root . clone ( ) )
. expect ( "Reload assets folder" ) ;
}
}
pub fn get_asset_name < T : Asset > ( server : & AssetServer , handle : Handle < T > ) -> String {
if let Some ( asset_path ) = server . get_handle_path ( handle . clone ( ) ) {
@ -1076,6 +1081,11 @@ mod fonts {
#[ derive(Debug, Component, Default) ]
pub struct FontWidget ;
#[ derive(Debug, Resource, Default) ]
pub struct FontInfo {
pub default : Option < Handle < Font > > ,
}
pub fn fonts_ui (
mut events : EventReader < AssetEvent < Font > > ,
mut commands : Commands ,
@ -1127,6 +1137,15 @@ mod fonts {
}
} ) ;
}
pub fn set_active_font (
events : Query < & ui ::TargetAsset < Font > , Added < ui ::Active > > ,
mut font : ResMut < FontInfo > ,
) {
events
. iter ( )
. for_each ( | ui ::TargetAsset { handle } | font . default = Some ( handle . clone ( ) ) ) ;
}
}
use monologues ::* ;
@ -1250,6 +1269,7 @@ mod monologues {
monologues : Res < Assets < Monologue > > ,
container : Query < Entity , With < MonologueContainer > > ,
mut commands : Commands ,
font : Res < FontInfo > ,
) {
added
. iter ( )
@ -1286,15 +1306,21 @@ mod monologues {
} ,
ui ::Sorting ( 0 ) ,
) ) ;
parent . spawn ( (
TextBundle ::from_section (
monologue . text . clone ( ) ,
TextStyle {
let style = match & font . default {
Some ( handle ) = > TextStyle {
color : Color ::BLACK . into ( ) ,
font_size : 16.0 ,
font : handle . clone ( ) ,
.. default ( )
} ,
) ,
None = > TextStyle {
color : Color ::BLACK . into ( ) ,
font_size : 16.0 ,
.. default ( )
} ,
} ;
parent . spawn ( (
TextBundle ::from_section ( monologue . text . clone ( ) , style ) ,
handle . clone ( ) ,
) ) ;
} ) ;
@ -1304,18 +1330,21 @@ mod monologues {
// TODO: Sync Handle<Monologue> and TextStyle components to automagically generate and sync text
pub fn sync_monologue_font (
events : Query < & ui ::TargetAsset < Font > , Added < ui ::Active > > ,
mut texts : Query < & mut Text , With < Handle < Monologue > > > ,
font : Res < FontInfo > ,
) {
events . iter ( ) . for_each ( | ui ::TargetAsset { handle } | {
if font . is_changed ( ) | | font . is_added ( ) {
texts . iter_mut ( ) . for_each ( | mut text | {
text . sections . iter_mut ( ) . for_each ( | section | {
section . style . font = handle . clone ( ) ;
} ) ;
text . sections
. iter_mut ( )
. for_each ( | section | match & font . default {
Some ( handle ) = > section . style . font = handle . clone ( ) ,
None = > section . style . font = Handle ::default ( ) ,
} ) ;
} ) ;
}
}
}
use cameras ::* ;
mod cameras {
@ -1448,7 +1477,6 @@ mod reset {
pub struct ClearAssets ;
pub fn clear_assets (
events : Query < Entity , ( With < ClearAssets > , Added < ui ::Active > ) > ,
asset_holders : Query <
Entity ,
Or < (
@ -1469,7 +1497,6 @@ mod reset {
mut registry : ResMut < AssetRegistry > ,
mut commands : Commands ,
) {
events . iter ( ) . for_each ( | _ | {
info ! ( "Clearing assets" ) ;
// Clear buttons holding asset references
@ -1479,6 +1506,5 @@ mod reset {
// Empty asset registry
registry . 0. clear ( ) ;
} )
}
}