Skip to content

Commit

Permalink
Fixes #409
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Oct 23, 2023
1 parent cd33a0a commit f41e993
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 25 deletions.
10 changes: 10 additions & 0 deletions src/Myra/Graphics2D/UI/Containers/SingleItemContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ protected internal virtual T InternalChild
}
}

protected override void InternalArrange()
{
base.InternalArrange();

if (InternalChild != null && InternalChild.Visible)
{
InternalChild.Arrange(ActualBounds);
}
}

protected override Point InternalMeasure(Point availableSize)
{
var result = Mathematics.PointZero;
Expand Down
61 changes: 38 additions & 23 deletions src/Myra/Graphics2D/UI/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ namespace Myra.Graphics2D.UI
{
public class TextBox : Widget
{
private const int CursorUpdateDelayInMs = 30;

private DateTime _lastCursorUpdate;
private DateTime _lastBlinkStamp = DateTime.Now;
private bool _cursorOn = true;
private bool _wrap = false;
Expand Down Expand Up @@ -328,18 +331,19 @@ internal set
if (Desktop != null)
{
Desktop.TouchUp -= DesktopTouchUp;
Desktop.TouchDown -= DesktopTouchDown;
}

base.Desktop = value;

if (Desktop != null)
{
Desktop.TouchUp += DesktopTouchUp;
Desktop.TouchDown += DesktopTouchDown;
}
}
}


/// <summary>
/// Fires when the value is about to be changed
/// Set Cancel to true if you want to cancel the change
Expand Down Expand Up @@ -1164,10 +1168,25 @@ public override void OnChar(char c)

private void SetCursorByTouch()
{
if (Desktop == null)
{
return;
}

var mousePos = ToLocal(Desktop.TouchPosition);
mousePos.X += _internalScrolling.X;
mousePos.Y += _internalScrolling.Y;

if (mousePos.X < 0)
{
mousePos.X = 0;
}

if (mousePos.Y < 0)
{
mousePos.Y = 0;
}

var line = _richTextLayout.GetLineByY(mousePos.Y);
if (line != null)
{
Expand All @@ -1186,33 +1205,24 @@ private void SetCursorByTouch()
}
}

public override bool OnTouchDown()
private void DesktopTouchUp(object sender, EventArgs args)
{
base.OnTouchDown();

if (!Enabled)
{
return false;
}
_isTouchDown = false;
}

if (Length == 0)
private void DesktopTouchDown(object sender, EventArgs e)
{
if (!Enabled || !IsTouchInside || Length == 0)
{
return false;
return;
}

SetCursorByTouch();

_lastCursorUpdate = DateTime.Now;
_isTouchDown = true;

return true;
}

public override void OnTouchMoved()
{
base.OnTouchMoved();

SetCursorByTouch();
}

public override void OnTouchDoubleClick()
{
Expand Down Expand Up @@ -1398,6 +1408,16 @@ public override void InternalRender(RenderContext context)
return;
}

if (_isTouchDown)
{
var passed = DateTime.Now - _lastCursorUpdate;
if (passed.TotalMilliseconds > CursorUpdateDelayInMs)
{
SetCursorByTouch();
_lastCursorUpdate = DateTime.Now;
}
}

var bounds = ActualBounds;
RenderSelection(context);

Expand Down Expand Up @@ -1540,10 +1560,5 @@ public float GetWidth(int index)

return glyph.Value.Bounds.Width;
}

private void DesktopTouchUp(object sender, EventArgs args)
{
_isTouchDown = false;
}
}
}
4 changes: 2 additions & 2 deletions src/Myra/Graphics2D/UI/Widget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ protected virtual void InternalSetStyle(Stylesheet stylesheet, string name)

public virtual void OnMouseLeft()
{
ChildrenCopy.ProcessMouseMovement();
ChildrenCopy.ProcessMouseLeft();

MouseLeft.Invoke(this);
}
Expand Down Expand Up @@ -1338,7 +1338,7 @@ public virtual void OnMouseWheel(float delta)
public virtual void OnTouchLeft()
{
IsTouchInside = false;
ChildrenCopy.ProcessTouchMovement();
ChildrenCopy.ProcessTouchLeft();

TouchLeft.Invoke(this);
}
Expand Down
24 changes: 24 additions & 0 deletions src/Myra/Utility/InputHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public static void ProcessTouchDoubleClick(this List<Widget> widgets)
}
}

public static void ProcessMouseLeft(this List<Widget> widgets)
{
for (var i = widgets.Count - 1; i >= 0; --i)
{
var w = widgets[i];
if (w.IsMouseInside)
{
w.IsMouseInside = false;
}
}
}

public static void ProcessMouseMovement(this List<Widget> widgets)
{
// First run: call on OnMouseLeft on all widgets if it is required
Expand Down Expand Up @@ -154,6 +166,18 @@ public static void ProcessMouseMovement(this List<Widget> widgets)
}
}

public static void ProcessTouchLeft(this List<Widget> widgets)
{
for (var i = widgets.Count - 1; i >= 0; --i)
{
var w = widgets[i];
if (w.IsTouchInside)
{
w.OnTouchLeft();
}
}
}

public static void ProcessTouchMovement(this List<Widget> widgets)
{
// First run: call on OnTouchLeft on all widgets if it is required
Expand Down

0 comments on commit f41e993

Please sign in to comment.