Skip to content

Commit

Permalink
Changed to IComponentObject3D
Browse files Browse the repository at this point in the history
Fixed a crash in convex hull
Improving path editor widget
Added x only pinch to radial pinch object
  • Loading branch information
larsbrubaker committed Nov 13, 2023
1 parent 3a0dd55 commit c4b58c9
Show file tree
Hide file tree
Showing 13 changed files with 591 additions and 526 deletions.
12 changes: 6 additions & 6 deletions MatterControlLib/ApplicationView/SceneOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,19 @@ public static SceneOperation EditComponentOperation()
return new SceneOperation("EditComponent")
{
TitleGetter = () => "Edit Component".Localize(),
ResultType = typeof(ComponentObject3D),
ResultType = typeof(IComponentObject3D),
Action = (sceneContext) =>
{
var scene = sceneContext.Scene;
var sceneItem = scene.SelectedItem;
if (sceneItem is ComponentObject3D componentObject)
if (sceneItem is IComponentObject3D componentObject)
{
// Enable editing mode
componentObject.Finalized = false;
// Force editor rebuild
scene.SelectedItem = null;
scene.SelectedItem = componentObject;
scene.SelectedItem = componentObject as Object3D;
scene.Invalidate(new InvalidateArgs(null, InvalidateType.Children));
}
Expand All @@ -242,13 +242,13 @@ public static SceneOperation EditComponentOperation()
var sceneItem = scene.SelectedItem;
return sceneItem.Parent != null
&& sceneItem.Parent.Parent == null
&& sceneItem is ComponentObject3D componentObject
&& sceneItem is IComponentObject3D componentObject
&& componentObject.Finalized
&& !componentObject.ProOnly;
},
Icon = (theme) => StaticData.Instance.LoadIcon("scale_32x32.png", 16, 16).GrayToColor(theme.TextColor).SetPreMultiply(),
HelpTextGetter = () => "A component must be selected".Localize().Stars(),
IsEnabled = (sceneContext) => sceneContext.Scene.SelectedItem != null && (sceneContext.Scene.SelectedItem is ComponentObject3D),
IsEnabled = (sceneContext) => sceneContext.Scene.SelectedItem != null && (sceneContext.Scene.SelectedItem is IComponentObject3D),
};
}

Expand All @@ -274,7 +274,7 @@ public static SceneOperation ImageConverterOperation()
return new SceneOperation("ImageConverter")
{
TitleGetter = () => "Image Converter".Localize(),
ResultType = typeof(ComponentObject3D),
ResultType = typeof(IComponentObject3D),
Action = (sceneContext) =>
{
var scene = sceneContext.Scene;
Expand Down
4 changes: 2 additions & 2 deletions MatterControlLib/DesignTools/Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static IEnumerable<DirectOrExpression> GetActiveExpression(IObject3D item
}
}

public static IEnumerable<int> GetComponentExpressions(ComponentObject3D component, string checkForString, bool startsWith)
public static IEnumerable<int> GetComponentExpressions(IComponentObject3D component, string checkForString, bool startsWith)
{
for (var i = 0; i < component.SurfacedEditors.Count; i++)
{
Expand Down Expand Up @@ -239,7 +239,7 @@ public static bool HasExpressionWithString(IObject3D itemToCheck, string checkFo
foreach (var item in itemToCheck.DescendantsAndSelf())
{
if (GetActiveExpression(item, checkForString, startsWith).Any()
|| itemToCheck is ComponentObject3D component
|| itemToCheck is IComponentObject3D component
&& GetComponentExpressions(component, checkForString, startsWith).Any())
{
// three is one so return true
Expand Down
4 changes: 2 additions & 2 deletions MatterControlLib/DesignTools/Operations/ArrayObject3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override void Apply(Agg.UI.UndoBuffer undoBuffer)
}

// Also convert index expressions in ComponentObjects to their constants
if (item is ComponentObject3D component)
if (item is IComponentObject3D component)
{
for (int i = 0; i < component.SurfacedEditors.Count; i++)
{
Expand Down Expand Up @@ -105,7 +105,7 @@ internal void ProcessIndexExpressions()
// we don't need to rebuild our source object
return false;
}
else if (item.Parent is ComponentObject3D)
else if (item.Parent is IComponentObject3D)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ IEnumerable<Vector2> GetVertices()
foreach (var visibleMesh in visibleMeshes)
{
var matrix = visibleMesh.source.WorldMatrix(object3D);
if (visibleMesh.convexHull == null)
{
continue;
}
foreach (var positon in visibleMesh.convexHull.Vertices)
{
var transformed = positon.Transform(matrix);
Expand Down
96 changes: 74 additions & 22 deletions MatterControlLib/DesignTools/Operations/PathEditorWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public enum ControlPointConstraint

private VertexStorage vertexStorage;

// the last vertex storage before the last change
private VertexStorage beforeLastChange;

private ControlPointConstraint controlPointConstraint = ControlPointConstraint.Free;

public PathEditorWidget(VertexStorage vertexStorage,
Expand All @@ -92,6 +95,7 @@ public PathEditorWidget(VertexStorage vertexStorage,

this.unscaledRenderOffset = unscaledRenderOffset;
this.layerScale = layerScale;
this.undoBuffer = undoBuffer;

var topToBottom = this;

Expand All @@ -105,6 +109,8 @@ public PathEditorWidget(VertexStorage vertexStorage,
this.vertexChanged = vertexChanged;
this.theme = theme;
this.vertexStorage = vertexStorage;
this.beforeLastChange = new VertexStorage();
beforeLastChange.SvgDString = vertexStorage.SvgDString;

var toolBar = new FlowLayoutWidget()
{
Expand Down Expand Up @@ -148,7 +154,7 @@ private void AddPositionControls(ThemeConfig theme, FlowLayoutWidget toolBar)
var oldPosition = vertexStorage[controlPointBeingDragged].Position;
var newPosition = new Vector2(xEditWidget.ActuallNumberEdit.Value, yEditWidget.ActuallNumberEdit.Value);
var delta = newPosition - oldPosition;
OffsetSelectedPoint(delta);
OffsetSelectedPoint(delta, true);
};

xEditWidget.ActuallNumberEdit.KeyDown += NumberField.InternalTextEditWidget_KeyDown;
Expand All @@ -168,7 +174,7 @@ private void AddPositionControls(ThemeConfig theme, FlowLayoutWidget toolBar)
var oldPosition = vertexStorage[controlPointBeingDragged].Position;
var newPosition = new Vector2(xEditWidget.ActuallNumberEdit.Value, yEditWidget.ActuallNumberEdit.Value);
var delta = newPosition - oldPosition;
OffsetSelectedPoint(delta);
OffsetSelectedPoint(delta, true);
};
yEditWidget.ActuallNumberEdit.KeyDown += NumberField.InternalTextEditWidget_KeyDown;

Expand Down Expand Up @@ -234,6 +240,8 @@ private void AddButtons(ThemeConfig theme, FlowLayoutWidget toolBar)
public ETransformState TransformState { get; set; }
private double layerScale { get; set; } = 1;

private UndoBuffer undoBuffer;

private Affine ScalingTransform => Affine.NewScaling(layerScale, layerScale);
private Affine TotalTransform => Affine.NewTranslation(unscaledRenderOffset) * ScalingTransform * Affine.NewTranslation(Width / 2, Height / 2);

Expand All @@ -246,6 +254,7 @@ private void AddButtons(ThemeConfig theme, FlowLayoutWidget toolBar)
private ThemedRadioTextButton sharpButton;
private ThemedRadioTextButton alignedButton;
private ThemedRadioTextButton freeButton;
private bool hasBeenStartupPositioned;

public void CenterPartInView()
{
Expand All @@ -254,15 +263,29 @@ public void CenterPartInView()
var partBounds = vertexStorage.GetBounds();
var weightedCenter = partBounds.Center;

var bottomPixels = 20;
var margin = 30;
unscaledRenderOffset = -weightedCenter;
layerScale = Math.Min((Height - 30) / partBounds.Height, (Width - 30) / partBounds.Width);
layerScale = Math.Min((Height - margin - bottomPixels * 2) / partBounds.Height, (Width - margin) / partBounds.Width);
unscaledRenderOffset += new Vector2(0, bottomPixels) / layerScale;

Invalidate();
scaleChanged?.Invoke(unscaledRenderOffset, layerScale);
}
}

public override void OnDraw(Graphics2D graphics2D)
{
if (!hasBeenStartupPositioned)
{
if (unscaledRenderOffset == Vector2.Zero
&& layerScale == 1)
{
CenterPartInView();
}
hasBeenStartupPositioned = true;
}

new VertexSourceApplyTransform(vertexStorage, TotalTransform).RenderPath(graphics2D, theme.TextColor, 2, true, theme.PrimaryAccentColor.Blend(theme.TextColor, .5), theme.PrimaryAccentColor);

//if (vertexStorage.GetType().GetCustomAttributes(typeof(PathEditorFactory.ShowAxisAttribute), true).FirstOrDefault() is PathEditorFactory.ShowAxisAttribute showAxisAttribute)
Expand Down Expand Up @@ -412,7 +435,7 @@ public override void OnMouseMove(MouseEventArgs mouseEvent)
if (mouseDelta.LengthSquared > 0)
{
ScalingTransform.inverse_transform(ref mouseDelta);
OffsetSelectedPoint(mouseDelta);
OffsetSelectedPoint(mouseDelta, false);
UpdateControlsForSelection();
}
}
Expand All @@ -425,38 +448,67 @@ public override void OnMouseMove(MouseEventArgs mouseEvent)
lastMousePosition = mouseEvent.Position;
}

private void OffsetSelectedPoint(Vector2 delta)
public override void OnMouseUp(MouseEventArgs mouseEvent)
{
OffsetSelectedPoint(new Vector2(), true);
base.OnMouseUp(mouseEvent);
}

private void OffsetSelectedPoint(Vector2 delta, bool recordUndo)
{
if (controlPointBeingDragged < 0
|| controlPointBeingDragged >= vertexStorage.Count
|| delta.LengthSquared == 0)
|| controlPointBeingDragged >= vertexStorage.Count)
{
return;
}

var vertexData = vertexStorage[controlPointBeingDragged];

if (vertexData.Hint == CommandHint.C4Point)
if (delta.LengthSquared > 0)
{
for (int i = -1; i < 2; i++)
var vertexData = vertexStorage[controlPointBeingDragged];

if (vertexData.Hint == CommandHint.C4Point)
{
var pointIndex = controlPointBeingDragged + i;
// the prev point
if (pointIndex > 0
&& pointIndex < vertexStorage.Count)
for (int i = -1; i < 2; i++)
{
var vertexData2 = vertexStorage[pointIndex];
vertexStorage[pointIndex] = new VertexData(vertexData2.Command, vertexData2.Position + delta, vertexData2.Hint);
var pointIndex = controlPointBeingDragged + i;
// the prev point
if (pointIndex > 0
&& pointIndex < vertexStorage.Count)
{
var vertexData2 = vertexStorage[pointIndex];
vertexStorage[pointIndex] = new VertexData(vertexData2.Command, vertexData2.Position + delta, vertexData2.Hint);
}
}
}
else
{
// only drag the point
vertexStorage[controlPointBeingDragged] = new VertexData(vertexData.Command, vertexData.Position + delta, vertexData.Hint);
}

vertexChanged?.Invoke();
}
else

if (recordUndo)
{
// only drag the point
vertexStorage[controlPointBeingDragged] = new VertexData(vertexData.Command, vertexData.Position + delta, vertexData.Hint);
}
var doVertexBuffer = new VertexStorage();
doVertexBuffer.SvgDString = vertexStorage.SvgDString;

var undoVertexBuffer = new VertexStorage();
undoVertexBuffer.SvgDString = beforeLastChange.SvgDString;

vertexChanged?.Invoke();
undoBuffer.AddAndDo(new UndoRedoActions(() =>
{
vertexStorage.SvgDString = undoVertexBuffer.SvgDString;
vertexChanged?.Invoke();
}, () =>
{
vertexStorage.SvgDString = doVertexBuffer.SvgDString;
vertexChanged?.Invoke();
}));
// record the change
beforeLastChange.SvgDString = vertexStorage.SvgDString;
}
}

private void DoTranslateAndZoom(MouseEventArgs mouseEvent)
Expand Down
18 changes: 10 additions & 8 deletions MatterControlLib/DesignTools/Operations/RadialPinchObject3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ public RadialPinchObject3D()
[Slider(0, 50, snapDistance: 1)]
public IntOrExpression PinchSlices { get; set; } = 20;

[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]

[Description("Enable advanced features.")]
public bool Advanced { get; set; } = false;

public enum PinchType { Radial, XAxis }

public PinchType PinchTypeValue { get; set; } = PinchType.Radial;

[Description("Allows for the repositioning of the rotation origin")]
public Vector2 RotationOffset { get; set; }

Expand Down Expand Up @@ -203,15 +209,11 @@ public override Task Rebuild()
}
var positionXy = new Vector2(position) - rotationCenter;
var fromLine = true;
if (fromLine)
{
positionXy *= horizontalOffset.GetXAtY(position.Z * 10) / (maxRadius * 10);
//positionXy *= xAtYInterpolator.Get(position.Z * 10) / maxRadius;
}
else
positionXy *= horizontalOffset.GetXAtY(position.Z * 10) / (maxRadius * 10);
if (PinchTypeValue == PinchType.XAxis)
{
positionXy *= Easing.Quadratic.InOut(ratio);
// only use the x value
positionXy.Y = position.Y;
}
positionXy += rotationCenter;
transformedMesh.Vertices[i] = new Vector3Float(positionXy.X, positionXy.Y, position.Z);
Expand Down
Loading

0 comments on commit c4b58c9

Please sign in to comment.