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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021, 2022 Polyfrost.
* <https://polyfrost.cc> <https://github.com/Polyfrost/>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation, AND
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
* either version 1.0 of the Additional Terms, or (at your option) any later
* version.
*
* This program 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. If not, see <https://www.gnu.org/licenses/>. You should
* have also received a copy of the Additional Terms Applicable
* to OneConfig, as published by Polyfrost. If not, see
* <https://polyfrost.cc/legal/oneconfig/additional-terms>
*/
package cc.polyfrost.oneconfig.hud;
import cc.polyfrost.oneconfig.config.annotations.Exclude;
import cc.polyfrost.oneconfig.libs.universal.UResolution;
import com.google.gson.annotations.SerializedName;
public class Position {
private AnchorPosition anchor;
private float x;
private float y;
@Exclude
private float width;
@Exclude
private float height;
/**
* Position object used for huds
*
* @param x The X coordinate
* @param y The Y coordinate
* @param width The width of the HUD
* @param height The height of the HUD
* @param screenWidth The width of the screen to initialize the position width
* @param screenHeight The height of the screen to initialize the position width
*/
public Position(float x, float y, float width, float height, float screenWidth, float screenHeight) {
setSize(width, height);
setPosition(x, y, screenWidth, screenHeight);
}
/**
* Position object used for huds
*
* @param x The X coordinate
* @param y The Y coordinate
* @param width The width of the HUD
* @param height The height of the HUD
*/
public Position(float x, float y, float width, float height) {
this(x, y, width, height, 1920, 1080);
}
/**
* Set the position
*
* @param x The X coordinate
* @param y The Y coordinate
* @param screenWidth The screen width
* @param screenHeight The screen height
*/
public void setPosition(float x, float y, float screenWidth, float screenHeight) {
float rightX = x + width;
float bottomY = y + height;
if (x <= screenWidth / 3f && y <= screenHeight / 3f)
this.anchor = AnchorPosition.TOP_LEFT;
else if (rightX >= screenWidth / 3f * 2f && y <= screenHeight / 3f)
this.anchor = AnchorPosition.TOP_RIGHT;
else if (x <= screenWidth / 3f && bottomY >= screenHeight / 3f * 2f)
this.anchor = AnchorPosition.BOTTOM_LEFT;
else if (rightX >= screenWidth / 3f * 2f && bottomY >= screenHeight / 3f * 2f)
this.anchor = AnchorPosition.BOTTOM_RIGHT;
else if (y <= screenHeight / 3f)
this.anchor = AnchorPosition.TOP_CENTER;
else if (x <= screenWidth / 3f)
this.anchor = AnchorPosition.MIDDLE_LEFT;
else if (rightX >= screenWidth / 3f * 2f)
this.anchor = AnchorPosition.MIDDLE_RIGHT;
else if (bottomY >= screenHeight / 3f * 2f)
this.anchor = AnchorPosition.BOTTOM_CENTER;
else
this.anchor = AnchorPosition.MIDDLE_CENTER;
this.x = x - getAnchorX(screenWidth) + getAnchorX(width);
this.y = y - getAnchorY(screenHeight) + getAnchorY(height);
}
/**
* Set the position
*
* @param x The X coordinate
* @param y The Y coordinate
*/
public void setPosition(float x, float y) {
setPosition(x, y, UResolution.getScaledWidth(), UResolution.getScaledHeight());
}
/**
* Set the size of the position
*
* @param width The width
* @param height The height
*/
public void setSize(float width, float height) {
this.width = width;
this.height = height;
}
/**
* Update the position so the top left corner stays in the same spot
*
* @param width The width
* @param height The height
*/
public void updateSizePosition(float width, float height) {
float x = getX();
float y = getY();
setSize(width, height);
setPosition(x, y);
}
/**
* Get the X coordinate scaled to the size of the screen
*
* @param screenWidth The width of the screen
* @return The X coordinate
*/
public float getX(float screenWidth) {
return x + getAnchorX(screenWidth) - getAnchorX(width);
}
/**
* Get the X coordinate scaled to the size of the screen
*
* @return The X coordinate
*/
public float getX() {
return getX(UResolution.getScaledWidth());
}
/**
* Get the Y coordinate scaled to the size of the screen
*
* @param screenHeight The height of the screen
* @return The Y coordinate
*/
public float getY(float screenHeight) {
return y + getAnchorY(screenHeight) - getAnchorY(height);
}
/**
* Get the Y coordinate scaled to the size of the screen
*
* @return The Y coordinate
*/
public float getY() {
return getY(UResolution.getScaledHeight());
}
/**
* Get the X coordinate scaled to the size of the screen of the right corner
*
* @param screenWidth The width of the screen
* @return The X coordinate of the right corner
*/
public float getRightX(float screenWidth) {
return getX(screenWidth) + width;
}
/**
* Get the X coordinate scaled to the size of the screen of the right corner
*
* @return The X coordinate of the right corner
*/
public float getRightX() {
return getRightX(UResolution.getScaledWidth());
}
/**
* Get the Y coordinate scaled to the size of the screen of the bottom corner
*
* @param screenHeight The width of the screen
* @return The Y coordinate of the bottom corner
*/
public float getBottomY(float screenHeight) {
return getY(screenHeight) + height;
}
/**
* Get the Y coordinate scaled to the size of the screen of the bottom corner
*
* @return The Y coordinate of the bottom corner
*/
public float getBottomY() {
return getBottomY(UResolution.getScaledHeight());
}
/**
* Get the center X coordinate
*
* @param screenWidth The width of the screen
* @return The center X coordinate
*/
public float getCenterX(float screenWidth) {
return getX(screenWidth) + width / 2f;
}
/**
* Get the center X coordinate
*
* @return The center X coordinate
*/
public float getCenterX() {
return getCenterX(UResolution.getScaledWidth());
}
/**
* Get the center Y coordinate
*
* @param screenHeight The width of the screen
* @return The center Y coordinate
*/
public float getCenterY(float screenHeight) {
return getY(screenHeight) + height / 2f;
}
/**
* Get the center Y coordinate
*
* @return The center Y coordinate
*/
public float getCenterY() {
return getCenterY(UResolution.getScaledHeight());
}
/**
* @return The width of the position
*/
public float getWidth() {
return width;
}
/**
* @return The height of the position
*/
public float getHeight() {
return height;
}
private float getAnchorX(float value) {
return value * anchor.x;
}
private float getAnchorY(float value) {
return value * anchor.y;
}
/**
* Position of the anchors were the position is relative too
*/
public enum AnchorPosition {
@SerializedName("0")
TOP_LEFT(0f, 0f),
@SerializedName("1")
TOP_CENTER(0.5f, 0f),
@SerializedName("2")
TOP_RIGHT(1f, 0f),
@SerializedName("3")
MIDDLE_LEFT(0f, 0.5f),
@SerializedName("4")
MIDDLE_CENTER(0.5f, 0.5f),
@SerializedName("5")
MIDDLE_RIGHT(1f, 0.5f),
@SerializedName("6")
BOTTOM_LEFT(0f, 1f),
@SerializedName("7")
BOTTOM_CENTER(0.5f, 1f),
@SerializedName("8")
BOTTOM_RIGHT(1f, 1f);
public final float x;
public final float y;
AnchorPosition(float x, float y) {
this.x = x;
this.y = y;
}
}
}
|