Skip to content

ButtonFactory

Moksh Mehta edited this page Oct 2, 2023 · 3 revisions

Here is a draft wiki page in Markdown format for the ButtonFactory class:

ButtonFactory Class

The ButtonFactory class provides static utility methods for creating TextButton UI components with different styles in a LibGDX game.

Usage

To use the ButtonFactory, simply call one of the static create methods, passing the button text and other parameters:

TextButton button = ButtonFactory.createButton("Play");

This will create a TextButton with the default style using the default skin.

The ButtonFactory has methods to create buttons with:

  • The default style
  • A custom image
  • An image loaded from a TextureAtlas

Create Methods

createButton(String text)

Creates a TextButton with the specified text using the default skin and style.

  • text - The text to display on the button

createCustomButton(String text, String imagePath)

Creates a custom TextButton with the specified text and custom image.

  • text - The text to display
  • imagePath - Path to the custom button image

createCustomButtonWithAtlas(String text, String atlasPath)

Creates a custom TextButton loading the button image from a TextureAtlas.

  • text - The text to display
  • atlasPath - Path to the TextureAtlas

Default Skin

The ButtonFactory initializes a default skin on startup that provides the button style, font, etc.

The default skin loads the font from configs/text.json and sets the button background image to images/ui/Sprites/UI_Glass_Button_Large_Lock_01a1.png.

The create methods that don't specify a custom image will use this default skin and style.

Custom Buttons

To create a button with a custom image, use the createCustomButton() method, passing the path to your custom PNG image.

To use images packed into a TextureAtlas, use createCustomButtonWithAtlas(), passing the atlas file path. This allows sharing images across multiple buttons.

Font Scaling

All buttons created clamp the font scale to 0.8f by default to make the text legible.

Padding

A padding of 10px is applied to all buttons by default.

Examples

// Default button
TextButton playBtn = ButtonFactory.createButton("Play"); 

// Custom image button
TextButton exitBtn = ButtonFactory.createCustomButton("Exit", "images/buttons/exit.png");

// Button from texture atlas
TextButton nextBtn = ButtonFactory.createCustomButtonWithAtlas("Next", "images/ui/atlas.atlas");

This provides a flexible way to create text buttons with custom styles and images for a game UI using LibGDX.

Clone this wiki locally