Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  // Nenya library - tools for developing networked games
  // Copyright (C) 2002-2012 Three Rings Design, Inc., All Rights Reserved
  // https://github.com/threerings/nenya
  //
  // This library is free software; you can redistribute it and/or modify it
  // under the terms of the GNU Lesser General Public License as published
  // by the Free Software Foundation; either version 2.1 of the License, or
  // (at your option) any later version.
 //
 // This library 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
 // Lesser General Public License for more details.
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 package com.threerings.cast;
 
 
 import java.awt.Point;
 
 
Used to composite the action frames for a particular orientation of a CompositedActionFrames.
 
     implements TrimmedMultiFrameImage
 {
     public CompositedMultiFrameImage (
         ImageManager imgrComponentFrames[] sources,
         String actionint orient)
     {
          = imgr;
          = sources;
          = action;
          = orient;
 
         // create our frame images (which will do the compositing)
         int fcount = sources[0]..getFrames(orient).getFrameCount();
          = new CompositedMirage[fcount];
         for (int ii = 0; ii < fcountii++) {
             [ii] = createCompositedMirage(ii);
         }
     }
 
     // documentation inherited
     public int getFrameCount () {
         return .;
     }
 
     // documentation inherited from interface
     public int getWidth (int index) {
         return [index].getWidth();
     }
 
     // documentation inherited from interface
     public int getHeight (int index) {
         return [index].getHeight();
     }
 
     public int getXOrigin (int index)
     {
         return [index].getXOrigin();
     }
 
     public int getYOrigin (int index)
     {
         return [index].getYOrigin();
     }
 
     // documentation inherited from interface
     public void paintFrame (Graphics2D gint indexint xint y) {
         [index].paint(gxy);
     }
 
     // documentation inherited from interface
     public boolean hitTest (int indexint xint y) {
         return [index].hitTest(xy);
     }
 
     // documentation inherited from interface
     public void getTrimmedBounds (int indexRectangle bounds) {
         bounds.setBounds(0, 0, getWidth(index), getHeight(index));
    }

    
Returns the estimated memory usage of our composited frame images.
    public long getEstimatedMemoryUsage ()
    {
        long size = 0;
        for (CompositedMirage element : ) {
            size += element.getEstimatedMemoryUsage();
        }
        return size;
    }

    
Creates a composited image for the specified frame.
    protected CompositedMirage createCompositedMirage (int index)
    {
        if (. == 1 && [0]. instanceof TileSetFrameImage) {
            TileSetFrameImage frames = (TileSetFrameImage)[0].;
            Rectangle tbounds = new Rectangle();
            frames.getTrimmedBounds(indextbounds);
            int x = frames.getXOrigin(index) - tbounds.x;
            int y = frames.getYOrigin(index) - tbounds.y;
            return new SubmirageForwarder(frames.getTileMirage(index), xy);
        }
        return new CompositedVolatileMirage(index);
    }

    
A CompositedMirage that forwards all of its Mirage calls to a delegate Mirage.
    protected class SubmirageForwarder implements CompositedMirage {
        public SubmirageForwarder(Mirage mint xint y) {
             = m;
            this. = x;
            this. = y;
        }
        public int getXOrigin ()
        {
            return ;
        }
        public int getYOrigin ()
        {
            return ;
        }
        public long getEstimatedMemoryUsage ()
        {
            return .getEstimatedMemoryUsage();
        }
        public int getHeight ()
        {
            return .getHeight();
        }
        public BufferedImage getSnapshot ()
        {
            return .getSnapshot();
        }
        public int getWidth ()
        {
            return .getWidth();
        }
        public boolean hitTest (int xint y)
        {
            return .hitTest(xy);
        }
        public void paint (Graphics2D gfxint xint y)
        {
            .paint(gfxxy);
        }
        protected int xy;
        protected Mirage delegate;
    }
    // documentation inherited
    protected Mirage getFrame (int orientint index)
    {
        return [index];
    }

    
The image manager from whom we load our images.
    protected ImageManager _imgr;

    
The action frames from which we obtain our source imagery.
    protected ComponentFrames[] _sources;

    
The action we're compositing.
    protected String _action;

    
The orientation we're compositing.
    protected int _orient;

    
Our composited action frame images.
    protected CompositedMirage[] _images;

    
Used to create our mirage using the source action frame images.
    protected class CompositedVolatileMirage extends VolatileMirage
        implements CompositedMirageComparator<ComponentFrames>
    {
        public CompositedVolatileMirage (int index)
        {
            super(CompositedMultiFrameImage.this.,
                  new Rectangle(0, 0, 0, 0));
            // keep this for later
             = index;
            // first we need to determine the bounds of the rectangle that
            // will enclose all of our various components
            Rectangle tbounds = new Rectangle();
            int scount = .;
            for (int ii = 0; ii < scountii++) {
                TrimmedMultiFrameImage source =
                    [ii]..getFrames();
                source.getTrimmedBounds(indextbounds);
                 = combineBounds(tbounds);
            }
            if (. <= 0) {
                . = 1;
            }
            if (. <= 0) {
                . = 1;
            }
            // compute our new origin
            . = ([0]..getXOrigin(index) -
                         .);
            . = ([0]..getYOrigin(index) -
                         .);
//             Log.info("New origin [x=" + _origin.x + ", y=" + _origin.y + "].");
            // render our volatile image for the first time
            createVolatileImage();
        }
        public int getXOrigin ()
        {
            return .;
        }
        public int getYOrigin ()
        {
            return .;
        }
        // documentation inherited from interface
        public int compare (ComponentFrames cf1ComponentFrames cf2)
        {
            return (cf1.ccomp.getRenderPriority() -
                    cf2.ccomp.getRenderPriority());
        }

        
Combines the working bounds with a new set of bounds.
        protected Rectangle combineBounds (Rectangle boundsRectangle tbounds)
        {
            // the first one defines our initial bounds
            if (bounds.width == 0 && bounds.height == 0) {
                bounds.setBounds(tbounds);
            } else {
                bounds.add(tbounds);
            }
            return bounds;
        }
        @Override
        protected int getTransparency ()
        {
            return .;
        }
        @Override
        protected void refreshVolatileImage ()
        {
//             long start = System.currentTimeMillis();
            // sort the sources appropriately for this orientation
            Arrays.sort(this);
            // now render each of the components into a composited frame
            int scount = .;
            Graphics2D g = (Graphics2D).getGraphics();
            try {
                for (int ii = 0; ii < scountii++) {
                    TrimmedMultiFrameImage source =
                        [ii]..getFrames();
                    source.paintFrame(g, -., -.);
                }
            } finally {
                // clean up after ourselves
                if (g != null) {
                    g.dispose();
                }
            }
//             Log.info("Composited [orient=" + _orient + ", index=" + _index +
//                      ", tbounds=" + StringUtil.toString(_bounds) + "].");
//             long now = System.currentTimeMillis();
//             Log.info("Composited " + scount + " frames in " +
//                      (now-start) + " millis.");
        }
        protected int _index;
        protected Point _origin = new Point();
    }
New to GrepCode? Check out our FAQ X