Skip to main content

Python bindings for Gum UI (.NET runtime via PythonNET)

Project description

Gum UI

Gum is not an acronym, or a backronym. It is just the name of the User Interface engine.

This project utilizes the GUM Layout engine from the Flat Red Ball C# Open Source project here https://docs.flatredball.com/gum

If you want to contribute to the Layout engine, or this Python Rendering Engine visit us on github: https://github.com/vchelaru/Gum

Experimental Note

WARNING: This project is an experiment and proof of concept. The goal was to see if PythonNET could be used to export the C# GUM UI backend Layout Engine to be used in other tools like Python with PyGame.

Currently it "works" but there are many missing features and bugs.

Right now it only draws a RECTANGLE the encompasses the entire size of the GUM UI's GraphicalUiElement.

You can then add other objects to this root object (that are currently only drawing rectangles).

You can't control the color, it starts at (20, 20, 20) and increases in color intensity by 20 and wraps around at 256.

If this ends up being something that others want, please join the discord or put tickets into github.

Setup

Install gumui and pygame

  • pip install gumui
  • pip install pygame

Getting started with

There are really only 3 important things after you import. Create an Instance of GumUI, call Update, and call Draw()

  1. First you need to import the GumUI and PyGame
from gumui import GumUI, GraphicalUiElement, InvisibleRenderable, Converters
import pygame
from pygame.locals import *
  1. Next you need to initialize and configure pygame, might as well do it now
pygame.init()
BLACK = (0, 0, 0)
SCREEN_WIDTH = 680
SCREEN_HEIGHT = 480
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  1. Setup timing to force to 60 FPS (Frames Per Second)
fps_clock = pygame.time.Clock()
fps = 60.0
dt = fps_clock.tick(fps)
  1. Once PyGame is setup, you need to create the GumUI and initialize it
# Initialize GumUI
myUi = GumUI()
myUi.initialize(screen) # `screen` is a Surface you wish GUM to draw to https://www.pygame.org/docs/ref/surface.html
  1. Lets add a single item to the UI for now it will be simple 32x32 square
child = GraphicalUiElement(InvisibleRenderable())
myUi.RootElement.Children.Add(child)
child.XUnits = Converters.GeneralUnitType.PixelsFromSmall
child.Width = 32
child.YUnits = Converters.GeneralUnitType.PixelsFromSmall
child.Height = 32
  1. Now that you've defined the UI (1 square inside the root element) you can create the standard pygame loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

NOTE: This will run forever, or until you hit ESC or press the X

  1. Update and then Draw the UI elements
    myUi.update()
    screen.fill(BLACK)  # Fill the screen with black.
    myUi.draw()
    pygame.display.flip()
    dt = fps_clock.tick(fps)

Adding more UI elements

Currently this is a work in progress, as such, the only thing that is drawn are squares.

To add another UI element "square" you create an instance of GraphicalUiElement, set it's properties, and add it to a parent.

child = GraphicalUiElement(InvisibleRenderable())
myUi.RootElement.Children.Add(child)

child.X = 100
child.Y = 100
child.Width = 200
child.YUnits = Converters.GeneralUnitType.PixelsFromSmall
child.Height = 200

Adding grandchildren and great grandchildren

Here another child (100x100) is added to the larger (200x200) child from above.

grandChild = GraphicalUiElement(InvisibleRenderable())
child.Children.Add(grandChild)

grandChild.XUnits = Converters.GeneralUnitType.PixelsFromSmall
grandChild.X = 10
grandChild.Y = 10
grandChild.Width = 100
grandChild.YUnits = Converters.GeneralUnitType.PixelsFromSmall
grandChild.Height = 100

Notice how the (100x100) rectangle is drawn at (10, 10), but that position is relative to the parent so it's actually drawn at (110, 110)

To see the full list of properties available for a GraphicalUiElement see this documentation https://docs.flatredball.com/gum/code/gum-code-reference/graphicaluielement

Contributing

This is a FOSS (Free and Open Source Software) project.

Change Log

0.1.3

  • Added this changelog

0.1.2

  • Added missing import of Converts to the from line
  • Added a grandchild example

0.1.1

  • First proof of concept working release

0.1.0

  • Deleted first push attempt

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gumui-0.1.3.tar.gz (272.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gumui-0.1.3-py3-none-any.whl (279.3 kB view details)

Uploaded Python 3

File details

Details for the file gumui-0.1.3.tar.gz.

File metadata

  • Download URL: gumui-0.1.3.tar.gz
  • Upload date:
  • Size: 272.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.1

File hashes

Hashes for gumui-0.1.3.tar.gz
Algorithm Hash digest
SHA256 60849999a19f20103268657396d14e6d7a379a7c57d6183532cc45bf36007a40
MD5 4a7a0f0d6ef4433ce27e938f7d1baf8b
BLAKE2b-256 69c55f36bdbb2748aac9ff62c12f900cd24496b08487e75e1a9806d3116f014c

See more details on using hashes here.

File details

Details for the file gumui-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: gumui-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 279.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.1

File hashes

Hashes for gumui-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8257058335cac86319f8340bb11ce7e0cb7a92162dc32ddd4cf9a3f637aa811c
MD5 5c013d09f04c1044f46074c76b8f2736
BLAKE2b-256 6e697835d629ad893be6fe2688f110043a1c2b53461b9df0baeb5d9a7222fbc5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page