Example #1
0
		public override object ReadJson (Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			object instance;
			JContainer container;
			if (reader.TokenType == JsonToken.Null) {
				return null;
			}
			if (reader.TokenType == JsonToken.StartArray) {
				container = JArray.Load (reader);
				if (objectType == typeof(DynamicRow)) {
					var dynamicRow = new DynamicRow ();
					instance = dynamicRow;
					serializer.Populate (container.CreateReader (), dynamicRow.Items);
				}
				else if (objectType == typeof(DynamicItem)) {
					var dynamicTable = new DynamicTable ();
					instance = dynamicTable;
					serializer.Populate (container.CreateReader (), dynamicTable.Rows);
				}
				else throw new EtoException("Invalid object graph");
			} else {
				container = JObject.Load (reader);
				if (container["$type"] == null) {
					if (container["Rows"] != null)
						instance = new DynamicTable ();
					else if (container["Control"] != null)
						instance = new DynamicControl ();
					else
						throw new EtoException("Could not infer the type of object to create");

					serializer.Populate(container.CreateReader(), instance);
				}
				else {
					var type = Type.GetType ((string)container ["$type"]);
					if (!typeof(DynamicItem).IsAssignableFrom (type)) {
						var dynamicControl = new DynamicControl ();
						dynamicControl.Control = serializer.Deserialize (container.CreateReader ()) as Control;
						instance = dynamicControl;
					} else {
						instance = serializer.Deserialize (container.CreateReader ());
					}
				}
			}
			if (objectType == typeof(DynamicRow) && instance.GetType () != typeof(DynamicRow)) {
				var row = new DynamicRow();
				row.Items.Add (instance as DynamicItem);
				return row;
			}

			return instance;
		}
Example #2
0
		public DynamicControl Add(Control control, bool? xscale = null, bool? yscale = null)
		{
			if (xscale == null && currentItem.CurrentRow != null && control == null)
				xscale = true;
			yscale = yscale ?? this.yscale;
			if (yscale == null && currentItem.CurrentRow == null && control == null)
				yscale = true;
			var dynamicControl = new DynamicControl { Control = control, XScale = xscale, YScale = yscale };
			currentItem.Add(dynamicControl);
			return dynamicControl;
		}
Example #3
0
		public override Control Generate (DynamicLayout layout)
		{
			if (rows.Count == 0)
				return null;
			int cols = rows.Where (r => r != null).Max (r => r.Items.Count);

			this.Table = new TableLayout (cols, rows.Count);
			var tableLayout = this.Table;
			var padding = this.Padding ?? layout.DefaultPadding;
			if (padding != null)
				tableLayout.Padding = padding.Value;

			var spacing = this.Spacing ?? layout.DefaultSpacing;
			if (spacing != null)
				tableLayout.Spacing = spacing.Value;

			var scalingRow = new DynamicRow ();
			scalingRow.Items.Add (new DynamicControl{ YScale = true });
			for (int cy = 0; cy < rows.Count; cy++) {
				var row = rows[cy];
				if (row == null) row = scalingRow;
				for (int cx = 0; cx < row.Items.Count; cx++) {
					var item = row.Items[cx];
					if (item == null) item = new DynamicControl { XScale = true };
					item.Generate (layout, tableLayout, cx, cy);
				}
			}
			return Table;
		}