IconButton.java

/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.amaze.ui;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 * Simple icon button.
 */
public class IconButton {
	// Constants
	// Button types
	public static final int EXIT = 0;
	public static final int INFO = 1;
	public static final int PAUSE = 2;
	public static final int VIEW_MODE = 3;	
	
	// Icon filenames
	private static final String EXIT_NORMAL = "/graphics/exit-button-normal-30.png";
	private static final String EXIT_PRESSED = "/graphics/exit-button-pressed-30.png";
	private static final String INFO_NORMAL = "/graphics/info-button-normal-30.png";
	private static final String INFO_PRESSED = "/graphics/info-button-pressed-30.png";
	private static final String PAUSE_NORMAL = "/graphics/pause-button-normal-30.png";
	private static final String PAUSE_PRESSED = "/graphics/pause-button-pressed-30.png";
	private static final String VIEW_MODE_NORMAL = "/graphics/view-mode-button-normal-30.png";
	private static final String VIEW_MODE_PRESSED = "/graphics/view-mode-button-pressed-30.png";
	
	// Members
	private Image _normalImage = null;
	private Image _pressedImage = null;
	private final int _type;
	private boolean _pressed = false;
	
	/**
	 * Constructor.
	 * @param type The type of the image.
	 */
	public IconButton(int type) {
		_type = type;
		
		// Load the images based on the type
		createImages(true);
	}
	
	/** 
	 * @param pressed
	 */
	public void setPressed(final boolean pressed) {
		_pressed = pressed;
	}
	
	/** 
	 * @return True if the button is pressed, false otherwise.
	 */
	public final boolean pressed() {
		return _pressed;
	}
	
	/**
	 * Rotates the button image based on the orientation. 
	 * @param isPortrait If false, will rotate the images 90 degrees.
	 */
	public void setIsPortrait(final boolean isPortrait) {
		createImages(isPortrait);
	}
	
	/**
	 * Paints the button.
	 * @param graphics
	 * @param x
	 * @param y
	 */
	public void paint(Graphics graphics, int x, int y) {
		if (_pressed && _pressedImage != null) {
			graphics.drawImage(_pressedImage, x,  y, Graphics.TOP | Graphics.LEFT);
		}
		else if (_normalImage != null) {
			graphics.drawImage(_normalImage, x,  y, Graphics.TOP | Graphics.LEFT);
		}
	}
	
	/**
	 * Creates and returns a new image or null in case of failure.
	 * @param source The image filename.
	 * @param isPortrait If false, will rotate the image 90 degrees.
	 * @return The newly created image or null in case of failure.
	 */
	private Image createImage(final String source,
							  final boolean isPortrait)
	{
		Image image = null;
		
		try {
			image = Image.createImage(source);
			
			if (!isPortrait && image != null) {
				// Rotate the image
				image = Image.createImage(image, 0, 0,
							image.getWidth(), image.getHeight(),
							Sprite.TRANS_ROT90);
			}
		}
		catch (IOException e) {}
		
		return image;
	}
	
	/**
	 * Loads the button images based on the type.
	 * @param isPortrait
	 */
	private final void createImages(final boolean isPortrait) {
		switch (_type) {
		case EXIT: {
			_normalImage = createImage(EXIT_NORMAL, isPortrait);
			_pressedImage = createImage(EXIT_PRESSED, isPortrait);
			break;
		}
		case INFO: {
			_normalImage = createImage(INFO_NORMAL, isPortrait);
			_pressedImage = createImage(INFO_PRESSED, isPortrait);
			break;
		}
		case PAUSE: {
			_normalImage = createImage(PAUSE_NORMAL, isPortrait);
			_pressedImage = createImage(PAUSE_PRESSED, isPortrait);
			break;
		}
		case VIEW_MODE: {
			_normalImage = createImage(VIEW_MODE_NORMAL, isPortrait);
			_pressedImage = createImage(VIEW_MODE_PRESSED, isPortrait);
			break;
		}
		}
		}
}