move current route to profile model. Autochange current entry if change starsystem/station
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package ru.trader.controllers;
|
||||
|
||||
import javafx.beans.property.BooleanProperty;
|
||||
import javafx.beans.property.IntegerProperty;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
import javafx.beans.property.SimpleIntegerProperty;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
@@ -12,11 +11,9 @@ import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.stage.Stage;
|
||||
import ru.trader.KeyBinding;
|
||||
import ru.trader.model.MissionModel;
|
||||
import ru.trader.model.OrderModel;
|
||||
import ru.trader.model.RouteEntryModel;
|
||||
import ru.trader.model.RouteModel;
|
||||
import ru.trader.model.*;
|
||||
import ru.trader.view.support.ViewUtils;
|
||||
import ru.trader.view.support.cells.OfferListCell;
|
||||
import ru.trader.view.support.cells.OrderListCell;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -39,28 +36,29 @@ public class HelperController {
|
||||
private ListView<OrderModel> sellOrders;
|
||||
@FXML
|
||||
private ListView<MissionModel> missions;
|
||||
@FXML
|
||||
private ListView<StationModel> stations;
|
||||
@FXML
|
||||
private ListView<OfferModel> sellOffers;
|
||||
|
||||
private Stage stage;
|
||||
private RouteModel route;
|
||||
private final BooleanProperty docked;
|
||||
private final IntegerProperty currentEntry;
|
||||
|
||||
public HelperController() {
|
||||
currentEntry = new SimpleIntegerProperty(-1);
|
||||
docked = new SimpleBooleanProperty(true);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void initialize(){
|
||||
currentEntry.addListener(routeEntryListener);
|
||||
MainController.getProfile().routeProperty().addListener(routeListener);
|
||||
buyOrders.setCellFactory(new OrderListCell(false));
|
||||
sellOrders.setCellFactory(new OrderListCell(true));
|
||||
sellOffers.setCellFactory(new OfferListCell(true));
|
||||
bindKeys();
|
||||
}
|
||||
|
||||
public void show(Parent content, RouteModel route) {
|
||||
this.route = route;
|
||||
currentEntry.setValue(0);
|
||||
public void show(Parent content) {
|
||||
if (stage == null){
|
||||
stage = new Stage();
|
||||
stage.setScene(new Scene(content));
|
||||
@@ -76,6 +74,15 @@ public class HelperController {
|
||||
}
|
||||
}
|
||||
|
||||
private void setRoute(RouteModel route){
|
||||
if (this.route != null){
|
||||
this.route.currentEntryProperty().removeListener(currentEntryListener);
|
||||
}
|
||||
this.route = route;
|
||||
setRouteEntry(route.getCurrentEntry());
|
||||
this.route.currentEntryProperty().addListener(currentEntryListener);
|
||||
}
|
||||
|
||||
private void setRouteEntry(int index){
|
||||
RouteEntryModel entry = route.get(index);
|
||||
station.setText(entry.getStation().getName());
|
||||
@@ -85,13 +92,19 @@ public class HelperController {
|
||||
buyOrders.setItems(entry.orders());
|
||||
sellOrders.setItems(entry.sellOrders());
|
||||
missions.setItems(entry.missions());
|
||||
stations.setItems(FXCollections.observableArrayList(route.getStations(index)));
|
||||
sellOffers.setItems(FXCollections.observableArrayList(route.getSellOffers(index)));
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void next(){
|
||||
int index = currentEntry.get();
|
||||
int index = route.getCurrentEntry();
|
||||
if (index < route.getJumps() - 1){
|
||||
currentEntry.setValue(index+1);
|
||||
route.setCurrentEntry(index+1);
|
||||
} else {
|
||||
if (route.isLoop()){
|
||||
route.setCurrentEntry(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,16 +115,19 @@ public class HelperController {
|
||||
|
||||
@FXML
|
||||
private void previous(){
|
||||
int index = currentEntry.get();
|
||||
int index = route.getCurrentEntry();
|
||||
if (index > 0){
|
||||
currentEntry.setValue(index-1);
|
||||
route.setCurrentEntry(index-1);
|
||||
}
|
||||
}
|
||||
|
||||
private final ChangeListener<Number> routeEntryListener = (ov, o, n) -> ViewUtils.doFX(() -> setRouteEntry(n.intValue()));
|
||||
|
||||
private void bindKeys(){
|
||||
KeyBinding.bind(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD4, KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK), k -> ViewUtils.doFX(this::previous));
|
||||
KeyBinding.bind(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD6, KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK), k -> ViewUtils.doFX(this::next));
|
||||
}
|
||||
|
||||
private final ChangeListener<? super Number> currentEntryListener = (ov, o, n) -> ViewUtils.doFX(() -> setRouteEntry(n.intValue()));
|
||||
private final ChangeListener<RouteModel> routeListener = (ov, o, n) -> ViewUtils.doFX(() -> setRoute(n));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -57,43 +57,32 @@ public class ProfileController {
|
||||
initListeners();
|
||||
}
|
||||
|
||||
private void initListeners(){
|
||||
name.textProperty().addListener((ov, o, n) -> {
|
||||
if (!ignoreChanges)
|
||||
profile.setName(n);
|
||||
});
|
||||
balance.numberProperty().addListener((ov, o, n) -> {
|
||||
if (!ignoreChanges)
|
||||
profile.setBalance(n.doubleValue());
|
||||
});
|
||||
system.completionProperty().addListener((ov, o , n) -> {
|
||||
if (!ignoreChanges){
|
||||
private void consumeChanges(Runnable runnable){
|
||||
if (ignoreChanges) return;
|
||||
ignoreChanges = true;
|
||||
profile.setSystem(n);
|
||||
}
|
||||
station.setItems(n.getStationsList());
|
||||
ViewUtils.doFX(runnable);
|
||||
ignoreChanges = false;
|
||||
}
|
||||
|
||||
private void doAndConsumeChanges(Runnable runnable){
|
||||
boolean old = ignoreChanges;
|
||||
ignoreChanges = true;
|
||||
ViewUtils.doFX(runnable);
|
||||
ignoreChanges = old;
|
||||
}
|
||||
|
||||
private void initListeners(){
|
||||
name.textProperty().addListener((ov, o, n) -> consumeChanges(() -> profile.setName(n)));
|
||||
balance.numberProperty().addListener((ov, o, n) -> consumeChanges(() -> profile.setBalance(n.doubleValue())));
|
||||
system.completionProperty().addListener((ov, o , n) -> {
|
||||
doAndConsumeChanges(() -> station.setItems(n.getStationsList()));
|
||||
consumeChanges(() -> {profile.setSystem(n); profile.setStation(ModelFabric.NONE_STATION);});
|
||||
});
|
||||
station.valueProperty().addListener((ov, o, n) -> {
|
||||
if (!ignoreChanges)
|
||||
profile.setStation(n);
|
||||
});
|
||||
mass.numberProperty().addListener((ov, o, n) -> {
|
||||
if (!ignoreChanges)
|
||||
profile.setShipMass(n.doubleValue());
|
||||
});
|
||||
tank.numberProperty().addListener((ov, o, n) -> {
|
||||
if (!ignoreChanges)
|
||||
profile.setShipTank(n.doubleValue());
|
||||
});
|
||||
cargo.numberProperty().addListener((ov, o, n) -> {
|
||||
if (!ignoreChanges)
|
||||
profile.setShipCargo(n.intValue());
|
||||
});
|
||||
engine.valueProperty().addListener((ov, o, n) -> {
|
||||
if (!ignoreChanges)
|
||||
profile.setShipEngine(n);
|
||||
});
|
||||
station.valueProperty().addListener((ov, o, n) -> consumeChanges(() -> profile.setStation(n)));
|
||||
mass.numberProperty().addListener((ov, o, n) -> consumeChanges(() -> profile.setShipMass(n.doubleValue())));
|
||||
tank.numberProperty().addListener((ov, o, n) -> consumeChanges(() -> profile.setShipTank(n.doubleValue())));
|
||||
cargo.numberProperty().addListener((ov, o, n) -> consumeChanges(() -> profile.setShipCargo(n.intValue())));
|
||||
engine.valueProperty().addListener((ov, o, n) -> consumeChanges(() -> profile.setShipEngine(n)));
|
||||
}
|
||||
|
||||
public void setProfile(ProfileModel profile){
|
||||
@@ -101,7 +90,6 @@ public class ProfileController {
|
||||
unbind();
|
||||
}
|
||||
this.profile = profile;
|
||||
ignoreChanges = true;
|
||||
name.setText(profile.getName());
|
||||
balance.setValue(profile.getBalance());
|
||||
system.setValue(profile.getSystem());
|
||||
@@ -111,7 +99,6 @@ public class ProfileController {
|
||||
cargo.setValue(profile.getShipCargo());
|
||||
engine.setValue(profile.getShipEngine());
|
||||
bind();
|
||||
ignoreChanges = false;
|
||||
}
|
||||
|
||||
private void bind(){
|
||||
@@ -137,17 +124,14 @@ public class ProfileController {
|
||||
}
|
||||
|
||||
|
||||
private final ChangeListener<String> nameListener = (ov, o, n) -> ViewUtils.doFX(() -> name.setText(n));
|
||||
private final ChangeListener<Number> balanceListener = (ov, o, n) -> ViewUtils.doFX(() -> balance.setValue(n));
|
||||
private final ChangeListener<SystemModel> systemListener = (ov, o, n) -> {
|
||||
if (!ignoreChanges)
|
||||
ViewUtils.doFX(() -> system.setValue(n));
|
||||
};
|
||||
private final ChangeListener<StationModel> stationListener = (ov, o, n) -> ViewUtils.doFX(() -> station.setValue(n));
|
||||
private final ChangeListener<Number> massListener = (ov, o, n) -> ViewUtils.doFX(() -> mass.setValue(n));
|
||||
private final ChangeListener<Number> tankListener = (ov, o, n) -> ViewUtils.doFX(() -> tank.setValue(n));
|
||||
private final ChangeListener<Number> cargoListener = (ov, o, n) -> ViewUtils.doFX(() -> cargo.setValue(n));
|
||||
private final ChangeListener<Engine> engineListener = (ov, o, n) -> ViewUtils.doFX(() -> engine.setValue(n));
|
||||
private final ChangeListener<String> nameListener = (ov, o, n) -> consumeChanges(() -> name.setText(n));
|
||||
private final ChangeListener<Number> balanceListener = (ov, o, n) -> consumeChanges(() -> balance.setValue(n));
|
||||
private final ChangeListener<SystemModel> systemListener = (ov, o, n) -> consumeChanges(() -> system.setValue(n));
|
||||
private final ChangeListener<StationModel> stationListener = (ov, o, n) -> consumeChanges(() -> station.setValue(n));
|
||||
private final ChangeListener<Number> massListener = (ov, o, n) -> consumeChanges(() -> mass.setValue(n));
|
||||
private final ChangeListener<Number> tankListener = (ov, o, n) -> consumeChanges(() -> tank.setValue(n));
|
||||
private final ChangeListener<Number> cargoListener = (ov, o, n) -> consumeChanges(() -> cargo.setValue(n));
|
||||
private final ChangeListener<Engine> engineListener = (ov, o, n) -> consumeChanges(() -> engine.setValue(n));
|
||||
|
||||
private class EngineStringConverter extends StringConverter<Engine> {
|
||||
@Override
|
||||
|
||||
@@ -83,7 +83,8 @@ public class RouteSearchController {
|
||||
if (path.isPresent()){
|
||||
RouteModel route = path.get();
|
||||
route.addAll(0, missionsList.getItems());
|
||||
Screeners.showHelper(route);
|
||||
profile.setRoute(route);
|
||||
Screeners.showHelper();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -275,7 +275,8 @@ public class RouterController {
|
||||
if (path.isPresent()){
|
||||
orders.addAll(path.get().getOrders());
|
||||
addRouteToPath(path.get());
|
||||
Screeners.showHelper(path.get());
|
||||
MainController.getProfile().setRoute(path.get());
|
||||
Screeners.showHelper();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -247,8 +247,8 @@ public class Screeners {
|
||||
return dialog.showAndWait();
|
||||
}
|
||||
|
||||
public static void showHelper(RouteModel route){
|
||||
helperController.show(helperScreen, route);
|
||||
public static void showHelper(){
|
||||
helperController.show(helperScreen);
|
||||
}
|
||||
|
||||
public static void reinitAll() {
|
||||
|
||||
@@ -21,6 +21,7 @@ public class ProfileModel {
|
||||
private final DoubleProperty shipTank;
|
||||
private final IntegerProperty shipCargo;
|
||||
private final ObjectProperty<Engine> shipEngine;
|
||||
private final ObjectProperty<RouteModel> route;
|
||||
|
||||
public ProfileModel(Profile profile, MarketModel market) {
|
||||
this.market = market;
|
||||
@@ -34,6 +35,7 @@ public class ProfileModel {
|
||||
shipTank = new SimpleDoubleProperty();
|
||||
shipCargo = new SimpleIntegerProperty();
|
||||
shipEngine = new SimpleObjectProperty<>();
|
||||
route = new SimpleObjectProperty<>();
|
||||
refresh();
|
||||
initListeners();
|
||||
}
|
||||
@@ -50,10 +52,12 @@ public class ProfileModel {
|
||||
system.addListener((ov, o, n) -> {
|
||||
LOG.debug("Change system, old: {}, new: {}", o, n);
|
||||
profile.setSystem(n != null && n != ModelFabric.NONE_SYSTEM ? n.getSystem() : null);
|
||||
if (route.getValue() != null) {getRoute().updateCurrentEntry(n, null);}
|
||||
});
|
||||
station.addListener((ov, o, n) -> {
|
||||
LOG.debug("Change station, old: {}, new: {}", o, n);
|
||||
profile.setStation(n != null && n != ModelFabric.NONE_STATION ? n.getStation() : null);
|
||||
if (route.getValue() != null) {getRoute().updateCurrentEntry(getSystem(), n);}
|
||||
});
|
||||
docked.addListener((ov, o, n) -> {
|
||||
LOG.debug("Change docked, old: {}, new: {}", o, n);
|
||||
@@ -195,6 +199,18 @@ public class ProfileModel {
|
||||
this.shipEngine.set(engine);
|
||||
}
|
||||
|
||||
public RouteModel getRoute() {
|
||||
return route.get();
|
||||
}
|
||||
|
||||
public ObjectProperty<RouteModel> routeProperty() {
|
||||
return route;
|
||||
}
|
||||
|
||||
public void setRoute(RouteModel route) {
|
||||
this.route.set(route);
|
||||
}
|
||||
|
||||
private void refresh(){
|
||||
name.setValue(profile.getName());
|
||||
balance.setValue(profile.getBalance());
|
||||
|
||||
@@ -70,6 +70,10 @@ public class RouteEntryModel {
|
||||
return entry.getRefill();
|
||||
}
|
||||
|
||||
public boolean isTransit(){
|
||||
return entry.isTransit();
|
||||
}
|
||||
|
||||
public ObservableList<OrderModel> orders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package ru.trader.model;
|
||||
|
||||
import javafx.beans.property.DoubleProperty;
|
||||
import javafx.beans.property.ReadOnlyDoubleProperty;
|
||||
import javafx.beans.property.SimpleDoubleProperty;
|
||||
import javafx.beans.property.*;
|
||||
import ru.trader.analysis.Route;
|
||||
import ru.trader.analysis.RouteEntry;
|
||||
import ru.trader.analysis.RouteFiller;
|
||||
@@ -11,9 +9,7 @@ import ru.trader.core.Offer;
|
||||
import ru.trader.core.Order;
|
||||
import ru.trader.model.support.BindingsHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RouteModel {
|
||||
@@ -22,6 +18,7 @@ public class RouteModel {
|
||||
private final DoubleProperty profit;
|
||||
private final DoubleProperty profitByTime;
|
||||
private final List<RouteEntryModel> entries;
|
||||
private final IntegerProperty currentEntry;
|
||||
|
||||
RouteModel(Route route, MarketModel market) {
|
||||
this.market = market;
|
||||
@@ -32,6 +29,7 @@ public class RouteModel {
|
||||
profitByTime = new SimpleDoubleProperty();
|
||||
profitByTime.bind(profit.divide(_route.getTime()));
|
||||
fillSellOrders();
|
||||
currentEntry = new SimpleIntegerProperty(0);
|
||||
}
|
||||
|
||||
private void fillSellOrders(){
|
||||
@@ -73,6 +71,10 @@ public class RouteModel {
|
||||
return _route;
|
||||
}
|
||||
|
||||
public boolean isLoop(){
|
||||
return _route.isLoop();
|
||||
}
|
||||
|
||||
public int getLands() {
|
||||
return _route.getLands();
|
||||
}
|
||||
@@ -187,4 +189,69 @@ public class RouteModel {
|
||||
fillSellOrders();
|
||||
}
|
||||
|
||||
public Collection<StationModel> getStations(int offset){
|
||||
Collection<StationModel> res = new HashSet<>();
|
||||
int startIndex = _route.isLoop() ? 1 : offset+1;
|
||||
if (startIndex >= entries.size()) return res;
|
||||
entries.subList(startIndex, entries.size()).stream().map(RouteEntryModel::getStation)
|
||||
.filter(station -> station != ModelFabric.NONE_STATION)
|
||||
.forEach(res::add);
|
||||
return res;
|
||||
}
|
||||
|
||||
public Collection<OfferModel> getSellOffers(int offset){
|
||||
Map<ItemModel, OfferModel> res = new HashMap<>();
|
||||
for (StationModel station : getStations(offset)) {
|
||||
for (OfferModel offer : station.getSells()) {
|
||||
if (offer.getItem().isMarketItem()){
|
||||
OfferModel old = res.get(offer.getItem());
|
||||
if (old == null || old.getPrice() > offer.getPrice()){
|
||||
res.put(offer.getItem(), offer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res.values();
|
||||
}
|
||||
|
||||
public int getCurrentEntry() {
|
||||
return currentEntry.get();
|
||||
}
|
||||
|
||||
public IntegerProperty currentEntryProperty() {
|
||||
return currentEntry;
|
||||
}
|
||||
|
||||
public void setCurrentEntry(int currentEntry) {
|
||||
this.currentEntry.set(currentEntry);
|
||||
}
|
||||
|
||||
public void updateCurrentEntry(SystemModel system, StationModel station) {
|
||||
for (int i = getCurrentEntry()+1; i < entries.size(); i++) {
|
||||
RouteEntryModel entry = entries.get(i);
|
||||
if (system.equals(entry.getStation().getSystem())
|
||||
&& (station == null || station == ModelFabric.NONE_STATION ||
|
||||
station.equals(entry.getStation()))
|
||||
)
|
||||
{
|
||||
setCurrentEntry(i);
|
||||
return;
|
||||
}
|
||||
if (!entry.isTransit()) return;
|
||||
}
|
||||
if (isLoop()){
|
||||
for (int i = 0; i < getCurrentEntry()-1; i++) {
|
||||
RouteEntryModel entry = entries.get(i);
|
||||
if (system.equals(entry.getStation().getSystem())
|
||||
&& (station == null || station == ModelFabric.NONE_STATION ||
|
||||
station.equals(entry.getStation()))
|
||||
)
|
||||
{
|
||||
setCurrentEntry(i);
|
||||
return;
|
||||
}
|
||||
if (!entry.isTransit()) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,15 @@ import ru.trader.model.OfferModel;
|
||||
import ru.trader.model.support.ModelBindings;
|
||||
|
||||
public class OfferListCell implements Callback<ListView<OfferModel>, ListCell<OfferModel>> {
|
||||
private final boolean asItem;
|
||||
|
||||
public OfferListCell(){
|
||||
this(false);
|
||||
}
|
||||
|
||||
public OfferListCell(boolean toItemString){
|
||||
asItem = toItemString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListCell<OfferModel> call(ListView<OfferModel> param){
|
||||
@@ -19,7 +28,7 @@ public class OfferListCell implements Callback<ListView<OfferModel>, ListCell<Of
|
||||
if (!empty){
|
||||
if (o != offer){
|
||||
textProperty().unbind();
|
||||
textProperty().bind(ModelBindings.asString(offer));
|
||||
textProperty().bind(asItem ? ModelBindings.asItemString(offer) : ModelBindings.asString(offer));
|
||||
o = offer;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<HBox><Label text="Станция:" /><Label fx:id="station" /></HBox>
|
||||
<HBox><Label text="Время:" /><Label fx:id="time" /></HBox>
|
||||
<HBox><Label text="Заправить:" /><Label fx:id="refuel" /></HBox>
|
||||
<HBox maxHeight="200">
|
||||
<HBox maxHeight="100">
|
||||
<VBox>
|
||||
<Label text="Продать:" />
|
||||
<ListView fx:id="sellOrders"/>
|
||||
@@ -24,10 +24,20 @@
|
||||
<ListView fx:id="buyOrders"/>
|
||||
</VBox>
|
||||
</HBox>
|
||||
<VBox>
|
||||
<VBox maxHeight="100">
|
||||
<Label text="Сдать миссии:" />
|
||||
<ListView fx:id="missions"/>
|
||||
</VBox>
|
||||
<HBox>
|
||||
<VBox maxHeight="100">
|
||||
<Label text="Станции:" />
|
||||
<ListView fx:id="stations"/>
|
||||
</VBox>
|
||||
<VBox maxHeight="100">
|
||||
<Label text="Товары:" />
|
||||
<ListView fx:id="sellOffers"/>
|
||||
</VBox>
|
||||
</HBox>
|
||||
<HBox>
|
||||
<Button prefWidth="30" onAction="#previous">
|
||||
<graphic><Glyph text="FontAwesome|ARROW_LEFT"/></graphic>
|
||||
|
||||
Reference in New Issue
Block a user