Created
          September 7, 2012 21:48 
        
      - 
      
- 
        Save ulmangt/3669999 to your computer and use it in GitHub Desktop. 
    A Glimpse example demonstrating dragging IconPainter icons
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import java.io.IOException; | |
| import java.util.Collection; | |
| import com.metsci.glimpse.axis.Axis1D; | |
| import com.metsci.glimpse.axis.Axis2D; | |
| import com.metsci.glimpse.event.mouse.GlimpseMouseAdapter; | |
| import com.metsci.glimpse.event.mouse.GlimpseMouseEvent; | |
| import com.metsci.glimpse.event.mouse.GlimpseMouseMotionListener; | |
| import com.metsci.glimpse.examples.Example; | |
| import com.metsci.glimpse.examples.basic.HeatMapExample; | |
| import com.metsci.glimpse.examples.icon.TextureAtlasExample; | |
| import com.metsci.glimpse.layout.GlimpseLayoutProvider; | |
| import com.metsci.glimpse.plot.ColorAxisPlot2D; | |
| import com.metsci.glimpse.support.atlas.TextureAtlas; | |
| import com.metsci.glimpse.support.atlas.painter.IconPainter; | |
| import com.metsci.glimpse.support.atlas.painter.IconPainter.PickResult; | |
| import com.metsci.glimpse.support.selection.SpatialSelectionListener; | |
| public class ClickDragExample implements GlimpseLayoutProvider | |
| { | |
| public static void main( String[] args ) throws Exception | |
| { | |
| example = Example.showWithSwing( new ClickDragExample( ) ); | |
| } | |
| protected static Example example; | |
| protected boolean dragInProgress; | |
| protected PickResult lastPickResult; | |
| @Override | |
| public ColorAxisPlot2D getLayout( ) throws IOException | |
| { | |
| // start with HeatMapExample, which uses a ShadedTexturePainter | |
| final ColorAxisPlot2D plot = new HeatMapExample( ).getLayout( ); | |
| // don't have any absolute minimum constraints on the x axis | |
| plot.getAxisY( ).setAbsoluteMin( -Double.MAX_VALUE ); | |
| plot.getAxisY( ).validate( ); | |
| // create a texture atlas (contains icons to draw efficiently to the screen) | |
| final TextureAtlas atlas = new TextureAtlas( 256, 256 ); | |
| // load the atlas with some example images | |
| TextureAtlasExample.loadTextureAtlas( atlas ); | |
| // create a painter for drawing icons | |
| final IconPainter iconPainter = new IconPainter( ); | |
| // enable picking on the IconPainter for the plots center drawing area | |
| // this will allow the SpatialSelectionListener added later to function | |
| iconPainter.setPickingEnabled( plot.getLayoutCenter( ) ); | |
| // add the painter to the plot | |
| plot.addPainter( iconPainter ); | |
| // tell the painter to use the texture atlas we created | |
| // for the set of icons we arbitrarily designate "group1" | |
| iconPainter.addIconGroup( "group1", atlas ); | |
| iconPainter.addIconGroup( "group2", atlas ); | |
| // add the icon with id "glimpse" at position ( 0.0, 0.0 ) in axis coordinates | |
| // the icon "glimpse" was loaded/defined by the TextureAtlasExample.loadTextureAtlas( ) call | |
| iconPainter.addIcon( "group1", "glimpse", 0.0f, 0.0f, 0.0f, 0.4f ); | |
| iconPainter.addIcon( "group2", "glimpse", 100.0f, 100.0f, 0.0f, 0.4f ); | |
| // add a listener which will report the icon under the mouse and save it to a field | |
| iconPainter.addSpatialSelectionListener( new SpatialSelectionListener<PickResult>( ) | |
| { | |
| @Override | |
| public void selectionChanged( Collection<PickResult> newSelectedIcons ) | |
| { | |
| if ( newSelectedIcons.isEmpty( ) ) | |
| { | |
| if ( !dragInProgress ) lastPickResult = null; | |
| } | |
| else | |
| { | |
| lastPickResult = newSelectedIcons.iterator( ).next( ); | |
| } | |
| } | |
| } ); | |
| // add a listener which reports when the mouse moves | |
| // if the mouse button is held down under an icon, the icon is moved | |
| // so that it appears as if it is being dragged by the mouse | |
| plot.addGlimpseMouseMotionListener( new GlimpseMouseMotionListener( ) | |
| { | |
| @Override | |
| public void mouseMoved( final GlimpseMouseEvent e ) | |
| { | |
| // if a button is being held down and the mouse is over an icon | |
| // then drag the icon instead of translating the plot | |
| if ( e.isAnyButtonDown( ) && lastPickResult != null ) | |
| { | |
| // do this on the repaint thread to avoid the possibility | |
| // of the icon appearing to flicker | |
| example.getManager( ).asyncExec( new Runnable( ) | |
| { | |
| @Override | |
| public void run( ) | |
| { | |
| Axis2D axis = e.getAxis2D( ); | |
| // get the coordinates of the mouse event in axis coordinates ( e.getX( ) returns pixel coordinates ) | |
| double x = axis.getAxisX( ).screenPixelToValue( e.getX( ) ); | |
| double y = axis.getAxisY( ).screenPixelToValue( axis.getAxisY( ).getSizePixels( ) - e.getY( ) ); | |
| // icon painter does not currently provide a way to change the position of an icon | |
| // so we have to remove the group and recreate it | |
| iconPainter.removeIconGroup( lastPickResult.getGroupId( ) ); | |
| iconPainter.addIconGroup( lastPickResult.getGroupId( ), atlas ); | |
| iconPainter.addIcon( lastPickResult.getGroupId( ), lastPickResult.getIconId( ), ( float ) x, ( float ) y, 0.0f, 0.4f ); | |
| } | |
| } ); | |
| } | |
| } | |
| } ); | |
| // add another mouse listener which reports when the mouse button is pressed | |
| // this is used to lock the axes if the mouse is under an icon | |
| plot.addGlimpseMouseListener( new GlimpseMouseAdapter( ) | |
| { | |
| @Override | |
| public void mousePressed( GlimpseMouseEvent event ) | |
| { | |
| // if an icon is selected, lock the axes so that the icon can | |
| // be dragged around without translating the plot | |
| if ( lastPickResult != null ) | |
| { | |
| dragInProgress = true; | |
| setAxisLocked( plot.getAxis( ), true ); | |
| } | |
| } | |
| @Override | |
| public void mouseReleased( GlimpseMouseEvent event ) | |
| { | |
| dragInProgress = false; | |
| setAxisLocked( plot.getAxis( ), false ); | |
| } | |
| } ); | |
| return plot; | |
| } | |
| // helper function to lock / unlock an Axis2D | |
| protected void setAxisLocked( Axis2D axis, boolean locked ) | |
| { | |
| if ( locked ) | |
| { | |
| Axis1D axisX = axis.getAxisX( ); | |
| axisX.lockMax( axisX.getMax( ) ); | |
| axisX.lockMin( axisX.getMin( ) ); | |
| Axis1D axisY = axis.getAxisY( ); | |
| axisY.lockMax( axisY.getMax( ) ); | |
| axisY.lockMin( axisY.getMin( ) ); | |
| } | |
| else | |
| { | |
| Axis1D axisX = axis.getAxisX( ); | |
| axisX.unlockMax( ); | |
| axisX.unlockMin( ); | |
| Axis1D axisY = axis.getAxisY( ); | |
| axisY.unlockMax( ); | |
| axisY.unlockMin( ); | |
| } | |
| axis.getAxisX( ).validate( ); | |
| axis.getAxisY( ).validate( ); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment