comparison game/IDMWidget.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 55ea5821c802
comparison
equal deleted inserted replaced
170:b9bc493ae53c 171:5070e57ebbfc
11 public class IDMWidget 11 public class IDMWidget
12 { 12 {
13 IDMWidget parent; 13 IDMWidget parent;
14 IDMPoint pos, size, scale; 14 IDMPoint pos, size, scale;
15 int keyCode; 15 int keyCode;
16
17 IDMPoint textCurrPos, textCurrOffs;
18 Paint textPaint;
19 Font textFont;
20 FontMetrics textMetrics;
21
16 22
17 public IDMWidget() 23 public IDMWidget()
18 { 24 {
19 keyCode = -1; 25 keyCode = -1;
20 this.scale = new IDMPoint(1, 1); 26 this.scale = new IDMPoint(1, 1);
178 } 184 }
179 185
180 public void clicked() 186 public void clicked()
181 { 187 {
182 } 188 }
189
190 public void setTextFont(Font font, FontMetrics metrics)
191 {
192 textFont = font;
193 textMetrics = metrics;
194 }
195
196 public void setTextPaint(Paint paint)
197 {
198 textPaint = paint;
199 }
200
201 public void setCurrPos(IDMPoint npos)
202 {
203 textCurrPos = npos;
204 textCurrOffs = new IDMPoint(0, 0);
205 }
206
207 public void setCurrPos(float x, float y)
208 {
209 setCurrPos(new IDMPoint(x, y));
210 }
211
212 public void setCurrPosScaledRel(float x, float y)
213 {
214 setCurrPos(new IDMPoint(getScaledRelX(x), getScaledRelY(y)));
215 }
216
217 public void drawString(Graphics2D g, String text)
218 {
219 Paint savePaint = g.getPaint();
220 g.setPaint(textPaint);
221 g.setFont(textFont);
222
223 int i = 0;
224 while (i < text.length())
225 {
226 int p = text.indexOf("\n", i);
227 boolean linefeed;
228 String str;
229 if (p >= i)
230 {
231 str = text.substring(i, p);
232 i = p + 1;
233 linefeed = true;
234 }
235 else
236 {
237 str = text.substring(i);
238 i += str.length();
239 linefeed = false;
240 }
241
242 g.drawString(str, textCurrPos.x + textCurrOffs.x, textCurrPos.y + textCurrOffs.y);
243
244 if (linefeed)
245 {
246 textCurrOffs.x = 0;
247 textCurrOffs.y += textMetrics.getHeight();
248 }
249 else
250 {
251 textCurrOffs.x += textMetrics.stringWidth(str);
252 }
253 }
254
255 g.setPaint(savePaint);
256 }
183 } 257 }