Skip to content

Commit

Permalink
implemented working action buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
bazola committed Sep 18, 2014
1 parent 10c1b7f commit a3843ca
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

package com.cardshifter.client;

import com.cardshifter.server.outgoing.UseableActionMessage;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class ActionButton extends Group {

private final UseableActionMessage message;
private final GameClientController controller;
private final double sizeX;
private final double sizeY;

public ActionButton(UseableActionMessage message, GameClientController controller, double sizeX, double sizeY) {
this.message = message;
this.controller = controller;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.setUpRectangle();
this.setUpLabel();
this.setOnMouseClicked(this::actionButtonClicked);
}
private void setUpRectangle() {
Rectangle actionBack = new Rectangle(0, 0, this.sizeX, this.sizeY);
actionBack.setFill(Color.BLUEVIOLET);
this.getChildren().add(actionBack);
}
private void setUpLabel() {
Label actionLabel = new Label();
actionLabel.setText(this.message.getAction());
actionLabel.relocate(this.sizeX/2.5, 0);
this.getChildren().add(actionLabel);
}

private void actionButtonClicked(MouseEvent event) {
this.controller.createAndSendMessage(this.message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

Expand Down Expand Up @@ -43,7 +43,15 @@ public CardHandDocumentController(CardInfoMessage message, GameClientController
this.card = message;
this.setCardId();
this.setCardLabels();
this.setActionForRootPane();
}

private void setActionForRootPane() {
this.anchorPane.setOnMouseClicked(this::actionOnClick);
}
private void actionOnClick(MouseEvent event) {
System.out.println("Action detected on card" + this.cardId.textProperty());
}

public AnchorPane getRootPane() {
return this.anchorPane;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
Expand All @@ -55,6 +56,7 @@ public class GameClientController {
@FXML private HBox opponentBattlefieldPane;
@FXML private HBox playerHandPane;
@FXML private HBox playerBattlefieldPane;
@FXML private HBox actionBox;

private final ObjectMapper mapper = new ObjectMapper();
private final BlockingQueue<Message> messages = new LinkedBlockingQueue<>();
Expand Down Expand Up @@ -137,7 +139,6 @@ public void play() {
message = messages.take();
}
this.gameId = ((NewGameMessage) message).getGameId();
this.playLoop();
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -160,6 +161,7 @@ private void listen() {
}
}

/*
private void playLoop() throws JsonParseException, JsonMappingException, IOException {
while (true) {
if (actionsForServer.isEmpty()) {
Expand All @@ -179,6 +181,23 @@ private void playLoop() throws JsonParseException, JsonMappingException, IOExcep
}
}
}
*/

public void createAndSendMessage(Message message) {
try {
UseableActionMessage action = (UseableActionMessage)message;

if (action.isTargetRequired()) {
this.send(new RequestTargetsMessage(gameId, action.getId(), action.getAction()));
} else {
this.send(new UseAbilityMessage(gameId, action.getId(), action.getAction(), action.getTargetId()));
}
} catch (NumberFormatException | IndexOutOfBoundsException ex) {
System.out.println("Not a valid action");
}

this.actionBox.getChildren().clear();
}

private void send(Message message) {
try {
Expand Down Expand Up @@ -208,9 +227,22 @@ private void processMessageFromServer(Message message) {
this.processCardInfoMessage((CardInfoMessage)message);
} else if (message instanceof NewGameMessage) {
this.processNewGameMessage((NewGameMessage)message);
} else if (message instanceof UseableActionMessage) {
this.processUseableActionMessage((UseableActionMessage)message);
}
}

private void processUseableActionMessage(UseableActionMessage message) {
double paneHeight = actionBox.getHeight();
double paneWidth = actionBox.getWidth();

int maxActions = 5;
double actionWidth = paneWidth / maxActions;

ActionButton actionButton = new ActionButton(message, this, actionWidth, paneHeight);
actionBox.getChildren().add(actionButton);
}

private void processNewGameMessage(NewGameMessage message) {
this.playerIndex = message.getPlayerIndex();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

<AnchorPane fx:id="rootPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.client.GameClientController">
<children>

<!-- temporary action box -->
<HBox fx:id="actionBox" alignment="CENTER" spacing="10" layoutX="239.0" layoutY="343.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="44.0" prefWidth="810.0" scaleShape="false" />

<!-- Buttons -->
<!-- <Button fx:id="button" layoutX="1098.0" layoutY="530.0" onAction="#startGameButtonAction" text="Start Game" /> -->
Expand Down

0 comments on commit a3843ca

Please sign in to comment.