javauis/lcdui_qt/src/javax/microedition/lcdui/game/LayerManager.java
changeset 23 98ccebc37403
parent 21 2a9601315dfc
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
    21 import javax.microedition.lcdui.Graphics;
    21 import javax.microedition.lcdui.Graphics;
    22 
    22 
    23 /**
    23 /**
    24  * LayerManager class.
    24  * LayerManager class.
    25  */
    25  */
    26 public class LayerManager {
    26 public class LayerManager
       
    27 {
    27 
    28 
    28     private Vector layers;
    29     private Vector layers;
    29 
    30 
    30     private int viewX;
    31     private int viewX;
    31     private int viewY;
    32     private int viewY;
    32 
    33 
    33     private int viewWidth = Integer.MAX_VALUE;
    34     private int viewWidth = Integer.MAX_VALUE;
    34     private int viewHeight = Integer.MAX_VALUE;
    35     private int viewHeight = Integer.MAX_VALUE;
    35 
    36 
    36     public LayerManager() {
    37     public LayerManager()
       
    38     {
    37         layers = new Vector();
    39         layers = new Vector();
    38     }
    40     }
    39 
    41 
    40     public void append(Layer aLayer) {
    42     public void append(Layer aLayer)
       
    43     {
    41         remove(aLayer);
    44         remove(aLayer);
    42         layers.addElement(aLayer);
    45         layers.addElement(aLayer);
    43     }
    46     }
    44 
    47 
    45     public void insert(Layer layer, int index) {
    48     public void insert(Layer layer, int index)
    46         if (layer == null) {
    49     {
       
    50         if(layer == null)
       
    51         {
    47             throw new NullPointerException(
    52             throw new NullPointerException(
    48                     MsgRepository.LAYERMANAGER_EXCEPTION_LAYER_NULL);
    53                 MsgRepository.LAYERMANAGER_EXCEPTION_LAYER_NULL);
    49         }
    54         }
    50         if (index < 0 || index > layers.size()) {
    55         if(index < 0 || index > layers.size())
       
    56         {
    51             throw new IndexOutOfBoundsException(
    57             throw new IndexOutOfBoundsException(
    52                     MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_LAYER_INDEX);
    58                 MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_LAYER_INDEX);
    53         }
    59         }
    54         if (index > layers.size() - 1 && layers.contains(layer)) {
    60         if(index > layers.size() - 1 && layers.contains(layer))
       
    61         {
    55             throw new IndexOutOfBoundsException(
    62             throw new IndexOutOfBoundsException(
    56                     MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_LAYER_INDEX);
    63                 MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_LAYER_INDEX);
    57         }
    64         }
    58         remove(layer);
    65         remove(layer);
    59         layers.insertElementAt(layer, index);
    66         layers.insertElementAt(layer, index);
    60     }
    67     }
    61 
    68 
    62     public void remove(Layer layer) {
    69     public void remove(Layer layer)
    63         if (layer == null) {
    70     {
       
    71         if(layer == null)
       
    72         {
    64             throw new NullPointerException(
    73             throw new NullPointerException(
    65                     MsgRepository.LAYERMANAGER_EXCEPTION_LAYER_NULL);
    74                 MsgRepository.LAYERMANAGER_EXCEPTION_LAYER_NULL);
    66         }
    75         }
    67         layers.removeElement(layer);
    76         layers.removeElement(layer);
    68     }
    77     }
    69 
    78 
    70     public Layer getLayerAt(int index) {
    79     public Layer getLayerAt(int index)
       
    80     {
    71         return (Layer) layers.elementAt(index);
    81         return (Layer) layers.elementAt(index);
    72     }
    82     }
    73 
    83 
    74     public int getSize() {
    84     public int getSize()
       
    85     {
    75         return layers.size();
    86         return layers.size();
    76     }
    87     }
    77 
    88 
    78     public void setViewWindow(int x, int y, int width, int height) {
    89     public void setViewWindow(int x, int y, int width, int height)
    79         if (width < 0 || height < 0) {
    90     {
       
    91         if(width < 0 || height < 0)
       
    92         {
    80             throw new IllegalArgumentException(
    93             throw new IllegalArgumentException(
    81                     MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_WIDTH_HEIGHT);
    94                 MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_WIDTH_HEIGHT);
    82         }
    95         }
    83         viewX = x;
    96         viewX = x;
    84         viewY = y;
    97         viewY = y;
    85         viewWidth = width;
    98         viewWidth = width;
    86         viewHeight = height;
    99         viewHeight = height;
    87     }
   100     }
    88 
   101 
    89     public void paint(Graphics graphics, int x, int y) {
   102     public void paint(Graphics graphics, int x, int y)
       
   103     {
    90         // save Graphics clip and translate
   104         // save Graphics clip and translate
    91         final int oldClipX = graphics.getClipX();
   105         final int oldClipX = graphics.getClipX();
    92         final int oldClipY = graphics.getClipY();
   106         final int oldClipY = graphics.getClipY();
    93         final int oldClipW = graphics.getClipWidth();
   107         final int oldClipW = graphics.getClipWidth();
    94         final int oldClipH = graphics.getClipHeight();
   108         final int oldClipH = graphics.getClipHeight();
   104          */
   118          */
   105         graphics.translate(-viewX, -viewY);
   119         graphics.translate(-viewX, -viewY);
   106 
   120 
   107         // paint visible Layers
   121         // paint visible Layers
   108         Layer layer = null;
   122         Layer layer = null;
   109         for (int i = layers.size() - 1; i >= 0; i--) {
   123         for(int i = layers.size() - 1; i >= 0; i--)
       
   124         {
   110             layer = getLayerAt(i);
   125             layer = getLayerAt(i);
   111             if (layer.isVisible(graphics, x, y)) {
   126             if(layer.isVisible(graphics, x, y))
       
   127             {
   112                 graphics.translate(x, y);
   128                 graphics.translate(x, y);
   113                 layer.paint(graphics);
   129                 layer.paint(graphics);
   114                 graphics.translate(-x, -y);
   130                 graphics.translate(-x, -y);
   115             }
   131             }
   116         }
   132         }
   117 
   133 
   118         // restore Graphics translation because translation is concatenated
   134         // restore Graphics translation because translation is concatenated
   119         // need to translate back to 0,0 first
   135         // need to translate back to 0,0 first
   120         graphics.translate(-graphics.getTranslateX(),
   136         graphics.translate(-graphics.getTranslateX(),
   121                 -graphics.getTranslateY());
   137                            -graphics.getTranslateY());
   122 
   138 
   123         // restore Graphics translate and clip
   139         // restore Graphics translate and clip
   124         graphics.translate(oldTransX, oldTransY);
   140         graphics.translate(oldTransX, oldTransY);
   125         graphics.setClip(oldClipX, oldClipY, oldClipW, oldClipH);
   141         graphics.setClip(oldClipX, oldClipY, oldClipW, oldClipH);
   126     }
   142     }