Skip to content

Commit

Permalink
[SmartScript] Holding control while dragging event/action/condition w…
Browse files Browse the repository at this point in the history
…ill duplicate them now
  • Loading branch information
BAndysc committed Aug 4, 2021
1 parent 4a36600 commit dd20c30
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 202 deletions.
8 changes: 3 additions & 5 deletions WDE.Conditions/Exporter/ConditionsSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class ConditionsSerializer
private static readonly string ConditionSql =
@"({SourceTypeOrReferenceId}, {SourceGroup}, {SourceEntry}, {SourceId}, {ElseGroup}, {ConditionTypeOrReference}, {ConditionTarget}, {ConditionValue1}, {ConditionValue2}, {ConditionValue3}, {NegativeCondition}, {Comment})";

public static object ToSqlObject(this IConditionLine line)
public static object ToSqlObject(this IConditionLine line, bool escapeComment = false)
{
return new
{
Expand All @@ -28,15 +28,13 @@ public static object ToSqlObject(this IConditionLine line)
ConditionValue2 = line.ConditionValue2,
ConditionValue3 = line.ConditionValue3,
NegativeCondition = line.NegativeCondition,
Comment = line.Comment,
Comment = escapeComment ? line.Comment?.ToSqlEscapeString() : line.Comment,
};
}

public static string ToSqlString(this IConditionLine line)
{
var obj = line.ToSqlObject();
((dynamic)obj).Comment = ((string)((dynamic)obj).Comment).ToSqlEscapeString();
return Smart.Format(ConditionSql, obj);
return Smart.Format(ConditionSql, line.ToSqlObject(true));
}

public static bool TryToConditionLine(this string str, out IConditionLine line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@ namespace WDE.SmartScriptEditor.Avalonia.Editor.UserControls
/// </summary>
public class GlobalVariableView : SelectableTemplatedControl
{
public static AvaloniaProperty DeselectAllRequestProperty =
AvaloniaProperty.Register<GlobalVariableView, ICommand>(nameof(DeselectAllRequest));

public static AvaloniaProperty DeselectAllButGlobalVariablesRequestProperty =
AvaloniaProperty.Register<GlobalVariableView, ICommand>(nameof(DeselectAllButGlobalVariablesRequest));

public static AvaloniaProperty EditGlobalVariableCommandProperty =
AvaloniaProperty.Register<GlobalVariableView, ICommand>(nameof(EditGlobalVariableCommand));

public ICommand DeselectAllRequest
{
get => (ICommand) GetValue(DeselectAllRequestProperty);
set => SetValue(DeselectAllRequestProperty, value);
}

public ICommand DeselectAllButGlobalVariablesRequest
{
get => (ICommand) GetValue(DeselectAllButGlobalVariablesRequestProperty);
Expand All @@ -36,22 +27,9 @@ public ICommand EditGlobalVariableCommand
set => SetValue(EditGlobalVariableCommandProperty, value);
}

protected override void OnPointerPressed(PointerPressedEventArgs e)
protected override void DeselectOthers()
{
base.OnPointerPressed(e);

if (e.ClickCount == 1)
{
DeselectAllButGlobalVariablesRequest?.Execute(null);

if (!IsSelected)
{
if (!e.KeyModifiers.HasFlag(MultiselectGesture))
DeselectAllRequest?.Execute(null);
IsSelected = true;
}
e.Handled = true;
}
DeselectAllButGlobalVariablesRequest?.Execute(null);
}

protected override void OnEdit()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
Expand All @@ -7,11 +9,19 @@

namespace WDE.SmartScriptEditor.Avalonia.Editor.UserControls
{
public class SelectableTemplatedControl : TemplatedControl
public abstract class SelectableTemplatedControl : TemplatedControl
{
public static KeyModifiers MultiselectGesture { get; } = AvaloniaLocator.Current
.GetService<PlatformHotkeyConfiguration>()?.CommandModifiers ?? KeyModifiers.Control;

public static readonly AvaloniaProperty DeselectAllRequestProperty =
AvaloniaProperty.Register<SelectableTemplatedControl, ICommand>(nameof(DeselectAllRequest));

public ICommand DeselectAllRequest
{
get => (ICommand) GetValue(DeselectAllRequestProperty);
set => SetValue(DeselectAllRequestProperty, value);
}
public static readonly DirectProperty<SelectableTemplatedControl, bool> IsSelectedProperty =
AvaloniaProperty.RegisterDirect<SelectableTemplatedControl, bool>(
nameof(IsSelected),
Expand All @@ -25,16 +35,43 @@ public bool IsSelected
set => SetAndRaise(IsSelectedProperty, ref isSelected, value);
}

private bool IsMultiSelect(KeyModifiers modifiers)
{
return modifiers.HasFlag(MultiselectGesture);
}

private ulong lastPressedTimestamp = 0;
private int lastClickCount = 0;
private bool lastPressedWithControlOn = false;
private Point pressPosition;
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);

lastPressedTimestamp = e.Timestamp;
lastClickCount = e.ClickCount;
lastPressedWithControlOn = IsMultiSelect(e.KeyModifiers);
pressPosition = e.GetPosition(this);

if (e.ClickCount == 1)
{
if (e.Source is FormattedTextBlock tb && tb.OverContext != null)
return;

if (!IsSelected || !lastPressedWithControlOn)
DeselectOthers();

if (!lastPressedWithControlOn && !IsSelected)
{
DeselectAllRequest?.Execute(null);
IsSelected = true;
}
e.Handled = true;
}
}

protected abstract void DeselectOthers();

protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
Expand All @@ -45,6 +82,20 @@ protected override void OnPointerReleased(PointerReleasedEventArgs e)
OnDirectEdit(tb.OverContext);
e.Handled = true;
}
else
{
if (lastPressedWithControlOn)
{
var vector = pressPosition - e.GetPosition(this);
var dist = Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y);
if (dist < 5)
{
DeselectOthers();
IsSelected = !IsSelected;
e.Handled = true;
}
}
}
}
if (lastClickCount == 2 && (e.Timestamp - lastPressedTimestamp) <= 1000)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ namespace WDE.SmartScriptEditor.Avalonia.Editor.UserControls
/// </summary>
public class SmartActionView : SelectableTemplatedControl
{
public static AvaloniaProperty DeselectAllRequestProperty =
AvaloniaProperty.Register<SmartActionView, ICommand>(nameof(DeselectAllRequest));

public static AvaloniaProperty DeselectAllButActionsRequestProperty =
AvaloniaProperty.Register<SmartActionView, ICommand>(nameof(DeselectAllButActionsRequest));

Expand All @@ -28,12 +25,6 @@ public class SmartActionView : SelectableTemplatedControl
public static readonly DirectProperty<SmartActionView, int> IndentProperty
= AvaloniaProperty.RegisterDirect<SmartActionView, int>("Indent", o => o.Indent, (o, v) => o.Indent = v);

public ICommand DeselectAllRequest
{
get => (ICommand) GetValue(DeselectAllRequestProperty);
set => SetValue(DeselectAllRequestProperty, value);
}

public ICommand DeselectAllButActionsRequest
{
get => (ICommand) GetValue(DeselectAllButActionsRequestProperty);
Expand All @@ -57,28 +48,10 @@ public int Indent
get { return indent; }
set { SetAndRaise(IndentProperty, ref indent, value); }
}

protected override void OnPointerPressed(PointerPressedEventArgs e)
protected override void DeselectOthers()
{
base.OnPointerPressed(e);

if (e.ClickCount == 1)
{
if (e.Source is FormattedTextBlock tb && tb.OverContext != null)
{
return;
}

DeselectAllButActionsRequest?.Execute(null);

if (!IsSelected)
{
if (!e.KeyModifiers.HasFlag(MultiselectGesture))
DeselectAllRequest?.Execute(null);
IsSelected = true;
}
e.Handled = true;
}
DeselectAllButActionsRequest?.Execute(null);
}

protected override void OnDirectEdit(object context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ namespace WDE.SmartScriptEditor.Avalonia.Editor.UserControls
/// </summary>
public class SmartConditionView : SelectableTemplatedControl
{
public static readonly AvaloniaProperty DeselectAllRequestProperty =
AvaloniaProperty.Register<SmartActionView, ICommand>(nameof(DeselectAllRequest));

public static readonly AvaloniaProperty DeselectAllButConditionsRequestProperty =
AvaloniaProperty.Register<SmartActionView, ICommand>(nameof(DeselectAllButConditionsRequest));

Expand All @@ -22,11 +19,6 @@ public class SmartConditionView : SelectableTemplatedControl
public static readonly AvaloniaProperty DirectEditParameterProperty =
AvaloniaProperty.Register<SmartActionView, ICommand>(nameof(DirectEditParameter));

public ICommand DeselectAllRequest
{
get => (ICommand) GetValue(DeselectAllRequestProperty);
set => SetValue(DeselectAllRequestProperty, value);
}

public ICommand DirectEditParameter
{
Expand Down Expand Up @@ -56,27 +48,9 @@ protected override void OnDirectEdit(object context)
DirectEditParameter?.Execute(context);
}

protected override void OnPointerPressed(PointerPressedEventArgs e)
protected override void DeselectOthers()
{
base.OnPointerPressed(e);

if (e.ClickCount == 1)
{
if (e.Source is FormattedTextBlock tb && tb.OverContext != null)
{
return;
}

DeselectAllButConditionsRequest?.Execute(null);

if (!IsSelected)
{
if (!e.KeyModifiers.HasFlag(MultiselectGesture))
DeselectAllRequest?.Execute(null);
IsSelected = true;
}
e.Handled = true;
}
DeselectAllButConditionsRequest?.Execute(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ public class SmartEventView : SelectableTemplatedControl
o => o.EditEventCommand,
(o, v) => o.EditEventCommand = v);

public static readonly DirectProperty<SmartEventView, ICommand?> DeselectAllRequestProperty =
AvaloniaProperty.RegisterDirect<SmartEventView, ICommand?>(
nameof(DeselectAllRequest),
o => o.DeselectAllRequest,
(o, v) => o.DeselectAllRequest = v);

public static readonly DirectProperty<SmartEventView, ICommand?> DeselectActionsOfDeselectedEventsRequestProperty =
AvaloniaProperty.RegisterDirect<SmartEventView, ICommand?>(
nameof(DeselectActionsOfDeselectedEventsRequest),
Expand All @@ -42,14 +36,7 @@ public ICommand? EditEventCommand
get => editEventCommand;
set => SetAndRaise(EditEventCommandProperty, ref editEventCommand, value);
}

private ICommand? deselectAllRequest;
public ICommand? DeselectAllRequest
{
get => deselectAllRequest;
set => SetAndRaise(DeselectAllRequestProperty, ref deselectAllRequest, value);
}


private ICommand? deselectActionsOfDeselectedEventsRequest;
public ICommand? DeselectActionsOfDeselectedEventsRequest
{
Expand All @@ -74,28 +61,9 @@ protected override void OnDirectEdit(object context)
DirectEditParameter?.Execute(context);
}

protected override void OnPointerPressed(PointerPressedEventArgs e)
protected override void DeselectOthers()
{
base.OnPointerPressed(e);

if (e.ClickCount == 1)
{
if (e.Source is FormattedTextBlock tb && tb.OverContext != null)
{
return;
}

DeselectActionsOfDeselectedEventsRequest?.Execute(null);

if (!GetSelected(this))
{
if (!e.KeyModifiers.HasFlag(MultiselectGesture))
DeselectAllRequest?.Execute(null);
SetSelected(this, true);
}

e.Handled = true;
}
DeselectActionsOfDeselectedEventsRequest?.Execute(null);
}

public static readonly AvaloniaProperty SelectedProperty = AvaloniaProperty.RegisterAttached<SmartEventView, IControl, bool>("Selected");
Expand Down
Loading

0 comments on commit dd20c30

Please sign in to comment.