Skip to content

Commit

Permalink
Fixes, add res display to UIW
Browse files Browse the repository at this point in the history
Fix #81, fix #91 (?), fix #100, add resource display to UnitInfoWindow
Bizarrely, the fix to #91 seems to be to revert the previous fix to #91


pom.xml
Bump version

src/gui/BuildPanel.java
Refactor resource display to static utility methods.

src/gui/PlanetMap.java
fix #81; fix #91 by reverting the fix to #91

src/gui/UnitInfoWindow.java
fix #100; add resource display

src/util/UtilG.java
Refactor resource display to static utility methods.
  • Loading branch information
joulupunikki committed Dec 13, 2016
1 parent 1b37d17 commit fe627e7
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -9,7 +9,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.joulupunikki</groupId>
<artifactId>Phoenix</artifactId>
<version>0.52.2-alpha</version>
<version>0.52.3-alpha</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down
61 changes: 4 additions & 57 deletions src/gui/BuildPanel.java
Expand Up @@ -42,9 +42,7 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -77,6 +75,7 @@
import util.Comp;
import util.FN;
import util.Util;
import util.UtilG;
import util.WindowSize;

/**
Expand Down Expand Up @@ -166,26 +165,6 @@ public void setUpResDisplay() {

}

/**
* Show required res amounts for unit
*
* @param unit
*/
public void drawResAmounts(int[] unit) {
int[] res_needed = game.getUnitTypes()[unit[0]][unit[1]].reqd_res;
int planet = (Integer) planet_list.getSelectedValue();
//System.out.println("Planet name = " + game.getPlanet(planet).name);
int[] res_avail = game.getResources().getResourcesAvailable(planet, game.getTurn());
for (int i = 0; i < res_display.length; i++) {
if (res_avail[C.REQUIRED_RESOURCES[i]] - res_needed[C.REQUIRED_RESOURCES[i]] < 0) {
res_display[i].setForeground(Color.RED);
} else {
res_display[i].setForeground(C.COLOR_RES_DISP_GREEN);
}
res_display[i].setText(Util.c4Display(res_needed[C.REQUIRED_RESOURCES[i]]));

}
}

/**
* Show res amounts for planet
Expand Down Expand Up @@ -352,7 +331,8 @@ public void mousePressed(MouseEvent e) {
} else {
input_unit_nr = -1;
}
drawResAmounts(unit);
UtilG.drawResAmounts(unit, (Integer) planet_list.getSelectedValue(),
game, res_display);
UnitType ut = game.getUnitTypes()[unit[0]][unit[1]];
left_stats.setValues(ut);
right_stats.setValues(ut);
Expand Down Expand Up @@ -723,44 +703,11 @@ public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(33, 33, 33));
g.fillRect(0, 0, ws.planet_map_width, ws.planet_map_height);
drawResourceIcons(bi.getRaster());
UtilG.drawResourceIcons(bi.getRaster(), input_unit_nr, gui, ws, 11, 166);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bi, null, 0, 0);
}

public void drawResourceIcons(WritableRaster wr) {
int[][] res_icons = gui.getResources().getResIcons();
int x = 11;
int y = 166;
int x_offset = 38;
int[] pixel_data = new int[1];
int w;
int h;
int i = 0;
final int[] ZEROS = new int[C.EFSUNIT_BIN_WIDTH * C.EFSUNIT_BIN_HEIGHT];
Arrays.fill(ZEROS, C.INDEX_COLOR_EFS_BLACK);
// if we need input unit draw its icon instead of resource 0
wr.setPixels(x, y, C.EFSUNIT_BIN_WIDTH, C.EFSUNIT_BIN_HEIGHT, ZEROS);
if (input_unit_nr > -1) {
i = 1;
w = C.EFSUNIT_BIN_WIDTH;
h = C.EFSUNIT_BIN_HEIGHT;
int[][] unit_icons = Gui.getUnitIcons();
Util.writeImage(pixel_data, input_unit_nr, unit_icons,
wr, ws, w, h, x, y);
}

w = C.CARGO_WIDTH;
h = C.CARGO_HEIGHT;

for (; i < C.REQUIRED_RESOURCES.length; i++) {
Util.writeImage(pixel_data, C.REQUIRED_RESOURCES[i], res_icons,
wr, ws, w, h, x + i * x_offset, y);

}

}

class CustomRendererInt extends JLabel
implements ListCellRenderer {

Expand Down
10 changes: 5 additions & 5 deletions src/gui/PlanetMap.java
Expand Up @@ -1307,7 +1307,7 @@ public static int[] getTileNo(int u, int v, Game game) {
if (city != null) {
if (h.isSpotted(game.getTurn())) {
tile_no[STRUCTURE] = city.type;
} else {
} else if (city.type != C.RUINS && city.type != C.ALIEN_RUINS && city.type != C.MONASTERY) { // fix #81
tile_no[STRUCTURE] = 21;
}
}
Expand Down Expand Up @@ -1683,13 +1683,13 @@ public static int[] getTileNo(int u, int v, Game game) {
}
{
int flags = planet_map_flags[u][v];
if (h.getTerrain(C.ROAD)) { // fix #91
flags >>>= 9;
}
// if (h.getTerrain(C.ROAD)) { // fix #91
// System.out.println(" ROAD");
// flags >>>= 9;
// }
final int mask = 0b0000_1111_1111;
flags &= mask;
String s_flags = Util.createFlagString(flags);

switch (s_flags) {
case "0124": tile_no[MOUNTAIN] = 132; break;
case "0125": tile_no[MOUNTAIN] = 133; break;
Expand Down
33 changes: 31 additions & 2 deletions src/gui/UnitInfoWindow.java
Expand Up @@ -117,6 +117,7 @@ public class UnitInfoWindow extends JPanel {
private JPopupMenu filter_type;
private JMenuItem[] type_items;
private JButton filter_type_button;
private JTextField[] res_display;

private TYPE_FILTER type_filter;
private Map<Enum, Integer> c;
Expand All @@ -137,10 +138,33 @@ public void setUpWindow() {
setUpListeners();
setUpButtons();
setUpStatDisplay();
setUpResDisplay();
byte[][] pallette = gui.getPallette();
bi = Util.loadImage(FN.S_UNITINFO_PCX, ws.is_double, pallette, 640, 480);
}

public void setUpResDisplay() {
res_display = new JTextField[C.REQUIRED_RESOURCES.length];
for (int i = 0; i < res_display.length; i++) {
res_display[i] = new JTextField();
this.add(res_display[i]);
int x_offset = 123;
if (ws.is_double) {
x_offset *= 2;
}
res_display[i].setBounds(x_offset + ws.bp_res_display_x_offset + i * ws.pw_res_display_x_gap, ws.bp_res_display_y_offset, ws.bp_res_display_w, ws.bp_res_display_h);
// res_display[i].setBackground(Color.WHITE);
res_display[i].setOpaque(false);
res_display[i].setForeground(C.COLOR_RES_DISP_GREEN);
res_display[i].setEditable(false);
res_display[i].setHorizontalAlignment(JTextField.CENTER);
res_display[i].setBorder(null);
res_display[i].setFont(ws.font_default);
// res_display[i].setText("123");
}

}

/**
* Set mode, true for Group Finder, false for Unit Info.
*
Expand Down Expand Up @@ -600,9 +624,14 @@ public void setStats() {
attack_stats.setValues(null);

if (u == null) {
for (JTextField item : res_display) {
item.setText("");
}
return;
}

UtilG.drawResourceIcons(bi.getRaster(), u.type_data.unit, gui, ws, 134, 166);
int[] unit = {u.type, u.t_lvl};
UtilG.drawResAmounts(unit, -1, game, res_display);
top_stats.setValues(u);
left_stats.setValues(u.type_data);
right_stats.setValues(u.type_data);
Expand Down Expand Up @@ -687,7 +716,7 @@ public void drawUnits(Graphics g) {
g.setColor(C.COLOR_GOLD);
g.setFont(ws.font_abbrev);

if (e.experience > 0) {
if (e.experience > 0 && e.type != C.CARGO_UNIT_TYPE) { // fix #100
if (e.experience == 1) {
g.setColor(Color.LIGHT_GRAY);
}
Expand Down
51 changes: 51 additions & 0 deletions src/util/UtilG.java
Expand Up @@ -51,6 +51,7 @@
import java.nio.ByteOrder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -61,6 +62,7 @@
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.plaf.BorderUIResource;
Expand Down Expand Up @@ -1132,6 +1134,55 @@ public static int[][] makeDarkUnitIcons(int[] scaler, int[][] unit_icons) {
return tmp;
}

public static void drawResourceIcons(WritableRaster wr, int input_unit_nr, Gui gui, WindowSize ws, int x, int y) {
int[][] res_icons = gui.getResources().getResIcons();
int x_offset = 38;
int[] pixel_data = new int[1];
int w;
int h;
int i = 0;
final int[] ZEROS = new int[C.EFSUNIT_BIN_WIDTH * C.EFSUNIT_BIN_HEIGHT];
Arrays.fill(ZEROS, C.INDEX_COLOR_EFS_BLACK);
// if we need input unit draw its icon instead of resource 0
wr.setPixels(x, y, C.EFSUNIT_BIN_WIDTH, C.EFSUNIT_BIN_HEIGHT, ZEROS);
if (input_unit_nr > -1) {
i = 1;
w = C.EFSUNIT_BIN_WIDTH;
h = C.EFSUNIT_BIN_HEIGHT;
int[][] unit_icons = Gui.getUnitIcons();
Util.writeImage(pixel_data, input_unit_nr, unit_icons, wr, ws, w, h, x, y);
}
w = C.CARGO_WIDTH;
h = C.CARGO_HEIGHT;
for (; i < C.REQUIRED_RESOURCES.length; i++) {
Util.writeImage(pixel_data, C.REQUIRED_RESOURCES[i], res_icons, wr, ws, w, h, x + i * x_offset, y);
}
}

/**
* Show required res amounts for unit
*
* @param unit
*/
public static void drawResAmounts(int[] unit, int planet, Game game, JTextField[] res_display) {
int[] res_needed = game.getUnitTypes()[unit[0]][unit[1]].reqd_res;
//int planet = (Integer) planet_list.getSelectedValue();
//System.out.println("Planet name = " + game.getPlanet(planet).name);
int[] res_avail = null;
if (planet > -1) {
res_avail = game.getResources().getResourcesAvailable(planet, game.getTurn());
}

for (int i = 0; i < res_display.length; i++) {
if (planet > -1 && res_avail[C.REQUIRED_RESOURCES[i]] - res_needed[C.REQUIRED_RESOURCES[i]] < 0) {
res_display[i].setForeground(Color.RED);
} else {
res_display[i].setForeground(C.COLOR_RES_DISP_GREEN);
}
res_display[i].setText(Util.c4Display(res_needed[C.REQUIRED_RESOURCES[i]]));
}
}

public static class DarkTheme extends DefaultMetalTheme {

public String getName() {
Expand Down

0 comments on commit fe627e7

Please sign in to comment.