aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/mbgui/MBGuiGroupFloating.java
blob: 770a8423781fdc7e1724c46fa54bad66189d19ce (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
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.mbgui;

import io.github.moulberry.notenoughupdates.miscgui.GuiItemRecipe;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.gui.inventory.GuiContainer;
import org.lwjgl.util.vector.Vector2f;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class MBGuiGroupFloating extends MBGuiGroup {
	private GuiScreen lastScreen = null;
	private final HashMap<MBGuiElement, Vector2f> childrenPositionOffset = new HashMap<>();

	//Serialized
	private final LinkedHashMap<MBGuiElement, MBAnchorPoint> children;

	public MBGuiGroupFloating(int width, int height, LinkedHashMap<MBGuiElement, MBAnchorPoint> children) {
		this.width = width;
		this.height = height;
		this.children = children;
		recalculate();
	}

	public Map<MBGuiElement, MBAnchorPoint> getChildrenMap() {
		return Collections.unmodifiableMap(children);
	}

	@Override
	public Map<MBGuiElement, Vector2f> getChildrenPosition() {
		GuiScreen currentScreen = Minecraft.getMinecraft().currentScreen;

		if (currentScreen instanceof GuiContainer || currentScreen instanceof GuiItemRecipe) {

			if (lastScreen != currentScreen) {
				lastScreen = currentScreen;

				ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
				int screenWidth = scaledResolution.getScaledWidth();
				int screenHeight = scaledResolution.getScaledHeight();

				int xSize = -1;
				int ySize = -1;
				int guiLeft = -1;
				int guiTop = -1;

				if (currentScreen instanceof GuiContainer) {
					GuiContainer currentContainer = (GuiContainer) currentScreen;

					try {
						xSize = (int) Utils.getField(GuiContainer.class, currentContainer, "xSize", "field_146999_f");
						ySize = (int) Utils.getField(GuiContainer.class, currentContainer, "ySize", "field_147000_g");
						guiLeft = (int) Utils.getField(GuiContainer.class, currentContainer, "guiLeft", "field_147003_i");
						guiTop = (int) Utils.getField(GuiContainer.class, currentContainer, "guiTop", "field_147009_r");
					} catch (Exception ignored) {
					}
				} else {
					xSize = ((GuiItemRecipe) currentScreen).xSize;
					ySize = ((GuiItemRecipe) currentScreen).ySize;
					guiLeft = ((GuiItemRecipe) currentScreen).guiLeft;
					guiTop = ((GuiItemRecipe) currentScreen).guiTop;
				}

				if (xSize <= 0 && ySize <= 0 && guiLeft <= 0 && guiTop <= 0) {
					lastScreen = null;
					return Collections.unmodifiableMap(childrenPosition);
				}

				for (Map.Entry<MBGuiElement, MBAnchorPoint> entry : children.entrySet()) {
					MBGuiElement child = entry.getKey();
					MBAnchorPoint anchorPoint = entry.getValue();

					Vector2f childPos;
					if (childrenPosition.containsKey(child)) {
						childPos = new Vector2f(childrenPosition.get(child));
					} else {
						childPos = new Vector2f();
					}

					if (anchorPoint.inventoryRelative) {
						int defGuiLeft = (screenWidth - xSize) / 2;
						int defGuiTop = (screenHeight - ySize) / 2;

						childPos.x += guiLeft - defGuiLeft + (0.5f - anchorPoint.anchorPoint.x) * xSize;
						childPos.y += guiTop - defGuiTop + (0.5f - anchorPoint.anchorPoint.y) * ySize;
					}

					childrenPositionOffset.put(child, childPos);
				}
			}
			return Collections.unmodifiableMap(childrenPositionOffset);
		} else {
			return Collections.unmodifiableMap(childrenPosition);
		}
	}

	@Override
	public void recalculate() {
		lastScreen = null;

		for (MBGuiElement child : children.keySet()) {
			child.recalculate();
		}

		for (Map.Entry<MBGuiElement, MBAnchorPoint> entry : children.entrySet()) {
			MBGuiElement child = entry.getKey();
			MBAnchorPoint anchorPoint = entry.getValue();
			float x = anchorPoint.anchorPoint.x * width - anchorPoint.anchorPoint.x * child.getWidth() + anchorPoint.offset.x;
			float y =
				anchorPoint.anchorPoint.y * height - anchorPoint.anchorPoint.y * child.getHeight() + anchorPoint.offset.y;

			if (anchorPoint.inventoryRelative) {
				x = width * 0.5f + anchorPoint.offset.x;
				y = height * 0.5f + anchorPoint.offset.y;
			}

			childrenPosition.put(child, new Vector2f(x, y));
		}
	}

	@Override
	public Collection<MBGuiElement> getChildren() {
		return children.keySet();
	}
}