package NET.worlds.console; import NET.worlds.core.IniFile; import NET.worlds.core.Std; import NET.worlds.network.URL; import NET.worlds.scape.BGLoaded; import NET.worlds.scape.BackgroundLoader; import NET.worlds.scape.Camera; import NET.worlds.scape.FrameEvent; import NET.worlds.scape.LoadedURLSelf; import NET.worlds.scape.Material; import NET.worlds.scape.Pilot; import NET.worlds.scape.Rect; import NET.worlds.scape.Room; import NET.worlds.scape.SendURLAction; import NET.worlds.scape.SuperRoot; import NET.worlds.scape.Transform; import NET.worlds.scape.URLSelf; import NET.worlds.scape.WObject; import NET.worlds.scape.World; import java.awt.Container; import java.awt.Dimension; import java.awt.Event; import java.awt.Point; import java.io.File; import java.text.MessageFormat; public class AdPart extends RenderCanvas implements LoadedURLSelf, BGLoaded { private static final long serialVersionUID = 4290496811864911807L; private static World world; private static Room room; private static Material[] faces = new Material[4]; private static WObject cube; private static Transform cubeStart; private float angle; private int currentFace = -1; private int lastAd = -1; private boolean lastAdIsRemote = false; private boolean initialized; URL image0; int nextAdNumber; int nextLoadTime; int lastFrame; static boolean cycleAds = IniFile.gamma().getIniInt("CYCLEADS", 1) == 1; public AdPart(URL defaultImage) { super(new Dimension(130, 130)); this.image0 = defaultImage; } private void init() { if (!this.initialized) { if (world == null) { World.load(URL.make("home:ad.world"), this, true); } else { this.initCamera(); } } } private void initCamera() { this.setCamera(new Camera()); room.add(this.getCamera()); this.getCamera().moveTo(room.getDefaultPosition()).spin(room.getDefaultOrientationAxis(), room.getDefaultOrientation()); } @Override public synchronized void loadedURLSelf(URLSelf o, URL url, String err) { if (world != null) { this.initCamera(); } else if (o == null) { Object[] arguments = new Object[]{new String("" + url)}; Console.println(MessageFormat.format(Console.message("Error-ad"), arguments)); } else { world = (World)o; room = world.getRoom(world.getDefaultRoomName()); this.initCamera(); cube = (WObject)SuperRoot.nameSearch(room.getContents(), "Cube"); if (cube != null) { cubeStart = cube.getTransform(); for (int i = 0; i < 4; i++) { Rect r = (Rect)SuperRoot.nameSearch(cube.getContents(), "Rect" + (i + 1)); if (r != null) { faces[i] = r.getMaterial(); } } } } } private String getAdBaseURL() { World world = Pilot.getActiveWorld(); return world == null ? "ads/ad" : world.getAdCubeBaseURL(); } @Override public boolean action(Event event, Object what) { return false; } @Override public boolean mouseDown(Event e, int x, int y) { World w = Pilot.getActiveWorld(); if (w == null || !w.getHasClickableAdCube()) { return false; } else if ((e.modifiers & 12) != 0) { return false; } else { if (this.nextAdNumber > 1 && this.getAdBaseURL() != null) { new SendURLAction(this.getAdBaseURL() + (this.nextAdNumber - 1) + ".html").startBrowser(); } else { new SendURLAction(w.getDefaultAdCubeURL()).startBrowser(); } return true; } } @Override public Object asyncBackgroundLoad(String localName, URL remoteURL) { return localName; } @Override public boolean syncBackgroundLoad(Object obj, URL remoteURL) { String localName = (String)obj; if (remoteURL != null != this.lastAdIsRemote) { this.lastAd = -1; } this.lastAdIsRemote = remoteURL != null; if (localName != null && new File(localName).exists()) { remoteURL = URL.make(localName); } else { this.lastAd = this.nextAdNumber; this.nextAdNumber = 0; remoteURL = this.image0; } if (++this.currentFace > 3) { this.currentFace = 0; } Material m = faces[this.currentFace]; if (m != null) { m.loadTexture(remoteURL); } if (++this.nextAdNumber == this.lastAd) { this.nextAdNumber = 0; } this.nextLoadTime = Std.getFastTime() + 10000; return false; } @Override public Room getBackgroundLoadRoom() { return null; } @Override public synchronized boolean handle(FrameEvent f) { if (this.getCamera() != null && cube != null) { int now = Std.getRealTime(); if (now >= this.nextLoadTime) { if (this.nextAdNumber == 0) { this.syncBackgroundLoad(null, null); } else if (cycleAds) { String extension = ".cmp"; World w = Pilot.getActiveWorld(); if (w != null && w.getAdCubeFormatIsGif()) { extension = ".gif"; } String name = this.getAdBaseURL() + this.nextAdNumber + extension; BackgroundLoader.get(this, URL.make(name)); this.nextLoadTime = now + 10000; } } else { float targetAngle = (float) (Math.PI / 2) * this.currentFace; if (targetAngle != this.angle) { int duration = now - this.lastFrame; float rot = (float) (Math.PI / 4) * (duration / 1000.0F); if (targetAngle == 0.0F) { targetAngle = (float) (Math.PI * 2); } float dist = targetAngle - this.angle; if (rot >= dist) { this.angle = (float) (Math.PI / 2) * this.currentFace; } else { this.angle += rot; } cube.setTransform(cubeStart); cube.spin(0.0F, 0.0F, -1.0F, this.angle * 180.0F / (float) Math.PI); } } this.lastFrame = now; if (this.checkForWindow(false)) { Point pt = this.getLocationOnScreen(); Dimension dim = this.getSize(); this.getWindow().reShape(pt.x, pt.y, dim.width, dim.height); this.getCamera().renderToCanvas(); } return true; } else { return true; } } @Override public void activate(Console c, Container f, Console prev) { super.activate(c, f, prev); this.init(); } }