/** * Copyright (c) 2012-2013 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.racer.views; import com.nokia.example.racer.Main; import com.nokia.example.racer.helpers.ImageLoader; import com.nokia.example.racer.helpers.KeyCodes; import com.nokia.example.racer.views.components.ExitButton; import com.nokia.example.racer.views.components.Button; import com.nokia.example.racer.views.components.SettingsButton; import com.nokia.example.racer.views.components.StartButton; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; public class MenuView extends View { private Image background, backgroundLandscape; private Image shader, shaderLandscape; private Timer mTimer; private StartButton startButton; private SettingsButton settingsButton; private ExitButton exitButton; private Button[] buttons; private int currentActiveItem = 0; /** * Constructor of MenuView. Loads needed resources and sets positions of * them. */ public MenuView() { super(); try { ImageLoader loader = ImageLoader.getInstance(); background = loader.loadImage("/splash_screen.png"); backgroundLandscape = loader.loadImage( "/splash_screen_landscape.png"); shader = loader.loadImage("/shader.png"); shaderLandscape = loader.loadImage("/shader_landscape.png"); } catch (IOException io) { } startButton = new StartButton(); settingsButton = new SettingsButton(); exitButton = new ExitButton(Main.getInstance()); // set positions for buttons setPositions(); // put buttons to an array buttons = new Button[3]; buttons[0] = startButton; buttons[1] = settingsButton; buttons[2] = exitButton; if (!Main.gesturesSupported) { startButton.setActive(); } } /** * Called when this view is changed to be displayed. Starts render thread. */ public void showNotify() { mTimer = new Timer(); g = getGraphics(); TimerTask ui = new TimerTask() { public void run() { render(g); flushGraphics(); } }; mTimer.schedule(ui, 0, 30); } /** * Called when this view is hidden. Stops render thread. */ public void hideNotify() { mTimer.cancel(); } /* * Handle touch events */ protected void pointerPressed(int x, int y) { startButton.pointerPressed(x, y); settingsButton.pointerPressed(x, y); exitButton.pointerPressed(x, y); } protected void pointerReleased(int x, int y) { startButton.pointerReleased(x, y); settingsButton.pointerReleased(x, y); exitButton.pointerReleased(x, y); } /* * Handle key events */ protected void keyPressed(int keyCode) { switch (keyCode) { case KeyCodes.UP: buttons[currentActiveItem].setInactive(); if (currentActiveItem == 0) { currentActiveItem = 2; } else { currentActiveItem--; } buttons[currentActiveItem].setActive(); break; case KeyCodes.DOWN: buttons[currentActiveItem].setInactive(); if (currentActiveItem == 2) { currentActiveItem = 0; } else { currentActiveItem++; } buttons[currentActiveItem].setActive(); break; case KeyCodes.MIDDLE_SOFT_KEY: buttons[currentActiveItem].onRelease(); break; } } protected void sizeChanged(int w, int h) { super.sizeChanged(w, h); setPositions(); } private void render(Graphics g) { int anchor = Graphics.TOP | Graphics.LEFT; if (Main.landscape) { g.drawImage(backgroundLandscape, 0, 0, anchor); g.drawImage(shaderLandscape, 0, 0, anchor); } else { g.drawImage(background, 0, -40, anchor); g.drawImage(shader, 0, 0, anchor); } startButton.render(g); settingsButton.render(g); exitButton.render(g); } private void setPositions() { if (Main.landscape) { startButton.setPositionRel(0.75f, 0.45f); settingsButton.setPositionRel(0.75f, 0.65f); exitButton.setPositionRel(0.75f, 0.85f); } else { startButton.setPositionRel(0.5f, 0.35f); settingsButton.setPositionRel(0.5f, 0.55f); exitButton.setPositionRel(0.5f, 0.75f); } } }