Class HudSegmentedControl<T>

Type Parameters:
T - the type of value each segment stands for
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible
Direct Known Subclasses:
HudModeSelector, HudTabStrip

public class HudSegmentedControl<T> extends JPanel
A segmented, mutually exclusive choice. Each segment has a tracked title and an optional sub-line that explains its effect (for example, how much money the choice makes or costs). The selected segment is raised with the map's accent underline; disabled segments are dimmed and ignore clicks.

Setting the segments or the selection never fires the listener; only the player's choices do.

This is also the one implementation behind HudTabStrip and HudModeSelector, which differ only in their HudSegmentedControl.SegmentStyle, so a change to how segments are drawn or handled is made here once.

Since:
0.51.01
See Also:
  • Constructor Details

    • HudSegmentedControl

      public HudSegmentedControl(Consumer<T> onChoose)
      Parameters:
      onChoose - called with the segment's value when the player chooses a segment
  • Method Details

    • setSegments

      public void setSegments(List<HudSegmentedControl.Segment<T>> segments)
      Replaces the control's segments, keeping the selection if its value is still offered.
      Parameters:
      segments - the segments, left to right
      Since:
      0.51.01
    • setSelected

      public void setSelected(@Nullable T value)
      Selects the segment for a value, without notifying the listener.
      Parameters:
      value - the value to select, or null to select nothing
      Since:
      0.51.01
    • getSelected

      @Nullable public T getSelected()
      Returns:
      the selected value, or null if nothing is selected
    • setSegmentEnabled

      public void setSegmentEnabled(T value, boolean enabled)
      Locks or unlocks the segment for a value. A locked segment is dimmed and ignores clicks and keys.
      Parameters:
      value - the segment's value; does nothing if no segment has it
      enabled - true to let the player choose the segment
    • setSegmentTitle

      public void setSegmentTitle(T value, String title)
      Replaces the title of the segment for a value, for example to show a count.
      Parameters:
      value - the segment's value; does nothing if no segment has it
      title - the new title
    • setColumns

      public void setColumns(int columns)
      Wraps the segments onto several rows. Call before setSegments(List).
      Parameters:
      columns - how many segments sit in each row, or 0 for a single row
      Since:
      0.51.01
    • setSegmentSub

      public void setSegmentSub(T value, String sub)
      Replaces the sub-line of the segment for a value, for example when the effect it describes changes.
      Parameters:
      value - the segment's value; does nothing if no segment has it, or if the segment was made without a sub-line
      sub - the new sub-line
      Since:
      0.51.01