Changeset 27120 in osm for applications/editors


Ignore:
Timestamp:
2011-11-22T16:18:12+01:00 (13 years ago)
Author:
larry0ua
Message:

'PicLayer rewritten - lots changed'

Location:
applications/editors/josm/plugins/piclayer
Files:
17 added
5 deleted
2 edited
15 moved

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/piclayer/build.xml

    r27119 r27120  
    2222-->
    2323<project name="PicLayer" default="dist" basedir=".">
    24     <property name="commit.message" value="applied #J5852 (patch by Petschge) - new shear option"/>
     24    <property name="commit.message" value="PicLayer rewritten - lots changed"/>
    2525    <property name="plugin.main.version" value="4549"/>
    2626    <!--
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java

    r26489 r27120  
    2525
    2626import java.awt.event.KeyEvent;
     27
    2728import javax.swing.JMenu;
    2829
    2930import org.openstreetmap.josm.Main;
    30 import org.openstreetmap.josm.plugins.Plugin;
    31 import org.openstreetmap.josm.plugins.PluginInformation;
    3231import org.openstreetmap.josm.gui.IconToggleButton;
     32import org.openstreetmap.josm.gui.MapFrame;
    3333import org.openstreetmap.josm.gui.MapView;
    3434import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    3535import org.openstreetmap.josm.gui.layer.Layer;
    36 import org.openstreetmap.josm.gui.MapFrame;
     36import org.openstreetmap.josm.plugins.Plugin;
     37import org.openstreetmap.josm.plugins.PluginInformation;
     38import org.openstreetmap.josm.plugins.piclayer.actions.newlayer.NewLayerFromClipboardAction;
     39import org.openstreetmap.josm.plugins.piclayer.actions.newlayer.NewLayerFromFileAction;
     40import org.openstreetmap.josm.plugins.piclayer.actions.transform.MovePictureAction;
     41import org.openstreetmap.josm.plugins.piclayer.actions.transform.RotatePictureAction;
     42import org.openstreetmap.josm.plugins.piclayer.actions.transform.ScaleXPictureAction;
     43import org.openstreetmap.josm.plugins.piclayer.actions.transform.ScaleXYPictureAction;
     44import org.openstreetmap.josm.plugins.piclayer.actions.transform.ScaleYPictureAction;
     45import org.openstreetmap.josm.plugins.piclayer.actions.transform.ShearPictureAction;
     46import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.MovePointAction;
     47import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.TransformPointAction;
    3748
    3849/**
     
    4152public class PicLayerPlugin extends Plugin implements LayerChangeListener {
    4253
    43     // Plugin menu
    44     private JMenu m_menu = null;
    4554
    4655    // Toolbar buttons
    47     private IconToggleButton m_movePictureButton = null;
    48     private IconToggleButton m_rotatePictureButton = null;
    49     private IconToggleButton m_scalexPictureButton = null;
    50     private IconToggleButton m_scaleyPictureButton = null;
    51     private IconToggleButton m_scalexyPictureButton = null;
    52     private IconToggleButton m_shearPictureButton = null;
     56    static IconToggleButton movePictureButton = null;
     57    static IconToggleButton movePointButton = null;
     58    static IconToggleButton transformPointButton = null;
     59    static IconToggleButton rotatePictureButton = null;
     60    static IconToggleButton scalexPictureButton = null;
     61    static IconToggleButton scaleyPictureButton = null;
     62    static IconToggleButton scalexyPictureButton = null;
     63    static IconToggleButton shearPictureButton = null;
    5364
    54     // Menu actions
    55     private NewLayerFromFileAction      m_newFromFileAction = null;
    56     private NewLayerFromClipboardAction m_newFromClipAction = null;
     65    // Plugin menu
     66    private JMenu menu = null;
     67    private ActionVisibilityChangeMenu actionVisibility;
    5768
    5869    /**
     
    6475        // Create menu entry
    6576        if ( Main.main.menu != null ) {
    66             m_menu = Main.main.menu.addMenu(marktr("PicLayer") , KeyEvent.VK_I, Main.main.menu.defaultMenuPos, ht("/Plugin/PicLayer"));
     77            menu = Main.main.menu.addMenu(marktr("PicLayer") , KeyEvent.VK_I, Main.main.menu.defaultMenuPos, ht("/Plugin/PicLayer"));
    6778        }
    6879
    6980        // Add menu items
    70         if ( m_menu != null ) {
    71             m_menu.add( m_newFromFileAction = new NewLayerFromFileAction() );
    72             m_menu.add( m_newFromClipAction = new NewLayerFromClipboardAction() );
    73             m_newFromFileAction.setEnabled( false );
    74             m_newFromClipAction.setEnabled( false );
     81        if ( menu != null ) {
     82            menu.add(new NewLayerFromFileAction());
     83            menu.add(new NewLayerFromClipboardAction());
     84            menu.setEnabled(false);
    7585        }
    7686
     
    8292     * Called when the map is created. Creates the toolbar buttons.
    8393     */
     94    @Override
    8495    public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
    8596        if(newFrame != null) {
    8697            // Create plugin map modes
    8798            MovePictureAction movePictureAction = new MovePictureAction(newFrame);
     99            MovePointAction movePointAction = new MovePointAction(newFrame);
     100            TransformPointAction transformPointAction = new TransformPointAction(newFrame);
     101
    88102            RotatePictureAction rotatePictureAction = new RotatePictureAction(newFrame);
    89103            ScaleXYPictureAction scaleXYPictureAction = new ScaleXYPictureAction(newFrame);
     
    92106            ShearPictureAction shearPictureAction = new ShearPictureAction(newFrame);
    93107            // Create plugin buttons and add them to the toolbar
    94             m_movePictureButton = new IconToggleButton(movePictureAction);
    95             m_rotatePictureButton = new IconToggleButton(rotatePictureAction);
    96             m_scalexyPictureButton = new IconToggleButton(scaleXYPictureAction);
    97             m_scalexPictureButton = new IconToggleButton(scaleXPictureAction);
    98             m_scaleyPictureButton = new IconToggleButton(scaleYPictureAction);
    99             m_shearPictureButton = new IconToggleButton(shearPictureAction);
    100             newFrame.addMapMode(m_movePictureButton);
    101             newFrame.addMapMode(m_rotatePictureButton);
    102             newFrame.addMapMode(m_scalexyPictureButton);
    103             newFrame.addMapMode(m_scalexPictureButton);
    104             newFrame.addMapMode(m_scaleyPictureButton);
    105             newFrame.addMapMode(m_shearPictureButton);
     108            movePictureButton = new IconToggleButton(movePictureAction);
     109            movePointButton = new IconToggleButton(movePointAction);
     110            transformPointButton = new IconToggleButton(transformPointAction);
     111            rotatePictureButton = new IconToggleButton(rotatePictureAction);
     112            scalexyPictureButton = new IconToggleButton(scaleXYPictureAction);
     113            scalexPictureButton = new IconToggleButton(scaleXPictureAction);
     114            scaleyPictureButton = new IconToggleButton(scaleYPictureAction);
     115            shearPictureButton = new IconToggleButton(shearPictureAction);
     116            newFrame.addMapMode(movePictureButton);
     117            newFrame.addMapMode(movePointButton);
     118            newFrame.addMapMode(transformPointButton);
     119            newFrame.addMapMode(rotatePictureButton);
     120            newFrame.addMapMode(scalexyPictureButton);
     121            newFrame.addMapMode(scalexPictureButton);
     122            newFrame.addMapMode(scaleyPictureButton);
     123            newFrame.addMapMode(shearPictureButton);
    106124//            newFrame.toolGroup.add(m_movePictureButton);
    107125//            newFrame.toolGroup.add(m_rotatePictureButton);
    108126//            newFrame.toolGroup.add(m_scalePictureButton);
    109127            // Show them by default
    110             m_movePictureButton.setVisible(true);
    111             m_rotatePictureButton.setVisible(true);
    112             m_scalexyPictureButton.setVisible(true);
    113             m_scalexPictureButton.setVisible(true);
    114             m_scaleyPictureButton.setVisible(true);
    115             m_shearPictureButton.setVisible(true);
     128
     129            if (actionVisibility == null)
     130                menu.add(actionVisibility = new ActionVisibilityChangeMenu());
    116131        }
    117132    }
    118133
    119     /**
     134        /**
    120135     * The toolbar buttons shall be active only when the PicLayer is active.
    121136     */
     137    @Override
    122138    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
    123139    }
     
    128144     * one must exist first). User should not be able to load a picture too early.
    129145     */
     146    @Override
    130147    public void layerAdded(Layer arg0) {
    131         m_newFromFileAction.setEnabled( true );
    132         m_newFromClipAction.setEnabled( true );
     148        menu.setEnabled(true);
    133149    }
    134150
     
    136152     * When all layers are gone - the menu is gone too.
    137153     */
     154    @Override
    138155    public void layerRemoved(Layer arg0) {
    139156        boolean enable = Main.map.mapView.getAllLayers().size() != 0;
    140         m_newFromFileAction.setEnabled( enable );
    141         m_newFromClipAction.setEnabled( enable );
     157        menu.setEnabled(enable);
    142158    }
    143159};
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/LoadPictureCalibrationAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
     
    2626import java.io.File;
    2727import java.io.FileInputStream;
    28 import java.io.FileNotFoundException;
    29 import java.io.FileOutputStream;
    30 import java.io.IOException;
    31 import java.util.Properties;
    3228
    3329import javax.swing.JFileChooser;
     
    3632import org.openstreetmap.josm.Main;
    3733import org.openstreetmap.josm.actions.JosmAction;
     34import org.openstreetmap.josm.plugins.piclayer.layer.CalibrationFileFilter;
     35import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerAbstract;
    3836
    3937/**
     
    4139 *
    4240 */
     41@SuppressWarnings("serial")
    4342public class LoadPictureCalibrationAction extends JosmAction {
    4443
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/SavePictureCalibrationAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
     
    2525import java.awt.event.ActionEvent;
    2626import java.io.File;
    27 import java.io.FileNotFoundException;
    2827import java.io.FileOutputStream;
    29 import java.io.IOException;
    3028import java.util.Properties;
    3129
     
    3533import org.openstreetmap.josm.Main;
    3634import org.openstreetmap.josm.actions.JosmAction;
     35import org.openstreetmap.josm.plugins.piclayer.layer.CalibrationFileFilter;
     36import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerAbstract;
    3737
    3838/**
     
    4141 * TODO Four almost identical classes. Refactoring needed.
    4242 */
     43@SuppressWarnings("serial")
    4344public class SavePictureCalibrationAction extends JosmAction {
    4445
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/newlayer/NewLayerFromClipboardAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions.newlayer;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
     
    2929import org.openstreetmap.josm.Main;
    3030import org.openstreetmap.josm.actions.JosmAction;
     31import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerFromClipboard;
    3132
    3233/**
     
    3435 * the content of the clipboard.
    3536 */
     37@SuppressWarnings("serial")
    3638public class NewLayerFromClipboardAction extends JosmAction {
    3739
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/newlayer/NewLayerFromFileAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions.newlayer;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
     
    3636import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    3737import org.openstreetmap.josm.gui.layer.Layer;
     38import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerFromFile;
    3839
    3940/**
    4041 * Action responsible for creation of new layers based on image files.
    4142 */
     43@SuppressWarnings("serial")
    4244public class NewLayerFromFileAction extends JosmAction {
    4345
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/MovePictureAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions.transform;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
    2424
    2525import java.awt.event.MouseEvent;
    26 import java.awt.event.MouseListener;
    27 import java.awt.event.MouseMotionListener;
    2826
    2927import org.openstreetmap.josm.Main;
    30 import org.openstreetmap.josm.actions.mapmode.MapMode;
    3128import org.openstreetmap.josm.gui.MapFrame;
     29import org.openstreetmap.josm.plugins.piclayer.actions.GenericPicTransformAction;
    3230import org.openstreetmap.josm.tools.ImageProvider;
    3331import org.openstreetmap.josm.data.coor.EastNorth;
    34 
    35 //TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3632
    3733/**
    3834 * This class handles the input during moving the picture.
    3935 */
    40 public class MovePictureAction extends MapMode implements MouseListener, MouseMotionListener
    41 {
    42     // Action ongoing?
    43     private boolean mb_dragging = false;
    44 
    45     // Last mouse position
    46     private EastNorth m_prevEastNorth;
    47 
    48     // The layer we're working on
    49     private PicLayerAbstract m_currentLayer = null;
     36@SuppressWarnings("serial")
     37public class MovePictureAction extends GenericPicTransformAction {
    5038
    5139    /**
     
    5644    }
    5745
    58     @Override
    59     public void enterMode() {
    60         super.enterMode();
    61         Main.map.mapView.addMouseListener(this);
    62         Main.map.mapView.addMouseMotionListener(this);
    63     }
    64 
    65     @Override
    66     public void exitMode() {
    67         super.exitMode();
    68         Main.map.mapView.removeMouseListener(this);
    69         Main.map.mapView.removeMouseMotionListener(this);
    70     }
    71 
    72     @Override
    73     public void mousePressed(MouseEvent e) {
    74 
    75         // If everything is OK, we start dragging/moving the picture
    76         if ( Main.map.mapView.getActiveLayer() instanceof PicLayerAbstract ) {
    77             m_currentLayer = (PicLayerAbstract)Main.map.mapView.getActiveLayer();
    78 
    79             if ( m_currentLayer != null && e.getButton() == MouseEvent.BUTTON1 ) {
    80                 mb_dragging = true;
    81                 m_prevEastNorth=Main.map.mapView.getEastNorth(e.getX(),e.getY());
    82             }
    83         }
    84     }
    85 
    86     @Override
    87     public void mouseDragged(MouseEvent e) {
    88         // Picture moving is ongoing
    89         if(mb_dragging) {
    90             EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
    91             m_currentLayer.movePictureBy(
    92                 eastNorth.east()-m_prevEastNorth.east(),
    93                 eastNorth.north()-m_prevEastNorth.north()
    94             );
    95             m_prevEastNorth = eastNorth;
    96             Main.map.mapView.repaint();
    97         }
    98     }
    99 
    100     @Override
    101     public void mouseReleased(MouseEvent e) {
    102         // Stop moving
    103         mb_dragging = false;
    104     }
     46        @Override
     47        protected void doAction(MouseEvent e) {
     48                EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
     49        currentLayer.movePictureBy(
     50            eastNorth.east() - prevEastNorth.east(),
     51            eastNorth.north() - prevEastNorth.north()
     52        );
     53        }
    10554
    10655}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/RotatePictureAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions.transform;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
    2424
     25import java.awt.event.InputEvent;
    2526import java.awt.event.MouseEvent;
    26 import java.awt.event.MouseListener;
    27 import java.awt.event.MouseMotionListener;
    2827
    2928import org.openstreetmap.josm.Main;
    30 import org.openstreetmap.josm.actions.mapmode.MapMode;
    3129import org.openstreetmap.josm.gui.MapFrame;
     30import org.openstreetmap.josm.plugins.piclayer.actions.GenericPicTransformAction;
    3231import org.openstreetmap.josm.tools.ImageProvider;
    33 
    34 //TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3532
    3633/**
    3734 * This class handles the input during rotating the picture.
    3835 */
    39 public class RotatePictureAction extends MapMode implements MouseListener, MouseMotionListener
    40 {
    41     // Action ongoing?
    42     private boolean mb_dragging = false;
    43 
    44     // Last mouse position
    45     private int m_prevY;
    46 
    47     // Layer we're working on
    48     private PicLayerAbstract m_currentLayer = null;
     36@SuppressWarnings("serial")
     37public class RotatePictureAction extends GenericPicTransformAction {
    4938
    5039    /**
     
    5342    public RotatePictureAction(MapFrame frame) {
    5443        super(tr("PicLayer rotate"), "rotate", tr("Drag to rotate the picture"), frame, ImageProvider.getCursor("crosshair", null));
    55         // TODO Auto-generated constructor stub
    5644    }
    5745
    58     @Override
    59     public void enterMode() {
    60         super.enterMode();
    61         Main.map.mapView.addMouseListener(this);
    62         Main.map.mapView.addMouseMotionListener(this);
    63     }
    64 
    65     @Override
    66     public void exitMode() {
    67         super.exitMode();
    68         Main.map.mapView.removeMouseListener(this);
    69         Main.map.mapView.removeMouseMotionListener(this);
    70     }
    71 
    72     @Override
    73     public void mousePressed(MouseEvent e) {
    74         // Start rotating
    75         if ( Main.map.mapView.getActiveLayer() instanceof PicLayerAbstract ) {
    76             m_currentLayer = (PicLayerAbstract)Main.map.mapView.getActiveLayer();
    77 
    78             if ( m_currentLayer != null && e.getButton() == MouseEvent.BUTTON1 ) {
    79                 mb_dragging = true;
    80                 m_prevY=e.getY();
    81             }
     46        @Override
     47        protected void doAction(MouseEvent e) {
     48                double factor;
     49        if ( ( e.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK ) != 0 ) {
     50            factor = Main.pref.getDouble("piclayer.rotatefactors.high_precision", 100.0);
    8251        }
    83     }
    84 
    85     @Override
    86     public void mouseDragged(MouseEvent e) {
    87         // Rotate the picture
    88         if(mb_dragging) {
    89             double factor;
    90             if ( ( e.getModifiersEx() & e.SHIFT_DOWN_MASK ) != 0 ) {
    91                 factor = Main.pref.getDouble("piclayer.rotatefactors.high_precision", 100.0);
    92             }
    93             else {
    94                 factor = Main.pref.getDouble("piclayer.rotatefactors.low_precision", 10.0 );
    95             }           
    96             m_currentLayer.rotatePictureBy( ( e.getY() - m_prevY ) / factor );
    97             m_prevY = e.getY();
    98             Main.map.mapView.repaint();
    99         }
    100     }
    101 
    102     @Override public void mouseReleased(MouseEvent e) {
    103         // End rotating
    104         mb_dragging = false;
    105     }
    106 
     52        else {
     53            factor = Main.pref.getDouble("piclayer.rotatefactors.low_precision", 10.0 );
     54        }           
     55        currentLayer.rotatePictureBy( ( e.getY() - prevMousePoint.getY() ) / factor );
     56        }
    10757}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ScalePictureActionAbstract.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions.transform;
    2222
     23import java.awt.event.InputEvent;
    2324import java.awt.event.MouseEvent;
    24 import java.awt.event.MouseListener;
    25 import java.awt.event.MouseMotionListener;
    2625
    2726import org.openstreetmap.josm.Main;
    28 import org.openstreetmap.josm.actions.mapmode.MapMode;
    2927import org.openstreetmap.josm.gui.MapFrame;
     28import org.openstreetmap.josm.plugins.piclayer.actions.GenericPicTransformAction;
    3029import org.openstreetmap.josm.tools.ImageProvider;
    31 
    32 // TODO: Move/Rotate/Scale action classes are similar. Do the redesign!
    3330
    3431/**
    3532 * This class handles the input during scaling the picture.
    3633 */
    37 public abstract class ScalePictureActionAbstract extends MapMode implements MouseListener, MouseMotionListener
    38 {
    39     // Scaling ongoing?
    40     private boolean mb_dragging = false;
     34@SuppressWarnings("serial")
     35public abstract class ScalePictureActionAbstract extends GenericPicTransformAction {
    4136
    42     // Last mouse position
    43     private int m_prevY;
    44 
    45     // Layer we're working on
    46     protected PicLayerAbstract m_currentLayer = null;
    47 
    48     /**
     37        /**
    4938     * Constructor
    5039     */
    5140    public ScalePictureActionAbstract (String name, String icon, String tooltip, MapFrame frame) {
    5241        super(name, icon, tooltip, frame, ImageProvider.getCursor("crosshair", null));
    53         // TODO Auto-generated constructor stub
    5442    }
    5543
    56     @Override
    57     public void enterMode() {
    58         super.enterMode();
    59         Main.map.mapView.addMouseListener(this);
    60         Main.map.mapView.addMouseMotionListener(this);
    61     }
    62 
    63     @Override
    64     public void exitMode() {
    65         super.exitMode();
    66         Main.map.mapView.removeMouseListener(this);
    67         Main.map.mapView.removeMouseMotionListener(this);
    68     }
    69 
    70     @Override
    71     public void mousePressed(MouseEvent e) {
    72         // Start scaling
    73         if ( Main.map.mapView.getActiveLayer() instanceof PicLayerAbstract ) {
    74             m_currentLayer = (PicLayerAbstract)Main.map.mapView.getActiveLayer();
    75 
    76             if ( m_currentLayer != null && e.getButton() == MouseEvent.BUTTON1 ) {
    77                 mb_dragging = true;
    78                 m_prevY = e.getY();
    79             }
     44    protected void doAction(MouseEvent e) {
     45        double factor;
     46        if ( ( e.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK ) != 0 ) {
     47            factor = Main.pref.getDouble("piclayer.scalefactors.high_precision", 1.0005);
    8048        }
    81     }
    82 
    83     @Override
    84     public void mouseDragged(MouseEvent e) {
    85         // Scale the picture
    86         if(mb_dragging) {
    87             double factor;
    88             if ( ( e.getModifiersEx() & e.SHIFT_DOWN_MASK ) != 0 ) {
    89                 factor = Main.pref.getDouble("piclayer.scalefactors.high_precision", 1.0005);
    90             }
    91             else {
    92                 factor = Main.pref.getDouble("piclayer.scalefactors.low_precision", 1.015);
    93             }           
    94             doTheScale( Math.pow(factor, m_prevY - e.getY() ) );
    95             m_prevY = e.getY();
    96             Main.map.mapView.repaint();
    97         }
    98     }
    99 
    100     @Override
    101     public void mouseReleased(MouseEvent e) {
    102         // Stop scaling
    103         mb_dragging = false;
     49        else {
     50            factor = Main.pref.getDouble("piclayer.scalefactors.low_precision", 1.015);
     51        }           
     52        doTheScale( Math.pow(factor, prevMousePoint.getY() - e.getY() ) );
    10453    }
    10554
    10655    /**
    107     * Does the actual scaling in the inherited class.
    108     */
    109     protected abstract void doTheScale( double scale );
     56     * Does the actual scaling in the inherited class.
     57     */
     58     protected abstract void doTheScale( double scale );
    11059
    11160}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ScaleXPictureAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions.transform;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
    2424
    25 import org.openstreetmap.josm.Main;
    26 import org.openstreetmap.josm.actions.mapmode.MapMode;
    2725import org.openstreetmap.josm.gui.MapFrame;
    28 import org.openstreetmap.josm.tools.ImageProvider;
    29 
    30 // TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3126
    3227/**
    3328 * This class handles the input during scaling the picture.
    3429 */
     30@SuppressWarnings("serial")
    3531public class ScaleXPictureAction extends ScalePictureActionAbstract
    3632{
     
    4036    public ScaleXPictureAction(MapFrame frame) {
    4137        super(tr("PicLayer scale X"), "scale_x", tr("Drag to scale the picture in the X Axis"), frame);
    42         // TODO Auto-generated constructor stub
    4338    }
    4439
    4540    public void doTheScale( double scale ) {
    46             m_currentLayer.scalePictureBy( scale, 1.0 );
     41            currentLayer.scalePictureBy( scale, 1.0 );
    4742        }
    4843}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ScaleXYPictureAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions.transform;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
    2424
    25 import org.openstreetmap.josm.Main;
    26 import org.openstreetmap.josm.actions.mapmode.MapMode;
    2725import org.openstreetmap.josm.gui.MapFrame;
    28 import org.openstreetmap.josm.tools.ImageProvider;
    29 
    30 // TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3126
    3227/**
    3328 * This class handles the input during scaling the picture.
    3429 */
     30@SuppressWarnings("serial")
    3531public class ScaleXYPictureAction extends ScalePictureActionAbstract
    3632{
     
    4036    public ScaleXYPictureAction(MapFrame frame) {
    4137        super(tr("PicLayer scale"), "scale", tr("Drag to scale the picture in the X and Y Axis"), frame);
    42         // TODO Auto-generated constructor stub
    4338    }
    4439
    4540    public void doTheScale( double scale ) {
    46             m_currentLayer.scalePictureBy( scale, scale );
     41            currentLayer.scalePictureBy( scale, scale );
    4742        }
    4843}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ScaleYPictureAction.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.actions.transform;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
    2424
    25 import org.openstreetmap.josm.Main;
    26 import org.openstreetmap.josm.actions.mapmode.MapMode;
    2725import org.openstreetmap.josm.gui.MapFrame;
    28 import org.openstreetmap.josm.tools.ImageProvider;
    29 
    30 // TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3126
    3227/**
    3328 * This class handles the input during scaling the picture.
    3429 */
     30@SuppressWarnings("serial")
    3531public class ScaleYPictureAction extends ScalePictureActionAbstract
    3632{
     
    4036    public ScaleYPictureAction(MapFrame frame) {
    4137        super(tr("PicLayer scale Y"), "scale_y", tr("Drag to scale the picture in the Y Axis"), frame);
    42         // TODO Auto-generated constructor stub
    4338    }
    4439
    4540    public void doTheScale( double scale ) {
    46             m_currentLayer.scalePictureBy( 1.0, scale );
    47         }
     41        currentLayer.scalePictureBy( 1.0, scale );
     42    }
    4843}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ShearPictureAction.java

    r27054 r27120  
    2121 ***************************************************************************/
    2222
    23 package org.openstreetmap.josm.plugins.piclayer;
     23package org.openstreetmap.josm.plugins.piclayer.actions.transform;
    2424
    2525import static org.openstreetmap.josm.tools.I18n.tr;
    2626
    2727import java.awt.event.MouseEvent;
    28 import java.awt.event.MouseListener;
    29 import java.awt.event.MouseMotionListener;
    3028
    3129import org.openstreetmap.josm.Main;
    32 import org.openstreetmap.josm.actions.mapmode.MapMode;
    3330import org.openstreetmap.josm.gui.MapFrame;
     31import org.openstreetmap.josm.plugins.piclayer.actions.GenericPicTransformAction;
    3432import org.openstreetmap.josm.tools.ImageProvider;
    3533import org.openstreetmap.josm.data.coor.EastNorth;
    36 
    37 //TODO: Move/Rotate/Scale/Shear action classes are similar. Do the redesign!
    3834
    3935/**
    4036 * This class handles the input during shearing of the picture.
    4137 */
    42 public class ShearPictureAction extends MapMode implements MouseListener, MouseMotionListener
    43 {
    44     // Action ongoing?
    45     private boolean mb_dragging = false;
     38@SuppressWarnings("serial")
     39public class ShearPictureAction extends GenericPicTransformAction {
    4640
    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     /**
     41        /**
    5442     * Constructor
    5543     */
     
    5846    }
    5947
    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     }
     48        @Override
     49        protected void doAction(MouseEvent e) {
     50        EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
     51        currentLayer.shearPictureBy(
     52            1000* (eastNorth.east() - prevEastNorth.east()),
     53            1000* (eastNorth.north() - prevEastNorth.north())
     54        );
     55        }
    10756
    10857}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/layer/CalibrationFileFilter.java

    r27054 r27120  
    1818 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
    1919 ***************************************************************************/
    20 package org.openstreetmap.josm.plugins.piclayer;
     20package org.openstreetmap.josm.plugins.piclayer.layer;
    2121
    2222import static org.openstreetmap.josm.tools.I18n.tr;
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerAbstract.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.layer;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
    2424
    2525import java.awt.Color;
    26 import java.awt.Component;
    2726import java.awt.Graphics2D;
    2827import java.awt.Image;
     28import java.awt.Point;
    2929import java.awt.Toolkit;
    30 import java.awt.event.ActionEvent;
     30import java.awt.geom.AffineTransform;
     31import java.awt.geom.NoninvertibleTransformException;
     32import java.awt.geom.Point2D;
    3133import java.io.BufferedReader;
    32 import java.io.File;
    33 import java.io.FileInputStream;
    34 import java.io.FileReader;
    3534import java.io.IOException;
    3635import java.io.InputStream;
    3736import java.io.InputStreamReader;
    3837import java.io.Reader;
    39 import java.util.List;
    4038import java.util.Properties;
    4139
    42 import javax.swing.AbstractAction;
    4340import javax.swing.Action;
    4441import javax.swing.Icon;
    4542import javax.swing.ImageIcon;
    46 import javax.swing.JMenu;
    4743
    4844import org.openstreetmap.josm.Main;
     
    5450import org.openstreetmap.josm.gui.MapView;
    5551import org.openstreetmap.josm.gui.layer.Layer;
     52import org.openstreetmap.josm.plugins.piclayer.actions.LoadPictureCalibrationAction;
     53import org.openstreetmap.josm.plugins.piclayer.actions.ResetCalibrationAction;
     54import org.openstreetmap.josm.plugins.piclayer.actions.SavePictureCalibrationAction;
     55import org.openstreetmap.josm.plugins.piclayer.transform.PictureTransform;
    5656import org.openstreetmap.josm.tools.Utils;
    5757
     
    6262 * anything...)
    6363 */
    64 public abstract class PicLayerAbstract extends Layer
    65 {
     64public abstract class PicLayerAbstract extends Layer {
    6665    // Counter - just for naming of layers
    67     private static int m_counter = 0;
     66    private static int imageCounter = 0;
    6867    // This is the main image to be displayed
    69     private Image m_image = null;
     68    private Image image = null;
     69    private static Image pinImage;
    7070    // Initial position of the image in the real world
    71     private EastNorth m_initial_position;
     71    private EastNorth initialImagePosition;
    7272    // Position of the image in the real world
    73     private EastNorth m_position;
    74     // Angle of rotation of the image
    75     private double m_angle = 0.0;
    76     // Scale of the image
    77     private double m_scalex = 1.0;
    78     private double m_scaley = 1.0;
    79     // Shear of the image
    80     private double m_shearx = 0.0;
    81     private double m_sheary = 0.0;
     73    private EastNorth imagePosition;
    8274    // The scale that was set on the map during image creation
    83     private double m_initial_scale = 1.0;
     75    private double initialImageScale = 1.0;
    8476    // Layer icon
    85     private Icon m_layericon = null;
     77    private Icon layerIcon = null;
     78
     79    private PictureTransform transformer;
     80
     81    public PictureTransform getTransformer() {
     82        return transformer;
     83    }
    8684
    8785    // Keys for saving in Properties
     
    9795    private final String SHEARY = "SHEARY";
    9896
     97    private final String MATRIXm00 = "M00";
     98    private final String MATRIXm01 = "M01";
     99    private final String MATRIXm10 = "M10";
     100    private final String MATRIXm11 = "M11";
     101    private final String MATRIXm02 = "M02";
     102    private final String MATRIXm12 = "M12";
     103
    99104    /**
    100105     * Constructor
    101106     */
    102107    public PicLayerAbstract() {
    103         super("PicLayer #" + m_counter);
     108        super("PicLayer #" + imageCounter);
    104109
    105110        //Increase number
    106         m_counter++;
     111        imageCounter++;
    107112
    108113        // Load layer icon
    109         m_layericon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(PicLayerAbstract.class.getResource("/images/layericon.png")));
     114        layerIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/layericon.png")));
     115
     116        if (pinImage == null) {
     117            // allow system to load the image and use it in future
     118            pinImage = new ImageIcon(Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/arrow.png"))).getImage();
     119        }
    110120    }
    111121
     
    116126    public void initialize() throws IOException {
    117127        // First, we initialize the calibration, so that createImage() can rely on it
    118        
     128
    119129        // If the map does not exist - we're screwed. We should not get into this situation in the first place!
    120130        if ( Main.map != null && Main.map.mapView != null ) {
    121             // Geographical position of the image
    122             // getCenter() documentation claims it returns a clone, but this is not in line with the code,
    123             // which actually returns the same object upon subsequent calls. This messes things up
    124             // when we loading several pictures and associated cal's in one go.
    125             // So as a workaround, copy the object manually :
    126             // TODO: not sure about this code below, probably there is a better way to clone the objects
     131
    127132            EastNorth center = Main.map.mapView.getCenter();
    128             m_initial_position = new EastNorth(center.east(), center.north());
    129             m_position = new EastNorth(center.east(), center.north());
     133
     134            imagePosition = new EastNorth(center.east(), center.north());
     135            initialImagePosition = new EastNorth(imagePosition.east(), imagePosition.north());
    130136            // Initial scale at which the image was loaded
    131             m_initial_scale = Main.map.mapView.getDist100Pixel();
     137            initialImageScale = Main.map.mapView.getDist100Pixel();
    132138        } else {
    133139            throw new IOException(tr("Could not find the map object."));
     
    135141
    136142        // Create image
    137         m_image = createImage();
    138         if ( m_image == null ) {
     143        image = createImage();
     144        if ( image == null ) {
    139145            throw new IOException(tr("PicLayer failed to load or import the image."));
    140146        }
    141147        // Load image completely
    142         (new ImageIcon(m_image)).getImage();
    143        
     148        (new ImageIcon(image)).getImage();
     149
     150        transformer = new PictureTransform();
     151
    144152        lookForCalibration();
    145153    }
     
    157165     * To be overridden by subclasses. Returns the user readable name of the layer.
    158166     */
    159     protected abstract String getPicLayerName();
    160 
    161     @Override
    162     public Icon getIcon() {
    163         return m_layericon;
    164     }
    165 
    166     @Override
    167     public Object getInfoComponent() {
    168         // TODO Auto-generated method stub
    169         return null;
    170     }
     167    public abstract String getPicLayerName();
     168
     169    @Override
     170    public Icon getIcon() { return layerIcon; }
     171
     172    @Override
     173    public Object getInfoComponent() { return null; }
    171174
    172175    @Override
     
    174177        // Main menu
    175178        return new Action[] {
    176                 new ResetSubmenuAction(),
     179                new ResetCalibrationAction(this, transformer),
    177180                SeparatorLayerAction.INSTANCE,
    178181                new SavePictureCalibrationAction(this),
    179182                new LoadPictureCalibrationAction(this),
    180183                SeparatorLayerAction.INSTANCE,
    181                 new RenameLayerAction(null,this)
     184                new RenameLayerAction(null,this),
    182185        };
    183186    }
     
    190193    @Override
    191194    public boolean isMergable(Layer arg0) {
    192         // TODO Auto-generated method stub
    193195        return false;
    194196    }
    195197
    196198    @Override
    197     public void mergeFrom(Layer arg0) {
    198         // TODO Auto-generated method stub
    199 
    200     }
     199    public void mergeFrom(Layer arg0) {}
    201200
    202201    @Override
    203202    public void paint(Graphics2D g2, MapView mv, Bounds bounds) {
    204203
    205         if ( m_image != null) {
     204        if ( image != null) {
    206205
    207206            // Position image at the right graphical place
     
    213212
    214213            //     This is now the offset in screen pixels
    215             double pic_offset_x = (( m_position.east() - leftop.east() ) * pixel_per_en);
    216             double pic_offset_y = (( leftop.north() - m_position.north() ) * pixel_per_en);
     214            double pic_offset_x = (( imagePosition.east() - leftop.east() ) * pixel_per_en);
     215            double pic_offset_y = (( leftop.north() - imagePosition.north() ) * pixel_per_en);
    217216
    218217            Graphics2D g = (Graphics2D)g2.create();
    219218            // Move
    220219            g.translate( pic_offset_x, pic_offset_y );
    221             // Rotate
    222             g.rotate( m_angle * Math.PI / 180.0 );
     220
    223221            // Scale
    224             double scalex = m_scalex * m_initial_scale * pixel_per_en / getMetersPerEasting(m_position) / 100;
    225             double scaley = m_scaley * m_initial_scale * pixel_per_en / getMetersPerNorthing(m_position) / 100;
     222            double scalex = initialImageScale * pixel_per_en / getMetersPerEasting(imagePosition) / 100;
     223            double scaley = initialImageScale * pixel_per_en / getMetersPerNorthing(imagePosition) / 100;
    226224            g.scale( scalex, scaley );
    227             // Shear
    228             g.shear(m_shearx, m_sheary);
     225
     226            g.transform(transformer.getTransform());
    229227
    230228            // Draw picture
    231             g.drawImage( m_image, -m_image.getWidth(null) / 2, -m_image.getHeight(null) / 2, null );
     229            g.drawImage( image, -image.getWidth(null) / 2, -image.getHeight(null) / 2, null );
    232230
    233231            // Draw additional rectangle for the active pic layer
     
    235233                g.setColor( new Color( 0xFF0000 ) );
    236234                g.drawRect(
    237                     -m_image.getWidth(null) / 2,
    238                     -m_image.getHeight(null) / 2,
    239                     m_image.getWidth(null),
    240                     m_image.getHeight(null)
     235                    -image.getWidth(null) / 2,
     236                    -image.getHeight(null) / 2,
     237                    image.getWidth(null),
     238                    image.getHeight(null)
    241239                );
    242240            }
     241            // draw points for selection
     242            Graphics2D gPoints = (Graphics2D)g2.create();
     243
     244            gPoints.translate(pic_offset_x, pic_offset_y);
     245
     246            gPoints.setColor(Color.RED); // red color for points output
     247
     248            AffineTransform tr = AffineTransform.getScaleInstance(scalex, scaley);
     249            tr.concatenate(transformer.getTransform());
     250
     251            for (Point2D p : transformer.getOriginPoints()) {
     252               Point2D trP = tr.transform(p, null);
     253               int x = (int)trP.getX(), y = (int)trP.getY();
     254               gPoints.drawOval(x-2, y-2, 5, 5);
     255               gPoints.drawImage(pinImage, x, y, null);
     256            }
     257
    243258        } else {
    244259            // TODO: proper logging
    245             System.out.println( "PicLayerAbstract::paint - general drawing error (m_image is null or Graphics not 2D" );
    246         }
    247     }
    248    
     260            System.out.println( "PicLayerAbstract::paint - general drawing error (image is null or Graphics not 2D" );
     261        }
     262    }
     263
    249264    /**
    250265     * Returns the distance in meter, that corresponds to one unit in east north
    251      * space. For normal projections, it is about 1 (but usually changing with 
     266     * space. For normal projections, it is about 1 (but usually changing with
    252267     * latitude).
    253      * For EPSG:4326, it is the distance from one meridian of full degree to the 
     268     * For EPSG:4326, it is the distance from one meridian of full degree to the
    254269     * next (a couple of kilometers).
    255270     */
     
    259274         * a shift of that size in east north space without
    260275         * going out of bounds.
    261          * 
     276         *
    262277         * Also, this should get us somewhere in the range of meters,
    263278         * so we get the result at the point 'en' and not some average.
     
    265280        double naturalScale = Main.getProjection().getDefaultZoomInPPD();
    266281        naturalScale *= 0.01; // make a little smaller
    267        
     282
    268283        LatLon ll1 = Main.getProjection().eastNorth2latlon(
    269284                new EastNorth(en.east() - naturalScale, en.north()));
    270285        LatLon ll2 = Main.getProjection().eastNorth2latlon(
    271286                new EastNorth(en.east() + naturalScale, en.north()));
    272        
     287
    273288        double dist = ll1.greatCircleDistance(ll2) / naturalScale / 2;
    274289        return dist;
     
    279294        double naturalScale = Main.getProjection().getDefaultZoomInPPD();
    280295        naturalScale *= 0.01;
    281        
     296
    282297        LatLon ll1 = Main.getProjection().eastNorth2latlon(
    283298                new EastNorth(en.east(), en.north()- naturalScale));
    284299        LatLon ll2 = Main.getProjection().eastNorth2latlon(
    285300                new EastNorth(en.east(), en.north() + naturalScale));
    286        
     301
    287302        double dist = ll1.greatCircleDistance(ll2) / naturalScale / 2;
    288303        return dist;
    289304    }
    290    
    291     /**
    292      * Moves the picture. Scaled in EastNorth...
    293      */
    294     public void movePictureBy( double east, double north ) {
    295         m_position = m_position.add( east, north );
    296     }
    297 
    298     /**
    299      * Scales the picture. scalex and scaley will multiply the current factor
    300      */
    301     public void scalePictureBy( double scalex, double scaley ) {
    302         m_scalex *= scalex;
    303         m_scaley *= scaley;
    304     }
    305 
    306     /**
    307      * Rotates the picture. Scales in angles.
    308      */
    309     public void rotatePictureBy( double angle ) {
    310         m_angle += angle;
    311     }
    312 
    313     /**
    314      * Shear the picture. shearx and sheary will separately add to the
    315      * corresponding current value
    316      */
    317     public void shearPictureBy( double shearx, double sheary ) {
    318         m_shearx += shearx;
    319         m_sheary += sheary;
    320     }
    321 
    322     /**
    323      * Sets the image position to the initial position
    324      */
    325     public void resetPosition() {
    326         m_position = m_initial_position;
    327     }
    328 
    329     /**
    330      * Sets the image scale to 1.0
    331      */
    332     public void resetScale() {
    333         m_scalex = 1.0;
    334         m_scaley = 1.0;
    335     }
    336 
    337     /**
    338      * Sets the image angle to 0.0
    339      */
    340     public void resetAngle() {
    341         m_angle = 0.0;
    342     }
    343 
    344 
    345     /**
    346      * Sets the image to no shear
    347      */
    348     public void resetShear() {
    349         m_shearx = 0.0;
    350         m_sheary = 0.0;
    351     }
     305
    352306    @Override
    353307    /**
     
    357311     */
    358312    public void visitBoundingBox(BoundingXYVisitor arg0) {
    359         if ( m_image == null )
     313        if ( image == null )
    360314            return;
    361315        String projcode = Main.getProjection().toCode();
    362316
    363317        // TODO: bounding box only supported when coordinates are in meters
    364         // The reason for that is that this .cal think makes us a hard time. 
    365         // The position is stored as a raw data (can be either in degrees or 
    366         // in meters, depending on the projection used at creation), but the 
     318        // The reason for that is that this .cal think makes us a hard time.
     319        // The position is stored as a raw data (can be either in degrees or
     320        // in meters, depending on the projection used at creation), but the
    367321        // initial scale is in m/100pix
    368322        // So for now, we support the bounding box only when everything is in meters
    369323        if (projcode.equals("EPSG:4326") )
    370324            return;
    371            
    372         EastNorth center = m_position;
    373         double w = m_image.getWidth(null);
    374         double h = m_image.getHeight(null);
     325
     326        EastNorth center = imagePosition;
     327        double w = image.getWidth(null);
     328        double h = image.getHeight(null);
    375329        double diag_pix = Math.sqrt(w*w+h*h);
    376        
    377         // m_initial_scale is a the scale (unit: m/100pix) at creation time
    378         double diag_m = (diag_pix/100) * m_initial_scale;
    379        
    380         double factor = Math.max(m_scalex, m_scaley);
     330
     331        // initialImageScale is a the scale (unit: m/100pix) at creation time
     332        double diag_m = (diag_pix/100) * initialImageScale;
     333
     334        AffineTransform trans = transformer.getTransform();
     335        double factor = Math.max(trans.getScaleX(), trans.getScaleY());
     336
    381337        double offset = factor * diag_m / 2.0;
    382338
     
    393349    public void saveCalibration( Properties props ) {
    394350        // Save
    395         props.put(INITIAL_POS_X, "" + m_initial_position.getX());
    396         props.put(INITIAL_POS_Y, "" + m_initial_position.getY());
    397         props.put(POSITION_X, "" + m_position.getX());
    398         props.put(POSITION_Y, "" + m_position.getY());
    399         props.put(INITIAL_SCALE, "" + m_initial_scale);
    400         props.put(SCALEX, "" + m_scalex);
    401         props.put(SCALEY, "" + m_scaley);
    402         props.put(ANGLE, "" + m_angle);
    403         props.put(SHEARX, "" + m_shearx);
    404         props.put(SHEARY, "" + m_sheary);
     351        double[] matrix = new double[6];
     352        transformer.getTransform().getMatrix(matrix);
     353
     354        props.put(MATRIXm00, Double.toString(matrix[0]));
     355        props.put(MATRIXm01, Double.toString(matrix[1]));
     356        props.put(MATRIXm10, Double.toString(matrix[2]));
     357        props.put(MATRIXm11, Double.toString(matrix[3]));
     358        props.put(MATRIXm02, Double.toString(matrix[4]));
     359        props.put(MATRIXm12, Double.toString(matrix[5]));
     360        props.put(POSITION_X, Double.toString(imagePosition.getX()));
     361        props.put(POSITION_Y, Double.toString(imagePosition.getY()));
    405362    }
    406363
     
    423380    public void loadCalibration( Properties props ) {
    424381        // Load
    425             double pos_x = Double.valueOf( props.getProperty(POSITION_X));
    426             double pos_y = Double.valueOf( props.getProperty(POSITION_Y));
     382
     383        AffineTransform transform;
     384
     385        if (props.containsKey(INITIAL_POS_X)) {// old format
    427386            double in_pos_x = Double.valueOf( props.getProperty(INITIAL_POS_X));
    428387            double in_pos_y = Double.valueOf( props.getProperty(INITIAL_POS_Y));
     
    433392            double shear_x = Double.valueOf( props.getProperty(SHEARX));
    434393            double shear_y = Double.valueOf( props.getProperty(SHEARY));
    435             m_position.setLocation(pos_x, pos_y);
    436             m_initial_position.setLocation(pos_x, pos_y);
    437             m_angle = angle;
    438             m_scalex = scale_x;
    439             m_scaley = scale_y;
    440             m_shearx = shear_x;
    441             m_sheary = shear_y;
    442             m_initial_scale = in_scale;
    443             // Refresh
    444             Main.map.mapView.repaint();
    445     }
    446    
     394
     395            initialImagePosition.setLocation(in_pos_x, in_pos_y);
     396
     397            // transform to matrix from these values - need testing
     398            transform = AffineTransform.getRotateInstance(angle);
     399            transform.scale(scale_x, scale_y);
     400            transform.shear(shear_x, shear_y);
     401            initialImageScale = in_scale;
     402        } else {
     403            double pos_x = Double.valueOf( props.getProperty(POSITION_X));
     404            double pos_y = Double.valueOf( props.getProperty(POSITION_Y));
     405            imagePosition = new EastNorth(pos_x, pos_y);
     406            initialImageScale = 1; //in_scale
     407
     408            // initialize matrix
     409            double[] matrix = new double[6];
     410            matrix[0] = Double.parseDouble(props.getProperty(MATRIXm00));
     411            matrix[1] = Double.parseDouble(props.getProperty(MATRIXm01));
     412            matrix[2] = Double.parseDouble(props.getProperty(MATRIXm10));
     413            matrix[3] = Double.parseDouble(props.getProperty(MATRIXm11));
     414            matrix[4] = Double.parseDouble(props.getProperty(MATRIXm02));
     415            matrix[5] = Double.parseDouble(props.getProperty(MATRIXm12));
     416
     417            transform = new AffineTransform(matrix);
     418        }
     419        transformer.resetCalibration();
     420        transformer.getTransform().concatenate(transform);
     421
     422        // Refresh
     423        Main.map.mapView.repaint();
     424    }
     425
    447426    public void loadWorldfile(InputStream is) throws IOException {
    448427        BufferedReader br = null;
     
    456435            }
    457436            double sx=e[0], ry=e[1], rx=e[2], sy=e[3], dx=e[4], dy=e[5];
    458             int w = m_image.getWidth(null);
    459             int h = m_image.getHeight(null);
    460             m_position.setLocation(
    461                     dx + w/2*sx + h/2*rx, 
     437            int w = image.getWidth(null);
     438            int h = image.getHeight(null);
     439            imagePosition.setLocation(
     440                    dx + w/2*sx + h/2*rx,
    462441                    dy + w/2*ry + h/2*sy
    463442            );
    464             m_initial_position.setLocation(m_position);
    465             m_angle = 0;
    466             m_scalex = 100*sx*getMetersPerEasting(m_position);
    467             m_scaley = -100*sy*getMetersPerNorthing(m_position);
    468             m_shearx = rx / sx;
    469             m_sheary = ry / sy;
    470             m_initial_scale = 1;
     443            initialImagePosition.setLocation(imagePosition);
     444//            m_angle = 0;
     445            double scalex = 100*sx*getMetersPerEasting(imagePosition);
     446            double scaley = -100*sy*getMetersPerNorthing(imagePosition);
     447            double shearx = rx / sx;
     448            double sheary = ry / sy;
     449
     450            transformer.resetCalibration();
     451            AffineTransform tr = transformer.getTransform();
     452            tr.scale(scalex, scaley);
     453            tr.shear(shearx, sheary);
     454
     455            initialImageScale = 1;
    471456            Main.map.mapView.repaint();
    472457        } finally {
     
    475460    }
    476461
    477     private class ResetSubmenuAction extends AbstractAction implements LayerAction {
    478 
    479         public ResetSubmenuAction() {
    480             super(tr("Reset"));
    481         }
    482 
    483         public void actionPerformed(ActionEvent e) {
    484         }
    485 
    486         public Component createMenuComponent() {
    487             JMenu reset_submenu = new JMenu(this);
    488             reset_submenu.add( new ResetPictureAllAction( PicLayerAbstract.this ) );
    489             reset_submenu.addSeparator();
    490             reset_submenu.add( new ResetPicturePositionAction( PicLayerAbstract.this ) );
    491             reset_submenu.add( new ResetPictureAngleAction( PicLayerAbstract.this ) );
    492             reset_submenu.add( new ResetPictureScaleAction( PicLayerAbstract.this ) );
    493             reset_submenu.add( new ResetPictureShearAction( PicLayerAbstract.this ) );
    494             return reset_submenu;
    495         }
    496 
    497         public boolean supportLayers(List<Layer> layers) {
    498             return layers.size() == 1 && layers.get(0) instanceof PicLayerAbstract;
    499         }
    500 
     462    public Point2D transformPoint(Point p) throws NoninvertibleTransformException {
     463        // Position image at the right graphical place
     464
     465        EastNorth center = Main.map.mapView.getCenter();
     466        EastNorth leftop = Main.map.mapView.getEastNorth( 0, 0 );
     467        // Number of pixels for one unit in east north space.
     468        // This is the same in x- and y- direction.
     469        double pixel_per_en = ( Main.map.mapView.getWidth() / 2.0 ) / ( center.east() - leftop.east() );
     470
     471        //     This is now the offset in screen pixels
     472        double pic_offset_x = (( imagePosition.east() - leftop.east() ) * pixel_per_en);
     473        double pic_offset_y = (( leftop.north() - imagePosition.north() ) * pixel_per_en); // something bad...
     474
     475        AffineTransform pointTrans = AffineTransform.getTranslateInstance(pic_offset_x, pic_offset_y);
     476
     477        double scalex = initialImageScale * pixel_per_en / getMetersPerEasting(imagePosition) / 100;
     478        double scaley = initialImageScale * pixel_per_en / getMetersPerNorthing(imagePosition) / 100;
     479
     480        pointTrans.scale(scalex, scaley); // ok here
     481
     482        pointTrans.concatenate(transformer.getTransform());
     483
     484        Point2D result = pointTrans.inverseTransform(p, null);
     485        return result;
     486    }
     487
     488    /**
     489     * Moves the picture. Scaled in EastNorth...
     490     */
     491    public void movePictureBy(double x, double y) {
     492        imagePosition = imagePosition.add(x, y);
     493    }
     494
     495    public void rotatePictureBy(double angle) {
     496        transformer.concatenateTransform(AffineTransform.getRotateInstance(angle));
     497    }
     498
     499    public void scalePictureBy(double scalex, double scaley) {
     500        transformer.concatenateTransform(AffineTransform.getScaleInstance(scalex, scaley));
     501    }
     502
     503    public void shearPictureBy(double shx, double shy) {
     504        transformer.concatenateTransform(AffineTransform.getShearInstance(shx, shy));
     505    }
     506
     507    public void resetCalibration() {
     508        transformer.resetCalibration();
     509        imagePosition.setLocation(initialImagePosition);
     510    }
     511
     512    // get image coordinates by mouse coords
     513    public Point2D findSelectedPoint(Point point) {
     514        if (image == null)
     515            return null;
     516
     517        try {
     518            Point2D pressed = transformPoint(point);
     519            for (Point2D p : transformer.getOriginPoints())
     520                if (p.distance(pressed) <= 4.0) // if user clicked to select some of origin point
     521                    return p;
     522        } catch (NoninvertibleTransformException e) {
     523            e.printStackTrace();
     524        }
     525        return null;
    501526    }
    502527}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerFromClipboard.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.layer;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
     
    6262
    6363    @Override
    64     protected String getPicLayerName() {
     64    public String getPicLayerName() {
    6565        return "Clipboard";
    6666    }
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerFromFile.java

    r27054 r27120  
    1919 ***************************************************************************/
    2020
    21 package org.openstreetmap.josm.plugins.piclayer;
     21package org.openstreetmap.josm.plugins.piclayer.layer;
    2222
    2323import static org.openstreetmap.josm.tools.I18n.tr;
     
    263263
    264264    @Override
    265     protected String getPicLayerName() {
     265    public String getPicLayerName() {
    266266        return m_tooltiptext;
    267267    }
Note: See TracChangeset for help on using the changeset viewer.