diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBusConfig.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBusConfig.java new file mode 100644 index 0000000..06fbd20 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBusConfig.java @@ -0,0 +1,187 @@ +package com.example.doubledeckerbus; + +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; +import javafx.scene.control.Label; +import javafx.scene.control.Spinner; +import javafx.scene.input.*; +import javafx.scene.layout.Background; +import javafx.scene.layout.Region; +import javafx.scene.paint.Color; +import javafx.scene.paint.Paint; +import javafx.stage.Stage; + +import java.util.function.Consumer; + +public class ControllerBusConfig { + DataFormat colorFormat = DataFormat.lookupMimeType("SerializableColor"); + DataFormat additionalElementFormat = DataFormat.lookupMimeType("IDrawningAdditionalElement"); + private Stage _stage; + private DrawingBus _bus = null; + + private Event addBus; + + public void SetStage(Stage stage) + { + _stage = stage; + } + + public void AddEvent(Consumer ev) + { + addBus.AddListener(ev); + } + + private void DrawBus() + { + GraphicsContext gc = canvasObject.getGraphicsContext2D(); + gc.clearRect(0, 0, canvasObject.getWidth(), canvasObject.getHeight()); + if (_bus == null) + { + return; + } + _bus.SetPosition(5, 5, (int)canvasObject.getWidth(), (int)canvasObject.getHeight()); + _bus.DrawTransport(gc); + } + + @FXML + private Canvas canvasObject; + + @FXML + private CheckBox checkBoxSecondStage; + + @FXML + private CheckBox checkBoxLadder; + + @FXML + private Spinner spinnerSpeed = new Spinner<>(); + + @FXML + private Spinner spinnerDoors = new Spinner<>(); + + @FXML + private Spinner spinnerWeight = new Spinner<>(); + + @FXML + public void initialize() + { + if (colorFormat == null) + { + colorFormat = new DataFormat("SerializableColor"); + } + if (additionalElementFormat == null) + { + additionalElementFormat = new DataFormat("IDrawningAdditionalElement"); + } + addBus = new Event<>(); + } + + @FXML + void ButtonOk_Click(ActionEvent event) { + if (_bus != null) + { + addBus.Broadcast(_bus); + } + if (_stage != null) + { + _stage.close(); + } + } + + @FXML + void CanvasObject_OnDragDropped(DragEvent event) { + Dragboard db = event.getDragboard(); + switch (db.getString()) + { + case "labelSimpleObject" -> _bus = new DrawingBus(spinnerSpeed.getValue(), + spinnerWeight.getValue(), Color.WHITE, spinnerDoors.getValue()); + case "labelModifiedObject" -> _bus = new DrawingDDB(spinnerSpeed.getValue(), + spinnerWeight.getValue(), Color.WHITE, spinnerDoors.getValue(), Color.BLACK, + checkBoxSecondStage.isSelected(), checkBoxLadder.isSelected()); + case "labelTriangle" -> _bus.ChangeDoor(new DrawingTriangleDoors()); + case "labelOval" -> _bus.ChangeDoor(new DrawingEllipsoidDoors()); + case "labelRect" -> _bus.ChangeDoor(new DrawingDoors()); + } + DrawBus(); + event.consume(); + + } + + @FXML + void CanvasObject_OnDragOver(DragEvent event) { + if (event.getDragboard().hasString()) + { + event.acceptTransferModes(TransferMode.COPY_OR_MOVE); + } + event.consume(); + } + + @FXML + void LabelBaseColor_OnDragDropped(DragEvent event) { + if (_bus == null) + { + return; + } + + Dragboard db = event.getDragboard(); + + _bus.Bus.BodyColor = ((SerializableColor)(db.getContent(colorFormat))).getFXColor(); + event.consume(); + DrawBus(); + } + + @FXML + void LabelColor_OnDragOver(DragEvent event) { + if (event.getDragboard().hasContent(colorFormat)) + { + event.acceptTransferModes(TransferMode.COPY_OR_MOVE); + } + event.consume(); + } + + @FXML + void LabelDopColor_OnDragDropped(DragEvent event) { + if (_bus == null || !(_bus.Bus instanceof EntityDDB ddb)) + { + return; + } + + Dragboard db = event.getDragboard(); + + ddb.ExtraColor = ((SerializableColor)(db.getContent(colorFormat))).getFXColor(); + event.consume(); + DrawBus(); + } + + @FXML + void LabelObject_OnDragDetected(MouseEvent event) { + Label labelObject = (Label)(event.getSource()); + Dragboard db = labelObject.startDragAndDrop(TransferMode.ANY); + + ClipboardContent content = new ClipboardContent(); + content.putString(((Label)(event.getSource())).getId()); + db.setContent(content); + + event.consume(); + } + + @FXML + void RegionColor_OnDragDetected(MouseEvent event) { + Region region = (Region)(event.getSource()); + Dragboard db = region.startDragAndDrop(TransferMode.ANY); + + Background regionBackground = region.getBackground(); + Paint regionPaint = regionBackground.getFills().get(0).getFill(); + if (regionPaint instanceof Color) + { + ClipboardContent content = new ClipboardContent(); + content.put(colorFormat, new SerializableColor((Color)regionPaint)); + db.setContent(content); + } + event.consume(); + } + +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java index caa7a48..dbd5737 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java @@ -1,6 +1,4 @@ package com.example.doubledeckerbus; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; @@ -12,6 +10,7 @@ import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.*; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; +import javafx.stage.Stage; import java.io.IOException; import java.util.HashMap; @@ -21,7 +20,7 @@ import java.util.Optional; public class ControllerMapWithSetBus { static MapWithSetBusesGeneric _mapBusesCollectionGeneric; - private HashMap _mapsDict = new HashMap<>(); + private final HashMap _mapsDict = new HashMap<>(); { _mapsDict.put("Простая карта", new SimpleMap()); _mapsDict.put("Водная карта", new WaterMap()); @@ -38,30 +37,6 @@ public class ControllerMapWithSetBus { static String map_name = "Простая карта"; protected ObservableList countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта"); - @FXML - private Button buttonAddBus; - - @FXML - private Button buttonDown; - - @FXML - private Button buttonLeft; - - @FXML - private Button buttonRemoveCar; - - @FXML - private Button buttonRight; - - @FXML - private Button buttonShowMap; - - @FXML - private Button buttonShowStorage; - - @FXML - private Button buttonUp; - @FXML private Canvas canvasBus; @@ -84,26 +59,23 @@ public class ControllerMapWithSetBus { private void initialize(){ gc = canvasBus.getGraphicsContext2D(); if (selected != null) { - showStorage(); + ShowStorage(); } if (_mapsCollection == null) _mapsCollection = new MapsCollection((int) canvasBus.getWidth(), (int) canvasBus.getHeight()); comboBoxSelectorMap.setItems(countOfMap); comboBoxSelectorMap.setValue(map_name); listViewMaps.getSelectionModel().selectedItemProperty() - .addListener(new ChangeListener() { - @Override - public void changed(ObservableValue observableValue, String s, String t1) { - selected = t1; - showStorage(); - } + .addListener((observableValue, s, t1) -> { + selected = t1; + ShowStorage(); }); listViewMaps.setItems(_mapsCollection.toObserveList()); } GraphicsContext gc; private void FirstIncome() { - if (comboBoxSelectorMap.getValue() != map_name) { + if (!Objects.equals(comboBoxSelectorMap.getValue(), map_name)) { map_name = comboBoxSelectorMap.getValue(); switch (map_name) { case "Простая карта" -> map = new SimpleMap(); @@ -118,17 +90,14 @@ public class ControllerMapWithSetBus { ObservableList listMaps = FXCollections.observableArrayList(); - for (int i = 0; i < _mapsCollection.Keys().size(); i++) - { - listMaps.add(_mapsCollection.Keys().get(i)); - } + listMaps.addAll(_mapsCollection.Keys()); listViewMaps.setItems(listMaps); if (listMaps.size() > 0 && (index == -1 || index >= listMaps.size())) { listViewMaps.getSelectionModel().select(0); } - else if (listMaps.size() > 0 && index > -1 && index < listMaps.size()) + else if (listMaps.size() > 0 && index > -1) { listViewMaps.getSelectionModel().select(index); } @@ -136,14 +105,47 @@ public class ControllerMapWithSetBus { @FXML private void ButtonAddBus_Click(ActionEvent event) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml")); - Scene scene = new Scene(fxmlLoader.load()); - Form.myStage.setTitle("DoubleDeckerBus"); - Form.myStage.setScene(scene); - Form.myStage.show(); + if (listViewMaps.getSelectionModel().selectedItemProperty().isNull().get()) + { + return; + } + Stage stageBus = new Stage(); + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBusConfig.fxml")); + Scene sceneBus = new Scene(fxmlLoader.load()); + + stageBus.setScene(sceneBus); + stageBus.show(); + + ControllerBusConfig controller = fxmlLoader.getController(); + controller.AddEvent(this::AddBus); + controller.SetStage(stageBus); FirstIncome(); } + + private void AddBus(DrawingBus bus) { + if (listViewMaps.getSelectionModel().selectedItemProperty().isNull().get()) + { + return; + } + DrawingObjectBus objectBus = new DrawingObjectBus(bus); + String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem(); + + Alert alert; + if (selectedMapName != null && selectedMapName.length() != 0 && _mapsCollection.get(selectedMapName).add(objectBus) != -1) + { + alert = new Alert(Alert.AlertType.INFORMATION, "Объект добавлен", ButtonType.OK); + _mapsCollection.get(selectedMapName).ShowSet(gc); + ShowStorage(); + } + else + { + alert = new Alert(Alert.AlertType.ERROR, "Не удалось добавить объект", ButtonType.OK); + } + alert.showAndWait(); + + } + @FXML private void ButtonAddMap_Click(ActionEvent event) { @@ -165,7 +167,7 @@ public class ControllerMapWithSetBus { } _mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(comboBoxSelectorMap.getValue())); ReloadMaps(); - showStorage(); + ShowStorage(); } @FXML private void ButtonDeleteMap_Click(ActionEvent event) @@ -176,7 +178,7 @@ public class ControllerMapWithSetBus { } _mapsCollection.DelMap(listViewMaps.getSelectionModel().getSelectedItem()); ReloadMaps(); - showStorage(); + ShowStorage(); } @FXML @@ -236,19 +238,23 @@ public class ControllerMapWithSetBus { alert.setContentText("Не удалось удалить объект"); option = alert.showAndWait(); } - showStorage(); + ShowStorage(); } @FXML private void ButtonShowStorage_Click(ActionEvent event) { FirstIncome(); - showStorage(); + ShowStorage(); } @FXML private void ButtonShowOnMap_Click(ActionEvent event) { FirstIncome(); + ShowMap(); + } + + private void ShowMap() { if (selected == null) { return; } @@ -276,13 +282,13 @@ public class ControllerMapWithSetBus { _mapsCollection.GetId(selected).MoveObject(dir); } - private void showStorage() { + private void ShowStorage() { if (selected == null) { return; } gc.setFill(Color.WHITE); - gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); + gc.clearRect(0, 0, canvasBus.getWidth(), canvasBus.getHeight()); _mapsCollection.GetId(selected).ShowSet(gc); } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java index d5f3158..8b96671 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java @@ -9,10 +9,10 @@ public class DrawingBus { public EntityBus Bus; public IDrawingDoors Doors; - - public EntityBus getBus() { - return Bus; - } + int _speed; + float _weight; + Color _bodyColor; + int _countOfDoors = 3; private static final int _null = -1000; protected float _startPosX; @@ -23,15 +23,31 @@ public class DrawingBus { private static final int _busHeight = 50; public DrawingBus(int speed, float weight, Color bodyColor, int countOfDoors) { - Bus = new EntityBus(speed, weight, bodyColor); + Bus = CreateBus(speed, weight, bodyColor, countOfDoors); + } + + private EntityBus CreateBus(int speed, float weight, Color bodyColor, int countOfDoors) { + _speed = speed; + _weight = weight; + _bodyColor = bodyColor; + _countOfDoors = countOfDoors; + EntityBus bus = new EntityBus(speed, weight, bodyColor); switch (new Random().nextInt(3)) { case 0 -> Doors = new DrawingTriangleDoors(); case 1 -> Doors = new DrawingDoors(); case 2 -> Doors = new DrawingEllipsoidDoors(); } Doors.setCountOfDoors(countOfDoors); + return bus; } + + public void ChangeDoor(IDrawingDoors door) { + Doors = door; + Doors.setCountOfDoors(_countOfDoors); + } + + public DrawingBus(int speed, float weight, Color bodyColor) { Bus = new EntityBus(speed, weight, bodyColor); } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java index 0c76b40..8884246 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java @@ -4,9 +4,26 @@ import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; public class DrawingDDB extends DrawingBus{ - public DrawingDDB(int speed, float weight, Color bodyColor, int countOfDoors, Color extraColor, boolean ladder, boolean secondStage) { + Color _extraColor; + boolean _ladder; + boolean _secondStage; + + public DrawingDDB(int speed, float weight, Color bodyColor, int countOfDoors, + Color extraColor, boolean ladder, boolean secondStage) { super(speed, weight, bodyColor, countOfDoors); - Bus = new EntityDDB(speed, weight, bodyColor, extraColor, ladder, secondStage); + Bus = CreateDDB(speed, weight, bodyColor, countOfDoors, extraColor, ladder, secondStage); + } + + private EntityDDB CreateDDB(int speed, float weight, Color bodyColor, int countOfDoors, + Color extraColor, boolean ladder, boolean secondStage) { + _speed = speed; + _weight = weight; + _bodyColor = bodyColor; + _countOfDoors = countOfDoors; + _extraColor = extraColor; + _ladder = ladder; + _secondStage = secondStage; + return new EntityDDB(speed, weight, bodyColor, extraColor, ladder, secondStage); } public DrawingDDB(int speed, float weight, Color bodyColor, Color extraColor, boolean ladder, boolean secondStage) { diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Event.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Event.java new file mode 100644 index 0000000..fc9c62c --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Event.java @@ -0,0 +1,17 @@ +package com.example.doubledeckerbus; + +import java.util.HashSet; +import java.util.Set; +import java.util.function.Consumer; + +public class Event { + private final Set> listeners = new HashSet<>(); + + public void AddListener(Consumer listener) { + listeners.add(listener); + } + + public void Broadcast(T args) { + listeners.forEach(x -> x.accept(args)); + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SerializableColor.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SerializableColor.java new file mode 100644 index 0000000..00c915c --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SerializableColor.java @@ -0,0 +1,26 @@ +package com.example.doubledeckerbus; + +import java.io.Serializable; +import javafx.scene.paint.Color; + +public class SerializableColor implements Serializable +{ + private final double red; + private final double green; + private final double blue; + private final double alpha; + + public SerializableColor(Color color) + { + red = color.getRed(); + green = color.getGreen(); + blue = color.getBlue(); + alpha = color.getOpacity(); + } + + public Color getFXColor() + { + return new Color(red, green, blue, alpha); + } +} + diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java index 983a1c5..55591a4 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java @@ -55,6 +55,7 @@ class SetBusesGeneric { } return result; } + public int Count() { return _places.size(); } diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBusConfig.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBusConfig.fxml new file mode 100644 index 0000000..7b95532 --- /dev/null +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBusConfig.fxml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +