Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make scroll view touch friendly #229

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion samples/Myra.Samples.AllWidgets/AllWidgetsGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected override void LoadContent()
#endif

#if ANDROID
Desktop.WidgetGotKeyboardFocus += (s, a) =>
_desktop.WidgetGotKeyboardFocus += (s, a) =>
{
var asTextBox = a.Data as TextBox;
if (asTextBox == null)
Expand Down
18 changes: 9 additions & 9 deletions samples/Myra.Samples.NonModalWindows/UI/MainPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public MainPanel()
{
BuildUI();

_button1.PressedChanged += _button1_PressedChanged;
_button2.PressedChanged += _button2_PressedChanged;
_button3.PressedChanged += _button3_PressedChanged;
_button1.ToggledChanged += _button1_ToggledChanged;
_button2.ToggledChanged += _button2_ToggledChanged;
_button3.ToggledChanged += _button3_ToggledChanged;

_window1.Closed += (s, a) =>
{
Expand All @@ -40,9 +40,9 @@ public void ShowWindows()
_button3.IsPressed = true;
}

private void _button1_PressedChanged(object sender, System.EventArgs e)
private void _button1_ToggledChanged(object sender, System.EventArgs e)
{
if (_button1.IsPressed)
if (_button1.IsToggled)
{
_window1.Show(Desktop, new Point(100, 100));
}
Expand All @@ -52,9 +52,9 @@ private void _button1_PressedChanged(object sender, System.EventArgs e)
}
}

private void _button2_PressedChanged(object sender, System.EventArgs e)
private void _button2_ToggledChanged(object sender, System.EventArgs e)
{
if (_button2.IsPressed)
if (_button2.IsToggled)
{
_window2.Show(Desktop, new Point(400, 100));
}
Expand All @@ -64,9 +64,9 @@ private void _button2_PressedChanged(object sender, System.EventArgs e)
}
}

private void _button3_PressedChanged(object sender, System.EventArgs e)
private void _button3_ToggledChanged(object sender, System.EventArgs e)
{
if (_button3.IsPressed)
if (_button3.IsToggled)
{
_window3.Show(Desktop, new Point(200, 400));
}
Expand Down
99 changes: 41 additions & 58 deletions src/Myra/Graphics2D/UI/ButtonBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Myra.Graphics2D.UI
{
public class ButtonBase<T> : SingleItemContainer<T> where T : Widget
{
private bool _isPressed = false;
private bool _isToggled = false;

[Category("Appearance")]
[DefaultValue(HorizontalAlignment.Center)]
Expand All @@ -41,26 +41,32 @@ public virtual VerticalAlignment ContentVerticalAlignment

[Browsable(false)]
[XmlIgnore]
public bool IsPressed
public bool IsToggled
{
get
{
return _isPressed;
return _isToggled;
}

set
{
if (value == _isPressed)
if (value == _isToggled)
{
return;
}

_isPressed = value;
_isToggled = value;

OnPressedChanged();
OnToggledChanged();
}
}

/// <summary>
/// Indicates whether the current touch gesture started in this widget.
/// Similar to <see cref="Widget.TouchStayedInside"/> but stays <see langword="true"/> also if the gesture leaves the widget's bounds.
/// </summary>
public bool IsPressed { get; set; }

internal bool ReleaseOnTouchLeft;

public override Desktop Desktop
Expand Down Expand Up @@ -92,14 +98,13 @@ protected internal override void OnActiveChanged()
{
base.OnActiveChanged();

if (!Active && IsPressed && !Toggleable)
if (!Active && IsToggled && !Toggleable)
{
IsPressed = false;
IsToggled = false;
}
}

public event EventHandler Click;
public event EventHandler PressedChanged;
public event EventHandler ToggledChanged;

public ButtonBase()
{
Expand All @@ -113,68 +118,46 @@ public void DoClick()
OnTouchUp();
}

public virtual void OnPressedChanged()
public virtual void OnToggledChanged()
{
PressedChanged.Invoke(this);
ToggledChanged.Invoke(this);
}

public override void OnTouchLeft()
public override void OnTouchLeft(HookableEventArgs args)
{
base.OnTouchLeft();
base.OnTouchLeft(args);

if (ReleaseOnTouchLeft && !Toggleable)
{
IsPressed = false;
IsToggled = false;
}
}

public override void OnTouchUp()
public override void OnClick()
{
base.OnTouchUp();

if (!Enabled)
{
return;
}
base.OnClick();

var invokeClick = false;
if (!Toggleable)
{
invokeClick = IsPressed;
IsPressed = false;
}

if (invokeClick)
{
Click.Invoke(this);
}
if (Toggleable && CanChangeToggleable(!IsToggled))
{
IsToggled = !IsToggled;
}
}

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

if (!Enabled)
{
return;
}
IsPressed = true;
}

if (!Toggleable)
{
IsPressed = true;
}
else
{
var value = !IsPressed;
if (CanChangeToggleable(value))
{
IsPressed = value;
Click.Invoke(this);
}
}
}
public override void OnTouchUp()
{
base.OnTouchUp();

IsPressed = false;
}

protected virtual bool CanChangeToggleable(bool value)
protected virtual bool CanChangeToggleable(bool value)
{
return true;
}
Expand All @@ -192,7 +175,7 @@ public override void OnKeyDown(Keys k)
}
else
{
IsPressed = !IsPressed;
IsToggled = !IsToggled;
}
}
}
Expand All @@ -203,7 +186,7 @@ public override IBrush GetCurrentBackground()

if (Enabled)
{
if (IsPressed && PressedBackground != null)
if (IsToggled && PressedBackground != null)
{
result = PressedBackground;
}
Expand Down Expand Up @@ -232,7 +215,7 @@ public void ApplyButtonStyle(ButtonStyle style)

private void DesktopTouchUp(object sender, EventArgs args)
{
IsPressed = false;
IsToggled = false;
}

protected override void InternalSetStyle(Stylesheet stylesheet, string name)
Expand Down
12 changes: 6 additions & 6 deletions src/Myra/Graphics2D/UI/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public int? DropdownMaximumHeight
[XmlIgnore]
public bool IsExpanded
{
get { return InternalChild.IsPressed; }
get { return InternalChild.IsToggled; }
}

public override Desktop Desktop
Expand Down Expand Up @@ -94,7 +94,7 @@ public ComboBox(string styleName = Stylesheet.DefaultStyleName)
HorizontalAlignment = HorizontalAlignment.Stretch
};

InternalChild.PressedChanged += InternalChild_PressedChanged;
InternalChild.ToggledChanged += InternalChild_ToggledChanged;

_listBox._parentComboBox = this;
_listBox.Items.CollectionChanged += Items_CollectionChanged;
Expand All @@ -117,17 +117,17 @@ internal void HideDropdown()

private void DesktopOnContextMenuClosed(object sender, GenericEventArgs<Widget> genericEventArgs)
{
InternalChild.IsPressed = false;
InternalChild.IsToggled = false;
}

private void InternalChild_PressedChanged(object sender, EventArgs e)
private void InternalChild_ToggledChanged(object sender, EventArgs e)
{
if (_listBox.Items.Count == 0)
{
return;
}

if (InternalChild.IsPressed)
if (InternalChild.IsToggled)
{
if (_listBox.SelectedIndex == null && Items.Count > 0)
{
Expand Down Expand Up @@ -187,7 +187,7 @@ internal void UpdateSelectedItem()
{
InternalChild.Text = item.Text;
InternalChild.TextColor = item.Color ?? _listBox.ListBoxStyle.ListItemStyle.LabelStyle.TextColor;
((ImageTextButton)item.Widget).IsPressed = true;
((ImageTextButton)item.Widget).IsToggled = true;
}
else
{
Expand Down
18 changes: 9 additions & 9 deletions src/Myra/Graphics2D/UI/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,25 @@ public override void OnMouseMoved()
ChildrenCopy.ProcessMouseMovement();
}

public override void OnTouchEntered()
public override void OnTouchEntered(HookableEventArgs args)
{
base.OnTouchEntered();
base.OnTouchEntered(args);

ChildrenCopy.ProcessTouchMovement();
ChildrenCopy.ProcessTouchMovement(args);
}

public override void OnTouchLeft()
public override void OnTouchLeft(HookableEventArgs args)
{
base.OnTouchLeft();
base.OnTouchLeft(args);

ChildrenCopy.ProcessTouchMovement();
ChildrenCopy.ProcessTouchMovement(args);
}

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

ChildrenCopy.ProcessTouchMovement();
ChildrenCopy.ProcessTouchMovement(args);
}

public override void OnTouchDown()
Expand Down
34 changes: 17 additions & 17 deletions src/Myra/Graphics2D/UI/DebugOptionsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ public DebugOptionsWindow()

BuildUI();

_checkBoxWidgetFrames.IsPressed = MyraEnvironment.DrawWidgetsFrames;
_checkBoxWidgetFrames.PressedChanged += (s, a) =>
_checkBoxWidgetFrames.IsToggled = MyraEnvironment.DrawWidgetsFrames;
_checkBoxWidgetFrames.ToggledChanged += (s, a) =>
{
MyraEnvironment.DrawWidgetsFrames = _checkBoxWidgetFrames.IsPressed;
MyraEnvironment.DrawWidgetsFrames = _checkBoxWidgetFrames.IsToggled;
};

_checkBoxKeyboardFocusedWidgetFrame.IsPressed = MyraEnvironment.DrawKeyboardFocusedWidgetFrame;
_checkBoxKeyboardFocusedWidgetFrame.PressedChanged += (s, a) =>
_checkBoxKeyboardFocusedWidgetFrame.IsToggled = MyraEnvironment.DrawKeyboardFocusedWidgetFrame;
_checkBoxKeyboardFocusedWidgetFrame.ToggledChanged += (s, a) =>
{
MyraEnvironment.DrawKeyboardFocusedWidgetFrame = _checkBoxKeyboardFocusedWidgetFrame.IsPressed;
MyraEnvironment.DrawKeyboardFocusedWidgetFrame = _checkBoxKeyboardFocusedWidgetFrame.IsToggled;
};

_checkBoxMouseWheelFocusedWidgetFrame.IsPressed = MyraEnvironment.DrawMouseWheelFocusedWidgetFrame;
_checkBoxMouseWheelFocusedWidgetFrame.PressedChanged += (s, a) =>
_checkBoxMouseWheelFocusedWidgetFrame.IsToggled = MyraEnvironment.DrawMouseWheelFocusedWidgetFrame;
_checkBoxMouseWheelFocusedWidgetFrame.ToggledChanged += (s, a) =>
{
MyraEnvironment.DrawMouseWheelFocusedWidgetFrame = _checkBoxMouseWheelFocusedWidgetFrame.IsPressed;
MyraEnvironment.DrawMouseWheelFocusedWidgetFrame = _checkBoxMouseWheelFocusedWidgetFrame.IsToggled;
};

_checkBoxGlyphFrames.IsPressed = MyraEnvironment.DrawTextGlyphsFrames;
_checkBoxGlyphFrames.PressedChanged += (s, a) =>
_checkBoxGlyphFrames.IsToggled = MyraEnvironment.DrawTextGlyphsFrames;
_checkBoxGlyphFrames.ToggledChanged += (s, a) =>
{
MyraEnvironment.DrawTextGlyphsFrames = _checkBoxGlyphFrames.IsPressed;
MyraEnvironment.DrawTextGlyphsFrames = _checkBoxGlyphFrames.IsToggled;
};

_checkBoxDisableClipping.IsPressed = MyraEnvironment.DisableClipping;
_checkBoxDisableClipping.PressedChanged += (s, a) =>
_checkBoxDisableClipping.IsToggled = MyraEnvironment.DisableClipping;
_checkBoxDisableClipping.ToggledChanged += (s, a) =>
{
MyraEnvironment.DisableClipping = _checkBoxDisableClipping.IsPressed;
MyraEnvironment.DisableClipping = _checkBoxDisableClipping.IsToggled;
};
}

Expand All @@ -58,9 +58,9 @@ public void AddOption(string text, Action onEnabled, Action onDisabled)
Visible = true
};

optionsCheckBox.PressedChanged += (s, a) =>
optionsCheckBox.ToggledChanged += (s, a) =>
{
if (optionsCheckBox.IsPressed)
if (optionsCheckBox.IsToggled)
{
onEnabled();
}
Expand Down
Loading