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
|
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();
}
}
|