H073.SurfaceKit 1.0.1

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package H073.SurfaceKit --version 1.0.1                
NuGet\Install-Package H073.SurfaceKit -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="H073.SurfaceKit" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add H073.SurfaceKit --version 1.0.1                
#r "nuget: H073.SurfaceKit, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install H073.SurfaceKit as a Cake Addin
#addin nuget:?package=H073.SurfaceKit&version=1.0.1

// Install H073.SurfaceKit as a Cake Tool
#tool nuget:?package=H073.SurfaceKit&version=1.0.1                

SurfaceKit

SurfaceKit 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.

Features

  • Container-Based Layouts: Arrange UI elements using HStack, VStack, and ZStack for horizontal, vertical, and layered arrangements.
  • Modifiers and Gestures: Enhance views with padding, backgrounds, and gestures like tap actions.
  • Graphics Overlay System: Customizable strategies for layout calculation, rendering, and interaction handling.
  • Input Management: Capture keyboard and mouse input with SurfaceInputManager.
  • View Building Tools: Construct complex view structures using ViewBuilder and ViewHelper.
  • Customizable UI Elements: Includes essential components like Text, Image, and Background.

Installation

To install SurfaceKit via NuGet:

dotnet add package SurfaceKit

Core Concepts

Views

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.

Container Views

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 });

Modifiers and Gestures

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);

View Building with ViewBuilder

ViewBuilder 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.
  • Use 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();

Utility Functions with ViewHelper

ViewHelper provides utility functions, such as EnsureCorrectViewType<T>, to verify view types at runtime.

var correctView = ViewHelper.EnsureCorrectViewType<Text>(someView);

Graphics Overlay System

Calculation Strategies

CalculationStrategyBase defines methods for layout calculations, handling proposed and desired sizes, and arranging child views.

Interaction Strategies

InteractionStrategyBase provides input-handling methods for views, enabling interactive responses to user actions.

Render Strategies

RenderStrategyBase manages view rendering, enabling custom drawing logic for different view types.

System Components

GraphicOverlayCalculator

Manages layout calculations for each view using specific calculation strategies.

GraphicOverlayInteractor

Processes input across views with interaction strategies for gesture and input handling.

GraphicOverlayRenderer

Draws each view using assigned render strategies for flexible UI rendering.

Example: Custom View with StartMenuView

The 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.

License

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.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4.6-preview 62 11/15/2024
1.0.4.5-preview 55 11/15/2024
1.0.4.4-preview 61 11/15/2024
1.0.4.3 66 11/15/2024
1.0.4.3-preview 55 11/15/2024
1.0.4.2 67 11/15/2024
1.0.4.1 70 11/14/2024
1.0.4 62 11/14/2024
1.0.3 69 11/13/2024
1.0.2 76 11/12/2024
1.0.1 74 11/12/2024