private void CalculateVertices(List <windows.Point> points, ref int index, Vector4 color, bool IsGrid = false) { int increment = 1; float offset = 2.5f; if (IsGrid) { increment = 2; offset = 1.0f; } for (int i = 0; i < points.Count - 1; i += increment) { Vector2 start = new Vector2((float)points[i].X, (float)points[i].Y); Vector2 end = new Vector2((float)points[i + 1].X, (float)points[i + 1].Y); // Calculate the angle between start and end using polar coordinates var tan = Math.Atan(((start.Y - end.Y) / (start.X - end.X))); List <System.Windows.Point> sList = new List <System.Windows.Point>(); List <System.Windows.Point> eList = new List <System.Windows.Point>(); sList.Add(new windows.Point(start.X, start.Y - offset)); sList.Add(new windows.Point(start.X, start.Y + offset)); eList.Add(new windows.Point(end.X, end.Y + offset)); eList.Add(new windows.Point(end.X, end.Y - offset)); sList = LerpMath.RotateAroundPoint(sList, new System.Windows.Point(start.X, start.Y), tan); eList = LerpMath.RotateAroundPoint(eList, new System.Windows.Point(end.X, end.Y), tan); // 2 triangles form one quad // Triangle 1 vertices[index] = new Vector4((float)sList[0].X, (float)sList[0].Y, 1.0f, 1.0f); vertices[index + 1] = color; vertices[index + 2] = new Vector4(1.0f, 0.0f, 1.0f, 1.0f); vertices[index + 3] = new Vector4((float)sList[1].X, (float)sList[1].Y, 1.0f, 1.0f); vertices[index + 4] = color; vertices[index + 5] = new Vector4(0.0f, 0.0f, 1.0f, 1.0f); vertices[index + 6] = new Vector4((float)eList[0].X, (float)eList[0].Y, 1.0f, 1.0f); vertices[index + 7] = color; vertices[index + 8] = new Vector4(0.0f, 1.0f, 1.0f, 1.0f); // Triangle 2 vertices[index + 9] = vertices[index + 6]; vertices[index + 10] = color; vertices[index + 11] = vertices[index + 8]; vertices[index + 15] = vertices[index]; vertices[index + 16] = color; vertices[index + 17] = vertices[index + 2]; vertices[index + 12] = new Vector4((float)eList[1].X, (float)eList[1].Y, 1.0f, 1.0f); vertices[index + 13] = color; vertices[index + 14] = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); index += 18; } }
public void sodoff(object sender, MouseEventArgs e) { GraphView_StateManager manager = ProjectStateManager.Instance.StateManagers.OfType <GraphView_StateManager>().First(); CurveGraphViewModel vm = ((CurveGraphViewModel)manager.ViewModel); if (vm == null) { return; } Point pos = e.GetPosition(sender as Canvas); pos.X -= vm.Camera.GetTransform(0, 0); pos.Y -= vm.Camera.GetTransform(0, 1); double value = 0; for (int x = 0; x < vm.ModelItems.Item.Curve.Count; x++) { var curve = vm.ModelItems.Item.Curve[x].Points; for (int i = 0; i < curve.Count; i++) { if (i != 0 && pos.X < curve[i].X) { Point point1 = curve[i - 1]; Point point2 = curve[i]; value = LerpMath.Lerp(point1, point2, pos.X / point2.X).Y; break; } } } double[] camCoords = new double[] { vm.Camera.GetTransform(0, 0), vm.Camera.GetTransform(0, 1) }; double scale = vm.Camera.GetScale; xLabel.Content = String.Format("Mouse: {0:N2} {1:N2} | Camera: {2:N2} {3:N2} | Scale: {4:N2} | Curve value: {5:N2}", (e.GetPosition(_View.GraphView).X - camCoords[0]) * scale, (e.GetPosition(_View.GraphView).Y - camCoords[1]) * scale, camCoords[0], camCoords[1], scale, value ); }
//Calculates end point for object movement public void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e) { _Canvas = sender as Canvas; dragEnd[0] = e.GetPosition(_Canvas).X; dragEnd[1] = e.GetPosition(_Canvas).Y; if (_DragSelection) { if (dragBox != null) { double width = LerpMath.CalculateDelta(dragOrigin[0], dragEnd[0]); double height = LerpMath.CalculateDelta(dragOrigin[1], dragEnd[1]); width = (width < 0) ? width * -1 : width; height = (height < 0) ? height * -1 : height; dragBox.Width = width; dragBox.Height = height; double offsetX = (dragOrigin[0] < dragEnd[0]) ? dragOrigin[0] : dragEnd[0]; double offsetY = (dragOrigin[1] < dragEnd[1]) ? dragOrigin[1] : dragEnd[1]; Canvas.SetLeft(dragBox, offsetX); Canvas.SetTop(dragBox, offsetY); } else { Rectangle rect = new Rectangle(); rect.IsHitTestVisible = false; rect.Fill = new SolidColorBrush(Colors.Blue); rect.Opacity = 0.4; rect.Name = "dragArea"; rect.Width = LerpMath.CalculateDelta(dragOrigin[0], dragEnd[0]); rect.Height = LerpMath.CalculateDelta(dragOrigin[1], dragEnd[1]); Items.Add(rect); dragBox = rect; Canvas.SetLeft(rect, dragOrigin[0]); Canvas.SetTop(rect, dragOrigin[1]); dragIndex = Items.Count - 1; } } }
//TODO: Fix adorners not being drawn correctly after moving if scale != 1 or being redrawn on scale change /// <summary> /// Adds adorner to items in list /// </summary> /// <param name="modifiedList">List of item Points</param> private void RenderAdorner(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add && selectedItems.Count > 0) { ClearAdorner(); Point pos; var item = Items[0]; if (selectedPos.Count > 1) { pos = LerpMath.CalculateAverage(selectedPos); } else { pos = selectedPos[0]; } var myAdornerLayer = AdornerLayer.GetAdornerLayer(item); RenderDragHandles(item, myAdornerLayer, camera.OffsetPosition(pos, true)); for (int i = 0; i < selectedPos.Count; i++) { for (int j = 0; j < Items.Count; j++) { if (Items[j] is Rectangle) { var listItem = Items[j]; Point itemPos = camera.OffsetPosition(new Point(Canvas.GetLeft(listItem), Canvas.GetTop(listItem))); if (LerpMath.CalculateDelta(selectedPos[i].X, itemPos.X) <= 2.5 && LerpMath.CalculateDelta(selectedPos[i].Y, itemPos.Y) <= 2.5) { RenderSelection(listItem, AdornerLayer.GetAdornerLayer(item)); break; } } } } } else { ClearAdorner(); } }
// Update is called once per frame void Update() { time += Time.deltaTime; if (time >= timeTotal) { time = timeTotal; } var t = time / timeTotal; var modT = LerpMath.ModifyTimeWithStyle(style, t); var lerpedVal = Mathf.Lerp(startValue, targetValue, modT); var type = targetObject.GetType(); var field = type.GetProperty(fieldName); field.SetValue(targetObject, lerpedVal, null); if (time == timeTotal) { Destroy(this); } }
void LerpUpdate() { if (!shouldAutoStart) { return; } time += Time.deltaTime; if (time >= timeTotal) { time = timeTotal; } var t = time / timeTotal; var modT = LerpMath.ModifyTimeWithStyle(style, t); var lerpedVal = Vector3.Lerp(startValue, targetValue, modT); var type = targetObject.GetType(); var field = type.GetProperty(fieldName); field.SetValue(targetObject, lerpedVal, null); if (time == timeTotal) { if (shouldLoop) { var temp = targetValue; targetValue = startValue; startValue = temp; time = 0f; } else { Destroy(this); } } // targetObject.GetType ().GetField (fieldName).SetValue (targetObject, lerpedVal); }
/// <summary> /// Calculates delta movement /// </summary> /// <param name="model"></param> private void CalculatePosition(CurveGraphModel model) { Point dest; if (!MiddleMouseUsed) { dest = LerpMath.CalculateDelta(new Point(dragOrigin[0], dragOrigin[1]), new Point(dragEnd[0], dragEnd[1]), false, true); } else { dest = LerpMath.CalculateDelta(new Point(dragOrigin[2], dragOrigin[3]), new Point(dragEnd[0], dragEnd[1]), false, true); double offsetMultiplier = 0.55; dest = new Point( (dest.X * offsetMultiplier), (dest.Y * offsetMultiplier) ); MiddleMouseUsed = false; dragDirection = 0; } movementQueue.Add(new Point(dest.X, dest.Y)); }
public static List <Point> Draw(BezierCurve curve, Camera cam) { double[] offset = new double[] { cam.GetTransform(0, 0), cam.GetTransform(0, 1) }; double scale = cam.GetScale; BezierCurve _Curve = BezierCurve.ScaleAdjusted(curve, cam); PointCollection lerpList = new PointCollection(); for (int i = 0; i < _Curve.Smoothness.Count; i++) { double steps = 0; int multiplier = 0; for (int j = 0; j < i; j++) { multiplier += _Curve.Smoothness[j] + 1; } try { int lineSmoothness = _Curve.Smoothness[i]; while (steps < 1.05) { if (lineSmoothness != 0) { if (lineSmoothness == 1) { Point lerp1 = LerpMath.Lerp(_Curve.Points[multiplier], _Curve.Points[1 + multiplier], steps); Point lerp2 = LerpMath.Lerp(_Curve.Points[1 + multiplier], _Curve.Points[2 + multiplier], steps); Point lerp4 = LerpMath.Lerp(lerp1, lerp2, steps); lerpList.Add(lerp4); } else { Point lerp1 = LerpMath.Lerp(_Curve.Points[multiplier], _Curve.Points[1 + (multiplier)], steps); Point lerp2 = LerpMath.Lerp(_Curve.Points[1 + multiplier], _Curve.Points[2 + multiplier], steps); Point lerp3 = LerpMath.Lerp(_Curve.Points[2 + multiplier], _Curve.Points[3 + multiplier], steps); Point lerp4 = LerpMath.Lerp(lerp1, lerp2, steps); Point lerp5 = LerpMath.Lerp(lerp2, lerp3, steps); Point lerp6 = LerpMath.Lerp(lerp4, lerp5, steps); lerpList.Add(lerp6); } } else { lerpList.Add(LerpMath.Lerp(_Curve.Points[multiplier], _Curve.Points[multiplier + 1], steps)); } if (scale < 0.5) { steps += 0.05; } else { steps += 0.1; } } } catch (Exception) { //throw; } } return(lerpList.ToList()); }
private void HandleSelection(object sender, MouseButtonEventArgs e) { //If rectangle hit if (e.OriginalSource.GetType() == typeof(Rectangle)) { if (selectedItems.Count > 0 && !_shift) { ClearItems(); } Rectangle rect = (Rectangle)e.OriginalSource; selectedPos.Add(camera.OffsetPosition(new Point(Canvas.GetLeft(rect), Canvas.GetTop(rect)))); if (rect != null && String.IsNullOrEmpty(rect.Name)) { var myAdornerLayer = AdornerLayer.GetAdornerLayer(rect); if (myAdornerLayer != null) { if (selectedPos.Count > 1) { Point avgPosition = LerpMath.CalculateAverage(selectedPos); Point offset = LerpMath.CalculateDelta( modelitems.Item.Curve[selectedItems[0].Item1].Points[selectedItems[0].Item2], avgPosition, true ); } else { RenderSelection(rect, myAdornerLayer); } var matches = Regex.Matches(rect.Tag.ToString(), "[0-9]{1,3}"); if (!Keyboard.IsKeyDown(Key.LeftShift) && !Keyboard.IsKeyDown(Key.RightShift)) { ClearItems(); selectedPos.Add(camera.OffsetPosition(new Point(Canvas.GetLeft(rect), Canvas.GetTop(rect)))); } selectedItems.Add(new Tuple <int, int>(Convert.ToInt32(matches[0].ToString()), Convert.ToInt32(matches[1].ToString()))); } } _shift = false; } //Drag selection else if (_dragDropped && (3 * camera.GetScale) < LerpMath.CalculateDelta(dragOrigin[0], dragEnd[0]) || (3 * camera.GetScale) < LerpMath.CalculateDelta(dragOrigin[1], dragEnd[1])) { if ((DateTime.Now - _time).Milliseconds > 100) { if (!_shift) { ClearItems(); } _shift = false; for (int j = 0; j < modelitems.Item.Curve.Count; j++) { var list = modelitems.Item.Curve[j].Original; double minX = (dragOrigin[0] < dragEnd[0]) ? dragOrigin[0] : dragEnd[0]; double minY = (dragOrigin[1] < dragEnd[1]) ? dragOrigin[1] : dragEnd[1]; double maxX = (dragOrigin[0] > dragEnd[0]) ? dragOrigin[0] : dragEnd[0]; double maxY = (dragOrigin[1] > dragEnd[1]) ? dragOrigin[1] : dragEnd[1]; for (int i = 0; i < _Canvas.Children.Count; i++) { if (_Canvas.Children[i] is Rectangle) { Rectangle rect = (Rectangle)_Canvas.Children[i]; if ((minX - 3) < Canvas.GetLeft(rect) && (maxX + 3) > Canvas.GetLeft(rect)) { if ((minY - 3) < Canvas.GetTop(rect) && (maxY + 3) > Canvas.GetTop(rect)) { var matches = Regex.Matches(rect.Tag.ToString(), "[0-9]{1,3}"); Tuple <int, int> curvePointNr = new Tuple <int, int>(Convert.ToInt32(matches[0].ToString()), Convert.ToInt32(matches[1].ToString())); bool exists = false; if (selectedItems.IndexOf(curvePointNr) != -1) { exists = true; } if (!exists) { selectedPos.Add(camera.OffsetPosition(new Point(Canvas.GetLeft(rect), Canvas.GetTop(rect)))); selectedItems.Add(curvePointNr); dragOrigin[0] = Canvas.GetLeft(rect); dragOrigin[1] = Canvas.GetTop(rect); var myAdornerLayer = AdornerLayer.GetAdornerLayer(rect); RenderSelection(rect, myAdornerLayer); } } } } } } } } //Remove selection else { ClearItems(); draggedItem = null; } }
public void OnMouseUp(object sender, MouseButtonEventArgs e) { List <Point> modifiedList = new List <Point>(); switch (GraphView_ToolHandler.ActiveTool) { case 0: if (dragIndex < Items.Count && Items[dragIndex].Name == "dragArea") { Items.RemoveAt(dragIndex); } if (_DragSelection) { _dragDropped = true; _DragSelection = false; dragBox = null; } //Move if (draggedItem != null || MiddleMouseUsed || oldPosition.X != 0) { CurveGraphModel model = new CurveGraphModel(); model = modelitems; if (oldPosition.X != 0) { Point delta = LerpMath.CalculateDelta ( oldPosition, Mouse.GetPosition(Application.Current.MainWindow), true ); double x = camera.GetTransform(0, 0); double y = camera.GetTransform(0, 1); camera.SetTransform(x + delta.X, y + delta.Y); oldPosition.X = 0; } CalculatePosition(model); ApplyMovement(ref model, ref modifiedList); Add(model); draggedItem = null; } //Selection else { HandleSelection(sender, e); } modifiedList.Clear(); break; case 1: ClearItems(); break; default: break; } }
public override void OnMouseDown(object sender, MouseButtonEventArgs e) { CheckModifier(); switch (GraphView_ToolHandler.ActiveTool) { //Select tool case 0: //Toolhandle if (e.ChangedButton == MouseButton.Left && e.OriginalSource.GetType() == typeof(ToolAdorner)) { ToolAdorner rect = (ToolAdorner)e.OriginalSource; if (Regex.IsMatch(rect.Name, "(left)?(right)?(top)?(bottom)?")) { dragDirection = (rect.Name == "left" || rect.Name == "right") ? dragDirection = 1 : dragDirection = 2; if (selectedPos.Count > 1) { Point origin = LerpMath.CalculateAverage(selectedPos); dragOrigin[0] = origin.X; dragOrigin[1] = origin.Y; } else { dragOrigin[0] = e.GetPosition(_Canvas).X; dragOrigin[1] = e.GetPosition(_Canvas).Y; } draggedItem = rect; return; } return; } //Drag select if (e.ChangedButton == MouseButton.Left && !(e.OriginalSource.GetType() == typeof(Rectangle) && !(e.OriginalSource.GetType() == typeof(ToolAdorner)))) { _DragSelection = true; _time = DateTime.Now; dragOrigin[0] = e.GetPosition(_Canvas).X; dragOrigin[1] = e.GetPosition(_Canvas).Y; return; } //move camera if (_alt && e.ChangedButton == MouseButton.Middle) { _alt = false; oldPosition = Mouse.GetPosition(Application.Current.MainWindow); return; } //drag move if (e.ChangedButton == MouseButton.Middle && selectedItems.Count > 0) { dragOrigin[2] = Mouse.GetPosition(Application.Current.MainWindow).X; dragOrigin[3] = Mouse.GetPosition(Application.Current.MainWindow).Y; MiddleMouseUsed = true; return; } break; //Insert Point Tool case 1: if (e.ChangedButton == MouseButton.Left) { Point pos = camera.OffsetPosition(e.GetPosition(_Canvas)); CurveGraphModel model = ModelItems; // TODO: Need to find ways to separate curves from each other //BezierCurve curve = model.Item.Curve; //curve.InsertPoint(pos); //Add(model); } break; default: break; } }