CrabUI
Loading...
Searching...
No Matches
CUITickBox.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;
10
11namespace CrabUI
12{
13 /// <summary>
14 /// Just a tick box
15 /// </summary>
16 public class CUITickBox : CUIComponent
17 {
18 public GUISoundType ClickSound { get; set; } = GUISoundType.TickBox;
19
20 public event Action<bool> OnStateChange;
21 public void AddOnStateChange(Action<bool> callback) => OnStateChange += callback;
22
23 public CUISprite OnSprite { get; set; }
24 public CUISprite OffSprite { get; set; }
25 public CUISprite HoverOffSprite { get; set; }
26 public CUISprite HoverOnSprite { get; set; }
27 public CUISprite DisabledSprite { get; set; }
28
29 private bool state; public bool State
30 {
31 get => state;
32 set
33 {
34 if (state == value) return;
35 state = value;
36 ChangeSprite();
37 }
38 }
39
40 public override bool Disabled
41 {
42 get => disabled;
43 set
44 {
45 disabled = value;
46 ChangeSprite();
47 }
48 }
49
50 public virtual void ChangeSprite()
51 {
52 if (Disabled)
53 {
54 BackgroundSprite = DisabledSprite;
55 return;
56 }
57
58 if (State)
59 {
60 BackgroundSprite = OnSprite;
61 if (MouseOver) BackgroundSprite = HoverOnSprite;
62 }
63 else
64 {
65 BackgroundSprite = OffSprite;
66 if (MouseOver) BackgroundSprite = HoverOffSprite;
67 }
68 }
69
70
71 public CUITickBox() : base()
72 {
73 Absolute = new CUINullRect(w: 20, h: 20);
74 BackgroundColor = Color.Cyan;
75 Border.Color = Color.Transparent;
76 ConsumeMouseClicks = true;
77 ConsumeDragAndDrop = true;
78 ConsumeSwipe = true;
79
80
81 OffSprite = new CUISprite(CUI.CUITexturePath)
82 {
83 SourceRect = new Rectangle(0, 0, 32, 32),
84 };
85
86 OnSprite = new CUISprite(CUI.CUITexturePath)
87 {
88 SourceRect = new Rectangle(32, 0, 32, 32),
89 };
90
91 HoverOffSprite = new CUISprite(CUI.CUITexturePath)
92 {
93 SourceRect = new Rectangle(64, 0, 32, 32),
94 };
95 HoverOnSprite = new CUISprite(CUI.CUITexturePath)
96 {
97 SourceRect = new Rectangle(96, 0, 32, 32),
98 };
99
100 DisabledSprite = new CUISprite(CUI.CUITexturePath)
101 {
102 SourceRect = new Rectangle(128, 0, 32, 32),
103 };
104
105 ChangeSprite();
106
107 OnMouseDown += (e) =>
108 {
109 if (Disabled) return;
110
111 SoundPlayer.PlayUISound(ClickSound);
112 State = !State;
113 OnStateChange?.Invoke(State);
114 if (Command != null) DispatchUp(new CUICommand(Command, State));
115 };
116
117 OnMouseEnter += (e) => ChangeSprite();
118 OnMouseLeave += (e) => ChangeSprite();
119
120 OnConsume += (o) =>
121 {
122 if (o is bool b) State = b;
123 };
124 }
125 }
126
127}
Base class for all components.
string Command
This command will be dispatched up when some component specific event happens.
Color BackgroundColor
Color of BackgroundSprite, default is black If you're using custom sprite and don't see it make sur...
Action< Object > OnConsume
Happens when appropriate data is received.
void DispatchUp(CUICommand command)
Dispathes command up the component tree until someone consumes it.
CUINullRect Absolute
Absolute size and position in pixels.
CUISprite BackgroundSprite
Will be drawn in background with BackgroundColor Default is solid white 1x1 texture.
In fact a static class managing static things.
Definition CUIErrors.cs:16
Wrapper Containing link to texture and drawing settings, like SourceRedt, DrawMode,...
Definition CUISprite.cs:27
Just a tick box.
Definition CUITickBox.cs:17
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...
Rectangle with float?
Definition CUIRect.cs:104