123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Shapes;
- using OptiTrack;
- using System.Windows.Ink;
- namespace SketchAssistantWPF
- {
- public class MVP_Presenter
- {
-
-
-
- MVP_View programView;
-
-
-
- MVP_Model programModel;
-
-
-
- Dictionary<int, Shape> rightPolyLines;
- ImageDimension CanvasSizeLeft = new ImageDimension(0, 0);
- ImageDimension CanvasSizeRight = new ImageDimension(0, 0);
- ImageDimension ImageSizeLeft = new ImageDimension(0, 0);
- ImageDimension ImageSizeRight = new ImageDimension(0, 0);
- List<double> ImageSimilarity = new List<double>();
- List<InternalLine> LeftLines = new List<InternalLine>();
-
-
-
- public enum MouseAction
- {
- Down,
- Up,
- Up_Invalid,
- Move
- }
-
-
-
-
-
-
- private FileImporter fileImporter;
- public MVP_Presenter(MVP_View form)
- {
- programView = form;
- programModel = new MVP_Model(this);
-
- fileImporter = new FileImporter();
- }
-
-
-
-
-
-
-
-
- public void Resize(Tuple<int, int> leftPBS, Tuple<int, int> rightPBS)
- {
- CanvasSizeLeft.ChangeDimension(leftPBS.Item1, leftPBS.Item2);
- CanvasSizeRight.ChangeDimension(rightPBS.Item1, rightPBS.Item2);
-
- programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
- }
-
-
-
-
- public bool SVGToolStripMenuItemClick()
- {
- var okToContinue = true; bool returnval = false;
- if (programModel.HasUnsavedProgress())
- {
- okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
- }
- if (okToContinue)
- {
- var fileNameTup = programView.openNewDialog("Scalable Vector Graphics|*.svg");
- if (!fileNameTup.Item1.Equals("") && !fileNameTup.Item2.Equals(""))
- {
- programView.SetToolStripLoadStatus(fileNameTup.Item2);
- try
- {
- Tuple<int, int, List<InternalLine>> values = fileImporter.ParseSVGInputFile(fileNameTup.Item1, programModel.leftImageBoxWidth, programModel.leftImageBoxHeight);
- values.Item3.ForEach(line => line.MakePermanent(0));
- programModel.SetLeftLineList(values.Item1, values.Item2, values.Item3);
- programModel.ResetRightImage();
- programModel.CanvasActivated();
- programModel.ChangeState(true);
- programView.EnableTimer();
- ClearRightLines();
- returnval = true;
- }
- catch (FileImporterException ex)
- {
- programView.ShowInfoMessage(ex.ToString());
- }
- catch (Exception ex)
- {
- programView.ShowInfoMessage("exception occured while trying to parse svg file:\n\n" + ex.ToString() + "\n\n" + ex.StackTrace);
- }
- }
- }
- return returnval;
- }
-
-
-
-
- public void ChangeState(bool NowDrawing)
- {
- programModel.ChangeState(NowDrawing);
- }
-
-
-
-
- public bool GetDrawingState()
- {
- return programModel.inDrawingMode;
- }
-
-
-
-
- public bool GetOptitrackActive()
- {
- return programModel.optiTrackInUse;
- }
-
-
-
- public void ChangeOptiTrack(bool usingOptiTrack)
- {
- if (programModel.optitrackAvailable)
- {
- programModel.SetOptiTrack(usingOptiTrack);
- }
- }
-
-
-
- public void Undo()
- {
- programModel.Undo();
- }
-
-
-
- public void Redo()
- {
- programModel.Redo();
- }
-
-
-
- public void Tick()
- {
- programModel.Tick();
- }
-
-
-
- public void NewCanvas()
- {
- var okToContinue = true;
- if (programModel.HasUnsavedProgress())
- {
- okToContinue = programView.ShowWarning("You have unsaved progress. Continue?");
- }
- if (okToContinue)
- {
- programModel.ResizeEvent(CanvasSizeLeft, CanvasSizeRight);
- programModel.ResetRightImage();
- programModel.CanvasActivated();
- programModel.ChangeState(true);
- programView.EnableTimer();
- ClearRightLines();
- }
- }
-
-
-
-
-
- public void MouseEvent(MouseAction mouseAction, Point position)
- {
- switch (mouseAction)
- {
- case MouseAction.Move:
- programModel.SetCurrentCursorPosition(position);
- break;
- default:
- break;
- }
- }
-
-
-
-
-
- public void MouseEvent(MouseAction mouseAction, StrokeCollection strokes)
- {
- if (!programModel.optiTrackInUse)
- {
- switch (mouseAction)
- {
- case MouseAction.Down:
- programModel.StartNewLine();
- break;
- case MouseAction.Up:
- if (strokes.Count > 0)
- {
- StylusPointCollection sPoints = strokes.First().StylusPoints;
- List<Point> points = new List<Point>();
- foreach (StylusPoint p in sPoints)
- points.Add(new Point(p.X, p.Y));
- programModel.FinishCurrentLine(points);
- }
- else
- {
- programModel.FinishCurrentLine(true);
- }
- break;
- case MouseAction.Up_Invalid:
- programModel.FinishCurrentLine(false);
- break;
- default:
- break;
- }
- }
- }
-
-
-
-
-
-
-
- public Point GetCursorPosition()
- {
- return programView.GetCursorPosition();
- }
-
-
-
-
- public void UpdateCurrentLine(List<Point> linepoints)
- {
- Polyline currentLine = new Polyline();
- currentLine.Stroke = Brushes.Black;
- currentLine.Points = new PointCollection(linepoints);
- programView.DisplayCurrLine(currentLine);
- }
-
-
-
- public void ClearRightLines()
- {
- programView.RemoveAllRightLines();
- rightPolyLines = new Dictionary<int, Shape>();
-
- UpdateSimilarityScore(Double.NaN);
- }
-
-
-
- public void UpdateRightLines(List<Tuple<bool, InternalLine>> lines)
- {
- foreach (Tuple<bool, InternalLine> tup in lines)
- {
- var status = tup.Item1;
- var line = tup.Item2;
- if (!rightPolyLines.ContainsKey(line.GetID()))
- {
- if (!line.isPoint)
- {
- Polyline newLine = new Polyline();
- newLine.Points = line.GetPointCollection();
- rightPolyLines.Add(line.GetID(), newLine);
- programView.AddNewLineRight(newLine);
- }
- else
- {
- Ellipse newPoint = new Ellipse();
- rightPolyLines.Add(line.GetID(), newPoint);
- programView.AddNewPointRight(newPoint, line);
- }
- }
- SetVisibility(rightPolyLines[line.GetID()], status);
- }
-
- UpdateSimilarityScore(Double.NaN); var templist = lines.Where(tup => tup.Item1).ToList();
- if (LeftLines.Count > 0)
- {
- for (int i = 0; i < LeftLines.Count; i++)
- {
- if (templist.Count == i) break;
- UpdateSimilarityScore(GeometryCalculator.CalculateSimilarity(templist[i].Item2, LeftLines[i]));
- }
- }
- else if (templist.Count > 1)
- {
- UpdateSimilarityScore(GeometryCalculator.CalculateSimilarity(templist[templist.Count - 2].Item2, templist[templist.Count - 1].Item2));
- }
- }
-
-
-
- public void UpdateLeftLines(List<InternalLine> lines)
- {
- programView.RemoveAllLeftLines();
- foreach (InternalLine line in lines)
- {
- Polyline newLine = new Polyline();
- newLine.Stroke = Brushes.Black;
- newLine.Points = line.GetPointCollection();
- programView.AddNewLineLeft(newLine);
- }
- programView.SetCanvasState("LeftCanvas", true);
- programView.SetCanvasState("RightCanvas", true);
- LeftLines = lines;
- }
-
-
-
-
-
-
-
-
-
-
-
- public void UpdateUIState(bool inDrawingMode, bool canUndo, bool canRedo, bool canvasActive,
- bool graphicLoaded, bool optiTrackAvailable, bool optiTrackInUse)
- {
- Dictionary<String, MainWindow.ButtonState> dict = new Dictionary<String, MainWindow.ButtonState> {
- {"canvasButton", MainWindow.ButtonState.Enabled }, {"drawButton", MainWindow.ButtonState.Disabled}, {"deleteButton",MainWindow.ButtonState.Disabled },
- {"undoButton", MainWindow.ButtonState.Disabled },{"redoButton", MainWindow.ButtonState.Disabled}, {"drawWithOptiButton", MainWindow.ButtonState.Disabled}};
- if (canvasActive)
- {
- if (inDrawingMode)
- {
- if (optiTrackAvailable)
- {
- if (optiTrackInUse)
- {
- dict["drawButton"] = MainWindow.ButtonState.Enabled;
- dict["drawWithOptiButton"] = MainWindow.ButtonState.Active;
- dict["deleteButton"] = MainWindow.ButtonState.Enabled;
- }
- else
- {
- dict["drawButton"] = MainWindow.ButtonState.Active;
- dict["drawWithOptiButton"] = MainWindow.ButtonState.Enabled;
- dict["deleteButton"] = MainWindow.ButtonState.Enabled;
- }
- }
- else
- {
- dict["drawButton"] = MainWindow.ButtonState.Active;
- dict["deleteButton"] = MainWindow.ButtonState.Enabled;
- }
- }
- else
- {
- dict["drawButton"] = MainWindow.ButtonState.Enabled;
- dict["deleteButton"] = MainWindow.ButtonState.Active;
- if (optiTrackAvailable)
- {
- dict["drawWithOptiButton"] = MainWindow.ButtonState.Enabled;
- }
- }
- if (canUndo) { dict["undoButton"] = MainWindow.ButtonState.Enabled; }
- if (canRedo) { dict["redoButton"] = MainWindow.ButtonState.Enabled; }
- }
- foreach (KeyValuePair<String, MainWindow.ButtonState> entry in dict)
- {
- programView.SetToolStripButtonStatus(entry.Key, entry.Value);
- }
- programView.SetCanvasState("RightCanvas", canvasActive);
- programView.SetCanvasState("LeftCanvas", graphicLoaded);
- }
-
-
-
-
- public void PassMessageToView(String msg)
- {
- programView.ShowInfoMessage(msg);
- }
-
-
-
-
-
- public bool PassWarning(String msg)
- {
- return programView.ShowWarning(msg);
- }
-
-
-
-
- public void PassLastActionTaken(String msg)
- {
- programView.SetLastActionTakenText(msg);
- }
-
-
-
-
- public bool IsMousePressed()
- {
- return programView.IsMousePressed();
- }
- public void PassOptiTrackMessage(String stringToPass)
- {
- programView.SetOptiTrackText(stringToPass);
-
- }
-
-
-
-
-
- public void UpdateSimilarityScore(double score)
- {
- if (Double.IsNaN(score))
- {
- ImageSimilarity.Clear();
- programView.SetImageSimilarityText("");
- }
- else
- {
- if (score >= 0 && score <= 1) ImageSimilarity.Add(score);
- programView.SetImageSimilarityText((ImageSimilarity.Sum() / ImageSimilarity.Count).ToString());
- }
- }
-
-
-
-
-
-
- public void SetOverlayStatus(String name, bool visible, Point position)
- {
- Shape shape = new Ellipse(); double visibility = 1; double xDif = 0; double yDif = 0;
- Point point = ConvertRightCanvasCoordinateToOverlay(position);
-
- switch (name)
- {
- case "optipoint":
- shape = ((MainWindow)programView).OverlayDictionary["optipoint"];
- visibility = 0.5; xDif = 2.5; yDif = 2.5; point = position;
- break;
- case "startpoint":
- shape = ((MainWindow)programView).OverlayDictionary["startpoint"];
- xDif = ((MainWindow)programView).markerRadius; yDif = xDif;
- break;
- case "endpoint":
- shape = ((MainWindow)programView).OverlayDictionary["endpoint"];
- xDif = ((MainWindow)programView).markerRadius; yDif = xDif;
- break;
- default:
- Console.WriteLine("Unknown Overlay Item. Please check in MVP_Presenter if this item exists.");
- return;
- }
- if (visible) shape.Opacity = visibility;
- else shape.Opacity = 0.00001;
- shape.Margin = new Thickness(point.X - xDif, point.Y - yDif, 0, 0);
- }
-
-
-
-
-
-
-
- public void SetOverlayStatus(String name, bool visible, Point pos1, Point pos2)
- {
- Shape shape = new Ellipse();
- switch (name.Substring(0, 7))
- {
- case "dotLine":
- if (((MainWindow)programView).OverlayDictionary.ContainsKey(name))
- {
- shape = ((MainWindow)programView).OverlayDictionary[name];
- break;
- }
- goto default;
- default:
- SetOverlayStatus(name, visible, pos1);
- return;
- }
- if (visible) shape.Opacity = 1;
- else shape.Opacity = 0.00001;
- Point p1 = ConvertRightCanvasCoordinateToOverlay(pos1); Point p2 = ConvertRightCanvasCoordinateToOverlay(pos2);
- ((Line)shape).X1 = p1.X; ((Line)shape).X2 = p2.X; ((Line)shape).Y1 = p1.Y; ((Line)shape).Y2 = p2.Y;
- }
-
-
-
-
- public void MoveOptiPoint(Point position)
- {
- Point point = ConvertRightCanvasCoordinateToOverlay(position);
- Shape shape = ((MainWindow)programView).OverlayDictionary["optipoint"];
- shape.Margin = new Thickness(position.X - 2.5, position.Y - 2.5, 0, 0);
- }
-
-
-
-
-
-
-
-
- private Point ConvertRightCanvasCoordinateToOverlay(Point p)
- {
- MainWindow main = (MainWindow)programView;
- double xDif = (main.CanvasLeftEdge.ActualWidth + main.LeftCanvas.ActualWidth + main.CanvasSeperator.ActualWidth);
- double yDif = (main.ButtonToolbar.ActualHeight);
- return new Point(p.X + xDif, p.Y + yDif);
- }
-
-
-
-
-
- private void SetVisibility(Shape line, bool visible)
- {
- if (!visible)
- {
- line.Opacity = 0.00001;
- }
- else
- {
- line.Opacity = 1;
- }
- }
- }
- }
|