Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UIColoredImageButton.cs
Go to the documentation of this file.
5using Terraria.UI;
6
8
10{
12
14
16
18
20
21 private Color _color;
22
23 private float _visibilityActive = 1f;
24
25 private float _visibilityInactive = 0.4f;
26
27 private bool _selected;
28
29 private bool _hovered;
30
31 public UIColoredImageButton(Asset<Texture2D> texture, bool isSmall = false)
32 {
34 _texture = texture;
35 if (isSmall)
36 {
37 _backPanelTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/SmallPanel", (AssetRequestMode)1);
38 }
39 else
40 {
41 _backPanelTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/CategoryPanel", (AssetRequestMode)1);
42 }
43 Width.Set(_backPanelTexture.Width(), 0f);
44 Height.Set(_backPanelTexture.Height(), 0f);
45 _backPanelHighlightTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/CategoryPanelHighlight", (AssetRequestMode)1);
46 if (isSmall)
47 {
48 _backPanelBorderTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/SmallPanelBorder", (AssetRequestMode)1);
49 }
50 else
51 {
52 _backPanelBorderTexture = Main.Assets.Request<Texture2D>("Images/UI/CharCreation/CategoryPanelBorder", (AssetRequestMode)1);
53 }
54 }
55
56 public void SetImage(Asset<Texture2D> texture)
57 {
58 _texture = texture;
59 Width.Set(_texture.Width(), 0f);
60 Height.Set(_texture.Height(), 0f);
61 }
62
64 {
65 _texture = texture;
66 }
67
68 protected override void DrawSelf(SpriteBatch spriteBatch)
69 {
70 CalculatedStyle dimensions = GetDimensions();
71 Vector2 position = dimensions.Position() + new Vector2(dimensions.Width, dimensions.Height) / 2f;
72 spriteBatch.Draw(_backPanelTexture.Value, position, null, Color.White * (base.IsMouseHovering ? _visibilityActive : _visibilityInactive), 0f, _backPanelTexture.Size() / 2f, 1f, SpriteEffects.None, 0f);
73 _ = Color.White;
74 if (_hovered)
75 {
76 spriteBatch.Draw(_backPanelBorderTexture.Value, position, null, Color.White, 0f, _backPanelBorderTexture.Size() / 2f, 1f, SpriteEffects.None, 0f);
77 }
78 if (_selected)
79 {
80 spriteBatch.Draw(_backPanelHighlightTexture.Value, position, null, Color.White, 0f, _backPanelHighlightTexture.Size() / 2f, 1f, SpriteEffects.None, 0f);
81 }
82 if (_middleTexture != null)
83 {
84 spriteBatch.Draw(_middleTexture.Value, position, null, Color.White, 0f, _middleTexture.Size() / 2f, 1f, SpriteEffects.None, 0f);
85 }
86 spriteBatch.Draw(_texture.Value, position, null, _color, 0f, _texture.Size() / 2f, 1f, SpriteEffects.None, 0f);
87 }
88
89 public override void MouseOver(UIMouseEvent evt)
90 {
91 base.MouseOver(evt);
93 _hovered = true;
94 }
95
96 public void SetVisibility(float whenActive, float whenInactive)
97 {
98 _visibilityActive = MathHelper.Clamp(whenActive, 0f, 1f);
99 _visibilityInactive = MathHelper.Clamp(whenInactive, 0f, 1f);
100 }
101
102 public void SetColor(Color color)
103 {
104 _color = color;
105 }
106
107 public void SetMiddleTexture(Asset<Texture2D> texAsset)
108 {
109 _middleTexture = texAsset;
110 }
111
112 public void SetSelected(bool selected)
113 {
114 _selected = selected;
115 }
116
117 public override void MouseOut(UIMouseEvent evt)
118 {
119 base.MouseOut(evt);
120 _hovered = false;
121 }
122}
void Draw(Texture2D texture, Vector2 position, Color color)
static float Clamp(float value, float min, float max)
Definition MathHelper.cs:46
static void PlaySound(int type, Vector2 position, int style=1)
UIColoredImageButton(Asset< Texture2D > texture, bool isSmall=false)
void SetVisibility(float whenActive, float whenInactive)
static IAssetRepository Assets
Definition Main.cs:209
StyleDimension Height
Definition UIElement.cs:29
StyleDimension Width
Definition UIElement.cs:27
CalculatedStyle GetDimensions()
Definition UIElement.cs:382
void Set(float pixels, float precent)