javauis/lcdui_akn/javalcdui/javasrc/javax/microedition/lcdui/game/LayerManager.java
branchRCL_3
changeset 19 04becd199f91
child 77 7cee158cb8cd
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package javax.microedition.lcdui.game;
       
    20 
       
    21 import javax.microedition.lcdui.Graphics;
       
    22 
       
    23 import java.util.Vector;
       
    24 
       
    25 public class LayerManager
       
    26 {
       
    27     private Vector iLayers;
       
    28     private int iViewX;
       
    29     private int iViewY;
       
    30     private int iViewWidth  = Integer.MAX_VALUE;
       
    31     private int iViewHeight = Integer.MAX_VALUE;
       
    32 
       
    33     public LayerManager()
       
    34     {
       
    35         iLayers = new Vector();
       
    36     }
       
    37 
       
    38     public void append(Layer aLayer)
       
    39     {
       
    40         remove(aLayer);
       
    41         iLayers.addElement(aLayer);
       
    42     }
       
    43 
       
    44     public void insert(Layer aLayer,int aIndex)
       
    45     {
       
    46         if (aLayer == null)
       
    47         {
       
    48             throw new NullPointerException();
       
    49         }
       
    50 
       
    51         int indexLimit = iLayers.size();
       
    52         if (aIndex<0 || aIndex>indexLimit)
       
    53         {
       
    54             throw new IndexOutOfBoundsException();
       
    55         }
       
    56 
       
    57         indexLimit--;
       
    58         if (aIndex>indexLimit && iLayers.contains(aLayer))
       
    59         {
       
    60             throw new IndexOutOfBoundsException();
       
    61         }
       
    62 
       
    63         remove(aLayer);
       
    64         iLayers.insertElementAt(aLayer,aIndex);
       
    65     }
       
    66 
       
    67     public void remove(Layer aLayer)
       
    68     {
       
    69         if (aLayer == null)
       
    70         {
       
    71             throw new NullPointerException();
       
    72         }
       
    73         iLayers.removeElement(aLayer);
       
    74     }
       
    75 
       
    76     public Layer getLayerAt(int aIndex)
       
    77     {
       
    78         return (Layer)iLayers.elementAt(aIndex);
       
    79     }
       
    80 
       
    81     public int getSize()
       
    82     {
       
    83         return iLayers.size();
       
    84     }
       
    85 
       
    86 
       
    87 
       
    88     public void setViewWindow(int aX,int aY,int aWidth,int aHeight)
       
    89     {
       
    90         if (aWidth<0 || aHeight<0)
       
    91         {
       
    92             throw new IllegalArgumentException("Width or height is less than 0");
       
    93         }
       
    94         iViewX=aX;
       
    95         iViewY=aY;
       
    96         iViewWidth=aWidth;
       
    97         iViewHeight=aHeight;
       
    98     }
       
    99 
       
   100     public void paint(Graphics aGraphics,int aX,int aY)
       
   101     {
       
   102         // save Graphics clip and translate
       
   103         final int clipX = aGraphics.getClipX();
       
   104         final int clipY = aGraphics.getClipY();
       
   105         final int clipWidth = aGraphics.getClipWidth();
       
   106         final int clipHeight = aGraphics.getClipHeight();
       
   107         final int transX = aGraphics.getTranslateX();
       
   108         final int transY = aGraphics.getTranslateY();
       
   109 
       
   110         // intersect Graphics clip region with View Window
       
   111         if ((iViewWidth<Integer.MAX_VALUE) && (iViewWidth<Integer.MAX_VALUE))
       
   112             aGraphics.clipRect(aX,aY,iViewWidth,iViewHeight);
       
   113 
       
   114         // translate the graphics object so the point (aX,aY) corresponds to
       
   115         // the location of the viewWindow in the coordinate system of the LayerManager.
       
   116         aGraphics.translate(-iViewX,-iViewY);
       
   117 
       
   118         // paint visible Layers
       
   119         final int size = iLayers.size();
       
   120         for (int i=size-1; i>=0; i--)
       
   121         {
       
   122             final Layer layer = getLayerAt(i);
       
   123             if (layerVisible(aGraphics,layer,aX,aY))
       
   124             {
       
   125                 aGraphics.translate(aX,aY);
       
   126                 layer.paint(aGraphics);
       
   127                 aGraphics.translate(-aX,-aY);
       
   128             }
       
   129         }
       
   130 
       
   131         // restore Graphics translation
       
   132         // because translation is concatenated, we need to translate back to 0,0 first
       
   133         aGraphics.translate(-aGraphics.getTranslateX(), -aGraphics.getTranslateY());
       
   134         // now back to original translation, per spec
       
   135         aGraphics.translate(transX,transY);
       
   136         // restore Graphics clip - must do after restore translation
       
   137         aGraphics.setClip(clipX,clipY,clipWidth,clipHeight);
       
   138     }
       
   139 
       
   140     /**
       
   141      * Check if Layer is visible in Graphics clip region.
       
   142      */
       
   143     private boolean layerVisible(Graphics aGraphics, Layer aLayer, int aX, int aY)
       
   144     {
       
   145         final int clipTlX = aGraphics.getClipX();
       
   146         final int clipTlY = aGraphics.getClipY();
       
   147         final int clipBrX = clipTlX + aGraphics.getClipWidth();
       
   148         final int clipBrY = clipTlY + aGraphics.getClipHeight();
       
   149 
       
   150         final int layerTlX = aLayer.getX() + aX;
       
   151         final int layerTlY = aLayer.getY() + aY;
       
   152         final int layerBrX = layerTlX + aLayer.getWidth();
       
   153         final int layerBrY = layerTlY + aLayer.getHeight();
       
   154 
       
   155         return(!(clipBrX<=layerTlX || clipBrY<=layerTlY || clipTlX>=layerBrX || clipTlY>=layerBrY));
       
   156     }
       
   157 
       
   158 }
       
   159 
       
   160