CrabUI
Loading...
Searching...
No Matches
CUIBorder.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
14 //TODO why is this mutable?
15 public class CUIBorder : ICloneable
16 {
17 private Color color; public Color Color
18 {
19 get => color;
20 set
21 {
22 color = value;
23 UpdateVisible();
24 }
25 }
26 private float thickness = 1f; public float Thickness
27 {
28 get => thickness;
29 set
30 {
31 thickness = value;
32 UpdateVisible();
33 }
34 }
35
36 public bool Visible { get; set; }
37
38
39 public void UpdateVisible()
40 {
41 Visible = Thickness != 0f && color != Color.Transparent;
42 }
43
44 public CUIBorder() { }
45 public CUIBorder(Color color, float thickness = 1f)
46 {
47 this.color = color;
48 this.thickness = thickness;
49 UpdateVisible();
50 }
51
52 public override bool Equals(object obj)
53 {
54 if (obj is CUIBorder border)
55 {
56 if (Color == border.Color && Thickness == border.Thickness) return true;
57 }
58 return false;
59 }
60
61 public object Clone()
62 {
63 return new CUIBorder(Color, Thickness);
64 }
65
66
67 public override string ToString() => $"[{CUIExtensions.ColorToString(Color)},{Thickness}]";
68 public static CUIBorder Parse(string raw)
69 {
70 CUIBorder border = new CUIBorder();
71 try
72 {
73 string[] sub = raw.Split('[', ']');
74
75 if (sub.Length == 1)
76 {
77 border.Color = CUIExtensions.ParseColor(sub[0]);
78 }
79
80 if (sub.Length > 1)
81 {
82 string content = raw.Split('[', ']').ElementAtOrDefault(1);
83
84 if (content.Trim() != "")
85 {
86 IEnumerable<string> values = content.Split(',');
87 if (values.ElementAtOrDefault(0) != null)
88 {
89 border.Color = CUIExtensions.ParseColor(values.ElementAtOrDefault(0));
90 }
91 if (values.ElementAtOrDefault(1) != null)
92 {
93 float t = 1f;
94 if (float.TryParse(values.ElementAtOrDefault(1), out t))
95 {
96 border.Thickness = t;
97 }
98 }
99 }
100 }
101
102 }
103 catch (Exception e) { CUI.Warning($"Couldn't parse CUIBorder [{raw}]:\n{e.Message}"); }
104
105 return border;
106 }
107 }
108}