Client LuaCsForBarotrauma
GUIColorPicker.cs
1 #nullable enable
2 using System;
3 using Microsoft.Xna.Framework.Graphics;
4 using Microsoft.Xna.Framework;
5 
6 namespace Barotrauma
7 {
8  public class GUIColorPicker : GUIComponent, IDisposable
9  {
10  public delegate bool OnColorSelectedHandler(GUIColorPicker component, Color color);
12 
13  public float SelectedHue;
14  public float SelectedSaturation;
15  public float SelectedValue;
16 
17  public Color CurrentColor = Color.Black;
18 
19  private Rectangle MainArea,
20  HueArea;
21 
22  private Texture2D? mainTexture,
23  hueTexture;
24 
25  private Color[]? colorData;
26 
27  private Rectangle selectedRect;
28 
29  private bool mouseHeld;
30  private bool isInitialized;
31 
32  private readonly Color transparentWhite = Color.White * 0.8f,
33  transparentBlack = Color.Black * 0.8f;
34 
35  public GUIColorPicker(RectTransform rectT, string? style = null) : base(style, rectT) { }
36 
37  private void Init()
38  {
39  int tWidth = Rect.Width;
40  int sliceWidth = Rect.Width / 8;
41 
42  int mainWidth = tWidth - sliceWidth;
43  int hueWidth = sliceWidth;
44 
45  MainArea = new Rectangle(0, 0, mainWidth, Rect.Height);
46  HueArea = new Rectangle(mainWidth, 0, hueWidth, Rect.Height);
47 
48  colorData = new Color[MainArea.Width * MainArea.Height];
49 
50  if (mainTexture == null)
51  {
52  int width = MainArea.Width,
53  height = MainArea.Height;
54 
55  GenerateGradient(ref colorData!, width, height, DrawHVArea);
56  mainTexture = CreateGradientTexture(colorData!, MainArea.Width, MainArea.Height);
57  }
58 
59  if (hueTexture == null)
60  {
61  int width = HueArea.Width,
62  height = HueArea.Height;
63 
64  Color[] hueData = new Color[width * height];
65 
66  GenerateGradient(ref hueData, width, height, DrawHueArea);
67  hueTexture = CreateGradientTexture(hueData, width, height);
68  }
69  }
70 
71  protected override void Draw(SpriteBatch spriteBatch)
72  {
73  if (mainTexture == null || hueTexture == null || !isInitialized) { return; }
74 
75  Rectangle mainArea = MainArea,
76  hueArea = HueArea;
77 
78  hueArea.Location += Rect.Location;
79  mainArea.Location += Rect.Location;
80 
81  Vector2 mainLocation = mainArea.Location.ToVector2(),
82  hueLocation = hueArea.Location.ToVector2();
83 
84  spriteBatch.Draw(mainTexture, mainLocation, Color.White);
85  spriteBatch.Draw(hueTexture, hueLocation, Color.White);
86 
87  float hueY = hueLocation.Y + ((SelectedHue / 360f) * hueArea.Height);
88  spriteBatch.DrawLine(hueArea.Left, hueY, hueArea.Right, hueY, transparentWhite, thickness: 3);
89  spriteBatch.DrawLine(hueArea.Left, hueY, hueArea.Right, hueY, transparentBlack, thickness: 1);
90 
91  float saturationX = mainLocation.X + SelectedSaturation * MainArea.Width;
92  float valueY = mainLocation.Y + (1.0f - SelectedValue) * MainArea.Height;
93 
94  spriteBatch.DrawLine(saturationX, mainArea.Top,saturationX, mainArea.Bottom, transparentWhite, thickness: 3);
95  spriteBatch.DrawLine(mainArea.Left,valueY, mainArea.Right, valueY, transparentWhite, thickness: 3);
96 
97  spriteBatch.DrawLine(saturationX, mainArea.Top,saturationX, mainArea.Bottom, transparentBlack, thickness: 1);
98  spriteBatch.DrawLine(mainArea.Left,valueY, mainArea.Right, valueY, transparentBlack, thickness: 1);
99  }
100 
101  protected override void Update(float deltaTime)
102  {
103  base.Update(deltaTime);
104 
105  if (!isInitialized)
106  {
107  Init();
108  isInitialized = true;
109  }
110 
112  {
113  mouseHeld = false;
114  }
115 
116  if (GUI.MouseOn != this) { return; }
117 
118  Rectangle mainArea = MainArea,
119  hueArea = HueArea;
120 
121  hueArea.Location += Rect.Location;
122  mainArea.Location += Rect.Location;
123 
125  {
126  mouseHeld = true;
127  if (hueArea.Contains(PlayerInput.MousePosition))
128  {
129  selectedRect = HueArea;
130  }
131  else if (mainArea.Contains(PlayerInput.MousePosition))
132  {
133  selectedRect = MainArea;
134  }
135  else
136  {
137  mouseHeld = false;
138  }
139  }
140 
142  {
143  mouseHeld = false;
144  }
145 
146  if (mouseHeld && (PlayerInput.MouseSpeed != Vector2.Zero || PlayerInput.PrimaryMouseButtonDown()))
147  {
148  if (selectedRect == HueArea)
149  {
150  Vector2 pos = PlayerInput.MousePosition - hueArea.Location.ToVector2();
151  SelectedHue = Math.Clamp(pos.Y / hueArea.Height * 360f, 0, 360);
152  RefreshHue();
153 
154  }
155  else if (selectedRect == MainArea)
156  {
157  var (x, y) = PlayerInput.MousePosition - mainArea.Location.ToVector2();
158  SelectedSaturation = Math.Clamp(x / mainArea.Width, 0, 1);
159  SelectedValue = Math.Clamp(1f - (y / mainArea.Height), 0, 1);
160  }
161 
162  CurrentColor = ToolBoxCore.HSVToRGB(SelectedHue, SelectedSaturation, SelectedValue);
163 
164  OnColorSelected?.Invoke(this, CurrentColor);
165  }
166  }
167 
168  public void Dispose()
169  {
170  mainTexture?.Dispose();
171  mainTexture = null;
172  hueTexture?.Dispose();
173  hueTexture = null;
174  }
175 
176  public void RefreshHue()
177  {
178  if (colorData == null || mainTexture == null) { return; }
179  GenerateGradient(ref colorData, mainTexture.Width, mainTexture.Height, DrawHVArea);
180  mainTexture.SetData(colorData);
181  }
182 
183  private Texture2D CreateGradientTexture(Color[] data, int width, int height)
184  {
185  Texture2D texture = new Texture2D(GameMain.GraphicsDeviceManager.GraphicsDevice, width, height);
186  texture.SetData(data);
187  return texture;
188  }
189 
190  private void GenerateGradient(ref Color[] data, int width, int height, Func<float, float, Color> algorithm)
191  {
192  for (int y = 0; y < height; y++)
193  {
194  for (int x = 0; x < width; x++)
195  {
196  float relativeX = x / (float) width,
197  relativeY = y / (float) height;
198 
199  data[y * width + x] = algorithm(relativeX, relativeY);
200  }
201  }
202  }
203 
204  private Color DrawHVArea(float x, float y) => ToolBoxCore.HSVToRGB(SelectedHue, x, 1.0f - y);
205  private Color DrawHueArea(float x, float y) => ToolBoxCore.HSVToRGB(y * 360f, 1f, 1f);
206  }
207 }
GUIColorPicker(RectTransform rectT, string? style=null)
delegate bool OnColorSelectedHandler(GUIColorPicker component, Color color)
OnColorSelectedHandler? OnColorSelected
override void Draw(SpriteBatch spriteBatch)
override void Update(float deltaTime)
virtual Rectangle Rect
static GraphicsDeviceManager GraphicsDeviceManager
Definition: GameMain.cs:150