Align object can now select anchor

added a children selector list field
This commit is contained in:
Lars Brubaker 2018-08-13 13:48:10 -07:00
parent 85ca363fe9
commit 36efa6d8e2
7 changed files with 295 additions and 59 deletions

View file

@ -0,0 +1,38 @@
/*
Copyright (c) 2018, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
namespace MatterHackers.MatterControl.DesignTools
{
[AttributeUsage(AttributeTargets.Property)]
public class ShowAsListAttribute : Attribute
{
}
}

View file

@ -149,6 +149,10 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
Children.Add(item.Clone());
}
[ShowAsList]
[DisplayName("Anchor")]
public ChildrenSelector AnchorObjectSelector { get; set; } = new ChildrenSelector();
public bool Advanced { get; set; } = false;
[DisplayName("X")]
@ -203,6 +207,39 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
}
}
[JsonIgnore]
private IObject3D AnchorObject
{
get
{
if (AnchorObjectSelector.Count == 1)
{
return this.Children.Where(c => c.ID == AnchorObjectSelector[0]).FirstOrDefault();
}
return null;
}
}
[JsonIgnore]
private int AnchorObjectIndex
{
get
{
int index = 0;
foreach(var child in this.Children)
{
if(child.ID == AnchorObjectSelector[0])
{
return index;
}
index++;
}
return -1;
}
}
public static Vector3 GetPositionToAlignTo(IObject3D objectToAlignTo, FaceAlign boundingFacesToAlignTo, Vector3 extraOffset)
{
Vector3 positionToAlignTo = new Vector3();
@ -258,6 +295,24 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
{
this.DebugDepth("Rebuild");
var childrenIds = Children.Select(c => c.ID).ToArray();
if(childrenIds.Length == 0)
{
AnchorObjectSelector.Clear();
}
else if (AnchorObjectSelector.Count != 1
|| !AnchorObjectSelector.Where(i => childrenIds.Contains(i)).Any())
{
AnchorObjectSelector.Clear();
AnchorObjectSelector.Add(childrenIds[0]);
}
// if the count of our children changed clear our cache of the bounds
if (Children.Count != OriginalChildrenBounds.Count)
{
OriginalChildrenBounds.Clear();
}
using (RebuildLock())
{
var aabb = this.GetAxisAlignedBoundingBox();
@ -274,82 +329,98 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
});
}
var currentChildrenBounds = CurrentChildrenBounds;
this.Children.Modify(list =>
{
if (list.Count == 0)
{
return;
}
var firstBounds = currentChildrenBounds[0];
int anchorIndex = AnchorObjectIndex;
var anchorBounds = CurrentChildrenBounds[anchorIndex];
int i = 0;
// first align the anchor object
foreach (var child in list)
{
if (i > 0)
if (XAlign == Align.None
|| i == anchorIndex)
{
if (XAlign == Align.None)
if (i < OriginalChildrenBounds.Count)
{
if (i < OriginalChildrenBounds.Count)
{
// make sure it is where it started
AlignAxis(0, Align.Min, OriginalChildrenBounds[i].minXYZ.X, 0, child);
}
// make sure it is where it started
AlignAxis(0, Align.Min, OriginalChildrenBounds[i].minXYZ.X, 0, child);
}
}
if (YAlign == Align.None
|| i == anchorIndex)
{
if (i < OriginalChildrenBounds.Count)
{
AlignAxis(1, Align.Min, OriginalChildrenBounds[i].minXYZ.Y, 0, child);
}
}
if (ZAlign == Align.None
|| i == anchorIndex)
{
if (i < OriginalChildrenBounds.Count)
{
AlignAxis(2, Align.Min, OriginalChildrenBounds[i].minXYZ.Z, 0, child);
}
}
i++;
}
// the align all the objects to it
i = 0;
foreach (var child in list)
{
if (XAlign != Align.None
&& i != anchorIndex)
{
if (XAlign == Align.Origin)
{
// find the origin in world space of the child
var firstOrigin = Vector3.Transform(Vector3.Zero, AnchorObject.WorldMatrix());
var childOrigin = Vector3.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(-(childOrigin - firstOrigin).X + (Advanced ? XOffset : 0), 0, 0));
}
else
{
if (XAlign == Align.Origin)
{
// find the origin in world space of the child
var firstOrigin = Vector3.Transform(Vector3.Zero, this.Children.First().WorldMatrix());
var childOrigin = Vector3.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(-(childOrigin - firstOrigin).X + (Advanced ? XOffset : 0), 0, 0));
}
else
{
AlignAxis(0, XAlign, GetAlignToOffset(currentChildrenBounds, 0, (!Advanced || XAlignTo == Align.None) ? XAlign : XAlignTo), XOffset, child);
}
AlignAxis(0, XAlign, GetAlignToOffset(CurrentChildrenBounds, 0, (!Advanced || XAlignTo == Align.None) ? XAlign : XAlignTo), XOffset, child);
}
if (YAlign == Align.None)
}
if (YAlign != Align.None
&& i != anchorIndex)
{
if (YAlign == Align.Origin)
{
if (i < OriginalChildrenBounds.Count)
{
AlignAxis(1, Align.Min, OriginalChildrenBounds[i].minXYZ.Y, 0, child);
}
// find the origin in world space of the child
var firstOrigin = Vector3.Transform(Vector3.Zero, AnchorObject.WorldMatrix());
var childOrigin = Vector3.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(0, -(childOrigin - firstOrigin).Y + (Advanced ? YOffset : 0), 0));
}
else
{
if (YAlign == Align.Origin)
{
// find the origin in world space of the child
var firstOrigin = Vector3.Transform(Vector3.Zero, this.Children.First().WorldMatrix());
var childOrigin = Vector3.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(0, -(childOrigin - firstOrigin).Y + (Advanced ? YOffset : 0), 0));
}
else
{
AlignAxis(1, YAlign, GetAlignToOffset(currentChildrenBounds, 1, (!Advanced || YAlignTo == Align.None) ? YAlign : YAlignTo), YOffset, child);
}
AlignAxis(1, YAlign, GetAlignToOffset(CurrentChildrenBounds, 1, (!Advanced || YAlignTo == Align.None) ? YAlign : YAlignTo), YOffset, child);
}
if (ZAlign == Align.None)
}
if (ZAlign != Align.None
&& i != anchorIndex)
{
if (ZAlign == Align.Origin)
{
if (i < OriginalChildrenBounds.Count)
{
AlignAxis(2, Align.Min, OriginalChildrenBounds[i].minXYZ.Z, 0, child);
}
// find the origin in world space of the child
var firstOrigin = Vector3.Transform(Vector3.Zero, AnchorObject.WorldMatrix());
var childOrigin = Vector3.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(0, 0, -(childOrigin - firstOrigin).Z + (Advanced ? ZOffset : 0)));
}
else
{
if (ZAlign == Align.Origin)
{
// find the origin in world space of the child
var firstOrigin = Vector3.Transform(Vector3.Zero, this.Children.First().WorldMatrix());
var childOrigin = Vector3.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(0, 0, -(childOrigin - firstOrigin).Z + (Advanced ? ZOffset : 0)));
}
else
{
AlignAxis(2, ZAlign, GetAlignToOffset(currentChildrenBounds, 2, (!Advanced || ZAlignTo == Align.None) ? ZAlign : ZAlignTo), ZOffset, child);
}
AlignAxis(2, ZAlign, GetAlignToOffset(CurrentChildrenBounds, 2, (!Advanced || ZAlignTo == Align.None) ? ZAlign : ZAlignTo), ZOffset, child);
}
}
i++;
@ -440,13 +511,13 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
switch (alignTo)
{
case Align.Min:
return currentChildrenBounds[0].minXYZ[axis];
return currentChildrenBounds[AnchorObjectIndex].minXYZ[axis];
case Align.Center:
return currentChildrenBounds[0].Center[axis];
return currentChildrenBounds[AnchorObjectIndex].Center[axis];
case Align.Max:
return currentChildrenBounds[0].maxXYZ[axis];
return currentChildrenBounds[AnchorObjectIndex].maxXYZ[axis];
default:
throw new NotImplementedException();

View file

@ -401,8 +401,26 @@ namespace MatterHackers.MatterControl.DesignTools
}
else if (propertyValue is ChildrenSelector childSelector)
{
rowContainer = CreateSettingsColumn(property);
rowContainer.AddChild(CreateSelector(childSelector, property.Item, theme));
var showAsList = property.PropertyInfo.GetCustomAttributes(true).OfType<ShowAsListAttribute>().FirstOrDefault() != null;
if (showAsList)
{
UIField field = new ChildrenSelectorListField(property, theme);
field.Initialize(0);
field.ValueChanged += (s, e) =>
{
property.SetValue(new ChildrenSelector() { field.Value });
object3D?.Invalidate(new InvalidateArgs(context.item, InvalidateType.Properties, undoBuffer));
propertyGridModifier?.UpdateControls(new PublicPropertyChange(context, property.PropertyInfo.Name));
};
rowContainer = CreateSettingsRow(property, field);
}
else // show the subtarct editor for boolean subtract and subtract and replace
{
rowContainer = CreateSettingsColumn(property);
rowContainer.AddChild(CreateSelector(childSelector, property.Item, theme));
}
}
else if (propertyValue is ImageBuffer imageBuffer)
{