Skip to content

Commit

Permalink
Propertly restoring set properties now
Browse files Browse the repository at this point in the history
  • Loading branch information
X39 committed Mar 13, 2020
1 parent 1389ffe commit e3be2ea
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 20 deletions.
59 changes: 49 additions & 10 deletions Arma.Studio.UiEditor/UI/CanvasManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ public int GridSize
get => this._GridSize;
set
{
if (value < 0)
{
value = 1;
}
this._GridSize = value;
this.RaisePropertyChanged();
}
Expand Down Expand Up @@ -285,12 +289,40 @@ public bool ShowGrid
private bool _ShowGrid;
#endregion

public void SetPosition(IControlElement node, Point p) => this.SetPosition(node, p.X, p.Y);
public void SetPosition(IControlElement node, double left, double top)
public Point SnapToGrid(Point p)
{
var p = this.GetPosition(node, left, top);
node.Left = p.X;
node.Top = p.Y;
var gridX = this.GridSize;
var gridY = this.GridSize;
if (gridX > 0)
{
// Check if mid is closer to centered-left or centered-right
var deltaX = p.X % gridX;
if (deltaX > gridX / 2)
{ // right
// Move left to grid-based left
p.X = ((int)p.X / gridX + 1) * gridX;
}
else
{ // left
// Move left to grid-based left
p.X = ((int)p.X / gridX) * gridX;
}
}
if (gridY > 0)
{
// Check if mid is closer to centered-top or centered-bot
var deltaY = p.Y % gridY;
if (deltaY > gridY / 2)
{ // bot
// Move top to grid-based top
p.Y = ((int)p.Y / gridY + 1) * gridY;
}
else
{ // top
p.Y = ((int)p.Y / gridY) * gridY;
}
}
return p;
}
public Point GetPosition(IControlElement node, double left, double top)
{
Expand Down Expand Up @@ -490,14 +522,21 @@ public void OnMouseUp(UIElement sender, MouseButtonEventArgs e)
else
{
var newPositions = this.PreMovePositions
.Select((it) => new Tuple<IControlElement, Point>(it.Item1, this.GetPosition(it.Item1, it.Item1.Left, it.Item1.Top)))
.ToDictionary((it) => it.Item1, (it) => it.Item2);
.Select((it) =>
{
var rect = new Rect(it.Item1.Left, it.Item1.Top, it.Item1.Width, it.Item1.Height);
return new Tuple<IControlElement, Point, Point>(it.Item1, this.SnapToGrid(new Point(rect.Left, rect.Top)), this.SnapToGrid(new Point(rect.Right, rect.Bottom)));
})
.ToDictionary((it) => it.Item1, (it) => new { TopLeft = it.Item2, BotomRight = it.Item3 });
var selectedNodes = this.SelectedNodes.ToArray();
foreach (var node in selectedNodes)
{
var pos = newPositions[node];
node.Left = pos.X;
node.Top = pos.Y;
var obj = newPositions[node];
var rect = new Rect(obj.TopLeft, obj.BotomRight);
node.Left = rect.Left;
node.Top = rect.Top;
node.Width = rect.Width;
node.Height = rect.Height;
}
e.Handled = true;
}
Expand Down
30 changes: 20 additions & 10 deletions Arma.Studio.UiEditor/UI/UiEditorDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Arma.Studio.UiEditor.UI
{
Expand Down Expand Up @@ -142,25 +139,29 @@ public string ClassName
private string _ClassName;
#endregion
#region SerializationProperties
[ArmaName("ArmaStudio_InterfaceSize", Display = false)]
private const string InterfaceSizeSerializedKey = "ArmaStudio_InterfaceSize";
[ArmaName(InterfaceSizeSerializedKey, Display = false)]
public string InterfaceSizeSerialized
{
get => this.InterfaceSize.Key;
set => this.InterfaceSize = InterfaceSize.InterfaceSizes.FirstOrDefault((it) => it.Key == value) ?? InterfaceSize.Small;
}
[ArmaName("ArmaStudio_GridSize", Display = false)]
private const string GridSizeSerializedKey = "ArmaStudio_GridSize";
[ArmaName(GridSizeSerializedKey, Display = false)]
public int GridSizeSerialized
{
get => this.CanvasManager.GridSize;
set => this.CanvasManager.GridSize = value;
}
[ArmaName("ArmaStudio_Width", Display = false)]
private const string WidthSerializedKey = "ArmaStudio_Width";
[ArmaName(WidthSerializedKey, Display = false)]
public double WidthSerialized
{
get => this.CanvasManager.Width;
set => this.CanvasManager.Width = value;
}
[ArmaName("ArmaStudio_Height", Display = false)]
private const string HeightSerializedKey = "ArmaStudio_Height";
[ArmaName(HeightSerializedKey, Display = false)]
public double HeightSerialized
{
get => this.CanvasManager.Height;
Expand Down Expand Up @@ -369,8 +370,8 @@ public void Load()
var dialogConfig = configBin.Values.FirstOrDefault((it) =>
{
if (it.ContainsKey(dlg_idd) ||
it.ContainsKey(dlg_movingEnable) ||
it.ContainsKey(dlg_enableSimulation) ||
it.ContainsKey(dlg_movingEnable) ||
it.ContainsKey(dlg_enableSimulation) ||
it.ContainsKey(dlg_controls))
{
return true;
Expand Down Expand Up @@ -410,13 +411,22 @@ public void Load()
}

this.ClassName = dialogConfig.Name;
try
{
if (dialogConfig.ContainsKey("idd")) { this.InterfaceSizeSerialized = (string)dialogConfig["idd"].Value; }
if (dialogConfig.ContainsKey(InterfaceSizeSerializedKey)) { this.InterfaceSizeSerialized = (string)dialogConfig[InterfaceSizeSerializedKey].Value; }
if (dialogConfig.ContainsKey(GridSizeSerializedKey)) { this.GridSizeSerialized = (int)dialogConfig[GridSizeSerializedKey].Value; }
if (dialogConfig.ContainsKey(WidthSerializedKey)) { this.WidthSerialized = (double)dialogConfig[WidthSerializedKey].Value; }
if (dialogConfig.ContainsKey(HeightSerializedKey)) { this.HeightSerialized = (double)dialogConfig[HeightSerializedKey].Value; }
}
catch { }

var backgroundControls = dialogConfig[dlg_controlsBackground];
var foregroundControls = dialogConfig[dlg_controls];
if (backgroundControls != null)
{
var controls = getControls(backgroundControls);
foreach(var node in controls)
foreach (var node in controls)
{
var control = LoadControl(vm, node);
if (control != null)
Expand Down

0 comments on commit e3be2ea

Please sign in to comment.