summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/window/MessageWindow.java
blob: ea34ab9acc676917ae7faf7f404c44716f7736db (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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.window;

import com.fourisland.fourpuzzle.Game;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hatkirby
 */
public class MessageWindow {
    
    private static final int SPACER = 4;
    private static final int HEIGHT = 4*(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics().getFontMetrics().getHeight()+SPACER);
    
    private List<String> messages;
    int width;
    BufferedImage cacheBase;
    public MessageWindow(String message)
    {
        width = Game.WIDTH - Window.Default.getFullWidth(0);
        messages = new ArrayList<String>();
        
        initalizeMessages(message, new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics());
        
        cacheBase = Window.Default.getImage(width, HEIGHT);
    }
    
    private void initalizeMessages(String message, Graphics2D g)
    {
        setFont(g);
        
        String temp = message;
        int len = 0;
        while (!temp.isEmpty())
        {
            while ((g.getFontMetrics().stringWidth(temp.substring(0, len)) < (width - SPACER)) && (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;
        }
    }
    
    public void render(Graphics2D g2)
    {
        int y = MessageWindowLocation.Bottom.getY();
        
        g2.drawImage(cacheBase, 0, y, null);

        setFont(g2);
        
        int fh = g2.getFontMetrics().getHeight();
        int ty = Window.Default.getTopY()+fh-(SPACER/2)+y;
        int msgs = Math.min(messages.size(), 4);
        for (int i=0;i<msgs;i++)
        {
            String message = messages.get(i);
            int fw = g2.getFontMetrics().stringWidth(message);
            int tx = Window.Default.getLeftX();
            
            g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh)));
            g2.drawString(message, tx, ty);
            
            ty+=(SPACER+g2.getFontMetrics().getHeight());
        }
        
        g2.setFont(g2.getFont().deriveFont(Font.PLAIN));
    }
    
    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;
        }
    }
    
    public static void setFont(Graphics2D g)
    {
        try {
            g.setFont(Font.createFont(Font.TRUETYPE_FONT, new File("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf")));
        } catch (FontFormatException ex) {
            Logger.getLogger(MessageWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MessageWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        g.setFont(g.getFont().deriveFont(Font.PLAIN, 10));        
    }

}