comparison game/Engine.java @ 171:5070e57ebbfc

Move text drawing stuff to IDMWidget class.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 06 Mar 2017 10:19:04 +0200
parents b9bc493ae53c
children 8995a0363e0a
comparison
equal deleted inserted replaced
170:b9bc493ae53c 171:5070e57ebbfc
23 class AboutBox extends IDMWidget 23 class AboutBox extends IDMWidget
24 { 24 {
25 BufferedImage aboutImg; 25 BufferedImage aboutImg;
26 boolean aboutSecret; 26 boolean aboutSecret;
27 27
28 IDMPoint currPos, currOffs;
29 Paint textPaint;
30 Font textFont;
31 FontMetrics textMetrics;
32
33
34 public AboutBox() 28 public AboutBox()
35 { 29 {
36 try { 30 try {
37 ResourceLoader res = new ResourceLoader("graphics/girl.jpg"); 31 ResourceLoader res = new ResourceLoader("graphics/girl.jpg");
38 aboutImg = ImageIO.read(res.getStream()); 32 aboutImg = ImageIO.read(res.getStream());
43 37
44 aboutSecret = false; 38 aboutSecret = false;
45 39
46 setPos(150f, 150f); 40 setPos(150f, 150f);
47 setSize(720f, 420f); 41 setSize(720f, 420f);
48 }
49
50 public void setTextFont(Font font, FontMetrics metrics)
51 {
52 textFont = font;
53 textMetrics = metrics;
54 }
55
56 public void setTextPaint(Paint paint)
57 {
58 textPaint = paint;
59 }
60
61 public void setCurrPos(IDMPoint npos)
62 {
63 currPos = npos;
64 currOffs = new IDMPoint(0, 0);
65 }
66
67 public void setCurrPos(float x, float y)
68 {
69 setCurrPos(new IDMPoint(x, y));
70 }
71
72 public void setCurrPosScaledRel(float x, float y)
73 {
74 setCurrPos(new IDMPoint(getScaledRelX(x), getScaledRelY(y)));
75 }
76
77 public void drawString(Graphics2D g, String text)
78 {
79 Paint savePaint = g.getPaint();
80 g.setPaint(textPaint);
81 g.setFont(textFont);
82
83 int i = 0;
84 while (i < text.length())
85 {
86 int p = text.indexOf("\n", i);
87 boolean linefeed;
88 String str;
89 if (p >= i)
90 {
91 str = text.substring(i, p);
92 i = p + 1;
93 linefeed = true;
94 }
95 else
96 {
97 str = text.substring(i);
98 i += str.length();
99 linefeed = false;
100 }
101
102 g.drawString(str, currPos.x + currOffs.x, currPos.y + currOffs.y);
103
104 if (linefeed)
105 {
106 currOffs.x = 0;
107 currOffs.y += textMetrics.getHeight();
108 }
109 else
110 {
111 currOffs.x += textMetrics.stringWidth(str);
112 }
113 }
114
115 g.setPaint(savePaint);
116 } 42 }
117 43
118 public void paint(Graphics2D g) 44 public void paint(Graphics2D g)
119 { 45 {
120 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f)); 46 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f));
152 "Audio from archive.org, used non-commercially.\n \n"); 78 "Audio from archive.org, used non-commercially.\n \n");
153 79
154 setTextPaint(Color.red); 80 setTextPaint(Color.red);
155 drawString(g, "Controls:\n"); 81 drawString(g, "Controls:\n");
156 82
157 IDMPoint old = currOffs.copy(); 83 IDMPoint old = textCurrOffs.copy();
158 84
159 setTextPaint(Color.white); 85 setTextPaint(Color.white);
160 drawString(g, 86 drawString(g,
161 "Arrow keys / mouse wheel\n"+ 87 "Arrow keys / mouse wheel\n"+
162 "Enter / mouse click\n"+ 88 "Enter / mouse click\n"+
163 "Space bar\n"); 89 "Space bar\n");
164 90
165 currPos.x += getScaledX(330); 91 textCurrPos.x += getScaledX(330);
166 currOffs.y = old.y; 92 textCurrOffs.y = old.y;
167 drawString(g, 93 drawString(g,
168 "- Rotate piece\n" + 94 "- Rotate piece\n" +
169 "- Place/lock piece\n" + 95 "- Place/lock piece\n" +
170 "- Swap piece\n"); 96 "- Swap piece\n");
171 97
172 currPos.x -= getScaledX(330); 98 textCurrPos.x -= getScaledX(330);
173 setTextPaint(Color.green); 99 setTextPaint(Color.green);
174 drawString(g, 100 drawString(g,
175 "\nObjective: Create a path long as possible by rotating\n"+ 101 "\nObjective: Create a path long as possible by rotating\n"+
176 "and placing pieces. More points will be awarded for\n"+ 102 "and placing pieces. More points will be awarded for\n"+
177 "advancing the path by several segments per turn." 103 "advancing the path by several segments per turn."