Skip to content

Commit

Permalink
Merge pull request #45 from TorisanKitsune/FixZoom
Browse files Browse the repository at this point in the history
Fix zoom
  • Loading branch information
mkromis committed Oct 28, 2019
2 parents 977bb1a + 7801f7c commit 41ee77e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 39 deletions.
@@ -1,5 +1,6 @@
using MinoriDemo.Core.Modules.VirtualCanvas.Models;
using MinoriEditorShell.Services;
using MinoriEditorShell.VirtualCanvas.Extensions;
using MinoriEditorShell.VirtualCanvas.Services;
using MvvmCross;
using MvvmCross.Commands;
Expand Down Expand Up @@ -81,7 +82,7 @@ public class VirtualCanvasViewModel : MesDocument, IMesVirtualCanvas
{
if (x == "Fit")
{
ResetZoom();
this.ZoomToContent(ZoomToContent.WidthAndHeight);
}
else
{
Expand All @@ -91,15 +92,6 @@ public class VirtualCanvasViewModel : MesDocument, IMesVirtualCanvas
}
});

private void ResetZoom()
{
Double scaleX = Graph.ViewportWidth / Graph.ExtentWidth;
Double scaleY = Graph.ViewportHeight / Graph.ExtentHeight;

Zoom.Value = Math.Min(scaleX, scaleY);
//zoom.Offset = new Point(0, 0);
}

public Double ZoomValue
{
get => Zoom?.Value ?? 0;
Expand Down Expand Up @@ -127,7 +119,10 @@ public override void ViewAppeared()
_statusbar.Text = $"Zoom:{e}";
};

RectZoom.ZoomReset += (s, e) => ResetZoom();
RectZoom.ZoomReset += (s, e) => this.ZoomToContent(ZoomToContent.WidthAndHeight);

// Set the background to see the boarder vs demo.
Graph.ContentCanvas.SetCanvasBackgroundColor(Color.AliceBlue);

// Do I even need this?
//IVirtualCanvasControl graph = Canvas.Graph;
Expand Down
@@ -0,0 +1,73 @@
using MinoriEditorShell.VirtualCanvas.Services;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace MinoriEditorShell.VirtualCanvas.Extensions
{
/// <summary>
/// Sets the zoom property, or manual if user changed
/// </summary>
public enum ZoomToContent
{
/// <summary>
/// User definded size
/// </summary>
Manual = 0,
/// <summary>
/// request zoom by width
/// </summary>
Width,

/// <summary>
/// Request zoom by height
/// </summary>
Height,

/// <summary>
/// Reuqest to zoom by both
/// </summary>
WidthAndHeight
}

/// <summary>
/// Extensions for IMesVirtualCanvas used in place of inheritance.
/// </summary>
public static class IMesVirtualCanvasExtensions
{
/// <summary>
/// Zoom content to view. Must be done on IVirtualCanvas due to needing zoom and graph
/// </summary>
/// <param name="canvas"></param>
/// <param name="zoomToContent"></param>
public static void ZoomToContent(this IMesVirtualCanvas canvas, ZoomToContent zoomToContent)
{
Double? zoom = null;
switch (zoomToContent)
{
case Extensions.ZoomToContent.Manual:
zoom = null;
break;
case Extensions.ZoomToContent.Width:
zoom = canvas.Graph.ViewportWidth / canvas.Graph.Extent.Width;
break;
case Extensions.ZoomToContent.Height:
zoom = canvas.Graph.ViewportHeight / canvas.Graph.Extent.Width;
break;
case Extensions.ZoomToContent.WidthAndHeight:
Double scaleX = canvas.Graph.ViewportWidth / canvas.Graph.Extent.Width;
Double scaleY = canvas.Graph.ViewportHeight / canvas.Graph.Extent.Height;

zoom = Math.Min(scaleX, scaleY);
break;
}

if (zoom != null)
{
canvas.Zoom.Value = zoom.Value;
canvas.Zoom.ResetTranslate();
}
}
}
}
@@ -1,6 +1,7 @@
using MinoriEditorShell.VirtualCanvas.Services;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Controls;

Expand All @@ -11,5 +12,16 @@ namespace MinoriEditorShell.VirtualCanvas.Platforms.Wpf.Controls
/// </summary>
public class MesContentCanvas : Canvas, IMesContentCanvas
{
/// <summary>
/// Sets the background color of the canvas object
/// </summary>
/// <param name="color"></param>
public void SetCanvasBackgroundColor(Color color)
{
System.Windows.Media.Color newcolor =
System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
System.Windows.Media.Brush brush = new System.Windows.Media.SolidColorBrush(newcolor);
Background = brush;
}
}
}
}
Expand Up @@ -23,17 +23,6 @@

namespace MinoriEditorShell.VirtualCanvas.Platforms.Wpf.Controls
{
public class VisualChangeEventArgs : EventArgs
{
public Int32 Added { get; set; }
public Int32 Removed { get; set; }
public VisualChangeEventArgs(Int32 added, Int32 removed)
{
Added = added;
Removed = removed;
}
}

/// <summary>
/// VirtualCanvas dynamically figures out which children are visible and creates their visuals
/// and which children are no longer visible (due to scrolling or zooming) and destroys their
Expand Down Expand Up @@ -107,14 +96,6 @@ public void ShowQuadTree(Boolean drawing)
/// <param name="e">noop</param>
void OnChildrenCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) => RebuildVisuals();

/// <summary>
/// Get/Set the MapZoom object used for manipulating the scale and translation on this canvas.
/// </summary>
public IMesMapZoom Zoom {
get => _zoom;
set => _zoom = (MesMapZoom)value;
}

/// <summary>
/// Returns true if all Visuals have been created for the current scroll position
/// and there is no more idle processing needed.
Expand Down Expand Up @@ -434,7 +415,6 @@ void SetViewportSize(System.Windows.Size s)
private readonly MesContentCanvas _contentCanvas;
Int32 _added;
RectangleF _visible = RectangleF.Empty;
private MesMapZoom _zoom;

delegate Int32 QuantizedWorkHandler(Int32 quantum);

Expand Down Expand Up @@ -815,11 +795,12 @@ Int32 LazyGarbageCollectNodes(Int32 quantum)
/// <returns>The bounds that is actually visible.</returns>
public Rect MakeVisible(Visual visual, Rect rectangle)
{
if (Zoom != null && visual != this)
{
return _zoom.ScrollIntoView(visual as FrameworkElement);
}
return rectangle;
//if (_contentCanvas.Zoom != null && visual != this)
//{
// return _zoom.ScrollIntoView(visual as FrameworkElement);
//}
//return rectangle;
throw new NotImplementedException();
}

/// <summary>
Expand Down Expand Up @@ -912,6 +893,7 @@ public void SetVerticalOffset(Double offset)
public SizeF SmallScrollIncrement1 { get; set; } = new SizeF(10, 10);
public Int32 Removed { get; set; }


#endregion

/// <summary>
Expand Down
Expand Up @@ -399,7 +399,7 @@ void StopAnimations()
/// <summary>
/// Overload to assist in platform interface
/// </summary>
/// <param name=""></param>
/// <param name="rectf"></param>
public void ZoomToRect(System.Drawing.RectangleF rectf)
{
Rect r = new Rect(rectf.X, rectf.Y, rectf.Width, rectf.Height);
Expand Down
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace MinoriEditorShell.VirtualCanvas.Services
Expand All @@ -10,5 +11,10 @@ namespace MinoriEditorShell.VirtualCanvas.Services
/// </summary>
public interface IMesContentCanvas
{
/// <summary>
/// Sets the background color of the canvas object
/// </summary>
/// <param name="color"></param>
void SetCanvasBackgroundColor(Color color);
}
}
Expand Up @@ -8,5 +8,6 @@ public interface IMesMapZoom
event EventHandler<Double> ValueChanged;
Double Value { get; set; }
void ZoomToRect(RectangleF rectangle);
void ResetTranslate();
}
}
Expand Up @@ -4,16 +4,30 @@

namespace MinoriEditorShell.VirtualCanvas.Services
{
public class VisualChangeEventArgs : EventArgs
{
public Int32 Added { get; set; }
public Int32 Removed { get; set; }
public VisualChangeEventArgs(Int32 added, Int32 removed)
{
Added = added;
Removed = removed;
}
}

public interface IMesVirtualCanvasControl
{
IMesContentCanvas ContentCanvas { get; }
IMesMapZoom Zoom { get; set; }
Double ExtentWidth { get; }
Double ExtentHeight { get; }
Double ViewportWidth { get; }
Double ViewportHeight { get; }

ObservableCollection<IMesVirtualChild> VirtualChildren { get; }
SizeF Extent { get; }
Int32 LiveVisualCount { get; }

event EventHandler<VisualChangeEventArgs> VisualsChanged;

void AddVirtualChild(IMesVirtualChild shape);
}
Expand Down

0 comments on commit 41ee77e

Please sign in to comment.