Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UIIconTextButton.cs
Go to the documentation of this file.
1using System;
6using Terraria.ID;
8using Terraria.UI;
9
11
13{
15
17
19
20 private Color _color;
21
23
24 public float FadeFromBlack = 1f;
25
26 private float _whiteLerp = 0.7f;
27
28 private float _opacity = 0.7f;
29
30 private bool _hovered;
31
32 private bool _soundedHover;
33
34 private UIText _title;
35
36 public UIIconTextButton(LocalizedText title, Color textColor, string iconTexturePath, float textSize = 1f, float titleAlignmentX = 0.5f, float titleWidthReduction = 10f)
37 {
41 _BasePanelTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/PanelGrayscale", (AssetRequestMode)1);
42 _hoveredTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/CategoryPanelHighlight", (AssetRequestMode)1);
43 if (iconTexturePath != null)
44 {
45 _iconTexture = Main.Assets.Request<Texture2D>(iconTexturePath, (AssetRequestMode)1);
46 }
48 if (title != null)
49 {
50 SetText(title, textSize, textColor);
51 }
52 }
53
54 public void SetText(LocalizedText text, float textSize, Color color)
55 {
56 if (_title != null)
57 {
58 _title.Remove();
59 }
60 UIText uIText = new UIText(text, textSize)
61 {
62 HAlign = 0f,
63 VAlign = 0.5f,
67 };
68 uIText.TextColor = color;
69 Append(uIText);
70 _title = uIText;
71 if (_iconTexture != null)
72 {
73 Width.Set(_title.GetDimensions().Width + (float)_iconTexture.Width() + 26f, 0f);
74 Height.Set(Math.Max(_title.GetDimensions().Height, _iconTexture.Height()) + 16f, 0f);
75 }
76 }
77
78 protected override void DrawSelf(SpriteBatch spriteBatch)
79 {
80 if (_hovered)
81 {
82 if (!_soundedHover)
83 {
85 }
86 _soundedHover = true;
87 }
88 else
89 {
90 _soundedHover = false;
91 }
92 CalculatedStyle dimensions = GetDimensions();
93 Color color = _color;
94 float opacity = _opacity;
95 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) * opacity);
96 if (_iconTexture != null)
97 {
98 Color color2 = Color.Lerp(color, Color.White, _whiteLerp) * opacity;
99 spriteBatch.Draw(_iconTexture.Value, new Vector2(dimensions.X + dimensions.Width - (float)_iconTexture.Width() - 5f, dimensions.Center().Y - (float)(_iconTexture.Height() / 2)), color2);
100 }
101 }
102
103 public override void LeftMouseDown(UIMouseEvent evt)
104 {
106 base.LeftMouseDown(evt);
107 }
108
109 public override void MouseOver(UIMouseEvent evt)
110 {
111 base.MouseOver(evt);
113 _hovered = true;
114 }
115
116 public override void MouseOut(UIMouseEvent evt)
117 {
118 base.MouseOut(evt);
120 _hovered = false;
121 }
122
123 public void SetColor(Color color, float opacity)
124 {
125 _color = color;
126 _opacity = opacity;
127 }
128
129 public void SetHoverColor(Color color)
130 {
131 _hoverColor = color;
132 }
133}
void Draw(Texture2D texture, Vector2 position, Color color)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static void PlaySound(int type, Vector2 position, int style=1)
UIIconTextButton(LocalizedText title, 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)
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 Left
Definition UIElement.cs:25
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)
void Set(float pixels, float precent)
static StyleDimension FromPixelsAndPercent(float pixels, float percent)