Files
JustEnoughCalculation/src/main/java/me/towdium/jecalculation/gui/widgets/WSwitcher.java
T
2021-05-01 22:34:55 +08:00

118 lines
3.4 KiB
Java

package me.towdium.jecalculation.gui.widgets;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import me.towdium.jecalculation.gui.JecaGui;
import me.towdium.jecalculation.gui.Resource;
import me.towdium.jecalculation.utils.Utilities.Circulator;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static me.towdium.jecalculation.gui.JecaGui.COLOR_GUI_GREY;
import static me.towdium.jecalculation.gui.JecaGui.Font.SHADOW;
import static me.towdium.jecalculation.gui.Resource.WGT_ARR_L;
import static me.towdium.jecalculation.gui.Resource.WGT_ARR_R;
/**
* Author: towdium
* Date: 17-8-19.
* Widget to select page with button for left and right
*/
@ParametersAreNonnullByDefault
@SideOnly(Side.CLIENT)
public class WSwitcher extends WContainer {
public static final int SIZE = 13;
public ListenerAction<? super WSwitcher> listener;
protected int xPos, xSize, yPos;
protected WButton left, right;
protected WRectangle wRect;
protected WText wText;
protected List<String> keys;
protected Circulator index;
protected String temp;
public WSwitcher(int xPos, int yPos, int xSize, int total) {
this(xPos, yPos, xSize,
IntStream.rangeClosed(1, total).mapToObj(i -> i + "/" + total).collect(Collectors.toList()));
}
public WSwitcher(int xPos, int yPos, int xSize, List<String> keys) {
this.xPos = xPos;
this.xSize = xSize;
this.yPos = yPos;
this.keys = keys;
left = new WButtonIcon(xPos, yPos, SIZE, SIZE, WGT_ARR_L).setListener(i -> {
if (temp == null) move(-1);
else setTemp(null);
});
right = new WButtonIcon(xPos + xSize - SIZE, yPos, SIZE, SIZE, WGT_ARR_R).setListener(i -> {
if (temp == null) move(1);
else setTemp(null);
});
wRect = new WRectangle(xPos + SIZE, yPos, xSize - 2 * SIZE, SIZE, COLOR_GUI_GREY);
wText = new WText(xPos + SIZE, yPos + 2, xSize - 2 * SIZE, SHADOW, "", true);
index = new Circulator(keys.size());
refresh();
add(left, right, wRect, wText);
}
public void move(int diff) {
if (diff == 0) return;
if (temp != null) {
temp = null;
if (diff > 0) diff -= 1;
else diff += 1;
}
index.move(diff);
notifyLsnr();
refresh();
}
public WSwitcher setListener(ListenerAction<? super WSwitcher> listener) {
this.listener = listener;
return this;
}
public void setTemp(@Nullable String temp) {
this.temp = temp;
notifyLsnr();
refresh();
}
public int getIndex() {
return temp == null ? index.current() : -1;
}
public void setIndex(int i) {
if (temp != null) temp = null;
index.set(i);
refresh();
}
public List<String> getTexts() {
return keys;
}
public String getText() {
return temp == null ? keys.get(index.current()) : temp;
}
public void refresh() {
boolean b = keys.size() < (temp == null ? 2 : 1);
left.setDisabled(b);
right.setDisabled(b);
wText.key = temp == null ? (keys.isEmpty() ? "" : keys.get(index.current())) : temp;
}
public void notifyLsnr() {
if (listener != null) listener.invoke(this);
}
}