Bläddra i källkod

TerrainEditor: Fixed a few possible bugs/bad code

MeFisto94 8 år sedan
förälder
incheckning
1f597a3032

+ 15 - 7
jme3-terrain-editor/src/com/jme3/gde/terraineditor/AddTerrainAction.java

@@ -116,7 +116,7 @@ public class AddTerrainAction extends AbstractNewSpatialWizardAction {
         com.jme3.material.Material mat = new com.jme3.material.Material(manager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
 
         String assetFolder = "";
-        if (manager != null)
+        if (manager != null) {
             assetFolder = manager.getAssetFolderName();
 
         // write out 3 alpha blend images
@@ -128,12 +128,19 @@ public class AddTerrainAction extends AbstractNewSpatialWizardAction {
                     for (int w=0; w<alphaTextureSize; w++)
                         alphaBlend.setRGB(w, h, 0x00FF0000);//argb
             }
-            File textureFolder = new File(assetFolder+"/Textures/");
-            if (!textureFolder.exists())
-                textureFolder.mkdir();
-            File alphaFolder = new File(assetFolder+"/Textures/terrain-alpha/");
-            if (!alphaFolder.exists())
-                alphaFolder.mkdir();
+            File textureFolder = new File(assetFolder + "/Textures/");
+            if (!textureFolder.exists()) {
+                if (!textureFolder.mkdir()) {
+                    throw new IOException("Could not create the Texture Folder (assets/Textures)!");
+                }
+            }
+            
+            File alphaFolder = new File(assetFolder + "/Textures/terrain-alpha/");
+            if (!alphaFolder.exists()) {
+                if (!alphaFolder.mkdir()) {
+                    throw new IOException("Could not create the Terrain Alpha Folder (assets/Textures/terrain-alpha)!");
+                }
+            }
             String alphaBlendFileName = "/Textures/terrain-alpha/"+sceneName+"-"+((Node)terrain).getName()+"-alphablend"+i+".png";
             File alphaImageFile = new File(assetFolder+alphaBlendFileName);
             ImageIO.write(alphaBlend, "png", alphaImageFile);
@@ -185,6 +192,7 @@ public class AddTerrainAction extends AbstractNewSpatialWizardAction {
 
         //setNeedsSave(true);
         //addSpatialUndo(parent, (Node)terrain, jmeNodeParent);
+        }
         
         return (Spatial)terrain;
     }

+ 4 - 3
jme3-terrain-editor/src/com/jme3/gde/terraineditor/RenameTerrainAction.java

@@ -45,6 +45,7 @@ public class RenameTerrainAction extends AbstractToolWizardAction {
         name = "Rename Terrain Alphamaps";
     }
     
+    @Override
     public Class<?> getNodeClass() {
         return JmeTerrainQuad.class;
     }
@@ -108,11 +109,9 @@ public class RenameTerrainAction extends AbstractToolWizardAction {
     }
     
     private void rename(TerrainQuad quad, String prevName, String newName) {
-        
         ProjectAssetManager manager = (ProjectAssetManager) SceneApplication.getApplication().getAssetManager();
         String texFolder =  "Textures/terrain-alpha/";
         
-        
         // rename the files
         String texFolderFilePath = manager.getAssetFolderName() +"/"+ texFolder;
         String prevPath0 = texFolderFilePath+prevName+"-alphablend0.png";
@@ -162,7 +161,9 @@ public class RenameTerrainAction extends AbstractToolWizardAction {
     
     private void copyFile(File sourceFile, File destFile) throws IOException {
         if(!destFile.exists()) {
-            destFile.createNewFile();
+            if (!destFile.createNewFile()) {
+                throw new IOException(String.format("Could not create File %s, probably because it already exists!", destFile.toString()));
+            }
         }
 
         FileChannel source = null;

+ 3 - 10
jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainCameraController.java

@@ -159,17 +159,13 @@ public class TerrainCameraController extends AbstractCameraController {
      * of the editing.
      */
     private void doTerrainUpdates(float dt) {
-
         if (terrainEditToolActivated) {
             lastModifyTime += dt;
 
             if (lastModifyTime >= toolModifyRate) {
-
                 lastModifyTime = 0;
-                if (terrainEditToolActivated) {
-                    toolController.doTerrainEditToolActivated();
-                    toolController.doTerrainEditToolAlternateActivated();
-                }
+                toolController.doTerrainEditToolActivated();
+                toolController.doTerrainEditToolAlternateActivated();
                 //terrainEditToolActivated = false;
                 lastModifyTime = app.getContext().getTimer().getTime();
             }
@@ -208,13 +204,10 @@ public class TerrainCameraController extends AbstractCameraController {
         ray.setOrigin(pos);
         ray.setDirection(dir);
         editorController.getTerrain(null).collideWith(ray, results);
-        if (results == null) {
+        if (results.size() == 0) {
             return null;
         }
         final CollisionResult result = results.getClosestCollision();
-        if (result == null) {
-            return null;
-        }
         return result.getContactPoint();
     }
 

+ 37 - 17
jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorController.java

@@ -338,6 +338,7 @@ public class TerrainEditorController implements NodeListener {
         } else {
             SceneApplication.getApplication().enqueue(new Callable<Object>() {
 
+                @Override
                 public Object call() throws Exception {
                     generateEntropies(progressMonitor);
                     return null;
@@ -355,15 +356,16 @@ public class TerrainEditorController implements NodeListener {
             Terrain terrain = (Terrain) getTerrain(null);
             if (terrain == null)
                 return 1f;
-            MatParam matParam = null;
-            matParam = terrain.getMaterial().getParam("DiffuseMap_"+layer+"_scale");
-            if (matParam == null)
+            MatParam matParam = terrain.getMaterial().getParam("DiffuseMap_"+layer+"_scale");
+            if (matParam == null) {
                 return -1f;
+            }
             return (Float) matParam.getValue();
         } else {
             try {
                 Float scale =
                     SceneApplication.getApplication().enqueue(new Callable<Float>() {
+                        @Override
                         public Float call() throws Exception {
                             return getTextureScale(layer);
                         }
@@ -393,6 +395,7 @@ public class TerrainEditorController implements NodeListener {
         } else {
             try {
                 SceneApplication.getApplication().enqueue(new Callable() {
+                    @Override
                     public Object call() throws Exception {
                         setTextureScale(layer, scale);
                         return null;
@@ -416,11 +419,12 @@ public class TerrainEditorController implements NodeListener {
             Terrain terrain = (Terrain) getTerrain(null);
             if (terrain == null)
                 return null;
-            MatParam matParam = null;
-            if (layer == 0)
+            MatParam matParam;
+            if (layer == 0) {
                 matParam = terrain.getMaterial().getParam("DiffuseMap");
-            else
+            } else {
                 matParam = terrain.getMaterial().getParam("DiffuseMap_"+layer);
+            }
 
             if (matParam == null || matParam.getValue() == null) {
                 return null;
@@ -432,6 +436,7 @@ public class TerrainEditorController implements NodeListener {
             try {
                 Texture tex =
                     SceneApplication.getApplication().enqueue(new Callable<Texture>() {
+                        @Override
                         public Texture call() throws Exception {
                             return getDiffuseTexture(layer);
                         }
@@ -447,15 +452,25 @@ public class TerrainEditorController implements NodeListener {
     }
 
     private Texture doGetAlphaTexture(Terrain terrain, int alphaLayer) {
-        if (terrain == null)
+        if (terrain == null) {
             return null;
+        }
+        
         MatParam matParam = null;
-        if (alphaLayer == 0)
-            matParam = terrain.getMaterial().getParam("AlphaMap");
-        else if(alphaLayer == 1)
-            matParam = terrain.getMaterial().getParam("AlphaMap_1");
-        else if(alphaLayer == 2)
-            matParam = terrain.getMaterial().getParam("AlphaMap_2");
+        
+        switch (alphaLayer) {
+            case 0:
+                matParam = terrain.getMaterial().getParam("AlphaMap");
+                break;
+            case 1:
+                matParam = terrain.getMaterial().getParam("AlphaMap_1");
+                break;
+            case 2:
+                matParam = terrain.getMaterial().getParam("AlphaMap_2");
+                break;
+            default:
+                throw new IllegalArgumentException("Invalid AlphaLayer");
+        }
         
         if (matParam == null || matParam.getValue() == null) {
             return null;
@@ -500,6 +515,7 @@ public class TerrainEditorController implements NodeListener {
         } else {
             try {
                 SceneApplication.getApplication().enqueue(new Callable() {
+                    @Override
                     public Object call() throws Exception {
                         setDiffuseTexture(layer, texture);
                         return null;
@@ -526,6 +542,7 @@ public class TerrainEditorController implements NodeListener {
         } else {
             try {
                 SceneApplication.getApplication().enqueue(new Callable() {
+                    @Override
                     public Object call() throws Exception {
                         removeTextureLayer(layer);
                         return null;
@@ -591,6 +608,8 @@ public class TerrainEditorController implements NodeListener {
                         color.b = 0; break;
                     case 3:
                         color.a = 0; break;
+                    default:
+                        throw new IllegalArgumentException("Invalid texIndex " + texIndex);
                 }
                 color.clamp();
                 paint.manipulatePixel(image, x, y, color, true); // set the new color
@@ -611,12 +630,13 @@ public class TerrainEditorController implements NodeListener {
             Terrain terrain = (Terrain) getTerrain(null);
             if (terrain == null)
                 return null;
-            MatParam matParam = null;
-            if (layer == 0)
+            MatParam matParam;
+            if (layer == 0) {
                 matParam = terrain.getMaterial().getParam("NormalMap");
-            else
+            } else {
                 matParam = terrain.getMaterial().getParam("NormalMap_"+layer);
-
+            }
+            
             if (matParam == null || matParam.getValue() == null) {
                 return null;
             }

+ 1 - 1
jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorTopComponent.form

@@ -13,7 +13,7 @@
           <ResourceString bundle="com/jme3/gde/terraineditor/Bundle.properties" key="TerrainEditorTopComponent.textureFileChooser.approveButtonText_1" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
         </Property>
         <Property name="currentDirectory" type="java.io.File" editor="org.netbeans.beaninfo.editors.FileEditor">
-          <SerializedValue value="-84,-19,0,5,115,114,0,12,106,97,118,97,46,105,111,46,70,105,108,101,4,45,-92,69,14,13,-28,-1,3,0,1,76,0,4,112,97,116,104,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,120,112,116,0,16,47,65,115,115,101,116,115,47,84,101,120,116,117,114,101,115,119,2,0,47,120"/>
+          <SerializedValue value="-84,-19,0,5,115,114,0,12,106,97,118,97,46,105,111,46,70,105,108,101,4,45,-92,69,14,13,-28,-1,3,0,1,76,0,4,112,97,116,104,116,0,18,76,106,97,118,97,47,108,97,110,103,47,83,116,114,105,110,103,59,120,112,116,0,15,97,115,115,101,116,115,47,84,101,120,116,117,114,101,115,119,2,0,47,120"/>
         </Property>
         <Property name="dialogTitle" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
           <ResourceString bundle="com/jme3/gde/terraineditor/Bundle.properties" key="TerrainEditorTopComponent.textureFileChooser.dialogTitle_1" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>

+ 12 - 5
jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorTopComponent.java

@@ -277,7 +277,7 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce
         hintTextArea = new javax.swing.JTextArea();
 
         textureFileChooser.setApproveButtonText(org.openide.util.NbBundle.getMessage(TerrainEditorTopComponent.class, "TerrainEditorTopComponent.textureFileChooser.approveButtonText_1")); // NOI18N
-        textureFileChooser.setCurrentDirectory(new java.io.File("/Assets/Textures"));
+        textureFileChooser.setCurrentDirectory(new java.io.File("/Users/darki/assets/Textures"));
         textureFileChooser.setDialogTitle(org.openide.util.NbBundle.getMessage(TerrainEditorTopComponent.class, "TerrainEditorTopComponent.textureFileChooser.dialogTitle_1")); // NOI18N
         textureFileChooser.setFileFilter(new ImageFilter());
 
@@ -1121,6 +1121,7 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce
     private void refreshSelected() {
         java.awt.EventQueue.invokeLater(new Runnable() {
 
+            @Override
             public void run() {
                 if (selectedSpat != null) {
                     selectedSpat.refresh(false);
@@ -1133,6 +1134,7 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce
     /**
      * listener for node selection changes
      */
+    @Override
     public void resultChanged(LookupEvent ev) {
         if (currentRequest == null || !currentRequest.isDisplayed()) {
             return;
@@ -1159,20 +1161,25 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce
      */
     private class TerrainNodeListener implements NodeListener {
 
+        @Override
         public void childrenAdded(NodeMemberEvent nme) {
         }
 
+        @Override
         public void childrenRemoved(NodeMemberEvent nme) {
         }
 
+        @Override
         public void childrenReordered(NodeReorderEvent nre) {
         }
 
+        @Override
         public void nodeDestroyed(NodeEvent ne) {
             createTerrainButton.setEnabled(true);
             reinitTextureTable();
         }
 
+        @Override
         public void propertyChange(PropertyChangeEvent evt) {
         }
     }
@@ -1299,6 +1306,7 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce
     }
 
     // runs on AWT thread now
+    @Override
     public void sceneOpened(SceneRequest request) {
 
         if (request.equals(sentRequest) && editorController != null) {
@@ -1356,6 +1364,7 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce
     protected synchronized void addDataObject(final DataObjectSaveNode dataObject) {
         java.awt.EventQueue.invokeLater(new Runnable() {
 
+            @Override
             public void run() {
                 addSaveNode(dataObject);
             }
@@ -1368,10 +1377,7 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce
 
     private void setSceneInfo(final JmeNode jmeNode, final boolean active) {
         final TerrainEditorTopComponent inst = this;
-        if (jmeNode != null) {
-        } else {
-        }
-
+     
         if (!active) {
             result.removeLookupListener(inst);
             close();
@@ -1381,6 +1387,7 @@ public final class TerrainEditorTopComponent extends TopComponent implements Sce
         }
     }
 
+    @Override
     public void sceneClosed(SceneRequest request) {
         if (request.equals(currentRequest)) {
             setActivatedNodes(new org.openide.nodes.Node[]{});