Search in sources :

Example 1 with RotateBuildingCommand

use of org.concord.energy3d.undo.RotateBuildingCommand in project energy3d by concord-consortium.

the class SceneManager method rotate.

public void rotate(final double angle) {
    if (SceneManager.getInstance().getSolarHeatMap()) {
        EnergyPanel.getInstance().updateRadiationHeatMap();
    }
    taskManager.update(new Callable<Object>() {

        @Override
        public Object call() {
            final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
            if (selectedPart instanceof Foundation) {
                final RotateBuildingCommand c = new RotateBuildingCommand((Foundation) selectedPart, angle);
                SceneManager.getInstance().rotateFoundation(angle, true);
                SceneManager.getInstance().getUndoManager().addEdit(c);
            } else if (selectedPart instanceof SolarPanel) {
                final SolarPanel solarPanel = (SolarPanel) selectedPart;
                final ChangeAzimuthCommand c = new ChangeAzimuthCommand(solarPanel);
                solarPanel.setRelativeAzimuth(solarPanel.getRelativeAzimuth() + Math.toDegrees(angle));
                solarPanel.draw();
                SceneManager.getInstance().getUndoManager().addEdit(c);
            } else if (selectedPart instanceof Rack) {
                final Rack rack = (Rack) selectedPart;
                final ChangeAzimuthCommand c = new ChangeAzimuthCommand(rack);
                rack.setRelativeAzimuth(rack.getRelativeAzimuth() + Math.toDegrees(angle));
                rack.draw();
                SceneManager.getInstance().getUndoManager().addEdit(c);
            } else if (selectedPart instanceof Mirror) {
                final Mirror mirror = (Mirror) selectedPart;
                final ChangeAzimuthCommand c = new ChangeAzimuthCommand(mirror);
                mirror.setRelativeAzimuth(mirror.getRelativeAzimuth() + Math.toDegrees(angle));
                mirror.draw();
                SceneManager.getInstance().getUndoManager().addEdit(c);
            } else if (selectedPart == null) {
                final RotateBuildingCommand c = new RotateBuildingCommand(null, angle);
                rotateAllFoundations(angle);
                SceneManager.getInstance().getUndoManager().addEdit(c);
            }
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    EnergyPanel.getInstance().updateProperties();
                }
            });
            return null;
        }
    });
}
Also used : RotateBuildingCommand(org.concord.energy3d.undo.RotateBuildingCommand) Rack(org.concord.energy3d.model.Rack) SolarPanel(org.concord.energy3d.model.SolarPanel) Foundation(org.concord.energy3d.model.Foundation) ChangeAzimuthCommand(org.concord.energy3d.undo.ChangeAzimuthCommand) Mirror(org.concord.energy3d.model.Mirror) PickedHousePart(org.concord.energy3d.model.PickedHousePart) HousePart(org.concord.energy3d.model.HousePart)

Example 2 with RotateBuildingCommand

use of org.concord.energy3d.undo.RotateBuildingCommand in project energy3d by concord-consortium.

the class MainPanel method getRotateButton.

public JButton getRotateButton() {
    if (rotateButton == null) {
        rotateButton = new JButton();
        if (Config.isMac()) {
            // for some reason, the newer version of Mac draws border for JButton (but not JToggleButton)
            rotateButton.setBorderPainted(false);
        }
        rotateButton.addMouseListener(refreshUponMouseExit);
        rotateButton.setIcon(new ImageIcon(getClass().getResource("icons/rotate_cw.png")));
        rotateButton.setToolTipText("<html>Rotate in the clockwise direction (change azimuth).<br>Hold down the Ctrl key and press this button for counter-clockwise rotation.<br>Hold down the Shift key while pressing this button to rotate more slowly.<br>If a component is selected, rotate around its center. Otherwise rotate everything around the origin.</html>");
        rotateButton.setFocusable(false);
        addMouseOverEffect(rotateButton);
        rotateButton.addMouseListener(new MouseAdapter() {

            private volatile boolean mousePressed = false;

            @Override
            public void mousePressed(final MouseEvent e) {
                energyButton.setSelected(false);
                mousePressed = true;
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (selectedPart == null || selectedPart instanceof Tree || selectedPart instanceof Human) {
                    int count = 0;
                    HousePart hp = null;
                    for (final HousePart x : Scene.getInstance().getParts()) {
                        if (x instanceof Foundation) {
                            count++;
                            hp = x;
                        }
                    }
                    if (count == 1) {
                        // if there is only one building, automatically select it to ensure that we always rotate around its center
                        SceneManager.getInstance().setSelectedPart(hp);
                        SceneManager.getInstance().refresh();
                        EnergyPanel.getInstance().updateProperties();
                    }
                }
                new Thread("Energy3D Continuous Rotation") {

                    private int count;

                    private ChangeAzimuthCommand c;

                    @Override
                    public void run() {
                        final HousePart part = SceneManager.getInstance().getSelectedPart();
                        if (part != null) {
                            c = new ChangeAzimuthCommand(part);
                        }
                        while (mousePressed) {
                            SceneManager.getTaskManager().update(new Callable<Object>() {

                                @Override
                                public Object call() throws Exception {
                                    if (part == null) {
                                        SceneManager.getInstance().rotateAllFoundations(rotationAngle);
                                    } else {
                                        if (part instanceof Foundation) {
                                            SceneManager.getInstance().rotateFoundation(rotationAngle, true);
                                        } else if (part instanceof SolarPanel) {
                                            final SolarPanel solarPanel = (SolarPanel) part;
                                            solarPanel.setRelativeAzimuth(solarPanel.getRelativeAzimuth() + Math.toDegrees(rotationAngle));
                                            solarPanel.draw();
                                        } else if (part instanceof Rack) {
                                            final Rack rack = (Rack) part;
                                            rack.setRelativeAzimuth(rack.getRelativeAzimuth() + Math.toDegrees(rotationAngle));
                                            rack.draw();
                                        } else if (part instanceof Mirror) {
                                            final Mirror mirror = (Mirror) part;
                                            mirror.setRelativeAzimuth(mirror.getRelativeAzimuth() + Math.toDegrees(rotationAngle));
                                            mirror.draw();
                                        }
                                    }
                                    count++;
                                    Scene.getInstance().setEdited(true);
                                    EventQueue.invokeLater(new Runnable() {

                                        @Override
                                        public void run() {
                                            EnergyPanel.getInstance().updateProperties();
                                        }
                                    });
                                    return null;
                                }
                            });
                            final int partCount = Scene.getInstance().getParts().size();
                            try {
                                // give it enough time for the above call to complete (the more parts it has, the more time it needs)
                                Thread.sleep(200 + partCount * 5);
                            } catch (final InterruptedException e) {
                            }
                        }
                        // undo only after the thread ends
                        if (part == null) {
                            SceneManager.getInstance().getUndoManager().addEdit(new RotateBuildingCommand(null, rotationAngle * count));
                        } else {
                            if (part instanceof Foundation) {
                                SceneManager.getInstance().getUndoManager().addEdit(new RotateBuildingCommand((Foundation) part, rotationAngle * count));
                            } else if (part instanceof SolarPanel || part instanceof Rack || part instanceof Mirror) {
                                if (c != null) {
                                    SceneManager.getInstance().getUndoManager().addEdit(c);
                                }
                            }
                        }
                    }
                }.start();
            }

            @Override
            public void mouseReleased(final MouseEvent e) {
                mousePressed = false;
            }
        });
    }
    return rotateButton;
}
Also used : Human(org.concord.energy3d.model.Human) ImageIcon(javax.swing.ImageIcon) RotateBuildingCommand(org.concord.energy3d.undo.RotateBuildingCommand) MouseEvent(java.awt.event.MouseEvent) JButton(javax.swing.JButton) MouseAdapter(java.awt.event.MouseAdapter) ChangeAzimuthCommand(org.concord.energy3d.undo.ChangeAzimuthCommand) BadLocationException(javax.swing.text.BadLocationException) Rack(org.concord.energy3d.model.Rack) SolarPanel(org.concord.energy3d.model.SolarPanel) Tree(org.concord.energy3d.model.Tree) Foundation(org.concord.energy3d.model.Foundation) Mirror(org.concord.energy3d.model.Mirror) HousePart(org.concord.energy3d.model.HousePart)

Aggregations

Foundation (org.concord.energy3d.model.Foundation)2 HousePart (org.concord.energy3d.model.HousePart)2 Mirror (org.concord.energy3d.model.Mirror)2 Rack (org.concord.energy3d.model.Rack)2 SolarPanel (org.concord.energy3d.model.SolarPanel)2 ChangeAzimuthCommand (org.concord.energy3d.undo.ChangeAzimuthCommand)2 RotateBuildingCommand (org.concord.energy3d.undo.RotateBuildingCommand)2 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 ImageIcon (javax.swing.ImageIcon)1 JButton (javax.swing.JButton)1 BadLocationException (javax.swing.text.BadLocationException)1 Human (org.concord.energy3d.model.Human)1 PickedHousePart (org.concord.energy3d.model.PickedHousePart)1 Tree (org.concord.energy3d.model.Tree)1