001    // Copyright Andy Deck 2005, GNU Public License 2
002    // With the notable provision that any commercial use
003    // must be approved by the copyright holder.
004    import javax.swing.*;
005    import java.awt.geom.*;
006    import java.awt.*;
007    
008    public class textTool{
009    imprimatur im=null;
010    int style = Font.PLAIN;
011    int size  = 48;
012    int fontNum = 1;
013    int ox, oy;
014    
015        public textTool(imprimatur im){
016            this.im = im;
017        }
018    
019        public void doubleClicked(int x, int y){}
020        public void mouseReleased(int x, int y){
021               iLayer L = im.lyrs.getSelected();
022               if(L!=null && L.type == env.TEXT){
023                      if(L.ptArr.isClosed()){       
024                             im.da.move.mouseReleased(x,y); //handle as move item
025                             return;
026                      }
027               }
028            }
029        public void mouseDragged(int x, int y){
030               iLayer L = im.lyrs.getSelected();
031               if(L!=null && L.type == env.TEXT){
032                      if(L.ptArr.isClosed()){       
033                             im.da.move.mouseDragged(x,y); //handle as move item
034                             return;
035                      }
036    
037                      if(L.type == env.TEXT){
038                 AffineTransform at = new AffineTransform();
039                 at.translate(x-ox,y-oy);
040                 L.gp.transform(at);
041                 L.ptArr = getPts(L.gp);
042                 im.da.paintShapes();
043                      }
044           }
045           ox = x;
046           oy = y;
047        }
048    
049        public void adjustText(iLayer L, int x, int y, String s){
050                    L.lbl.setText(s);
051    
052                    GeneralPath tmp = im.da.getStringPath(s,L.font,(Graphics2D)im.da.getGraphics());
053            Rectangle t = tmp.getBounds();
054            int descent = (t.height+t.y);
055    
056                    AffineTransform at = new AffineTransform();
057                    at.translate(x,y); //+(r.height+r.y));
058                    tmp.transform(at);
059    
060                    L.gp = tmp;
061            //L.gp = fitRect(x,y,s);
062            L.ptArr = getPts(L.gp);
063    //WOOO
064                im.da.move.container = L.gp.getBounds();
065                    im.da.move.container.translate(t.x,0);
066            im.da.move.startRect = new Rectangle(im.da.move.container.x,im.da.move.container.y,im.da.move.container.width,im.da.move.container.height);
067    
068        }
069        public void mousePressed(int x, int y){
070                    iLayer L = im.lyrs.getSelected();
071                    if(L!=null && L.type == env.TEXT){
072    
073                        if(L.ptArr.isClosed()){     
074                               im.da.move.mousePressed(x,y); //handle as move item
075                               return;
076                        }
077    
078                        GeneralPath tmp = im.da.getStringPath(L.lbl.getText(),L.font,(Graphics2D)im.da.getGraphics());
079                Rectangle offset = tmp.getBounds();
080    
081                            Rectangle rect = L.gp.getBounds();
082                            if(env.hit(offset.x+rect.x+rect.width,rect.y+rect.height,x,y)){ // hit lwr-rt drag point on text
083                             ox = x; 
084                     oy = y;
085                                     if(L.type!=env.TEXT) im.info.setText("Not a text item");
086                                     return;
087                            }
088                            else if(rect.contains(x,y)){
089                            String s = (String) JOptionPane.showInputDialog((Component)im,(Object)"Please enter text:",(String)"", 
090                         JOptionPane.QUESTION_MESSAGE,env.logo,(Object[])null,(Object)null);
091                                //im.lyrs.requestFocus();
092                            if(s!=null && s.length()!=0){ 
093                                               adjustText(L,x-rect.width/2,y,s);
094                                    }
095                                    im.da.paintShapes();
096                            ox = x; 
097                    oy = y;
098                                return;
099                            }
100            }
101                    String s = null;
102                    try{
103                        s = (String) JOptionPane.showInputDialog((Component)im,(Object)"Please enter text:",(String)"", 
104                                    JOptionPane.QUESTION_MESSAGE, env.logo,(Object[])null,(Object)null);
105                            im.lyrs.requestFocus();
106                    } 
107                    catch(Exception he){ he.printStackTrace(); }
108    
109                    if(s!=null && s.length()>0)
110                    {
111                      im.da.font = new Font(im.fntStyle.names[im.fntStyle.jcb.getSelectedIndex()],style,size);
112                      FontMetrics fm = im.da.buf.getGraphics().getFontMetrics(im.da.font);
113              int width = fm.stringWidth(s);
114                      im.lyrs.curLayer = im.lyrs.addLayer( fitRect(x-width/2,y,s),s);
115                      im.lyrs.curLayer.ptArr = getPts(im.lyrs.curLayer.gp);
116                      im.lyrs.curLayer.font = im.da.font; 
117                      im.da.paintShapes();
118                      ox = x+width/2; 
119              oy = y;
120            }
121            }
122            public PtArr getPts(GeneralPath gp){
123                    PtArr pta = im.lyrs.getPts(gp);
124                    int minx = 10000;
125                    int miny = 10000;
126                    int maxx = -10000;
127                    int maxy = -10000;
128                for(int i=0;i<pta.p.size();i++){
129                            Pt pp = (Pt)pta.p.get(i);
130                            if(pp.x < minx) minx = pp.x;
131                            if(pp.y < miny) miny = pp.y;
132                            if(pp.x > maxx) maxx = pp.x;
133                            if(pp.y > maxy) maxy = pp.y;
134            }
135                      Rectangle r = gp.getBounds();
136                      PtArr ptArr = new PtArr();
137                      ptArr.add(minx,miny+(maxy-miny)); 
138                      ptArr.add(minx,miny); 
139                      ptArr.add(minx+(maxx-minx),miny); 
140                      ptArr.add(minx+(maxx-minx),miny+(maxy-miny));
141                      return ptArr;
142            }
143            
144            public GeneralPath fitRect(int x,int y, String s){
145                Font f = new Font(im.fntStyle.names[im.fntStyle.jcb.getSelectedIndex()],style,
146                           Integer.parseInt(im.fntStyle.size.getText())); //dangerous
147                    GeneralPath gp = im.da.getStringPath(s, f, (Graphics2D)im.da.buf.getGraphics());
148                    Rectangle r = gp.getBounds();
149                    AffineTransform at = new AffineTransform();
150                    at.translate(x+r.x,y); 
151                    gp.transform(at);
152                    return gp;
153        }
154    }