summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java
blob: ef572264206d52e0ee62e102a79724a566ed47fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.window;

import com.fourisland.fourpuzzle.Audio;
import com.fourisland.fourpuzzle.Display;
import com.fourisland.fourpuzzle.Game;
import com.fourisland.fourpuzzle.database.Database;
import com.fourisland.fourpuzzle.database.Sound;
import com.fourisland.fourpuzzle.util.Renderable;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 *
 * @author hatkirby
 */
public class ChoiceWindow implements Renderable {
    
    private static final int SPACER = 4;
    
    private List<String> choices;
    int numChoices;
    boolean center;
    private int width;
    private int height;
    BufferedImage cacheBase;
    int x;
    int y;
    public ChoiceWindow(List<String> choices, boolean center, ChoiceWindowLocation cwl)
    {
        this.choices = choices;
        numChoices = choices.size();
        this.center = center;
        
        for (String choice : choices)
        {
            int l = Display.getFontMetrics().stringWidth(choice);
            if (l > getWidth())
            {
                width = l;
            }
            
            height += Display.getFontMetrics().getHeight() + SPACER;
        }
        
        cacheBase = Window.Default.getImage(width, height);
        
        x = cwl.getX(width);
        y = cwl.getY(height);
    }

    public void render(Graphics2D g2)
    {
        Display.setFont(g2);
        
        g2.drawImage(cacheBase, x, y, null);
        
        int fh = g2.getFontMetrics().getHeight();
        int ty = Window.Default.getTopY()+fh+y;
        for (String choice : choices)
        {
            int fw = g2.getFontMetrics().stringWidth(choice);
            int tx = Window.Default.getLeftX()+x;
            
            if (center)
            {
                tx += ((width/2)-(fw/2));
            }
            
            if (getSelected().equals(choice))
            {
                g2.drawImage(Window.Selector.getImage(fw-Window.Selector.getLeftX(), fh-Window.Selector.getTopY()), tx-SPACER, ty-fh-SPACER, null);
            }

            g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh)));
            g2.drawString(choice, tx, ty);
            
            ty+=(SPACER+fh);
        }
    }

    public int getWidth()
    {
        return width;
    }

    public int getHeight()
    {
        return height;
    }
    
    int selected = 0;
    public void moveUp()
    {
        if (selected > 0)
        {
            Audio.playSound(Database.getSound(Sound.MoveCursor));
        
            selected--;
        }
    }
    
    public void moveDown()
    {
        if (selected < (choices.size()-1))
        {
            Audio.playSound(Database.getSound(Sound.MoveCursor));
        
            selected++;
        }
    }
    
    public String getSelected()
    {
        return choices.get(selected);
    }
    
    public static enum ChoiceWindowLocation
    {
        BottomLeft
        {
            public int getX(int width)
            {
                return (Game.WIDTH/5)-(width/2);
            }
        },
        BottomCenter
        {
            public int getX(int width)
            {
                return (Game.WIDTH/2)-(width/2);
            }
        },
        BottomRight
        {
            public int getX(int width)
            {
                return (Game.WIDTH/5*4)-(width/2);
            }    
        };
        
        public abstract int getX(int width);
        
        public int getY(int height)
        {
            return (Game.HEIGHT/4*3)-(height/2);
        }
    }

}