Crosshair.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.ghosts;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * This class implements the crosshair functionality.
 */
public class Crosshair
{
    private Size parentSize;
    private Image crosshairImage;
    private Point crosshairPosition = new Point();
    private int crosshairRadius = 0;

    public Crosshair(Size parentSize) throws IOException
    {
        super();
        this.parentSize = parentSize;
        this.crosshairImage = Image.createImage("/images/Crosshair.png");

        this.crosshairPosition.x = this.parentSize.width / 2;
        this.crosshairPosition.y = this.parentSize.height / 2;
        this.crosshairRadius = this.crosshairImage.getWidth() / 2;
    }

    public void paint(Graphics graphics)
    {
        // Draw the crosshair image
        graphics.drawImage(
            this.crosshairImage,
            this.crosshairPosition.x,
            this.crosshairPosition.y,
            Graphics.HCENTER | Graphics.VCENTER
        );
    }

    public Point getCrosshairPosition()
    {
        return new Point(
                   this.crosshairPosition.x,
                   this.crosshairPosition.y
               );
    }

    /**
     * This method should be called periodically from the game's world clock handler
     * to allow reposition of the crosshair. The method takes the position of
     * gamepad analog stick as parameter and calculates the new position for the crosshair
     * on the screen.
     *
     * @param analogStickPosition Position of gamepad analog stick.
     */
    public void handleClockTick(Point analogStickPosition)
    {
        int deltaX = analogStickPosition.x / 2;
        int deltaY = analogStickPosition.y / 2;
        final int threshold = 8;
        if (deltaX > threshold)
        {
            deltaX = threshold + (deltaX - threshold) / 2;
        }
        if (deltaY > threshold)
        {
            deltaY = threshold + (deltaY - threshold) / 2;
        }
        this.crosshairPosition.x += deltaX;
        this.crosshairPosition.y += deltaY;
        if (this.crosshairPosition.x < this.crosshairRadius)
        {
            this.crosshairPosition.x = this.crosshairRadius;
        }
        else if (this.crosshairPosition.x > (this.parentSize.width - this.crosshairRadius))
        {
            this.crosshairPosition.x  = this.parentSize.width - this.crosshairRadius;
        }
        if (this.crosshairPosition.y < this.crosshairRadius)
        {
            this.crosshairPosition.y = this.crosshairRadius;
        }
        else if (this.crosshairPosition.y > (this.parentSize.height - this.crosshairRadius))
        {
            this.crosshairPosition.y  = this.parentSize.height - this.crosshairRadius;
        }
    }
}