Skip to main content

FlixelBar

View source

class

org.flixelgdx.util.FlixelBar

public class FlixelBar extends FlixelSprite

A UI bar for progress, health, stamina, experience, cooldowns, loading, or any value mapped to a numeric range. It extends FlixelSprite so you can add instances to a FlixelSpriteGroup, use sprite transforms (position, scale, rotation, tint, alpha) with the rest of your HUD, and rely on the same camera and lifecycle rules as other sprites.

Rendering: The bar draws custom geometry with a shared white-pixel texture and optional TextureRegion backgrounds and fills. It does not use FlixelSprite.loadGraphic(FileHandle); those entry points are blocked so a bar never accidentally shows a loaded texture on top of the bar UI.

Value and range: You set a logical range with FlixelBar.setRange(float, float) and either FlixelBar.setValue(float) or FlixelBar.setTrack(FloatSupplier) so the fill updates each frame. Use FlixelBar.setMaxSupplier(FloatSupplier) when the maximum changes at runtime (for example leveling systems) without calling setRange manually.

Fill direction: BarFillDirection controls whether the fill grows left-to-right, right-to-left, top-to-bottom, or bottom-to-top.

Smoothing: FlixelBar.setLerp(float) applies frame-rate independent smoothing to the displayed value so the bar can lag slightly behind the target, similar to camera follow smoothing in FlixelCamera.

Appearance: Solid colors, custom empty and filled regions, optional two-color gradients, optional border, and threshold-based fill colors with optional color smoothing when the fill percent drops. Optional overlay text is set with FlixelBar.setText(CharSequence); pass a reused FlixelString for allocation-free live labels (see that method for why).

Screen space: With FlixelBar.setScreenSpace(boolean) true, the bar is offset by the current draw camera scroll so it stays fixed on screen while the world moves.

Constructors

FlixelBar(float, float, float, float)

public FlixelBar(float x, float y, float width, float height)

Creates a bar at the given world position with the given hitbox size. No texture is loaded on the sprite; all visuals come from the bar configuration API.

Parameters:

NameDescription
xLeft edge in world space (or screen-anchored space if FlixelBar.setScreenSpace(boolean) is used).
yTop edge (Flixel convention: Y down).
widthBar width in pixels; used as the drawable width and for gradient resolution hints.
heightBar height in pixels.

Fields

DEFAULT_MIN

public static final float DEFAULT_MIN = 0.0f

DEFAULT_MAX

public static final float DEFAULT_MAX = 100.0f

text

public FlixelText text

The text object used to display the overlay text.


Methods

loadGraphic(Texture, int, int)

public final FlixelSprite loadGraphic(Texture texture, int frameWidth, int frameHeight)

makeGraphic(int, int, Color)

public final FlixelSprite makeGraphic(int width, int height, Color color)

setScreenSpace(boolean)

public FlixelBar setScreenSpace(boolean screenSpace)

When true, each draw adds the current Flixel.getDrawCamera() scroll to the bar position so the bar stays fixed on the monitor while the camera moves. When false, the bar uses normal sprite coordinates (moves with the world).

Parameters:

NameDescription
screenSpacetrue to pin to the viewport in screen space; false for world space.

Returns: this for chaining.


setFillDirection(BarFillDirection)

public FlixelBar setFillDirection(BarFillDirection direction)

Sets which edge of the bar is the fill origin and which axis the fill grows along. Changing this may rebuild the internal gradient texture if a gradient is enabled.

Parameters:

NameDescription
directionOne of BarFillDirection; must not be null.

Returns: this for chaining.


getFillDirection()

public BarFillDirection getFillDirection()

setRange(float, float)

public FlixelBar setRange(float min, float max)

Sets the inclusive logical range [min, max] used to map FlixelBar.getValue() to fill percent. If max is less than min, the two are swapped. Current and displayed values are clamped into the new range.

Parameters:

NameDescription
minLower bound of the value range (for example 0 for health).
maxUpper bound (for example max HP). Must define a positive span after ordering for a non-zero fill.

Returns: this for chaining.


getMin()

public float getMin()

getMax()

public float getMax()

setMaxSupplier(FloatSupplier)

public FlixelBar setMaxSupplier(FloatSupplier maxSupplier)

Supplies a new maximum each frame (for example current max HP from a stats object). When non-null, FlixelBar.update(float) calls FlixelBar.setRange(float, float) with the current minimum and the supplied max so the bar stays consistent when max changes without manual range updates.

Parameters:

NameDescription
maxSuppliernull to use only FlixelBar.setRange(float, float); otherwise polled each update.

Returns: this for chaining.


getMaxSupplier()

public FloatSupplier getMaxSupplier()

setValue(float)

public FlixelBar setValue(float value)

Sets the target value when not using FlixelBar.setTrack(FloatSupplier). Ignored while a track supplier is set. If FlixelBar.getLerp() is 1, the displayed value snaps immediately; otherwise the displayed value catches up in FlixelBar.update(float).

Parameters:

NameDescription
valueLogical value clamped to the current FlixelBar.setRange(float, float).

Returns: this for chaining.


getValue()

public float getValue()

getDisplayedValue()

public float getDisplayedValue()

setTrack(FloatSupplier)

public FlixelBar setTrack(FloatSupplier supplier)

When non-null, FlixelBar.update(float) sets the target value from FloatSupplier.getAsFloat() each frame (for example player::getHealth). When null, FlixelBar.setValue(float) drives the bar. Primitive supplier avoids boxing.

Parameters:

NameDescription
suppliernull for manual values; otherwise the polled value source.

Returns: this for chaining.


getTrack()

public FloatSupplier getTrack()

setLerp(float)

public FlixelBar setLerp(float lerp)

Smoothing factor for the value animation: 1 means the displayed fill matches the target immediately; values between 0 and 1 apply exponential smoothing scaled by elapsed time and target framerate (same idea as camera follow lerp in FlixelCamera). This is separate from FlixelBar.setThresholdSmoothing(float, boolean) which only affects threshold colors.

Parameters:

NameDescription
lerpSmoothing amount in [0, 1]; clamped if out of range.

Returns: this for chaining.


getLerp()

public float getLerp()

setEmptyColor(Color)

public FlixelBar setEmptyColor(Color c)

Sets the tint for the empty (background) strip when no FlixelBar.setEmptyGraphic(TextureRegion) is set. Clears any empty graphic region so the bar uses solid color for the background again.

Parameters:

NameDescription
clibGDX color; not null.

Returns: this for chaining.


setEmptyColor(FlixelColor)

public FlixelBar setEmptyColor(FlixelColor c)

Same as FlixelBar.setEmptyColor(Color) using a FlixelColor object.

Parameters:

NameDescription
cEngine color wrapper; not null.

Returns: this for chaining.


setFilledColor(Color)

public FlixelBar setFilledColor(Color c)

Sets the tint for the filled portion when no FlixelBar.setFilledGraphic(TextureRegion) or gradient is used, unless threshold coloring overrides the fill color. Clears any filled graphic region.

Parameters:

NameDescription
cLibGDX color; not null.

Returns: this for chaining.


setFilledColor(FlixelColor)

public FlixelBar setFilledColor(FlixelColor c)

Same as FlixelBar.setFilledColor(Color) using FlixelColor.

Parameters:

NameDescription
cEngine color wrapper; not null.

Returns: this for chaining.


setEmptyGraphic(TextureRegion)

public FlixelBar setEmptyGraphic(TextureRegion region)

Uses a texture region for the full empty background stretched to the bar size. Set null to fall back to FlixelBar.setEmptyColor(Color).

Parameters:

NameDescription
regionEmpty-bar art, or null for solid FlixelBar.setEmptyColor(Color).

Returns: this for chaining.


setFilledGraphic(TextureRegion)

public FlixelBar setFilledGraphic(TextureRegion region)

Uses a texture region for the fill; the bar crops UVs so only a fraction matching the current percent is shown. Set null to use FlixelBar.setFilledColor(Color) or FlixelBar.setGradient(Color, Color).

Parameters:

NameDescription
regionFill art, or null for color or gradient fill.

Returns: this for chaining.


setGradient(Color, Color)

public FlixelBar setGradient(Color start, Color end)

Enables a two-color linear gradient for the filled portion along the fill axis. Either argument null disables the gradient and restores solid or textured fill. Rebuilds an internal gradient texture when size or FlixelBar.setFillDirection(BarFillDirection) changes.

Use this if you need something like a health bar, where at the start of the bar the color is green, and at the end of the bar the color is red.

Parameters:

NameDescription
startColor at the start of the gradient axis (left or bottom of the fill direction).
endColor at the end of the gradient axis.

Returns: this for chaining.


setGradient(FlixelColor, FlixelColor)

public FlixelBar setGradient(FlixelColor start, FlixelColor end)

Same as FlixelBar.setGradient(Color, Color) using FlixelColor.

Parameters:

NameDescription
startStart color, or null to help disable the gradient.
endEnd color, or null to help disable the gradient.

Returns: this for chaining.


setBorder(Color, float)

public FlixelBar setBorder(Color color, float thickness)

Draws a simple axis-aligned frame by tinting four rectangles. Pass null color or non-positive thickness to draw no border (or use FlixelBar.clearBorder()).

Parameters:

NameDescription
colorBorder tint; null clears the border.
thicknessWidth of each border strip in pixels; values below zero are clamped to zero.

Returns: this for chaining.


clearBorder()

public FlixelBar clearBorder()

Removes the border drawn by FlixelBar.setBorder(Color, float).

Returns: this for chaining.


setThresholdColors(Color, Color, float)

public FlixelBar setThresholdColors(Color fullColor, Color lowColor, float lowPercent)

Convenience for two-stop threshold coloring: from lowColor at lowPercent up to fullColor at 100% fill. Replaces any previous threshold stops from FlixelBar.setThresholdStops(Collection<? extends ThresholdStop>).

Parameters:

NameDescription
fullColorColor used when fill percent is at or above the top stop (full bar).
lowColorColor blended in below lowPercent.
lowPercentFill fraction in [0,1] where the low color applies; clamped if out of range.

Returns: this for chaining.


setThresholdStops(Collection<? extends ThresholdStop>)

public FlixelBar setThresholdStops(Collection<? extends ThresholdStop> stops)

Replaces threshold stops from a Collection. Values are copied into an internal Array and sorted by percent. Null entries are skipped.

For List implementations that also implement RandomAccess, copying uses index loops and avoids iterator allocation on this (typically rare) call. For a libGDX Array, use FlixelBar.setThresholdStops(Array<ThresholdStop>).

Parameters:

NameDescription
stopsNon-null collection; may be empty to clear thresholds.

Returns: this for chaining.


setThresholdStops(Array<ThresholdStop>)

public FlixelBar setThresholdStops(Array<ThresholdStop> stops)

Same as FlixelBar.setThresholdStops(Collection<? extends ThresholdStop>) but reads stops from a libGDX Array by index (no iterator on the source).

Parameters:

NameDescription
stopsNon-null libGDX array; null entries are skipped.

Returns: this for chaining.


clearThresholds()

public FlixelBar clearThresholds()

Disables threshold-based fill coloring so the bar uses FlixelBar.setFilledColor(Color) or gradient only.

Returns: this for chaining.


setThresholdSmoothing(float)

public FlixelBar setThresholdSmoothing(float lerp)

Same as FlixelBar.setThresholdSmoothing(float, boolean) with decrease-only smoothing enabled.

Parameters:

NameDescription
lerpSmoothing factor in [0,1] for threshold color transitions.

Returns: this for chaining.


setThresholdSmoothing(float, boolean)

public FlixelBar setThresholdSmoothing(float lerp, boolean onDecreaseOnly)

When lerp is below 1, the displayed threshold color eases toward the target color each frame. If onDecreaseOnly is true, smoothing applies when fill percent drops (typical for damage feedback); if false, color also smooths when the percent increases.

Parameters:

NameDescription
lerpSmoothing amount in [0,1]; clamped if out of range.
onDecreaseOnlytrue to smooth mainly on falling health; false to smooth on any change.

Returns: this for chaining.


setText(CharSequence)

public FlixelBar setText(CharSequence label)

Sets an optional label rendered on top of the bar. Text is centered on the bar by default; use FlixelBar.setTextOffset(float, float) to nudge it. Pass null to remove overlay text.

The bar keeps the CharSequence you pass and re-reads it each FlixelBar.update(float), so for live text (a score, a percentage) hand it a FlixelString that you mutate in place: the bar picks up the new characters without you allocating a fresh String every frame, compares them to the last label, and only calls FlixelText.setText(CharSequence) when they actually change. For static text, a plain String works just as well.

Parameters:

NameDescription
labelnull for no overlay; otherwise the label source (often a reused FlixelString).

Returns: this for chaining.


setTextOffset(float, float)

public FlixelBar setTextOffset(float dx, float dy)

Pixel offset added to the centered text position after FlixelBar.setText(CharSequence).

Parameters:

NameDescription
dxHorizontal offset in pixels (positive moves right).
dyVertical offset in pixels (positive moves down in Flixel coordinates).

Returns: this for chaining.


update(float)

public void update(float elapsed)

Updates sprite animation state, then applies max supplier, value tracking, value smoothing, and refreshes overlay text from the FlixelBar.setText(CharSequence) source when set.

Parameters:

NameDescription
elapsedSeconds since last frame; passed to FlixelSprite.update(float) and smoothing.

draw(FlixelBatch)

public void draw(FlixelBatch batch)

destroy()

public void destroy()

ThresholdStop

record

org.flixelgdx.util.FlixelBar.ThresholdStop

public static final record ThresholdStop(float percent, Color color)

One entry in a piecewise-linear threshold color ramp. At fill percent FlixelBar.percent the bar uses FlixelBar.color, interpolating between stops for values in between.

Constructors

ThresholdStop(float, Color)

public ThresholdStop(float percent, Color color)

Parameters:

NameDescription
percentFill fraction; clamped to [0,1].
colorStop color; copied internally.

Methods

toString()

public final String toString()

hashCode()

public final int hashCode()

equals(Object)

public final boolean equals(Object o)

percent()

public float percent()

color()

public Color color()

BarFillDirection

enum

org.flixelgdx.util.FlixelBar.BarFillDirection

public static final enum BarFillDirection

Fill direction for FlixelBar.

Fields

LEFT_TO_RIGHT

public static final BarFillDirection LEFT_TO_RIGHT

RIGHT_TO_LEFT

public static final BarFillDirection RIGHT_TO_LEFT

TOP_TO_BOTTOM

public static final BarFillDirection TOP_TO_BOTTOM

BOTTOM_TO_TOP

public static final BarFillDirection BOTTOM_TO_TOP

Methods

values()

public static BarFillDirection[] values()

valueOf(String)

public static BarFillDirection valueOf(String name)