-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInteractivityAndDynamicSizingExample.cs
More file actions
100 lines (89 loc) · 3.33 KB
/
InteractivityAndDynamicSizingExample.cs
File metadata and controls
100 lines (89 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using MagicUI.Core;
using MagicUI.Elements;
using UnityEngine;
namespace MagicUIExamples
{
// example demonstrating the use of interactive components and dynamic layout behaviors
public static class InteractivityAndDynamicSizingExample
{
private static LayoutRoot? layout;
public static void Setup(LayoutRoot inputLayout)
{
layout = inputLayout;
StackLayout interactionLayout = new(layout)
{
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Spacing = 10,
Orientation = Orientation.Vertical,
Padding = new(20)
};
Button testButton = new(layout)
{
MinWidth = 100
};
testButton.Click += ClearContent;
testButton.OnHover += (_) => MagicUIExamples.Instance!.Log("Hovered!");
testButton.OnUnhover += (_) => MagicUIExamples.Instance!.Log("Unhovered!");
Button toggleButton = new(layout)
{
Content = "Toggle is on",
BorderColor = Color.blue,
ContentColor = Color.green,
Margin = 20
};
toggleButton.Click += ToggleButtonState;
TextInput input = new(layout, "TestInput")
{
FontSize = 22,
Placeholder = "Enter text here...",
};
Button inputButton = new(layout)
{
Content = "Click to capture input",
FontSize = 15,
Margin = 20
};
inputButton.Click += CaptureInputContentForButton;
interactionLayout.Children.Add(testButton);
interactionLayout.Children.Add(toggleButton);
interactionLayout.Children.Add(input);
interactionLayout.Children.Add(inputButton);
layout.ListenForHotkey(KeyCode.A, () => testButton.Content += "A");
layout.ListenForHotkey(KeyCode.Return, () => testButton.Content += "\n");
layout.ListenForHotkey(KeyCode.Equals, () => testButton.Margin += 5, ModifierKeys.Shift);
}
private static void CaptureInputContentForButton(Button sender)
{
TextInput? input = layout?.GetElement<TextInput>("TestInput");
if (input != null)
{
sender.Content = input.Text;
}
}
private static void ToggleButtonState(Button sender)
{
// realistally you would make a custom element to handle the toggle logic rather than managing state this way
bool currentState = sender.ContentColor == Color.green;
TextInput? input = layout?.GetElement<TextInput>("TestInput");
if (input != null)
{
input.Borderless = currentState;
}
if (currentState)
{
sender.ContentColor = Color.red;
sender.Content = "Toggle is off";
}
else
{
sender.ContentColor = Color.green;
sender.Content = "Toggle is on";
}
}
private static void ClearContent(Button sender)
{
sender.Content = "";
}
}
}