فهرست منبع

domkit v2 changes

Nicolas Cannasse 6 سال پیش
والد
کامیت
4ab76561a1
4فایلهای تغییر یافته به همراه31 افزوده شده و 74 حذف شده
  1. 12 1
      h2d/Object.hx
  2. 0 10
      h2d/domkit/BaseComponents.hx
  3. 1 50
      h2d/domkit/InitComponents.hx
  4. 18 13
      h2d/domkit/Style.hx

+ 12 - 1
h2d/Object.hx

@@ -7,7 +7,7 @@ import hxd.Math;
 	so the various transforms are inherited to its children.
 **/
 @:allow(h2d.Tools)
-class Object {
+class Object #if domkit implements domkit.Model<h2d.Object> #end {
 
 	static var nullDrawable : h2d.Drawable;
 
@@ -76,6 +76,11 @@ class Object {
 	**/
 	public var blendMode : BlendMode;
 
+	#if domkit
+	public var dom : domkit.Properties<h2d.Object>;
+	@:noCompletion public inline function getChildren() return children;
+	#end
+
 	var matA : Float;
 	var matB : Float;
 	var matC : Float;
@@ -350,6 +355,9 @@ class Object {
 				s.onHierarchyMoved(true);
 		}
 		onContentChanged();
+		#if domkit
+		if( s.dom != null ) s.dom.onParentChanged();
+		#end
 	}
 
 	inline function onContentChanged() {
@@ -398,6 +406,9 @@ class Object {
 			s.parent = null;
 			if( s.parentContainer != null ) s.setParentContainer(null);
 			s.posChanged = true;
+			#if domkit
+			if( s.dom != null ) s.dom.onParentChanged();
+			#end
 			onContentChanged();
 		}
 	}

+ 0 - 10
h2d/domkit/BaseComponents.hx

@@ -342,16 +342,6 @@ class ObjectComp implements h2d.domkit.Object implements domkit.Component.Compon
 		if( p != null ) p.minHeight = v;
 	}
 
-	@:keep static var init = {
-		domkit.Element.addElement = function(e, parent) {
-			(e.obj : h2d.Object).name = e.id;
-			(parent.obj : h2d.Object).addChild(e.obj);
-		}
-		domkit.Element.removeElement = function(e) (e.obj : h2d.Object).remove();
-		domkit.Element.getParent = function(e:h2d.Object) return e.parent;
-		true;
-	}
-
 }
 
 @:uiComp("drawable")

+ 1 - 50
h2d/domkit/InitComponents.hx

@@ -14,56 +14,7 @@ class InitComponents {
 		return null;
 	}
 
-	static function addOnRemove(fields : Array<haxe.macro.Expr.Field>) : Array<haxe.macro.Expr.Field> {
-		var removeExpr = macro {
-			var style = hxd.impl.Api.downcast(document.style, h2d.domkit.Style);
-			if( style != null ) @:privateAccess style.remove(this);
-			// make sure it's also removed from document
-			var elt = document.get(this);
-			if( elt != null && elt.parent != null ) {
-				elt.parent.children.remove(elt);
-				@:privateAccess elt.parent = null;
-			}
-		};
-
-		var found = false;
-		for( f in fields ) {
-			if( f.name == "onRemove" ) {
-				function repl(e:haxe.macro.Expr) {
-					switch( e.expr ) {
-					case ECall( { expr : EField( { expr : EConst(CIdent("super")) }, "onRemove") }, []):
-						found = true;
-						return macro { $e; $removeExpr; }
-					default:
-						return haxe.macro.ExprTools.map(e, repl);
-					}
-				}
-				switch( f.kind ) {
-				case FFun(f):
-					f.expr = repl(f.expr);
-				default:
-				}
-				if( !found ) haxe.macro.Context.error("Override of onRemove() with no super.onRemove() found", f.pos);
-			}
-		}
-		if(!found) {
-			fields = fields.concat((macro class {
-				override function onRemove() {
-					super.onRemove();
-					$removeExpr;
-				}
-			}).fields);
-		}
-		return fields;
-	}
-
 	public static function build() {
-		var fields = domkit.Macros.buildObject();
-		for( f in fields )
-			if( f.name == "document" ) {
-				fields = addOnRemove(fields);
-				break;
-			}
-		return fields;
+		return domkit.Macros.buildObject();
 	}
 }

+ 18 - 13
h2d/domkit/Style.hx

@@ -2,7 +2,7 @@ package h2d.domkit;
 
 class Style extends domkit.CssStyle {
 
-	var currentObjects : Array<h2d.domkit.Object> = [];
+	var currentObjects : Array<h2d.Object> = [];
 	var resources : Array<hxd.res.Resource> = [];
 	var errors : Array<String>;
 	var errorsText : h2d.Text;
@@ -20,20 +20,30 @@ class Style extends domkit.CssStyle {
 		resources.push(r);
 		add(new domkit.CssParser().parseSheet(r.entry.getText()));
 		for( o in currentObjects )
-			getDocument(o).setStyle(this);
+			o.dom.applyStyle(this);
 	}
 
-	public function applyTo( obj ) {
+	public function addObject( obj ) {
 		currentObjects.remove(obj);
 		currentObjects.push(obj);
-		getDocument(obj).setStyle(this);
+		obj.dom.applyStyle(this);
 	}
 
-	function remove(obj) {
+	public function removeObject(obj) {
 		currentObjects.remove(obj);
 	}
 
-	override function onInvalidProperty(e:domkit.Element<Dynamic>, s:domkit.CssStyle.RuleStyle, msg:String) {
+	/**
+		Returns number of dom elements that were updated
+	**/
+	public function sync() {
+		var T0 = domkit.CssStyle.TAG;
+		for( o in currentObjects )
+			o.dom.applyStyle(this, true);
+		return domkit.CssStyle.TAG - T0;
+	}
+
+	override function onInvalidProperty(e:domkit.Properties<Dynamic>, s:domkit.CssStyle.RuleStyle, msg:String) {
 		if( errors != null ) {
 			var path = s.p.name;
 			var ee = e;
@@ -41,7 +51,6 @@ class Style extends domkit.CssStyle {
 				path = (ee.id != null ? "#" + ee.id : ee.component.name) + "." + path;
 				ee = ee.parent;
 			}
-
 			if( msg == null ) msg = "Invalid property value '"+(domkit.CssParser.valueStr(s.value))+"'";
 			errors.push(msg+" for " + path);
 		}
@@ -67,7 +76,7 @@ class Style extends domkit.CssStyle {
 		 	}
 		}
 		for( o in currentObjects )
-			getDocument(o).setStyle(this);
+			o.dom.applyStyle(this);
 		if( errors.length == 0 ) {
 			if( errorsText != null ) {
 				errorsText.parent.remove();
@@ -76,7 +85,7 @@ class Style extends domkit.CssStyle {
 		} else {
 			if( errorsText == null ) {
 				if( currentObjects.length == 0 ) return;
-				var scene = getDocument(currentObjects[0]).root.obj.getScene();
+				var scene = currentObjects[0].getScene();
 				var fl = new h2d.Flow();
 				scene.addChildAt(fl,100);
 				fl.backgroundTile = h2d.Tile.fromColor(0x400000,0.9);
@@ -92,8 +101,4 @@ class Style extends domkit.CssStyle {
 		}
 	}
 
-	function getDocument( o : h2d.domkit.Object ) : domkit.Document<h2d.Object> {
-		return (o : Dynamic).document;
-	}
-
 }