javauis/lcdui_qt/src/javax/microedition/lcdui/game/Layer.java
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2009 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 package javax.microedition.lcdui.game;
       
    18 
       
    19 import javax.microedition.lcdui.Graphics;
       
    20 import javax.microedition.lcdui.Image;
       
    21 
       
    22 /**
       
    23  * LCDUI Layer class.
       
    24  */
       
    25 public abstract class Layer {
       
    26 
       
    27     int x;
       
    28     int y;
       
    29     boolean visible;
       
    30 
       
    31     Image tileImage;
       
    32 
       
    33     int tileWidth;
       
    34     int tileHeight;
       
    35 
       
    36     int tileCount;
       
    37     int tileColumns;
       
    38 
       
    39     /**
       
    40      * Package level constructor prevents Layer being subclassed. This is
       
    41      * consistent with the MIDP 2.0 RI.
       
    42      */
       
    43     Layer(Image tileImage, int frameWidth, int frameHeight) {
       
    44         visible = true;
       
    45         setTileImage(tileImage, frameWidth, frameHeight);
       
    46     }
       
    47 
       
    48     Layer(Layer from) {
       
    49         x = from.x;
       
    50         y = from.y;
       
    51         visible = from.visible;
       
    52         tileImage = from.tileImage;
       
    53         tileWidth = from.tileWidth;
       
    54         tileHeight = from.tileHeight;
       
    55         tileCount = from.tileCount;
       
    56         tileColumns = from.tileColumns;
       
    57     }
       
    58 
       
    59     public void setPosition(int x, int y) {
       
    60         this.x = x;
       
    61         this.y = y;
       
    62     }
       
    63 
       
    64     public void move(int deltaX, int deltaY) {
       
    65         x += deltaX;
       
    66         y += deltaY;
       
    67     }
       
    68 
       
    69     public final int getX() {
       
    70         return x;
       
    71     }
       
    72 
       
    73     public final int getY() {
       
    74         return y;
       
    75     }
       
    76 
       
    77     public final int getWidth() {
       
    78         return getLayerWidth();
       
    79     }
       
    80 
       
    81     public final int getHeight() {
       
    82         return getLayerHeight();
       
    83     }
       
    84 
       
    85     public void setVisible(boolean visible) {
       
    86         this.visible = visible;
       
    87     }
       
    88 
       
    89     public final boolean isVisible() {
       
    90         return visible;
       
    91     }
       
    92 
       
    93     public abstract void paint(Graphics graphics);
       
    94 
       
    95     abstract int getLayerWidth();
       
    96 
       
    97     abstract int getLayerHeight();
       
    98 
       
    99     /**
       
   100      * Set the source image and tile size. checks that the image is completely
       
   101      * tiled. computes the tile count.
       
   102      */
       
   103     void setTileImage(Image tileImage, int tileWidth, int tileHeight) {
       
   104         if (tileWidth > 0 && tileHeight > 0) {
       
   105             final int imageWidth = tileImage.getWidth();
       
   106             final int imageHeight = tileImage.getHeight();
       
   107 
       
   108             int rows = imageHeight / tileHeight;
       
   109             int cols = imageWidth / tileWidth;
       
   110 
       
   111             int totalHeight = rows * tileHeight;
       
   112             int totalWidth = cols * tileWidth;
       
   113 
       
   114             if (totalHeight == imageHeight && totalWidth == imageWidth) {
       
   115                 this.tileImage = tileImage;
       
   116                 this.tileWidth = tileWidth;
       
   117                 this.tileHeight = tileHeight;
       
   118                 this.tileCount = rows * cols;
       
   119                 this.tileColumns = cols;
       
   120                 return;
       
   121             }
       
   122         }
       
   123         throw new IllegalArgumentException(
       
   124                 MsgRepository.LAYER_EXCEPTION_INVALID_TILE_WIDTH_HEIGHT);
       
   125     }
       
   126 
       
   127     /**
       
   128      * Is Layer visible in Graphics clip region.
       
   129      */
       
   130     final boolean isVisible(Graphics graphics, int pX, int pY) {
       
   131         int x1 = x + pX;
       
   132         int y1 = y + pY;
       
   133         int x2 = x1 + getWidth();
       
   134         int y2 = y1 + getHeight();
       
   135         int clipX1 = graphics.getClipX();
       
   136         int clipY1 = graphics.getClipY();
       
   137         int clipX2 = clipX1 + graphics.getClipWidth();
       
   138         int clipY2 = clipY1 + graphics.getClipHeight();
       
   139 
       
   140         return !(clipX2 <= x1 || clipY2 <= y1 || clipX1 >= x2 || clipY1 >= y2);
       
   141     }
       
   142 
       
   143 }