summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/window/MessageWindow.java
blob: dab67d1d266d4566262cdc21c6deefd69f45e7c9 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.window;

import com.fourisland.fourpuzzle.Display;
import com.fourisland.fourpuzzle.Game;
import com.fourisland.fourpuzzle.KeyboardInput;
import com.fourisland.fourpuzzle.KeyInput;
import com.fourisland.fourpuzzle.gamestate.mapview.FaceSet;
import com.fourisland.fourpuzzle.util.Inputable;
import com.fourisland.fourpuzzle.util.Interval;
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.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 *
 * @author hatkirby
 */
public class MessageWindow implements Renderable {
    
    private static final int SPACER = 4;
    private static final int HEIGHT = (4*(Display.getFontMetrics().getHeight()+SPACER));
    
    private volatile List<String> messages;
    int width;
    BufferedImage cacheBase;
    int num = 0;
    int upTo = 0;
    boolean bounceArrow = false;
    Interval in = Interval.createTickInterval(4);
    private MessageWindow(String message)
    {
        width = Game.WIDTH - Window.Default.getFullWidth(0);
        cacheBase = Window.Default.getImage(width, HEIGHT);
        
        initalizeMessages(message);
    }
    
    boolean hasFace = false;
    BufferedImage face;
    private MessageWindow(String message, String faceSet, int face)
    {
        width = Game.WIDTH - Window.Default.getFullWidth(0);
        cacheBase = Window.Default.getImage(width, HEIGHT);
        
        this.face = FaceSet.getFaceSet(faceSet).getImage(face);
        hasFace = true;
        
        initalizeMessages(message);
    }
    
    private static void displayMessage(final MessageWindow mw) throws InterruptedException
    {
        final CountDownLatch cdl = new CountDownLatch(1);
        Inputable in = new Inputable() {
            public void processInput(KeyInput key)
            {
                if (key.isActionDown())
                {
                    if (mw.pushEnter())
                    {    
                        cdl.countDown();
                    }
                    
                    key.letGo();
                }
            }
        };
        
        Display.registerRenderable(mw);
        KeyboardInput.registerInputable(in);

        try
        {
            cdl.await();
        } catch (InterruptedException ex)
        {
            throw ex;
        } finally {    
            Display.unregisterRenderable(mw);
            KeyboardInput.unregisterInputable(in);
        }
    }
    
    public static void displayMessage(String message) throws InterruptedException
    {
        displayMessage(new MessageWindow(message));
    }
    
    public static void displayMessage(String message, String faceSet, int face) throws InterruptedException
    {
        displayMessage(new MessageWindow(message, faceSet, face));
    }
    
    private void initalizeMessages(String message)
    {
        messages = new ArrayList<String>();
        
        int length = width - SPACER;
        if (hasFace)
        {
            length -= (48 + (SPACER*3));
        }
        
        String temp = message;
        int len = 0;
        while (!temp.isEmpty())
        {
            while ((Display.getFontMetrics().stringWidth(temp.substring(0, len)) < length) && (len < temp.length()))
            {
                len++;
            }

            if (len != temp.length())
            {
                while ((!temp.substring(len, len+1).equals(" ")) && (len > 0))
                {
                    len--;
                }
            }
            
            messages.add(temp.substring(0, len));
            
            if (len != temp.length())
            {
                temp = temp.substring(len+1);
            } else {
                temp = "";
            }
            
            len = 0;
        }
        
        setLength();
    }
    
    private void setLength()
    {
        num = 0;
        
        for (int i=0;i<Math.min(messages.size(),4);i++)
        {
            num += messages.get(i).length();
        }
    }
    
    public void render(Graphics2D g2)
    {
        int y = MessageWindowLocation.Bottom.getY();

        Display.setFont(g2);
        
        g2.drawImage(cacheBase, 0, y, null);
        
        int fh = g2.getFontMetrics().getHeight();
        int tx = Window.Default.getLeftX();
        int ty = Window.Default.getTopY()+fh-(SPACER/2)+y;
        int msgs = Math.min(messages.size(), 4);
        int toPrint = upTo;
        
        if (hasFace)
        {
            g2.drawImage(face, tx+SPACER, ty-fh+SPACER, null);

            tx += 48 + (SPACER*2);
        }
        
        for (int i=0;i<msgs;i++)
        {
            String message = messages.get(i);
            int fw = g2.getFontMetrics().stringWidth(message);
            
            g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh)));
            g2.drawString(message.substring(0, Math.min(toPrint, message.length())), tx, ty);
            
            ty+=(SPACER+fh);
            
            toPrint -= Math.min(toPrint, message.length());
        }
        
        if (upTo < num)
        {
            upTo+=3;
        } else {
            g2.drawImage(SystemGraphic.getDownArrow(), (Window.Default.getFullWidth(width)/2)-5, y+HEIGHT+SPACER+(bounceArrow ? 1 : 0), null);
            
            if (in.isElapsed())
            {
                bounceArrow = !bounceArrow;
            }
        }
    }
    
    private synchronized boolean pushEnter()
    {
        if (upTo >= num)
        {
            int msgs = messages.size();
            for (int i=0;i<Math.min(4, msgs);i++)
            {
                messages.remove(0);
            }
            
            if (messages.size() > 0)
            {
                upTo = 0;
                setLength();
            } else {
                return true;
            }
        }
        
        return false;
    }
    
    public static enum MessageWindowLocation
    {
        Top(0),
        Middle((Game.HEIGHT/2)-(Window.Default.getFullHeight(MessageWindow.HEIGHT)/2)),
        Bottom(Game.HEIGHT-Window.Default.getFullHeight(MessageWindow.HEIGHT));
        
        private int y;
        private MessageWindowLocation(int y)
        {
            this.y = y;
        }
        
        public int getY()
        {
            return y;
        }
    }

}