Package Description
$ dotnet add package H073.SurfaceKitSurfaceKit is a modular, UI framework for C# and MonoGame, designed to help developers build flexible, organized, and interactive interfaces for games and applications. The library offers container views, input handling, graphics overlays, and now, tools for building complex view hierarchies with ViewBuilder.
HStack, VStack, and ZStack for horizontal, vertical, and layered arrangements.SurfaceInputManager.ViewBuilder and ViewHelper.Text, Image, and Background.To install SurfaceKit via NuGet:
dotnet add package SurfaceKit
View is the base class for UI elements. Views have position, dimension, layout flexibility, and modifier properties, which allow them to be customized and controlled in a parent view. Common UI components like Text, Image, and Background inherit from View.
SurfaceKit provides HStack, VStack, and ZStack for arranging views horizontally, vertically, and in layers, respectively. These containers allow for easy, nested layouts.
var hStack = new HStack();
hStack.Add(new Image("texture.png"));
hStack.Add(new Text("Hello World", "Arial") { ForegroundStyle = Color.White });
SurfaceKit includes ModifierBase and specific modifiers like Padding to customize views. Gestures such as OnTapGesture add interactive behaviors to views.
var onTap = new OnTapGesture(() => Console.WriteLine("Tapped!"));
var paddedView = new Padding(10);
paddedView.Add(onTap);
ViewBuilderViewBuilder simplifies the construction of complex view hierarchies, allowing views to be created and nested declaratively.
ViewBuilder enables recursive building of views and applies modifiers to each view.Build() to finalize and retrieve the structured view.Example:
var view = new ViewBuilder {
new VStack {
new Image("logo"),
new Text("Welcome", "Arial"),
new Padding(10) {
new Background(Color.Gray) {
new Text("Start Game", "Arial")
}
}
}
}.Build();
ViewHelperViewHelper provides utility functions, such as EnsureCorrectViewType<T>, to verify view types at runtime.
var correctView = ViewHelper.EnsureCorrectViewType<Text>(someView);
CalculationStrategyBase defines methods for layout calculations, handling proposed and desired sizes, and arranging child views.
InteractionStrategyBase provides input-handling methods for views, enabling interactive responses to user actions.
RenderStrategyBase manages view rendering, enabling custom drawing logic for different view types.
GraphicOverlayCalculatorManages layout calculations for each view using specific calculation strategies.
GraphicOverlayInteractorProcesses input across views with interaction strategies for gesture and input handling.
GraphicOverlayRendererDraws each view using assigned render strategies for flexible UI rendering.
StartMenuViewThe StartMenuView class demonstrates how to create a custom view with nested structures using ViewBuilder and built-in views.
public class StartMenuView : View
{
protected override View BuildBody()
{
return new ViewBuilder {
new VStack
{
new Image("heart"),
new OnTapGesture(() =>
{
TheaterKit.SceneManager.Instance.StageAsync(GlobalSceneTag.Game);
})
{
new Background(Color.Red) {
new Padding(10) {
new Text("Hallo", "Arial")
}
}
},
new Text("Was Geht", "Arial"),
}
}.Build();
}
}
In this example:
VStack vertically arranges Image and Text views.OnTapGesture wraps a nested Background with padding, which triggers an action upon tapping.BuildBody() uses ViewBuilder to construct and return the entire view hierarchy.SurfaceKit is licensed under the MIT License.
SurfaceKit offers a flexible, extensible framework for creating user interfaces in MonoGame. With tools like ViewBuilder and strategy-driven graphics overlays, SurfaceKit provides powerful options for managing complex UI layouts and interactions.