Skip to content

Commit

Permalink
Merge pull request #6 from D-Cysteine/main
Browse files Browse the repository at this point in the history
Merge v1.4.3 from upstream
  • Loading branch information
Dream-Master authored Jan 8, 2022
2 parents 1c1074c + 1af980a commit caa807a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.minecraft.client.gui.ScaledResolution;
import org.lwjgl.opengl.GL11;

import java.util.Optional;

/** Handles things like getting the mouse position, and scrolling. */
public final class GuiManager {
// Margins for glScissor, in pixels. These margins will be excluded from the scissor region.
Expand Down Expand Up @@ -201,7 +203,13 @@ public Point getAbsoluteMousePosition() {
}

public Point getRelativeMousePosition(int recipe) {
GuiRecipe gui = getGui();
Optional<GuiRecipe> guiOptional = getGui();
if (!guiOptional.isPresent()) {
// The GUI got closed already, or something.
return Point.create(0, 0);
}
GuiRecipe gui = guiOptional.get();

java.awt.Point mouse = GuiDraw.getMousePosition();
java.awt.Point offset = gui.getRecipePosition(recipe);

Expand Down Expand Up @@ -261,14 +269,26 @@ public void scrollToMouse(Dimension diagramDimension) {
* <p>Note that this is incorrect for {@code glScissor}!
*/
public Point getViewportPosition() {
GuiRecipe gui = getGui();
Optional<GuiRecipe> guiOptional = getGui();
if (!guiOptional.isPresent()) {
// The GUI got closed already, or something.
return Point.create(0, 0);
}
GuiRecipe gui = guiOptional.get();

return Point.create(
Reflection.GUI_LEFT.get(gui) + SIDE_MARGIN,
Reflection.GUI_TOP.get(gui) + TOP_MARGIN);
}

public Dimension getViewportDimension() {
GuiRecipe gui = getGui();
Optional<GuiRecipe> guiOptional = getGui();
if (!guiOptional.isPresent()) {
// The GUI got closed already, or something.
return Dimension.create(0, 0);
}
GuiRecipe gui = guiOptional.get();

return Dimension.create(
Reflection.X_SIZE.get(gui) - 2 * SIDE_MARGIN,
Reflection.Y_SIZE.get(gui) - (TOP_MARGIN + BOTTOM_MARGIN));
Expand All @@ -280,7 +300,13 @@ private int computeScrollableHeight(Dimension diagramDimension) {
}

private void setScissor() {
GuiRecipe gui = getGui();
Optional<GuiRecipe> guiOptional = getGui();
if (!guiOptional.isPresent()) {
// The GUI got closed already, or something.
return;
}
GuiRecipe gui = guiOptional.get();

int left = Reflection.GUI_LEFT.get(gui) + SIDE_MARGIN;
int bottom =
gui.height - (Reflection.GUI_TOP.get(gui) + Reflection.Y_SIZE.get(gui))
Expand All @@ -300,7 +326,13 @@ private void setScissor() {
viewportDim.width() * scaleFactor, viewportDim.height() * scaleFactor);
}

private GuiRecipe getGui() {
return (GuiRecipe) GuiContainerManager.getManager().window;
/** Returns empty {@link Optional} in cases such as the GUI being instantly closed. */
private Optional<GuiRecipe> getGui() {
GuiContainerManager manager = GuiContainerManager.getManager();
if (manager == null || !(manager.window instanceof GuiRecipe)) {
return Optional.empty();
}

return Optional.of((GuiRecipe) manager.window);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public void load(GuiContainer guiContainer) {}

@Override
public ItemStack getStackUnderMouse(GuiContainer guiContainer, int mousex, int mousey) {
if (!(guiContainer instanceof GuiRecipe)) {
return null;
}

Optional<DiagramGroup> diagramGroupOptional = getDiagramGroup(guiContainer);
if (!diagramGroupOptional.isPresent()) {
return null;
Expand Down

0 comments on commit caa807a

Please # to comment.