CrabUI
Loading...
Searching...
No Matches
CUIButton.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 System.Xml.Linq;
11using Barotrauma.Extensions;
12namespace CrabUI
13{
14 /// <summary>
15 /// A button
16 /// It's derived from CUITextBlock and has all its props
17 /// </summary>
18 public class CUIButton : CUITextBlock
19 {
20 [CUISerializable]
21 public GUISoundType ClickSound { get; set; } = GUISoundType.Select;
22 [CUISerializable] public bool PlaySound { get; set; } = true;
23 [CUISerializable] public Color DisabledColor { get; set; }
24 [CUISerializable] public Color InactiveColor { get; set; }
25 [CUISerializable] public Color MouseOverColor { get; set; }
26 [CUISerializable] public Color MousePressedColor { get; set; }
27 [CUISerializable] public bool AutoUpdateColor { get; set; } = true;
28
29 /// <summary>
30 /// Convenient prop to set all colors at once
31 /// </summary>
32 public Color MasterColor
33 {
34 set
35 {
36 InactiveColor = value.Multiply(0.7f);
37 MouseOverColor = value.Multiply(0.9f);
38 MousePressedColor = value;
39 DetermineColor();
40 }
41 }
42
43 public Color MasterColorOpaque
44 {
45 set
46 {
47 InactiveColor = new Color((int)(value.R * 0.7f), (int)(value.G * 0.7f), (int)(value.B * 0.7f), value.A);
48 MouseOverColor = new Color((int)(value.R * 0.9f), (int)(value.G * 0.9f), (int)(value.B * 0.9f), value.A);
49 MousePressedColor = value;
50 DetermineColor();
51 }
52 }
53
54 /// <summary>
55 /// BackgroundColor is used in base.Draw, but here it's calculated from colors above
56 /// So it's not a prop anymore, and i don't want to serialize it
57 /// </summary>
58 public new Color BackgroundColor
59 {
60 get => CUIProps.BackgroundColor.Value;
61 set => CUIProps.BackgroundColor.SetValue(value);
62 }
63
64 public void DetermineColor()
65 {
66 if (!AutoUpdateColor) return;
67 if (Disabled)
68 {
69 BackgroundColor = DisabledColor;
70 }
71 else
72 {
73 BackgroundColor = InactiveColor;
74 if (MouseOver) BackgroundColor = MouseOverColor;
75 if (MousePressed) BackgroundColor = MousePressedColor;
76 }
77 }
78
79 public override void Draw(SpriteBatch spriteBatch)
80 {
81 //DetermineColor();
82 base.Draw(spriteBatch);
83 }
84 public CUIButton() : base()
85 {
86 Text = "CUIButton";
87 ConsumeMouseClicks = true;
88 ConsumeDragAndDrop = true;
89 ConsumeSwipe = true;
90
91 OnMouseDown += (e) =>
92 {
93 if (!Disabled)
94 {
95 if (PlaySound) SoundPlayer.PlayUISound(ClickSound);
96
97 if (Command != null && Command != "")
98 {
100 }
101 }
102 };
103
104 OnMouseOff += (e) => DetermineColor();
105 OnMouseOn += (e) => DetermineColor();
106 OnStyleApplied += DetermineColor;
107 DetermineColor();
108 }
109
110 public CUIButton(string text) : this()
111 {
112 Text = text;
113 }
114
115
116 }
117}
A button It's derived from CUITextBlock and has all its props.
Definition CUIButton.cs:19
override void Draw(SpriteBatch spriteBatch)
Here component should be drawn.
Definition CUIButton.cs:79
new Color BackgroundColor
BackgroundColor is used in base.Draw, but here it's calculated from colors above So it's not a prop a...
Definition CUIButton.cs:59
Color MasterColor
Convenient prop to set all colors at once.
Definition CUIButton.cs:33
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
string Command
This command will be dispatched up when some component specific event happens.
void DispatchUp(CUICommand command)
Dispathes command up the component tree until someone consumes it.
Action OnStyleApplied
Use it to e.g. update component color.
Passive block of text.
record CUICommand(string Name, object Data=null)
Can be dispatched up the component tree to notify parent about something add pass some event data wit...