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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
package NET.worlds.console;
import NET.worlds.core.IniFile;
import NET.worlds.core.Std;
import NET.worlds.network.NetUpdate;
import NET.worlds.network.ProgressDialog;
import NET.worlds.network.URL;
import NET.worlds.scape.BGLoaded;
import NET.worlds.scape.BackgroundLoader;
import NET.worlds.scape.Room;
import NET.worlds.scape.World;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
public class UniversePanel extends Panel implements MainCallback, ImageButtonsCallback, BGLoaded {
private static final long serialVersionUID = 4029344486393517219L;
UniverseImage bg;
int xOff = 0;
int yOff = 0;
ImageButtons backButton;
DefaultConsole owningConsole;
private static String datName = "universe/universe.dat";
private static String bgName = "universe/universe.jpg";
static String lastCacheDat;
static String lastCacheBg;
static Object locker = new Object();
Image offscreen;
private boolean registered;
int upDownState;
int leftRightState;
int lastKeyTime;
Vector<WorldButton> buttons = new Vector<WorldButton>();
Vector<WorldButtonBullet> bullets = new Vector<WorldButtonBullet>();
public UniversePanel(DefaultConsole cons) {
this.owningConsole = cons;
this.setLayout(null);
this.backButton = new ImageButtons(Console.message("back.gif"), 60, 22, this);
String rbgName = IniFile.override().getIniString("UniverseBgFile", bgName);
String rdatName = IniFile.override().getIniString("UniverseDatFile", datName);
if (!new File(datName).exists()) {
lastCacheDat = "";
if (!ProgressDialog.copyFile("universe.dat", datName)) {
ProgressDialog.copyFile("../universe.dat", datName);
}
}
this.readUniverseFile(datName);
if (!new File(bgName).exists()) {
lastCacheBg = "";
if (!ProgressDialog.copyFile("universe.jpg", bgName)) {
ProgressDialog.copyFile("../universe.jpg", bgName);
}
}
this.bg = new UniverseImage(bgName);
this.add(this.bg);
Dimension image = this.bg.imageSize();
this.bg.setSize(image.width, image.height);
String serv = NetUpdate.getUpgradeServerURL();
BackgroundLoader.get(this, URL.make(serv + rbgName));
BackgroundLoader.get(this, URL.make(serv + rdatName));
}
private boolean safeCopyFile(String name) {
synchronized (locker) {
if (name.endsWith("dat") && !name.equals(lastCacheDat)) {
lastCacheDat = name;
if (ProgressDialog.copyFile(name, datName)) {
this.readUniverseFile(datName);
this.add(this.bg);
return true;
}
} else if (name.endsWith("jpg") && !name.equals(lastCacheBg)) {
lastCacheBg = name;
if (ProgressDialog.copyFile(name, bgName)) {
this.remove(this.bg);
this.bg.flushImage();
this.add(this.bg = new UniverseImage(bgName));
Dimension image = this.bg.imageSize();
this.bg.setSize(image.width, image.height);
return true;
}
}
return false;
}
}
public void flushImage() {
this.remove(this.bg);
this.bg.flushImage();
}
@Override
public synchronized Object asyncBackgroundLoad(String localName, URL remoteURL) {
return localName;
}
@Override
public boolean syncBackgroundLoad(Object obj, URL remoteURL) {
String localName = (String)obj;
if (localName != null && new File(localName).exists() && this.safeCopyFile(localName)) {
this.invalidate();
this.validate();
this.doLayout();
this.repaint();
}
return false;
}
@Override
public Room getBackgroundLoadRoom() {
return null;
}
public synchronized void setViewportPos() {
Dimension viewport = this.getSize();
Dimension image = this.bg.imageSize();
int centerToLeft = image.width - viewport.width >> 1;
int centerToTop = image.height - viewport.height >> 1;
if (centerToLeft < 0) {
this.xOff = 0;
}
if (centerToTop < 0) {
this.yOff = 0;
}
int x = this.xOff + centerToLeft;
int y = this.yOff + centerToTop;
Dimension backSize = this.backButton.preferredSize();
this.backButton.setSize(backSize);
this.backButton.setLocation(viewport.width - backSize.width - 5, viewport.height - backSize.height - 3);
this.bg.setLocation(-x, -y);
int i = this.buttons.size();
while (--i >= 0) {
WorldButton b = this.buttons.elementAt(i);
b.setLocation(-x + b.buttonX, -y + b.buttonY);
WorldButtonBullet bl = this.bullets.elementAt(i);
bl.setLocation(-x + bl.circleX, -y + bl.circleY);
}
}
@Override
public void invalidate() {
super.invalidate();
this.offscreen = null;
}
@Override
public void update(Graphics g) {
this.paint(g);
}
@Override
public synchronized void paint(Graphics g) {
this.setViewportPos();
if (this.offscreen == null) {
this.offscreen = this.createImage(this.getSize().width, this.getSize().height);
}
Graphics og = this.offscreen.getGraphics();
og.setClip(0, 0, this.getSize().width, this.getSize().height);
super.paint(og);
try {
g.drawImage(this.offscreen, 0, 0, this);
} catch (NullPointerException var4) {
this.offscreen = null;
}
og.dispose();
this.startWatch();
}
public synchronized void startWatch() {
if (!this.registered) {
Main.register(this);
this.registered = true;
}
}
public synchronized void stopWatch() {
if (this.registered) {
Main.unregister(this);
this.registered = false;
}
}
public void setOffset(int x, int y) {
if (this.owningConsole.isUniverseMode()) {
Dimension viewport = this.getSize();
Dimension image = this.bg.imageSize();
int centerToLeft = image.width - viewport.width >> 1;
int centerToTop = image.height - viewport.height >> 1;
x += centerToLeft;
y += centerToTop;
if (x < 0) {
x = 0;
} else if (x + viewport.width > image.width) {
x = image.width - viewport.width;
}
if (y < 0) {
y = 0;
} else if (y + viewport.height > image.height) {
y = image.height - viewport.height;
}
x -= centerToLeft;
y -= centerToTop;
if (viewport.width > image.width) {
x = 0;
}
if (viewport.height > image.height) {
y = 0;
}
if (x != this.xOff || y != this.yOff) {
this.xOff = x;
this.yOff = y;
this.repaint();
}
}
}
public void addOffset(int dx, int dy) {
this.setOffset(this.xOff + dx, this.yOff + dy);
}
@Override
public boolean keyUp(Event event, int key) {
if (key != 1004 && key != 1005 && key != 1006 && key != 1007) {
return super.keyUp(event, key);
} else {
if (this.upDownState != 0 || this.leftRightState != 0) {
this.mainCallback();
this.upDownState = 0;
this.leftRightState = 0;
}
return true;
}
}
@Override
public synchronized void mainCallback() {
int now = Std.getRealTime();
int xDiff = 0;
int yDiff = 0;
if (this.upDownState != 0) {
yDiff = this.upDownState * (now - this.lastKeyTime) / 5;
}
if (this.leftRightState != 0) {
xDiff = this.leftRightState * (now - this.lastKeyTime) / 5;
}
if (xDiff < -50) {
xDiff = -50;
}
if (xDiff > 50) {
xDiff = 50;
}
if (yDiff < -50) {
yDiff = -50;
}
if (yDiff > 50) {
yDiff = 50;
}
String curpkg = WorldsMarkPart.getCurrentPackageName();
if (curpkg != null && !curpkg.equals(WorldButton.currentPackageName)) {
WorldButton.currentPackageName = curpkg;
if (xDiff == 0 && yDiff == 0) {
this.repaint();
}
}
if (xDiff != 0 || yDiff != 0) {
this.addOffset(xDiff, yDiff);
}
this.lastKeyTime = now;
if (this.owningConsole != Console.getActive() || !this.owningConsole.isUniverseMode()) {
this.stopWatch();
}
}
@Override
public synchronized boolean keyDown(Event event, int key) {
boolean changed = false;
if (key == 1004) {
if (this.upDownState != -1) {
this.upDownState = -1;
changed = true;
}
} else if (key == 1005) {
if (this.upDownState != 1) {
this.upDownState = 1;
changed = true;
}
} else if (key == 1006) {
if (this.leftRightState != -1) {
this.leftRightState = -1;
changed = true;
}
} else {
if (key != 1007) {
return super.keyDown(event, key);
}
if (this.leftRightState != 1) {
this.leftRightState = 1;
changed = true;
}
}
if (changed) {
this.startWatch();
this.lastKeyTime = Std.getRealTime() - 20;
}
return true;
}
private synchronized void readUniverseFile(String localName) {
this.removeAll();
this.buttons = new Vector<WorldButton>();
this.bullets = new Vector<WorldButtonBullet>();
boolean showAll = IniFile.gamma().getIniInt("ShowAllWorlds", 0) != 0;
this.add(this.backButton);
try {
FileInputStream in = new FileInputStream(localName);
DataInputStream dis = new DataInputStream(in);
String line;
while ((line = dis.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
if (st.hasMoreTokens() && line.charAt(0) != ';') {
int circleX = Integer.parseInt(st.nextToken());
int circleY = Integer.parseInt(st.nextToken());
int buttonX = Integer.parseInt(st.nextToken());
int buttonY = Integer.parseInt(st.nextToken());
String pkg = st.nextToken();
int privacy = Integer.parseInt(st.nextToken());
String text = st.nextToken("").trim();
Dimension sz = WorldButton.measure(text);
String[] texts = new String[]{text};
boolean isLoaded = WorldsMarkPart.findPackage(pkg) != null;
if (showAll
|| privacy == 0
|| privacy == 1 && !World.isCloistered()
|| privacy <= 2 && isLoaded
|| privacy == 3 && !World.isWorldsStoreProscribed()) {
WorldButton button = new WorldButton(isLoaded, buttonX, buttonY, sz.width, sz.height, texts, pkg, privacy, this.owningConsole, this);
this.add(button);
this.buttons.addElement(button);
WorldButtonBullet bullet = new WorldButtonBullet(circleX, circleY, button);
if (circleX > 0 || circleY > 0) {
this.add(bullet);
}
this.bullets.addElement(bullet);
}
}
}
dis.close();
in.close();
} catch (FileNotFoundException var19) {
System.out.println(var19);
} catch (IOException var20) {
System.out.println(var20);
}
}
@Override
public Object imageButtonsCallback(Component who, int which) {
if (who instanceof WorldButton) {
((WorldButton)who).doAction();
} else if (who == this.backButton) {
this.owningConsole.toggleUniverseMode();
}
return null;
}
}
|