Author:
deepthought (posted
2015-09-07 03:00:39 , viewed 372 times)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package SPAGE .engine .gui ;
import java .awt .Graphics ;
import java .awt .Graphics2D ;
import java .awt .event .InvocationEvent ;
import java .awt .event .KeyEvent ;
import java .awt .image .BufferedImage ;
import java .nio .IntBuffer ;
import java .util .Hashtable ;
import java .util .concurrent .CountDownLatch ;
import java .util .concurrent .locks .ReentrantLock ;
import javax .swing .JPanel ;
import sun .awt .AppContext ;
import sun .awt .SunToolkit ;
import sun .util .logging .PlatformLogger .Level ;
import javafx .application .Platform ;
import javafx .embed .swing .JFXPanel ;
import javafx .scene .Scene ;
import javafx .scene .image .Image ;
import javafx .scene .image .PixelReader ;
import com .sun .javafx .application .PlatformImpl ;
import com .sun .javafx .cursor .CursorFrame ;
import com .sun .javafx .cursor .CursorType ;
import com .sun .javafx .cursor .ImageCursorFrame ;
import com .sun .javafx .embed .AbstractEvents ;
import com .sun .javafx .embed .EmbeddedSceneInterface ;
import com .sun .javafx .embed .EmbeddedStageInterface ;
import com .sun .javafx .embed .HostInterface ;
import com .sun .javafx .stage .EmbeddedWindow ;
import com .sun .javafx .tk .Toolkit ;
public class GLStagePlatform {
private volatile EmbeddedWindow stage ;
private volatile Scene scene ;
private EmbeddedStageInterface stagePeer ;
private EmbeddedSceneInterface scenePeer ;
private HostContainer hostContainer ;
private texupdater tex ;
private int width ;
private int height ;
private int scaleFactor = 1 ;
private boolean leftbtn ,midbtn ,rightbtn ;
private boolean shift ,ctrl ,alt ,meta ;
private boolean dirty =true ;
private int mousex ,mousey ;
private CursorChangeListener cursListen ;
private boolean drawcursor ;
private ImageCursorFrame currentframe ;
private Hashtable <CursorType ,ImageCursorFrame > cursorimages = new Hashtable <CursorType ,ImageCursorFrame >();
ReentrantLock cursorlock = new ReentrantLock ();
public boolean getPixels (IntBuffer dest , int width , int height )
{
if (scenePeer !=null )
{
boolean success =scenePeer .getPixels (dest , width , height );
if (success )
{
if (drawcursor &¤tframe !=null )
{
cursorlock .lock ();
System .out .println ("draw cursor" );
com .sun .prism .Image img =(com .sun .prism .Image ) currentframe .getPlatformImage ();
for (int x =0 ;x <currentframe .getWidth ();x ++)
{
for (int y =0 ;y <currentframe .getHeight ();y ++)
{
int framex =(int ) (mousex -currentframe .getHotspotX ()+x );
int framey =(int ) (mousey -currentframe .getHotspotY ()+y );
if (framex >=0 &&framex <=width &&framey >=0 &&framey <=height )
{
int index =framex +(width *framey );
int destcolor =dest .get (index );
int srccolor =img .getArgb (x , y );
int endcolor =AlphaBlend (destcolor ,srccolor );
dest .put (index , endcolor );
}
}
}
cursorlock .unlock ();
}
return true ;
}
}
return false ;
}
public GLStagePlatform (int w , int h , texupdater t )
{
tex =t ;
tex .giveplatform (this );
width =w ;
height =h ;
initFx ();
hostContainer = new HostContainer ();
}
private synchronized static void initFx () {
PlatformImpl .startup (() -> {
});
}
public void setScene (final Scene newScene ) {
if (Toolkit .getToolkit ().isFxUserThread ()) {
setSceneImpl (newScene );
} else {
final CountDownLatch initLatch = new CountDownLatch (1 );
Platform .runLater (() -> {
setSceneImpl (newScene );
initLatch .countDown ();
});
try {
initLatch .await ();
} catch (InterruptedException z ) {
z .printStackTrace (System .err );
}
}
}
private void setSceneImpl (Scene newScene ) {
if ((stage != null ) && (newScene == null )) {
stage .hide ();
stage = null ;
}
scene = newScene ;
if ((stage == null ) && (newScene != null )) {
stage = new EmbeddedWindow (hostContainer );
}
if (stage != null ) {
stage .setScene (newScene );
if (!stage .isShowing ()) {
stage .show ();
}
}
}
public Scene getScene () {
return scene ;
}
private void invokeOnClientEDT (Runnable r ) {
AppContext context = SunToolkit .targetToAppContext (this );
if (context == null ) {
return ;
}
SunToolkit .postEvent (context , new InvocationEvent (this , r ));
}
private class HostContainer implements HostInterface {
public boolean grabFocus () {
System .out .println ("grabfocus" );
return false ;
}
public void repaint () {
dirty =true ;
}
public boolean requestFocus () {
System .out .println ("requestfocus" );
return false ;
}
public void setCursor (CursorFrame cursor ) {
if (cursListen !=null )
{
cursListen .cursorchanged (cursor );
}
if (drawcursor )
{
cursorlock .lock ();
dirty =true ;
if (cursor instanceof ImageCursorFrame )
{
currentframe =(ImageCursorFrame ) cursor ;
}
else
{
currentframe =cursorimages .get (cursor .getCursorType ());
}
cursorlock .unlock ();
}
System .out .println ("setcursor" +cursor );
}
public void setEmbeddedScene (EmbeddedSceneInterface embeddedScene ) {
if (scenePeer == embeddedScene ) {
return ;
}
scenePeer = embeddedScene ;
if (width > 0 && height > 0 ) {
scenePeer .setSize (width , height );
}
scenePeer .setPixelScaleFactor (scaleFactor );
}
public void setEmbeddedStage (EmbeddedStageInterface embeddedStage ) {
stagePeer = embeddedStage ;
if (stagePeer == null ) {
return ;
}
if (width > 0 && height > 0 ) {
stagePeer .setSize (width , height );
}
stagePeer .setLocation (0 , 0 );
}
public void setEnabled (boolean arg0 ) {
System .out .println ("setenabled" );
}
public void setPreferredSize (int arg0 , int arg1 ) {
System .out .println ("setpreferredsize: " +arg0 +" x " +arg1 );
}
public boolean traverseFocusOut (boolean arg0 ) {
System .out .println ("traversefocusout" );
return false ;
}
public void ungrabFocus () {
System .out .println ("ungrabfocus" );
}
public void giveCursorImage (CursorType type , ImageCursorFrame image )
{
cursorimages .put (type , image );
}
}
public int setupTexture ()
{
return tex .setup (width , height );
}
public void update ()
{
if (dirty )
{
tex .update ();
dirty =false ;
}
}
public void mousePress (int button )
{
if (scenePeer !=null )
{
stagePeer .setFocused (true ,AbstractEvents .FOCUSEVENT_ACTIVATED );
int eventbutton =0 ;
switch (button )
{
case 1 : leftbtn =true ; eventbutton =AbstractEvents .MOUSEEVENT_PRIMARY_BUTTON ; break ;
case 2 : rightbtn =true ; eventbutton =AbstractEvents .MOUSEEVENT_SECONDARY_BUTTON ;break ;
case 3 : midbtn =true ;eventbutton = AbstractEvents .MOUSEEVENT_MIDDLE_BUTTON ; break ;
}
scenePeer .mouseEvent (AbstractEvents .MOUSEEVENT_PRESSED ,
eventbutton , leftbtn , midbtn , rightbtn , mousex , mousey , mousex , mousey ,
shift , ctrl , alt , meta , 0 , false );
}
}
public void mouseRelease (int button )
{
if (scenePeer !=null )
{
int eventbutton =0 ;
switch (button )
{
case 1 : leftbtn =false ; eventbutton =AbstractEvents .MOUSEEVENT_PRIMARY_BUTTON ; break ;
case 2 : rightbtn =false ; eventbutton =AbstractEvents .MOUSEEVENT_SECONDARY_BUTTON ; break ;
case 3 : midbtn =false ;eventbutton = AbstractEvents .MOUSEEVENT_MIDDLE_BUTTON ; break ;
}
scenePeer .mouseEvent (AbstractEvents .MOUSEEVENT_RELEASED ,
eventbutton , leftbtn , midbtn , rightbtn , mousex , mousey , mousex , mousey ,
shift , ctrl , alt , meta , 0 ,false );
}
}
public void mouseMove (int x , int y )
{
if (drawcursor )
{
dirty =true ;
}
if (scenePeer !=null )
{
mousex =x ;mousey =y ;
if (leftbtn ||midbtn ||rightbtn )
{
scenePeer .mouseEvent (AbstractEvents .MOUSEEVENT_DRAGGED ,
AbstractEvents .MOUSEEVENT_NONE_BUTTON , leftbtn , midbtn , rightbtn , mousex , mousey , mousex , mousey ,
shift , ctrl , alt , meta , 0 , false );
}
else
{
scenePeer .mouseEvent (AbstractEvents .MOUSEEVENT_MOVED ,
AbstractEvents .MOUSEEVENT_NONE_BUTTON , leftbtn , midbtn , rightbtn , mousex , mousey , mousex , mousey ,
shift , ctrl , alt , meta , 0 , false );
}
}
}
public void resize (int w , int h )
{
width =w ;
height =h ;
if (scenePeer !=null )
{
scenePeer .setSize (w , h );
}
if (stagePeer !=null )
{
stagePeer .setSize (w , h );
}
if (tex !=null )
{
tex .resize (w , h );
}
}
public void keyPress (int VKcode )
{
switch (VKcode )
{
case KeyEvent .VK_SHIFT :
shift =true ;
break ;
case KeyEvent .VK_CONTROL :
ctrl =true ;
break ;
case KeyEvent .VK_ALT :
alt =true ;
break ;
case KeyEvent .META_DOWN_MASK :
meta =true ;
break ;
}
int modifiers =0 ;
if (shift )
modifiers |= AbstractEvents .MODIFIER_SHIFT ;
if (ctrl )
modifiers |= AbstractEvents .MODIFIER_CONTROL ;
if (alt )
modifiers |= AbstractEvents .MODIFIER_ALT ;
if (meta )
modifiers |= AbstractEvents .MODIFIER_META ;
char [] c ={};
scenePeer .keyEvent (AbstractEvents .KEYEVENT_PRESSED , VKcode , c , modifiers );
}
public void keyRelease (int VKcode )
{
switch (VKcode )
{
case KeyEvent .VK_SHIFT :
shift =false ;
break ;
case KeyEvent .VK_CONTROL :
ctrl =false ;
break ;
case KeyEvent .VK_ALT :
alt =false ;
break ;
case KeyEvent .META_DOWN_MASK :
meta =false ;
break ;
}
int modifiers =0 ;
if (shift )
modifiers |= AbstractEvents .MODIFIER_SHIFT ;
if (ctrl )
modifiers |= AbstractEvents .MODIFIER_CONTROL ;
if (alt )
modifiers |= AbstractEvents .MODIFIER_ALT ;
if (meta )
modifiers |= AbstractEvents .MODIFIER_META ;
char [] c ={};
scenePeer .keyEvent (AbstractEvents .KEYEVENT_RELEASED , VKcode , c , modifiers );
}
public void keyType (char letter )
{
int modifiers =0 ;
if (shift )
modifiers |= AbstractEvents .MODIFIER_SHIFT ;
if (ctrl )
modifiers |= AbstractEvents .MODIFIER_CONTROL ;
if (alt )
modifiers |= AbstractEvents .MODIFIER_ALT ;
if (meta )
modifiers |= AbstractEvents .MODIFIER_META ;
char [] c = {letter };
scenePeer .keyEvent (AbstractEvents .KEYEVENT_TYPED , com .sun .glass .events .KeyEvent .VK_UNDEFINED , c , modifiers );
}
public void setCursorChangeListener (CursorChangeListener listen )
{
cursListen =listen ;
}
public void drawCursor (boolean draw )
{
drawcursor =draw ;
}
public interface texupdater {
public int setup (int w , int h );
public void resize (int w , int h );
public void update ();
public void giveplatform (GLStagePlatform plat );
}
public interface CursorChangeListener
{
public void cursorchanged (CursorFrame cursor );
}
int AlphaBlend ( int bg , int src )
{
int bA =src &0xff ;
int gA =(src >>8 )&0xff ;
int rA =(src >>16 )&0xff ;
int aA =(src >>24 )&0xff ;
int bB =bg &0xff ;
int gB =(bg >>8 )&0xff ;
int rB =(bg >>16 )&0xff ;
int aB =(bg >>24 )&0xff ;
int rOut = (rA * aA / 255 ) + (rB * aB * (255 - aA ) / (255 *255 ));
int gOut = (gA * aA / 255 ) + (gB * aB * (255 - aA ) / (255 *255 ));
int bOut = (bA * aA / 255 ) + (bB * aB * (255 - aA ) / (255 *255 ));
int aOut = aA + (aB * (255 - aA ) / 255 );
return (aOut <<24 )|(rOut <<16 )|(gOut <<8 )|(bOut );
}
}
Special syntax:
To highlight a line (yellow background), prefix it with '@@'
To indicate that a line should be removed (red background), prefix it with '-'
To indicate that a line should be added (green background), prefix it with '+'
To post multiple snippets, seperate them by '~~~~'
java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org