CrabUI
Loading...
Searching...
No Matches
Calc.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
7using System.IO;
8
9using Barotrauma;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
13using HarmonyLib;
14using CrabUI;
15
16namespace CrabUITest
17{
18 /// <summary>
19 /// I was trying to make a calculator
20 /// But failed miserably because i have no idea how calculator works
21 /// </summary>
22 public class CalculatorMod : IFakeMod
23 {
24 public class Calculator
25 {
26 public event Action<float> OnValueChange;
27
28 private float _value; public float Value
29 {
30 get => _value;
31 set
32 {
33 _value = value;
34 OnValueChange?.Invoke(value);
35 }
36 }
37
38 public void SetDigit(float digit)
39 {
40 Value = digit;
41 }
42 }
43
44 public Calculator calculator;
45
46 public void Initialize(CUIComponent FakeMain)
47 {
48 calculator = new Calculator();
49
50 CUIComponent Header;
51 CUIGrid ButtonGrid;
52 CUITextBlock tb;
53
54
55 CUIFrame mainframe = new CUIFrame()
56 {
57 Absolute = new CUINullRect(700, 200, 300, 400),
58 AbsoluteMin = new CUINullRect(w: 100, h: 100),
59
60 //Anchor = CUIAnchor.Center,
61 };
62
63 mainframe["list"] = new CUIVerticalList()
64 {
65 Relative = new CUINullRect(0, 0, 1, 1),
66 BackgroundColor = Color.Blue,
67 };
68
69 mainframe["list"]["Header"] = Header = new CUIComponent()
70 {
71 BackgroundColor = Color.Green,
72 FitContent = new CUIBool2(false, true),
73 };
74
75 mainframe["list"]["ButtonGrid"] = ButtonGrid = new CUIGrid()
76 {
77 FillEmptySpace = new CUIBool2(false, true),
78 BackgroundColor = Color.Brown,
79 GridTemplateColumns = "25% 25% 25% 25%",
80 GridTemplateRows = "25% 25% 25% 25%",
81 };
82
83
84 Header["tb"] = tb = new CUITextBlock("Value");
85
86
87 calculator.OnValueChange += (value) =>
88 {
89 tb.Text = value.ToString();
90 };
91
92 void addNumButton(int i, int x, int y)
93 {
94 ButtonGrid.Append(new CUIButton($"{i}")
95 {
96 GridCell = new Point(x, y),
97 AddOnClick = (e) => calculator.SetDigit(i),
98 });
99 }
100
101 addNumButton(1, 0, 0);
102 addNumButton(2, 1, 0);
103 addNumButton(3, 2, 0);
104 addNumButton(4, 0, 1);
105 addNumButton(5, 1, 1);
106 addNumButton(6, 2, 1);
107 addNumButton(7, 0, 2);
108 addNumButton(8, 1, 2);
109 addNumButton(9, 2, 2);
110
111 ButtonGrid.Append(new CUIButton($"+")
112 {
113 GridCell = new Point(3, 2),
114 });
115
116 ButtonGrid.Append(new CUIButton($"-")
117 {
118 GridCell = new Point(3, 1),
119 });
120
121 ButtonGrid.Append(new CUIButton($"=")
122 {
123 GridStartCell = new Point(2, 3),
124 GridEndCell = new Point(3, 3),
125 });
126
127 FakeMain.Append(mainframe);
128 }
129
130 public void Dispose()
131 {
132
133 }
134 }
135}
A button It's derived from CUITextBlock and has all its props.
Definition CUIButton.cs:19
Base class for all components.
virtual CUIComponent Append(CUIComponent child, string name=null, [CallerMemberName] string memberName="")
Adds children to the end of the list.
Draggable and resizable container for other components.
Definition CUIFrame.cs:16
A Grid containing children in its cells.
Definition CUIGrid.cs:16
Passive block of text.
Resizing components to it's Width and placing them sequentially.
I was trying to make a calculator But failed miserably because i have no idea how calculator works.
Definition Calc.cs:23
Vector2 but with bools.
Definition CUIBool2.cs:17
Rectangle with float?
Definition CUIRect.cs:104