Skip to content

Commit

Permalink
Merge pull request #465 from TheJoeFin/Grab-Frame-Tweaks
Browse files Browse the repository at this point in the history
Grab frame tweaks
  • Loading branch information
TheJoeFin authored Jun 1, 2024
2 parents 8c2a022 + 1bac2ac commit f5c6ea5
Show file tree
Hide file tree
Showing 9 changed files with 603 additions and 213 deletions.
6 changes: 6 additions & 0 deletions Text-Grab/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@
<setting name="EtwUseMargins" serializeAs="String">
<value>False</value>
</setting>
<setting name="CloseFrameOnGrab" serializeAs="String">
<value>False</value>
</setting>
<setting name="GrabFrameScrollBehavior" serializeAs="String">
<value>Resize</value>
</setting>
</Text_Grab.Properties.Settings>
</userSettings>
</configuration>
5 changes: 5 additions & 0 deletions Text-Grab/Controls/WordBorder.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
HorizontalAlignment="Left"
Click="TryToAlphaMenuItem_Click"
Header="Try To Make _Letters" />
<MenuItem
x:Name="MakeSingleLineMenuItem"
HorizontalAlignment="Left"
Click="MakeSingleLineMenuItem_Click"
Header="Make Text _Single Line" />
<Separator />
<MenuItem
x:Name="MergeWordBordersMenuItem"
Expand Down
9 changes: 9 additions & 0 deletions Text-Grab/Controls/WordBorder.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ private void TryToNumberMenuItem_Click(object sender, RoutedEventArgs e)
OwnerGrabFrame?.UndoableWordChange(this, oldWord, true);
}

private void MakeSingleLineMenuItem_Click(object sender, RoutedEventArgs e)
{
string oldWord = Word;
Word = Word.MakeStringSingleLine();

OwnerGrabFrame?.UndoableWordChange(this, oldWord, true);
}

private void WordBorder_MouseEnter(object sender, RoutedEventArgs e)
{
if (OwnerGrabFrame?.isCtrlDown is true)
Expand All @@ -351,6 +359,7 @@ private void WordBorder_MouseLeave(object sender, RoutedEventArgs e)
{
MoveResizeBorder.Visibility = Visibility.Collapsed;
}

private void WordBorderControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (EditWordTextBox.Visibility == Visibility.Collapsed)
Expand Down
158 changes: 158 additions & 0 deletions Text-Grab/Controls/ZoomBorder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

// From StackOverFlow:
// https://stackoverflow.com/questions/741956/pan-zoom-image
// Answered by https://stackoverflow.com/users/282801/wies%c5%82aw-%c5%a0olt%c3%a9s
// Read on 2024-05-02
// Modified to match code style of this project

namespace Text_Grab.Controls;

public class ZoomBorder : Border
{
private UIElement? child = null;
private Point origin;
private Point start;

private TranslateTransform GetTranslateTransform(UIElement element) =>
(TranslateTransform)((TransformGroup)element.RenderTransform)
.Children.First(tr => tr is TranslateTransform);

private ScaleTransform GetScaleTransform(UIElement element) =>
(ScaleTransform)((TransformGroup)element.RenderTransform)
.Children.First(tr => tr is ScaleTransform);

public override UIElement Child
{
get { return base.Child; }
set
{
if (value != null && value != Child)
Initialize(value);
base.Child = value;
}
}

public bool CanPan { get; set; } = true;

public bool CanZoom { get; set; } = true;

public void Initialize(UIElement element)
{
child = element;
if (child is null)
return;

TransformGroup group = new();
ScaleTransform st = new();
group.Children.Add(st);
TranslateTransform tt = new();
group.Children.Add(tt);
child.RenderTransform = group;
child.RenderTransformOrigin = new Point(0.0, 0.0);
MouseWheel += Child_MouseWheel;
MouseLeftButtonDown += Child_MouseLeftButtonDown;
MouseLeftButtonUp += Child_MouseLeftButtonUp;
PreviewMouseDown += ZoomBorder_PreviewMouseDown;
MouseMove += Child_MouseMove;
PreviewMouseRightButtonDown += new MouseButtonEventHandler(
Child_PreviewMouseRightButtonDown);
}

private void ZoomBorder_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.MiddleButton == MouseButtonState.Pressed)
Reset();
}

public void Reset()
{
if (child is null)
return;

// reset zoom
ScaleTransform st = GetScaleTransform(child);
st.ScaleX = 1.0;
st.ScaleY = 1.0;

// reset pan
TranslateTransform tt = GetTranslateTransform(child);
tt.X = 0.0;
tt.Y = 0.0;
}

private void Child_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (child is null || !CanZoom)
return;

ScaleTransform st = GetScaleTransform(child);
TranslateTransform tt = GetTranslateTransform(child);

double zoom = e.Delta > 0 ? .2 : -.2;
if (!(e.Delta > 0) && (st.ScaleX < .4 || st.ScaleY < .4))
return;

Point relative = e.GetPosition(child);
double absoluteX;
double absoluteY;

absoluteX = relative.X * st.ScaleX + tt.X;
absoluteY = relative.Y * st.ScaleY + tt.Y;

st.ScaleX += zoom;
st.ScaleY += zoom;

tt.X = absoluteX - relative.X * st.ScaleX;
tt.Y = absoluteY - relative.Y * st.ScaleY;
}

private void Child_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (child is null)
return;

TranslateTransform tt = GetTranslateTransform(child);
start = e.GetPosition(this);
origin = new Point(tt.X, tt.Y);
Cursor = Cursors.Hand;
// child.CaptureMouse();
}

private void Child_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (child is null)
return;

child.ReleaseMouseCapture();
Cursor = Cursors.Arrow;
}

void Child_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
}

private void Child_MouseMove(object sender, MouseEventArgs e)
{
if (child is null
|| GetScaleTransform(child) is not ScaleTransform st
|| st.ScaleX == 1.0
|| Mouse.LeftButton == MouseButtonState.Released
|| !CanPan
|| KeyboardExtensions.IsShiftDown()
|| KeyboardExtensions.IsCtrlDown())
{
child?.ReleaseMouseCapture();
return;
}

TranslateTransform tt = GetTranslateTransform(child);
Vector v = start - e.GetPosition(this);
tt.X = origin.X - v.X;
tt.Y = origin.Y - v.Y;
}
}
7 changes: 7 additions & 0 deletions Text-Grab/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ public enum VirtualKeyCodes : short
LeftButton = 0x01,
RightButton = 0x02,
MiddleButton = 0x04
}

public enum ScrollBehavior
{
None = 0,
Resize = 1,
Zoom = 2,
}
24 changes: 24 additions & 0 deletions Text-Grab/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Text-Grab/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,11 @@
<Setting Name="EtwUseMargins" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="CloseFrameOnGrab" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="GrabFrameScrollBehavior" Type="System.String" Scope="User">
<Value Profile="(Default)">Resize</Value>
</Setting>
</Settings>
</SettingsFile>
Loading

0 comments on commit f5c6ea5

Please sign in to comment.