Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
GroupOptionButton.cs
Go to the documentation of this file.
5using Terraria.ID;
7using Terraria.UI;
8
10
12{
13 private T _currentOption;
14
16
18
20
22
23 private readonly T _myOption;
24
25 private Color _color;
26
28
29 public float FadeFromBlack = 1f;
30
31 private float _whiteLerp = 0.7f;
32
33 private float _opacity = 0.7f;
34
35 private bool _hovered;
36
37 private bool _soundedHover;
38
39 public bool ShowHighlightWhenSelected = true;
40
41 private bool _UseOverrideColors;
42
44
46
48
50
51 public readonly LocalizedText Description;
52
53 private UIText _title;
54
56
57 public bool IsSelected
58 {
59 get
60 {
61 if (_currentOption != null)
62 {
63 return _currentOption.Equals(_myOption);
64 }
65 return false;
66 }
67 }
68
69 public GroupOptionButton(T option, LocalizedText title, LocalizedText description, Color textColor, string iconTexturePath, float textSize = 1f, float titleAlignmentX = 0.5f, float titleWidthReduction = 10f)
70 {
72 _currentOption = option;
73 _myOption = option;
74 Description = description;
77 _BasePanelTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/PanelGrayscale", (AssetRequestMode)1);
78 _selectedBorderTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/CategoryPanelHighlight", (AssetRequestMode)1);
79 _hoveredBorderTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/CategoryPanelBorder", (AssetRequestMode)1);
80 if (iconTexturePath != null)
81 {
82 _iconTexture = Main.Assets.Request<Texture2D>(iconTexturePath, (AssetRequestMode)1);
83 }
85 if (title != null)
86 {
87 UIText uIText = new UIText(title, textSize)
88 {
89 HAlign = titleAlignmentX,
90 VAlign = 0.5f,
91 Width = StyleDimension.FromPixelsAndPercent(0f - titleWidthReduction, 1f),
93 };
94 uIText.TextColor = textColor;
95 Append(uIText);
96 _title = uIText;
97 }
98 }
99
100 public void SetText(LocalizedText text, float textSize, Color color)
101 {
102 if (_title != null)
103 {
104 _title.Remove();
105 }
106 UIText uIText = new UIText(text, textSize)
107 {
108 HAlign = 0.5f,
109 VAlign = 0.5f,
112 };
113 uIText.TextColor = color;
114 Append(uIText);
115 _title = uIText;
116 }
117
118 public void SetCurrentOption(T option)
119 {
120 _currentOption = option;
121 }
122
123 protected override void DrawSelf(SpriteBatch spriteBatch)
124 {
125 if (_hovered)
126 {
127 if (!_soundedHover)
128 {
130 }
131 _soundedHover = true;
132 }
133 else
134 {
135 _soundedHover = false;
136 }
137 CalculatedStyle dimensions = GetDimensions();
138 Color color = _color;
139 float num = _opacity;
140 bool isSelected = IsSelected;
142 {
143 color = (isSelected ? _overridePickedColor : _overrideUnpickedColor);
145 }
146 Utils.DrawSplicedPanel(spriteBatch, _BasePanelTexture.Value, (int)dimensions.X, (int)dimensions.Y, (int)dimensions.Width, (int)dimensions.Height, 10, 10, 10, 10, Color.Lerp(Color.Black, color, FadeFromBlack) * num);
147 if (isSelected && ShowHighlightWhenSelected)
148 {
149 Utils.DrawSplicedPanel(spriteBatch, _selectedBorderTexture.Value, (int)dimensions.X + 7, (int)dimensions.Y + 7, (int)dimensions.Width - 14, (int)dimensions.Height - 14, 10, 10, 10, 10, Color.Lerp(color, Color.White, _whiteLerp) * num);
150 }
151 if (_hovered)
152 {
153 Utils.DrawSplicedPanel(spriteBatch, _hoveredBorderTexture.Value, (int)dimensions.X, (int)dimensions.Y, (int)dimensions.Width, (int)dimensions.Height, 10, 10, 10, 10, _borderColor);
154 }
155 if (_iconTexture != null)
156 {
157 Color color2 = Color.White;
158 if (!_hovered && !isSelected)
159 {
160 color2 = Color.Lerp(color, Color.White, _whiteLerp) * num;
161 }
162 spriteBatch.Draw(_iconTexture.Value, new Vector2(dimensions.X + 1f, dimensions.Y + 1f), color2);
163 }
164 }
165
166 public override void LeftMouseDown(UIMouseEvent evt)
167 {
169 base.LeftMouseDown(evt);
170 }
171
172 public override void MouseOver(UIMouseEvent evt)
173 {
174 base.MouseOver(evt);
175 _hovered = true;
176 }
177
178 public override void MouseOut(UIMouseEvent evt)
179 {
180 base.MouseOut(evt);
181 _hovered = false;
182 }
183
184 public void SetColor(Color color, float opacity)
185 {
186 _color = color;
187 _opacity = opacity;
188 }
189
190 public void SetColorsBasedOnSelectionState(Color pickedColor, Color unpickedColor, float opacityPicked, float opacityNotPicked)
191 {
192 _UseOverrideColors = true;
193 _overridePickedColor = pickedColor;
194 _overrideUnpickedColor = unpickedColor;
195 _overrideOpacityPicked = opacityPicked;
196 _overrideOpacityUnpicked = opacityNotPicked;
197 }
198
199 public void SetBorderColor(Color color)
200 {
201 _borderColor = color;
202 }
203}
void Draw(Texture2D texture, Vector2 position, Color color)
static void PlaySound(int type, Vector2 position, int style=1)
GroupOptionButton(T option, LocalizedText title, LocalizedText description, Color textColor, string iconTexturePath, float textSize=1f, float titleAlignmentX=0.5f, float titleWidthReduction=10f)
void SetText(LocalizedText text, float textSize, Color color)
override void DrawSelf(SpriteBatch spriteBatch)
void SetColorsBasedOnSelectionState(Color pickedColor, Color unpickedColor, float opacityPicked, float opacityNotPicked)
static readonly Color InventoryDefaultColor
Definition Colors.cs:93
static IAssetRepository Assets
Definition Main.cs:209
StyleDimension Height
Definition UIElement.cs:29
void Append(UIElement element)
Definition UIElement.cs:166
StyleDimension Width
Definition UIElement.cs:27
CalculatedStyle GetDimensions()
Definition UIElement.cs:382
StyleDimension Top
Definition UIElement.cs:23
static void DrawSplicedPanel(SpriteBatch sb, Texture2D texture, int x, int y, int w, int h, int leftEnd, int rightEnd, int topEnd, int bottomEnd, Color c)
Definition Utils.cs:1964
static Color Lerp(Color value1, Color value2, float amount)
Definition Color.cs:491
static StyleDimension FromPixels(float pixels)
static StyleDimension FromPixelsAndPercent(float pixels, float percent)