Search in sources :

Example 1 with BetterAction

use of org.pepsoft.worldpainter.util.BetterAction in project WorldPainter by Captain-Chaos.

the class App method initComponents.

private void initComponents() {
    view = new WorldPainter(selectedColourScheme, autoBiomeScheme, customBiomeManager);
    Configuration config = Configuration.getInstance();
    if (config.getBackgroundColour() == -1) {
        view.setBackground(new Color(VoidRenderer.getColour()));
    } else {
        view.setBackground(new Color(config.getBackgroundColour()));
    }
    view.setDrawBorders(config.isShowBorders());
    view.setDrawBiomes(config.isShowBiomes());
    view.setBackgroundImageMode(config.getBackgroundImageMode());
    if (config.getBackgroundImage() != null) {
        new Thread("Background Image Loader") {

            @Override
            public void run() {
                try {
                    BufferedImage image = ImageIO.read(config.getBackgroundImage());
                    SwingUtilities.invokeLater(() -> view.setBackgroundImage(image));
                } catch (IOException e) {
                    logger.error("I/O error loading background image; disabling background image", e);
                }
            }
        }.start();
    }
    view.setRadius(radius);
    view.setBrushShape(brush.getBrushShape());
    final Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(IconUtils.loadScaledImage("org/pepsoft/worldpainter/cursor.png"), new Point(15 * UI_SCALE, 15 * UI_SCALE), "Custom Crosshair");
    view.addMouseListener(new java.awt.event.MouseAdapter() {

        @Override
        public void mouseEntered(MouseEvent e) {
            setCursor(cursor);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            setCursor(null);
        }
    });
    TiledImageViewerContainer viewContainer = new TiledImageViewerContainer(view);
    glassPane = new GlassPane();
    JRootPane privateRootPane = new JRootPane();
    privateRootPane.putClientProperty(HELP_KEY_KEY, "Editor");
    privateRootPane.setContentPane(viewContainer);
    privateRootPane.setGlassPane(glassPane);
    glassPane.setVisible(true);
    // Set up docking framework
    JPanel contentContainer = new JPanel(new BorderLayout());
    getContentPane().add(contentContainer, BorderLayout.CENTER);
    dockingManager = new DefaultDockingManager(this, contentContainer);
    if (SystemUtils.isLinux()) {
        // On Linux, at least in the GTK look and feel, the default
        // (whatever it is) doesn't work; nothing is displayed
        dockingManager.setOutlineMode(DockingManager.MIX_OUTLINE_MODE);
    }
    dockingManager.setGroupAllowedOnSidePane(false);
    dockingManager.setTabbedPaneCustomizer(tabbedPane -> tabbedPane.setTabPlacement(JTabbedPane.LEFT));
    Workspace workspace = dockingManager.getWorkspace();
    workspace.setLayout(new BorderLayout());
    workspace.add(privateRootPane, BorderLayout.CENTER);
    setJMenuBar(createMenuBar());
    getContentPane().add(createToolBar(), BorderLayout.NORTH);
    getContentPane().add(createStatusBar(), BorderLayout.SOUTH);
    final ScrollController scrollController = new ScrollController();
    scrollController.install();
    mapDragControl = new MapDragControl() {

        @Override
        public boolean isMapDraggingInhibited() {
            return mapDraggingInhibited;
        }

        @Override
        public void setMapDraggingInhibited(boolean mapDraggingInhibited) {
            if (mapDraggingInhibited != this.mapDraggingInhibited) {
                this.mapDraggingInhibited = mapDraggingInhibited;
                if (mapDraggingInhibited) {
                    scrollController.uninstall();
                } else {
                    scrollController.install();
                }
            }
        }

        private boolean mapDraggingInhibited;
    };
    dockingManager.addFrame(new DockableFrameBuilder(createToolPanel(), "Tools", DOCK_SIDE_WEST, 1).build());
    dockingManager.addFrame(new DockableFrameBuilder(createToolSettingsPanel(), "Tool Settings", DOCK_SIDE_WEST, 2).expand().build());
    dockingManager.addFrame(new DockableFrameBuilder(createLayerPanel(), "Layers", DOCK_SIDE_WEST, 3).build());
    dockingManager.addFrame(new DockableFrameBuilder(createTerrainPanel(), "Terrain", DOCK_SIDE_WEST, 3).build());
    biomesPanel = new DockableFrameBuilder(createBiomesPanel(), "Biomes", DOCK_SIDE_WEST, 3).build();
    dockingManager.addFrame(biomesPanel);
    dockingManager.addFrame(new DockableFrameBuilder(createAnnotationsPanel(), "Annotations", DOCK_SIDE_WEST, 3).build());
    dockingManager.addFrame(new DockableFrameBuilder(createBrushPanel(), "Brushes", DOCK_SIDE_EAST, 1).build());
    if (customBrushes.containsKey(CUSTOM_BRUSHES_DEFAULT_TITLE)) {
        dockingManager.addFrame(new DockableFrameBuilder(createCustomBrushPanel(CUSTOM_BRUSHES_DEFAULT_TITLE, customBrushes.get(CUSTOM_BRUSHES_DEFAULT_TITLE)), "Custom Brushes", DOCK_SIDE_EAST, 1).withId("customBrushesDefault").build());
    }
    for (Map.Entry<String, List<Brush>> entry : customBrushes.entrySet()) {
        if (entry.getKey().equals(CUSTOM_BRUSHES_DEFAULT_TITLE)) {
            continue;
        }
        dockingManager.addFrame(new DockableFrameBuilder(createCustomBrushPanel(entry.getKey(), entry.getValue()), entry.getKey(), DOCK_SIDE_EAST, 1).withId("customBrushes." + entry.getKey()).build());
    }
    dockingManager.addFrame(new DockableFrameBuilder(createBrushSettingsPanel(), "Brush Settings", DOCK_SIDE_EAST, 2).withId("brushSettings").build());
    dockingManager.addFrame(new DockableFrameBuilder(createInfoPanel(), "Info", DOCK_SIDE_EAST, 2).withId("infoPanel").expand().withIcon(loadScaledIcon("information")).build());
    if (config.getDefaultJideLayoutData() != null) {
        dockingManager.loadLayoutFrom(new ByteArrayInputStream(config.getDefaultJideLayoutData()));
    } else {
        dockingManager.resetToDefault();
    }
    MouseAdapter viewListener = new MouseAdapter() {

        @Override
        public void mouseMoved(MouseEvent e) {
            Point worldCoords = view.viewToWorld(e.getX(), e.getY());
            updateStatusBar(worldCoords.x, worldCoords.y);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            locationLabel.setText(strings.getString("location-"));
            heightLabel.setText(" ");
            waterLabel.setText(" ");
            App.this.biomeLabel.setText(" ");
            materialLabel.setText(" ");
        }

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.isControlDown()) {
                int oldZoom = view.getZoom(), zoom;
                if (e.getWheelRotation() < 0) {
                    zoom = Math.min(oldZoom - e.getWheelRotation(), 6);
                } else {
                    zoom = Math.max(oldZoom - e.getWheelRotation(), -4);
                }
                if (zoom != oldZoom) {
                    view.setZoom(zoom, e.getX(), e.getY());
                    updateZoomLabel();
                    ACTION_ZOOM_IN.setEnabled(zoom < 6);
                    ACTION_ZOOM_OUT.setEnabled(zoom > -4);
                    ACTION_ZOOM_RESET.setEnabled(zoom != 0);
                }
            } else if (e.isAltDown() || e.isAltGraphDown()) {
                if (e.getWheelRotation() < 0) {
                    ACTION_ROTATE_BRUSH_LEFT.actionPerformed(new ActionEvent(e.getSource(), e.getID(), e.paramString()));
                } else {
                    ACTION_ROTATE_BRUSH_RIGHT.actionPerformed(new ActionEvent(e.getSource(), e.getID(), e.paramString()));
                }
            } else if (activeOperation instanceof RadiusOperation) {
                if (e.isShiftDown()) {
                    if (e.getWheelRotation() < 0) {
                        decreaseRadiusByOne();
                    } else {
                        increaseRadiusByOne();
                    }
                } else {
                    if (e.getWheelRotation() < 0) {
                        decreaseRadius(-e.getWheelRotation());
                    } else {
                        increaseRadius(e.getWheelRotation());
                    }
                }
            }
        }
    };
    view.addMouseMotionListener(viewListener);
    view.addMouseListener(viewListener);
    view.addMouseWheelListener(viewListener);
    if (config.getShowCalloutCount() > 0) {
        BufferedImage callout = loadCallout("callout_1");
        view.addOverlay("callout_1", 0, dockingManager.getFrame("tools"), callout);
        callout = loadCallout("callout_2");
        view.addOverlay("callout_2", -callout.getWidth(), dockingManager.getFrame("brushes"), callout);
        callout = loadCallout("callout_3");
        view.addOverlay("callout_3", 0, dockingManager.getFrame("layers"), callout);
        config.setShowCalloutCount(config.getShowCalloutCount() - 1);
    }
    JRootPane rootPane = getRootPane();
    ActionMap actionMap = rootPane.getActionMap();
    actionMap.put(ACTION_NAME_INCREASE_RADIUS, new BetterAction(ACTION_NAME_INCREASE_RADIUS, strings.getString("increase.radius")) {

        @Override
        public void performAction(ActionEvent e) {
            increaseRadius(1);
        }

        private static final long serialVersionUID = 2011090601L;
    });
    actionMap.put(ACTION_NAME_INCREASE_RADIUS_BY_ONE, new BetterAction(ACTION_NAME_INCREASE_RADIUS_BY_ONE, "Increase brush radius by one") {

        @Override
        public void performAction(ActionEvent e) {
            increaseRadiusByOne();
        }

        private static final long serialVersionUID = 2011090601L;
    });
    actionMap.put(ACTION_NAME_DECREASE_RADIUS, new BetterAction(ACTION_NAME_DECREASE_RADIUS, strings.getString("decrease.radius")) {

        @Override
        public void performAction(ActionEvent e) {
            decreaseRadius(1);
        }

        private static final long serialVersionUID = 2011090601L;
    });
    actionMap.put(ACTION_NAME_DECREASE_RADIUS_BY_ONE, new BetterAction(ACTION_NAME_DECREASE_RADIUS_BY_ONE, "Decrease brush radius by one") {

        @Override
        public void performAction(ActionEvent e) {
            decreaseRadiusByOne();
        }

        private static final long serialVersionUID = 2011090601L;
    });
    actionMap.put(ACTION_NAME_REDO, ACTION_REDO);
    actionMap.put(ACTION_NAME_ZOOM_IN, ACTION_ZOOM_IN);
    actionMap.put(ACTION_NAME_ZOOM_OUT, ACTION_ZOOM_OUT);
    actionMap.put(ACTION_ZOOM_RESET.getName(), ACTION_ZOOM_RESET);
    actionMap.put(ACTION_ROTATE_BRUSH_LEFT.getName(), ACTION_ROTATE_BRUSH_LEFT);
    actionMap.put(ACTION_ROTATE_BRUSH_RIGHT.getName(), ACTION_ROTATE_BRUSH_RIGHT);
    actionMap.put(ACTION_ROTATE_BRUSH_RESET.getName(), ACTION_ROTATE_BRUSH_RESET);
    actionMap.put(ACTION_ROTATE_BRUSH_RIGHT_30_DEGREES.getName(), ACTION_ROTATE_BRUSH_RIGHT_30_DEGREES);
    actionMap.put(ACTION_ROTATE_BRUSH_RIGHT_45_DEGREES.getName(), ACTION_ROTATE_BRUSH_RIGHT_45_DEGREES);
    actionMap.put(ACTION_ROTATE_BRUSH_RIGHT_90_DEGREES.getName(), ACTION_ROTATE_BRUSH_RIGHT_90_DEGREES);
    int platformCommandMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(KeyStroke.getKeyStroke(VK_SUBTRACT, 0), ACTION_NAME_DECREASE_RADIUS);
    inputMap.put(KeyStroke.getKeyStroke(VK_MINUS, 0), ACTION_NAME_DECREASE_RADIUS);
    inputMap.put(KeyStroke.getKeyStroke(VK_ADD, 0), ACTION_NAME_INCREASE_RADIUS);
    inputMap.put(KeyStroke.getKeyStroke(VK_EQUALS, SHIFT_DOWN_MASK), ACTION_NAME_INCREASE_RADIUS);
    inputMap.put(KeyStroke.getKeyStroke(VK_SUBTRACT, SHIFT_DOWN_MASK), ACTION_NAME_DECREASE_RADIUS_BY_ONE);
    inputMap.put(KeyStroke.getKeyStroke(VK_MINUS, SHIFT_DOWN_MASK), ACTION_NAME_DECREASE_RADIUS_BY_ONE);
    inputMap.put(KeyStroke.getKeyStroke(VK_ADD, SHIFT_DOWN_MASK), ACTION_NAME_INCREASE_RADIUS_BY_ONE);
    inputMap.put(KeyStroke.getKeyStroke(VK_Z, platformCommandMask | SHIFT_DOWN_MASK), ACTION_NAME_REDO);
    inputMap.put(KeyStroke.getKeyStroke(VK_MINUS, platformCommandMask), ACTION_NAME_ZOOM_OUT);
    inputMap.put(KeyStroke.getKeyStroke(VK_EQUALS, platformCommandMask | SHIFT_DOWN_MASK), ACTION_NAME_ZOOM_IN);
    inputMap.put(KeyStroke.getKeyStroke(VK_NUMPAD0, platformCommandMask), ACTION_ZOOM_RESET.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_SUBTRACT, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_LEFT.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_MINUS, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_LEFT.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_ADD, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RIGHT.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_EQUALS, ALT_DOWN_MASK | SHIFT_DOWN_MASK), ACTION_ROTATE_BRUSH_RIGHT.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_0, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RESET.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_NUMPAD0, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RESET.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_3, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RIGHT_30_DEGREES.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_NUMPAD3, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RIGHT_30_DEGREES.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_4, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RIGHT_45_DEGREES.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_NUMPAD4, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RIGHT_45_DEGREES.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_9, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RIGHT_90_DEGREES.getName());
    inputMap.put(KeyStroke.getKeyStroke(VK_NUMPAD9, ALT_DOWN_MASK), ACTION_ROTATE_BRUSH_RIGHT_90_DEGREES.getName());
    programmaticChange = true;
    try {
        selectBrushButton(brush);
        levelSlider.setValue((int) (level * 100));
        brushRotationSlider.setValue(brushRotation);
    } finally {
        programmaticChange = false;
    }
}
Also used : BufferedImage(java.awt.image.BufferedImage) List(java.util.List) java.awt.event(java.awt.event) BetterAction(org.pepsoft.worldpainter.util.BetterAction) Paint(org.pepsoft.worldpainter.painting.Paint) TiledImageViewerContainer(org.pepsoft.util.swing.TiledImageViewerContainer)

Aggregations

java.awt.event (java.awt.event)1 BufferedImage (java.awt.image.BufferedImage)1 List (java.util.List)1 TiledImageViewerContainer (org.pepsoft.util.swing.TiledImageViewerContainer)1 Paint (org.pepsoft.worldpainter.painting.Paint)1 BetterAction (org.pepsoft.worldpainter.util.BetterAction)1