Skip to content

Commit

Permalink
WIP #4: ShipSelectionPanel
Browse files Browse the repository at this point in the history
-Replace PreGamePanel with ShipSelectionPanel
-Radio button group implementation

Also:
-Fix default ready to false
-Add accessors for Ship
-Catch for null callback
-TransparentJPanel now defaults to null LayoutManager

Various debugging/WIP/hack stuff is left in to help debug this issue:
-Hack in getCurrentShip() defaulting to 1
-Hack in RealEngineeringConsoleManager.java left in but commented
-Need to sleep in selectShip()?
-Print sending ready
-Print sending ship/console selection
  • Loading branch information
jacobmaxfrank committed Oct 3, 2016
1 parent 775f8c4 commit 8ddf76a
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 78 deletions.
Binary file added assets/art/textures/gui/radiobutton/checked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/art/textures/gui/radiobutton/unchecked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public BaseEngineeringConsoleManager() {
}

this.shipSystemGrid = shipSystemGrid;

}

protected void afterChildConstructor() {
Expand All @@ -85,7 +84,7 @@ public void resetForNewGame() {
public Property<Boolean> getPlayerReady() {
return playerReady;
}
private final SettableProperty<Boolean> playerReady = new SettableProperty<>(true);
private final SettableProperty<Boolean> playerReady = new SettableProperty<>(false);


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,20 @@ public Ship(String name, ShipType type, int shipNumber) {
@Override
public String toString() {
return "Ship #" + shipNumber + ": " + name + " (" + type.getDescription() + ")";
}
}

public String getName() {
return this.name;
}

public ShipType getType() {
return this.type;
}

public int getShipNumber()
{
return this.shipNumber;
}
}

public enum ShipType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public void connect(String host, int port) {
connectionStateChangeObservable.triggerChange();
if (!connectProcessed) {
sendShipAndConsoleChoice();
// TODO hack so you can get into the game - remove once UI for marking ready is done
System.out.println("Temporarily marking ready");
ready();

// TODO: ! hack so you can get into the game - remove once UI for marking ready is done
// System.out.println("Temporarily marking ready");
// ready();
}
});

Expand All @@ -80,14 +80,18 @@ public void connect(String host, int port) {
@Override
public void selectShip(int shipNumber) {
super.selectShip(shipNumber);
// TODO: !! sleep here?
sendShipAndConsoleChoice();
}

@Override
public void ready() {
super.ready();

if (worldAwareServer != null && this.worldAwareServer.isConnected()) {
// TODO: ! DBG
System.out.println("Sending ready");

this.worldAwareServer.ready();
}
}
Expand All @@ -96,6 +100,8 @@ private void sendShipAndConsoleChoice() {
if (worldAwareServer != null && this.worldAwareServer.isConnected()) {
this.worldAwareServer.getServer().send(new SetShipPacket(shipNumber));
this.worldAwareServer.getServer().send(new SetConsolePacket(Console.ENGINEERING, true));

System.out.println("Selecting engineering console on ship number " + this.shipNumber);
}
}

Expand All @@ -107,14 +113,15 @@ public void disconnect() {
public Property<GameState> getGameState() {
return gameState;
}
private final DerivedProperty<GameState> gameState = new DerivedProperty<>(() -> {
private final DerivedProperty<GameState> gameState = new DerivedProperty<>(() -> {
if (worldAwareServer == null || !worldAwareServer.isConnected()) {
return GameState.DISCONNECTED;
}
else if (this.worldAwareServer.getSystemManager().isGameOverScreen()) {

if (this.worldAwareServer.getSystemManager().isGameOverScreen()) {
return GameState.GAMEOVER;
}
else if ( this.getCurrentShip() != null && getPlayerReady().get()) {
else if (this.getCurrentShip() != null && getPlayerReady().get()) {
return GameState.INGAME;
}
else {
Expand All @@ -127,7 +134,7 @@ else if ( this.getCurrentShip() != null && getPlayerReady().get()) {
public Property<Map<ShipSystem, Integer>> getSystemEnergyAllocated() {
return systemEnergyAllocated;
}
private final DerivedProperty<Map<ShipSystem, Integer>> systemEnergyAllocated = new DerivedProperty<>( () -> {
private final DerivedProperty<Map<ShipSystem, Integer>> systemEnergyAllocated = new DerivedProperty<>(() -> {

Map<ShipSystem, Integer> result = new HashMap<>();
for(ShipSystem system: ShipSystem.values()) {
Expand Down Expand Up @@ -423,7 +430,10 @@ public Property<Ship[]> getAllShips() {
}, systemManagerChangeObservable);

private ArtemisPlayer getCurrentShip() {
return this.worldAwareServer.getSystemManager().getPlayerShip(shipNumber);
// System.out.println("Getting current ship with number " + shipNumber);
// System.out.println("ship null? " + (this.worldAwareServer.getSystemManager().getPlayerShip(1) == null));
// TODO: ! dbg
return this.worldAwareServer.getSystemManager().getPlayerShip(1); // shipNumber);
}


Expand Down
9 changes: 7 additions & 2 deletions src/com/brindyblitz/artemis/engconsole/ui/Button.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.brindyblitz.artemis.engconsole.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
Expand All @@ -15,7 +14,7 @@ public class Button extends TransparentJPanel implements MouseListener, MouseMot

private String label;
private Color backgroundColor, backgroundColorHover, labelColor, borderColor; // TODO: ENHANCEMENT > on hover and on click colors, SFX
private Font labelFont = new Font("Courier New", Font.BOLD, 12);
protected Font labelFont = new Font("Courier New", Font.BOLD, 12);
private int width, height;
private Runnable onClickHandler;

Expand All @@ -30,6 +29,7 @@ public Button(String label, Runnable onClickHandler,
this.label = label;
this.onClickHandler = onClickHandler;
this.backgroundColor = backgroundColor;
this.backgroundColorHover = backgroundColorHover;
this.labelColor = labelColor;
this.borderColor = borderColor;
this.width = minWidth;
Expand Down Expand Up @@ -82,6 +82,11 @@ public void mouseMoved(MouseEvent arg0) {

@Override
public void mouseClicked(MouseEvent arg0) {
if (this.onClickHandler == null) {
System.err.println("No click handler defined for Button " + this.label);
throw new RuntimeException();
}

this.onClickHandler.run();
}

Expand Down
38 changes: 0 additions & 38 deletions src/com/brindyblitz/artemis/engconsole/ui/PreGamePanel.java

This file was deleted.

134 changes: 134 additions & 0 deletions src/com/brindyblitz/artemis/engconsole/ui/RadioButtonGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.brindyblitz.artemis.engconsole.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class RadioButtonGroup extends TransparentJPanel {
private static final long serialVersionUID = 1L;

private List<RadioButton> buttons;
private RadioButton selected = null;

private static BufferedImage imageChecked, imageUnchecked;

private Color backgroundColor, backgroundColorHover, labelColor, borderColor;

public RadioButtonGroup(String[] labels, Runnable[] callbacks,
int width, int button_height, int button_spacer,
Color backgroundColor, Color backgroundColorHover, Color labelColor, Color borderColor) {
this.backgroundColor = backgroundColor;
this.backgroundColorHover = backgroundColorHover;
this.labelColor = labelColor;
this.borderColor = borderColor;

loadIcons();

this.setLayout(null);
this.setSize(width, (button_height + button_spacer) * (labels.length - 1) + button_height + 1);

this.buttons = new ArrayList<RadioButton>(labels.length);
for (int i = 0; i < labels.length; i++) {
String label = labels[i];
RadioButton rb = new RadioButton(label, this, callbacks[i],
this.backgroundColor, this.backgroundColorHover, this.labelColor, this.borderColor,
width, button_height);

this.buttons.add(rb);
this.add(rb).setLocation(0, i * (button_height + button_spacer));
}
}

protected void loadIcons() {
try {
if (imageChecked == null) {
imageChecked = ImageIO.read(new File(System.getProperty("user.dir"), "assets/art/textures/gui/radiobutton/checked.png"));
imageUnchecked = ImageIO.read(new File(System.getProperty("user.dir"), "assets/art/textures/gui/radiobutton/unchecked.png"));
}
} catch (IOException e) {
System.err.println("Unable to locate radio button icon(s)");
e.printStackTrace(System.err);
throw new RuntimeException();
}
}

private void onChildSelected(RadioButton rb) {
if (rb.equals(this.selected))
return;

for (RadioButton child : this.buttons) {
child.setSelected(false);
}

this.selected = rb;
rb.setSelected(true);
}

public RadioButton getSelected() {
return this.selected;
}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D gfx = (Graphics2D) g;

gfx.setColor(this.borderColor);
gfx.drawRect(0, 0, this.getWidth()-1, this.getHeight()-1);
}


/////////////////
// RadioButton //
/////////////////

public class RadioButton extends Button {
private static final long serialVersionUID = 1L;

public RadioButtonGroup parent;
private boolean selected = false;
private int imageBorder = 2;

public RadioButton(String label, RadioButtonGroup parent, Runnable onClickHandler,
Color backgroundColor, Color backgroundColorHover, Color labelColor, Color borderColor,
int minWidth, int minHeight) {
super(label, onClickHandler, backgroundColor, backgroundColorHover, labelColor, borderColor, minWidth, minHeight);
this.parent = parent;

this.setMaximumSize(new Dimension(parent.getWidth(), parent.getHeight()));

this.labelFont = new Font("Courier New", Font.BOLD, 18);
}

public void setSelected(boolean selected) {
this.selected = selected;
this.repaint();
}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D gfx = (Graphics2D) g;

BufferedImage img = this.selected ? RadioButtonGroup.imageChecked : RadioButtonGroup.imageUnchecked;
float image_scale = ((float)this.getHeight() - 2f * (float)imageBorder) / (float)img.getHeight();
gfx.drawImage(img, imageBorder, imageBorder, (int)(image_scale * img.getWidth()), (int)(image_scale * img.getHeight()), this);
}

@Override
public void mouseClicked(MouseEvent e) {
parent.onChildSelected(this);
super.mouseClicked(e);
}
}
}
Loading

0 comments on commit 8ddf76a

Please sign in to comment.