浏览代码

review choose* not to return null unless explicitly asked

Nicolas Cannasse 4 年之前
父节点
当前提交
75f994d044

+ 9 - 6
hide/Ide.hx

@@ -825,11 +825,11 @@ class Ide {
 	}
 
 	public static var IMG_EXTS = ["jpg", "jpeg", "gif", "png", "raw", "dds", "hdr", "tga"];
-	public function chooseImage( onSelect ) {
-		chooseFile(IMG_EXTS, onSelect);
+	public function chooseImage( onSelect, allowNull=false ) {
+		chooseFile(IMG_EXTS, onSelect, allowNull);
 	}
 
-	public function chooseFiles( exts : Array<String>, onSelect : Array<String> -> Void ) {
+	public function chooseFiles( exts : Array<String>, onSelect : Array<String> -> Void, allowNull=false ) {
 		var e = new Element('<input type="file" style="visibility:hidden" value="" accept="${[for( e in exts ) "."+e].join(",")}" multiple="multiple"/>');
 		e.change(function(_) {
 			var files = [for( f in (""+e.val()).split(";") ) f];
@@ -840,16 +840,17 @@ class Ide {
 		}).appendTo(window.window.document.body).click();
 	}
 
-	public function chooseFile( exts : Array<String>, onSelect : Null<String> -> Void ) {
+	public function chooseFile( exts : Array<String>, onSelect : Null<String> -> Void, allowNull=false ) {
 		var e = new Element('<input type="file" style="visibility:hidden" value="" accept="${[for( e in exts ) "."+e].join(",")}"/>');
 		e.change(function(_) {
 			var file = e.val();
+			if( file == "" && !allowNull ) return;
 			e.remove();
 			onSelect(file == "" ? null : makeRelative(file));
 		}).appendTo(window.window.document.body).click();
 	}
 
-	public function chooseFileSave( defaultPath : String, onSelect : String -> Void ) {
+	public function chooseFileSave( defaultPath : String, onSelect : String -> Void, allowNull=false ) {
 		var path = getPath(defaultPath).split("/");
 		var file = path.pop();
 		var c = isWindows ? "\\" : "/";
@@ -857,15 +858,17 @@ class Ide {
 		var e = new Element('<input type="file" style="visibility:hidden" value="" nwworkingdir="$path" nwsaveas="$file"/>');
 		e.change(function(_) {
 			var file = e.val();
+			if( file == "" && !allowNull ) return;
 			e.remove();
 			onSelect(file == "" ? null : makeRelative(file));
 		}).appendTo(window.window.document.body).click();
 	}
 
-	public function chooseDirectory( onSelect : String -> Void, ?isAbsolute = false ) {
+	public function chooseDirectory( onSelect : String -> Void, ?isAbsolute = false, allowNull=false ) {
 		var e = new Element('<input type="file" style="visibility:hidden" value="" nwdirectory/>');
 		e.change(function(ev) {
 			var dir = ev.getThis().val();
+			if( dir == "" && !allowNull ) return;
 			onSelect(dir == "" ? null : (isAbsolute ? dir : makeRelative(dir)));
 			e.remove();
 		}).appendTo(window.window.document.body).click();

+ 0 - 1
hide/comp/SceneEditor.hx

@@ -2316,7 +2316,6 @@ class SceneEditor {
 
 				if( pmodel.inf.fileSource != null )
 					ide.chooseFile(pmodel.inf.fileSource, function(path) {
-						if( path == null ) return;
 						var p = make(path);
 						addObject([p]);
 					});

+ 3 - 5
hide/comp/cdb/Cell.hx

@@ -661,10 +661,8 @@ class Cell extends Component {
 			modal.click(function(_) color.close());
 		case TFile:
 			ide.chooseFile(["*"], function(file) {
-				if( file != null ) {
-					setValue(file);
-					refresh();
-				}
+				setValue(file);
+				refresh();
 			});
 		case TFlags(values):
 			var div = new Element("<div>").addClass("flagValues");
@@ -730,7 +728,7 @@ class Cell extends Component {
 					setVal();
 					closeEdit();
 					edit();
-				});
+				},true);
 				return;
 			}
 

+ 0 - 2
hide/prefab/terrain/TerrainEditor.hx

@@ -1179,8 +1179,6 @@ class TerrainEditor {
 	function onSurfaceAdd( props : Element, ctx : EditContext, path : String ) {
 		editContext.scene.setCurrent();
 		terrainPrefab.modified = true;
-		if( path == null )
-			return;
 		var split : Array<String> = [];
 		var curTypeIndex = 0;
 		while( split.length <= 1 && curTypeIndex < textureType.length) {

+ 1 - 1
hide/view/FileTree.hx

@@ -32,7 +32,7 @@ class FileTree extends FileView {
 				state.path = dir.split("\\").join("/")+"/";
 				saveState();
 				rebuild();
-			});
+			},true);
 		}
 
 		keys.register("search", function() tree.openFilter());

+ 0 - 1
hide/view/FileView.hx

@@ -120,7 +120,6 @@ class FileView extends hide.ui.View<{ path : String }> {
 
 	public function saveAs() {
 		ide.chooseFileSave(state.path, function(target) {
-			if( target == null ) return;
 			state.path = target;
 			save();
 			skipNextChange = false;

+ 0 - 1
hide/view/Model.hx

@@ -162,7 +162,6 @@ class Model extends FileView {
 		});
 		element.find("input[value=Import]").click(function(_) {
 			ide.chooseFile(["prefab"], function(f) {
-				if( f == null ) return;
 				if( ide.loadPrefab(f, hrt.prefab.RenderProps) == null ) {
 					ide.error("This prefab does not have renderer properties");
 					return;

+ 0 - 1
hide/view/shadereditor/ShaderEditor.hx

@@ -201,7 +201,6 @@ class ShaderEditor extends hide.view.Graph {
 
 		element.find("#changeModel").on("click", function() {
 			ide.chooseFile(["fbx"], function(path) {
-				if( path == null ) return; // cancel
 				sceneEditor.scene.setCurrent();
 				sceneEditor.scene.s3d.removeChild(obj);
 				obj = sceneEditor.scene.loadModel(path, true);

+ 1 - 1
hrt/prefab/l2d/NoiseGenerator.hx

@@ -197,7 +197,7 @@ class NoiseGenerator extends Prefab {
 		});
 		var bmp = cast(ctx.getContext(this).local2d, h2d.Bitmap);
 		e.find("[name=dl]").click(function(_) {
-			ctx.ide.chooseFileSave("noise.png", function(f) if( f != null ) {
+			ctx.ide.chooseFileSave("noise.png", function(f) {
 				try {
 					var data = cast(ctx.getContext(this).local2d, h2d.Bitmap).tile.getTexture().capturePixels().toPNG();
 					sys.io.File.saveBytes(ctx.ide.getPath(f), data);

+ 0 - 1
hrt/prefab/rfx/ColorGrading.hx

@@ -78,7 +78,6 @@ class ColorGrading extends RendererFX {
 		var but = e.find(".createDefault");
 		but.click(function(_) {
 			function saveTexture( name : String ) {
-				if( name == null ) return;
 				var step = hxd.Math.ceil(255/(size - 1));
 				var p = hxd.Pixels.alloc(size * size, size, RGBA);
 				for( r in 0 ... size ) {

+ 1 - 1
hrt/prefab/vlm/LightProbe.hx

@@ -650,7 +650,7 @@ class LightProbe extends Object3D {
 
 				function loadData( name : String ) {
 
-					if( name == null || name == "null" )
+					if( name == "null" )
 						return;
 
 					var b = hxd.res.Loader.currentInstance.load(name).entry.getBytes();