Skip to content

Commit ccc5edc

Browse files
committed
Add control styling to C# library
1 parent 42905a2 commit ccc5edc

File tree

9 files changed

+301
-80
lines changed

9 files changed

+301
-80
lines changed

csharp/Controls.cs

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,33 @@
77
using ConstraintSDK.ConstraintArithmetic;
88

99
namespace ConstraintSDK.Controls {
10+
public struct Color {
11+
public float Red;
12+
public float Green;
13+
public float Blue;
14+
public float Alpha;
15+
16+
public Color(float red, float green, float blue, float alpha) {
17+
Red = red;
18+
Green = green;
19+
Blue = blue;
20+
Alpha = alpha;
21+
}
22+
23+
public int Pack() {
24+
return (int)(Red * 0xFF) | (int)(Green * 0xFF) << 8 | (int)(Blue * 0xFF) << 16 | (int)(Alpha * 0xFF) << 24;
25+
}
26+
}
27+
1028
public class Label {
29+
public class Style {
30+
public Color TextColor;
31+
32+
public Style(Color textColor) {
33+
TextColor = textColor;
34+
}
35+
}
36+
1137
public ArithmeticVariable Left;
1238
public ArithmeticVariable Top;
1339

@@ -25,6 +51,8 @@ public class Label {
2551
public string FontFamily;
2652
public float FontSize;
2753

54+
public Style CurrentStyle;
55+
2856
public UIntPtr Control = UIntPtr.Zero;
2957

3058
public Label(LayoutContext context, string text, string fontFamily, float fontSize) {
@@ -36,11 +64,29 @@ public Label(LayoutContext context, string text, string fontFamily, float fontSi
3664
FontFamily = fontFamily;
3765
FontSize = fontSize;
3866

67+
CurrentStyle = context.DefaultLabelStyle;
68+
3969
context.Labels.Add(this);
4070
}
4171
}
4272

4373
public class Button {
74+
public class Style {
75+
public Color TextColor;
76+
77+
public Color BackgroundColor;
78+
79+
public float BorderSize;
80+
public Color BorderColor;
81+
82+
public Style(Color textColor, Color backgroundColor, float borderSize, Color borderColor) {
83+
TextColor = textColor;
84+
BackgroundColor = backgroundColor;
85+
BorderSize = borderSize;
86+
BorderColor = borderColor;
87+
}
88+
}
89+
4490
public ArithmeticVariable Left;
4591
public ArithmeticVariable Top;
4692

@@ -58,6 +104,8 @@ public class Button {
58104
public string FontFamily;
59105
public float FontSize;
60106

107+
public Style CurrentStyle;
108+
61109
public event Action Press = null;
62110

63111
public UIntPtr Control = UIntPtr.Zero;
@@ -74,6 +122,8 @@ public Button(LayoutContext context, string text, string fontFamily, float fontS
74122
FontFamily = fontFamily;
75123
FontSize = fontSize;
76124

125+
CurrentStyle = context.DefaultButtonStyle;
126+
77127
context.Buttons.Add(this);
78128
}
79129

@@ -85,6 +135,22 @@ public void OnPress() {
85135
}
86136

87137
public class TextInput {
138+
public class Style {
139+
public Color TextColor;
140+
141+
public Color BackgroundColor;
142+
143+
public float BorderSize;
144+
public Color BorderColor;
145+
146+
public Style(Color textColor, Color backgroundColor, float borderSize, Color borderColor) {
147+
TextColor = textColor;
148+
BackgroundColor = backgroundColor;
149+
BorderSize = borderSize;
150+
BorderColor = borderColor;
151+
}
152+
}
153+
88154
public ArithmeticVariable Left;
89155
public ArithmeticVariable Top;
90156

@@ -102,6 +168,8 @@ public class TextInput {
102168
public string FontFamily;
103169
public float FontSize;
104170

171+
public Style CurrentStyle;
172+
105173
public event Action<string> Change = null;
106174

107175
public UIntPtr Control = UIntPtr.Zero;
@@ -118,6 +186,8 @@ public TextInput(LayoutContext context, string text, string fontFamily, float fo
118186
FontFamily = fontFamily;
119187
FontSize = fontSize;
120188

189+
CurrentStyle = context.DefaultTextInputStyle;
190+
121191
context.TextInputs.Add(this);
122192
}
123193

@@ -145,10 +215,30 @@ public class LayoutContext {
145215
public List<Button> Buttons = new List<Button>();
146216
public List<TextInput> TextInputs = new List<TextInput>();
147217

218+
public Color BackgroundColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
219+
220+
public Label.Style DefaultLabelStyle = new Label.Style(
221+
new Color(0.0f, 0.0f, 0.0f, 1.0f)
222+
);
223+
224+
public Button.Style DefaultButtonStyle = new Button.Style(
225+
new Color(0.0f, 0.0f, 0.0f, 1.0f),
226+
new Color(0.9f, 0.9f, 0.9f, 1.0f),
227+
1.0f,
228+
new Color(0.7f, 0.7f, 0.7f, 1.0f)
229+
);
230+
231+
public TextInput.Style DefaultTextInputStyle = new TextInput.Style(
232+
new Color(0.0f, 0.0f, 0.0f, 1.0f),
233+
new Color(1.0f, 1.0f, 1.0f, 1.0f),
234+
1.0f,
235+
new Color(0.7f, 0.7f, 0.7f, 1.0f)
236+
);
237+
148238
public event Action<float, float> FrameResize = null;
149239

150240
public void PerformLayout() {
151-
clear_controls();
241+
clear_controls(BackgroundColor.Pack());
152242

153243
ArithmeticContext.Solve();
154244

@@ -163,7 +253,8 @@ public void PerformLayout() {
163253
(UIntPtr)textBytes.Length,
164254
fontFamilyBytes,
165255
(UIntPtr)fontFamilyBytes.Length,
166-
label.FontSize
256+
label.FontSize,
257+
label.CurrentStyle.TextColor.Pack()
167258
);
168259
}
169260

@@ -180,7 +271,11 @@ public void PerformLayout() {
180271
(UIntPtr)textBytes.Length,
181272
fontFamilyBytes,
182273
(UIntPtr)fontFamilyBytes.Length,
183-
button.FontSize
274+
button.FontSize,
275+
button.CurrentStyle.TextColor.Pack(),
276+
button.CurrentStyle.BackgroundColor.Pack(),
277+
button.CurrentStyle.BorderSize,
278+
button.CurrentStyle.BorderColor.Pack()
184279
);
185280
}
186281

@@ -197,7 +292,11 @@ public void PerformLayout() {
197292
(UIntPtr)textBytes.Length,
198293
fontFamilyBytes,
199294
(UIntPtr)fontFamilyBytes.Length,
200-
textInput.FontSize
295+
textInput.FontSize,
296+
textInput.CurrentStyle.TextColor.Pack(),
297+
textInput.CurrentStyle.BackgroundColor.Pack(),
298+
textInput.CurrentStyle.BorderSize,
299+
textInput.CurrentStyle.BorderColor.Pack()
201300
);
202301
}
203302

csharp/SystemControls.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ out float height
1919
);
2020

2121
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
22-
public static extern void clear_controls();
22+
public static extern void clear_controls(int background_color);
2323

2424
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
2525
public static extern UIntPtr create_label(
@@ -29,7 +29,8 @@ public static extern UIntPtr create_label(
2929
UIntPtr text_length,
3030
byte[] font_family_data,
3131
UIntPtr font_family_length,
32-
float font_size
32+
float font_size,
33+
int text_color
3334
);
3435

3536
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
@@ -42,7 +43,11 @@ public static extern UIntPtr create_button(
4243
UIntPtr text_length,
4344
byte[] font_family_data,
4445
UIntPtr font_family_length,
45-
float font_size
46+
float font_size,
47+
int text_color,
48+
int background_color,
49+
float border_size,
50+
int border_color
4651
);
4752

4853
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
@@ -55,7 +60,11 @@ public static extern UIntPtr create_text_input(
5560
UIntPtr text_length,
5661
byte[] font_family_data,
5762
UIntPtr font_family_length,
58-
float font_size
63+
float font_size,
64+
int text_color,
65+
int background_color,
66+
float border_size,
67+
int border_color
5968
);
6069

6170
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]

examples/emscripten_cpp/emscripten_library.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)