vpad

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: Apache-2.0 Imports: 5 Imported by: 1

README

Ebiten Virtual Pad

This is a package for displaying virtual directional keys and trigger buttons on the screen to handle user input.
It is intended to be used in games that use ebiten.

How to use

Please check the sample app for more detail.

Basic

Use the NewDirectionalPad and NewTriggerButton functions of the vpad package to generate the buttons.

import (
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	vpad "github.com/kemokemo/ebiten-virtualpad"
)

const (
	screenWidth  = 480
	screenHeight = 320
)

func NewGame() *Game {
	dpad := vpad.NewDirectionalPad(directional_pad, directional_button, vpad.SelectColor)
	dpad.SetLocation(10, 180)

	// vpad.Pressing is continuously triggered while this button is being pressed.
	aButton := vpad.NewTriggerButton(a_button, vpad.Pressing, vpad.SelectColor)
	aButton.SetLocation(150, 190)

	// vpad.JustRelease is triggered when this button is released.
	bButton := vpad.NewTriggerButton(b_button, vpad.JustReleased, vpad.SelectColor)
	bButton.SetLocation(260, 190)

	// vpad.JustPress is triggered when this button is pressed.
	cButton := vpad.NewTriggerButton(c_button, vpad.JustPressed, vpad.SelectColor)
	cButton.SetLocation(370, 190)

	return &Game{dpad: dpad, aButton: aButton, bButton: bButton, cButton: cButton}
}

type Game struct {
	dpad    vpad.DirectionalPad
	aButton vpad.TriggerButton
	bButton vpad.TriggerButton
	cButton vpad.TriggerButton
}
Get direction and triggered state

Update the internal state with the Update function, and get the direction or trigger presence with the GetDirection or IsTriggered function.

func (g *Game) Update() error {
	g.dpad.Update()
	g.aButton.Update()
	g.bButton.Update()
	g.cButton.Update()

	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	g.dpad.Draw(screen)
	d := g.dpad.GetDirection()
	switch d {
	case vpad.Upper:
		ebitenutil.DebugPrintAt(screen, "Upper direction", 20, 20)
	case vpad.Lower:
		ebitenutil.DebugPrintAt(screen, "Lower direction", 20, 20)
	case vpad.Left:
		ebitenutil.DebugPrintAt(screen, "Left direction", 20, 20)
	case vpad.Right:
		ebitenutil.DebugPrintAt(screen, "Right direction", 20, 20)
	}

	g.aButton.Draw(screen)
	if g.aButton.IsTriggered() {
		ebitenutil.DebugPrintAt(screen, "A button is being triggered.", 20, 50)
	}

	g.bButton.Draw(screen)
	if g.bButton.IsTriggered() {
		ebitenutil.DebugPrintAt(screen, "B button is triggered.", 20, 50)
	}

	g.cButton.Draw(screen)
	if g.cButton.IsTriggered() {
		ebitenutil.DebugPrintAt(screen, "C button is triggered.", 20, 50)
	}
}
Various triggers

The following types of TriggerButtons are currently available.

Type Description
JustReleased This is triggered only when just released.
Pressing This is triggered while this is being pressed.
JustPressed This is triggered only when just pressed.

License

Apache-2.0 License

Author

kemokemo

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SelectColor = color.RGBA{0, 148, 255, 255}

Functions

This section is empty.

Types

type Direction

type Direction int

Direction is the direction for the pad.

const (
	// None describes that no direction buttons are pressed.
	None Direction = iota
	// Left is left direction
	Left
	// Right is right direction.
	Right
	// Upper is the upper direction.
	Upper
	// UpperLeft is the upper left direction
	UpperLeft
	// UpperRight is the upper right direction
	UpperRight
	// Lower is the lower direction.
	Lower
	// LowerLeft is the lower left direction
	LowerLeft
	// LowerRight is the lower right direction
	LowerRight
)

type DirectionalPad

type DirectionalPad struct {
	// contains filtered or unexported fields
}

DirectionalPad is the directional pad for a game.

func NewDirectionalPad

func NewDirectionalPad(pad, button *ebiten.Image, cl color.RGBA) DirectionalPad

NewDirectionalPad returns a new DirectionalPad.

func (*DirectionalPad) Draw

func (dp *DirectionalPad) Draw(screen *ebiten.Image)

Draw draws the directional buttons belong this struct.

func (*DirectionalPad) GetDirection

func (dp *DirectionalPad) GetDirection() Direction

GetDirection returns the currently selected direction.

func (*DirectionalPad) SetLocation

func (dp *DirectionalPad) SetLocation(x, y int)

SetLocation sets the location to draw this directional pad.

func (*DirectionalPad) Update

func (dp *DirectionalPad) Update()

Update updates the internal status of this struct.

type SelectButton added in v0.3.0

type SelectButton interface {
	SetLocation(x, y int)
	Update()
	IsSelected() bool
	Draw(screen *ebiten.Image)
	SetSelectState(selected bool)
	SetSelectKeys(keys []ebiten.Key)
	SetSelectImage(img *ebiten.Image)
}

func NewSelectButton added in v0.3.0

func NewSelectButton(img *ebiten.Image, tt TriggerType, cl color.RGBA) SelectButton

NewSelectButton returns a new SelectButton. Argument 'TriggerType' specifies the operation for which this button will be selected. Only 'JustReleased' and 'JustPressed' are available. If you specify others, this func returns nil.

type TriggerButton

type TriggerButton interface {
	SetLocation(x, y int)
	Update()
	IsTriggered() bool
	Draw(screen *ebiten.Image)
	SetTriggerButton(keys []ebiten.Key)
}

func NewTriggerButton

func NewTriggerButton(img *ebiten.Image, tt TriggerType, cl color.RGBA) TriggerButton

NewTriggerButton returns a new TriggerButton.

type TriggerType

type TriggerType int

TriggerType is the type for the TriggerButton.

const (
	// JustReleased is triggered only when just released.
	JustReleased TriggerType = iota
	// Pressing is triggered while this is being pressed.
	Pressing
	// JustPressed is triggered only when just pressed.
	JustPressed
)

Directories

Path Synopsis
cmd module

Jump to

Keyboard shortcuts

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