1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package cs.pancava.caltha.graphics;
21
22 import cs.pancava.caltha.Editor;
23 import cs.pancava.caltha.worlds.GameEntity;
24 import cs.pancava.caltha.worlds.Route;
25 import java.awt.Color;
26 import java.awt.Container;
27 import java.awt.Cursor;
28 import java.awt.Dimension;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.Point;
32 import java.awt.event.MouseEvent;
33 import java.awt.event.MouseListener;
34 import java.awt.event.MouseMotionListener;
35 import java.awt.geom.RectangularShape;
36 import javax.swing.JPanel;
37 import javax.swing.JViewport;
38 import javax.swing.SwingUtilities;
39
40
41
42
43
44
45
46
47
48 public class EditorGraphicsWorld2D extends JPanel implements MouseListener, MouseMotionListener
49 {
50
51
52
53 private static final long serialVersionUID = 20081129192122L;
54
55
56
57
58 private int indexObektu;
59
60
61
62
63 private int lastLocationX;
64
65
66
67
68 private int lastLocationY;
69
70
71
72
73 private int lastLocationMouseX;
74
75
76
77
78 private int lastLocationMouseY;
79
80
81
82
83
84 private boolean pressOut;
85
86
87
88
89
90 private boolean selectedObject;
91
92
93
94
95
96 private boolean connectRoom;
97
98
99
100
101
102 private boolean disconnectRoom;
103
104
105
106
107 private final int MULTIPLE_NEW_AREA_X = 5;
108
109
110
111
112 private final int MULTIPLE_NEW_AREA_Y = 5;
113
114
115
116
117
118 private boolean removeRoom;
119
120
121
122
123 private final int SCROLLBAR_GROWING_AREA_X = 100;
124
125
126
127
128 private final int SCROLLBAR_GROWING_AREA_Y = 100;
129
130
131
132
133 private boolean changedMouseCursor;
134
135
136
137
138
139
140 public EditorGraphicsWorld2D()
141 {
142 this.setBackground(Color.white);
143
144 this.addMouseMotionListener(this);
145 this.addMouseListener(this);
146 }
147
148
149
150
151
152 public final boolean getSelectedObject()
153 {
154 return this.selectedObject;
155 }
156
157
158
159
160
161
162 public final boolean getConnectRoom()
163 {
164 return this.connectRoom;
165 }
166
167
168
169
170
171
172 public final boolean getDisconnectRoom()
173 {
174 return this.disconnectRoom;
175 }
176
177
178
179
180
181 public final int getNewAreaX()
182 {
183 return MULTIPLE_NEW_AREA_X * SCROLLBAR_GROWING_AREA_X;
184 }
185
186
187
188
189
190 public final int getNewAreaY()
191 {
192 return MULTIPLE_NEW_AREA_Y * SCROLLBAR_GROWING_AREA_Y;
193 }
194
195
196
197
198
199
200 public final boolean getRemoveRoom()
201 {
202 return this.removeRoom;
203 }
204
205
206
207
208
209 public final int getScrollbarGrowingAreaX()
210 {
211 return SCROLLBAR_GROWING_AREA_X;
212 }
213
214
215
216
217
218 public final int getScrollbarGrowingAreaY()
219 {
220 return SCROLLBAR_GROWING_AREA_Y;
221 }
222
223
224
225
226
227 @Override
228 public final void mousePressed(final MouseEvent e)
229 {
230 if (SwingUtilities.isLeftMouseButton(e))
231 {
232 Boolean nalezenObjekt = false;
233
234 lastLocationMouseX = e.getX();
235 lastLocationMouseY = e.getY();
236
237 for (GameEntity ge : Editor.getEditorDesktop().getWorld().getWorldRoomsAL())
238 {
239 final RectangularShape gg = (RectangularShape) ge.getGraphicsObject();
240
241
242
243 if (gg.contains(e.getX(), e.getY()))
244 {
245 if (this.getRemoveRoom())
246 {
247 this.removeRoom(ge);
248
249
250 return;
251 }
252
253 if (this.getConnectRoom())
254 {
255
256 this.connectRoom(ge);
257 }
258
259 if (this.getDisconnectRoom())
260 {
261
262 this.disconnectRoom(ge);
263 }
264
265
266
267
268 this.deselectAllObject();
269
270 ge.setSelected(true);
271 this.setSelectedObject(true);
272
273 this.indexObektu = ge.getId();
274
275
276 this.lastLocationX = ge.getLocationX() - e.getX();
277 this.lastLocationY = ge.getLocationY() - e.getY();
278
279
280 this.pressOut = false;
281 this.updateLocation(ge, e);
282
283 nalezenObjekt = true;
284
285 return;
286 }
287 else
288 {
289 this.pressOut = true;
290 }
291 }
292
293
294 this.setRemoveRoom(false);
295
296 if (!nalezenObjekt)
297 {
298 final int m = e.getModifiers();
299
300 if (SwingUtilities.isLeftMouseButton(e))
301 {
302
303
304
305
306
307
308
309 this.deselectAllObject();
310 this.setSelectedObject(false);
311 this.repaint();
312 }
313 }
314 }
315 }
316
317
318
319
320
321 @Override
322 public final void mouseDragged(final MouseEvent e)
323 {
324 if (SwingUtilities.isLeftMouseButton(e))
325 {
326 changeMouseCursor(Cursor.MOVE_CURSOR);
327
328 if (!this.pressOut)
329 {
330
331 if (Editor.getEditorDesktop().getWorld().getWorldRoomsAL(this.indexObektu) != null)
332 {
333 this.updateLocation(Editor.getEditorDesktop().getWorld().getWorldRoomsAL(this.indexObektu), e);
334 }
335 }
336
337 if (!getSelectedObject())
338 {
339
340 scrollGraphicsWorld(e);
341 }
342 }
343 }
344
345
346
347
348
349 @Override
350 public final void mouseReleased(final MouseEvent e)
351 {
352 restoreMouseCursor();
353
354 for (GameEntity ge : Editor.getEditorDesktop().getWorld().getWorldRoomsAL())
355 {
356 final RectangularShape gg = (RectangularShape) ge.getGraphicsObject();
357
358
359
360 if (gg.contains(e.getX(), e.getY()) && (!this.pressOut))
361 {
362 this.indexObektu = ge.getId();
363
364 ge.setLocationX(this.lastLocationX + e.getX());
365 ge.setLocationY(this.lastLocationY + e.getY());
366
367 this.updateLocation(ge, e);
368
369
370 return;
371 }
372 else
373 {
374 this.pressOut = false;
375 }
376 }
377 }
378
379
380
381
382
383 @Override
384 public final void mouseMoved(final MouseEvent e)
385 {
386 }
387
388
389
390
391
392 @Override
393 public final void mouseClicked(final MouseEvent e)
394 {
395 }
396
397
398
399
400
401 @Override
402 public final void mouseExited(final MouseEvent e)
403 {
404 }
405
406
407
408
409
410 @Override
411 public final void mouseEntered(final MouseEvent e)
412 {
413 }
414
415
416
417
418
419
420 public final void setConnectRoom(final boolean b)
421 {
422 this.connectRoom = b;
423 }
424
425
426
427
428
429
430 public final void setDisconnectRoom(final boolean b)
431 {
432 this.disconnectRoom = b;
433 }
434
435
436
437
438
439
440 public final void setRemoveRoom(final boolean b)
441 {
442 this.removeRoom = b;
443 }
444
445
446
447
448
449
450 public final void updateLocation(final GameEntity ge, final MouseEvent e)
451 {
452 Editor.getEditorDesktop().getWorld().updateGameEntityLocation(ge, this.indexObektu, this.lastLocationX + e.getX(), this.lastLocationY + e.getY());
453
454 this.repaint();
455 }
456
457
458
459
460
461 @Override
462 public final void paintComponent(final Graphics g)
463 {
464 super.paintComponent(g);
465 this.update(g);
466 }
467
468
469
470
471
472 @Override
473 public final void update(final Graphics g)
474 {
475 final Graphics2D g2 = (Graphics2D) g;
476 final Dimension dim = this.getSize();
477 final int w = (int) dim.getWidth();
478 final int h = (int) dim.getHeight();
479
480
481
482 g2.setPaint(Color.white);
483 g2.fillRect(0, 0, w, h);
484
485 this.showGameEntitys(g2);
486 }
487
488
489
490
491
492 private void changeMouseCursor(final int cursorType)
493 {
494 setCursor(Cursor.getPredefinedCursor(cursorType));
495 changedMouseCursor = true;
496 }
497
498
499
500
501
502 private void connectRoom(final GameEntity ge)
503 {
504 for (GameEntity ge2 : Editor.getEditorDesktop().getWorld().getWorldRoomsAL())
505 {
506 if (ge2.isSeleted() && !ge2.equals(ge))
507 {
508
509 Editor.getEditorDesktop().getWorld().addGameEntity(new Route(ge2, ge));
510
511
512 this.setConnectRoom(false);
513
514
515 return;
516 }
517 }
518 }
519
520
521
522
523
524 private void deselectPreviousObject(final int i)
525 {
526 if (!Editor.getEditorDesktop().getWorld().getWorldRoomsAL().isEmpty())
527 {
528 Editor.getEditorDesktop().getWorld().getWorldRoomsAL().get(i).setSelected(false);
529 }
530 }
531
532
533
534
535 private void deselectAllObject()
536 {
537 for (GameEntity ge : Editor.getEditorDesktop().getWorld().getWorldRoomsAL())
538 {
539 ge.setSelected(false);
540 }
541 }
542
543
544
545
546
547 private void disconnectRoom(final GameEntity ge)
548 {
549 for (GameEntity ge2 : Editor.getEditorDesktop().getWorld().getWorldRoomsAL())
550 {
551 if (ge2.isSeleted() && !ge2.equals(ge))
552 {
553
554
555
556 if (!Editor.getEditorDesktop().getWorld().getWorldRoutesAL().isEmpty())
557 {
558 GameEntity tmpGE;
559
560
561
562 for (int i = 0; i < Editor.getEditorDesktop().getWorld().getWorldRoutesAL().size(); i += 1)
563 {
564 tmpGE = Editor.getEditorDesktop().getWorld().getWorldRoutesAL(i);
565
566 if (tmpGE != null && ((tmpGE.getFirstRoom().equals(ge) && tmpGE.getSecondRoom().equals(ge2))
567 || (tmpGE.getFirstRoom().equals(ge2) && tmpGE.getSecondRoom().equals(ge))))
568 {
569 Editor.getEditorDesktop().getWorld().getWorldRoutesAL().remove(tmpGE);
570
571
572
573 i = -1;
574 }
575 }
576 }
577
578 this.setDisconnectRoom(false);
579
580
581 return;
582 }
583 }
584 }
585
586
587
588
589
590 private void removeRoom(final GameEntity ge)
591 {
592
593 if (!Editor.getEditorDesktop().getWorld().getWorldRoutesAL().isEmpty())
594 {
595 GameEntity tmpGE;
596
597
598
599 for (int i = 0; i < Editor.getEditorDesktop().getWorld().getWorldRoutesAL().size(); i += 1)
600 {
601 tmpGE = Editor.getEditorDesktop().getWorld().getWorldRoutesAL(i);
602
603 if (tmpGE != null && (tmpGE.getFirstRoom().equals(ge) || tmpGE.getSecondRoom().equals(ge)))
604 {
605 Editor.getEditorDesktop().getWorld().getWorldRoutesAL().remove(tmpGE);
606
607
608
609 i = -1;
610 }
611 }
612 }
613
614 if (!Editor.getEditorDesktop().getWorld().getWorldRoomsAL().isEmpty())
615 {
616
617 Editor.getEditorDesktop().getWorld().getWorldRoomsAL().remove(ge);
618
619
620 Editor.getEditorDesktop().getWorld().updateRooms(ge.getId());
621
622
623 this.repaint();
624 }
625
626 this.setRemoveRoom(false);
627 }
628
629
630
631
632 private void restoreMouseCursor()
633 {
634 if (changedMouseCursor)
635 {
636 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
637 }
638 }
639
640
641
642
643
644 private void scrollGraphicsWorld(final MouseEvent e)
645 {
646 Container c = this.getParent();
647
648 if (c instanceof JViewport)
649 {
650 JViewport jv = (JViewport) c;
651 Point p = jv.getViewPosition();
652
653 int newX = p.x - (e.getX() - lastLocationMouseX);
654 int newY = p.y - (e.getY() - lastLocationMouseY);
655
656 int maxX = this.getWidth() - jv.getWidth();
657 int maxY = this.getHeight() - jv.getHeight();
658
659 if (newX < 0)
660 {
661 newX = 0;
662 }
663
664 if (newX > maxX)
665 {
666 newX = maxX;
667 }
668
669 if (newY < 0)
670 {
671 newY = 0;
672 }
673
674 if (newY > maxY)
675 {
676 newY = maxY;
677 }
678
679 jv.setViewPosition(new Point(newX, newY));
680 }
681 }
682
683
684
685
686
687 private void setSelectedObject(final boolean b)
688 {
689 this.selectedObject = b;
690 }
691
692
693
694
695
696 private void showGameEntitys(final Graphics2D g2)
697 {
698 for (GameEntity ge : Editor.getEditorDesktop().getWorld().getWorldRoutesAL())
699 {
700 ge.showGraphicsObject(g2);
701 }
702
703 for (GameEntity ge : Editor.getEditorDesktop().getWorld().getWorldRoomsAL())
704 {
705 ge.showGraphicsObject(g2);
706 }
707 }
708 }