Adding IntOrExpression

This commit is contained in:
Lars Brubaker 2021-06-01 17:48:43 -07:00
parent eaac193ae8
commit 28bd0a1071
7 changed files with 132 additions and 10 deletions

View file

@ -188,7 +188,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
public Align XAlignTo { get; set; } = Align.None;
[DisplayName("Offset")]
public double XOffset { get; set; } = 0;
public DoubleOrExpression XOffset { get; set; } = 0;
[SectionStart("Y Axis"), DisplayName("Align")]
[EnumDisplay(IconPaths = new string[] { "424.png", "align_bottom.png", "align_center_y.png", "align_Top.png", "align_origin.png" }, InvertIcons = true)]
@ -199,7 +199,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
public Align YAlignTo { get; set; } = Align.None;
[DisplayName("Offset")]
public double YOffset { get; set; } = 0;
public DoubleOrExpression YOffset { get; set; } = 0;
[SectionStart("Z Axis"), DisplayName("Align")]
[EnumDisplay(IconPaths = new string[] { "424.png", "align_bottom.png", "align_center_y.png", "align_Top.png", "align_origin.png" }, InvertIcons = true)]
@ -210,7 +210,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
public Align ZAlignTo { get; set; } = Align.None;
[DisplayName("Offset")]
public double ZOffset { get; set; } = 0;
public DoubleOrExpression ZOffset { get; set; } = 0;
public override bool CanFlatten => true;
@ -373,9 +373,9 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
// align all the objects to the anchor
foreach (var child in children)
{
AlignAxis(0, XAlign, GetAlignToOffset(anchorBounds, 0, (!Advanced || XAlignTo == Align.None) ? XAlign : XAlignTo), XOffset, child);
AlignAxis(1, YAlign, GetAlignToOffset(anchorBounds, 1, (!Advanced || YAlignTo == Align.None) ? YAlign : YAlignTo), YOffset, child);
AlignAxis(2, ZAlign, GetAlignToOffset(anchorBounds, 2, (!Advanced || ZAlignTo == Align.None) ? ZAlign : ZAlignTo), ZOffset, child);
AlignAxis(0, XAlign, GetAlignToOffset(anchorBounds, 0, (!Advanced || XAlignTo == Align.None) ? XAlign : XAlignTo), XOffset.Value(this), child);
AlignAxis(1, YAlign, GetAlignToOffset(anchorBounds, 1, (!Advanced || YAlignTo == Align.None) ? YAlign : YAlignTo), YOffset.Value(this), child);
AlignAxis(2, ZAlign, GetAlignToOffset(anchorBounds, 2, (!Advanced || ZAlignTo == Align.None) ? ZAlign : ZAlignTo), ZOffset.Value(this), child);
}
});
}

View file

@ -61,6 +61,7 @@ namespace MatterHackers.MatterControl.DesignTools
{
typeof(double), typeof(int), typeof(char), typeof(string), typeof(bool),
typeof(DoubleOrExpression),
typeof(IntOrExpression),
typeof(Color),
typeof(Vector2), typeof(Vector3), typeof(Vector4),
typeof(DirectionVector), typeof(DirectionAxis),
@ -711,6 +712,43 @@ namespace MatterHackers.MatterControl.DesignTools
object3D.Invalidated += RefreshField;
field.Content.Closed += (s, e) => object3D.Invalidated -= RefreshField;
}
else if (propertyValue is IntOrExpression intExpresion)
{
// create a string editor
var field = new TextField(theme);
field.Initialize(0);
field.SetValue(intExpresion.Expression, false);
field.ClearUndoHistory();
field.Content.HAnchor = HAnchor.Stretch;
RegisterValueChanged(field,
(valueString) => new IntOrExpression(valueString),
(value) =>
{
return ((IntOrExpression)value).Expression;
});
rowContainer = CreateSettingsRow(property, field, theme);
var label = rowContainer.Children.First();
var spacer = rowContainer.Children.OfType<HorizontalSpacer>().FirstOrDefault();
spacer.HAnchor = HAnchor.Absolute;
spacer.Width = Math.Max(0, 100 - label.Width);
void RefreshField(object s, InvalidateArgs e)
{
if (e.InvalidateType.HasFlag(InvalidateType.DisplayValues))
{
IntOrExpression newValue = (IntOrExpression)property.Value;
if (newValue.Expression != field.Value)
{
field.TextValue = newValue.Value(object3D).ToString();
}
}
}
object3D.Invalidated += RefreshField;
field.Content.Closed += (s, e) => object3D.Invalidated -= RefreshField;
}
else if (propertyValue is string stringValue)
{
if (readOnly)

View file

@ -0,0 +1,70 @@
/*
Copyright (c) 2019, 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.ComponentModel;
using MatterHackers.DataConverters3D;
namespace MatterHackers.MatterControl.DesignTools
{
[TypeConverter(typeof(IntOrExpression))]
public class IntOrExpression
{
public string Expression { get; set; }
public int Value(IObject3D owner)
{
if (int.TryParse(Expression, out int result))
{
return result;
}
return SheetObject3D.EvaluateExpression<int>(owner, Expression);
}
public IntOrExpression(int value)
{
Expression = value.ToString();
}
public IntOrExpression(string expression)
{
Expression = expression;
}
public static implicit operator IntOrExpression(int value)
{
return new IntOrExpression(value);
}
public static implicit operator IntOrExpression(string expression)
{
return new IntOrExpression(expression);
}
}
}

View file

@ -131,18 +131,32 @@ namespace MatterHackers.MatterControl.DesignTools
if (typeof(T) == typeof(double))
{
if (double.TryParse(value, out double doubleValue))
if (double.TryParse(value, out double doubleValue)
&& !double.IsNaN(doubleValue)
&& !double.IsInfinity(doubleValue))
{
return (T)(object)doubleValue;
}
// else return an error
return (T)(object)5.5;
return (T)(object).1;
}
if (typeof(T) == typeof(int))
{
if (double.TryParse(value, out double doubleValue)
&& !double.IsNaN(doubleValue)
&& !double.IsInfinity(doubleValue))
{
return (T)(object)(int)Math.Round(doubleValue);
}
// else return an error
return (T)(object)1;
}
}
}
}
throw new NotImplementedException();
return (T)(object)default(T);
}
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)