summaryrefslogtreecommitdiff
path: root/NET/worlds/console/UniversePanel.java
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-02-12 22:33:32 -0800
committerFuwn <[email protected]>2026-02-12 22:33:32 -0800
commitc7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch)
treedf9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/console/UniversePanel.java
downloadworldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz
worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip
Initial commit
Diffstat (limited to 'NET/worlds/console/UniversePanel.java')
-rw-r--r--NET/worlds/console/UniversePanel.java407
1 files changed, 407 insertions, 0 deletions
diff --git a/NET/worlds/console/UniversePanel.java b/NET/worlds/console/UniversePanel.java
new file mode 100644
index 0000000..856b00d
--- /dev/null
+++ b/NET/worlds/console/UniversePanel.java
@@ -0,0 +1,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;
+ }
+}