# HG changeset patch # User Matti Hamalainen # Date 1488788344 -7200 # Node ID 5070e57ebbfca9ab884968ce766e0a1223a40654 # Parent b9bc493ae53cacb2864cb4935205c3bbd206a7f6 Move text drawing stuff to IDMWidget class. diff -r b9bc493ae53c -r 5070e57ebbfc game/Engine.java --- a/game/Engine.java Thu Mar 02 15:57:15 2017 +0200 +++ b/game/Engine.java Mon Mar 06 10:19:04 2017 +0200 @@ -25,12 +25,6 @@ BufferedImage aboutImg; boolean aboutSecret; - IDMPoint currPos, currOffs; - Paint textPaint; - Font textFont; - FontMetrics textMetrics; - - public AboutBox() { try { @@ -47,74 +41,6 @@ setSize(720f, 420f); } - public void setTextFont(Font font, FontMetrics metrics) - { - textFont = font; - textMetrics = metrics; - } - - public void setTextPaint(Paint paint) - { - textPaint = paint; - } - - public void setCurrPos(IDMPoint npos) - { - currPos = npos; - currOffs = 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, currPos.x + currOffs.x, currPos.y + currOffs.y); - - if (linefeed) - { - currOffs.x = 0; - currOffs.y += textMetrics.getHeight(); - } - else - { - currOffs.x += textMetrics.stringWidth(str); - } - } - - g.setPaint(savePaint); - } - public void paint(Graphics2D g) { g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f)); @@ -154,7 +80,7 @@ setTextPaint(Color.red); drawString(g, "Controls:\n"); - IDMPoint old = currOffs.copy(); + IDMPoint old = textCurrOffs.copy(); setTextPaint(Color.white); drawString(g, @@ -162,14 +88,14 @@ "Enter / mouse click\n"+ "Space bar\n"); - currPos.x += getScaledX(330); - currOffs.y = old.y; + textCurrPos.x += getScaledX(330); + textCurrOffs.y = old.y; drawString(g, "- Rotate piece\n" + "- Place/lock piece\n" + "- Swap piece\n"); - currPos.x -= getScaledX(330); + textCurrPos.x -= getScaledX(330); setTextPaint(Color.green); drawString(g, "\nObjective: Create a path long as possible by rotating\n"+ diff -r b9bc493ae53c -r 5070e57ebbfc game/IDMWidget.java --- 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); + } }