public LoopControl(MainWindow window) : base() { InitializeComponent(); Window = window; loopBody = new ExperimentGraph(); Canvas = new Canvas() { AllowDrop = true, ClipToBounds = true, Style = (Style)this.FindResource("CanvasStyle") }; Canvas.Drop += CanvasDrop; Canvas.MouseDown += Canvas_MouseDown; Canvas.MouseMove += Canvas_MouseMove; Canvas.MouseUp += Canvas_MouseUp; StartLoopNodeControl startNode = new StartLoopNodeControl() { Height = 30, Width = 30 }; Canvas.SetTop(startNode, 50); Canvas.SetLeft(startNode, 50); startNode.MouseLeftButtonDown += Window.node_MouseLeftButtonDown; startNode.MouseLeftButtonUp += Window.node_MouseLeftButtonUp; startNode.MouseMove += Window.node_MouseMove; startNode.LostMouseCapture += Window.node_LostMouseCapture; Canvas.Children.Add(startNode); loopBody.Nodes.Add(startNode); }
protected static void RenderToCanvas([NotNull] IConsoleBufferSource buffer, WpfCanvas linesPanel, WpfSize charSize) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } ConsoleColor currentForeColor = (ConsoleColor)int.MaxValue; ConsoleColor currentBackColor = (ConsoleColor)int.MaxValue; TextBlock text = null; for (int iy = 0; iy < buffer.Height; iy++) { ConsoleChar[] charsLine = buffer.GetLine(iy); for (int ix = 0; ix < buffer.Width; ix++) { ConsoleChar chr = charsLine[ix]; ConsoleColor foreColor = chr.ForegroundColor; ConsoleColor backColor = chr.BackgroundColor; if (text == null || foreColor != currentForeColor || backColor != currentBackColor) { currentForeColor = foreColor; currentBackColor = backColor; AppendTextBlockIfNeeded(); text = new TextBlock { Foreground = ConsoleBrushes[foreColor], Background = ConsoleBrushes[backColor], Height = charSize.Height, }; WpfCanvas.SetLeft(text, ix * charSize.Width); WpfCanvas.SetTop(text, iy * charSize.Height); } text.Text += chr.HasChar || chr.LineChar.IsEmpty() ? chr.PrintableChar : buffer.GetLineChar(ix, iy); } AppendTextBlockIfNeeded(); } AppendTextBlockIfNeeded(); void AppendTextBlockIfNeeded() { if (text == null) { return; } text.Width = text.Text.Length * charSize.Width; linesPanel.Children.Add(text); text = null; } }
public void Arrange() { if (Popup == null || PopupChild == null || Application.Current == null) { return; } UIElement u = Parent; if (Application.Current.CheckAccess() && Application.Current.Windows.Count > 0) { // TODO: USE THE CURRENT WINDOW INSTEAD! WALK THE TREE! u = Application.Current.Windows[0]; } while ((u as Window) == null && u != null) { u = VisualTreeHelper.GetParent(u) as UIElement; } Window w = u as Window; if (w == null) { return; } double rootWidth = w.ActualWidth; double rootHeight = w.ActualHeight; double popupContentWidth = PopupChild.ActualWidth; double popupContentHeight = PopupChild.ActualHeight; if (rootHeight == 0 || rootWidth == 0 || popupContentWidth == 0 || popupContentHeight == 0) { return; } double rootOffsetX = 0; double rootOffsetY = 0; double myControlHeight = Parent.ActualHeight; double myControlWidth = Parent.ActualWidth; // Use or come up with a maximum popup height. double popupMaxHeight = MaxDropDownHeight; if (double.IsInfinity(popupMaxHeight) || double.IsNaN(popupMaxHeight)) { popupMaxHeight = (rootHeight - myControlHeight) * 3 / 5; } popupContentWidth = Math.Min(popupContentWidth, rootWidth); popupContentHeight = Math.Min(popupContentHeight, popupMaxHeight); popupContentWidth = Math.Max(myControlWidth, popupContentWidth); // We prefer to align the popup box with the left edge of the // control, if it will fit. double popupX = rootOffsetX; if (rootWidth < popupX + popupContentWidth) { // Since it doesn't fit when strictly left aligned, we shift it // to the left until it does fit. popupX = rootWidth - popupContentWidth; popupX = Math.Max(0, popupX); } // We prefer to put the popup below the combobox if it will fit. bool below = true; double popupY = rootOffsetY + myControlHeight; if (rootHeight < popupY + popupContentHeight) { below = false; // It doesn't fit below the combobox, lets try putting it above // the combobox. popupY = rootOffsetY - popupContentHeight; if (popupY < 0) { // doesn't really fit below either. Now we just pick top // or bottom based on wich area is bigger. if (rootOffsetY < (rootHeight - myControlHeight) / 2) { below = true; popupY = rootOffsetY + myControlHeight; } else { below = false; popupY = rootOffsetY - popupContentHeight; } } } // Now that we have positioned the popup we may need to truncate // its size. popupMaxHeight = below ? Math.Min(rootHeight - popupY, popupMaxHeight) : Math.Min(rootOffsetY, popupMaxHeight); Popup.HorizontalOffset = 0; Popup.VerticalOffset = 0; PopupChild.MinWidth = myControlWidth; PopupChild.MaxWidth = rootWidth; PopupChild.MinHeight = 0; PopupChild.MaxHeight = Math.Max(0, popupMaxHeight); PopupChild.Width = popupContentWidth; // PopupChild.Height = popupContentHeight; PopupChild.HorizontalAlignment = HorizontalAlignment.Left; PopupChild.VerticalAlignment = VerticalAlignment.Top; // Set the top left corner for the actual drop down. Canvas.SetLeft(PopupChild, popupX - rootOffsetX); Canvas.SetTop(PopupChild, popupY - rootOffsetY); }
/// <summary> /// Initializes a new instance of the <see cref="GeoMap"/> class. /// </summary> public GeoMap() { Canvas = new Canvas(); Map = new Canvas(); Canvas.Children.Add(Map); Content = Canvas; Canvas.SetBinding(WidthProperty, new Binding { Path = new PropertyPath(ActualWidthProperty), Source = this }); Canvas.SetBinding(HeightProperty, new Binding { Path = new PropertyPath(ActualHeightProperty), Source = this }); Lands = new Dictionary<string, MapData>(); SetCurrentValue(DefaultLandFillProperty, new SolidColorBrush(Color.FromArgb(200,255,255,255))); SetCurrentValue(LandStrokeProperty, new SolidColorBrush(Color.FromArgb(30, 55,55, 55))); SetCurrentValue(LandStrokeThicknessProperty, 1.3d); SetCurrentValue(AnimationsSpeedProperty, TimeSpan.FromMilliseconds(500)); SetCurrentValue(BackgroundProperty, new SolidColorBrush(Color.FromArgb(150, 96, 125, 138))); SetCurrentValue(GradientStopCollectionProperty, new GradientStopCollection { new GradientStop(Color.FromArgb(100,2,119,188), 0d), new GradientStop(Color.FromRgb(2,119,188), 1d), }); SetCurrentValue(HeatMapProperty, new Dictionary<string, double>()); SetCurrentValue(GeoMapTooltipProperty, new DefaultGeoMapTooltip {Visibility = Visibility.Hidden}); Canvas.Children.Add(GeoMapTooltip); SizeChanged += (sender, e) => { Draw(); }; MouseWheel += (sender, e) => { if (!EnableZoomingAndPanning) return; e.Handled = true; var rt = Map.RenderTransform as ScaleTransform; var p = rt == null ? 1 : rt.ScaleX; p += e.Delta > 0 ? .05 : -.05; p = p < 1 ? 1 : p; var o = e.GetPosition(this); if (e.Delta > 0) Map.RenderTransformOrigin = new Point(o.X/ActualWidth,o.Y/ActualHeight); Map.RenderTransform = new ScaleTransform(p, p); }; MouseDown += (sender, e) => { if (!EnableZoomingAndPanning) return; DragOrigin = e.GetPosition(this); }; MouseUp += (sender, e) => { if (!EnableZoomingAndPanning) return; var end = e.GetPosition(this); var delta = new Point(DragOrigin.X - end.X, DragOrigin.Y - end.Y); var l = Canvas.GetLeft(Map) - delta.X; var t = Canvas.GetTop(Map) - delta.Y; if (DisableAnimations) { Canvas.SetLeft(Map, l); Canvas.SetTop(Map, t); } else { Map.BeginAnimation(Canvas.LeftProperty, new DoubleAnimation(l, AnimationsSpeed)); Map.BeginAnimation(Canvas.TopProperty, new DoubleAnimation(t, AnimationsSpeed)); } }; }
private void OnMouseMove(object sender, MouseEventArgs e) { if (mouseDown) { if (DateTime.UtcNow > lastScrollTime.AddMilliseconds(100)) { Point currentPointWin = Mouse.GetPosition(scrollViewer); if (currentPointWin.Y < 16) { scrollViewer.LineUp(); scrollViewer.UpdateLayout(); lastScrollTime = DateTime.UtcNow; } if (currentPointWin.Y > scrollViewer.ActualHeight - 16) { scrollViewer.LineDown(); scrollViewer.UpdateLayout(); lastScrollTime = DateTime.UtcNow; } } Point currentPoint = Mouse.GetPosition(content); double width = currentPoint.X - startPoint.X + 1; double height = currentPoint.Y - startPoint.Y + 1; double left = startPoint.X; double top = startPoint.Y; if (isFirstMove) { if (Math.Abs(width) <= SystemParameters.MinimumHorizontalDragDistance && Math.Abs(height) <= SystemParameters.MinimumVerticalDragDistance) { return; } isFirstMove = false; if (!SelectionMultiple.IsControlKeyDown) { if (!treeView.ClearSelectionByRectangle()) { EndAction(); return; } } } // Debug.WriteLine(string.Format("Drawing: {0};{1};{2};{3}",startPoint.X,startPoint.Y,width,height)); if (width < 1) { width = Math.Abs(width - 1) + 1; left = startPoint.X - width + 1; } if (height < 1) { height = Math.Abs(height - 1) + 1; top = startPoint.Y - height + 1; } border.Width = width; Canvas.SetLeft(border, left); border.Height = height; Canvas.SetTop(border, top); border.Visibility = Visibility.Visible; double right = left + width - 1; double bottom = top + height - 1; // Debug.WriteLine(string.Format("left:{1};right:{2};top:{3};bottom:{4}", null, left, right, top, bottom)); SelectionMultiple selection = (SelectionMultiple)treeView.Selection; bool foundFocusItem = false; foreach (var item in items) { FrameworkElement itemContent = (FrameworkElement)item.Template.FindName("headerBorder", item); Point p = itemContent.TransformToAncestor(content).Transform(new Point()); double itemLeft = p.X; double itemRight = p.X + itemContent.ActualWidth - 1; double itemTop = p.Y; double itemBottom = p.Y + itemContent.ActualHeight - 1; // Debug.WriteLine(string.Format("element:{0};itemleft:{1};itemright:{2};itemtop:{3};itembottom:{4}",item.DataContext,itemLeft,itemRight,itemTop,itemBottom)); // Compute the current input states for determining the new selection state of the item bool intersect = !(itemLeft > right || itemRight <left || itemTop> bottom || itemBottom < top); bool initialSelected = initialSelection != null && initialSelection.Contains(item.DataContext); bool ctrl = SelectionMultiple.IsControlKeyDown; // Decision matrix: // If the Ctrl key is pressed, each intersected item will be toggled from its initial selection. // Without the Ctrl key, each intersected item is selected, others are deselected. // // newSelected // ─────────┬─────────────────────── // │ intersect // │ 0 │ 1 // ├───────────┴─────────── // │ initial // │ 0 │ 1 │ 0 │ 1 // ─────────┼─────┼─────┼─────┼───── // ctrl 0 │ 0 │ 0 │ 1 │ 1 = intersect // ─────────┼─────┼─────┼─────┼───── // 1 │ 0 │ 1 │ 1 │ 0 = intersect XOR initial // bool newSelected = intersect ^ (initialSelected && ctrl); // The new selection state for this item has been determined. Apply it. if (newSelected) { // The item shall be selected if (!treeView.SelectedItems.Contains(item.DataContext)) { // The item is not currently selected. Try to select it. if (!selection.SelectByRectangle(item)) { if (selection.LastCancelAll) { EndAction(); return; } } } } else { // The item shall be deselected if (treeView.SelectedItems.Contains(item.DataContext)) { // The item is currently selected. Try to deselect it. if (!selection.DeselectByRectangle(item)) { if (selection.LastCancelAll) { EndAction(); return; } } } } // Always focus and bring into view the item under the mouse cursor if (!foundFocusItem && currentPoint.X >= itemLeft && currentPoint.X <= itemRight && currentPoint.Y >= itemTop && currentPoint.Y <= itemBottom) { FocusHelper.Focus(item, true); scrollViewer.UpdateLayout(); foundFocusItem = true; } } if (e != null) { e.Handled = true; } } }