Skip to content

Commit

Permalink
load_image!/load_texture! macros
Browse files Browse the repository at this point in the history
- reduces code bloat
  • Loading branch information
mrDIMAS committed Nov 11, 2024
1 parent 0f5f24a commit a06e324
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 113 deletions.
2 changes: 1 addition & 1 deletion editor/src/absm/toolbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Toolbar {
.with_margin(Thickness::uniform(1.0))
.with_background(BRUSH_BRIGHT),
)
.with_opt_texture(load_image(include_bytes!("../../resources/filter.png")))
.with_opt_texture(load_image!("../../resources/filter.png"))
.build(ctx),
)
.build(ctx);
Expand Down
24 changes: 6 additions & 18 deletions editor/src/animation/toolbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,7 @@ impl Toolbar {
.with_margin(Thickness::uniform(1.0))
.with_background(BRUSH_BRIGHT),
)
.with_opt_texture(load_image(include_bytes!(
"../../resources/import.png"
)))
.with_opt_texture(load_image!("../../resources/import.png"))
.build(ctx),
)
.build(ctx);
Expand Down Expand Up @@ -500,9 +498,7 @@ impl Toolbar {
.with_margin(Thickness::uniform(1.0))
.with_background(BRUSH_BRIGHT),
)
.with_opt_texture(load_image(include_bytes!(
"../../resources/reimport.png"
)))
.with_opt_texture(load_image!("../../resources/reimport.png"))
.build(ctx),
)
.build(ctx);
Expand All @@ -529,9 +525,7 @@ impl Toolbar {
.with_margin(Thickness::uniform(1.0))
.with_background(BRUSH_BRIGHT),
)
.with_opt_texture(load_image(include_bytes!(
"../../resources/rename.png"
)))
.with_opt_texture(load_image!("../../resources/rename.png"))
.build(ctx),
)
.build(ctx);
Expand Down Expand Up @@ -584,9 +578,7 @@ impl Toolbar {
.with_margin(Thickness::uniform(1.0))
.with_background(BRUSH_BRIGHT),
)
.with_opt_texture(load_image(include_bytes!(
"../../resources/copy.png"
)))
.with_opt_texture(load_image!("../../resources/copy.png"))
.build(ctx),
)
.build(ctx);
Expand Down Expand Up @@ -640,9 +632,7 @@ impl Toolbar {
.with_margin(Thickness::uniform(1.0))
.with_background(BRUSH_BRIGHT),
)
.with_opt_texture(load_image(include_bytes!(
"../../resources/speed.png"
)))
.with_opt_texture(load_image!("../../resources/speed.png"))
.build(ctx),
)
.with_child({
Expand All @@ -668,9 +658,7 @@ impl Toolbar {
.with_margin(Thickness::uniform(1.0))
.with_background(BRUSH_BRIGHT),
)
.with_opt_texture(load_image(include_bytes!(
"../../resources/time.png"
)))
.with_opt_texture(load_image!("../../resources/time.png"))
.build(ctx),
)
.with_child({
Expand Down
20 changes: 6 additions & 14 deletions editor/src/animation/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,7 @@ impl Toolbar {
.with_width(16.0)
.with_height(16.0),
)
.with_opt_texture(load_image(include_bytes!(
"../../resources/collapse.png"
)))
.with_opt_texture(load_image!("../../resources/collapse.png"))
.build(ctx),
)
.build(ctx);
Expand All @@ -573,7 +571,7 @@ impl Toolbar {
.with_width(16.0)
.with_height(16.0),
)
.with_opt_texture(load_image(include_bytes!("../../resources/expand.png")))
.with_opt_texture(load_image!("../../resources/expand.png"))
.build(ctx),
)
.build(ctx);
Expand Down Expand Up @@ -675,9 +673,7 @@ impl TrackList {
ctx,
22.0,
22.0,
load_image(include_bytes!(
"../../resources/property_track.png"
)),
load_image!("../../resources/property_track.png"),
"Add Property Track.\n\
Create generic property binding to a numeric property.",
Some(0),
Expand All @@ -689,9 +685,7 @@ impl TrackList {
ctx,
22.0,
22.0,
load_image(include_bytes!(
"../../resources/position_track.png"
)),
load_image!("../../resources/position_track.png"),
"Add Position Track.\n\
Creates a binding to a local position of a node. \
Such binding is much more performant than generic \
Expand All @@ -705,7 +699,7 @@ impl TrackList {
ctx,
22.0,
22.0,
load_image(include_bytes!("../../resources/scaling_track.png")),
load_image!("../../resources/scaling_track.png"),
"Add Scale Track.\n\
Creates a binding to a local scale of a node. \
Such binding is much more performant than generic \
Expand All @@ -719,9 +713,7 @@ impl TrackList {
ctx,
22.0,
22.0,
load_image(include_bytes!(
"../../resources/rotation_track.png"
)),
load_image!("../../resources/rotation_track.png"),
"Add Rotation Track.\n\
Creates a binding to a local rotation of a node. \
Such binding is much more performant than generic \
Expand Down
8 changes: 3 additions & 5 deletions editor/src/asset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl AssetBrowser {
ctx,
18.0,
18.0,
load_image(include_bytes!("../../resources/reimport.png")),
load_image!("../../resources/reimport.png"),
"Refresh",
Some(1),
);
Expand Down Expand Up @@ -819,7 +819,7 @@ impl AssetBrowser {
WidgetBuilder::new().with_context_menu(self.context_menu.menu.clone()),
)
.with_icon(if is_dir {
load_image(include_bytes!("../../resources/folder.png"))
load_image!("../../resources/folder.png")
} else {
None
})
Expand Down Expand Up @@ -924,9 +924,7 @@ impl AssetBrowser {
let asset_item = AssetItemBuilder::new(
WidgetBuilder::new().with_context_menu(self.context_menu.menu.clone()),
)
.with_icon(load_image(include_bytes!(
"../../resources/folder_return.png"
)))
.with_icon(load_image!("../../resources/folder_return.png"))
.with_path(parent_path)
.build(
resource_manager.clone(),
Expand Down
18 changes: 9 additions & 9 deletions editor/src/asset/preview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl AssetPreviewGenerator for SoundPreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/sound.png"))
load_image!("../../../resources/sound.png")
}
}

Expand Down Expand Up @@ -410,7 +410,7 @@ impl AssetPreviewGenerator for ModelPreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/model.png"))
load_image!("../../../resources/model.png")
}
}

Expand Down Expand Up @@ -451,7 +451,7 @@ impl AssetPreviewGenerator for SurfaceDataPreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/model.png"))
load_image!("../../../resources/model.png")
}
}

Expand Down Expand Up @@ -495,7 +495,7 @@ impl AssetPreviewGenerator for ShaderPreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/shader.png"))
load_image!("../../../resources/shader.png")
}
}

Expand Down Expand Up @@ -539,7 +539,7 @@ impl AssetPreviewGenerator for MaterialPreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/material.png"))
load_image!("../../../resources/material.png")
}
}

Expand Down Expand Up @@ -570,7 +570,7 @@ impl AssetPreviewGenerator for HrirPreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/hrir.png"))
load_image!("../../../resources/hrir.png")
}
}

Expand Down Expand Up @@ -600,7 +600,7 @@ impl AssetPreviewGenerator for CurvePreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/curve.png"))
load_image!("../../../resources/curve.png")
}
}

Expand Down Expand Up @@ -678,7 +678,7 @@ impl AssetPreviewGenerator for FontPreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/font.png"))
load_image!("../../../resources/font.png")
}
}

Expand Down Expand Up @@ -713,6 +713,6 @@ impl AssetPreviewGenerator for UserInterfacePreview {
_resource: &UntypedResource,
_resource_manager: &ResourceManager,
) -> Option<UntypedResource> {
load_image(include_bytes!("../../../resources/ui.png"))
load_image!("../../../resources/ui.png")
}
}
6 changes: 3 additions & 3 deletions editor/src/command/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl CommandStackViewer {
ctx,
20.0,
20.0,
load_image(include_bytes!("../../resources/undo.png")),
load_image!("../../resources/undo.png"),
"Undo The Command",
Some(0),
);
Expand All @@ -78,7 +78,7 @@ impl CommandStackViewer {
ctx,
20.0,
20.0,
load_image(include_bytes!("../../resources/redo.png")),
load_image!("../../resources/redo.png"),
"Redo The Command",
Some(1),
);
Expand All @@ -89,7 +89,7 @@ impl CommandStackViewer {
ctx,
20.0,
20.0,
load_image(include_bytes!("../../resources/clear.png")),
load_image!("../../resources/clear.png"),
"Clear Command Stack\nChanges history will be erased.",
Some(2),
);
Expand Down
4 changes: 2 additions & 2 deletions editor/src/inspector/editors/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use crate::fyrox::{
scene::node::Node,
};
use crate::{
load_image,
load_image_internal,
message::MessageSender,
scene::selector::{HierarchyNode, NodeSelectorMessage, NodeSelectorWindowBuilder},
world::graph::item::SceneItem,
Expand Down Expand Up @@ -292,7 +292,7 @@ fn make_icon(data: &[u8], color: Color, ctx: &mut BuildContext) -> Handle<UiNode
.with_margin(Thickness::uniform(1.0))
.with_background(Brush::Solid(color)),
)
.with_opt_texture(load_image(data))
.with_opt_texture(load_image_internal(data))
.build(ctx)
}

Expand Down
4 changes: 2 additions & 2 deletions editor/src/inspector/editors/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ where
.with_height(16.0)
.with_margin(Thickness::uniform(1.0)),
)
.with_opt_texture(load_image(include_bytes!(
.with_opt_texture(load_image!(
"../../../resources/sound_source.png"
)))
))
.build(ctx),
)
.with_child({
Expand Down
2 changes: 1 addition & 1 deletion editor/src/inspector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl InspectorPlugin {
ctx,
18.0,
18.0,
load_image(include_bytes!("../../resources/doc.png")),
load_image!("../../resources/doc.png"),
"Open Documentation",
Some(0),
);
Expand Down
4 changes: 2 additions & 2 deletions editor/src/interaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
},
scene::{camera::Projection, graph::Graph, node::Node},
},
load_image,
load_image_internal,
message::MessageSender,
scene::{controller::SceneController, Selection},
settings::Settings,
Expand Down Expand Up @@ -231,7 +231,7 @@ pub fn make_interaction_mode_button(
.with_width(23.0)
.with_height(23.0),
)
.with_opt_texture(load_image(image))
.with_opt_texture(load_image_internal(image))
.build(ctx),
)
.build(ctx)
Expand Down
20 changes: 17 additions & 3 deletions editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ lazy_static! {
static ref EDITOR_TEXTURE_CACHE: Mutex<FxHashMap<usize, TextureResource>> = Default::default();
}

pub fn load_texture(data: &[u8]) -> Option<TextureResource> {
pub fn load_texture_internal(data: &[u8]) -> Option<TextureResource> {
let mut cache = EDITOR_TEXTURE_CACHE.lock();

// Editor use data that is embedded in the binary, so each such piece of data will have fixed
Expand All @@ -227,8 +227,22 @@ pub fn load_texture(data: &[u8]) -> Option<TextureResource> {
}
}

pub fn load_image(data: &[u8]) -> Option<UntypedResource> {
Some(load_texture(data)?.into())
pub fn load_image_internal(data: &[u8]) -> Option<UntypedResource> {
Some(load_texture_internal(data)?.into())
}

#[macro_export]
macro_rules! load_texture {
($file:expr $(,)?) => {
$crate::load_texture_internal(include_bytes!($file))
};
}

#[macro_export]
macro_rules! load_image {
($file:expr $(,)?) => {
$crate::load_image_internal(include_bytes!($file))
};
}

lazy_static! {
Expand Down
2 changes: 1 addition & 1 deletion editor/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl LogPanel {
ctx,
24.0,
24.0,
load_image(include_bytes!("../resources/clear.png")),
load_image!("../resources/clear.png"),
"Clear the log.",
Some(0),
);
Expand Down
2 changes: 1 addition & 1 deletion editor/src/plugins/collider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn make_handle(scene: &mut Scene, root: Handle<Node>, visible: bool) -> Handle<N

material.bind(
"diffuseTexture",
load_texture(include_bytes!("../../../resources/circle.png")),
load_texture!("../../../resources/circle.png"),
);

let handle = SpriteBuilder::new(BaseBuilder::new().with_visibility(visible))
Expand Down
Loading

0 comments on commit a06e324

Please sign in to comment.