Skip to content

Commit

Permalink
FootballField.java: Moving transientRanks from MainController to Foot…
Browse files Browse the repository at this point in the history
…ballField.

This is part of a larger effort to minimize the number of steps between
controller and view and back again that rendering needs to take.
  • Loading branch information
dmk257 committed Jul 4, 2024
1 parent 1da3bfe commit 5d9edf6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashSet;

import org.bigredbands.mb.models.CommandPair;
import org.bigredbands.mb.models.Move;
import org.bigredbands.mb.models.RankPosition;

/**
Expand Down Expand Up @@ -273,26 +274,18 @@ public void setSongConstants(HashMap<Integer, Integer> tempoHashMap,
public void setMoveComment(String comment);

/**
* Returns the number of the current move
* Returns the number of the current move.
*
* @return - the number of the current move
* @return - the number of the current move.
*/
public int getCurrentMove();
public int getCurrentMoveNumber();

/**
* Returns additional indicators to be displayed to the user not stored as ranks
*
* @return - a hashmap mapping the name of the indicator to its position
*/
public HashMap<String, RankPosition> getTransientRanks();

/**
* Adds a temporary indicator to show the user how the rank would look if drawn to
* the current position
*
* @param temporaryDrawingRank - the position of the indicator to be drawn
* Returns the current move.
*
* @return - the current move.
*/
public void addTemporaryDrawingRank(RankPosition temporaryDrawingRank);
public Move getCurrentMove();

public void updateInitialPosition(String rankName, RankPosition newPos);

Expand Down
59 changes: 20 additions & 39 deletions src/main/java/org/bigredbands/mb/controllers/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ public class MainController implements ControllerInterface, SynchronizedControll
// The main model which stores the ranks and moves for the project
private DrillInfo drillInfo;

// Additional visual indicators to be displayed to the user, but not stored as ranks
private HashMap<String, RankPosition> transientRanks;

// The name of the indicator used to show the user how their rank will look if drawn
// at the mouse's current position
public static final String TEMP_DRAWING_RANK = "TEMP_DRAWING_RANK";

// The number of the current move
private int currentMove;

Expand Down Expand Up @@ -114,7 +107,6 @@ public static ControllerViewBundle BuildMainControllerAndView() {
public MainController() {
fileUrl = "";
drillInfo = new DrillInfo();
transientRanks = new HashMap<String, RankPosition>();
currentMove = 0;
selectedRanks.clear();
}
Expand Down Expand Up @@ -408,7 +400,7 @@ else if(rankNames.size()==1) {
if (altCmd.getCounts() == refCmd.getCounts()
&& altCmd.getCommand() == refCmd.getCommand()) {
if (i == rankArray.length - 1) {
if (diffCounts != 0) shared.add(new CommandPair(32, diffCounts));
if (diffCounts != 0) shared.add(new CommandPair(CommandPair.EMPTY, diffCounts));
diffCounts = 0;

shared.add(new CommandPair(refCmd.getCommand(), refCmd.getCounts()));
Expand Down Expand Up @@ -457,15 +449,15 @@ else if(rankNames.size()==1) {

diffCounts = (maxLen - sharedLen);

if (diffCounts > 0) shared.add(new CommandPair(32, diffCounts));
if (diffCounts > 0) shared.add(new CommandPair(CommandPair.EMPTY, diffCounts));

return shared;
}
}

/**
* Using the end position of the current move and the commands of all subsequent moves for a rank, updates the start and
* end positions of each move based on the current move. Useful for when the commands of an middle move are
* end positions of each move based on the current move. Useful for when the commands of a middle move are
* changed and the later moves need to have their positions updated.
* @param rankName - the rank whose positions need to be updated.
*/
Expand All @@ -482,9 +474,10 @@ private void updatePositions(String rankName) {
}

/**
* Sets the selected rank to the specified rank
* Sets the selected rank to the specified rank.
*
* @param rankName - the rank name of the new selected rank
* @param rankName - The rank name of the new selected rank
* @param reset - If true, unselects all other ranks before selecting the new one.
*/
@Override
public void addSelectedRank(String rankName, boolean reset) {
Expand Down Expand Up @@ -801,7 +794,7 @@ public void removeCommands(int[] commandIndices) {
ArrayList<CommandPair> sharedCommands = getSharedCommands(selectedRanks, allCommands);

for (int indx : commandIndices) {
if (sharedCommands.get(indx).getCommand() == 32) {
if (sharedCommands.get(indx).getCommand() == CommandPair.EMPTY) {
// If any of the requested commands are a mixed one, return an error and delete nothing
mainView.displayError("Cannot delete mixed command");
return;
Expand Down Expand Up @@ -848,7 +841,7 @@ public void renameCommand(int index, String name) {
HashMap<String,ArrayList<CommandPair>> allCommands = drillInfo.getMoves().get(currentMove).getCommands();
ArrayList<CommandPair> sharedCommands = getSharedCommands(selectedRanks, allCommands);

if (sharedCommands.get(index).getCommand() == 32) {
if (sharedCommands.get(index).getCommand() == CommandPair.EMPTY) {
mainView.displayError("Cannot rename mixed command");
return;
}
Expand Down Expand Up @@ -892,7 +885,7 @@ public void moveCommandsUp(int[] commandIndices) {
ArrayList<CommandPair> sharedCommands = getSharedCommands(selectedRanks, allCommands);

for (int indx : commandIndices) {
if (sharedCommands.get(indx).getCommand() == 32) {
if (sharedCommands.get(indx).getCommand() == CommandPair.EMPTY) {
mainView.displayError("Cannot move mixed command up");
return;
}
Expand Down Expand Up @@ -940,7 +933,7 @@ public void moveCommandsDown(int[] commandIndices) {
ArrayList<CommandPair> sharedCommands = getSharedCommands(selectedRanks, allCommands);

for (int indx : commandIndices) {
if (sharedCommands.get(indx).getCommand() == 32) {
if (sharedCommands.get(indx).getCommand() == CommandPair.EMPTY) {
mainView.displayError("Cannot move mixed command down");
return;
}
Expand Down Expand Up @@ -989,12 +982,12 @@ public void mergeCommands(int[] commandIndices) {
ArrayList<CommandPair> sharedCommands = getSharedCommands(selectedRanks, allCommands);

for (int indx : commandIndices) {
if (sharedCommands.get(indx).getCommand() == 32) {
if (sharedCommands.get(indx).getCommand() == CommandPair.EMPTY) {
mainView.displayError("Cannot merge mixed commands");
return;
}

if (sharedCommands.get(indx).getCommand() == 19) {
if (sharedCommands.get(indx).getCommand() == CommandPair.DTP) {
mainView.displayError("Cannot merge DTP commands");
return;
}
Expand Down Expand Up @@ -1053,12 +1046,12 @@ public String splitCommand(int index, int count) {
return "The specified count was larger than counts in the move.";
}

if (sharedCommands.get(index).getCommand() == 32) {
if (sharedCommands.get(index).getCommand() == CommandPair.EMPTY) {
mainView.displayError("Cannot split mixed command");
return "";
}

if (sharedCommands.get(index).getCommand() == 19) {
if (sharedCommands.get(index).getCommand() == CommandPair.DTP) {
mainView.displayError("Cannot split DTP command");
return "";
}
Expand Down Expand Up @@ -1140,31 +1133,19 @@ public void setMoveComment(String comment) {
* @return - the number of the current move
*/
@Override
public int getCurrentMove() {
public int getCurrentMoveNumber() {
return currentMove;
}

/**
* Returns additional indicators to be displayed to the user not stored as ranks
*
* @return - a hashmap mapping the name of the indicator to its position
* Returns the current move.
*
* @return - the current move.
*/
@Override
public HashMap<String, RankPosition> getTransientRanks() {
return transientRanks;
public Move getCurrentMove() {
return drillInfo.getMoves().get(currentMove);
}

/**
* Adds a temporary indicator to show the user how the rank would look if drawn to
* the current position
*
* @param temporaryDrawingRank - the position of the indicator to be drawn
*/
@Override
public void addTemporaryDrawingRank(RankPosition temporaryDrawingRank) {
transientRanks.put(TEMP_DRAWING_RANK, temporaryDrawingRank);
mainView.updateView(currentMove, drillInfo.getMoves().get(currentMove).getCounts());
}
}


50 changes: 33 additions & 17 deletions src/main/java/org/bigredbands/mb/views/FootballField.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.bigredbands.mb.controllers.MainController;
import org.bigredbands.mb.models.Point;
import org.bigredbands.mb.models.RankPosition;

Expand Down Expand Up @@ -78,13 +77,18 @@ public class FootballField extends JPanel {
private int xMouseOval = -1;
private int yMouseOval = -1;

// The name of the indicator used to show the user how their rank will look if drawn
// at the mouse's current position
public static final String TEMP_DRAWING_RANK = "TEMP_DRAWING_RANK";

// Additional visual indicators to be displayed to the user, but not stored as ranks
private HashMap<String, RankPosition> transientRanks;

/**
* Creates a static football field which only displays one move.
* @param mainView - the MainView used to call functions to interact with the controller and other views.
* @param moveNumber - the move number to display.
*/

public FootballField(MainView mainView, int moveNumber) {
super();

Expand All @@ -103,6 +107,7 @@ public FootballField(MainView mainView) {

this.mainView = mainView;
this.moveNumber = -1;
transientRanks = new HashMap<String, RankPosition>();

repaint();
}
Expand Down Expand Up @@ -193,7 +198,7 @@ public void paintField(Graphics g) {
lineMap = createShapes(mainView.getRankPositions(), topLeftX, topLeftY, scaleFactor);
}
drawRanks(lineMap,
createShapes(mainView.getTransientRanks(), topLeftX, topLeftY, scaleFactor),
createShapes(transientRanks, topLeftX, topLeftY, scaleFactor),
mainView.getSelectedRanks(),
g,
topLeftX,
Expand All @@ -218,7 +223,7 @@ public void paintField(Graphics g) {
}
//else, create a static football field for the specified move number
else {
if (moveNumber == mainView.getCurrentMove()) {
if (moveNumber == mainView.getCurrentMoveNumber()) {
//Set background to a transparent blue color
Color backgroundColor = new Color(0,0,255,100);
g.setColor(backgroundColor);
Expand Down Expand Up @@ -367,7 +372,7 @@ public static void drawFieldLines(Graphics g, Dimension dim, int margin,
gDash.setStroke(new BasicStroke(twoAndHalfYdThick));
for (int i = 0; i <22; i++) { // x's the same for each iteration
if (i==8){
// //draw nothing - hashes
//draw nothing - hashes
}
else{
gDash.drawLine((margin + dumbBufferWidth + (dim.width
Expand Down Expand Up @@ -636,7 +641,7 @@ public void mouseClicked(MouseEvent arg0) {
// If this is the first click, you've just placed the tail of the new rank
projectView.setMessageLabelText("Click where the head of the new rank is located");
drawRankPoint(p);
mainView.addTemporaryDrawingRank(new RankPosition(new Point(xToYards, yToYards), new Point(xToYards, yToYards)));
addTemporaryDrawingRank(new RankPosition(new Point(xToYards, yToYards), new Point(xToYards, yToYards)));
}
else if (clicks >= 2) {
// If this is the second click, you've just placed the head (i.e., second point)
Expand All @@ -653,7 +658,7 @@ else if (clicks >= 2) {
name = projectView.getRankNameText();

// Add the new rank to the list of ranks
RankPosition rankPosition = mainView.getTransientRanks().remove(MainController.TEMP_DRAWING_RANK);
RankPosition rankPosition = transientRanks.remove(TEMP_DRAWING_RANK);
rankPosition.getFront().setPoint(xToYards, yToYards);
mainView.addRank(name, rankPosition);

Expand Down Expand Up @@ -681,14 +686,14 @@ else if (clicks >= 2) {
if (clicks == 1) {
// If this is the first click, set that point as the tail of the rank
drawDTPPoint(p);
mainView.addTemporaryDrawingRank(new RankPosition(new Point(xToYards, yToYards),
addTemporaryDrawingRank(new RankPosition(new Point(xToYards, yToYards),
new Point(xToYards, yToYards)));
}
else if (clicks == 2) {
drawRankPoint(p);

// Set the DTP rank to (initially) be this
RankPosition rankPosition = mainView.getTransientRanks().remove(MainController.TEMP_DRAWING_RANK);
RankPosition rankPosition = transientRanks.remove(TEMP_DRAWING_RANK);
rankPosition.getFront().setPoint(xToYards, yToYards);
projectView.setDTPDest(rankPosition);

Expand Down Expand Up @@ -733,7 +738,7 @@ else if (clicks == 2) {
projectView.repaintFieldPanel();
projectView.repaintScrollBar();
}
} else if(SwingUtilities.isRightMouseButton(arg0) && mainView.getCurrentMove() == 0) {
} else if(SwingUtilities.isRightMouseButton(arg0) && mainView.getCurrentMoveNumber() == 0) {
// Right clicked on the normal rank in Move 0 - swap between line and curve rank

for (String rankName : lineMap.keySet()) {
Expand All @@ -759,7 +764,7 @@ else if (clicks == 2) {
}
}
} else if(SwingUtilities.isLeftMouseButton(arg0) ||
(SwingUtilities.isRightMouseButton(arg0) && mainView.getCurrentMove() != 0)) {
(SwingUtilities.isRightMouseButton(arg0) && mainView.getCurrentMoveNumber() != 0)) {
// Normal selection of a rank - if it's a control-press, add the rank to the list of
// those currently selected
System.out.println("Checking rank selection...");
Expand Down Expand Up @@ -966,7 +971,7 @@ public void mouseReleased(MouseEvent arg0) {
public void mouseDragged(MouseEvent arg0) {
// Dragging ranks on move 0
RankPosition DTPRank = mainView.getDTPRank();
if(mainView.getDrag() && !addDTPFlag && mainView.getCurrentMove() == 0){
if(mainView.getDrag() && !addDTPFlag && mainView.getCurrentMoveNumber() == 0){
HashSet<String> ranks = mainView.getSelectedRanks();

int newX = arg0.getX();
Expand Down Expand Up @@ -1126,7 +1131,7 @@ public void mouseDragged(MouseEvent arg0) {
xDragOrigin = newX;
yDragOrigin = newY;

if(mainView.getCurrentMove()==0) {
if(mainView.getCurrentMoveNumber()==0) {
for(String rankName : mainView.getSelectedRanks()) {
mainView.updateInitialPosition(rankName, mainView.getRankPositions().get(rankName));
}
Expand Down Expand Up @@ -1262,7 +1267,7 @@ public void mouseMoved(MouseEvent arg0) {
yMouseOval = (int)(topLeftY + yToYards * scaleFactor);
repaint();
} else if (clicks == 1) {
RankPosition temporaryDrawingRank = mainView.getTransientRanks().get(MainController.TEMP_DRAWING_RANK);
RankPosition temporaryDrawingRank = transientRanks.get(TEMP_DRAWING_RANK);
temporaryDrawingRank.getFront().setPoint(xToYards, yToYards);
repaint();
}
Expand All @@ -1283,7 +1288,7 @@ else if (addDTPFlag) {
yMouseOval = (int)(topLeftY + yToYards * scaleFactor);
repaint();
} else if (clicks == 1) {
RankPosition temporaryDrawingRank = mainView.getTransientRanks().get(MainController.TEMP_DRAWING_RANK);
RankPosition temporaryDrawingRank = transientRanks.get(TEMP_DRAWING_RANK);
temporaryDrawingRank.getFront().setPoint(xToYards, yToYards);
repaint();
}
Expand Down Expand Up @@ -1365,7 +1370,7 @@ public void endDTP() {
addDTPFlag = false;
xMouseOval = 0;
yMouseOval = 0;
mainView.getTransientRanks().remove(MainController.TEMP_DRAWING_RANK); // in case you're mid-draw
transientRanks.remove(TEMP_DRAWING_RANK); // in case you're mid-draw
}
}

Expand All @@ -1388,7 +1393,7 @@ public void endAddRank() {
addRankFlag = false;
xMouseOval = 0;
yMouseOval = 0;
mainView.getTransientRanks().remove(MainController.TEMP_DRAWING_RANK); // in case you're mid-draw
transientRanks.remove(TEMP_DRAWING_RANK); // in case you're mid-draw
}
}

Expand Down Expand Up @@ -1618,4 +1623,15 @@ else if (shape instanceof Path2D) {
g.setFont(new Font("",Font.BOLD,20));
}
}

/**
* Adds a temporary indicator to show the user how the rank would look if drawn to
* the current position
*
* @param temporaryDrawingRank - the position of the indicator to be drawn
*/
private void addTemporaryDrawingRank(RankPosition temporaryDrawingRank) {
transientRanks.put(TEMP_DRAWING_RANK, temporaryDrawingRank);
mainView.updateView(mainView.getCurrentMoveNumber(), mainView.getCurrentMove().getCounts());
}
}
Loading

0 comments on commit 5d9edf6

Please sign in to comment.