Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
UIToggleImage.cs
Go to the documentation of this file.
4using Terraria.UI;
5
7
8public class UIToggleImage : UIElement
9{
11
13
14 private int _drawWidth;
15
16 private int _drawHeight;
17
19
21
22 private bool _isOn;
23
24 public bool IsOn => _isOn;
25
26 public UIToggleImage(Asset<Texture2D> texture, int width, int height, Point onTextureOffset, Point offTextureOffset)
27 {
28 _onTexture = texture;
29 _offTexture = texture;
30 _offTextureOffset = offTextureOffset;
31 _onTextureOffset = onTextureOffset;
32 _drawWidth = width;
33 _drawHeight = height;
34 Width.Set(width, 0f);
35 Height.Set(height, 0f);
36 }
37
38 protected override void DrawSelf(SpriteBatch spriteBatch)
39 {
40 CalculatedStyle dimensions = GetDimensions();
41 Texture2D value;
42 Point point;
43 if (_isOn)
44 {
45 value = _onTexture.Value;
46 point = _onTextureOffset;
47 }
48 else
49 {
50 value = _offTexture.Value;
51 point = _offTextureOffset;
52 }
53 Color color = (base.IsMouseHovering ? Color.White : Color.Silver);
54 spriteBatch.Draw(value, new Rectangle((int)dimensions.X, (int)dimensions.Y, _drawWidth, _drawHeight), new Rectangle(point.X, point.Y, _drawWidth, _drawHeight), color);
55 }
56
57 public override void LeftClick(UIMouseEvent evt)
58 {
59 Toggle();
60 base.LeftClick(evt);
61 }
62
63 public void SetState(bool value)
64 {
65 _isOn = value;
66 }
67
68 public void Toggle()
69 {
70 _isOn = !_isOn;
71 }
72}
void Draw(Texture2D texture, Vector2 position, Color color)
override void DrawSelf(SpriteBatch spriteBatch)
UIToggleImage(Asset< Texture2D > texture, int width, int height, Point onTextureOffset, Point offTextureOffset)
override void LeftClick(UIMouseEvent evt)
StyleDimension Height
Definition UIElement.cs:29
StyleDimension Width
Definition UIElement.cs:27
CalculatedStyle GetDimensions()
Definition UIElement.cs:382
void Set(float pixels, float precent)