CrabUI
Loading...
Searching...
No Matches
CUIToggleButton.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6using Barotrauma;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
10using Barotrauma.Extensions;
11namespace CrabUI
12{
13 /// <summary>
14 /// A button which can be on and off
15 /// It's derived from CUITextBlock and has all its props
16 /// </summary>
18 {
19 [CUISerializable]
20 public GUISoundType ClickSound { get; set; } = GUISoundType.Select;
21 [CUISerializable] public bool PlaySound { get; set; } = true;
22
23 [CUISerializable] public Color DisabledColor { get; set; }
24 [CUISerializable] public Color OnColor { get; set; }
25 [CUISerializable] public Color OnHoverColor { get; set; }
26 [CUISerializable] public Color OffColor { get; set; }
27 [CUISerializable] public Color OffHoverColor { get; set; }
28
29 public Color MasterColor
30 {
31 set
32 {
33 OffColor = value.Multiply(0.5f);
34 OffHoverColor = value;
35 OnColor = value.Multiply(0.9f);
36 OnHoverColor = value;
37 }
38 }
39
40 public Color MasterColorOpaque
41 {
42 set
43 {
44 OffColor = new Color((int)(value.R * 0.5f), (int)(value.G * 0.5f), (int)(value.B * 0.5f), value.A);
45 OffHoverColor = value;
46 OnColor = new Color((int)(value.R * 0.9f), (int)(value.G * 0.9f), (int)(value.B * 0.9f), value.A); ;
47 OnHoverColor = value;
48 }
49 }
50
51 // BackgroundColor is used in base.Draw, but here it's calculated from OnColor/OffColor
52 // so it's not a prop anymore, and i don't want to serialize it
53 public new Color BackgroundColor { get => CUIProps.BackgroundColor.Value; set => CUIProps.BackgroundColor.SetValue(value); }
54
55
56 private string onText;
57 private string offText;
58 [CUISerializable]
59 public string OnText
60 {
61 get => onText;
62 set { onText = value; if (State && onText != null) Text = onText; }
63 }
64
65 [CUISerializable]
66 public string OffText
67 {
68 get => offText;
69 set { offText = value; if (!State && offText != null) Text = offText; }
70 }
71
72 public event Action<bool> OnStateChange;
73 public Action<bool> AddOnStateChange { set { OnStateChange += value; } }
74
75 [CUISerializable] public bool ToggleOnClick { get; set; } = true;
76 protected bool state;
77 [CUISerializable]
78 public bool State
79 {
80 get => state;
81 set
82 {
83 state = value;
84 if (state && OnText != null) Text = OnText;
85 if (!state && OffText != null) Text = OffText;
86 }
87 }
88
89 public override void Draw(SpriteBatch spriteBatch)
90 {
91 if (Disabled)
92 {
93 BackgroundColor = DisabledColor;
94 }
95 else
96 {
97 if (State)
98 {
99 if (MouseOver) BackgroundColor = OnHoverColor;
100 else BackgroundColor = OnColor;
101 }
102 else
103 {
104 if (MouseOver) BackgroundColor = OffHoverColor;
105 else BackgroundColor = OffColor;
106 }
107 }
108
109 base.Draw(spriteBatch);
110 }
111
112 public CUIToggleButton() : base()
113 {
114 ConsumeMouseClicks = true;
115 ConsumeDragAndDrop = true;
116 ConsumeSwipe = true;
117
118 //BackgroundColor = OffColor;
119
120 TextAlign = new Vector2(0.5f, 0.5f);
121 Padding = new Vector2(4, 2);
122
123 Text = nameof(CUIToggleButton);
124
125 OnMouseDown += (e) =>
126 {
127 if (!Disabled)
128 {
129 if (PlaySound) SoundPlayer.PlayUISound(ClickSound);
130 if (ToggleOnClick)
131 {
132 State = !State;
133 OnStateChange?.Invoke(State);
134 }
135 }
136 };
137 }
138
139 public CUIToggleButton(string text) : this()
140 {
141 Text = text;
142 }
143
144 }
145}
virtual bool Disabled
Usually means - non interactable, e.g. unclickable gray button.
CUIComponentProps CUIProps
Just a wrapper for CUIProps idk how to separate them better.
Definition CUIProps.cs:27
Vector2 Padding
Used for text, should be in CUITextBlock really.
Passive block of text.
Vector2 TextAlign
A Vector2 ([0..1],[0..1])
A button which can be on and off It's derived from CUITextBlock and has all its props.
override void Draw(SpriteBatch spriteBatch)
Here component should be drawn.