2020-09-13 13:04:57 -07:00
|
|
|
|
/*
|
|
|
|
|
|
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 MatterHackers.Agg;
|
2020-09-13 13:25:26 -07:00
|
|
|
|
using MatterHackers.Agg.UI;
|
2020-09-13 13:04:57 -07:00
|
|
|
|
using MatterHackers.DataConverters3D;
|
2021-06-06 09:07:18 -07:00
|
|
|
|
using MatterHackers.Localizations;
|
2020-09-13 13:04:57 -07:00
|
|
|
|
using MatterHackers.MatterControl.PartPreviewWindow;
|
|
|
|
|
|
using MatterHackers.MeshVisualizer;
|
|
|
|
|
|
using MatterHackers.PolygonMesh;
|
|
|
|
|
|
using MatterHackers.RayTracer;
|
|
|
|
|
|
using MatterHackers.RayTracer.Traceable;
|
|
|
|
|
|
using MatterHackers.RenderOpenGl;
|
|
|
|
|
|
using MatterHackers.VectorMath;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.DesignTools
|
|
|
|
|
|
{
|
|
|
|
|
|
public class TracedPositionObject3DControl : IObject3DControl
|
|
|
|
|
|
{
|
2020-09-15 07:56:31 -07:00
|
|
|
|
private readonly double blockSize = 11 * GuiWidget.DeviceScale;
|
2020-09-13 13:25:26 -07:00
|
|
|
|
|
|
|
|
|
|
private ITraceable collisionVolume;
|
2021-04-17 22:35:53 -07:00
|
|
|
|
|
|
|
|
|
|
public IObject3DControlContext Object3DControlContext { get; }
|
|
|
|
|
|
|
2020-09-13 15:02:30 -07:00
|
|
|
|
private ThemeConfig theme;
|
2020-09-13 13:25:26 -07:00
|
|
|
|
private IObject3DControlContext context;
|
|
|
|
|
|
|
2020-09-13 13:04:57 -07:00
|
|
|
|
private Func<Vector3> getPosition;
|
|
|
|
|
|
|
|
|
|
|
|
private Action<Vector3> setPosition;
|
2021-04-29 11:17:31 -07:00
|
|
|
|
|
|
|
|
|
|
private Action<Vector3> editComplete;
|
2020-09-13 13:04:57 -07:00
|
|
|
|
|
|
|
|
|
|
private Mesh shape;
|
2020-09-13 15:02:30 -07:00
|
|
|
|
private bool mouseOver;
|
2021-04-22 07:47:03 -07:00
|
|
|
|
public PlaneShape HitPlane { get; private set; }
|
2021-04-21 18:07:44 -07:00
|
|
|
|
public bool DownOnControl { get; private set; }
|
2020-09-13 13:04:57 -07:00
|
|
|
|
|
2021-04-27 07:19:02 -07:00
|
|
|
|
private Vector3 mouseDownPosition;
|
|
|
|
|
|
|
2021-04-17 22:35:53 -07:00
|
|
|
|
public TracedPositionObject3DControl(IObject3DControlContext object3DControlContext,
|
|
|
|
|
|
IObject3D owner,
|
|
|
|
|
|
Func<Vector3> getPosition,
|
2021-04-29 11:17:31 -07:00
|
|
|
|
Action<Vector3> setPosition,
|
|
|
|
|
|
Action<Vector3> editComplete)
|
2020-09-13 13:04:57 -07:00
|
|
|
|
{
|
2021-04-17 22:35:53 -07:00
|
|
|
|
this.Object3DControlContext = object3DControlContext;
|
|
|
|
|
|
|
2020-09-13 15:02:30 -07:00
|
|
|
|
this.theme = ApplicationController.Instance.Theme;
|
2021-04-17 22:35:53 -07:00
|
|
|
|
this.context = object3DControlContext;
|
2020-09-13 13:04:57 -07:00
|
|
|
|
this.getPosition = getPosition;
|
|
|
|
|
|
this.setPosition = setPosition;
|
2021-04-29 11:17:31 -07:00
|
|
|
|
this.editComplete = editComplete;
|
2020-09-15 07:56:31 -07:00
|
|
|
|
this.shape = PlatonicSolids.CreateCube();
|
|
|
|
|
|
this.shape = SphereObject3D.CreateSphere(1, 15, 10);
|
2020-09-13 13:04:57 -07:00
|
|
|
|
collisionVolume = shape.CreateBVHData();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-13 19:09:09 -07:00
|
|
|
|
public bool DrawOnTop => true;
|
2020-09-13 13:04:57 -07:00
|
|
|
|
|
|
|
|
|
|
public string Name => "Traced Position";
|
|
|
|
|
|
|
|
|
|
|
|
public bool Visible { get; set; }
|
|
|
|
|
|
|
2021-06-06 09:07:18 -07:00
|
|
|
|
public string UiHint => "Type 'Esc' to cancel".Localize();
|
2020-09-13 13:04:57 -07:00
|
|
|
|
|
2021-06-06 09:07:18 -07:00
|
|
|
|
public void CancelOperation()
|
2020-09-13 13:04:57 -07:00
|
|
|
|
{
|
2021-06-06 09:07:18 -07:00
|
|
|
|
if (DownOnControl)
|
|
|
|
|
|
{
|
|
|
|
|
|
ApplicationController.Instance.UiHint = "";
|
|
|
|
|
|
DownOnControl = false;
|
|
|
|
|
|
setPosition(mouseDownPosition);
|
|
|
|
|
|
ApplicationController.Instance.UiHint = "";
|
|
|
|
|
|
}
|
2020-09-13 13:04:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Draw(DrawGlContentEventArgs e)
|
2020-09-13 14:17:33 -07:00
|
|
|
|
{
|
2020-09-13 15:02:30 -07:00
|
|
|
|
var color = Color.Black;
|
|
|
|
|
|
if (mouseOver)
|
|
|
|
|
|
{
|
|
|
|
|
|
color = theme.PrimaryAccentColor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-17 22:35:53 -07:00
|
|
|
|
GLHelper.Render(shape, color.WithAlpha(e.Alpha0to255), ShapeMatrix(), RenderTypes.Shaded);
|
|
|
|
|
|
|
2021-04-22 07:47:03 -07:00
|
|
|
|
if (HitPlane != null)
|
2021-04-17 22:35:53 -07:00
|
|
|
|
{
|
|
|
|
|
|
//Object3DControlContext.World.RenderPlane(hitPlane.Plane, Color.Red, true, 30, 3);
|
|
|
|
|
|
//Object3DControlContext.World.RenderPlane(initialHitPosition, hitPlane.Plane.Normal, Color.Red, true, 30, 3);
|
|
|
|
|
|
}
|
2020-09-13 14:17:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-02 00:52:04 +00:00
|
|
|
|
AxisAlignedBoundingBox IObject3DControl.GetWorldspaceAABB()
|
|
|
|
|
|
{
|
|
|
|
|
|
return shape.GetAxisAlignedBoundingBox().NewTransformed(ShapeMatrix());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-13 14:17:33 -07:00
|
|
|
|
private Matrix4X4 ShapeMatrix()
|
2020-09-13 13:04:57 -07:00
|
|
|
|
{
|
2020-09-13 19:09:09 -07:00
|
|
|
|
var worldPosition = getPosition();
|
2020-09-13 13:25:26 -07:00
|
|
|
|
double distBetweenPixelsWorldSpace = context.World.GetWorldUnitsPerScreenPixelAtPosition(worldPosition);
|
|
|
|
|
|
var scale = Matrix4X4.CreateScale(distBetweenPixelsWorldSpace * blockSize);
|
|
|
|
|
|
var offset = Matrix4X4.CreateTranslation(getPosition());
|
|
|
|
|
|
|
2020-09-13 19:09:09 -07:00
|
|
|
|
var cubeMatrix = scale * offset;
|
2020-09-13 14:17:33 -07:00
|
|
|
|
return cubeMatrix;
|
2020-09-13 13:04:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ITraceable GetTraceable()
|
|
|
|
|
|
{
|
2020-09-13 14:17:33 -07:00
|
|
|
|
return new Transform(collisionVolume, ShapeMatrix());
|
2020-09-13 13:04:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnMouseDown(Mouse3DEventArgs mouseEvent3D)
|
|
|
|
|
|
{
|
2021-04-21 18:07:44 -07:00
|
|
|
|
DownOnControl = true;
|
2021-06-06 09:07:18 -07:00
|
|
|
|
ApplicationController.Instance.UiHint = UiHint;
|
2021-04-27 07:19:02 -07:00
|
|
|
|
mouseDownPosition = getPosition();
|
2021-04-22 07:47:03 -07:00
|
|
|
|
// Make sure we always get a new hit plane
|
|
|
|
|
|
ResetHitPlane();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ResetHitPlane()
|
|
|
|
|
|
{
|
|
|
|
|
|
HitPlane = null;
|
2020-09-13 13:04:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-13 15:02:30 -07:00
|
|
|
|
public void OnMouseMove(Mouse3DEventArgs mouseEvent3D, bool mouseIsOver)
|
2020-09-13 13:04:57 -07:00
|
|
|
|
{
|
2020-09-13 15:02:30 -07:00
|
|
|
|
if (mouseOver != mouseIsOver)
|
|
|
|
|
|
{
|
|
|
|
|
|
mouseOver = mouseIsOver;
|
|
|
|
|
|
context.GuiSurface.Invalidate();
|
|
|
|
|
|
}
|
2020-09-13 19:09:09 -07:00
|
|
|
|
|
2021-04-21 18:07:44 -07:00
|
|
|
|
if (DownOnControl)
|
2020-09-13 19:09:09 -07:00
|
|
|
|
{
|
2021-04-21 18:07:44 -07:00
|
|
|
|
UpdatePosition(mouseEvent3D.MouseEvent2D.Position);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-17 22:35:53 -07:00
|
|
|
|
|
2021-04-21 18:07:44 -07:00
|
|
|
|
private void UpdatePosition(Vector2 screenPosition)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ray = context.World.GetRayForLocalBounds(screenPosition);
|
|
|
|
|
|
var scene = context.Scene;
|
|
|
|
|
|
var intersectionInfo = scene.GetBVHData().GetClosestIntersection(ray);
|
|
|
|
|
|
|
|
|
|
|
|
var oldPosition = getPosition();
|
|
|
|
|
|
var newPosition = oldPosition;
|
|
|
|
|
|
var world = Object3DControlContext.World;
|
|
|
|
|
|
var rayNormal = (oldPosition - world.EyePosition).GetNormal();
|
|
|
|
|
|
if (intersectionInfo == null)
|
|
|
|
|
|
{
|
2021-04-22 07:47:03 -07:00
|
|
|
|
if (HitPlane == null)
|
2020-09-13 19:09:09 -07:00
|
|
|
|
{
|
2021-04-22 07:47:03 -07:00
|
|
|
|
HitPlane = new PlaneShape(new Plane(rayNormal, oldPosition), null);
|
2021-04-17 22:35:53 -07:00
|
|
|
|
}
|
2021-04-21 18:07:44 -07:00
|
|
|
|
|
2021-04-22 07:47:03 -07:00
|
|
|
|
intersectionInfo = HitPlane.GetClosestIntersection(ray);
|
2021-04-21 18:07:44 -07:00
|
|
|
|
if (intersectionInfo != null)
|
2021-04-17 22:35:53 -07:00
|
|
|
|
{
|
2021-04-21 18:07:44 -07:00
|
|
|
|
newPosition = intersectionInfo.HitPosition;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-04-22 07:47:03 -07:00
|
|
|
|
HitPlane = new PlaneShape(new Plane(rayNormal, oldPosition), null);
|
2021-04-17 22:35:53 -07:00
|
|
|
|
|
2021-04-21 18:07:44 -07:00
|
|
|
|
foreach (var object3D in scene.Children)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (object3D.GetBVHData().Contains(intersectionInfo.HitPosition))
|
2020-09-13 19:09:09 -07:00
|
|
|
|
{
|
2021-04-21 18:07:44 -07:00
|
|
|
|
newPosition = intersectionInfo.HitPosition;
|
|
|
|
|
|
break;
|
2020-09-13 19:09:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-21 18:07:44 -07:00
|
|
|
|
}
|
2021-04-17 22:35:53 -07:00
|
|
|
|
|
2021-04-21 18:07:44 -07:00
|
|
|
|
if (newPosition != oldPosition)
|
|
|
|
|
|
{
|
|
|
|
|
|
setPosition(newPosition);
|
|
|
|
|
|
context.GuiSurface.Invalidate();
|
2020-09-13 19:09:09 -07:00
|
|
|
|
}
|
2020-09-13 13:04:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnMouseUp(Mouse3DEventArgs mouseEvent3D)
|
|
|
|
|
|
{
|
2021-04-29 11:17:31 -07:00
|
|
|
|
if (DownOnControl)
|
|
|
|
|
|
{
|
|
|
|
|
|
DownOnControl = false;
|
|
|
|
|
|
editComplete(mouseDownPosition);
|
2021-06-06 09:07:18 -07:00
|
|
|
|
ApplicationController.Instance.UiHint = "";
|
2021-04-29 11:17:31 -07:00
|
|
|
|
}
|
2020-09-13 13:04:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetPosition(IObject3D selectedItem, MeshSelectInfo selectInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2021-04-21 18:07:44 -07:00
|
|
|
|
|
|
|
|
|
|
public void MoveToScreenPosition(Vector2 screenPosition)
|
|
|
|
|
|
{
|
|
|
|
|
|
UpdatePosition(screenPosition);
|
|
|
|
|
|
}
|
2021-06-06 09:07:18 -07:00
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2020-09-13 13:04:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|