ControlBar.java

/*
 * Copyright © 2011 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.overlay;
//
// Source File Name:   ControlBar.java

/**
 * ControlBar displays buttons on VideoCanvas
 * Buttons can be used to control VideoCanvas
 * */
import java.io.IOException;

import com.nokia.mid.ui.DirectGraphics;
import com.nokia.mid.ui.DirectUtils;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Player;

public class ControlBar {

    private DirectGraphics dg;
    private VideoCanvas parent;
    private int x, y;
    private int width, height;
    private final int W_GAP = 5;
    private final int H_GAP = 2 * W_GAP;
    private int button_width;
    private int button_height;
    /** Control Bar height spacing area */
    private final int BORDER_H_SPACING = 40;
    /** Control Bar width spacing area */
    private final int BORDER_W_SPACING = 40;
    private int first_button_x;
    private int first_button_y;
    private int second_button_x;
    private int second_button_y;
    private int third_button_x;
    private int third_button_y;
    /** ControlBar background color */
    private final int TRANSPARENT_COLOR = 0xB000FFFF;
    /** ControlBar button color */
    private final int BUTTON_TRANSPARENT_COLOR = 0xB000FF00;
    /** Largest image height */
    private int image_height = 40;
    /** Button type of ControlBar */
    private int button_type;
    private Image fullScreen_icon;
    private Image normalScreen_icon;
    private Image hide_icon;
    private Image play_icon;
    private Image pause_icon;
    private Image snap_icon;
    private String[] image_paths = {
        "fromfullscreen.png",
        "tofullscreen.png",
        "hide.png",
        "snap.png",
        "play.png",
        "pause.png"
    };

    /**
     * ControlBar constructor:
     * - Setting parent and values for ControlBar
     * - Setting images for ControlBar
     * - Height is fixed
     * @param vCanvas
     * @param x
     * @param y
     * @param width
     */
    public ControlBar(VideoCanvas vCanvas, int x, int y, int width) {
        this.parent = vCanvas;
        this.x = x;
        this.y = y;
        this.createImages();
        this.width = width + this.BORDER_W_SPACING;
        this.height = 2 * image_height + this.BORDER_H_SPACING;
        this.button_width = (width / 3) - W_GAP;
        this.button_height = (this.height) - 2 * H_GAP;

        this.first_button_x = x + W_GAP;
        this.first_button_y = y + H_GAP;
        this.second_button_x = this.first_button_x + this.button_width + W_GAP;
        this.second_button_y = y + H_GAP;
        this.third_button_x = this.second_button_x + this.button_width + W_GAP;
        this.third_button_y = y + H_GAP;
    }

    /** Creates images from resources for buttons in ControlBar */
    public void createImages() {
        normalScreen_icon = this.loadTestImage(0);
        fullScreen_icon = this.loadTestImage(1);
        hide_icon = this.loadTestImage(2);
        snap_icon = this.loadTestImage(3);
        play_icon = this.loadTestImage(4);
        pause_icon = this.loadTestImage(5);
    }

    /**
     * Returns image. If image loading fails then null is returned.
     * @param testNum
     * @return button image
     */
    protected Image loadTestImage(int testNum) {
        Image image;
        try {
            image = Image.createImage("/buttons/" + image_paths[testNum]);
            if (image.getHeight() > this.image_height) {
                this.image_height = image.getHeight();
            }
        } catch (IOException ex) {
            image = null;
        }
        return image;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width + this.BORDER_W_SPACING;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height + this.BORDER_H_SPACING;
    }

    public int getButton_type() {
        return button_type;
    }

    public void setButton_type(int button_type) {
        this.button_type = button_type;
    }

    /**
     * drawControlBar(Graphics g)
     * Drawing ControlBar with buttons:
     * - Snap = Take a snapshot, which is then displayed on screen
     * - Full/Norm = Toggle full screen mode
     * - Hide = Hide ControlBar
     * @param g - Graphics object from the paint event
     */
    public void drawControlBar(Graphics g) {
        dg = DirectUtils.getDirectGraphics(g);

        button_width = (width / 3) - W_GAP;
        button_height = (height) - 2 * H_GAP;

        first_button_x = x + W_GAP;
        first_button_y = y + H_GAP;
        second_button_x = first_button_x + button_width + W_GAP;
        second_button_y = y + H_GAP;
        third_button_x = second_button_x + button_width + W_GAP;
        third_button_y = y + H_GAP;

        // The whole ControlBar area
        dg.setARGBColor(TRANSPARENT_COLOR);
        g.drawRoundRect(x, y, width, height, 30, 30);

        // First button: Camera takes snapshot, video toggles play/stop
        if (parent.getType() == VideoOverlayMIDlet.CAMERA) {
            if (this.button_type == VideoOverlayMIDlet.ICON
                    && snap_icon != null) {
                this.drawIconButton(g, first_button_x, first_button_y,
                        button_width, button_height, snap_icon);
            }
        } else {
            if (parent.getPlayerState() == Player.PREFETCHED) {
                if (this.button_type == VideoOverlayMIDlet.ICON
                        && play_icon != null) {
                    this.drawIconButton(g, first_button_x, first_button_y,
                            button_width, button_height, play_icon);
                }
            } else {
                if (this.button_type == VideoOverlayMIDlet.ICON
                        && pause_icon != null) {
                    this.drawIconButton(g, first_button_x, first_button_y,
                            button_width, button_height, pause_icon);
                }
            }
        }

        // Second button: Toggles full screen mode

        if (parent.isFullScreen()) {
            if (this.button_type == VideoOverlayMIDlet.ICON
                    && normalScreen_icon != null) {
                this.drawIconButton(g, second_button_x, second_button_y,
                        button_width, button_height, normalScreen_icon);
            }
        } else {
            if (this.button_type == VideoOverlayMIDlet.ICON
                    && fullScreen_icon != null) {
                this.drawIconButton(g, second_button_x, second_button_y,
                        button_width, button_height, fullScreen_icon);
            }
        }
        // Third button: Hides ControlBar

        if (this.button_type == VideoOverlayMIDlet.ICON
                && hide_icon != null) {
            this.drawIconButton(g, third_button_x, third_button_y,
                    button_width, button_height, hide_icon);
        }
    }

    /**
     * drawIconButton
     * Drawing button with icon
     * @param g
     * @param x - button x
     * @param y - button y
     * @param width - button width
     * @param height - button height
     * @param icon - button icon
     */
    private void drawIconButton(Graphics g, int x, int y,
            int width, int height, Image icon) {
        dg.setARGBColor(BUTTON_TRANSPARENT_COLOR);
        g.fillRect(x, y, width, height);
        g.drawImage(icon,
                x + (width / 2),
                y + (height / 2),
                Graphics.VCENTER | Graphics.HCENTER);
    }
}