1+ //using System;
2+ //using Microsoft.Xna.Framework.Graphics;
3+ //using Terraria.Audio;
4+ //using Terraria.GameContent.UI.Elements;
5+ //using Terraria.ID;
6+ //using Terraria.ModLoader.UI;
7+ //using Terraria.UI;
8+
9+ //namespace UIEditor.UI
10+ //{
11+ // internal class SliderElement : UIElement
12+ // {
13+ // private readonly string labelTextKey;
14+ // public UIText Label;
15+ // public Slider Slider;
16+ // public float Min { get; }
17+ // public float Max { get; }
18+ // private readonly float step;
19+ // private readonly Action<float> onValueChangedCallback;
20+ // private float appliedValue;
21+ // private string tooltipText;
22+
23+ // private readonly bool applyOnReleaseBehavior;
24+ // private float liveDraggingValue;
25+
26+ // public SliderElement(string label, string tooltip, float min, float max, float defaultValue, float step = 0.01f, Action<float> onValueChanged = null, bool applyOnRelease = false)
27+ // {
28+ // Min = min;
29+ // Max = max;
30+ // tooltipText = tooltip;
31+ // labelTextKey = label;
32+ // this.step = step;
33+ // onValueChangedCallback = onValueChanged;
34+ // applyOnReleaseBehavior = applyOnRelease;
35+
36+ // Width.Set(0, 1f);
37+ // Height.Set(23, 0);
38+
39+ // Label = new UIText("", 1.00f, false)
40+ // {
41+ // Left = { Pixels = 0 },
42+ // VAlign = 0.5f,
43+ // TextOriginX = 0f,
44+ // TextOriginY = 0.5f,
45+ // TextColor = Color.Gray
46+ // };
47+ // Append(Label);
48+
49+ // Label.OnMouseOver += (_, _) =>
50+ // {
51+ // SoundEngine.PlaySound(SoundID.MenuTick);
52+ // Label.TextColor = Color.White;
53+ // };
54+ // Label.OnMouseOut += (_, _) =>
55+ // {
56+ // Label.TextColor = Color.Gray;
57+ // };
58+
59+ // Slider = new Slider()
60+ // {
61+ // Left = { Pixels = 120 },
62+ // Width = { Pixels = -125, Percent = 1f },
63+ // VAlign = 0.5f
64+ // };
65+
66+ // appliedValue = MathHelper.Clamp(defaultValue, Min, Max);
67+ // liveDraggingValue = appliedValue;
68+ // Slider.Ratio = (appliedValue - Min) / (Max - Min);
69+ // UpdateLabelText();
70+
71+ // if (applyOnReleaseBehavior)
72+ // {
73+ // Slider.OnDrag += HandleSliderDragLive;
74+ // Slider.OnValueAppliedOnMouseUp += HandleSliderValueAppliedOnRelease;
75+ // }
76+ // else
77+ // {
78+ // Slider.OnDrag += HandleSliderDragNormal;
79+ // Slider.OnValueAppliedOnMouseUp += HandleSliderValueAppliedOnRelease; // Also apply on release for normal sliders for click behavior
80+ // }
81+ // Append(Slider);
82+ // }
83+
84+ // private void HandleSliderDragLive(float currentRatio)
85+ // {
86+ // float rawValue = Min + currentRatio * (Max - Min);
87+ // liveDraggingValue = (float)Math.Round((rawValue - Min) / step) * step + Min;
88+ // liveDraggingValue = MathHelper.Clamp(liveDraggingValue, Min, Max);
89+ // UpdateLabelText();
90+ // }
91+
92+ // private void HandleSliderValueAppliedOnRelease(float finalRatio)
93+ // {
94+ // float rawValue = Min + finalRatio * (Max - Min);
95+ // appliedValue = (float)Math.Round((rawValue - Min) / step) * step + Min;
96+ // appliedValue = MathHelper.Clamp(appliedValue, Min, Max);
97+
98+ // if (applyOnReleaseBehavior)
99+ // {
100+ // liveDraggingValue = appliedValue;
101+ // }
102+
103+ // Slider.Ratio = (appliedValue - Min) / (Max - Min);
104+
105+ // onValueChangedCallback?.Invoke(appliedValue);
106+ // UpdateLabelText();
107+ // }
108+
109+ // private void HandleSliderDragNormal(float currentRatio)
110+ // {
111+ // float rawValue = Min + currentRatio * (Max - Min);
112+ // float newSnappedValue = (float)Math.Round((rawValue - Min) / step) * step + Min;
113+ // newSnappedValue = MathHelper.Clamp(newSnappedValue, Min, Max);
114+
115+ // if (Math.Abs(appliedValue - newSnappedValue) > float.Epsilon)
116+ // {
117+ // appliedValue = newSnappedValue;
118+ // liveDraggingValue = appliedValue; // For normal sliders, live is same as applied
119+
120+ // onValueChangedCallback?.Invoke(appliedValue);
121+ // UpdateLabelText();
122+ // }
123+ // }
124+
125+ // public override void Update(GameTime gameTime)
126+ // {
127+ // base.Update(gameTime);
128+ // }
129+ // private void UpdateLabelText()
130+ // {
131+ // string textToShow;
132+ // if (applyOnReleaseBehavior)
133+ // {
134+ // string liveText, appliedTextFormatted;
135+
136+ // bool usePercentage = labelTextKey != null && labelTextKey.Contains("UI Scale") || Max - Min <= 1f && Min >= 0f && Max <= 1f;
137+
138+ // if (usePercentage)
139+ // {
140+ // liveText = $"{(int)(liveDraggingValue * 100f)}%";
141+ // appliedTextFormatted = $"{(int)(appliedValue * 100f)}%";
142+ // }
143+ // else
144+ // {
145+ // // For applyOnRelease sliders that are not percentages, check if values are whole numbers
146+ // liveText = liveDraggingValue % 1 == 0 ? $"{(int)liveDraggingValue}" : $"{liveDraggingValue:F2}";
147+ // appliedTextFormatted = appliedValue % 1 == 0 ? $"{(int)appliedValue}" : $"{appliedValue:F2}";
148+ // }
149+ // textToShow = $"{labelTextKey}: {liveText} ";
150+ // //textToShow = $"{labelTextKey}: ({liveText}) [{appliedTextFormatted}]";
151+ // }
152+ // else // For normal sliders like Snap Threshold
153+ // {
154+ // bool usePercentage = Max - Min <= 1f && Min >= 0f && Max <= 1f; // This condition might not apply to Snap if its range is 0-100
155+
156+ // // Check if the Snap slider (or other non-percentage, non-applyOnRelease sliders) has a whole number value
157+ // if (!usePercentage && appliedValue % 1 == 0)
158+ // {
159+ // textToShow = $"{labelTextKey}: {(int)appliedValue}";
160+ // }
161+ // else if (usePercentage) // Handles the 0-1 range percentage case
162+ // {
163+ // textToShow = $"{labelTextKey}: {(int)(appliedValue * 100f)}%";
164+ // }
165+ // else // Default for non-whole numbers or other cases
166+ // {
167+ // textToShow = $"{labelTextKey}: {appliedValue:F2}";
168+ // }
169+ // }
170+ // Label.SetText(textToShow);
171+ // }
172+
173+ // public override void Draw(SpriteBatch spriteBatch)
174+ // {
175+ // base.Draw(spriteBatch);
176+ // if (Label.IsMouseHovering && !string.IsNullOrEmpty(tooltipText))
177+ // {
178+ // //Main.instance.MouseText(tooltipText);
179+ // UICommon.TooltipMouseText(tooltipText);
180+ // }
181+ // }
182+ // }
183+ //}
0 commit comments