ebitenlg

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 9 Imported by: 0

README

ebitenlg

demo

A lightweight floating GUI panel library ported to Ebitengine. Inspired by the web-based lil-gui, this Go implementation allows seamless integration with Ebitengine.

Features

  • Lightweight and easy-to-use floating GUI panels
  • Seamless integration with Ebitengine

Usage

First, create a new GUI panel using ebitenlg.NewGUI(), then add controls to it. In your main loop, call the Update() and Draw() methods to render the GUI.

package main

import (
	"image/color"
	_ "image/png"

	"github.com/demouth/ebitenlg"
	"github.com/hajimehoshi/ebiten/v2"
)

const (
	screenWidth  = 600
	screenHeight = 600
)

type Game struct {
	gui *ebitenlg.GUI
}

func (g *Game) Update() error {
	g.gui.Update()
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	screen.Fill(color.NRGBA{0x66, 0x66, 0x66, 0xff})
	g.gui.Draw(screen)
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
	return screenWidth, screenHeight
}

func main() {
	game := &Game{}

	gui := ebitenlg.NewGUI()
	gui.X = screenWidth
	gui.HorizontalAlign = ebitenlg.HorizontalAlignRight

	var default64, min64, max64 float64 = 0.05, -0.1, 0.1
	gui.AddSliderFloat64("float64", default64, min64, max64, func(v float64) {
		// v is the value of the slider
	})

	var default32, min32, max32 float32 = 10.0, -5.0, 20.0
	gui.AddSliderFloat32("float32", default32, min32, max32, func(v float32) {
		// v is the value of the slider
	})

	var defaultInt, minInt, maxInt int = 0, -500, 500
	gui.AddSliderInt("int", defaultInt, minInt, maxInt, func(v int) {
		// v is the value of the slider
	})

	var defaultBool bool = true
	gui.AddButton("button", defaultBool, func(v bool) {
		// v is the value of the button
	})
	game.gui = gui

	ebiten.SetWindowSize(screenWidth, screenHeight)
	ebiten.SetWindowTitle("Ebitengine lil GUI")
	ebiten.RunGame(game)
}

Adding Controllers

slider

To add a float64 slider, use the following code:

var default64, min64, max64 float64 = 0.05, -0.1, 0.1
gui.AddSliderFloat64("float64", default64, min64, max64, func(v float64) {
    // v is the value of the slider
})

float64

For a float32 slider, use the following code:

var default32, min32, max32 float32 = 10.0, -5.0, 20.0
gui.AddSliderFloat32("float32", default32, min32, max32, func(v float32) {
    // v is the value of the slider
})

float32

To add an int slider, use the following code:

var defaultInt, minInt, maxInt int = 0, -500, 500
gui.AddSliderInt("int", defaultInt, minInt, maxInt, func(v int) {
    // v is the value of the slider
})

int

button

To add a button, set a bool value as follows:


var defaultBool bool = true
gui.AddButton("button", defaultBool, func(v bool) {
    // v is the value of the button
})

bool

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DrawSliderShape

func DrawSliderShape[T float32 | float64 | int](s *slider[T], image *ebiten.Image, whiteImage *ebiten.Image, left, top, width, height, scale float32)

func UpdateSlider

func UpdateSlider[T float32 | float64 | int](s *slider[T], x, y, width, height, scale float32)

Types

type Component

type Component interface {
	Label() string
	Update(x, y, width, height, scale float32)
	Draw(image *ebiten.Image, whiteImage *ebiten.Image, top, left, width, height, scale float32)
}

type GUI

type GUI struct {
	X               float32
	Y               float32
	Width           float32
	ComponentHeight float32
	Scale           float32

	HorizontalAlign HorizontalAlign
	// contains filtered or unexported fields
}

func NewGUI

func NewGUI() *GUI

func (*GUI) AddButton

func (g *GUI) AddButton(label string, value bool, callback func(v bool))

func (*GUI) AddSliderFloat32

func (g *GUI) AddSliderFloat32(label string, value, min, max float32, callback func(v float32))

func (*GUI) AddSliderFloat64

func (g *GUI) AddSliderFloat64(label string, value, min, max float64, callback func(v float64))

func (*GUI) AddSliderInt

func (g *GUI) AddSliderInt(label string, value, min, max int, callback func(v int))

func (*GUI) Draw

func (g *GUI) Draw(image *ebiten.Image)

func (*GUI) SetTitle

func (g *GUI) SetTitle(t string)

func (*GUI) Update

func (g *GUI) Update()

type HorizontalAlign

type HorizontalAlign int
const (
	HorizontalAlignLeft HorizontalAlign = iota
	HorizontalAlignRight
)

type RangeInterpolator

type RangeInterpolator[T float32 | float64 | int] struct {
	Min   T
	Max   T
	Value T
}

func (*RangeInterpolator[T]) Ratio

func (r *RangeInterpolator[T]) Ratio() float64

func (*RangeInterpolator[T]) SetRatio

func (r *RangeInterpolator[T]) SetRatio(ratio float64)

func (*RangeInterpolator[T]) String

func (r *RangeInterpolator[T]) String() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL