Skip to content
Permalink
Browse files

Allow advancements to be dragged in the gui and fire an event when th…

…e mouse is released (#41)

Fixed debug coordinates not drawing on the lowest pixel of the gui.
  • Loading branch information...
Blargerist authored and way2muchnoise committed Aug 26, 2018
1 parent d47d072 commit eb6e4202d21e4a035c8fb3711f13bf3060d446c6
@@ -2,7 +2,7 @@ version_forge=14.23.2.2612
version_minecraft=1.12.2
version_major=0
version_minor=0
version_revis=10
version_revis=11
version_mappings=snapshot_20170918
version_jei=4.8.5.147
curse_project_id=272515
@@ -23,6 +23,7 @@
private int completedLineColor, unCompletedLineColor;
private Integer posX, posY;
private boolean hideLines;
private boolean allowDragging;

public BetterDisplayInfo(Advancement advancement) {
this(advancement.getId());
@@ -57,6 +58,7 @@ private void defaults() {
this.posX = null;
this.posY = null;
this.hideLines = defaultHideLines;
this.allowDragging = false;
}

private void parseDisplayJson(JsonObject displayJson) {
@@ -123,6 +125,7 @@ private void parseIBetterDisplayInfo(IBetterDisplayInfo betterDisplayInfo) {
if (betterDisplayInfo.hideLines() != null) {
this.hideLines = betterDisplayInfo.hideLines();
}
this.allowDragging = betterDisplayInfo.allowDragging();
}

public ResourceLocation getId() {
@@ -168,4 +171,8 @@ public Integer getPosY() {
public Boolean hideLines() {
return this.hideLines;
}

public boolean allowDragging() {
return this.allowDragging;
}
}
@@ -101,4 +101,13 @@ default Integer getPosY() {
default Boolean hideLines() {
return null;
}

/**
* If the advancement should be able to be dragged in the gui
*
* @return a boolean value
*/
default boolean allowDragging() {
return false;
}
}
@@ -0,0 +1,37 @@
package betteradvancements.api.event;

import betteradvancements.gui.GuiBetterAdvancement;
import net.minecraft.advancements.Advancement;
import net.minecraftforge.fml.common.eventhandler.Event;

/**
* Event fired after an advancement has been moved in the gui and the mouse button has been released.
*/
public class AdvancementMovedEvent extends Event {
/**
* Advancement that has been moved.
*/
private final Advancement advancement;
/*
* Coordinates the advancement was moved to.
*/
private final int x, y;

public AdvancementMovedEvent(GuiBetterAdvancement gui) {
this.advancement = gui.getAdvancement();
this.x = gui.getX();
this.y = gui.getY();
}

public Advancement getAdvancement() {
return this.advancement;
}

public int getX() {
return this.x;
}

public int getY() {
return this.y;
}
}
@@ -35,7 +35,7 @@

private final GuiBetterAdvancementTab guiBetterAdvancementTab;
private final Advancement advancement;
private final BetterDisplayInfo betterDisplayInfo;
protected final BetterDisplayInfo betterDisplayInfo;
private final DisplayInfo displayInfo;
private final String title;
private final int width;
@@ -44,7 +44,7 @@
private GuiBetterAdvancement parent;
private final List<GuiBetterAdvancement> children = Lists.newArrayList();
private AdvancementProgress advancementProgress;
private final int x, y;
protected int x, y;
private final int screenScale;

public GuiBetterAdvancement(GuiBetterAdvancementTab guiBetterAdvancementTab, Minecraft mc, Advancement advancement, DisplayInfo displayInfo) {
@@ -392,4 +392,8 @@ public int getY() {
public int getX() {
return this.x;
}

public Advancement getAdvancement() {
return this.advancement;
}
}
@@ -33,7 +33,7 @@
private final ItemStack icon;
private final String title;
private final GuiBetterAdvancement root;
private final Map<Advancement, GuiBetterAdvancement> guis = Maps.newLinkedHashMap();
protected final Map<Advancement, GuiBetterAdvancement> guis = Maps.newLinkedHashMap();
private final BetterDisplayInfoRegistry betterDisplayInfos;

protected int scrollX, scrollY;
@@ -1,5 +1,6 @@
package betteradvancements.gui;

import betteradvancements.api.event.AdvancementMovedEvent;
import betteradvancements.reference.Resources;
import betteradvancements.util.RenderUtil;
import com.google.common.collect.Maps;
@@ -12,6 +13,8 @@
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.resources.I18n;
import net.minecraft.network.play.client.CPacketSeenAdvancements;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Mouse;
@@ -33,6 +36,7 @@
private float zoom = MIN_ZOOM;
private boolean isScrolling;
public static boolean showDebugCoordinates = false;
private GuiBetterAdvancement advConnectedToMouse = null;

public GuiScreenBetterAdvancements(ClientAdvancementManager clientAdvancementManager) {
this.clientAdvancementManager = clientAdvancementManager;
@@ -110,27 +114,61 @@ protected void keyTyped(char typedChar, int keyCode) throws IOException {
* Draws the screen and all the components in it.
*/
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
if (Mouse.isButtonDown(0)) {
if (Mouse.isButtonDown(0) && !this.isScrolling) {
if (this.advConnectedToMouse == null) {
boolean inGui = mouseX < this.width - SIDE - PADDING && mouseX > SIDE + PADDING && mouseY < this.height - TOP + 1 && mouseY > TOP + PADDING * 2;
if (this.selectedTab != null && inGui) {
for (GuiBetterAdvancement guiBetterAdvancement : this.selectedTab.guis.values()) {
if (guiBetterAdvancement.isMouseOver(this.selectedTab.scrollX, this.selectedTab.scrollY, mouseX - SIDE - PADDING, mouseY - TOP - 2*PADDING)) {

if (guiBetterAdvancement.betterDisplayInfo.allowDragging())
{
this.advConnectedToMouse = guiBetterAdvancement;
break;
}
}
}
}
}
else {
int moveX = mouseX - this.scrollMouseX;
int moveY = mouseY - this.scrollMouseY;

this.advConnectedToMouse.x = this.advConnectedToMouse.x + moveX;
this.advConnectedToMouse.y = this.advConnectedToMouse.y + moveY;
}
}
else {
if (this.advConnectedToMouse != null) {
//Create and post event for the advancement movement
final AdvancementMovedEvent event = new AdvancementMovedEvent(advConnectedToMouse);
MinecraftForge.EVENT_BUS.post(event);
}
this.advConnectedToMouse = null;
}

if (Mouse.isButtonDown(0) && this.advConnectedToMouse == null) {
if (!this.isScrolling) {
this.isScrolling = true;
} else if (this.selectedTab != null) {
this.selectedTab.scroll(mouseX - this.scrollMouseX, mouseY - this.scrollMouseY, width - 2*SIDE - 2*PADDING, height - TOP - BOTTOM - 3*PADDING);
}

this.scrollMouseX = mouseX;
this.scrollMouseY = mouseY;
} else {
this.isScrolling = false;
}

if (Mouse.isButtonDown(0)) {
this.scrollMouseX = mouseX;
this.scrollMouseY = mouseY;
}

this.drawDefaultBackground();
this.renderInside(mouseX, mouseY, SIDE, TOP, width - SIDE, height - BOTTOM);
this.renderWindow(SIDE, TOP, width - SIDE, height - BOTTOM);
this.renderToolTips(mouseX, mouseY, SIDE, TOP, width - SIDE, height - BOTTOM);

//Draws a string containing the current position above the mouse. Locked to inside the advancement window.
if (GuiScreenBetterAdvancements.showDebugCoordinates && this.selectedTab != null && mouseX < this.width - SIDE - PADDING && mouseX > SIDE + PADDING && mouseY < this.height - TOP && mouseY > TOP + PADDING * 2)
{
if (GuiScreenBetterAdvancements.showDebugCoordinates && this.selectedTab != null && mouseX < this.width - SIDE - PADDING && mouseX > SIDE + PADDING && mouseY < this.height - TOP + 1 && mouseY > TOP + PADDING * 2) {
int xMouse = mouseX - SIDE - PADDING;
int yMouse = mouseY - TOP - 2 * PADDING;
//-3 and -1 are needed to have the position be where the advancement starts being rendered, rather than its real position.

0 comments on commit eb6e420

Please sign in to comment.
You can’t perform that action at this time.