Skip to content

Commit

Permalink
Admin Controller mostly done
Browse files Browse the repository at this point in the history
  • Loading branch information
MineDragonCZ committed Jul 15, 2023
1 parent 3196f06 commit ed9fa22
Show file tree
Hide file tree
Showing 11 changed files with 677 additions and 557 deletions.
8 changes: 4 additions & 4 deletions src/main/java/tauri/dev/jsg/gui/JSGTexturedGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import net.minecraft.util.ResourceLocation;

public abstract class JSGTexturedGui extends GuiScreen {
protected int guiLeft;
protected int guiTop;
public int guiLeft;
public int guiTop;

protected final int xSize;
protected final int ySize;
public final int xSize;
public final int ySize;

protected final int texW;
protected final int texH;
Expand Down
104 changes: 77 additions & 27 deletions src/main/java/tauri/dev/jsg/gui/admincontroller/AddressesSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.EnumHand;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fml.client.config.GuiUtils;
import tauri.dev.jsg.JSG;
import tauri.dev.jsg.gui.base.JSGButton;
import tauri.dev.jsg.gui.base.JSGTextField;
import tauri.dev.jsg.gui.element.ArrowButton;
import tauri.dev.jsg.gui.element.GuiHelper;
import tauri.dev.jsg.packet.JSGPacketHandler;
import tauri.dev.jsg.packet.gui.entry.EntryActionEnum;
import tauri.dev.jsg.packet.gui.entry.EntryActionToServer;
import tauri.dev.jsg.stargate.EnumIrisState;
import tauri.dev.jsg.stargate.network.StargateAddress;
import tauri.dev.jsg.stargate.network.StargateAddressDynamic;
import tauri.dev.jsg.stargate.network.StargatePos;
Expand All @@ -22,17 +24,20 @@

public class AddressesSection {

public static final int OFFSET = 15;
// ----------------------------------------------------------
protected static final int SCROLL_AMOUNT = 5;
public ArrayList<StargateEntry> entries = new ArrayList<>();

public int guiTop;
public int height;
public int guiLeft;
public int width;

public static final int OFFSET = 15;

public int scrolled = 0;
public GuiAdminController guiBase;
public int thisGateEntryIndex = -1;
public ArrayList<ArrowButton> dialButtons = new ArrayList<>();
public ArrayList<JSGButton> optionButtons = new ArrayList<>();
public ArrayList<GuiTextField> entriesTextFields = new ArrayList<>();

public AddressesSection(GuiAdminController baseGui) {
this.guiBase = baseGui;
Expand Down Expand Up @@ -65,10 +70,7 @@ public void generateAddressEntries() {
sortEntries();
}

public ArrayList<ArrowButton> dialButtons = new ArrayList<>();
public ArrayList<GuiTextField> entriesTextFields = new ArrayList<>();

public void generateAddressEntriesBoxes(boolean reset) {
public void init(boolean reset) {
int index = -1;
if (!reset && dialButtons.size() > 0) return;
if (!reset && entriesTextFields.size() > 0) return;
Expand All @@ -87,8 +89,9 @@ public void generateAddressEntriesBoxes(boolean reset) {
} else {
field.setEnabled(false);
}
ArrowButton btn = (ArrowButton) new ArrowButton(index, guiLeft + 120 + 5, 0, ArrowButton.ArrowType.RIGHT).setFgColor(GuiUtils.getColorCode('a', true)).setActionCallback(() -> dialGate(finalIndex));
ArrowButton btn = (ArrowButton) new ArrowButton(index, guiLeft + 125, 0, ArrowButton.ArrowType.RIGHT).setFgColor(GuiUtils.getColorCode('a', true)).setActionCallback(() -> dialGate(finalIndex));
if (e.pos.gatePos.equals(Objects.requireNonNull(guiBase.gateTile).getPos()) && e.pos.dimensionID == guiBase.gateTile.world().provider.getDimension()) {
thisGateEntryIndex = index;
btn.setEnabled(false);
btn.setActionCallback(() -> {
});
Expand All @@ -97,6 +100,46 @@ public void generateAddressEntriesBoxes(boolean reset) {
entriesTextFields.add(field);
dialButtons.add(btn);
}

// Options buttons
optionButtons.clear();

// Abort button
String text = "Abort dialing";
int width = (10 + guiBase.mc.fontRenderer.getStringWidth(text));
int y = this.guiTop;
int x = guiBase.guiRight - OFFSET - 40 - width;
optionButtons.add(new JSGButton(100, x, y, width, 20, text).setActionCallback(() -> {
if (guiBase.imaginaryGateTile != null && guiBase.imaginaryGateTile.getStargateState().dialing()) {
if (guiBase.imaginaryGateTile.abortDialingSequence()) {
guiBase.notifer.setText("Dialing aborted.", Notifier.EnumAlertType.INFO, 5);
sendPacket(EntryActionEnum.ABORT);
} else
guiBase.notifer.setText("Gate is busy!", Notifier.EnumAlertType.WARNING, 5);
} else if (guiBase.imaginaryGateTile != null) {
guiBase.notifer.setText("Gate is not dialing", Notifier.EnumAlertType.WARNING, 5);
} else {
guiBase.notifer.setText("Gate is NULL!", Notifier.EnumAlertType.ERROR, 5);
}
}));

// Toggle IRIS
text = "Toggle iris";
width = (10 + guiBase.mc.fontRenderer.getStringWidth(text));
x -= (width + 3);
optionButtons.add(new JSGButton(101, x, y, width, 20, text).setActionCallback(() -> {
if (guiBase.imaginaryGateTile != null && guiBase.imaginaryGateTile.hasIris()) {
if (guiBase.imaginaryGateTile.getIrisState() == EnumIrisState.OPENED || guiBase.imaginaryGateTile.getIrisState() == EnumIrisState.CLOSED) {
guiBase.notifer.setText("Toggling iris.", Notifier.EnumAlertType.INFO, 5);
sendPacket(EntryActionEnum.TOGGLE_IRIS);
} else
guiBase.notifer.setText("Gate's iris is busy!", Notifier.EnumAlertType.WARNING, 5);
} else if (guiBase.imaginaryGateTile != null) {
guiBase.notifer.setText("Gate has no iris!", Notifier.EnumAlertType.WARNING, 5);
} else {
guiBase.notifer.setText("Gate is NULL!", Notifier.EnumAlertType.ERROR, 5);
}
}));
}

public void sortEntries() {
Expand All @@ -112,30 +155,37 @@ public void sortEntries() {
entries = newList;
}

public void sendPacket(EntryActionEnum action) {
try {
JSGPacketHandler.INSTANCE.sendToServer(new EntryActionToServer(action, guiBase.pos));
} catch (Exception e) {
JSG.error("Error", e);
}
}

public void dialGate(int index) {
try {
EnumHand hand = guiBase.getHand();
StargateEntry entry = entries.get(index);
StargatePos pos = entry.pos;
if (guiBase.gateTile == null || guiBase.imaginaryGateTile == null){
if (guiBase.gateTile == null || guiBase.imaginaryGateTile == null) {
guiBase.notifer.setText("Linked gate is NULL!", Notifier.EnumAlertType.ERROR, 5);
return;
}
if (!guiBase.imaginaryGateTile.getStargateState().idle() && !guiBase.imaginaryGateTile.getStargateState().engaged()){
if (!guiBase.imaginaryGateTile.getStargateState().idle() && !guiBase.imaginaryGateTile.getStargateState().engaged()) {
guiBase.notifer.setText("Stargate is busy!", Notifier.EnumAlertType.WARNING, 5);
return;
}

if(!guiBase.imaginaryGateTile.getStargateState().engaged())
if (!guiBase.imaginaryGateTile.getStargateState().engaged())
guiBase.notifer.setText("Dialing gate " + (pos.getName().equals("") ? DimensionManager.getProviderType(pos.dimensionID).getName() : pos.getName()), Notifier.EnumAlertType.INFO, 5);
else
guiBase.notifer.setText("Closing gate...", Notifier.EnumAlertType.INFO, 5);

int symbolsCount = Objects.requireNonNull(guiBase.gateTile).getMinimalSymbolsToDial(pos.getGateSymbolType(), pos);

JSGPacketHandler.INSTANCE.sendToServer(new EntryActionToServer(hand, new StargateAddressDynamic(entry.address), symbolsCount, guiBase.gateTile.getPos()));
}
catch (Exception e){
} catch (Exception e) {
JSG.error("Error ", e);
guiBase.notifer.setText("Unknown error! (" + e.getMessage() + ")", Notifier.EnumAlertType.ERROR, 5);
}
Expand Down Expand Up @@ -164,19 +214,22 @@ public void updateY() {

public void renderEntries() {
updateY();

GlStateManager.color(1, 1, 1, 1);

GlStateManager.pushMatrix();
for (GuiTextField f : entriesTextFields) {
if (canNotRenderEntry(f.y)) continue;
f.drawTextBox();
}

boolean shouldBeEnabled = (guiBase.imaginaryGateTile != null && (guiBase.imaginaryGateTile.getStargateState().idle() || guiBase.imaginaryGateTile.getStargateState().engaged()));

for (ArrowButton b : dialButtons) {
if (canNotRenderEntry(b.y)) continue;
b.setEnabled(shouldBeEnabled && b.id != thisGateEntryIndex);
b.drawButton(Minecraft.getMinecraft(), guiBase.mouseX, guiBase.mouseY, guiBase.partialTicks);
}

for (JSGButton b : optionButtons) {
b.drawButton(Minecraft.getMinecraft(), guiBase.mouseX, guiBase.mouseY, guiBase.partialTicks);
}
GlStateManager.popMatrix();
}

public void renderFg() {
Expand All @@ -198,9 +251,6 @@ public void renderFg() {
}
}

// ----------------------------------------------------------
protected static final int SCROLL_AMOUNT = 5;

public void scroll(int k) {
if (k == 0) return;
if (k < 0) k = -1;
Expand All @@ -211,8 +261,8 @@ public void scroll(int k) {
}

public boolean canContinueScrolling(int k) {
int top = guiTop + OFFSET;
int bottom = guiTop + height - OFFSET;
int top = guiTop;
int bottom = guiTop + height;
if (entriesTextFields.size() < 1 && dialButtons.size() < 1) return false;

boolean isTop = ((entriesTextFields.size() > 0 && entriesTextFields.get(0).getId() < dialButtons.get(0).id) ? entriesTextFields.get(0).y > top : dialButtons.get(0).y > top);
Expand All @@ -222,8 +272,8 @@ public boolean canContinueScrolling(int k) {
}

public boolean canNotRenderEntry(int y) {
int top = guiTop + OFFSET;
int bottom = guiTop + height - OFFSET;
int top = guiTop;
int bottom = guiTop + height;
int height = 23;
return y < top || (y + height) > bottom;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import tauri.dev.jsg.JSG;
import tauri.dev.jsg.config.JSGConfig;
import tauri.dev.jsg.gui.JSGTexturedGui;
import tauri.dev.jsg.gui.base.JSGButton;
import tauri.dev.jsg.gui.element.ArrowButton;
import tauri.dev.jsg.gui.element.GuiHelper;
import tauri.dev.jsg.gui.mainmenu.GuiCustomMainMenu;
Expand All @@ -30,6 +32,7 @@
import tauri.dev.jsg.stargate.network.SymbolTypeEnum;
import tauri.dev.jsg.tileentity.stargate.StargateClassicBaseTile;
import tauri.dev.jsg.util.JSGTextureLightningHelper;
import tauri.dev.jsg.util.math.TemperatureHelper;

import javax.annotation.Nonnull;
import java.io.IOException;
Expand Down Expand Up @@ -157,7 +160,10 @@ public ResourceLocation getBackground() {
public void initGui() {
super.initGui();
center = getCenterPos(0, 0);
gateCenter = center;
gateCenter = new int[]{
center[0],
center[1] + OFFSET/2
};

guiRight = xSize;
guiBottom = ySize;
Expand All @@ -166,7 +172,7 @@ public void initGui() {
addressesSection.guiTop = OFFSET;
addressesSection.width = 145 + (AddressesSection.OFFSET * 2);
addressesSection.height = ySize - OFFSET * 2;
addressesSection.generateAddressEntriesBoxes(true);
addressesSection.init(true);
}

public static final int OFFSET = 15;
Expand All @@ -180,7 +186,7 @@ public void drawForeground(int mouseX, int mouseY, float partialTicks) {

// render background
addressesSection.generateAddressEntries();
addressesSection.generateAddressEntriesBoxes(false);
addressesSection.init(false);
addressesSection.renderEntries();

renderControlButtons();
Expand Down Expand Up @@ -217,7 +223,13 @@ public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
field.mouseClicked(this.mouseX, this.mouseY, mouseButton);
}
for (ArrowButton button : addressesSection.dialButtons) {
if (GuiHelper.isPointInRegion(button.x, button.y, button.width, button.height, this.mouseX, this.mouseY)) {
if (button.enabled && GuiHelper.isPointInRegion(button.x, button.y, button.width, button.height, this.mouseX, this.mouseY)) {
button.performAction();
button.playPressSound(Minecraft.getMinecraft().getSoundHandler());
}
}
for(JSGButton button : addressesSection.optionButtons){
if (button.enabled && GuiHelper.isPointInRegion(button.x, button.y, button.width, button.height, this.mouseX, this.mouseY)) {
button.performAction();
button.playPressSound(Minecraft.getMinecraft().getSoundHandler());
}
Expand Down Expand Up @@ -277,6 +289,26 @@ public void renderGateInfo() {
renderSymbol(x, y, width, height, symbol, originId);
}
// ----------------------

// Render info
String[] s = new String[]{
"Gate state: " + imaginaryGateTile.getStargateState(),
"Gate temp: " + JSGConfig.General.visual.temperatureUnit.getTemperatureToDisplay(TemperatureHelper.asKelvins(TemperatureHelper.asCelsius(imaginaryGateTile.gateHeat).toKelvins()), 0),
"Iris temp: " + JSGConfig.General.visual.temperatureUnit.getTemperatureToDisplay(TemperatureHelper.asKelvins(TemperatureHelper.asCelsius(imaginaryGateTile.irisHeat).toKelvins()), 0),
"Installed capacitors: " + (imaginaryGateTile.getPowerTier() - 1),
"Energy: " + String.format("%.0f", (float) imaginaryGateTile.getEnergyStored()) + "RF",
"Time opened: " + imaginaryGateTile.getOpenedSecondsToDisplayAsMinutes(),
"Seconds to close: " + imaginaryGateTile.getEnergySecondsToClose()
};

int y = OFFSET + 23;
for(String line : s){
width = fontRenderer.getStringWidth(line);
int lineX = guiRight - OFFSET - width - 40;
fontRenderer.drawString(line, lineX, y, 0xffffff, true);
y += 10;
}
// ----------------------
}

public static void renderSymbol(int x, int y, int w, int h, SymbolInterface symbol, int originId) {
Expand All @@ -297,4 +329,12 @@ public static void renderSymbol(int x, int y, int w, int h, SymbolInterface symb

GlStateManager.popMatrix();
}

@Override
public void onGuiClosed() {
for(GuiTextField f : addressesSection.entriesTextFields){
f.setFocused(false);
}
super.onGuiClosed();
}
}
Loading

0 comments on commit ed9fa22

Please sign in to comment.