Mercurial > hg > ristipolku
diff 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 |
line wrap: on
line diff
--- a/game/IDMWidget.java Thu Mar 02 15:57:15 2017 +0200 +++ b/game/IDMWidget.java Mon Mar 06 10:19:04 2017 +0200 @@ -14,6 +14,12 @@ IDMPoint pos, size, scale; int keyCode; + IDMPoint textCurrPos, textCurrOffs; + Paint textPaint; + Font textFont; + FontMetrics textMetrics; + + public IDMWidget() { keyCode = -1; @@ -180,4 +186,72 @@ public void clicked() { } + + public void setTextFont(Font font, FontMetrics metrics) + { + textFont = font; + textMetrics = metrics; + } + + public void setTextPaint(Paint paint) + { + textPaint = paint; + } + + public void setCurrPos(IDMPoint npos) + { + textCurrPos = npos; + textCurrOffs = new IDMPoint(0, 0); + } + + public void setCurrPos(float x, float y) + { + setCurrPos(new IDMPoint(x, y)); + } + + public void setCurrPosScaledRel(float x, float y) + { + setCurrPos(new IDMPoint(getScaledRelX(x), getScaledRelY(y))); + } + + public void drawString(Graphics2D g, String text) + { + Paint savePaint = g.getPaint(); + g.setPaint(textPaint); + g.setFont(textFont); + + int i = 0; + while (i < text.length()) + { + int p = text.indexOf("\n", i); + boolean linefeed; + String str; + if (p >= i) + { + str = text.substring(i, p); + i = p + 1; + linefeed = true; + } + else + { + str = text.substring(i); + i += str.length(); + linefeed = false; + } + + g.drawString(str, textCurrPos.x + textCurrOffs.x, textCurrPos.y + textCurrOffs.y); + + if (linefeed) + { + textCurrOffs.x = 0; + textCurrOffs.y += textMetrics.getHeight(); + } + else + { + textCurrOffs.x += textMetrics.stringWidth(str); + } + } + + g.setPaint(savePaint); + } }