Skip to content

Commit

Permalink
Implemented the GUI for typing label for the marker
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunternif committed Feb 20, 2014
1 parent 1db1831 commit 7db6e91
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 18 deletions.
63 changes: 46 additions & 17 deletions src/hunternif/mc/atlas/client/gui/GuiAtlas.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hunternif.mc.atlas.client.gui;

import hunternif.mc.atlas.AntiqueAtlasMod;
import hunternif.mc.atlas.api.AtlasAPI;
import hunternif.mc.atlas.client.Textures;
import hunternif.mc.atlas.core.AtlasData;
import hunternif.mc.atlas.core.BiomeTextureMap;
Expand Down Expand Up @@ -35,8 +34,6 @@ public class GuiAtlas extends GuiComponent {
private static final int CONTENT_X = 17;
private static final int CONTENT_Y = 11;

public static final String defaultMarker = "red_x";

private static final int MAP_WIDTH = WIDTH - 17*2;
private static final int MAP_HEIGHT = 194;
private static final int MAP_TILE_SIZE = 8;
Expand All @@ -52,14 +49,10 @@ public class GuiAtlas extends GuiComponent {
/** The radius of the area in which the marker will display hovering label. */
private static final int MARKER_RADIUS = 4;

/** Pause between after the arrow button is pressed and continuous
* navigation starts, in ticks. */
private static final int BUTTON_PAUSE = 8;
// Buttons =================================================================

/** Arrow buttons for navigating the map view via mouse clicks. */
private GuiArrowButton btnUp, btnDown, btnLeft, btnRight;
/** How much the map view is offset, in blocks, per click (or per tick). */
public static int navigateStep = 24;

/** Button for exporting PNG image of the Atlas's contents. */
private GuiBookmarkButton btnExportPng;
Expand All @@ -70,6 +63,16 @@ public class GuiAtlas extends GuiComponent {
/** Button for restoring player's position at the center of the Atlas. */
private GuiPositionButton btnPosition;


// Navigation ==============================================================

/** Pause between after the arrow button is pressed and continuous
* navigation starts, in ticks. */
private static final int BUTTON_PAUSE = 8;

/** How much the map view is offset, in blocks, per click (or per tick). */
public static int navigateStep = 24;

/** The button which is currently being pressed. Used for continuous
* navigation using the arrow buttons. Also used to prevent immediate
* canceling of placing marker. */
Expand All @@ -95,16 +98,28 @@ public class GuiAtlas extends GuiComponent {
* offset. */
private boolean followPlayer = true;


// Exporting image =========================================================

/** Progress bar for exporting images. */
private ProgressBarOverlay progressBar = new ProgressBarOverlay(100, 2);
private volatile boolean isExporting = false;


// Markers =================================================================

/** Temporary set for loading markers currently visible on the map. */
private final SortedSet<Marker> visibleMarkers = new TreeSet<Marker>();

/** If true, a semi-transparent marker is attached to the cursor, and the
* player's icon becomes semi-transparent as well. */
private boolean isPlacingMarker = false;
/** If true, a text input field will appear. */
private boolean isEnteringMarkerLabel = false;

private GuiMarkerFinalizer markerFinalizer = new GuiMarkerFinalizer();

// Misc stuff ==============================================================

private EntityPlayer player;
private ItemStack stack;
Expand Down Expand Up @@ -169,13 +184,14 @@ public void run() {
btnMarker.addListener(new IButtonListener() {
@Override
public void onClick(GuiComponentButton button) {
if (stack != null) {
if (stack != null && !isPlacingMarker && !isEnteringMarkerLabel) {
selectedButton = button;
isPlacingMarker = true;
}
}
});
}

public GuiAtlas setAtlasItemStack(ItemStack stack) {
this.player = Minecraft.getMinecraft().thePlayer;
this.stack = stack;
Expand Down Expand Up @@ -205,12 +221,15 @@ protected void mouseClicked(int mouseX, int mouseY, int mouseState) {
} else {
// If clicked on the map, place marker
if (isMouseOverMap && mouseState == 0 /* left click */) {
int x = screenXToWorldX(mouseX);
int z = screenYToWorldZ(mouseY);
AtlasAPI.getMarkerAPI().putMarker(player.worldObj, player.dimension, stack.getItemDamage(),
defaultMarker, "Test marker", x, z);
//TODO add a GUI to enter marker label.
AntiqueAtlasMod.logger.info("Put marker in Atlas #" + stack.getItemDamage() + " at (" + x + ", " + z + ")");
isEnteringMarkerLabel = true;
markerFinalizer.setMarkerData(player.worldObj,
stack.getItemDamage(), player.dimension,
screenXToWorldX(mouseX), screenYToWorldZ(mouseY));
addChild(markerFinalizer).setCentered();
// Need to intercept keyboard events to type in the label:
//TODO: BUG: player walks infinitely in the same direction,
// if he was moving when he placed the marker.
setInterceptKeyboard(true);
}
isPlacingMarker = false;
}
Expand Down Expand Up @@ -404,7 +423,8 @@ public void drawScreen(int mouseX, int mouseY, float par3) {
markerX - (double)MARKER_ICON_WIDTH/2,
markerY - (double)MARKER_ICON_HEIGHT/2,
MARKER_ICON_WIDTH, MARKER_ICON_HEIGHT);
if (isPointInRadius((int)markerX, (int)markerY, MARKER_RADIUS, mouseX, mouseY)) {
if (isPointInRadius((int)markerX, (int)markerY, MARKER_RADIUS, mouseX, mouseY)
&& marker.getLabel().length() > 0) {
drawTopLevelHoveringText(Arrays.asList(marker.getLabel()), mouseX, mouseY, Minecraft.getMinecraft().fontRenderer);
}
}
Expand Down Expand Up @@ -441,7 +461,7 @@ public void drawScreen(int mouseX, int mouseY, float par3) {
if (isPlacingMarker) {
GL11.glColor4f(1, 1, 1, 0.5f);
AtlasRenderHelper.drawFullTexture(
MarkerTextureMap.instance().getTexture(defaultMarker),
MarkerTextureMap.instance().getTexture(GuiMarkerFinalizer.defaultMarker),
mouseX - MARKER_ICON_WIDTH/2, mouseY - MARKER_ICON_HEIGHT/2,
MARKER_ICON_WIDTH, MARKER_ICON_HEIGHT);
GL11.glColor4f(1, 1, 1, 1);
Expand All @@ -462,6 +482,7 @@ public boolean doesGuiPauseGame() {
@Override
public void onGuiClosed() {
isPlacingMarker = false;
markerFinalizer.close();
Keyboard.enableRepeatEvents(false);
}

Expand All @@ -480,4 +501,12 @@ private int worldXToScreenX(int x) {
private int worldZToScreenY(int z) {
return (int)Math.round((double)z / BLOCK_TO_PIXEL_RATIO + this.height/2 + mapOffsetY);
}

@Override
protected void onChildClosed(GuiComponent child) {
if (child.equals(markerFinalizer)) {
isEnteringMarkerLabel = false;
setInterceptKeyboard(false);
}
}
}
16 changes: 16 additions & 0 deletions src/hunternif/mc/atlas/client/gui/GuiComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public final void setRelativeY(int y) {
public final void offsetGuiCoords(int dx, int dy) {
setGuiCoords(guiX + dx, guiY + dy);
}
/** Position this component in the center of its parent. */
public final void setCentered() {
setRelativeCoords((parent.getWidth() - getWidth())/2, (parent.getHeight() - getHeight())/2);
}
/** Absolute X coordinate on the screen. */
public int getGuiX() {
return guiX;
Expand Down Expand Up @@ -413,4 +417,16 @@ private static class HoveringTextInfo {
* This flag is reset to false after rendering finishes. */
boolean shouldDraw = false;
}

/** Remove itself from its parent component (if any), notifying it. */
public void close() {
if (parent != null) {
GuiComponent oldParent = parent;
parent.removeChild(this);
oldParent.onChildClosed(this);
}
}

/** Called when a child removes itself from this component. */
protected void onChildClosed(GuiComponent child) {}
}
73 changes: 73 additions & 0 deletions src/hunternif/mc/atlas/client/gui/GuiMarkerFinalizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package hunternif.mc.atlas.client.gui;

import hunternif.mc.atlas.AntiqueAtlasMod;
import hunternif.mc.atlas.api.AtlasAPI;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.world.World;

/**
* This GUI is used to enter marker label and, in future, select its icon and
* color. When the user clicks on the confirmation button, the call to MarkerAPI
* is made.
* @author Hunternif
*/
public class GuiMarkerFinalizer extends GuiComponent {
public static final String defaultMarker = "red_x";

private World world;
private int atlasID, dimension, x, z;

private GuiButton btnDone;
private GuiTextField textField;

private FontRenderer font;

public void setMarkerData(World world, int atlasID, int dimension, int markerX, int markerZ) {
this.world = world;
this.atlasID = atlasID;
this.dimension = dimension;
this.x = markerX;
this.z = markerZ;
font = Minecraft.getMinecraft().fontRenderer;
}

@Override
public void initGui() {
buttonList.add(btnDone = new GuiButton(0, (this.width - 120)/2, this.height / 4 + 120, 120, 20, "Done"));
textField = new GuiTextField(font, (this.width - 200)/2, this.height/2 - 40, 200, 20);
textField.setFocused(true);
textField.setText("");
}

@Override
protected void mouseClicked(int par1, int par2, int par3) {
super.mouseClicked(par1, par2, par3);
textField.mouseClicked(par1, par2, par3);
}

@Override
protected void keyTyped(char par1, int par2) {
super.keyTyped(par1, par2);
textField.textboxKeyTyped(par1, par2);
}

protected void actionPerformed(GuiButton button) {
if (button == btnDone) {
AtlasAPI.getMarkerAPI().putMarker(world, dimension, atlasID, defaultMarker, textField.getText(), x, z);
AntiqueAtlasMod.logger.info("Put marker in Atlas #" + atlasID + " \"" + textField.getText() + "\" at (" + x + ", " + z + ")");
close();
}
}

@Override
public void drawScreen(int mouseX, int mouseY, float partialTick) {
drawDefaultBackground();
int stringWidth = font.getStringWidth("Enter label:");
font.drawStringWithShadow("Enter label:", (this.width - stringWidth)/2, this.height/2 - 57, 0xffffff);
textField.drawTextBox();
super.drawScreen(mouseX, mouseY, partialTick);
}
}
2 changes: 1 addition & 1 deletion src/hunternif/mc/atlas/marker/Marker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Marker implements Comparable<Marker> {

public Marker(String type, String label, int x, int y) {
this.type = type;
this.label = label;
this.label = label == null ? "" : label;
this.x = x;
this.y = y;
}
Expand Down

0 comments on commit 7db6e91

Please sign in to comment.