-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathSpritesheetController.java
More file actions
1188 lines (1019 loc) · 46.5 KB
/
SpritesheetController.java
File metadata and controls
1188 lines (1019 loc) · 46.5 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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2009 Samuel Taylor
*
* This file is part of darkFunction Editor
*
* darkFunction Editor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* darkFunction Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with darkFunction Editor. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* SpritesheetController.java
*
* Created on 06-Dec-2009, 16:39:41
*/
package dfEditor;
import dfEditor.commands.*;
import dfEditor.command.*;
import dfEditor.io.*;
import javax.swing.tree.DefaultTreeModel;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.tree.TreePath;
import javax.swing.*;
import org.openide.awt.DropDownButtonFactory;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.Rectangle;
import javax.swing.tree.TreeSelectionModel;
/**
*
* @author Owner
*/
public class SpritesheetController extends dfEditorPanel implements ImageModifiedListener, GraphicPanelChangeListener
{
private File savedImageFile = null;
private String savedImageFormat = null;
private boolean bImageModified = false;
protected CustomNode _lastSelectedDirNode;
private JPopupMenu imagePopup;
private JMenuItem setImageItem = null;
private JMenuItem changeColourItem = null;
private JMenuItem packItem = null;
/** Creates new form SpritesheetController */
public SpritesheetController(CommandManager aCmdManager, boolean aNew, JLabel aHelpLabel, TaskChangeListener aListener, JFileChooser aChooser)
{
super(aCmdManager, aHelpLabel, aListener, aChooser);
initImagePopupMenu();
initComponents();
viewPanel.addGraphicChangeListener(this);
if (aNew)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showSetImageChooser();
}
});
}
postInit();
}
public void graphicSelectionChanged(GraphicPanel aPanel, GraphicObject aGraphic)
{
viewPanel.bringGraphicToFront(aGraphic);
}
public void graphicAdded(GraphicPanel aPanel, GraphicObject aGraphic)
{
}
public void graphicMoved(GraphicPanel aPanel, GraphicObject aGraphic)
{
this.refreshSpriteInfoPanel();
}
public void graphicErased(GraphicPanel aPanel, GraphicObject aGraphic){}
public void graphicsErased(GraphicPanel aPanel, ArrayList<GraphicObject> aGraphics)
{
ArrayList<CustomNode> erasedNodes = new ArrayList<CustomNode>();
for (int i=0; i<aGraphics.size(); ++i)
{
GraphicObject graphic = aGraphics.get(i);
erasedNodes.add(nameTree.nodeForObject(graphic));
}
ArrayList<UndoableCommand> commands = new ArrayList<UndoableCommand>();
for (int i=0; i<erasedNodes.size(); ++i)
commands.add(new RemoveGraphicCommand(nameTree, viewPanel, erasedNodes.get(i)));
GroupedUndoableCommand groupedCommand = new GroupedUndoableCommand(commands);
if (cmdManager != null)
cmdManager.execute(groupedCommand);
else
groupedCommand.execute();
}
private void initImagePopupMenu()
{
imagePopup = new JPopupMenu();
setImageItem = new JMenuItem("Set image...");
changeColourItem = new JMenuItem("Make colour transparent...");
packItem = new JMenuItem("Optimally pack sprites");
java.net.URL imgURL = this.getClass().getResource("resources/main_icons/Edit.png");
ImageIcon icon = new ImageIcon(imgURL);
setImageItem.setIcon(icon);
imgURL = this.getClass().getResource("resources/main_icons/paint.png");
icon = new ImageIcon(imgURL);
changeColourItem.setIcon(icon);
imgURL = this.getClass().getResource("resources/main_icons/pack.png");
icon = new ImageIcon(imgURL);
packItem.setIcon(icon);
java.awt.event.ActionListener actionListener = new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
imageItemActionPerformed(evt);
}
};
setImageItem.addActionListener(actionListener);
changeColourItem.addActionListener(actionListener);
packItem.addActionListener(actionListener);
setImageItem.setEnabled(true);
changeColourItem.setEnabled(false);
packItem.setEnabled(false);
imagePopup.add(setImageItem);
imagePopup.add(changeColourItem);
imagePopup.add(packItem);
}
private void imageItemActionPerformed(ActionEvent aEvt)
{
if (aEvt.getSource() == setImageItem)
{
showSetImageChooser();
}
else if (aEvt.getSource() == changeColourItem)
{
viewPanel.enterColourPickerMode(this);
helpLabel.setText("Select a pixel of the colour you wish to make transparent");
}
else if (aEvt.getSource() == packItem)
{
pack();
}
}
private void pack()
{
CustomNode rootNode = (CustomNode)nameTree.getModel().getRoot();
if (rootNode != null)
{
ArrayList<Rectangle> rectList = new ArrayList<Rectangle>();
addNodeToRectList(rootNode, rectList);
if (rectList.size() > 0)
{
Rectangle[] array = new Rectangle[rectList.size()];
for (int i=0; i<rectList.size(); ++i)
{
array[i] = rectList.get(i);
}
String[] choices = {" Power of two ", " Smallest " };
// TODO: a full dialog with options here instead of hard-coding
int choice = JOptionPane.showOptionDialog(
this // Center in window.
, "Would you like the resulting image to use power of two dimensions (eg, 256x512)?" // Message
, "Image dimensions?" // Title in titlebar
, JOptionPane.YES_NO_OPTION // Option type
, JOptionPane.QUESTION_MESSAGE // messageType
, null // Icon (none)
, choices // Button text as above.
, " " // Default button's label
);
int padding = 2;
PixelPacker packer = new PixelPacker();
BufferedImage newImage = packer.packPixels(viewPanel.getImage(), array, (choice == 0), padding);
viewPanel.setImage(newImage);
bImageModified = true;
viewPanel.repaint();
setModified(true);
taskChangeListener.taskChanged(this);
}
}
}
private void addNodeToRectList(final CustomNode aNode, final ArrayList aList)
{
if (aNode.isLeaf())
{
aList.add(((GraphicObject)aNode.getCustomObject()).getRect());
}
else
{
for (int i=0; i<aNode.getChildCount(); ++i)
{
addNodeToRectList((CustomNode)aNode.getChildAt(i), aList);
}
}
}
public void imageModified()
{
bImageModified = true;
taskChangeListener.taskChanged(this);
setTip();
}
private void setTip()
{
if (helpLabel != null)
{
if (viewPanel.hasTransparentPixels())
helpLabel.setText("Tip: double click on a sprite to automatically select it");
else
helpLabel.setText("Tip: Use the transparency tool to make a colour transparent");
}
}
public boolean load(String aFileName, DefaultTreeModel aModel)
{
if (aFileName == null)
{
JOptionPane.showMessageDialog(
this,
"Error loading sprites",
"Corrupt sprites definition file",
JOptionPane.ERROR_MESSAGE);
return false;
}
File imgFile = new File(aFileName);
if (!imgFile.exists())
{
JOptionPane.showMessageDialog(
this,
"Could not find image: \n\n"+aFileName,
"Image not found",
JOptionPane.ERROR_MESSAGE);
return false;
}
setImage(imgFile);
nameTree.setModel(aModel);
nameTree.setEnabled(true);
_lastSelectedDirNode = null;
// add everything to the graphic panel
addNodeToPanel((CustomNode)aModel.getRoot());
nameTree.setupCellRenderer();
setGraphicColoursFromTree(nameTree);
return true;
}
public void setGraphicColoursFromTree(SpriteTree aTree)
{
setGraphicColoursOnNode((CustomNode)aTree.getModel().getRoot());
}
private void setGraphicColoursOnNode(CustomNode aNode)
{
if (aNode.isLeaf())
{
SelectionBox sb = (SelectionBox)aNode.getCustomObject();
if (sb != null)
{
java.awt.Color col = aNode.getColour();
sb.setColour(col);
}
}
else
{
for (int i=0; i<aNode.getChildCount(); ++i)
setGraphicColoursOnNode((CustomNode)aNode.getChildAt(i));
}
}
private void addNodeToPanel(CustomNode aNode)
{
if (aNode.isLeaf())
{
GraphicObject sb = (GraphicObject)aNode.getCustomObject();
viewPanel.addGraphic(sb);
}
else
{
for (int i=0; i<aNode.getChildCount(); ++i)
{
addNodeToPanel((CustomNode)aNode.getChildAt(i));
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
spritePopupMenu = new javax.swing.JPopupMenu();
removeSpriteItem = new javax.swing.JMenuItem();
dirPopupMenu = new javax.swing.JPopupMenu();
newMenu = new javax.swing.JMenu();
addSpriteItem = new javax.swing.JMenuItem();
addDirItem = new javax.swing.JMenuItem();
removeDirItem = new javax.swing.JMenuItem();
jPanel1 = new javax.swing.JPanel();
addSpriteButton = new javax.swing.JButton();
removeSpriteButton = new javax.swing.JButton();
jSplitPane1 = new javax.swing.JSplitPane();
viewPanel = new dfEditor.SpritesheetPanel();
jPanel2 = new javax.swing.JPanel();
jSplitPane2 = new javax.swing.JSplitPane();
jScrollPane1 = new javax.swing.JScrollPane();
nameTree = new dfEditor.SpriteTree();
spriteInfoPanel = new dfEditor.SpriteInfoPanel();
zoomInButton = new javax.swing.JButton();
zoomOutButton = new javax.swing.JButton();
java.net.URL imgURL = this.getClass().getResource("resources/main_icons/Edit.png");
ImageIcon icon = new ImageIcon(imgURL);
imageButton = DropDownButtonFactory.createDropDownButton(icon, imagePopup)
;
addFolderButton = new javax.swing.JButton();
spritePopupMenu.setName("spritePopupMenu"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dfEditor.dfEditorApp.class).getContext().getResourceMap(SpritesheetController.class);
removeSpriteItem.setText(resourceMap.getString("removeSpriteItem.text")); // NOI18N
removeSpriteItem.setName("removeSpriteItem"); // NOI18N
removeSpriteItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeSpriteItemActionPerformed(evt);
}
});
spritePopupMenu.add(removeSpriteItem);
dirPopupMenu.setName("dirPopupMenu"); // NOI18N
newMenu.setText(resourceMap.getString("newMenu.text")); // NOI18N
newMenu.setName("newMenu"); // NOI18N
addSpriteItem.setText(resourceMap.getString("addSpriteItem.text")); // NOI18N
addSpriteItem.setName("addSpriteItem"); // NOI18N
addSpriteItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addSpriteItemActionPerformed(evt);
}
});
newMenu.add(addSpriteItem);
addDirItem.setText(resourceMap.getString("addDirItem.text")); // NOI18N
addDirItem.setName("addDirItem"); // NOI18N
addDirItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addDirItemActionPerformed(evt);
}
});
newMenu.add(addDirItem);
dirPopupMenu.add(newMenu);
removeDirItem.setText(resourceMap.getString("removeDirItem.text")); // NOI18N
removeDirItem.setName("removeDirItem"); // NOI18N
removeDirItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeDirItemActionPerformed(evt);
}
});
dirPopupMenu.add(removeDirItem);
setName("Untitled"); // NOI18N
jPanel1.setName("jPanel1"); // NOI18N
addSpriteButton.setIcon(resourceMap.getIcon("addSpriteButton.icon")); // NOI18N
addSpriteButton.setToolTipText(resourceMap.getString("addSpriteButton.toolTipText")); // NOI18N
addSpriteButton.setBorder(null);
addSpriteButton.setEnabled(false);
addSpriteButton.setFocusable(false);
addSpriteButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
addSpriteButton.setMaximumSize(new java.awt.Dimension(40, 40));
addSpriteButton.setMinimumSize(new java.awt.Dimension(0, 0));
addSpriteButton.setName("addSpriteButton"); // NOI18N
addSpriteButton.setPreferredSize(new java.awt.Dimension(38, 38));
addSpriteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addSpriteButtonActionPerformed(evt);
}
});
removeSpriteButton.setIcon(resourceMap.getIcon("removeSpriteButton.icon")); // NOI18N
removeSpriteButton.setToolTipText(resourceMap.getString("removeSpriteButton.toolTipText")); // NOI18N
removeSpriteButton.setBorder(null);
removeSpriteButton.setEnabled(false);
removeSpriteButton.setFocusable(false);
removeSpriteButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
removeSpriteButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
removeSpriteButton.setMaximumSize(new java.awt.Dimension(100, 100));
removeSpriteButton.setMinimumSize(new java.awt.Dimension(0, 0));
removeSpriteButton.setName("removeSpriteButton"); // NOI18N
removeSpriteButton.setPreferredSize(new java.awt.Dimension(38, 38));
removeSpriteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeSpriteButtonActionPerformed(evt);
}
});
jSplitPane1.setDividerLocation(125);
jSplitPane1.setName("jSplitPane1"); // NOI18N
viewPanel.setController(this);
viewPanel.setEnabled(true);
viewPanel.setName("viewPanel"); // NOI18N
viewPanel.setCommandManager(this.cmdManager);
javax.swing.GroupLayout viewPanelLayout = new javax.swing.GroupLayout(viewPanel);
viewPanel.setLayout(viewPanelLayout);
viewPanelLayout.setHorizontalGroup(
viewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 678, Short.MAX_VALUE)
);
viewPanelLayout.setVerticalGroup(
viewPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 450, Short.MAX_VALUE)
);
jSplitPane1.setRightComponent(viewPanel);
jPanel2.setName("jPanel2"); // NOI18N
jSplitPane2.setDividerLocation(this.getBounds().height - 150);
jSplitPane2.setDividerSize(3);
jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
jSplitPane2.setResizeWeight(0.9);
jSplitPane2.setName("jSplitPane2"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
nameTree.setName("nameTree"); // NOI18N
nameTree.setModel(new DefaultTreeModel(new CustomNode("/", true)));
nameTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
nameTree.setAutoscrolls(true);
nameTree.setEditable(true);
nameTree.setEnabled(false);
nameTree.setInvokesStopCellEditing(true);
nameTree.setupCellRenderer();
nameTree.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
nameTreeMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
nameTreeMouseReleased(evt);
}
});
nameTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
nameTreeValueChanged(evt);
}
});
jScrollPane1.setViewportView(nameTree);
jSplitPane2.setLeftComponent(jScrollPane1);
spriteInfoPanel.setFocusable(false);
spriteInfoPanel.setMinimumSize(new java.awt.Dimension(80, 100));
spriteInfoPanel.setName("spriteInfoPanel"); // NOI18N
jSplitPane2.setRightComponent(spriteInfoPanel);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
);
jSplitPane1.setLeftComponent(jPanel2);
zoomInButton.setIcon(resourceMap.getIcon("zoomInButton.icon")); // NOI18N
zoomInButton.setToolTipText(resourceMap.getString("zoomInButton.toolTipText")); // NOI18N
zoomInButton.setBorder(null);
zoomInButton.setEnabled(false);
zoomInButton.setFocusable(false);
zoomInButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
zoomInButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
zoomInButton.setMaximumSize(new java.awt.Dimension(100, 100));
zoomInButton.setMinimumSize(new java.awt.Dimension(0, 0));
zoomInButton.setName("zoomInButton"); // NOI18N
zoomInButton.setPreferredSize(new java.awt.Dimension(38, 38));
zoomInButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zoomInButtonActionPerformed(evt);
}
});
zoomOutButton.setIcon(resourceMap.getIcon("zoomOutButton.icon")); // NOI18N
zoomOutButton.setToolTipText(resourceMap.getString("zoomOutButton.toolTipText")); // NOI18N
zoomOutButton.setBorder(null);
zoomOutButton.setEnabled(false);
zoomOutButton.setFocusable(false);
zoomOutButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
zoomOutButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
zoomOutButton.setMaximumSize(new java.awt.Dimension(100, 100));
zoomOutButton.setMinimumSize(new java.awt.Dimension(0, 0));
zoomOutButton.setName("zoomOutButton"); // NOI18N
zoomOutButton.setPreferredSize(new java.awt.Dimension(38, 38));
zoomOutButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zoomOutButtonActionPerformed(evt);
}
});
imageButton.setToolTipText(resourceMap.getString("imageButton.toolTipText")); // NOI18N
imageButton.setBorder(null);
//imageButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
imageButton.setFocusable(false);
imageButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
imageButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
imageButton.setMaximumSize(new java.awt.Dimension(100, 100));
imageButton.setMinimumSize(new java.awt.Dimension(0, 0));
imageButton.setName("imageButton"); // NOI18N
imageButton.setPreferredSize(new java.awt.Dimension(38, 38));
imageButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
imageButtonActionPerformed(evt);
}
});
addFolderButton.setIcon(resourceMap.getIcon("addFolderButton.icon")); // NOI18N
addFolderButton.setToolTipText(resourceMap.getString("addFolderButton.toolTipText")); // NOI18N
addFolderButton.setBorder(null);
addFolderButton.setEnabled(false);
addFolderButton.setFocusable(false);
addFolderButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
addFolderButton.setMaximumSize(new java.awt.Dimension(40, 40));
addFolderButton.setMinimumSize(new java.awt.Dimension(0, 0));
addFolderButton.setName("addFolderButton"); // NOI18N
addFolderButton.setPreferredSize(new java.awt.Dimension(38, 38));
addFolderButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addFolderButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(addSpriteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(addFolderButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(removeSpriteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 534, Short.MAX_VALUE)
.addComponent(imageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(zoomInButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(zoomOutButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 811, Short.MAX_VALUE)
.addContainerGap())))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(zoomInButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(zoomOutButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(addSpriteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(removeSpriteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(addFolderButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(imageButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 835, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(0, 0, 0)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 520, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(0, 0, 0)))
);
}// </editor-fold>//GEN-END:initComponents
private void addSpriteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSpriteButtonActionPerformed
addSpriteAt(viewPanel.suggestVisibleSpriteRect());
}//GEN-LAST:event_addSpriteButtonActionPerformed
private void removeSpriteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeSpriteButtonActionPerformed
removeSelectedSprites();
}//GEN-LAST:event_removeSpriteButtonActionPerformed
private void zoomInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zoomInButtonActionPerformed
viewPanel.setZoom(viewPanel.getZoom() + 0.5f);
}//GEN-LAST:event_zoomInButtonActionPerformed
private void zoomOutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zoomOutButtonActionPerformed
viewPanel.setZoom(viewPanel.getZoom() - 0.5f);
}//GEN-LAST:event_zoomOutButtonActionPerformed
private void removeSpriteItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeSpriteItemActionPerformed
removeSelectedSprites();
}//GEN-LAST:event_removeSpriteItemActionPerformed
private void addSpriteItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSpriteItemActionPerformed
addSpriteAt(viewPanel.suggestVisibleSpriteRect());
}//GEN-LAST:event_addSpriteItemActionPerformed
public void addSpriteAt(java.awt.Rectangle aRect)
{
SelectionBox box = new SelectionBox(aRect, java.awt.Color.blue);
CustomNode parentNode = (CustomNode)nameTree.getSelectedNodeDir();
if (parentNode == null)
parentNode = _lastSelectedDirNode;
if (parentNode == null)
parentNode = (CustomNode)nameTree.getModel().getRoot();
cmdManager.execute(new AddGraphicToSheetCommand(nameTree, parentNode, viewPanel, box));
box.setColour(((CustomNode)parentNode.getLastChild()).getColour());
viewPanel.selectGraphic(box);
}
private void addDirItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDirItemActionPerformed
addDirToSelectedNode();
}//GEN-LAST:event_addDirItemActionPerformed
private void removeDirItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeDirItemActionPerformed
removeSelectedSprites();
}//GEN-LAST:event_removeDirItemActionPerformed
private void addFolderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFolderButtonActionPerformed
addDirToSelectedNode();
}//GEN-LAST:event_addFolderButtonActionPerformed
private void imageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imageButtonActionPerformed
imagePopup.show(imageButton, 0, imageButton.getHeight());
}//GEN-LAST:event_imageButtonActionPerformed
private void addDirToSelectedNode()
{
CustomNode parentNode = (CustomNode)nameTree.getSelectedNodeDir();
if (parentNode != null)
{
CustomNode newNode = new CustomNode(parentNode.suggestNameForChildDir(), true);
cmdManager.execute(new AddDirNodeCommand(nameTree, parentNode, newNode));
}
}
private void showSetImageChooser()
{
JFileChooser chooser = fileChooser;
ImageFilter filter = new ImageFilter();
chooser.resetChoosableFileFilters();
chooser.setFileFilter(filter);
chooser.setDialogTitle("Choose an image...");
JFrame mainFrame = dfEditorApp.getApplication().getMainFrame();
int returnVal = chooser.showOpenDialog(mainFrame);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
setImage(chooser.getSelectedFile());
}
}
private void setImage(File aFile)
{
try
{
BufferedImage image = ImageIO.read(aFile);
boolean bImage = (image != null);
zoomInButton.setEnabled(bImage);
zoomOutButton.setEnabled(bImage);
addSpriteButton.setEnabled(bImage);
addFolderButton.setEnabled(bImage);
nameTree.setEnabled(bImage);
packItem.setEnabled(bImage);
changeColourItem.setEnabled(bImage);
savedImageFile = aFile;
savedImageFormat = dfEditor.io.Utils.getExtension(aFile);
if (bImage)
{
bImageModified = false;
viewPanel.setImage(image);
viewPanel.repaint();
// start on root node
CustomNode rootNode = (CustomNode)nameTree.getModel().getRoot();
nameTree.setSelectionPath(new TreePath(rootNode.getPath()));
setTip();
}
}
catch (IOException e)
{
// TODO: show a dialog or something
}
}
private void removeSelectedSprites()
{
CustomNode[] selectedNodes = nameTree.getSelectedNodes();
cmdManager.execute(new RemoveGraphicListCommand(nameTree, viewPanel, selectedNodes));
}
@Override
public boolean hasBeenModified()
{
return super.hasBeenModified() || bImageModified;
}
public boolean save()
{
boolean bOK = true;
if (bImageModified)
{
if (savedImageFile != null && savedImageFile.exists())
{
int response = JOptionPane.showConfirmDialog (null,
"The image \"" + savedImageFile.getName() + "\" has been modified, would you like to overwrite it?", "Overwrite image?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
switch (response)
{
case JOptionPane.CANCEL_OPTION:
bOK = false;
break;
case JOptionPane.YES_OPTION:
bOK &= saveImage(savedImageFile, savedImageFormat);
break;
}
}
else
{
JOptionPane.showMessageDialog(
this,
"The image has been altered. You will now be prompted to save it.",
"Image has been modified",
JOptionPane.INFORMATION_MESSAGE);
bOK &= saveImageAs();
}
}
if (savedFile != null)
{
bOK &= saveCoords(savedFile);
}
else
{
bOK &= saveCoordsAs();
}
return bOK;
}
public boolean saveAs()
{
boolean bOK = true;
if (bImageModified)
{
JOptionPane.showMessageDialog(
this,
"The image has been altered. You will now be prompted to save it.",
"Image has been modified",
JOptionPane.INFORMATION_MESSAGE);
bOK &= saveImageAs();
}
bOK &= saveCoordsAs();
return bOK;
}
public boolean saveCoords(File aFile)
{
SpritesheetWriter writer = new SpritesheetWriter();
try
{
writer.createSpriteSheet(aFile, savedImageFile.getName(), nameTree, viewPanel.getImage().getWidth(), viewPanel.getImage().getHeight());
}
catch (IOException e)
{
JOptionPane.showMessageDialog(
this,
"Could not save the spritesheet!\n\n"+e.getMessage(),
"Spritesheet not saved",
JOptionPane.ERROR_MESSAGE);
{
return false;
}
}
this.setName(aFile.getName());
if (helpLabel != null)
helpLabel.setText("Spritesheet definition saved as " + aFile.toString());
setModified(false);
savedFile = aFile;
return true;
}
public boolean saveCoordsAs()
{
boolean bOK = false;
java.io.File f = null;
JFileChooser chooser = fileChooser;
CustomFilter filter = new CustomFilter();
filter.addExtension(CustomFilter.EXT_SPRITE);
chooser.resetChoosableFileFilters();
chooser.setFileFilter(filter);
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setApproveButtonText("Save coordinates");
chooser.setDialogTitle("Save spritesheet");
chooser.setSelectedFile(new File("newSpriteSheet.sprites"));
JFrame mainFrame = dfEditorApp.getApplication().getMainFrame();
while (true)
{
int returnVal = chooser.showSaveDialog(mainFrame);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
f = chooser.getSelectedFile();
if(null == dfEditor.io.Utils.getExtension(f))
{
f = new java.io.File(new String(f.getAbsolutePath() + "." + filter.getExtension()));
}
if (f.exists())
{
//Custom button text
int response = JOptionPane.showConfirmDialog (null,
"Overwrite existing coordinates?","Confirm Overwrite",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.CANCEL_OPTION)
continue;
}
bOK = saveCoords(f);
}
break;
}
return bOK;
}
private boolean saveImageAs()
{
boolean bOK = false;
JFileChooser chooser = fileChooser;
ImageFilter filter = new ImageFilter();
chooser.resetChoosableFileFilters();
chooser.setFileFilter(filter);
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setApproveButtonText("Save image");
chooser.setDialogTitle("Save modified image");
chooser.setSelectedFile(new File("newSpriteSheet.png"));
JFrame mainFrame = dfEditorApp.getApplication().getMainFrame();
while (true)
{
int returnVal = chooser.showSaveDialog(mainFrame);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
java.io.File f = chooser.getSelectedFile();
if (f.exists())
{
//Custom button text
int response = JOptionPane.showConfirmDialog (null,
"Overwrite existing image?","Confirm Overwrite",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.CANCEL_OPTION)
continue;
}
String[] supportedFormats = ImageIO.getWriterFormatNames();
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<supportedFormats.length; ++i)
{
boolean bGotAlready = false;
for (int j=0; j<list.size(); ++j)
{
if (supportedFormats[i].compareToIgnoreCase(list.get(j)) == 0)
{
bGotAlready = true;
break;
}
}
if (!bGotAlready)
{
list.add(supportedFormats[i].toLowerCase());
}
}
String formatName = null;
for (int i=0; i<supportedFormats.length; ++i)
{
if (Utils.getExtension(f) != null &&
Utils.getExtension(f).equals(supportedFormats[i]))
{
formatName = supportedFormats[i];
}
}
if (formatName != null)
{