aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiNavigation.java
blob: d34988f6675d0c5de3d07956cda0bbe10f5ff14e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package io.github.moulberry.notenoughupdates.miscgui;

import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.core.GuiElementTextField;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class GuiNavigation extends GuiScreen {

	public static ResourceLocation BACKGROUND = new ResourceLocation(
		"notenoughupdates",
		"textures/gui/navigation.png"
	);

	public static final int PIN_POSITION_U = 182;
	public static final int PIN_POSITION_V = 3;
	public static final int TICK_POSITION_U = 182;
	public static final int TICK_POSITION_V = 34;
	public static final int ICON_SIZE = 26;

	public static final int SEARCH_BAR_X = 14;
	public static final int SEARCH_BAR_Y = 11;
	public static final int SEARCH_BAR_WIDTH = 151;
	public static final int SEARCH_BAR_HEIGHT = 24;

	public static final int LIST_START_X = 14;
	public static final int LIST_START_Y = 43;
	public static final int LIST_OFFSET_Y = 28;
	public static final int TEXT_OFFSET_X = 28;
	public static final int LIST_COUNT = 6;

	List<String> searchResults = new ArrayList<>();

	public int xSize = 176;
	public int ySize = 222;
	public int guiLeft, guiTop;

	public GuiElementTextField textField = new GuiElementTextField("", SEARCH_BAR_WIDTH, SEARCH_BAR_HEIGHT, 0);

	@Override
	public void initGui() {
		super.initGui();
		NotEnoughUpdates.INSTANCE.config.hidden.hasOpenedWaypointMenu = true;
		guiLeft = (width - xSize) / 2;
		guiTop = (height - ySize) / 2;
	}

	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks) {
		drawDefaultBackground();
		Minecraft.getMinecraft().getTextureManager().bindTexture(BACKGROUND);
		this.drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
		textField.render(guiLeft + SEARCH_BAR_X, guiTop + SEARCH_BAR_Y);

		refreshResults();
		for (int i = 0; i < LIST_COUNT; i++) {
			if (i < searchResults.size()) {
				Minecraft.getMinecraft().getTextureManager().bindTexture(BACKGROUND);
				String name = searchResults.get(i);
				JsonObject json = NotEnoughUpdates.INSTANCE.navigation.getWaypoints().get(name);
				boolean selected = name.equals(NotEnoughUpdates.INSTANCE.navigation.getInternalname());
				int baseX = guiLeft + LIST_START_X;
				int baseY = guiTop + LIST_START_Y + LIST_OFFSET_Y * i;

				GlStateManager.color(1F, 1F, 1F);
				drawTexturedModalRect(
					baseX,
					baseY,
					selected ? TICK_POSITION_U : PIN_POSITION_U, selected ? TICK_POSITION_V : PIN_POSITION_V,
					ICON_SIZE, ICON_SIZE
				);
				Utils.drawStringF(
					json.get("displayname").getAsString(),
					Minecraft.getMinecraft().fontRendererObj,
					baseX + TEXT_OFFSET_X,
					baseY + LIST_OFFSET_Y / 2F - Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT / 2F,
					false,
					0x000000
				);
			}
		}
	}

	private void refreshResults() {
		String text = textField.getText().toLowerCase();
		List<String> results = NotEnoughUpdates.INSTANCE.navigation
			.getWaypoints()
			.values()
			.stream()
			.filter(it ->
				it.get("internalname").getAsString().toLowerCase().contains(text)
					|| it.get("displayname").getAsString().toLowerCase().contains(text))
			.map(it -> it.get("internalname").getAsString())
			.sorted(Comparator.comparing(String::length)
												.thenComparing(String.CASE_INSENSITIVE_ORDER))
			.collect(Collectors.toList());

		String internalname = NotEnoughUpdates.INSTANCE.navigation.getInternalname();
		if (internalname != null) {
			results.remove(internalname);
			results.add(0, internalname);
		}
		searchResults = results;
	}

	@Override
	protected void keyTyped(char p_keyTyped_1_, int p_keyTyped_2_) throws IOException {
		super.keyTyped(p_keyTyped_1_, p_keyTyped_2_);
		textField.keyTyped(p_keyTyped_1_, p_keyTyped_2_);
	}

	@Override
	protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
		super.mouseClicked(mouseX, mouseY, mouseButton);
		if (Utils.isWithinRect(
			mouseX,
			mouseY,
			guiLeft + SEARCH_BAR_X,
			guiTop + SEARCH_BAR_Y,
			SEARCH_BAR_WIDTH,
			SEARCH_BAR_HEIGHT
		)) {
			textField.mouseClicked(mouseX, mouseY, mouseButton);
		} else {
			textField.setFocus(false);
		}
		for (int i = 0; i < LIST_COUNT; i++) {
			if (i < searchResults.size()) {
				int baseX = guiLeft + LIST_START_X;
				int baseY = guiTop + LIST_START_Y + LIST_OFFSET_Y * i;
				if (Utils.isWithinRect(mouseX, mouseY, baseX, baseY, ICON_SIZE, ICON_SIZE)) {
					String thing = searchResults.get(i);
					boolean selected = thing.equals(NotEnoughUpdates.INSTANCE.navigation.getInternalname());
					if (selected) {
						NotEnoughUpdates.INSTANCE.navigation.untrackWaypoint();
					} else {
						NotEnoughUpdates.INSTANCE.navigation.trackWaypoint(thing);
					}
					Utils.playPressSound();
				}
			}
		}
	}
}