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

Custom asset via plugin #297

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a42fbe4
Add navmesh component type
Dgzt Nov 10, 2024
eab99f7
Init AssetExtension
Dgzt Nov 12, 2024
bc6c8d5
Empty custom asset
Dgzt Nov 14, 2024
92719d7
Inject project manager instead of asset manager
Dgzt Nov 15, 2024
edec40d
Post assert imported event at custom asset import
Dgzt Nov 15, 2024
dddb44b
Load custom asset
Dgzt Nov 15, 2024
e059a3e
Store used assets for custom component
Dgzt Nov 18, 2024
3f84532
Asset usage check for custom component
Dgzt Nov 19, 2024
640e3a9
Move ProjectChangedEvent and SceneChangedEvent to editor-commons
Dgzt Nov 22, 2024
8bda28c
Merge branch 'master' into plugin-custom-asset
Dgzt Nov 23, 2024
346fc30
Add missing EventListener interface
Dgzt Nov 23, 2024
708fa5b
javadoc
Dgzt Nov 23, 2024
a7b7ab2
javadoc #2
Dgzt Nov 23, 2024
20ee0d3
Add addTextField method for plugin's root widget
Dgzt Dec 11, 2024
5cf2e5d
Add properties into meta of custom asset
Dgzt Dec 12, 2024
53502d8
Create MetaCustom at CustomAsset creation
Dgzt Dec 12, 2024
dc4f5c0
Fix save MetaCustom after meta of CustomAsset creation
Dgzt Dec 12, 2024
bed988b
Add markAsModifiedAsset method
Dgzt Dec 12, 2024
36727d1
Fix load custom meta properties
Dgzt Dec 12, 2024
127874c
Delete asset method
Dgzt Dec 13, 2024
6c3dc10
Update asset list when delete asset
Dgzt Dec 13, 2024
2ffcb41
Throws AssetAlreadyExistsException at asset creation
Dgzt Dec 14, 2024
57341b4
Toaster extension
Dgzt Dec 14, 2024
80c276f
Fix compile error
Dgzt Dec 14, 2024
1908fe2
Default value for getAssetIds
Dgzt Dec 14, 2024
1600925
setText on label widget
Dgzt Dec 16, 2024
cbd6927
Use LabelCell as return type
Dgzt Dec 16, 2024
d97240b
Label cell from Cell
Dgzt Dec 16, 2024
6025bf9
Check supported component types
Dgzt Dec 22, 2024
abcbe36
Remove unused game object parameter
Dgzt Dec 22, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ public Asset loadAsset(Meta meta) throws AssetNotFoundException {
case SKYBOX:
asset = loadSkyboxAsset(meta, assetFile);
break;
case CUSTOM:
asset = loadCustomAsset(meta, assetFile);
break;
default:
return null;
}
Expand All @@ -410,6 +413,12 @@ private Asset loadSkyboxAsset(Meta meta, FileHandle assetFile) {
return asset;
}

private CustomAsset loadCustomAsset(Meta mea, FileHandle assetFile) {
final CustomAsset asset = new CustomAsset(mea, assetFile);
asset.load(gdxAssetManager);
return asset;
}

private MaterialAsset loadMaterialAsset(Meta meta, FileHandle assetFile) {
MaterialAsset asset = new MaterialAsset(meta, assetFile);
asset.load(gdxAssetManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public enum AssetType {
/** Water file. Contains data for water. */
WATER("Water"),
/** Skybox file. Holds reference to skybox textures. **/
SKYBOX("Skybox");
SKYBOX("Skybox"),
/** Custom file. For plugins. **/
CUSTOM("Custom");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2024. See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mbrlabs.mundus.commons.assets;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.ObjectMap;
import com.mbrlabs.mundus.commons.assets.meta.Meta;

import java.util.Map;

/**
* Custom asset for plugins.
*/
public class CustomAsset extends Asset {

/**
* @param meta The meta file.
* @param assetFile The asset file.
*/
public CustomAsset(Meta meta, FileHandle assetFile) {
super(meta, assetFile);
}

@Override
public void load() {

}

@Override
public void load(AssetManager assetManager) {

}

@Override
public void resolveDependencies(Map<String, Asset> assets) {

}

@Override
public void applyDependencies() {

}

@Override
public void dispose() {

}

@Override
public boolean usesAsset(Asset assetToCheck) {
return false;
}

public ObjectMap<String, String> getProperties() {
return meta.getCustom().getProperties();
}
}
11 changes: 11 additions & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/assets/meta/Meta.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Meta {
public static final String JSON_WATER = "wat";
public static final String JSON_MODEL = "mdl";
public static final String JSON_SKY_BOX = "sky";
public static final String JSON_CUSTOM = "cus";

private int version;
private long lastModified;
Expand All @@ -46,6 +47,7 @@ public class Meta {

private MetaModel model;
private MetaTerrain terrain;
private MetaCustom custom;

private final FileHandle file;

Expand Down Expand Up @@ -105,6 +107,14 @@ public void setTerrain(MetaTerrain terrain) {
this.terrain = terrain;
}

public MetaCustom getCustom() {
return custom;
}

public void setCustom(MetaCustom custom) {
this.custom = custom;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -131,6 +141,7 @@ public String toString() {
", type=" + type +
", model=" + model +
", terrain=" + terrain +
", custom=" + custom +
", file=" + file +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024. See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mbrlabs.mundus.commons.assets.meta;

import com.badlogic.gdx.utils.ObjectMap;

public class MetaCustom {

public static final String JSON_PROPERTIES = "properties";

private final ObjectMap<String, String> properties = new ObjectMap<>();

public ObjectMap<String, String> getProperties() {
return properties;
}

@Override
public String toString() {
return "MetaCustom{" +
"properties=" + properties +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public Meta load(FileHandle file) throws MetaFileParseException {
parseTerrain(meta, json.get(Meta.JSON_TERRAIN));
} else if(meta.getType() == AssetType.MODEL) {
parseModel(meta, json.get(Meta.JSON_MODEL));
} else if (meta.getType() == AssetType.CUSTOM) {
parseCustom(meta, json.get(Meta.JSON_CUSTOM));
}

return meta;
Expand Down Expand Up @@ -97,6 +99,22 @@ private void parseModel(Meta meta, JsonValue jsonModel) {
meta.setModel(model);
}

private void parseCustom(Meta meta, JsonValue jsonCustom) {
if (jsonCustom == null) return;

final MetaCustom custom = new MetaCustom();

final JsonValue properties = jsonCustom.get(MetaCustom.JSON_PROPERTIES);
for (int i = 0; i < properties.size; ++i) {
final JsonValue property = properties.get(i);
final String id = property.name;
final String value = properties.getString(id);
custom.getProperties().put(id, value);
}

meta.setCustom(custom);
}

/**
* When new values are added and cannot be found on jsonValue.getXXX(),
* an IllegalArgumentException is thrown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@

package com.mbrlabs.mundus.commons.dto;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.OrderedMap;
import com.mbrlabs.mundus.commons.assets.Asset;

public class CustomComponentDTO {
import java.util.Map;

public class CustomComponentDTO implements AssetUsageDTO {

private String componentType;

private OrderedMap<String, String> properties;

private Array<String> assetIds;

public String getComponentType() {
return componentType;
}
Expand All @@ -39,4 +45,27 @@ public OrderedMap<String, String> getProperties() {
public void setProperties(final OrderedMap<String, String> properties) {
this.properties = properties;
}

public Array<String> getAssetIds() {
return assetIds;
}

public void setAssetIds(final Array<String> assetIds) {
this.assetIds = assetIds;
}

@Override
public boolean usesAsset(final Asset assetToCheck, final Map<String, Asset> assetMap) {
if (assetIds == null) {
return false;
}

for (int i = 0; i < assetIds.size; ++i) {
if (assetToCheck.getID().equals(assetIds.get(i))) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ public boolean usesAsset(Asset assetToCheck, Map<String, Asset> assetMap) {
return true;
}

if (customComponents != null) {
for (int i = 0; i < customComponents.size; ++i) {
final CustomComponentDTO customComponent = customComponents.get(i);
if (customComponent.usesAsset(assetToCheck, assetMap)) {
return true;
}
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.mbrlabs.mundus.commons.mapper;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.OrderedMap;
import com.mbrlabs.mundus.commons.assets.Asset;
import com.mbrlabs.mundus.commons.scene3d.GameObject;
import com.mbrlabs.mundus.commons.scene3d.components.Component;

Expand All @@ -35,12 +38,22 @@ public interface CustomComponentConverter {
*/
OrderedMap<String, String> convert(Component component);

/**
* @param component The component.
*
* @return The ID of used assets.
*/
default Array<String> getAssetIds(Component component) {
return null;
}

/**
* Converts map into custom component.
*
* @param gameObject The game object.
* @param componentProperties The properties of custom component.
* @param assets The assets of component.
* @return The custom component.
*/
Component convert(GameObject gameObject, OrderedMap<String, String> componentProperties);
Component convert(GameObject gameObject, OrderedMap<String, String> componentProperties, ObjectMap<String, Asset> assets);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public interface Component {

enum Type {
MODEL, TERRAIN, LIGHT, PARTICLE_SYSTEM, WATER, CUSTOM_PROPERTIES, PHYSICS
MODEL, TERRAIN, LIGHT, PARTICLE_SYSTEM, WATER, CUSTOM_PROPERTIES, PHYSICS, NAVMESH
}

GameObject getGameObject();
Expand Down
47 changes: 47 additions & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/utils/AssetUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2021. See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mbrlabs.mundus.commons.utils;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import com.mbrlabs.mundus.commons.assets.Asset;

import java.util.Map;

/**
* Utils for assets.
*/
public class AssetUtils {

public static ObjectMap<String, Asset> getAssetsById(final Array<String> assetIds, final Map<String, Asset> assetMap) {
final ObjectMap<String, Asset> retMap = new ObjectMap<>();
if (assetIds != null) {
for (int i = 0; i < assetIds.size; ++i) {
final String assetId = assetIds.get(i);
if (assetId != null) {
final Asset asset = assetMap.get(assetId);

if (asset != null) {
retMap.put(assetId, asset);
}
}
}
}

return retMap;
}
}
Loading
Loading