Skip to content

Commit

Permalink
Add horizontal Menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightm4re94 committed Aug 5, 2024
1 parent 9ea870e commit 49bd374
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* Represents a list of image components in a GUI. This class provides functionality to manage and display a list of image components.
*/
public class ImageComponentList extends GuiComponent {

private final Spritesheet background;
Expand All @@ -18,15 +21,8 @@ public class ImageComponentList extends GuiComponent {
private double yOffset;
private boolean initialized;

public ImageComponentList(
final double x,
final double y,
final double width,
final double height,
final int rows,
final int columns,
final List<Image> images,
final Spritesheet background) {
public ImageComponentList(final double x, final double y, final double width, final double height, final int rows, final int columns,
final List<Image> images, final Spritesheet background) {
super(x, y, width, height);
if (images != null) {
this.images = images;
Expand Down Expand Up @@ -56,28 +52,52 @@ public ImageComponentList(
}
}

/**
* Gets the background spritesheet of the image component list.
*
* @return The background spritesheet.
*/
public Spritesheet getBackground() {
return this.background;
}

/**
* Gets the list of image components (cells) in the image component list.
*
* @return The list of image components.
*/
public List<ImageComponent> getCellComponents() {
return this.cells;
}

/**
* Gets the number of columns in the image component list.
*
* @return The number of columns.
*/
public int getColumns() {
return this.columns;
}

/**
* Gets the list of images in the image component list.
*
* @return The list of images.
*/
public List<Image> getImages() {
return this.images;
}

/**
* Gets the number of rows in the image component list.
*
* @return The number of rows.
*/
public int getRows() {
return this.rows;
}

@Override
public void prepare() {
@Override public void prepare() {

if (!initialized) {
int imageCount = -1;
Expand All @@ -92,14 +112,8 @@ public void prepare() {
img = null;
}
final ImageComponent cell =
this.createNewEntry(
this.getX() + i * (this.getColumnWidth() + this.xOffset),
this.getY() + j * (this.getRowHeight() + this.yOffset),
this.getColumnWidth(),
this.getRowHeight(),
this.getBackground(),
"",
img);
this.createNewEntry(this.getX() + i * (this.getColumnWidth() + this.xOffset), this.getY() + j * (this.getRowHeight() + this.yOffset),
this.getColumnWidth(), this.getRowHeight(), this.getBackground(), "", img);
this.cells.add(cell);
}
}
Expand All @@ -111,38 +125,74 @@ public void prepare() {
super.prepare();
}

/**
* Gets the height of each row in the image component list.
*
* @return The height of each row.
*/
public double getRowHeight() {
return this.rowHeight;
}

/**
* Sets the height of each row in the image component list.
*
* @param rowHeight The height to set for each row.
*/
public void setRowHeight(double rowHeight) {
this.rowHeight = rowHeight;
}

/**
* Gets the width of each column in the image component list.
*
* @return The width of each column.
*/
public double getColumnWidth() {
return this.columnWidth;
}

/**
* Sets the width of each column in the image component list.
*
* @param columnWidth The width to set for each column.
*/
public void setColumnWidth(double columnWidth) {
this.columnWidth = columnWidth;
}

/**
* Sets the horizontal offset for the image component list.
*
* @param xOffset The horizontal offset to set.
*/
public void setXOffset(final double xOffset) {
this.xOffset = xOffset;
}

/**
* Sets the vertical offset for the image component list.
*
* @param yOffset The vertical offset to set.
*/
public void setYOffset(final double yOffset) {
this.yOffset = yOffset;
}

protected ImageComponent createNewEntry(
final double x,
final double y,
final double width,
final double height,
final Spritesheet spritesheet,
final String text,
final Image image) {
/**
* Creates a new image component entry.
*
* @param x The x-coordinate of the new entry.
* @param y The y-coordinate of the new entry.
* @param width The width of the new entry.
* @param height The height of the new entry.
* @param spritesheet The spritesheet to be used for the new entry.
* @param text The text to be displayed on the new entry.
* @param image The image to be displayed on the new entry.
* @return The newly created image component.
*/
protected ImageComponent createNewEntry(final double x, final double y, final double width, final double height, final Spritesheet spritesheet,
final String text, final Image image) {
return new ImageComponent(x, y, width, height, spritesheet, text, image);
}
}
124 changes: 100 additions & 24 deletions litiengine/src/main/java/de/gurkenlabs/litiengine/gui/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,139 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.IntConsumer;

/** The Class Menu. */
/**
* Represents a menu component that extends the ImageComponentList. This class provides functionality to create a menu with selectable items.
*/
public class Menu extends ImageComponentList {

private int currentSelection;
/** The menu buttons. */

private final String[] items;

private final List<IntConsumer> selectionChangeConsumers;

private final Orientation orientation;

/**
* Constructs a new Menu with the specified position, size, and orientation.
*
* @param x The x-coordinate of the menu.
* @param y The y-coordinate of the menu.
* @param width The width of the menu.
* @param height The height of the menu.
* @param orientation The orientation of the menu (horizontal or vertical).
* @param items The items to be displayed in the menu.
*/
public Menu(
final double x,
final double y,
final double width,
final double height,
final String... items) {
this(x, y, width, height, null, items);
double x,
double y,
double width,
double height,
Orientation orientation,
String... items) {
this(x, y, width, height, null, orientation, items);
}

/**
* Constructs a new Menu with the specified position, size, and items.
*
* @param x The x-coordinate of the menu.
* @param y The y-coordinate of the menu.
* @param width The width of the menu.
* @param height The height of the menu.
* @param items The items to be displayed in the menu.
*/
public Menu(
final double x,
final double y,
final double width,
final double height,
final Spritesheet background,
final String... items) {
super(x, y, width, height, items.length, 1, null, background);
double x,
double y,
double width,
double height,
String... items) {
this(x, y, width, height, null, Orientation.VERTICAL, items);
}

/**
* Constructs a new Menu with the specified position, size, background, and orientation.
*
* @param x The x-coordinate of the menu.
* @param y The y-coordinate of the menu.
* @param width The width of the menu.
* @param height The height of the menu.
* @param background The background of the menu.
* @param orientation The orientation of the menu (horizontal or vertical).
* @param items The items to be displayed in the menu.
*/
public Menu(
double x,
double y,
double width,
double height,
Spritesheet background,
Orientation orientation,
String... items) {
super(x, y, width, height,
orientation == Orientation.VERTICAL ? items.length : 1,
orientation == Orientation.VERTICAL ? 1 : items.length,
null, background);
this.items = items;
this.selectionChangeConsumers = new CopyOnWriteArrayList<>();
this.orientation = orientation;
}

/**
* Gets the current selection index of the menu.
*
* @return The current selection index of the menu.
*/
public int getCurrentSelection() {
return this.currentSelection;
}

/**
* Gets the orientation of the menu.
*
* @return The orientation of the menu.
*/
public Orientation getOrientation() {
return orientation;
}

/**
* Adds a consumer that will be notified when the selection of the menu changes.
*
* @param cons The consumer that will be notified when the selection of the menu changes.
*/
public void onChange(final IntConsumer cons) {
this.selectionChangeConsumers.add(cons);
}

/**
* Prepares the menu by setting the text of the menu buttons and adding the click listeners.
*/
@Override
public void prepare() {

super.prepare();
for (int i = 0; i < this.items.length; i++) {
final ImageComponent menuButton = this.getCellComponents().get(i);
menuButton.setText(this.items[i]);
for (int i = 0; i < items.length; i++) {
final ImageComponent menuButton = getCellComponents().get(i);
menuButton.setText(items[i]);
menuButton.onClicked(
c -> this.setCurrentSelection(this.getCellComponents().indexOf(menuButton)));
c -> setCurrentSelection(getCellComponents().indexOf(menuButton)));
}
}

public void setCurrentSelection(final int currentSelection) {
this.currentSelection = currentSelection;
/**
* Sets the current selection index of the menu and updates the selection state of the menu items.
*
* @param newSelection The index of the item to be selected.
*/
public void setCurrentSelection(final int newSelection) {
this.currentSelection = newSelection;

for (int i = 0; i < this.getCellComponents().size(); i++) {
this.getCellComponents().get(this.currentSelection).setSelected(i == this.currentSelection);
for (int i = 0; i < getCellComponents().size(); i++) {
getCellComponents().get(getCurrentSelection()).setSelected(i == getCurrentSelection());
}

this.selectionChangeConsumers.forEach(c -> c.accept(this.getCurrentSelection()));
this.selectionChangeConsumers.forEach(c -> c.accept(getCurrentSelection()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.gurkenlabs.litiengine.gui;

/**
* Represents the orientation of a menu or component. It can be either horizontal or vertical.
*/
public enum Orientation {
HORIZONTAL,
VERTICAL
}

0 comments on commit 49bd374

Please sign in to comment.