Skip to content

Commit 76f414e

Browse files
committed
[SmartScripts] Small improvements
1 parent 178cab2 commit 76f414e

36 files changed

+632
-146
lines changed

WDE.Common.Avalonia/Controls/FastTreeView.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Specialized;
44
using Avalonia;
55
using Avalonia.Controls;
6+
using Avalonia.Data;
67
using Avalonia.Input;
78
using Avalonia.Media;
89
using Avalonia.VisualTree;
@@ -18,7 +19,7 @@ public abstract class FastTreeView<P, C> : Control where P : IParentType where C
1819
public static readonly StyledProperty<FlatTreeList<P, C>?> ItemsProperty = AvaloniaProperty.Register<FastTreeView<P, C>, FlatTreeList<P, C>?>(nameof(Items));
1920
public static readonly StyledProperty<bool> IsFilteredProperty = AvaloniaProperty.Register<FastTreeView<P, C>, bool>(nameof(IsFiltered));
2021
public static readonly StyledProperty<bool> RequestRenderProperty = AvaloniaProperty.Register<FastTreeView<P, C>, bool>(nameof(RequestRender));
21-
public static readonly StyledProperty<INodeType?> SelectedNodeProperty = AvaloniaProperty.Register<FastTreeView<P, C>, INodeType?>(nameof(SelectedNode));
22+
public static readonly StyledProperty<INodeType?> SelectedNodeProperty = AvaloniaProperty.Register<FastTreeView<P, C>, INodeType?>(nameof(SelectedNode), defaultBindingMode: BindingMode.TwoWay);
2223

2324

2425
public const float RowHeight = 24;

WDE.Common.Avalonia/Controls/FormattedTextBlock/FormattedTextBlock.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ public override void Render(DrawingContext context)
204204
context.FillRectangle(Background, new Rect(Bounds.Size));
205205
}
206206

207-
Stopwatch sw = new();
208-
Stopwatch sw2 = new();
209-
sw.Start();
210207
int newOverPartIndex = -1;
211208
double x = Padding.Left;
212209
double y = Padding.Top;
@@ -219,10 +216,8 @@ public override void Render(DrawingContext context)
219216
var styleId = parameter ? STYLE_PARAMETER : (source ? STYLE_SOURCE : (IsSelected ? STYLE_DEFAULT_SELECTED : STYLE_DEFAULT));
220217

221218
Rect bounds;
222-
sw2.Start();
223-
(wasWrapped, bounds) = drawer!.Draw(context, text, styleId, !wasWrapped, ref x, ref y, Padding.Left, Bounds.Width + 5);
224-
sw2.Stop();
225-
219+
(wasWrapped, bounds) = drawer!.Draw(context, text, styleId, !wasWrapped, ref x, ref y, Padding.Left, Bounds.Width - Padding.Right);
220+
226221
if (overPartIndex == partIndex && (source || parameter) && IsPointerOver)
227222
{
228223
overLink = true;
@@ -267,7 +262,7 @@ public static Size MeasureText(float maxWidth, string text, Point testPoint, out
267262

268263
protected override Size MeasureOverride(Size availableSize)
269264
{
270-
var size = MeasureText((float)availableSize.Width, Text, currentPos, out var hoverPartIndex);
265+
var size = MeasureText((float)(availableSize.Width - Padding.Left - Padding.Right), Text, currentPos, out var hoverPartIndex);
271266
if (hoverPartIndex.HasValue)
272267
overPartIndex = hoverPartIndex.Value;
273268
return size.Inflate(Padding);

WDE.Common.Avalonia/Utils/NiceColorGenerator/ColorsGenerator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Avalonia.Media;
33
using AvaloniaStyles;
4+
using AvaloniaStyles.Utils;
45

56
namespace WDE.Common.Avalonia.Utils.NiceColorGenerator
67
{
@@ -13,7 +14,7 @@ public class ColorsGenerator : IColorGenerator
1314
private double baseSaturation;
1415
private double? customLuminance;
1516
private double BaseLuminance => customLuminance ?? (SystemTheme.EffectiveThemeIsDark ? 0.16 : 0.84);
16-
private double LuminanceChange => SystemTheme.EffectiveThemeIsDark ? 0.1 : -0.1;
17+
private double LuminanceChange => SystemTheme.EffectiveThemeIsDark ? 0.05 : -0.05;
1718

1819
public void Reset()
1920
{
@@ -31,13 +32,13 @@ public ColorsGenerator(double customSaturation = 0.66, double? customLuminance =
3132

3233
public Color GetNext()
3334
{
34-
Color c = Utils.HSL2RGB(hue, satu, lumo);
35-
if (hue < 0.95)
36-
hue = Math.Min(1, hue + 0.1);
35+
Color c = new HslColor(hue, satu, lumo).ToRgba();
36+
if (hue < 0.92)
37+
hue = Math.Min(1, hue + 0.08);
3738
else
3839
{
3940
hue = 0;
40-
if (LuminanceChange < 0 && lumo >= 0.5 || LuminanceChange > 0 && lumo <= 0.5)
41+
if (LuminanceChange < 0 && lumo >= 0.65 || LuminanceChange > 0 && lumo <= 0.35)
4142
{
4243
lumo += LuminanceChange;
4344
}

WDE.DatabaseEditors/DbDefinitions/creature_tcpp.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
},
9797
{
9898
"name": "Wander Distance",
99-
"db_column_name": "spawndist",
99+
"db_column_name": "wander_distance",
100100
"value_type": "float",
101101
"zero_is_blank": true
102102
},

WDE.SmartScriptEditor.Avalonia/Editor/UserControls/SelectableTemplatedControl.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Avalonia.Controls.Primitives;
66
using Avalonia.Input;
77
using Avalonia.Input.Platform;
8+
using Avalonia.Media;
89
using WDE.Common.Avalonia.Controls;
910

1011
namespace WDE.SmartScriptEditor.Avalonia.Editor.UserControls
@@ -14,6 +15,14 @@ public abstract class SelectableTemplatedControl : TemplatedControl
1415
public static KeyModifiers MultiselectGesture { get; } = AvaloniaLocator.Current
1516
.GetService<PlatformHotkeyConfiguration>()?.CommandModifiers ?? KeyModifiers.Control;
1617

18+
public static readonly StyledProperty<IBrush> SpecialBackgroundProperty = AvaloniaProperty.Register<SelectableTemplatedControl, IBrush>(nameof(SpecialBackground));
19+
20+
public IBrush SpecialBackground
21+
{
22+
get => (IBrush)GetValue(SpecialBackgroundProperty);
23+
set => SetValue(SpecialBackgroundProperty, value);
24+
}
25+
1726
public static readonly AvaloniaProperty DeselectAllRequestProperty =
1827
AvaloniaProperty.Register<SelectableTemplatedControl, ICommand>(nameof(DeselectAllRequest));
1928

WDE.SmartScriptEditor.Avalonia/Editor/UserControls/SmartEventFlagsView.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private void RenderIcon(DrawingContext context, ref RenderContext data, string s
118118
context.DrawRectangle(new SolidColorBrush(Color.Parse("#1976d2")), null, new Rect(data.x, data.y, BoxSize, BoxSize), BoxSize / 2, BoxSize / 2);
119119

120120
var tl = new TextLayout(symbol, new Typeface(GetValue(TextBlock.FontFamilyProperty)), FontSize, foreground, TextAlignment.Center, maxWidth: BoxSize);
121-
tl.Draw(context, new Point(data.x, data.y));
121+
tl.Draw(context, new Point(data.x, data.y + 1));
122122

123123
data.x -= BoxSize + Spacing;
124124
if (data.x < 0)
@@ -131,6 +131,10 @@ private void RenderIcon(DrawingContext context, ref RenderContext data, string s
131131
protected override Size MeasureOverride(Size availableSize)
132132
{
133133
var icons = GetIconsCount();
134+
if (icons == 0)
135+
return new Size(0, 0);
136+
else if (icons == 1)
137+
return new Size(BoxSize, BoxSize);
134138
int rows = (int)Math.Ceiling(icons / 2f);
135139
return new Size(TotalWidth, rows * BoxSize + (rows - 1) * Spacing);
136140
}
@@ -181,6 +185,8 @@ public override void Render(DrawingContext context)
181185
{
182186
RenderContext data = new();
183187
data.x = TotalWidth - BoxSize;
188+
if (GetIconsCount() == 1)
189+
data.x = 0;
184190

185191
var eventFlagsNum = e.Flags.Value;
186192
var eventPhasesNum = e.Phases.Value;

WDE.SmartScriptEditor.Avalonia/Editor/UserControls/SmartScriptView.axaml

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
xmlns:userControls="clr-namespace:WDE.SmartScriptEditor.Avalonia.Editor.UserControls"
99
xmlns:models="clr-namespace:WDE.SmartScriptEditor.Models;assembly=WDE.SmartScriptEditor"
1010
xmlns:viewModels1="clr-namespace:WDE.SmartScriptEditor.Editor.ViewModels;assembly=WDE.SmartScriptEditor"
11+
xmlns:views="clr-namespace:WDE.SmartScriptEditor.Avalonia.Editor.Views"
1112
x:DataType="viewModels1:SmartScriptEditorViewModel"
1213
mc:Ignorable="d" x:Name="Rooot"
1314
Background="{DynamicResource SmartScripts.Background}"
@@ -16,6 +17,7 @@
1617
<system:Boolean x:Key="FalseValue">False</system:Boolean>
1718
<system:Boolean x:Key="TrueValue">True</system:Boolean>
1819
<FloatToScaleTransformConverter x:Key="FloatToScaleTransformConverter" />
20+
<views:ColorIdToNiceColorConverter x:Key="ColorIdToNiceColorConverter" />
1921
</UserControl.Resources>
2022
<UserControl.KeyBindings>
2123
<KeyBinding Gesture="Back" Command="{CompiledBinding DeleteSelected}" />
@@ -61,41 +63,43 @@
6163
>
6264
<Panel>
6365
<DockPanel>
66+
<DockPanel.ContextMenu>
67+
<ContextMenu>
68+
<MenuItem Command="{CompiledBinding NewActionAboveCommand}" IsVisible="{CompiledBinding AnySelected}" Header="New action above" />
69+
<MenuItem Command="{CompiledBinding NewActionBelowCommand}" IsVisible="{CompiledBinding AnySelected}" Header="New action below" />
70+
<Separator IsVisible="{CompiledBinding AnySelected}" />
71+
<MenuItem IsVisible="{CompiledBinding AnySelected}" Command="{CompiledBinding NewEventAboveCommand}" Header="New event above" />
72+
<MenuItem Command="{CompiledBinding NewEventBelowCommand}" Header="New event below" />
73+
<Separator />
74+
<MenuItem IsVisible="{CompiledBinding AnySelected}" Command="{CompiledBinding NewGroupAboveCommand}" Header="New group above" />
75+
<MenuItem Command="{CompiledBinding NewGroupBelowCommand}" Header="New group below" />
76+
<Separator />
77+
<MenuItem Command="{CompiledBinding AddLinkCommand}" IsVisible="{CompiledBinding AnySelected}" Header="Add link" />
78+
<MenuItem Command="{CompiledBinding EditConditionsCommand}" Header="Edit conditions" />
79+
<MenuItem Command="{CompiledBinding DefineGlobalVariable}" Header="Define a global variable" />
80+
<Separator />
81+
<MenuItem Command="{CompiledBinding Copy}" Header="Copy" />
82+
<MenuItem Command="{CompiledBinding Cut}" Header="Cut" />
83+
<MenuItem Command="{CompiledBinding Paste}" Header="Paste" />
84+
<Separator />
85+
<MenuItem Command="{CompiledBinding DeleteSelected}" Header="Delete" />
86+
</ContextMenu>
87+
</DockPanel.ContextMenu>
6488
<userControls:VirtualizedSmartScriptPanel Background="Transparent"
65-
Focusable="True"
66-
DropGroups="{CompiledBinding OnDropGroups}"
67-
DropItems="{CompiledBinding OnDropItems}"
68-
DropActions="{CompiledBinding OnDropActions}"
69-
DropConditions="{CompiledBinding OnDropConditions}"
70-
Script="{CompiledBinding Script}"
71-
Problems="{CompiledBinding ProblematicLines}"
72-
HideComments="{CompiledBinding HideComments}"
73-
HideConditions="{CompiledBinding HideConditions}"
74-
DockPanel.Dock="Top">
75-
<userControls:VirtualizedSmartScriptPanel.ContextMenu>
76-
<ContextMenu>
77-
<MenuItem Command="{CompiledBinding NewActionAboveCommand}" IsVisible="{CompiledBinding AnySelected}" Header="New action above" />
78-
<MenuItem Command="{CompiledBinding NewActionBelowCommand}" IsVisible="{CompiledBinding AnySelected}" Header="New action below" />
79-
<Separator IsVisible="{CompiledBinding AnySelected}" />
80-
<MenuItem IsVisible="{CompiledBinding AnySelected}" Command="{CompiledBinding NewEventAboveCommand}" Header="New event above" />
81-
<MenuItem Command="{CompiledBinding NewEventBelowCommand}" Header="New event below" />
82-
<Separator />
83-
<MenuItem IsVisible="{CompiledBinding AnySelected}" Command="{CompiledBinding NewGroupAboveCommand}" Header="New group above" />
84-
<MenuItem Command="{CompiledBinding NewGroupBelowCommand}" Header="New group below" />
85-
<Separator />
86-
<MenuItem Command="{CompiledBinding EditConditionsCommand}" Header="Edit conditions" />
87-
<MenuItem Command="{CompiledBinding DefineGlobalVariable}" Header="Define a global variable" />
88-
<Separator />
89-
<MenuItem Command="{CompiledBinding Copy}" Header="Copy" />
90-
<MenuItem Command="{CompiledBinding Cut}" Header="Cut" />
91-
<MenuItem Command="{CompiledBinding Paste}" Header="Paste" />
92-
<Separator />
93-
<MenuItem Command="{CompiledBinding DeleteSelected}" Header="Delete" />
94-
</ContextMenu>
95-
</userControls:VirtualizedSmartScriptPanel.ContextMenu>
89+
Focusable="True"
90+
DropGroups="{CompiledBinding OnDropGroups}"
91+
DropItems="{CompiledBinding OnDropItems}"
92+
DropActions="{CompiledBinding OnDropActions}"
93+
DropConditions="{CompiledBinding OnDropConditions}"
94+
Script="{CompiledBinding Script}"
95+
Problems="{CompiledBinding ProblematicLines}"
96+
HideComments="{CompiledBinding HideComments}"
97+
HideConditions="{CompiledBinding HideConditions}"
98+
DockPanel.Dock="Top">
9699
<userControls:VirtualizedSmartScriptPanel.EventItemTemplate>
97100
<DataTemplate DataType="{x:Type models:SmartEvent}">
98101
<userControls:SmartEventView
102+
SpecialBackground="{CompiledBinding ColorId, Converter={StaticResource ColorIdToNiceColorConverter}}"
99103
Selected="{CompiledBinding IsSelected, Mode=TwoWay}"
100104
userControls:SmartScriptPanelLayout.Selected="{CompiledBinding IsSelected, Mode=TwoWay}"
101105
DirectEditParameter="{Binding DataContext.DirectEditParameter, ElementName=Rooot}"
@@ -109,6 +113,7 @@
109113
<userControls:VirtualizedSmartScriptPanel.ActionItemTemplate>
110114
<DataTemplate DataType="{x:Type models:SmartAction}">
111115
<userControls:SmartActionView
116+
SpecialBackground="{CompiledBinding ColorId, Converter={StaticResource ColorIdToNiceColorConverter}}"
112117
Indent="{CompiledBinding Indent}"
113118
DirectEditParameter="{Binding DataContext.DirectEditParameter, ElementName=Rooot}"
114119
DirectOpenParameter="{Binding DataContext.DirectOpenParameter, ElementName=Rooot}"
@@ -123,6 +128,7 @@
123128
<userControls:VirtualizedSmartScriptPanel.ConditionItemTemplate>
124129
<DataTemplate DataType="{x:Type models:SmartCondition}">
125130
<userControls:SmartConditionView
131+
SpecialBackground="{CompiledBinding ColorId, Converter={StaticResource ColorIdToNiceColorConverter}}"
126132
DirectEditParameter="{Binding DataContext.DirectEditParameter, ElementName=Rooot}"
127133
DeselectAllRequest="{Binding DataContext.DeselectAll, ElementName=Rooot}"
128134
DeselectAllButConditionsRequest="{Binding DataContext.DeselectAllButConditions, ElementName=Rooot}"
@@ -181,12 +187,6 @@
181187
Opacity="0.5"
182188
Content="Add event"
183189
Command="{CompiledBinding AddEvent}">
184-
<Button.ContextMenu>
185-
<ContextMenu>
186-
<MenuItem Header="Define global variable"
187-
Command="{CompiledBinding DefineGlobalVariable}" />
188-
</ContextMenu>
189-
</Button.ContextMenu>
190190
</Button>
191191
</DockPanel>
192192
<userControls:SmartScriptOverlay

WDE.SmartScriptEditor.Avalonia/Editor/UserControls/VirtualizedSmartScriptPanel.Arrangement.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private void ArrangeEvent(in SizingContext context, SmartEvent e, bool inGroup,
9999
text.Padding = new Thickness(10,2,10,2);
100100
text.Opacity = 0.6f;
101101
text.Arrange((inGroup ? context.grupedConditionRect : context.conditionRect).WithVertical(y, AddConditionHeight));
102+
text.IsHitTestVisible = false;
102103
y += AddConditionHeight;
103104
conditionsHeight += AddConditionHeight;
104105
}

WDE.SmartScriptEditor.Avalonia/Editor/UserControls/VirtualizedSmartScriptPanel.Measurement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ namespace WDE.SmartScriptEditor.Avalonia.Editor.UserControls;
1212

1313
public partial class VirtualizedSmartScriptPanel
1414
{
15-
public const double AddActionHeight = 26;
16-
public const double AddConditionHeight = 26;
15+
public const double AddActionHeight = 25;
16+
public const double AddConditionHeight = 25;
1717

1818
private MeasureUtil measureEvent;
1919
private MeasureUtil measureCondition;

WDE.SmartScriptEditor.Avalonia/Editor/UserControls/VirtualizedSmartScriptPanel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static HorizRect ConditionWidth(double totalWidth)
116116
private static HorizRect EventWidth(double totalWidth)
117117
{
118118
var x = PaddingLeft + EventPaddingLeft;
119-
var right = Math.Min(Math.Max(totalWidth - 50, PaddingLeft + EventPaddingLeft + 10), 300);
119+
var right = Math.Min(Math.Max(totalWidth - 50, PaddingLeft + EventPaddingLeft + 10), 350);
120120
return new HorizRect(x, right - x);
121121
}
122122

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using Avalonia.Data.Converters;
5+
using Avalonia.Media;
6+
using WDE.Common.Avalonia.Utils.NiceColorGenerator;
7+
8+
namespace WDE.SmartScriptEditor.Avalonia.Editor.Views;
9+
10+
public class ColorIdToNiceColorConverter : IValueConverter
11+
{
12+
private Dictionary<int, IColorGenerator> generators = new();
13+
private Dictionary<(int, long), IBrush> entryToColor = new();
14+
15+
protected IBrush ConvertT(int kind, long entry)
16+
{
17+
if (entryToColor.TryGetValue((kind, entry), out var color))
18+
return color;
19+
20+
if (!generators.TryGetValue(kind, out var generator))
21+
generators[kind] = generator = new ColorsGenerator();
22+
23+
var col = generator.GetNext();
24+
color = new SolidColorBrush(new Color(col.A, col.R, col.G, col.B));
25+
entryToColor[(kind, entry)] = color;
26+
return color;
27+
}
28+
29+
public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
30+
{
31+
if (value is (int kind, long entry) && entry > 0)
32+
{
33+
var color = ConvertT(kind, entry);
34+
return color;
35+
}
36+
37+
return Brushes.Transparent;
38+
}
39+
40+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
41+
{
42+
throw new NotImplementedException();
43+
}
44+
}

0 commit comments

Comments
 (0)