Rename old palette code
[maximus:sft.git] / SuperFractalThing / source / PaletteDialogOld.java
1 //      PaletteDialog
2 //
3 //    Copyright 2013 Kevin Martin
4 //
5 //    This file is part of SuperFractalThing.
6 //
7 //    SuperFractalThing is free software: you can redistribute it and/or modify
8 //    it under the terms of the GNU General Public License as published by
9 //    the Free Software Foundation, either version 3 of the License, or
10 //    any later version.
11 //
12 //    SuperFractalThing is distributed in the hope that it will be useful,
13 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //    GNU General Public License for more details.
16 //
17 //    You should have received a copy of the GNU General Public License
18 //    along with SuperFractalThing.  If not, see <http://www.gnu.org/licenses/>.
19 //
20
21 import java.awt.Color;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.text.DecimalFormat;
35 import java.text.NumberFormat;
36
37 import javax.swing.BorderFactory;
38 import javax.swing.JButton;
39 import javax.swing.JColorChooser;
40 import javax.swing.JDialog;
41 import javax.swing.JFormattedTextField;
42 import javax.swing.JFrame;
43 import javax.swing.JLabel;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPanel;
46 /*
47 interface PaletteIO
48 {
49         void SavePalette(String str);
50         String LoadPalette();
51 }
52
53 class ColourButton extends JButton implements ActionListener
54 {
55         static final long serialVersionUID = 0;//get rid of warning
56         Color mColour;
57         JFrame mFrame;
58         
59         public ColourButton(String aName, Color col,JFrame aFrame)
60         {
61                 super(aName);
62                 mColour = col;
63                 mFrame = aFrame;
64             setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, mColour));
65             addActionListener(this);
66         }
67         @Override
68         public void actionPerformed(ActionEvent arg0)
69         {
70                 Color c = JColorChooser.showDialog(mFrame, "Pick a Color"
71                 , mColour);     
72                 if (c!=null)
73                         mColour = c;
74                 
75             setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, mColour));
76         }
77
78         public Color GetColour()
79         {
80                 return mColour;
81         }
82         public void SetColour(int r, int g, int b)
83         {
84                 mColour = new Color(r,g,b);
85             setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, mColour));
86         }
87         public void SetColour(Color aColour)
88         {
89                 mColour = aColour;
90             setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, mColour));
91         }
92 }
93
94 */
95 public class PaletteDialogOld implements ActionListener
96 {
97 JDialog mDialog;
98 JFormattedTextField md_di[][];
99 JFormattedTextField mDecay[];
100 JButton mCancel_button;
101 JButton mOK_button;
102 boolean mOK;
103 SFTPaletteOld mPalette;
104 ColourButton mStart_colour;
105 ColourButton mEnd_colour;
106 String mInitial_state;
107 Component mComponent;
108 JFrame mFrame;
109 PaletteIO mPalette_io;
110
111 static int NUM_BANDS = 6;
112
113 JFormattedTextField mBand_start[];
114 JFormattedTextField mBand_length[];
115 JFormattedTextField mBand_period[];
116 ColourButton mBand_modulate[];
117 ColourButton mBand_offset[];
118
119         public PaletteDialogOld(JFrame aFrame, Component aComponent, SFTPaletteOld aPalette, PaletteIO aPalette_io)
120         {
121                 mPalette = aPalette;
122                 mComponent = aComponent;
123                 mFrame = aFrame;
124                 mPalette_io = aPalette_io;
125                 
126                 mDialog = new JDialog(aFrame, "Edit Palette", true);
127                 md_di = new JFormattedTextField[3][2];
128                 mDecay = new JFormattedTextField[3];
129                 
130                 mBand_start = new JFormattedTextField[NUM_BANDS];
131                 mBand_length = new JFormattedTextField[NUM_BANDS];
132                 mBand_period = new JFormattedTextField[NUM_BANDS];
133                 mBand_modulate = new ColourButton[NUM_BANDS];
134                 mBand_offset= new ColourButton[NUM_BANDS];
135                 
136                 JPanel p = new JPanel();
137         JLabel label;
138         mDialog.setSize(new Dimension(550,500));
139         p.setPreferredSize(new Dimension(540,500));
140
141         mDialog.setContentPane(p);
142   
143         
144         p.setLayout(new GridBagLayout());
145         GridBagConstraints gbc = new GridBagConstraints();
146
147         NumberFormat format = new DecimalFormat();
148         format.setMaximumFractionDigits(7);
149    
150         gbc.ipady = 20;
151         gbc.gridy=0;
152         gbc.gridx=0;
153         gbc.gridwidth=1;
154         gbc.fill = GridBagConstraints.HORIZONTAL;
155  //       JButton col = new JButton("Start Colour");
156  //       col.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.orange));
157         mStart_colour = new ColourButton("Start Colour", Color.black, aFrame);
158         p.add(mStart_colour,gbc);       
159         //col.addActionListener(this);          
160         gbc.gridx+=2;
161         mEnd_colour = new ColourButton("M.Set Colour", Color.black, aFrame);
162         p.add(mEnd_colour,gbc);       
163         
164         
165         gbc.ipady = 5;
166
167         
168         gbc.gridx+=2;
169         gbc.fill = GridBagConstraints.HORIZONTAL;
170         gbc.gridwidth=1;
171         JButton help = new JButton("Help");
172         p.add(help,gbc);
173         help.addActionListener(this);           
174         
175         
176         gbc.gridy++;
177         gbc.gridx=0;
178         gbc.gridwidth=1;
179         gbc.fill = GridBagConstraints.HORIZONTAL;
180         label=new JLabel("", null, JLabel.RIGHT);
181         p.add(label,gbc);       
182  
183         gbc.gridx=1;
184         gbc.gridwidth=1;
185         gbc.fill = GridBagConstraints.HORIZONTAL;
186         label=new JLabel("Gradient Start", null, JLabel.CENTER);
187         p.add(label,gbc);       
188
189         gbc.gridx=2;
190         gbc.gridwidth=1;
191         gbc.fill = GridBagConstraints.HORIZONTAL;
192         label=new JLabel("Final Gradient", null, JLabel.CENTER);
193         p.add(label,gbc);
194         
195         gbc.gridx=3;
196         gbc.gridwidth=1;
197         gbc.fill = GridBagConstraints.HORIZONTAL;
198         label=new JLabel("Half Life", null, JLabel.CENTER);
199         p.add(label,gbc);       
200
201         
202         String[] labels ={"Red", "Green","Blue"};
203         
204         gbc.gridy++;
205         int i;
206         for (i=0; i<3; i++)
207         {
208                 gbc.gridx=0;
209                 gbc.gridwidth=1;
210                 gbc.fill = GridBagConstraints.HORIZONTAL;
211                 label=new JLabel(labels[i], null, JLabel.RIGHT);
212                 p.add(label,gbc);       
213          
214                 gbc.gridx=1;
215                 gbc.fill = GridBagConstraints.HORIZONTAL;
216                 md_di[i][0] = new JFormattedTextField(format);
217                 md_di[i][0].setPreferredSize(new Dimension(100,20));
218                 md_di[i][0].setValue(1024);
219                 p.add(md_di[i][0], gbc);       
220
221                 gbc.gridx=2;
222                 gbc.fill = GridBagConstraints.HORIZONTAL;
223                 md_di[i][1] = new JFormattedTextField(format);
224                 md_di[i][1].setPreferredSize(new Dimension(100,20));
225                 md_di[i][1].setValue(1024);
226                 p.add(md_di[i][1], gbc);   
227                                 
228                 gbc.gridx=3;
229                 gbc.fill = GridBagConstraints.HORIZONTAL;
230                 mDecay[i] = new JFormattedTextField(format);
231                 mDecay[i].setPreferredSize(new Dimension(100,20));
232                 mDecay[i].setValue(1024);
233                 p.add(mDecay[i], gbc); 
234                 
235                 gbc.gridy+=1;
236         } 
237   
238         gbc.gridx=0;
239         gbc.ipady=10;
240         gbc.gridwidth=3;
241         gbc.fill = GridBagConstraints.HORIZONTAL;
242         label=new JLabel("", null, JLabel.LEFT);
243         p.add(label,gbc);       
244         gbc.ipady=0;
245         gbc.gridy++;
246         
247         gbc.gridx=0;
248         gbc.ipady=10;
249         gbc.gridwidth=3;
250         gbc.fill = GridBagConstraints.HORIZONTAL;
251         label=new JLabel("Colour Bands", null, JLabel.LEFT);
252         p.add(label,gbc);       
253         gbc.ipady=0;
254  
255      
256         gbc.gridy++;
257         gbc.gridx=0;
258         gbc.gridwidth=1;
259         gbc.fill = GridBagConstraints.HORIZONTAL;
260         label=new JLabel("Start", null, JLabel.CENTER);
261         p.add(label,gbc);       
262  
263         gbc.gridx=1;
264         gbc.gridwidth=1;
265         gbc.fill = GridBagConstraints.HORIZONTAL;
266         label=new JLabel("Length", null, JLabel.CENTER);
267         p.add(label,gbc);       
268
269         gbc.gridx=2;
270         gbc.gridwidth=1;
271         gbc.fill = GridBagConstraints.HORIZONTAL;
272         label=new JLabel("Period", null, JLabel.CENTER);
273         p.add(label,gbc);
274         
275         gbc.gridx=3;
276         gbc.gridwidth=1;
277         gbc.fill = GridBagConstraints.HORIZONTAL;
278         label=new JLabel("Modulate", null, JLabel.CENTER);
279         p.add(label,gbc);       
280         
281         gbc.gridx=4;
282         gbc.gridwidth=1;
283         gbc.fill = GridBagConstraints.HORIZONTAL;
284         label=new JLabel("Offset", null, JLabel.CENTER);
285         p.add(label,gbc);       
286   
287         
288         for (i=0; i<NUM_BANDS; i++)
289         {
290                 gbc.gridy++;
291                 //Band boxes
292                 gbc.gridx=0;
293                 gbc.fill = GridBagConstraints.HORIZONTAL;
294                 mBand_start[i] = new JFormattedTextField(format);
295                 mBand_start[i].setPreferredSize(new Dimension(100,20));
296                 mBand_start[i].setValue(1024);
297                 p.add(mBand_start[i], gbc);       
298         
299                 gbc.gridx++;
300                        gbc.fill = GridBagConstraints.HORIZONTAL;
301                 mBand_length[i] = new JFormattedTextField(format);
302                 mBand_length[i].setPreferredSize(new Dimension(100,20));
303                 mBand_length[i].setValue(1024);
304                 p.add(mBand_length[i], gbc);   
305                                 
306                 gbc.gridx++;
307                 gbc.fill = GridBagConstraints.HORIZONTAL;
308                 mBand_period[i] = new JFormattedTextField(format);
309                 mBand_period[i].setPreferredSize(new Dimension(100,20));
310                 mBand_period[i].setValue(1024);
311                 p.add(mBand_period[i], gbc); 
312                 
313                 //band colours
314                 gbc.gridx++;
315                 mBand_modulate[i] = new ColourButton("", Color.black, aFrame);
316                 p.add(mBand_modulate[i],gbc);       
317                 //col.addActionListener(this);          
318                 gbc.gridx++;
319                 mBand_offset[i] = new ColourButton("", Color.black, aFrame);
320                 p.add(mBand_offset[i],gbc);       
321         }
322         
323         gbc.ipady = 0;
324         gbc.insets = new Insets(30,0,0,0);  //top padding
325         gbc.anchor = GridBagConstraints.PAGE_END; //bottom of space
326         gbc.gridx=0;
327         gbc.gridy++;
328         gbc.fill = GridBagConstraints.HORIZONTAL;
329         gbc.gridwidth=1;
330         mCancel_button = new JButton("Refresh");
331         p.add(mCancel_button,gbc);
332         mCancel_button.addActionListener(this);       
333
334  
335         gbc.gridx++;
336         gbc.fill = GridBagConstraints.HORIZONTAL;
337         gbc.gridwidth=1;
338         mCancel_button = new JButton("Load");
339         p.add(mCancel_button,gbc);
340         mCancel_button.addActionListener(this);  
341         
342         gbc.gridx++;
343         gbc.fill = GridBagConstraints.HORIZONTAL;
344         gbc.gridwidth=1;
345         mCancel_button = new JButton("Save");
346         p.add(mCancel_button,gbc);
347         mCancel_button.addActionListener(this); 
348         
349         gbc.gridx++;
350         gbc.fill = GridBagConstraints.HORIZONTAL;
351         gbc.gridwidth=1;
352         mCancel_button = new JButton("Cancel");
353         p.add(mCancel_button,gbc);
354         mCancel_button.addActionListener(this);       
355        
356         
357         gbc.gridx++;
358         gbc.fill = GridBagConstraints.HORIZONTAL;
359         gbc.gridwidth=1;
360         mOK_button = new JButton("   OK   ");
361         p.add(mOK_button,gbc);
362         mOK_button.addActionListener(this);             
363
364         }
365         
366         public boolean Run()
367         {       
368             mDialog.setLocationRelativeTo(mComponent);
369                 GetPaletteValues();     
370                 mInitial_state = mPalette.ToString();
371                 mDialog.setVisible(true);
372                 return mOK;
373         }
374
375         @Override
376         public void actionPerformed(ActionEvent event)
377         {
378                 String command = event.getActionCommand();
379                 if (command=="   OK   ")
380                 {
381                         mOK=true;
382                         mDialog.setVisible(false);
383                         SetPaletteValues();
384                 }
385                 else if (command=="Cancel")
386                 {
387                         mOK=false;
388                         mDialog.setVisible(false);
389                         mPalette.ParseString(mInitial_state);
390                 }
391                 else if (command=="Refresh")
392                 {
393                         SetPaletteValues();
394                 }
395                 else if (command=="Help")
396                 {
397                         JOptionPane.showMessageDialog(mComponent, "Use the gradient values to control how the colour channels change with the iteration count.\n"+
398                                         "Use the colour bands to create bands of modified colour to high light structure in highly chaotic regions.",
399                                                                 "Palette Help",                                 
400                                                                 JOptionPane.PLAIN_MESSAGE);
401         
402                 }
403                 else if (command=="Load")
404                 {
405                         String str = mPalette_io.LoadPalette();
406                         if (str!=null)
407                         {
408                                 mPalette.ParseString(str);
409                                 
410                                 GetPaletteValues();
411                         
412                         }
413                 }
414                 else if (command=="Save")
415                 {
416                         String str = mPalette.ToString();
417
418                         mPalette_io.SavePalette(str);
419                 }               
420         }
421         
422         void GetPaletteValues()
423         {
424                 float[][] p = new float[3][3];
425                 Color cols[] = new Color[2];
426                 
427                 mPalette.GetGradientValues(p, cols);
428                 
429                 for (int i=0; i<3; i++)
430                 {
431                         md_di[i][0].setValue(p[i][0]);
432                         md_di[i][1].setValue(p[i][1]);
433                         mDecay[i].setValue(p[i][2]);    
434                 }
435                 
436                 mStart_colour.SetColour(cols[0]);
437                 mEnd_colour.SetColour(cols[1]);
438                 
439                 for (int i=0; i<NUM_BANDS; i++)
440                 {
441                         int[] x = mPalette.GetBand(i);
442                         
443                         mBand_start[i].setValue(x[0]);
444                         mBand_length[i].setValue(x[1]);
445                         mBand_period[i].setValue(x[2]);
446                         
447                         mBand_modulate[i].SetColour(x[6],x[7],x[8]);
448                         mBand_offset[i].SetColour(x[3],x[4],x[5]);
449                 }
450         }
451         void SetPaletteValues()
452         {
453                 float[][] p = new float[3][3];
454                 
455                 for (int i=0; i<3; i++)
456                 {
457                         p[i][0] = Float.parseFloat(md_di[i][0].getText());
458                         p[i][1] = Float.parseFloat(md_di[i][1].getText());
459                         p[i][2] = ((Number)(mDecay[i].getValue())).floatValue();        
460                 }
461                 
462                 int[] band = new int[9];
463                 
464                 for (int i=0; i<NUM_BANDS; i++)
465                 {
466                         band[0] = Integer.parseInt(mBand_start[i].getText().replace(",",""));
467                         band[1] = Integer.parseInt(mBand_length[i].getText().replace(",",""));
468                         band[2] = Integer.parseInt(mBand_period[i].getText().replace(",",""));
469                         
470                         band[3] = mBand_offset[i].GetColour().getRed();
471                         band[4] = mBand_offset[i].GetColour().getGreen();
472                         band[5] = mBand_offset[i].GetColour().getBlue();
473         
474                         band[6] = mBand_modulate[i].GetColour().getRed();
475                         band[7] = mBand_modulate[i].GetColour().getGreen();
476                         band[8] = mBand_modulate[i].GetColour().getBlue();
477                         
478                         mPalette.SetBand(i,band);
479                 }
480                 mPalette.SetGradientValues(p, mStart_colour.GetColour(), mEnd_colour.GetColour());
481         }
482
483
484         public void LoadTheFile(File f)
485         {
486                 try
487                 {
488                         FileReader fr = new FileReader(f);
489                         BufferedReader br = new BufferedReader(fr);
490                         LoadTheFile(br);
491                 } catch (FileNotFoundException e) {
492                         // TODO Auto-generated catch block
493                         e.printStackTrace();
494                 }
495         }
496         
497         public void LoadTheFile(BufferedReader br)
498         {
499                 String str;
500                 
501                 char arr[]=new char[1024];
502
503                 try {
504                         br.read(arr, 0,1024);
505                 } catch (IOException e) {
506                         // TODO Auto-generated catch block
507                         e.printStackTrace();
508                         return;
509                 }
510                 str = String.copyValueOf(arr);
511                 
512                 mPalette.ParseString(str);
513                 
514                 GetPaletteValues();
515         }
516 }