BuyMenu.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.battletank.menu;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class BuyMenu extends Menu {

    public static final int BUY = 0;
    public static final int RESTORE = 1;
    public static final int BACK = 2;
    private static String appPrice = null;
    private final Image background;
    private WaitIndicator waitIndicator;
    private int width;
    private int height;
    private int menuWidth;
    private int menuHeight;
    private int leftOffset;
    private int topOffset;
    private final Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);

    public BuyMenu(int w, int h, Listener l) {
        super(3, w, h, l);
        background = loadImage("ad_background.gif");
        waitIndicator = new WaitIndicator(loadImage("wait_indicator.png"));
        setItem(BUY, new MenuItem(loadSprite("buy.png", 2)));
        setItem(RESTORE, new MenuItem(loadSprite("restore.png", 2)));
        setItem(BACK, new MenuItem(loadSprite("back.png", 2)));
        setSize(w, h);
    }

    public final void setSize(int w, int h) {
        width = w;
        height = h;
        final int bgW = background.getWidth();
        final int bgH = background.getHeight();
        leftOffset = (w - bgW) / 2;
        topOffset = (h - bgH) / 2;
        menuWidth = Math.min(w, bgW);
        menuHeight = Math.min(h, bgH);

        int x = leftOffset + menuWidth / 2;
        int y = topOffset + 2 * menuHeight / 10;
        waitIndicator.setCenter(menuWidth / 2, y + getItem(0).getHeight() / 2);
        for (int i = 0; i < getSize(); i++) {
            MenuItem item = getItem(i);
            item.setCenter(x, y);
            y += item.getHeight();
        }
    }

    protected void paint(Graphics g) {
        g.setColor(0x00000000);
        g.fillRect(0, 0, width, height);
        g.drawImage(background, width / 2, height / 2, Graphics.HCENTER | Graphics.VCENTER);
        if (appPrice != null) {
            g.setColor(0x00ffffff);
            g.setFont(font);
            g.drawString("now for only " + appPrice, width / 2,
                    topOffset + menuHeight / 9, Graphics.BASELINE | Graphics.HCENTER);
        }
        super.paint(g);
        waitIndicator.paint(g);
    }

    public static void setPrice(String price) {
        appPrice = price;
    }

    public void showWaitIndicator() {
        this.getItem(BUY).setVisible(false);
        this.getItem(RESTORE).setVisible(false);
        waitIndicator.show();
    }

    public void hideWaitIndicator() {
        this.getItem(BUY).setVisible(true);
        this.getItem(RESTORE).setVisible(true);
        waitIndicator.hide();
    }
}