Skip to content

Commit

Permalink
WPR-16 - Add groups selection to Route Details screen
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav-yo committed Jan 27, 2024
1 parent 3112e21 commit 49aa330
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
import java.util.List;
import java.util.Map;

@Getter
@Component
public class RouteService {

@Getter
LuaValue route;
private String coalition;
private int countryId;
private String unitType;
@Getter
private int groupId;

public void setRoute(LuaValue route, String coalition, int countryId, String unitType, int groupId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void loadRouteData() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("RouteDetails.fxml"));
loader.setControllerFactory(context::getBean);
SplitPane routePane = loader.load();
Tab tab = new Tab("Route " + routeService.printDetails());
Tab tab = new Tab(String.format("Route coalition %s, country %d, unitType %s", routeService.getCoalition(), routeService.getCountryId(), routeService.getUnitType()));
tab.setContent(routePane);
tabPane.getTabs().add(tab);
tabPane.getSelectionModel().select(tab);
Expand Down
75 changes: 47 additions & 28 deletions src/main/java/org/faulty/wpreplace/ui/RouteDetailsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.faulty.wpreplace.services.MissionService;
import org.faulty.wpreplace.services.RouteService;
import org.faulty.wpreplace.utils.RouteCanvasUtil;
import org.faulty.wpreplace.utils.RouteUtils;
import org.luaj.vm2.LuaValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractApplicationContext;
Expand All @@ -33,8 +34,6 @@

@Component
public class RouteDetailsController {
@FXML
public ImageView mapImageView;
@Autowired
private AbstractApplicationContext context;
@Autowired
Expand All @@ -53,16 +52,36 @@ public class RouteDetailsController {
public CheckBox showAll;
@FXML
public Canvas canvas;
@FXML
public ImageView mapImageView;
@FXML
public ComboBox<Integer> groupSelect;
private Map<Integer, List<RouteDetails>> allGroupRoutes;
private boolean drawAllRouts;

public void initialize() {
MapEntry mapEntry = new MapEntry(missionService.getMapName());
initRouteTable(mapEntry);
setAllGroups();
initGroupSelect(mapEntry);
initScrollPane(mapEntry);
initCanvas(mapEntry);
initRouteTable(mapEntry);
initDrawAllCheckBox(mapEntry);
}

private void setAllGroups() {
Map<Integer, LuaValue> friendlyGroupRoutes = routeService.getFriendlyGroupRoutes(missionService.getMission());
allGroupRoutes = friendlyGroupRoutes.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, r -> RouteDetails.fromLuaRoute(r.getValue().checktable())));
}

private void initGroupSelect(MapEntry mapEntry) {
groupSelect.getItems().addAll(RouteUtils.getSelectedGroupIds(missionService.getMission(),
routeService.getCoalition(), routeService.getCountryId(), routeService.getUnitType()));
groupSelect.setValue(routeService.getGroupId());
groupSelect.setOnAction(event -> setRouteDetails(mapEntry));
}

private void initScrollPane(MapEntry mapEntry) {
scrollPane.setOnMousePressed(event -> {
if (event.isMiddleButtonDown()) {
Expand All @@ -76,8 +95,8 @@ private void initScrollPane(MapEntry mapEntry) {
});
scrollPane.addEventFilter(ScrollEvent.ANY, event -> {
double deltaY = event.getDeltaY();
double scaleFactor = deltaY > 0 ? 1.1 : 0.9;
int ratio = (int)(mapEntry.getRatio() * scaleFactor);
double scaleFactor = deltaY > 0 ? 0.9 : 1.1;
int ratio = (int) (mapEntry.getRatio() * scaleFactor);
mapEntry.setRatio(ratio);
initCanvas(mapEntry);
event.consume();
Expand Down Expand Up @@ -108,44 +127,36 @@ private void initCanvas(MapEntry mapEntry) {
gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());
// Set up scrolling

if (drawAllRouts) {
drawAllRoutes(gc, mapEntry);
} else {
drawSingleRoute(gc, mapEntry);
}
drawRoutes(mapEntry, gc);
}

private void initDrawAllCheckBox(MapEntry mapEntry) {
showAll.selectedProperty().addListener((observable, oldValue, newValue) -> {
GraphicsContext gc = canvas.getGraphicsContext2D();
if (newValue) {
drawAllRouts = true;
drawAllRoutes(gc, mapEntry);
} else {
drawAllRouts = false;
drawSingleRoute(gc, mapEntry);
}
drawAllRouts = newValue;
drawRoutes(mapEntry, gc);
});
}

private void drawRoutes(MapEntry mapEntry, GraphicsContext gc) {
if (drawAllRouts) {
drawAllRoutes(gc, mapEntry);
} else {
drawSingleRoute(gc, mapEntry);
}
}

private void drawAllRoutes(GraphicsContext gc, MapEntry mapEntry) {
int groupId = routeService.getGroupId();
Map<Integer, LuaValue> friendlyGroupRoutes = routeService.getFriendlyGroupRoutes(missionService.getMission());
Map<Integer, List<RouteDetails>> routes = friendlyGroupRoutes.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, r -> RouteDetails.fromLuaRoute(r.getValue().checktable())));
RouteCanvasUtil.drawRoute(gc, mapEntry, routes, groupId);
RouteCanvasUtil.drawRoute(gc, mapEntry, allGroupRoutes, groupSelect.getValue());
}

private void drawSingleRoute(GraphicsContext gc, MapEntry mapEntry) {
List<RouteDetails> route = RouteDetails.fromLuaRoute(routeService.getRoute().get("points").checktable());
int groupId = routeService.getGroupId();
int groupId = groupSelect.getValue();
List<RouteDetails> route = allGroupRoutes.get(groupId);
RouteCanvasUtil.drawRoute(gc, mapEntry, Map.of(groupId, route), groupId);
}

private void initRouteTable(MapEntry mapEntry) {
List<RouteDetails> route = RouteDetails.fromLuaRoute(routeService.getRoute().get("points").checktable());
ObservableList<RouteDetails> routes = FXCollections.observableArrayList(route);

TableColumn<RouteDetails, Integer> idColumn = new TableColumn<>("ID");
idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
idColumn.setEditable(false);
Expand Down Expand Up @@ -193,7 +204,7 @@ private void initRouteTable(MapEntry mapEntry) {
dataTable.getColumns().addAll(idColumn, xColumn, yColumn, altColumn, speedColumn, etaColumn, typeColumn, taskColumn);
dataTable.setEditable(true);

dataTable.setItems(routes);
setRouteDetails(mapEntry);
idColumn.setSortType(TableColumn.SortType.ASCENDING);
dataTable.getSortOrder().add(idColumn);
dataTable.sort();
Expand All @@ -211,6 +222,14 @@ private void initRouteTable(MapEntry mapEntry) {
});
}

private void setRouteDetails(MapEntry mapEntry) {
List<RouteDetails> route = allGroupRoutes.get(groupSelect.getValue());
ObservableList<RouteDetails> routes = FXCollections.observableArrayList(route);
dataTable.getItems().clear();
dataTable.setItems(routes);
drawRoutes(mapEntry, canvas.getGraphicsContext2D());
}

public void saveChanges() {
routeService.updateRoute(dataTable.getItems());
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/org/faulty/wpreplace/ui/RouteDetails.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
<SplitPane xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.faulty.wpreplace.ui.RouteDetailsController">
<VBox spacing="10">
<Label text="Route Points" textAlignment="CENTER" maxHeight="20"/>
<HBox maxHeight="20" spacing="200">
<HBox alignment="CENTER_LEFT">
<Label text="Route Points"/>
</HBox>
<HBox alignment="CENTER_RIGHT" spacing="20">
<Label text="Group"/>
<ComboBox fx:id="groupSelect" layoutX="50" layoutY="50"/>
</HBox>
</HBox>
<SplitPane VBox.vgrow="ALWAYS" orientation="VERTICAL">

<TableView fx:id="dataTable" prefWidth="400.0"/>
Expand Down

0 comments on commit 49aa330

Please sign in to comment.