view game/IDMButton.java @ 138:9eb791e2fa17

Optimize board updating logic, so that the old placed tiles need not to be redrawn from scratch on each screen update, as they do not change usually.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 25 Nov 2011 11:04:09 +0200
parents 4c0dec72e2f0
children d6d92845d6a2
line wrap: on
line source

/*
 * Ristipolku IDM button widget
 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
 */
package game;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.io.*;


public class IDMButton extends IDMWidget
{
    enum State { FOCUSED, PRESSED, NORMAL }
    State state;
    static BufferedImage imgUp, imgPressed;
    Font font;
    FontMetrics metrics;
    String text;
    boolean active;

    public IDMButton(IDMPoint pos, int keyCode, Font font, String text)
    {
        super(pos);
        loadImages();
        setSize(imgUp.getWidth(), imgUp.getHeight());

        this.font = font;
        this.keyCode = keyCode;
        this.text = text;

        state = State.NORMAL;
        active = false;
    }

    public IDMButton(float x, float y, int keyCode, Font font, String text)
    {
        this(new IDMPoint(x, y), keyCode, font, text);
    }

    private static void loadImages()
    {
        if (imgUp != null && imgPressed != null)
            return;

        try
        {
            ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
            imgUp = ImageIO.read(res.getStream());

            res = new ResourceLoader("graphics/button1_down.png");
            imgPressed = ImageIO.read(res.getStream());
        }
        catch (IOException e)
        {
            System.out.print(e.getMessage());
        }
    }

    public void scale()
    {
    }

    public void paint(Graphics2D g)
    {
        BufferedImage img;
        int xoffs, yoffs;

        if (state == State.PRESSED)
        {
            img = imgPressed;
            xoffs = yoffs = 5;
        }
        else
        {
            xoffs = yoffs = 0;
            img = imgUp;
        }

        if (metrics == null)
            metrics = g.getFontMetrics(font);

        int textWidth = metrics.stringWidth(text);
        g.drawImage(img, getScaledX() + xoffs, getScaledY() + yoffs, null);

        g.setFont(font);
        g.setPaint(Color.black);
        g.drawString(text,
           getScaledX() + xoffs + (getScaledWidth() - textWidth) / 2,
           getScaledY() + yoffs + getScaledHeight() / 2);
    }


    public boolean mousePressed(MouseEvent e)
    {
        state = State.PRESSED;
        active = true;
        return true;
    }

    public boolean mouseReleased(MouseEvent e)
    {
        boolean oldActive = active;
        super.mouseReleased(e);
        state = State.NORMAL;
        active = false;
        return oldActive;
    }

    public boolean mouseEntered(MouseEvent e)
    {
        if (active)
        {
            state = State.PRESSED;
            return true;
        }
        else
            return false;
    }

    public boolean mouseExited(MouseEvent e)
    {
        if (active)
        {
            state = State.NORMAL;
            return true;
        }
        else
            return false;
    }
}