Skip to content

Commit

Permalink
Merge pull request #36 from UnGuZartex/GUI
Browse files Browse the repository at this point in the history
Gui
  • Loading branch information
UnGuZartex authored Mar 15, 2020
2 parents 41f70eb + d28d8d9 commit ce31c86
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 84 deletions.
10 changes: 5 additions & 5 deletions src/main/java/Controllers/ConnectionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
public class ConnectionController {

private final GUItoSystemInterface converter;
private final PABlockHandler handler;
private final PABlockHandler blockHandler;

public ConnectionController(GUItoSystemInterface converter, PABlockHandler handler) {
public ConnectionController(GUItoSystemInterface converter, PABlockHandler blockHandler) {
this.converter = converter;
this.handler = handler;
this.blockHandler = blockHandler;
}

public void connectBlocks(GUIBlock withMain, GUIBlock withSub, String connectionID) {
Block mainBlock = converter.getBlockFromGUIBlock(withMain);
SubConnector subConnector = converter.getSubConnectorFromGUIBlockWithID(withSub, connectionID);
handler.connectToExistingBlock(mainBlock, subConnector);
blockHandler.connectToExistingBlock(mainBlock, subConnector);
}

public void disconnectBlock(GUIBlock withMain) {
Block mainBlock = converter.getBlockFromGUIBlock(withMain);
handler.disconnectInPA(mainBlock);
blockHandler.disconnectInPA(mainBlock);
}

public boolean isValidConnection(GUIBlock withMain, GUIBlock withSub, String connectionID) {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/Controllers/GUItoSystemInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public GUIBlock createNewGUIBlock(String id, int x, int y) {
}
}

public Block getBlockFromGUIBlock(GUIBlock block) {
public Block getBlockFromGUIBlock(GUIBlock block) throws IllegalArgumentException {

if (!conversionTable.containsKey(block)) {
throw new IllegalArgumentException("The given GUI block is not present in the conversion table!");
}

return conversionTable.get(block);
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/Controllers/LevelDataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,51 @@

import java.awt.*;

/**
* A class used as a static level data loader.
* Is only used as a first loader to load the level data the first time.
*
* @author Alpha-team
*/
public class LevelDataLoader {

/**
* Return the current robot position
* @return the current robot position
*/
public Position getRobotPosition() {
return new Position(GameState.getCurrentLevel().getRobot().getX(), GameState.getCurrentLevel().getRobot().getY());
}

/**
* Return the current robot direction
* @return the current robot direction
*/
public Direction getRobotDirection() {
return GameState.getCurrentLevel().getRobot().getDirection();
}

/**
* Return the current grid size
* @return the current grid size
*/
public Position getGridSize() {
return new Position(GameState.getCurrentLevel().getGrid().getWidth(), GameState.getCurrentLevel().getGrid().getHeight());
}

/**
* Return the current array of grid cells
* @return the current array of grid cells
*/
public Cell[][] getGridCells() {
return GameState.getCurrentLevel().getGrid().getCells();
}

/**
* Subscribe the given robot listener to the current robot observer.
*
* @param listener the given robot listener.
*/
public void subscribe(RobotListener listener) {
GameState.getCurrentLevel().getRobot().subscribe(listener);
}
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/Controllers/ProgramController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@

public class ProgramController {

private final PABlockHandler blockHandler = new PABlockHandler();
private final LevelLoader loader = new LevelLoader();
private final GUItoSystemInterface converter = new GUItoSystemInterface(blockHandler);
private final ConnectionController controller = new ConnectionController(converter, blockHandler);
private final GUItoSystemInterface converter;
private final PABlockHandler blockHandler;

public ProgramController(GUItoSystemInterface converter, PABlockHandler blockHandler) {
this.converter = converter;
this.blockHandler = blockHandler;
}

public void addBlockToPA(GUIBlock block) {
Block toAdd = converter.getBlockFromGUIBlock(block);
Expand All @@ -29,8 +33,7 @@ public void deleteFromPA(GUIBlock block) {
}

public void deleteAsProgram(GUIBlock block) {
Block toDelete = converter.getBlockFromGUIBlock(block);
blockHandler.deleteProgram(toDelete);
blockHandler.deleteProgram(converter.getBlockFromGUIBlock(block));
}

public boolean reachedMaxBlocks() {
Expand All @@ -47,10 +50,7 @@ public GUIBlock getHightlightedBlock() {
}

public GUIBlock getBlock(String ID, int x, int y) {
if (!reachedMaxBlocks()) {
return converter.createNewGUIBlock(ID, x, y);
}
return null;
return converter.createNewGUIBlock(ID, x, y);
}

public void resetProgram() {
Expand All @@ -69,8 +69,4 @@ public void subscribeListener(ProgramListener listener) {
public void unsubscribeListener(ProgramListener listener) {
blockHandler.getPA().unsubscribe(listener);
}

public ConnectionController getController() {
return controller;
}
}
19 changes: 16 additions & 3 deletions src/main/java/GUI/BlockrCanvas.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package GUI;

import Controllers.ConnectionController;
import Controllers.GUItoSystemInterface;
import Controllers.ProgramController;
import GUI.Blocks.GUIBlock;
import GUI.Components.GUIBlockHandler;
Expand All @@ -8,6 +10,7 @@
import GUI.Panel.GameWorldPanel;
import GUI.Panel.PalettePanel;
import GUI.Panel.ProgramAreaPanel;
import System.Logic.ProgramArea.PABlockHandler;

import java.awt.*;
import java.awt.event.KeyEvent;
Expand All @@ -25,7 +28,8 @@ public class BlockrCanvas extends CanvasWindow {
private GUIBlockHandler blockHandler;
private GUIBlock previousBlock;

private ProgramController programController = new ProgramController();
private ProgramController programController;
private ConnectionController connectionController;

/**
* Initializes a CanvasWindow object.
Expand All @@ -40,7 +44,9 @@ protected BlockrCanvas(String title, int width, int height, String imagePackName
this.height = height;

GamePanel.setImageLibrary(ImagePreLoader.createImageLibrary(imagePackName));
setControllers();
setPanels();

blockHandler = new GUIBlockHandler(palettePanel, programAreaPanel);
}

Expand All @@ -61,13 +67,13 @@ protected void paint(Graphics g) {
@Override
protected void handleMouseEvent(int id, int x, int y, int clickCount) {
super.handleMouseEvent(id, x, y, clickCount);
blockHandler.handleMouseEvent(id, x, y, programController);
blockHandler.handleMouseEvent(id, x, y);
repaint();
}

private void setPanels() {
palettePanel = new PalettePanel(0, 0, (int)(width * PALETTE_WIDTH_RATIO), height, programController);
programAreaPanel = new ProgramAreaPanel((int)(width * PALETTE_WIDTH_RATIO),0, (int)(width * PROGRAMAREA_WIDTH_RATIO), height, programController);
programAreaPanel = new ProgramAreaPanel((int)(width * PALETTE_WIDTH_RATIO),0, (int)(width * PROGRAMAREA_WIDTH_RATIO), height, programController, connectionController);
gameWorldPanel = new GameWorldPanel((int)(width * PALETTE_WIDTH_RATIO) + (int)(width * PROGRAMAREA_WIDTH_RATIO),0, (int)(width * GAMEWORLD_WIDTH_RATIO), height, programController);
}

Expand All @@ -93,4 +99,11 @@ protected void handleKeyEvent(int id, int keyCode, char keyChar) {

repaint();
}

private void setControllers() {
PABlockHandler blockHandler = new PABlockHandler();
GUItoSystemInterface converter = new GUItoSystemInterface(blockHandler);
connectionController = new ConnectionController(converter, blockHandler);
programController = new ProgramController(converter, blockHandler);
}
}
50 changes: 37 additions & 13 deletions src/main/java/GUI/Blocks/GUIBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,49 +235,67 @@ public boolean intersectsWithConnector(GUIBlock other) {
return findCollidingConnector(subConnectors, other.mainConnector) != null || findCollidingConnector(other.subConnectors, mainConnector) != null;
}

// TODO doc
public void connectWithStaticBlock(GUIBlock other, ProgramController Pcontroller) {
/**
* The current dragged block or set of blocks this block is in is being connected with a given
* static block if possible, while using the given program controller and connection controller to handle
* program and connection actions.
*
* @param other The given static block
* @param programController The given program controller
* @param connectionController The given connection controller
*
* @post The height of this block and its connected set of blocks is changed accordingly.
* @post The position of this block set is changed accordingly to the type of completed connection.
*
* @effect The main connector of this block is connected to a sub connector of the given static block, if possible.
* @effect A sub connector of this block is connected to the main connector of the given static block, if possible.
* @effect The connection controller adds a logical system connection depending on the type of connection.
* @effect The program controller adds the highest block from the set this block is in to the logical program area.
* @effect The height of the given static block and its connected set of blocks is changed accordingly.
* @effect The position of the static block set is changed accordingly to the type of completed connection.
*
* @throws IllegalArgumentException When the given block does not have a colliding connector
*/
public void connectWithStaticBlock(GUIBlock other, ProgramController programController, ConnectionController connectionController) throws IllegalArgumentException{

GUIConnector intersectingConnectorSub;
ConnectionController controller = Pcontroller.getController();
Position staticBlockConnectorPosition, draggedBlockConnectorPosition;
GUIBlock main, sub;

if ((intersectingConnectorSub = findCollidingConnector(other.subConnectors, mainConnector)) != null) {
main = this;
sub = other;
Pcontroller.deleteAsProgram(this);
programController.deleteAsProgram(this);
draggedBlockConnectorPosition = mainConnector.getCollisionCircle().getPosition();
staticBlockConnectorPosition = intersectingConnectorSub.getCollisionCircle().getPosition();
}
else if ((intersectingConnectorSub = findCollidingConnector(subConnectors, other.mainConnector)) != null) {
sub = this;
main = other;
Pcontroller.deleteAsProgram(other);
programController.deleteAsProgram(other);
staticBlockConnectorPosition = other.mainConnector.getCollisionCircle().getPosition();
draggedBlockConnectorPosition = intersectingConnectorSub.getCollisionCircle().getPosition();
}
else {
throw new IllegalArgumentException("Given block does not have a colliding connector!");
}

if (controller.isValidConnection(main, sub, intersectingConnectorSub.getId())) {


if (connectionController.isValidConnection(main, sub, intersectingConnectorSub.getId())) {
if (!mainConnector.isConnected()) {
setPosition(staticBlockConnectorPosition.getX() + (getX() - draggedBlockConnectorPosition.getX()), staticBlockConnectorPosition.getY() + (getY() - draggedBlockConnectorPosition.getY()));
main.mainConnector.connect(intersectingConnectorSub);
controller.connectBlocks(main, sub, intersectingConnectorSub.getId());
connectionController.connectBlocks(main, sub, intersectingConnectorSub.getId());
changeHeight(main.getHeight(), main);
}
else {
other.setPosition(draggedBlockConnectorPosition.getX() + (other.getX() - staticBlockConnectorPosition.getX()), draggedBlockConnectorPosition.getY() + (other.getY() - staticBlockConnectorPosition.getY()));
main.mainConnector.connect(intersectingConnectorSub);
controller.connectBlocks(main, sub, intersectingConnectorSub.getId());
connectionController.connectBlocks(main, sub, intersectingConnectorSub.getId());
other.changeHeight(main.getHeight(), main);
}
}
Pcontroller.addBlockToPA(getHighest());

programController.addBlockToPA(getHighest());
}

/**
Expand All @@ -295,12 +313,18 @@ public GUIBlock getHighest() {
}
}

// TODO doc
/**
* Reset the height of the block structure connected to this block's main connector
* as if the current block and its connected sub-blocks where added to the block structure.
*/
public void resetHeight() {
changeHeight(getHeight(), this);
}

// TODO doc
/**
* Decrease the height of the block structure connected to this block's main connector
* as if the current block and its connected sub-blocks where removed from the block structure.
*/
public void disconnectHeight() {
changeHeight(-getHeight(), this);
}
Expand Down
Loading

0 comments on commit ce31c86

Please sign in to comment.