Ticket #5852: shear.patch

File shear.patch, 17.3 KB (added by osm@…, 15 years ago)

Patch to add shear mode relative to r25058

  • src/org/openstreetmap/josm/plugins/piclayer/ResetPictureShearAction.java

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: images/mapmode/shear.png
    ___________________________________________________________________
    Added: svn:mime-type
       + application/octet-stream
    
     
     1/***************************************************************************
     2 *   Copyright (C) 2011 by Patrick "Petschge" Kilian, based on code        *
     3 *   Copyright (C) 2009 by Tomasz Stelmach                                 *
     4 *   http://www.stelmach-online.net/                                       *
     5 *                                                                         *
     6 *   This program is free software; you can redistribute it and/or modify  *
     7 *   it under the terms of the GNU General Public License as published by  *
     8 *   the Free Software Foundation; either version 2 of the License, or     *
     9 *   (at your option) any later version.                                   *
     10 *                                                                         *
     11 *   This program is distributed in the hope that it will be useful,       *
     12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
     13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
     14 *   GNU General Public License for more details.                          *
     15 *                                                                         *
     16 *   You should have received a copy of the GNU General Public License     *
     17 *   along with this program; if not, write to the                         *
     18 *   Free Software Foundation, Inc.,                                       *
     19 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
     20 ***************************************************************************/
     21
     22package org.openstreetmap.josm.plugins.piclayer;
     23
     24import static org.openstreetmap.josm.tools.I18n.tr;
     25
     26import java.awt.event.ActionEvent;
     27
     28import org.openstreetmap.josm.Main;
     29import org.openstreetmap.josm.actions.JosmAction;
     30
     31/**
     32 * Action for resetting properties of an image.
     33 *
     34 * TODO Four almost identical classes. Refactoring needed.
     35 */
     36public class ResetPictureShearAction extends JosmAction {
     37
     38    // Owner layer of the action
     39    PicLayerAbstract m_owner = null;
     40   
     41    /**
     42     * Constructor
     43     */
     44    public ResetPictureShearAction( PicLayerAbstract owner ) {
     45        super(tr("Shear"), null, tr("Resets picture shear"), null, false);
     46        // Remember the owner...
     47        m_owner = owner;
     48    }
     49   
     50    /**
     51     * Action handler
     52     */
     53    public void actionPerformed(ActionEvent arg0) {
     54        // Reset
     55        m_owner.resetShear();
     56        // Redraw
     57        Main.map.mapView.repaint();
     58    }
     59}
  • src/org/openstreetmap/josm/plugins/piclayer/MovePictureAction.java

     
    3232import org.openstreetmap.josm.tools.ImageProvider;
    3333import org.openstreetmap.josm.data.coor.EastNorth;
    3434
    35 //TODO: Move/Rotate/Scale action classes are similar. Do the redesign!
     35//TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3636
    3737/**
    3838 * This class handles the input during moving the picture.
  • src/org/openstreetmap/josm/plugins/piclayer/ScaleXYPictureAction.java

     
    2727import org.openstreetmap.josm.gui.MapFrame;
    2828import org.openstreetmap.josm.tools.ImageProvider;
    2929
    30 // TODO: Move/Rotate/Scale action classes are similar. Do the redesign!
     30// TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3131
    3232/**
    3333 * This class handles the input during scaling the picture.
  • src/org/openstreetmap/josm/plugins/piclayer/ResetPictureAllAction.java

     
    5454        m_owner.resetAngle();
    5555        m_owner.resetPosition();
    5656        m_owner.resetScale();
     57        m_owner.resetShear();
    5758        // Redraw
    5859        Main.map.mapView.repaint();
    5960    }
  • src/org/openstreetmap/josm/plugins/piclayer/ScaleXPictureAction.java

     
    2727import org.openstreetmap.josm.gui.MapFrame;
    2828import org.openstreetmap.josm.tools.ImageProvider;
    2929
    30 // TODO: Move/Rotate/Scale action classes are similar. Do the redesign!
     30// TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3131
    3232/**
    3333 * This class handles the input during scaling the picture.
  • src/org/openstreetmap/josm/plugins/piclayer/ScaleYPictureAction.java

     
    2727import org.openstreetmap.josm.gui.MapFrame;
    2828import org.openstreetmap.josm.tools.ImageProvider;
    2929
    30 // TODO: Move/Rotate/Scale action classes are similar. Do the redesign!
     30// TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3131
    3232/**
    3333 * This class handles the input during scaling the picture.
  • src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java

     
    4949    private IconToggleButton m_scalexPictureButton = null;
    5050    private IconToggleButton m_scaleyPictureButton = null;
    5151    private IconToggleButton m_scalexyPictureButton = null;
     52    private IconToggleButton m_shearPictureButton = null;
    5253
    5354    // Menu actions
    5455    private NewLayerFromFileAction      m_newFromFileAction = null;
     
    8889            ScaleXYPictureAction scaleXYPictureAction = new ScaleXYPictureAction(newFrame);
    8990            ScaleXPictureAction scaleXPictureAction = new ScaleXPictureAction(newFrame);
    9091            ScaleYPictureAction scaleYPictureAction = new ScaleYPictureAction(newFrame);
     92            ShearPictureAction shearPictureAction = new ShearPictureAction(newFrame);
    9193            // Create plugin buttons and add them to the toolbar
    9294            m_movePictureButton = new IconToggleButton(movePictureAction);
    9395            m_rotatePictureButton = new IconToggleButton(rotatePictureAction);
    9496            m_scalexyPictureButton = new IconToggleButton(scaleXYPictureAction);
    9597            m_scalexPictureButton = new IconToggleButton(scaleXPictureAction);
    9698            m_scaleyPictureButton = new IconToggleButton(scaleYPictureAction);
     99            m_shearPictureButton = new IconToggleButton(shearPictureAction);
    97100            newFrame.addMapMode(m_movePictureButton);
    98101            newFrame.addMapMode(m_rotatePictureButton);
    99102            newFrame.addMapMode(m_scalexyPictureButton);
    100103            newFrame.addMapMode(m_scalexPictureButton);
    101104            newFrame.addMapMode(m_scaleyPictureButton);
     105            newFrame.addMapMode(m_shearPictureButton);
    102106//            newFrame.toolGroup.add(m_movePictureButton);
    103107//            newFrame.toolGroup.add(m_rotatePictureButton);
    104108//            newFrame.toolGroup.add(m_scalePictureButton);
     
    108112            m_scalexyPictureButton.setVisible(true);
    109113            m_scalexPictureButton.setVisible(true);
    110114            m_scaleyPictureButton.setVisible(true);
     115            m_shearPictureButton.setVisible(true);
    111116        }
    112117    }
    113118
  • src/org/openstreetmap/josm/plugins/piclayer/RotatePictureAction.java

     
    3131import org.openstreetmap.josm.gui.MapFrame;
    3232import org.openstreetmap.josm.tools.ImageProvider;
    3333
    34 //TODO: Move/Rotate/Scale action classes are similar. Do the redesign!
     34//TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3535
    3636/**
    3737 * This class handles the input during rotating the picture.
  • src/org/openstreetmap/josm/plugins/piclayer/PicLayerAbstract.java

     
    7373    // Scale of the image
    7474    private double m_scalex = 1.0;
    7575    private double m_scaley = 1.0;
     76    // Shear of the image
     77    private double m_shearx = 0.0;
     78    private double m_sheary = 0.0;
    7679    // The scale that was set on the map during image creation
    7780    private double m_initial_scale = 1.0;
    7881    // Layer icon
     
    8790    private final String INITIAL_SCALE = "INITIAL_SCALE";
    8891    private final String SCALEX = "SCALEX";
    8992    private final String SCALEY = "SCALEY";
     93    private final String SHEARX = "SHEARX";
     94    private final String SHEARY = "SHEARY";
    9095
    9196    /**
    9297     * Constructor
     
    214219            double scalex = m_scalex * m_initial_scale / Main.map.mapView.getDist100Pixel();
    215220            double scaley = m_scaley * m_initial_scale / Main.map.mapView.getDist100Pixel();
    216221            g.scale( scalex, scaley );
     222            // Shear
     223            g.shear(m_shearx, m_sheary);
    217224
    218225            // Draw picture
    219226            g.drawImage( m_image, -m_image.getWidth() / 2, -m_image.getHeight() / 2, null );
     
    257264    }
    258265
    259266    /**
     267     * Shear the picture. shearx and sheary will separately add to the
     268     * corresponding current value
     269     */
     270    public void shearPictureBy( double shearx, double sheary ) {
     271        m_shearx += shearx;
     272        m_sheary += sheary;
     273    }
     274
     275    /**
    260276     * Sets the image position to the initial position
    261277     */
    262278    public void resetPosition() {
     
    278294        m_angle = 0.0;
    279295    }
    280296
     297
     298    /**
     299     * Sets the image to no shear
     300     */
     301    public void resetShear() {
     302        m_shearx = 0.0;
     303        m_sheary = 0.0;
     304    }
    281305    @Override
    282306    /**
    283307     * Computes the (rough) bounding box.
     
    329353        props.put(SCALEX, "" + m_scalex);
    330354        props.put(SCALEY, "" + m_scaley);
    331355        props.put(ANGLE, "" + m_angle);
     356        props.put(SHEARX, "" + m_shearx);
     357        props.put(SHEARY, "" + m_sheary);
    332358    }
    333359
    334360    /**
     
    357383            double in_scale = Double.valueOf( props.getProperty(INITIAL_SCALE));
    358384            double scale_x = Double.valueOf( props.getProperty(SCALEX));
    359385            double scale_y = Double.valueOf( props.getProperty(SCALEY));
     386            double shear_x = Double.valueOf( props.getProperty(SHEARX));
     387            double shear_y = Double.valueOf( props.getProperty(SHEARY));
    360388            m_position.setLocation(pos_x, pos_y);
    361389            m_initial_position.setLocation(pos_x, pos_y);
    362390            m_angle = angle;
    363391            m_scalex = scale_x;
    364392            m_scaley = scale_y;
     393            m_shearx = shear_x;
     394            m_sheary = shear_y;
    365395            m_initial_scale = in_scale;
    366396            // Refresh
    367397            Main.map.mapView.repaint();
     
    383413            reset_submenu.add( new ResetPicturePositionAction( PicLayerAbstract.this ) );
    384414            reset_submenu.add( new ResetPictureAngleAction( PicLayerAbstract.this ) );
    385415            reset_submenu.add( new ResetPictureScaleAction( PicLayerAbstract.this ) );
     416            reset_submenu.add( new ResetPictureShearAction( PicLayerAbstract.this ) );
    386417            return reset_submenu;
    387418        }
    388419
  • src/org/openstreetmap/josm/plugins/piclayer/ShearPictureAction.java

     
     1/***************************************************************************
     2 *                                                                         *
     3 *   Copyright (C) 2011 Patrick "Petschge" Kilian, based on code           *
     4 *   (c) 2009 by Tomasz Stelmach                                           *
     5 *   http://www.stelmach-online.net/                                       *
     6 *                                                                         *
     7 *   This program is free software; you can redistribute it and/or modify  *
     8 *   it under the terms of the GNU General Public License as published by  *
     9 *   the Free Software Foundation; either version 2 of the License, or     *
     10 *   (at your option) any later version.                                   *
     11 *                                                                         *
     12 *   This program is distributed in the hope that it will be useful,       *
     13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
     14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
     15 *   GNU General Public License for more details.                          *
     16 *                                                                         *
     17 *   You should have received a copy of the GNU General Public License     *
     18 *   along with this program; if not, write to the                         *
     19 *   Free Software Foundation, Inc.,                                       *
     20 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
     21 ***************************************************************************/
     22
     23package org.openstreetmap.josm.plugins.piclayer;
     24
     25import static org.openstreetmap.josm.tools.I18n.tr;
     26
     27import java.awt.event.MouseEvent;
     28import java.awt.event.MouseListener;
     29import java.awt.event.MouseMotionListener;
     30
     31import org.openstreetmap.josm.Main;
     32import org.openstreetmap.josm.actions.mapmode.MapMode;
     33import org.openstreetmap.josm.gui.MapFrame;
     34import org.openstreetmap.josm.tools.ImageProvider;
     35import org.openstreetmap.josm.data.coor.EastNorth;
     36
     37//TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
     38
     39/**
     40 * This class handles the input during shearing of the picture.
     41 */
     42public class ShearPictureAction extends MapMode implements MouseListener, MouseMotionListener
     43{
     44    // Action ongoing?
     45    private boolean mb_dragging = false;
     46
     47    // Last mouse position
     48    private EastNorth m_prevEastNorth;
     49
     50    // The layer we're working on
     51    private PicLayerAbstract m_currentLayer = null;
     52
     53    /**
     54     * Constructor
     55     */
     56    public ShearPictureAction(MapFrame frame) {
     57        super(tr("PicLayer shear"), "shear", tr("Drag to shear the picture"), frame, ImageProvider.getCursor("crosshair", null));
     58    }
     59
     60    @Override
     61    public void enterMode() {
     62        super.enterMode();
     63        Main.map.mapView.addMouseListener(this);
     64        Main.map.mapView.addMouseMotionListener(this);
     65    }
     66
     67    @Override
     68    public void exitMode() {
     69        super.exitMode();
     70        Main.map.mapView.removeMouseListener(this);
     71        Main.map.mapView.removeMouseMotionListener(this);
     72    }
     73
     74    @Override
     75    public void mousePressed(MouseEvent e) {
     76
     77        // If everything is OK, we start dragging/moving the picture
     78        if ( Main.map.mapView.getActiveLayer() instanceof PicLayerAbstract ) {
     79            m_currentLayer = (PicLayerAbstract)Main.map.mapView.getActiveLayer();
     80
     81            if ( m_currentLayer != null && e.getButton() == MouseEvent.BUTTON1 ) {
     82                mb_dragging = true;
     83                m_prevEastNorth=Main.map.mapView.getEastNorth(e.getX(),e.getY());
     84            }
     85        }
     86    }
     87
     88    @Override
     89    public void mouseDragged(MouseEvent e) {
     90        // Picture moving is ongoing
     91        if(mb_dragging) {
     92            EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
     93            m_currentLayer.shearPictureBy(
     94                1000* (eastNorth.east()-m_prevEastNorth.east()),
     95                1000* (eastNorth.north()-m_prevEastNorth.north())
     96            );
     97            m_prevEastNorth = eastNorth;
     98            Main.map.mapView.repaint();
     99        }
     100    }
     101
     102    @Override
     103    public void mouseReleased(MouseEvent e) {
     104        // Stop moving
     105        mb_dragging = false;
     106    }
     107
     108}