mirror of
https://github.com/HbmMods/Hbm-s-Nuclear-Tech-GIT.git
synced 2026-01-25 10:32:49 +00:00
gui design is my passion
This commit is contained in:
parent
f17556a0d6
commit
27e4cd32aa
@ -1,23 +1,29 @@
|
|||||||
package com.hbm.inventory.gui;
|
package com.hbm.inventory.gui;
|
||||||
|
|
||||||
import com.hbm.lib.RefStrings;
|
import com.hbm.util.Tuple;
|
||||||
|
|
||||||
import net.minecraft.client.gui.GuiScreen;
|
import net.minecraft.client.gui.GuiScreen;
|
||||||
import net.minecraft.client.gui.GuiTextField;
|
import net.minecraft.client.gui.GuiTextField;
|
||||||
import net.minecraft.util.ResourceLocation;
|
|
||||||
import org.lwjgl.input.Keyboard;
|
import org.lwjgl.input.Keyboard;
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.MathContext;
|
import java.math.MathContext;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Deque;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
public class GUICalculator extends GuiScreen {
|
public class GUICalculator extends GuiScreen {
|
||||||
private static final ResourceLocation texture = new ResourceLocation(RefStrings.MODID, "textures/gui/calculator.png");
|
|
||||||
private int xSize = 220;
|
private int xSize = 220;
|
||||||
private int ySize = 50;
|
private int ySize = 50;
|
||||||
|
private int borderWidth = 2;
|
||||||
private GuiTextField inputField;
|
private GuiTextField inputField;
|
||||||
|
|
||||||
|
private int selectedHist = -1;
|
||||||
|
private static final int maxHistory = 6;
|
||||||
|
private static Deque<Tuple.Pair<String, Double>> history = new ArrayDeque<>();
|
||||||
private String latestResult = "?";
|
private String latestResult = "?";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -46,15 +52,31 @@ public class GUICalculator extends GuiScreen {
|
|||||||
String input = inputField.getText().replaceAll("[^\\d+\\-*/^!.()\\sA-Za-z]+", "");
|
String input = inputField.getText().replaceAll("[^\\d+\\-*/^!.()\\sA-Za-z]+", "");
|
||||||
|
|
||||||
if (p_73869_1_ == 13 || p_73869_1_ == 10) { // when pressing enter (CR or LF)
|
if (p_73869_1_ == 13 || p_73869_1_ == 10) { // when pressing enter (CR or LF)
|
||||||
try {
|
if (selectedHist != -1) {
|
||||||
double result = evaluateExpression(input);
|
input = new ArrayList<>(history).get(selectedHist).key;
|
||||||
String plainStringRepresentation = (new BigDecimal(result, MathContext.DECIMAL64)).toPlainString();
|
inputField.setText(input);
|
||||||
GuiScreen.setClipboardString(plainStringRepresentation);
|
selectedHist = -1;
|
||||||
inputField.setText(plainStringRepresentation);
|
} else {
|
||||||
inputField.setCursorPositionEnd();
|
try {
|
||||||
inputField.setSelectionPos(0);
|
double result = evaluateExpression(input);
|
||||||
} catch (Exception ignored) {}
|
history.addFirst(new Tuple.Pair<String,Double>(input, result));
|
||||||
return;
|
if (history.size() > maxHistory) history.removeLast();
|
||||||
|
String plainStringRepresentation = (new BigDecimal(result, MathContext.DECIMAL64)).toPlainString();
|
||||||
|
GuiScreen.setClipboardString(plainStringRepresentation);
|
||||||
|
inputField.setText(plainStringRepresentation);
|
||||||
|
inputField.setCursorPositionEnd();
|
||||||
|
inputField.setSelectionPos(0);
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p_73869_2_ == 200) { // up arrow
|
||||||
|
selectedHist = Math.max(selectedHist - 1, -1);
|
||||||
|
} else if (p_73869_2_ == 208) { // down arrow
|
||||||
|
selectedHist = Math.min(selectedHist + 1, history.size() - 1);
|
||||||
|
} else {
|
||||||
|
selectedHist = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input.isEmpty()) {
|
if (input.isEmpty()) {
|
||||||
@ -72,14 +94,27 @@ public class GUICalculator extends GuiScreen {
|
|||||||
super.drawScreen(mouseX, mouseY, partialTicks);
|
super.drawScreen(mouseX, mouseY, partialTicks);
|
||||||
|
|
||||||
GL11.glColor4f(1F, 1F, 1F, 1F);
|
GL11.glColor4f(1F, 1F, 1F, 1F);
|
||||||
mc.getTextureManager().bindTexture(texture);
|
|
||||||
int x = (width - xSize) / 2;
|
int x = (width - xSize) / 2;
|
||||||
int y = (height - ySize) / 2;
|
int y = (height - ySize) / 2;
|
||||||
|
int histHeight = (fontRendererObj.FONT_HEIGHT + 2) * maxHistory;
|
||||||
|
int histStart = y + 30 + fontRendererObj.FONT_HEIGHT + 8;
|
||||||
|
|
||||||
drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
|
drawRect(x, y, x+xSize, y+ySize+histHeight, 0xFF2d2d2d);
|
||||||
|
drawRect(x+borderWidth, y+borderWidth, x+xSize-borderWidth, y+ySize-borderWidth+histHeight, 0xFF3d3d3d);
|
||||||
|
drawRect(x, histStart - 5, x+xSize, histStart - 3, 0xFF2d2d2d);
|
||||||
|
|
||||||
inputField.drawTextBox();
|
inputField.drawTextBox();
|
||||||
fontRendererObj.drawString("=" + latestResult, x + 5, y + 30, -1);
|
fontRendererObj.drawString("=" + latestResult, x + 5, y + 30, -1);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (Tuple.Pair<String, Double> prevInput : history) {
|
||||||
|
int hy = y + 50 + (fontRendererObj.FONT_HEIGHT+1)*i;
|
||||||
|
if (i == selectedHist) {
|
||||||
|
drawRect(x + 4, hy - 1, x + 4 + xSize - 9, hy + fontRendererObj.FONT_HEIGHT, 0xFF111111);
|
||||||
|
}
|
||||||
|
fontRendererObj.drawString(prevInput.key + " = " + prevInput.value, x + 5, hy, -1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 566 B |
Loading…
x
Reference in New Issue
Block a user