private TextBlock CreateTextLabel(IsolineTextLabel textLabel) { var transform = Plotter2D.Viewport.Transform; Point screenPos = textLabel.Position.DataToScreen(transform); double angle = textLabel.Rotation; Debug.WriteLine(angle); if (angle < 0) { angle += 360; } if (135 < angle && angle < 225) { angle -= 180; } TextBlock res = new TextBlock { Text = textLabel.Text, RenderTransform = new RotateTransform(angle), Tag = textLabel, RenderTransformOrigin = new Point(0.5, 0.5) }; res.Measure(SizeHelper.CreateInfiniteSize()); Size textSize = res.DesiredSize; Point position = new Point(screenPos.X - textSize.Width / 2, screenPos.Y - textSize.Height / 2); Canvas.SetLeft(res, position.X); Canvas.SetTop(res, position.Y); return(res); }
private FrameworkElement CreateTextLabel(IsolineTextLabel textLabel) { var transform = Plotter2D.Viewport.Transform; Point screenPos = textLabel.Position.DataToScreen(transform); double angle = textLabel.Rotation; if (angle < 0) { angle += 360; } if (135 < angle && angle < 225) { angle -= 180; } string tooltip = textLabel.Value.ToString("F"); //String.Format("{0} at ({1}, {2})", textLabel.Text, textLabel.Position.X, textLabel.Position.Y); Grid grid = new Grid { RenderTransform = new RotateTransform(angle), Tag = textLabel, RenderTransformOrigin = new Point(0.5, 0.5), ToolTip = tooltip }; TextBlock res = new TextBlock { Text = textLabel.Value.ToString("F"), Margin = new Thickness(3, 1, 3, 1) }; //res.Measure(SizeHelper.CreateInfiniteSize()); Rectangle rect = new Rectangle { Stroke = Brushes.Gray, Fill = labelBackground, RadiusX = 8, RadiusY = 8 }; grid.Children.Add(rect); grid.Children.Add(res); grid.Measure(SizeHelper.CreateInfiniteSize()); Size textSize = grid.DesiredSize; Point position = new Point(screenPos.X - textSize.Width / 2, screenPos.Y - textSize.Height / 2); Canvas.SetLeft(grid, position.X); Canvas.SetTop(grid, position.Y); return(grid); }
private void UpdateUIRepresentation() { foreach (var path in linePaths) { LevelLine line = (LevelLine)path.Tag; path.Data = CreateGeometry(line); } var transform = Plotter2D.Viewport.Transform; Rect output = Plotter2D.Viewport.Output; DataRect visible = Plotter2D.Viewport.Visible; if (rebuildText) { foreach (var text in textBlocks) { if (text.Visibility == Visibility.Visible) { content.Children.Remove(text); } } textBlocks.Clear(); double wayBeforeText = new Rect(new Size(100, 100)).ScreenToData(transform).Width; Annotater.WayBeforeText = wayBeforeText; var textLabels = Annotater.Annotate(Collection, Plotter2D.Viewport.Visible); foreach (var textLabel in textLabels) { TextBlock text = CreateTextLabel(textLabel); textBlocks.Add(text); if (visible.Contains(textLabel.Position)) { content.Children.Add(text); } else { text.Visibility = Visibility.Hidden; } } } else { foreach (var text in textBlocks) { IsolineTextLabel label = (IsolineTextLabel)text.Tag; Point screenPos = label.Position.DataToScreen(transform); Size textSize = text.DesiredSize; Point position = new Point(screenPos.X - textSize.Width / 2, screenPos.Y - textSize.Height / 2); if (output.Contains(position)) { Canvas.SetLeft(text, position.X); Canvas.SetTop(text, position.Y); if (text.Visibility == Visibility.Hidden) { text.Visibility = Visibility.Visible; content.Children.Add(text); } } else if (text.Visibility == Visibility.Visible) { text.Visibility = Visibility.Hidden; content.Children.Remove(text); } } } }