|
| 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