Pārlūkot izejas kodu

review range property implementation

Nicolas Cannasse 8 gadi atpakaļ
vecāks
revīzija
535264a553
3 mainītis faili ar 106 papildinājumiem un 72 dzēšanām
  1. 104 71
      hide/comp/PropsEditor.hx
  2. 1 1
      hide/comp/TextureSelect.hx
  3. 1 0
      hide/ui/Keys.hx

+ 104 - 71
hide/comp/PropsEditor.hx

@@ -37,11 +37,7 @@ class PropsEditor extends Component {
 		e = e.wrap("<div></div>").parent(); // necessary to have find working on top level element
 		e = e.wrap("<div></div>").parent(); // necessary to have find working on top level element
 
 
 		e.find("input[type=checkbox]").wrap("<div class='checkbox-wrapper'></div>");
 		e.find("input[type=checkbox]").wrap("<div class='checkbox-wrapper'></div>");
-
-		for( s in e.find("input[type=range]").not("[step]").elements() ) {
-			var range = Std.parseFloat(s.attr("max")) - Std.parseFloat(s.attr("min"));
-			s.attr("step", ""+hxd.Math.fmt(range/200));
-		}
+		e.find("input[type=range]").not("[step]").attr("step", "any");
 
 
 		// -- reload states ---
 		// -- reload states ---
 		for( h in e.find(".section > h1").elements() )
 		for( h in e.find(".section > h1").elements() )
@@ -148,32 +144,49 @@ class PropsField extends Component {
 				onChange(false);
 				onChange(false);
 			}
 			}
 			return;
 			return;
-		default:
-		}
+		case "range":
 
 
-		if( f.is("select") ) {
-			enumValue = Type.getEnum(current);
-			if( enumValue != null && f.find("option").length == 0 ) {
-				for( c in enumValue.getConstructors() )
-					new Element('<option value="$c">$c</option>').appendTo(f);
-			}
-		}
-
-		var inputView = null;
-		if( f.is("[type=range]") ) {
 			f.wrap('<div class="range-group"/>');
 			f.wrap('<div class="range-group"/>');
 			var p = f.parent();
 			var p = f.parent();
+			var inputView = new Element('<input type="text">').appendTo(p);
+			var originMin = Std.parseFloat(f.attr("min"));
+			var originMax = Std.parseFloat(f.attr("max"));
+			var curMin = originMin, curMax = originMax;
+
+			function setVal( v : Float ) {
+				var tempChange = tempChange;
+				this.setVal(v);
+
+				if( tempChange )
+					return;
+
+				if( v < curMin ) {
+					curMin = Math.floor(v);
+					f.attr("min", curMin);
+				}
+				if( v > curMax ) {
+					curMax = Math.ceil(v);
+					f.attr("max", curMax);
+				}
+				if( v >= originMin && v <= originMax ) {
+					f.attr("min", originMin);
+					f.attr("max", originMax);
+					curMin = originMin;
+					curMax = originMax;
+				}
+			}
+
 			var original = current;
 			var original = current;
 			p.parent().prev("dt").contextmenu(function(e) {
 			p.parent().prev("dt").contextmenu(function(e) {
 				e.preventDefault();
 				e.preventDefault();
 				new ContextMenu([
 				new ContextMenu([
-					{ label : "Reset", click : function() { f.val(original); f.change(); } },
+					{ label : "Reset", click : function() { inputView.val(""+original); inputView.change(); } },
 					{ label : "Cancel", click : function() {} },
 					{ label : "Cancel", click : function() {} },
 				]);
 				]);
 				return false;
 				return false;
 			});
 			});
-			inputView = new Element('<input type="text">').appendTo(p);
-			inputView.val(current);
+
+			f.on("input", function(_) { tempChange = true; f.change(); });
 			inputView.keyup(function(e) {
 			inputView.keyup(function(e) {
 				if( e.keyCode == 13 || e.keyCode == 27 ) {
 				if( e.keyCode == 13 || e.keyCode == 27 ) {
 					inputView.blur();
 					inputView.blur();
@@ -181,67 +194,87 @@ class PropsField extends Component {
 					return;
 					return;
 				}
 				}
 				var v = Std.parseFloat(inputView.val());
 				var v = Std.parseFloat(inputView.val());
-				if( !Math.isNaN(v) ) {
-					f.val(v);
-					f.change();
-				}
+				if( Math.isNaN(v) ) return;
+				setVal(v);
+				f.val(v);
 			});
 			});
-			f.on("input", function(_) { tempChange = true; f.change(); });
-		}
 
 
-		f.val(current);
-		f.keyup(function(e) {
-			if( e.keyCode == 13 ) {
-				f.blur();
-				return;
-			}
-			if( e.keyCode == 27 ) {
-				f.blur();
-				return;
-			}
-			tempChange = true;
-			f.change();
-		});
-		f.change(function(e) {
+			f.val(current);
+			inputView.val(current);
 
 
-			var newVal : Dynamic = f.val();
+			f.change(function(e) {
 
 
-			if( f.is("[type=range]") || f.is("[type=number]") )
-				newVal = Std.parseFloat(newVal);
+				var v = Math.round(Std.parseFloat(f.val()) * 100) / 100;
+				setVal(v);
+				inputView.val(v);
 
 
-			if( enumValue != null )
-				newVal = Type.createEnum(enumValue, newVal);
+			});
 
 
-			if( f.is("select") ) f.blur();
+		default:
+			if( f.is("select") ) {
+				enumValue = Type.getEnum(current);
+				if( enumValue != null && f.find("option").length == 0 ) {
+					for( c in enumValue.getConstructors() )
+						new Element('<option value="$c">$c</option>').appendTo(f);
+				}
+			}
 
 
-			if( current == newVal ) {
-				if( tempChange || beforeTempChange == null )
+			f.val(current);
+			f.keyup(function(e) {
+				if( e.keyCode == 13 ) {
+					f.blur();
 					return;
 					return;
-				current = beforeTempChange.value;
-				beforeTempChange = null;
-			}
+				}
+				if( e.keyCode == 27 ) {
+					f.blur();
+					return;
+				}
+				tempChange = true;
+				f.change();
+			});
+			f.change(function(e) {
 
 
-			if( inputView != null )
-				inputView.val(newVal);
+				var newVal : Dynamic = f.val();
 
 
-			if( tempChange ) {
-				tempChange = false;
-				if( beforeTempChange == null ) beforeTempChange = { value : current };
-			}
-			else {
-				props.undo.change(Field(context, fname, current), function() {
-					var f = resolveField();
-					f.current = Reflect.field(f.context, fname);
-					f.root.val(f.current);
-					if( inputView != null )
-						f.root.parent().find("input[type=text]").val(f.current);
-					f.onChange(true);
-				});
-			}
-			current = newVal;
-			Reflect.setProperty(context, fname, newVal);
-			onChange(false);
-		});
+				if( f.is("[type=number]") )
+					newVal = Std.parseFloat(newVal);
+
+				if( enumValue != null )
+					newVal = Type.createEnum(enumValue, newVal);
+
+				if( f.is("select") )
+					f.blur();
+
+				setVal(newVal);
+			});
+		}
+
+	}
+
+	function setVal(v) {
+		if( current == v ) {
+			// delay history save until last change
+			if( tempChange || beforeTempChange == null )
+				return;
+			current = beforeTempChange.value;
+			beforeTempChange = null;
+		}
+		if( tempChange ) {
+			tempChange = false;
+			if( beforeTempChange == null ) beforeTempChange = { value : current };
+		} else {
+			props.undo.change(Field(context, fname, current), function() {
+				var f = resolveField();
+				var v = Reflect.field(f.context, fname);
+				f.current = v;
+				f.root.val(v);
+				f.root.parent().find("input[type=text]").val(v);
+				f.onChange(true);
+			});
+		}
+		current = v;
+		Reflect.setProperty(context, fname, v);
+		onChange(false);
 	}
 	}
 
 
 	public dynamic function onChange( wasUndo : Bool ) {
 	public dynamic function onChange( wasUndo : Bool ) {

+ 1 - 1
hide/comp/TextureSelect.hx

@@ -5,7 +5,7 @@ class TextureSelect extends FileSelect {
 	public var value(default, set) : h3d.mat.Texture;
 	public var value(default, set) : h3d.mat.Texture;
 
 
 	public function new(root) {
 	public function new(root) {
-		super(root,["jpg", "jpeg", "gif"]);
+		super(root,["jpg", "jpeg", "gif", "png"]);
 	}
 	}
 
 
 	override function set_path(p:String) {
 	override function set_path(p:String) {

+ 1 - 0
hide/ui/Keys.hx

@@ -45,6 +45,7 @@ class Keys {
 		if( callb != null ) {
 		if( callb != null ) {
 			callb();
 			callb();
 			e.stopPropagation();
 			e.stopPropagation();
+			e.preventDefault();
 		}
 		}
 	}
 	}