# HG changeset patch # User Matti Hamalainen # Date 1488463035 -7200 # Node ID b9bc493ae53cacb2864cb4935205c3bbd206a7f6 # Parent 32b1c41e194ae07401ec3a96ba0c50edde55ad9c Modularize and clean up code. diff -r 32b1c41e194a -r b9bc493ae53c game/Engine.java --- a/game/Engine.java Thu Mar 02 14:18:21 2017 +0200 +++ b/game/Engine.java Thu Mar 02 15:57:15 2017 +0200 @@ -44,7 +44,7 @@ aboutSecret = false; setPos(150f, 150f); - setSize(675f, 420f); + setSize(720f, 420f); } public void setTextFont(Font font, FontMetrics metrics) @@ -69,6 +69,11 @@ 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(); @@ -112,31 +117,28 @@ public void paint(Graphics2D g) { - int x = getScaledX(), y = getScaledY(), - w = getScaledWidth(), h = getScaledHeight(); - g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f)); - g.fill(new RoundRectangle2D.Float(x, y, w, h, 10, 10)); + g.fill(new RoundRectangle2D.Float(getScaledX(), getScaledY(), getScaledWidth(), getScaledHeight(), 10, 10)); setTextFont(G.fonts[3], G.metrics[3]); setTextPaint(Color.white); - setCurrPos(x + 20, y + 30); + setCurrPosScaledRel(20, 30); drawString(g, "RistiPolku (CrossPaths) v"+ G.version +"\n"); setTextFont(G.fonts[1], G.metrics[1]); if (aboutSecret) { - g.drawImage(aboutImg, x + 20, y + 55, + g.drawImage(aboutImg, (int) getScaledRelX(20), (int) getScaledRelY(55), aboutImg.getWidth(), aboutImg.getHeight(), null); - setCurrPos(x + 225, y + 75); + setCurrPosScaledRel(225, 75); drawString(g, "Dedicated to my\n" + "favorite woman\n" + "in the world."); - setCurrPos(x + 370, y + 175); + setCurrPosScaledRel(370, 175); drawString(g, "- Matti"); } else @@ -160,14 +162,14 @@ "Enter / mouse click\n"+ "Space bar\n"); - currPos.x += 330; + currPos.x += getScaledX(330); currOffs.y = old.y; drawString(g, "- Rotate piece\n" + "- Place/lock piece\n" + "- Swap piece\n"); - currPos.x -= 330; + currPos.x -= getScaledX(330); setTextPaint(Color.green); drawString(g, "\nObjective: Create a path long as possible by rotating\n"+ @@ -626,10 +628,9 @@ res = new ResourceLoader("graphics/font.ttf"); G.fonts = new Font[G.numFonts]; - G.fonts[0] = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); - G.fonts[1] = G.fonts[0].deriveFont(24f); - G.fonts[2] = G.fonts[0].deriveFont(64f); - G.fonts[3] = G.fonts[0].deriveFont(32f); + G.baseFont = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); + + setupDerivedFonts(1.0f); } catch (FontFormatException e) { @@ -689,6 +690,14 @@ lauta.startNewGame(); } + public void setupDerivedFonts(float scale) + { + G.fonts[0] = G.baseFont.deriveFont(16f * scale); + G.fonts[1] = G.baseFont.deriveFont(22f * scale); + G.fonts[2] = G.baseFont.deriveFont(62f * scale); + G.fonts[3] = G.baseFont.deriveFont(32f * scale); + } + public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; @@ -697,24 +706,27 @@ // Rescale if parent component size has changed Dimension dim = getSize(); - if (G.screenDim == null || !dim.equals(G.screenDim)) + if (G.screenDim == null || !dim.equals(G.screenDim) || G.metrics == null) { + dbg("Scale changed.\n"); + scaleChanged = true; + updateBoard = true; + float dw = dim.width / 1024.0f, dh = dim.height / 768.0f; + G.screenDim = dim; + + // Rescale fonts + setupDerivedFonts(dw); + + // Get font metrics against current Graphics2D context + G.metrics = new FontMetrics[G.numFonts]; + for (int i = 0; i < G.numFonts; i++) + G.metrics[i] = g2.getFontMetrics(G.fonts[i]); + // Rescale IDM GUI widgets widgets.setScale(dw, dh); - G.screenDim = dim; - - // Rescale background image - // Rescale fonts - G.fonts[1] = G.fonts[0].deriveFont(24f * dw); - G.fonts[2] = G.fonts[0].deriveFont(64f * dw); - G.fonts[3] = G.fonts[0].deriveFont(32f * dw); - - dbg("Scale changed.\n"); - scaleChanged = true; - updateBoard = true; } if (updateBoard) @@ -736,13 +748,6 @@ lauta.paintBoard(gimg, false); } - // Get font metrics against current Graphics2D context - if (G.metrics == null || scaleChanged) - { - G.metrics = new FontMetrics[G.numFonts]; - for (int i = 0; i < G.numFonts; i++) - G.metrics[i] = g2.getFontMetrics(G.fonts[i]); - } // Draw background image, pieces, widgets g2.drawImage(G.lautaBGScaled, 0, 0, null); @@ -840,6 +845,8 @@ public void keyPressed(KeyEvent e) { // Handle keyboard input + + // About box is modal, so pass key events to it when active if (widgets.containsObject(aboutBox)) aboutBox.keyPressed(e); else diff -r 32b1c41e194a -r b9bc493ae53c game/G.java --- a/game/G.java Thu Mar 02 14:18:21 2017 +0200 +++ b/game/G.java Thu Mar 02 15:57:15 2017 +0200 @@ -23,6 +23,7 @@ public static final String version = "0.86"; public static final int numFonts = 4; + public static Font baseFont; public static Font fonts[]; public static FontMetrics metrics[]; diff -r 32b1c41e194a -r b9bc493ae53c game/IDMWidget.java --- a/game/IDMWidget.java Thu Mar 02 14:18:21 2017 +0200 +++ b/game/IDMWidget.java Thu Mar 02 15:57:15 2017 +0200 @@ -88,6 +88,26 @@ return (int) (pos.y * scale.y); } + public int getScaledX(float x) + { + return (int) (x * scale.x); + } + + public int getScaledY(float y) + { + return (int) (y * scale.y); + } + + public int getScaledRelX(float x) + { + return (int) ((pos.x + x) * scale.x); + } + + public int getScaledRelY(float y) + { + return (int) ((pos.y + y) * scale.y); + } + public int getScaledWidth() { return (int) (size.x * scale.x);