Skip to content

Commit 2f40a39

Browse files
committed
Added both IntEditor & GradientEditor for their respective value types.
1 parent 18ec2c5 commit 2f40a39

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
namespace Editor.NodeEditor;
2+
3+
public class GradientEditor : ValueEditor
4+
{
5+
public string Title { get; set; }
6+
public Gradient Value { get; set; }
7+
public NodeUI Node { get; set; }
8+
9+
public GradientEditor( GraphicsItem parent ) : base( parent )
10+
{
11+
HoverEvents = true;
12+
Cursor = CursorShape.Finger;
13+
}
14+
15+
protected override void OnPaint()
16+
{
17+
if ( !Enabled )
18+
return;
19+
20+
Paint.Antialiasing = true;
21+
Paint.TextAntialiasing = true;
22+
23+
var bg = Theme.ControlBackground.WithAlpha( 0.4f );
24+
var fg = Theme.TextControl;
25+
26+
if ( !Paint.HasMouseOver )
27+
{
28+
bg = bg.Darken( 0.1f );
29+
fg = fg.Darken( 0.1f );
30+
}
31+
32+
var rect = LocalRect.Shrink( 1 );
33+
Paint.ClearPen();
34+
Paint.SetBrush( bg );
35+
Paint.DrawRect( rect, 2 );
36+
37+
Value.PaintBlock( rect.Shrink( 2 ) );
38+
}
39+
40+
protected override void OnMousePressed( GraphicsMouseEvent e )
41+
{
42+
base.OnMousePressed( e );
43+
44+
if ( !Enabled )
45+
return;
46+
47+
if ( !e.LeftMouseButton )
48+
return;
49+
50+
if ( !LocalRect.IsInside( e.LocalPosition ) )
51+
return;
52+
53+
var view = Node.GraphicsView;
54+
var position = view.ToScreen( view.FromScene( ToScene( new Vector2( Size.x + 1, 1 ) ) ) );
55+
56+
57+
OpenGradientEditorPopup( ( v ) =>
58+
{
59+
Value = v;
60+
Node.Graph.ChildValuesChanged( null );
61+
Node.Update();
62+
63+
}, position );
64+
65+
e.Accepted = true;
66+
}
67+
68+
private GradientEditorWidget OpenGradientEditorPopup( Action<Gradient> onChange, Vector2? position = null )
69+
{
70+
var popup = new PopupWidget( null );
71+
popup.WindowTitle = "Gradient Editor";
72+
popup.SetWindowIcon( "gradient" );
73+
popup.Layout = Layout.Column();
74+
popup.Layout.Margin = 8;
75+
popup.FixedHeight = 350;
76+
popup.FixedWidth = 500;
77+
popup.Position = position ?? Application.CursorPosition;
78+
79+
var editor = popup.Layout.Add( new GradientEditorWidget( popup ), 1 );
80+
editor.Value = Value;
81+
editor.ValueChanged += ( v ) => onChange?.Invoke( v );
82+
83+
popup.Show();
84+
popup.ConstrainToScreen();
85+
86+
return editor;
87+
}
88+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
namespace Editor.NodeEditor;
2+
3+
public class IntEditor : ValueEditor
4+
{
5+
public string Title { get; set; }
6+
public float Value { get; set; }
7+
public int Min { get; set; } = 0;
8+
public int Max { get; set; } = 1;
9+
10+
public NodeUI Node { get; set; }
11+
private bool IsRange => Min != Max;
12+
13+
private Vector2 _lastCursorPos;
14+
15+
public IntEditor( GraphicsItem parent ) : base( parent )
16+
{
17+
HoverEvents = true;
18+
Cursor = CursorShape.Finger;
19+
}
20+
21+
protected override void OnPaint()
22+
{
23+
if ( !Enabled )
24+
return;
25+
26+
Paint.Antialiasing = true;
27+
Paint.TextAntialiasing = true;
28+
29+
var isRange = IsRange;
30+
var sliderBg = Theme.Blue.WithAlpha( 0.6f );
31+
var bg = Theme.ControlBackground.WithAlpha( 0.4f );
32+
var fg = Theme.TextControl;
33+
34+
if ( !Paint.HasMouseOver )
35+
{
36+
bg = bg.Darken( 0.2f );
37+
fg = fg.Darken( 0.1f );
38+
sliderBg = sliderBg.WithAlpha( 0.5f );
39+
}
40+
41+
var rect = LocalRect.Shrink( 1 );
42+
43+
Paint.ClearPen();
44+
Paint.SetBrush( bg );
45+
Paint.DrawRect( rect, 2 );
46+
47+
if ( isRange && Value >= Min )
48+
{
49+
Paint.SetBrush( sliderBg.WithAlpha( 0.2f ) );
50+
Paint.DrawRect( rect.Shrink( 1, 1, (Value <= Max) ? 40 : 1, 1 ), 2 );
51+
52+
var sliderRect = rect.Shrink( 1, 1, (Value <= Max) ? 40 : 1, 1 );
53+
sliderRect.Right = sliderRect.Left.LerpTo( sliderRect.Right, (Value - Min) / (Max - Min) );
54+
55+
Paint.SetBrush( sliderBg );
56+
Paint.DrawRect( sliderRect, 2 );
57+
}
58+
59+
if ( !isRange )
60+
{
61+
Paint.SetBrush( Paint.HasPressed ? sliderBg : sliderBg.WithAlpha( 0.2f ) );
62+
Paint.DrawRect( rect.Shrink( 1 ), 2 );
63+
64+
if ( Paint.HasMouseOver )
65+
{
66+
Paint.SetPen( fg );
67+
Paint.DrawIcon( rect.Shrink( 1 ), "navigate_before", 10, TextFlag.LeftCenter );
68+
Paint.DrawIcon( rect.Shrink( 1 ), "navigate_next", 10, TextFlag.RightCenter );
69+
}
70+
}
71+
72+
Paint.SetDefaultFont();
73+
Paint.SetPen( fg );
74+
75+
var shrink = isRange ? 4 : 10;
76+
77+
if ( !string.IsNullOrWhiteSpace( Title ) )
78+
{
79+
Paint.DrawText( rect.Shrink( shrink, 0, shrink, 0 ), Title, TextFlag.LeftCenter );
80+
}
81+
82+
Paint.DrawText( rect.Shrink( shrink, 0, shrink, 0 ), $"{Value:0}", TextFlag.RightCenter );
83+
}
84+
85+
protected override void OnMousePressed( GraphicsMouseEvent e )
86+
{
87+
base.OnMousePressed( e );
88+
89+
if ( !Enabled )
90+
return;
91+
92+
if ( !e.LeftMouseButton )
93+
return;
94+
95+
if ( IsRange )
96+
{
97+
UpdateValue( e.LocalPosition.x );
98+
}
99+
else
100+
{
101+
_lastCursorPos = Application.UnscaledCursorPosition;
102+
}
103+
104+
e.Accepted = true;
105+
}
106+
107+
protected override void OnMouseReleased( GraphicsMouseEvent e )
108+
{
109+
base.OnMouseReleased( e );
110+
111+
Cursor = IsRange ? CursorShape.Finger : CursorShape.SizeH;
112+
113+
Update();
114+
}
115+
116+
protected override void OnHoverEnter( GraphicsHoverEvent e )
117+
{
118+
base.OnHoverEnter( e );
119+
120+
Cursor = IsRange ? CursorShape.Finger : CursorShape.SizeH;
121+
}
122+
123+
protected override void OnMouseMove( GraphicsMouseEvent e )
124+
{
125+
base.OnMouseMove( e );
126+
127+
if ( !Enabled )
128+
return;
129+
130+
if ( !e.LeftMouseButton )
131+
return;
132+
133+
if ( IsRange )
134+
{
135+
Cursor = CursorShape.Finger;
136+
UpdateValue( e.LocalPosition.x );
137+
}
138+
else
139+
{
140+
Cursor = CursorShape.Blank;
141+
142+
var cursorPos = Application.UnscaledCursorPosition;
143+
var delta = cursorPos - _lastCursorPos;
144+
Application.UnscaledCursorPosition = _lastCursorPos;
145+
146+
Value += delta.x * 0.01f;
147+
Node.Graph.ChildValuesChanged( null );
148+
Node.Update();
149+
}
150+
151+
e.Accepted = true;
152+
}
153+
154+
private void UpdateValue( float position )
155+
{
156+
Value = (position - 1) / (Size.x - 43);
157+
Value = ((float)Min).LerpTo( Max, Value ).Clamp( Min, Max );
158+
Value = (float)Math.Round( Value / 1.0f ) * 1.0f;
159+
160+
Node.Graph.ChildValuesChanged( null );
161+
Node.Update();
162+
}
163+
}

0 commit comments

Comments
 (0)