forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.java
More file actions
310 lines (267 loc) · 7.94 KB
/
Copy pathWindow.java
File metadata and controls
310 lines (267 loc) · 7.94 KB
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
package javaforce.ui;
/** OpenGL Window.
*
* @author pquiring
*/
import java.util.*;
import javaforce.*;
import javaforce.jni.*;
import javaforce.gl.*;
import javaforce.ui.theme.*;
import static javaforce.gl.GL.*;
public class Window {
private static native boolean ninit();
public static boolean init() {
return ninit();
}
private long id;
private int width, height;
private int scale = 1;
private KeyEvents keys;
private MouseEvents mouse;
private WindowEvents window;
private boolean visible;
private Texture texture;
private Object3 object;
private boolean active = true;
private Component content;
private static ArrayList<Canvas> canvasList = new ArrayList<>();
private static ArrayList<Window> windows = new ArrayList<>();
private static final int KEY_TYPED = 1;
private static final int KEY_PRESS = 2;
private static final int KEY_RELEASE = 3;
private static final int MOUSE_MOVE = 4;
private static final int MOUSE_DOWN = 5;
private static final int MOUSE_UP = 6;
private static final int MOUSE_SCROLL = 7;
private static final int WIN_RESIZE = 8;
private static final int WIN_CLOSING = 9;
/** This is called from native code to dispatch events. */
private void dispatchEvent(int type, int v1, int v2) {
try {
switch (type) {
case KEY_TYPED: if (keys != null) keys.keyTyped((char)v1); break;
case KEY_PRESS: if (keys != null) keys.keyPressed((char)v1); break;
case KEY_RELEASE: if (keys != null) keys.keyReleased((char)v1); break;
case MOUSE_MOVE: if (mouse != null) mouse.mouseMove(v1 / scale, v2 / scale); break;
case MOUSE_DOWN: if (mouse != null) mouse.mouseDown(v1); break;
case MOUSE_UP: if (mouse != null) mouse.mouseUp(v1); break;
case MOUSE_SCROLL: if (mouse != null) mouse.mouseScroll(v1, v2); break;
case WIN_RESIZE: resize(v1, v2); if (window != null) window.windowResize(v1 / scale, v2 / scale); break;
case WIN_CLOSING: if (window != null) window.windowClosing(); break;
}
} catch (Exception e) {
JFLog.log(e);
}
}
public static final int STYLE_VISIBLE = 1;
public static final int STYLE_RESIZABLE = 2;
public static final int STYLE_TITLEBAR = 4;
public static final int STYLE_FULLSCREEN = 8;
private static native long ncreate(int style, String title, int width, int height, Window eventMgr, long shared);
public boolean create(int style, String title, int width, int height, Window shared) {
this.width = width;
this.height = height;
id = ncreate(style, title, width, height, this, shared == null ? 0 : shared.id);
if (id != 0) {
synchronized(windows) {
windows.add(this);
}
}
return id != 0;
}
private static native void ndestroy(long id);
/** Show the window. */
public void destroy() {
if (id == 0) return;
active = false;
ndestroy(id);
id = 0;
synchronized(windows) {
windows.remove(this);
}
}
private static native void nsetcurrent(long id);
/** Set the OpenGL Context current for this window. */
public void setCurrent() {
nsetcurrent(id);
}
private static native void nseticon(long id, String icon, int x, int y);
/** Set an icon to the window.
* This function is somewhat platform dependant.
* Only windows is supported currently.
* @param filename = file (.ico for windows)
*/
public void setIcon(String filename, int x, int y) {
nseticon(id, filename, x, y);
}
public void setKeyListener(KeyEvents keys) {
this.keys = keys;
}
public void setMouseListener(MouseEvents mouse) {
this.mouse = mouse;
}
public void setWindowListener(WindowEvents window) {
this.window = window;
}
/** Polls for events.
* @param wait = time to wait for event to occur
* -1 = wait forever
* 0 = do not wait
* x = wait x milliseconds
*/
public static native void pollEvents(int wait);
/** Posts an empty event to wake main thread. */
public static native void postEvent();
/** Polls for events. Does not wait for an event. Same as pollEvents(0); */
public static void pollEvents() {
pollEvents(0);
}
private static native void nshow(long id);
/** Show the window. */
public void show() {
nshow(id);
visible = true;
}
private static native void nhide(long id);
/** Hide the window. */
public void hide() {
nhide(id);
visible = false;
}
private static native void nswap(long id);
/** Swaps the OpenGL Buffers. */
public void swap() {
nswap(id);
}
private static native void nhidecursor(long id);
/** Hide the cursor. */
public void hideCursor() {
nhidecursor(id);
}
private static native void nshowcursor(long id);
/** Show the cursor (default). */
public void showCursor() {
nshowcursor(id);
}
private static native void nlockcursor(long id);
/** Hide the cursor and lock to this window.
* Use showCursor() to unlock.
*/
public void lockCursor() {
nlockcursor(id);
}
private static native void ngetpos(long id, int[] pos);
/** Get window position.
* @return int[0] = x, int[1] = y
*/
public int[] getPosition() {
int[] ret = new int[2];
ngetpos(id, ret);
return ret;
}
private static native void nsetpos(long id, int x, int y);
/** set window position.
* return: int[0] = x, int[1] = y
*/
public void setPosition(int x,int y) {
nsetpos(id, x, y);
}
public int getWidth() {
return width;
}
public int getWidthScaled() {
return width / scale;
}
public int getHeight() {
return height;
}
public int getHeightScaled() {
return height / scale;
}
public static Window[] getWindows() {
synchronized(windows) {
return windows.toArray(new Window[windows.size()]);
}
}
public Component getContent() {
return content;
}
public void setContent(Component content) {
this.content = content;
keys = content;
mouse = content;
layout();
}
public int getScale() {
return scale;
}
public void setScale(int scale) {
if (scale < 1) scale = 1;
if (scale > 4) scale = 4;
this.scale = scale;
layout();
}
public void layout() {
if (content != null) {
content.layout(new LayoutMetrics(getWidthScaled(), getHeightScaled()));
}
}
public void resize(int w, int h) {
width = w;
height = h;
if (content != null) {
layout();
}
}
public static void registerCanvas(Canvas canvas) {
canvasList.add(canvas);
}
public void render(Scene scene) {
if (debug) System.out.println("Window.render()");
if (texture == null || texture.getWidth() != getWidthScaled() || texture.getHeight() != getHeightScaled()) {
if (texture != null) {
texture.unload();
texture = null;
}
if (object != null) {
object.freeBuffers();
object = null;
}
texture = new Texture(0, getWidthScaled(), getHeightScaled());
}
if (object == null) {
object = new Object3();
object.createUVMap();
//add vertex ccw
int z = -1;
object.addVertex(new float[] {0,0,z}, new float[] {0,1});
object.addVertex(new float[] {1,0,z}, new float[] {1,1});
object.addVertex(new float[] {1,1,z}, new float[] {1,0});
object.addVertex(new float[] {0,1,z}, new float[] {0,0});
//add triangle ccw
object.addPoly(new int[] {0,1,3});
object.addPoly(new int[] {1,2,3});
object.copyBuffers();
}
canvasList.clear();
texture.getImage().fill(0, 0, width, height, Color.OPAQUE + Theme.getTheme().getBackColor().getColor());
content.render(texture.getImage());
setCurrent();
clear(Color.black, getWidth(), getHeight());
texture.loaded = false;
texture.load();
object.bindBuffers(scene);
texture.bind();
object.render(scene);
glFlush();
swap();
}
public Canvas[] getCanvasList() {
return canvasList.toArray(new Canvas[0]);
}
public static void redrawAll() {
if (windows.size() == 0) return;
postEvent();
}
}