Sunscreen

repository·v2·Indexed 42 days ago

https://github.com/combimagnetron/sunscreen

A library for managing interactive menus and UI elements in games or interactive environments. It provides abstractions for users, sessions, and active menus, featuring layout systems like FlowLayout and GroupLayout, and customizable backgrounds via the Background interface.

Tokens
1.5K
Snippets
6
Records
9
Agent score
54%

What's inside Sunscreen

  1. Define GUI backgrounds using the Background interface

    v2

    The Background interface is used to define the visual background for a MenuRoot. It extends Renderable<Size, Canvas> and MenuComponent, meaning a background is a component that renders a Canvas based on a provided Size property.

    Implementations of Background are used to provide different visual styles, such as solid colors or tiled patterns, to the menu interface.

  2. Move an element in an ActiveMenu

    v2

    To move an element within a Sunscreen menu, you must access the ActiveMenu through the user's current Session, locate the element using its Identifier, and then apply a new Position.

    In the CLI implementation, the move subcommand demonstrates moving an element with the ID aaa to the coordinates (100, 200) using Position.fixed().

    // Logic used by the 'sunscreen move' command
    ElementLike<?> elementLike = menu.element(Identifier.of("aaa"));
    elementLike.position(Position.fixed(Vec2i.of(100, 200)));
  3. Manage elements in a Layout

    v2

    Both FlowLayout and GroupLayout implement the ElementContainer interface, allowing you to manage child elements using the following methods:

    • add(L elementLike): Adds a single element to the layout.
    • add(Iterable<L> elementLikes): Adds multiple elements.
    • remove(L elementLike): Removes a specific element.
    • remove(Identifier identifier): Removes elements whose identifier matches the provided Identifier (using Identifier.TestDepth.WHOLE).
    • children(): Returns a collection of all child elements.
  4. Create a GroupLayout

    v2

    Use Layout.group(Identifier identifier, ...) to create a GroupLayout. A GroupLayout positions its child elements based on their individual position properties relative to the layout's own position. Unlike FlowLayout, it does not automatically wrap or sequence elements; it acts as a container that preserves and offsets the coordinates of its children.

    // Create a group layout with an identifier and initial elements
    GroupLayout<?> layout = Layout.group(myIdentifier, element1, element2);
    
    // Or create an empty group layout and add elements later
    GroupLayout<?> layout = Layout.group(myIdentifier);
    layout.add(element3);
  5. Create a FlowLayout

    v2

    Use Layout.flow(Identifier identifier, ...) to create a FlowLayout. A FlowLayout arranges its child elements in a sequence, wrapping to a new row when the horizontal space of the layout's size is exceeded. It calculates row heights based on the tallest element in each row.

    // Create a flow layout with an identifier and initial elements
    FlowLayout<?> layout = Layout.flow(myIdentifier, element1, element2);
    
    // Or create an empty flow layout and add elements later
    FlowLayout<?> layout = Layout.flow(myIdentifier);
    layout.add(element3);
  6. Configure Layout properties

    v2

    Layouts can store and retrieve custom properties using the property(Class<P> propertyClass) and property(Property<T, C> property) methods. This allows you to attach metadata or configuration objects to the layout instance.

    // Setting a property
    layout.property(myCustomProperty);
    
    // Getting a property
    MyProperty prop = layout.property(MyProperty.class);
  7. Use Sunscreen CLI commands

    v2

    Sunscreen provides a command-line interface accessible via the sunscreen base command. The following subcommands are available:

    • sunscreen: The base command. In the current implementation, it initializes an ActiveMenu using a TestMenuTemplate for the executing user.
    • sunscreen add: Attempts to retrieve the current ActiveMenu from the user's active Session.
    • sunscreen move: Moves a specific UI element within the active menu. It targets an element by its Identifier and updates its Position to a fixed coordinate.
    ### Available Commands
    
    | Command | Description |
    | --- | --- |
    | `sunscreen` | Base command; initializes a test menu. |
    | `sunscreen add` | Accesses the user's current menu session. |
    | `sunscreen move` | Moves an element (e.g., `Identifier.of("aaa")`) to a fixed position. |
  8. Use Solid color backgrounds

    v2

    The Solid implementation of Background provides a single, uniform color across the entire background area. It uses the render method to create an empty Canvas of the specified Size and fills it with the provided Color starting from Position.nil().

    // Note: Solid constructor is protected in the source, 
    // implying it is intended to be instantiated via a factory or specific loader.
    Background<Solid> background = new Solid(Color.WHITE);