11package com .ss .editor .file .converter .impl ;
22
3+ import static com .ss .editor .extension .property .EditablePropertyType .*;
34import static com .ss .editor .util .EditorUtil .getAssetFile ;
45import static com .ss .editor .util .EditorUtil .toAssetPath ;
56import static com .ss .rlib .util .FileUtils .containsExtensions ;
1314import com .jme3 .scene .Geometry ;
1415import com .jme3 .scene .Spatial ;
1516import com .ss .editor .FileExtensions ;
17+ import com .ss .editor .Messages ;
1618import com .ss .editor .annotation .BackgroundThread ;
1719import com .ss .editor .annotation .FXThread ;
1820import com .ss .editor .model .tool .TangentGenerator ;
21+ import com .ss .editor .plugin .api .dialog .GenericFactoryDialog ;
22+ import com .ss .editor .plugin .api .property .PropertyDefinition ;
1923import com .ss .editor .serializer .MaterialSerializer ;
20- import com .ss .editor .ui .dialog .converter .ModelConverterDialog ;
2124import com .ss .editor .util .EditorUtil ;
2225import com .ss .editor .util .NodeUtils ;
26+ import com .ss .rlib .util .FileUtils ;
2327import com .ss .rlib .util .StringUtils ;
28+ import com .ss .rlib .util .VarTable ;
2429import com .ss .rlib .util .array .Array ;
2530import com .ss .rlib .util .array .ArrayFactory ;
2631import com .ss .rlib .util .dictionary .DictionaryFactory ;
2732import com .ss .rlib .util .dictionary .ObjectDictionary ;
2833import org .jetbrains .annotations .NotNull ;
2934
35+ import java .awt .*;
3036import java .io .IOException ;
3137import java .io .OutputStream ;
3238import java .io .PrintWriter ;
4046 */
4147public abstract class AbstractModelFileConverter extends AbstractFileConverter {
4248
49+ @ NotNull
50+ private static final Point DIALOG_SIZE = new Point (500 , -1 );
51+
52+ private static final String PROP_RESULT_NAME = "resultName" ;
53+ private static final String PROP_DESTINATION = "destination" ;
54+ private static final String PROP_EXPORT_MATERIALS = "exportMaterials" ;
55+ private static final String PROP_MATERIALS_FOLDER = "materialsFolder" ;
56+ private static final String PROP_OVERWRITE_MATERIALS = "overwriteMaterials" ;
57+
58+ @ NotNull
59+ private static final Array <String > MATERIAL_DEPS = ArrayFactory .asArray (PROP_EXPORT_MATERIALS );
60+
4361 @ Override
4462 public void convert (@ NotNull final Path source , @ NotNull final Path destination ) {
4563
@@ -52,19 +70,57 @@ public void convert(@NotNull final Path source, @NotNull final Path destination)
5270 throw new IllegalArgumentException ("incorrect extension of file " + source );
5371 }
5472
55- final ModelConverterDialog dialog = new ModelConverterDialog (source , destination , settings -> convert (source , settings ));
73+ final String resultName = FileUtils .getNameWithoutExtension (source );
74+ final Path assetDestination = EditorUtil .getAssetFile (destination );
75+
76+ final Array <PropertyDefinition > definitions = ArrayFactory .newArray (PropertyDefinition .class );
77+ definitions .add (new PropertyDefinition (STRING , Messages .MODEL_CONVERTER_DIALOG_RESULT_NAME , PROP_RESULT_NAME , resultName ));
78+ definitions .add (new PropertyDefinition (FOLDER_FROM_ASSET_FOLDER , Messages .MODEL_CONVERTER_DIALOG_DESTINATION_FOLDER , PROP_DESTINATION , assetDestination ));
79+ definitions .add (new PropertyDefinition (BOOLEAN , Messages .MODEL_CONVERTER_DIALOG_EXPORT_MATERIALS , PROP_EXPORT_MATERIALS , false ));
80+ definitions .add (new PropertyDefinition (FOLDER_FROM_ASSET_FOLDER , MATERIAL_DEPS , Messages .MODEL_CONVERTER_DIALOG_MATERIAL_FOLDER , PROP_MATERIALS_FOLDER , null ));
81+ definitions .add (new PropertyDefinition (BOOLEAN , MATERIAL_DEPS , Messages .MODEL_CONVERTER_DIALOG_OVERWRITE_MATERIALS , PROP_OVERWRITE_MATERIALS , false ));
82+
83+ final GenericFactoryDialog dialog = new GenericFactoryDialog (definitions , vars -> convert (source , vars ), this ::validate );
84+ dialog .setButtonOkText (Messages .SIMPLE_DIALOG_BUTTON_CONVERT );
85+ dialog .setTitle (Messages .MODEL_CONVERTER_DIALOG_TITLE );
86+ dialog .configureSize (DIALOG_SIZE );
5687 dialog .show ();
5788 }
5889
90+ /**
91+ * Validate the settings.
92+ *
93+ * @param vars the variables.
94+ * @return true if all are ok.
95+ */
96+ @ FXThread
97+ private boolean validate (@ NotNull final VarTable vars ) {
98+
99+ if (!vars .has (PROP_RESULT_NAME ) || !vars .has (PROP_DESTINATION )) {
100+ return false ;
101+ }
102+
103+ final String resultName = vars .getString (PROP_RESULT_NAME );
104+ if (StringUtils .isEmpty (resultName )) return false ;
105+
106+ final boolean exportMaterials = vars .getBoolean (PROP_EXPORT_MATERIALS );
107+
108+ if (exportMaterials && !vars .has (PROP_MATERIALS_FOLDER )) {
109+ return false ;
110+ }
111+
112+ return true ;
113+ }
114+
59115 /**
60116 * Convert a file using settings from the dialog.
61117 */
62118 @ FXThread
63- private void convert (@ NotNull final Path source , @ NotNull final ModelConverterDialog dialog ) {
119+ private void convert (@ NotNull final Path source , @ NotNull final VarTable vars ) {
64120 EditorUtil .incrementLoading ();
65121 EXECUTOR_MANAGER .addBackgroundTask (() -> {
66122 try {
67- convertImpl (source , dialog );
123+ convertImpl (source , vars );
68124 } catch (final Exception e ) {
69125 EditorUtil .handleException (LOGGER , this , e );
70126 EXECUTOR_MANAGER .addFXTask (EditorUtil ::decrementLoading );
@@ -76,10 +132,10 @@ private void convert(@NotNull final Path source, @NotNull final ModelConverterDi
76132 * Convert a file using settings from the dialog.
77133 */
78134 @ BackgroundThread
79- private void convertImpl (@ NotNull final Path source , @ NotNull final ModelConverterDialog dialog ) throws IOException {
135+ private void convertImpl (@ NotNull final Path source , @ NotNull final VarTable vars ) throws IOException {
80136
81- final String filename = dialog . getFilename ( );
82- final Path destinationFolder = dialog . getDestinationFolder ( );
137+ final String filename = vars . getString ( PROP_RESULT_NAME );
138+ final Path destinationFolder = vars . get ( PROP_DESTINATION );
83139 final Path destination = destinationFolder .resolve (filename + "." + FileExtensions .JME_OBJECT );
84140 final boolean isOverwrite = Files .exists (destination );
85141
@@ -93,13 +149,13 @@ private void convertImpl(@NotNull final Path source, @NotNull final ModelConvert
93149 TangentGenerator .useMikktspaceGenerator (model );
94150 }
95151
96- if (dialog . isExportMaterials ( )) {
152+ if (vars . getBoolean ( PROP_EXPORT_MATERIALS )) {
97153
98154 final Array <Geometry > geometries = ArrayFactory .newArray (Geometry .class );
99155 final ObjectDictionary <String , Geometry > mapping = DictionaryFactory .newObjectDictionary ();
100156
101- final Path materialsFolder = dialog . getMaterialsFolder ( );
102- final boolean canOverwrite = dialog . isOverwriteMaterials ( );
157+ final Path materialsFolder = vars . get ( PROP_MATERIALS_FOLDER );
158+ final boolean canOverwrite = vars . getBoolean ( PROP_OVERWRITE_MATERIALS );
103159
104160 NodeUtils .visitGeometry (model , geometry -> checkAndAdd (geometries , geometry ));
105161 geometries .forEach (geometry -> generateNames (mapping , geometry ));
0 commit comments