Bläddra i källkod

Image viewer: better errors

lviguier 1 år sedan
förälder
incheckning
5710e8afca
2 ändrade filer med 30 tillägg och 10 borttagningar
  1. 21 7
      hide/comp/Scene.hx
  2. 9 3
      hide/view/Image.hx

+ 21 - 7
hide/comp/Scene.hx

@@ -348,11 +348,14 @@ class Scene extends hide.comp.Component implements h3d.IDrawable {
 		return loadTexture("", t, onReady);
 	}
 
-	public function loadTexture( modelPath : String, texturePath : String, ?onReady : h3d.mat.Texture -> Void, async=false, ?uncompressed: Bool = false) {
+	public function loadTexture( modelPath : String, texturePath : String, ?onReady : h3d.mat.Texture -> Void,  ?onFail : Void -> Void, async=false, ?uncompressed: Bool = false) {
 		checkCurrent();
 		var path = resolvePath(modelPath, texturePath);
 		if( path == null ) {
-			ide.error("Could not load texture " + { modelPath : modelPath, texturePath : texturePath });
+			if (onFail != null)
+				onFail();
+			else
+				ide.error("Could not load texture " + { modelPath : modelPath, texturePath : texturePath });
 			return null;
 		}
 		var t = texCache.get(path);
@@ -362,14 +365,22 @@ class Scene extends hide.comp.Component implements h3d.IDrawable {
 		}
 		var relPath = StringTools.startsWith(path, ide.resourceDir) ? path.substr(ide.resourceDir.length+1) : path;
 
-		var res = try hxd.res.Loader.currentInstance.load(relPath) catch( e : hxd.res.NotFound ) {
+		function loadUncompressed() {
 			var bytes = sys.io.File.getBytes(path);
-			hxd.res.Any.fromBytes(path, bytes);
+			return hxd.res.Any.fromBytes(path, bytes);
+		}
+
+		var res = try hxd.res.Loader.currentInstance.load(relPath) catch( e ) {
+			if (e is hxd.res.NotFound)
+				ide.quickError('Resource not found, original texture is loaded instead');
+			else if (onFail != null)
+				onFail();
+
+			loadUncompressed();
 		};
 
 		if (uncompressed) {
-			var bytes = sys.io.File.getBytes(path);
-			res = hxd.res.Any.fromBytes(path, bytes);
+			loadUncompressed();
 		}
 
 		if( onReady == null ) onReady = function(_) {};
@@ -380,7 +391,10 @@ class Scene extends hide.comp.Component implements h3d.IDrawable {
 			t.setName( ide.makeRelative(path));
 			texCache.set(path, t);
 		} catch( error : Dynamic ) {
-			throw "Could not load texure " + texturePath + ":\n" + Std.string(error);
+			if (onFail != null)
+				onFail();
+			else
+				throw "Could not load texure " + texturePath + ":\n" + Std.string(error);
 		};
 		return t;
 	}

+ 9 - 3
hide/view/Image.hx

@@ -383,12 +383,13 @@ class Image extends FileView {
 		// have been changed
 		@:privateAccess fs.fileCache.remove(state.path);
 
+		var onFail = () -> Ide.inst.quickError('Can\'t load texture with this compression parameters, original texture is loaded instead!');
 		scene.onReady = function() {
 			scene.loadTexture(state.path, state.path, function(compressedTexture) {
 				scene.loadTexture(state.path, state.path, function(uncompressedTexture) {
 					onTexturesLoaded(compressedTexture, uncompressedTexture);
-				}, false, true);
-			}, false);
+				}, onFail, false, true);
+			}, onFail, false);
 		};
 	}
 
@@ -713,7 +714,12 @@ class Image extends FileView {
 			else
 				comp.params = { alpha:Std.parseInt(alpha.val()), format:format.val().toString(), mips:mips.is(':checked'), size:Std.parseInt(size.val()) };
 
-			comp.convert();
+			try {
+				comp.convert();
+			}
+			catch(e) {
+				Ide.inst.quickError('Can\'t load texture with this compression parameters, original texture is loaded instead!');
+			}
 		}
 		else {
 			tmpPath = state.path;