Created
          March 18, 2012 06:40 
        
      - 
      
- 
        Save ulmangt/2069421 to your computer and use it in GitHub Desktop. 
    StackedTimePlot2D Overlay Layout and GlimpseAxisLayout1D TextureAtlas Example
  
        
  
    
      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
    
  
  
    
  | /* | |
| * Copyright (c) 2012, Metron, Inc. | |
| * All rights reserved. | |
| * | |
| * Redistribution and use in source and binary forms, with or without | |
| * modification, are permitted provided that the following conditions are met: | |
| * * Redistributions of source code must retain the above copyright | |
| * notice, this list of conditions and the following disclaimer. | |
| * * Redistributions in binary form must reproduce the above copyright | |
| * notice, this list of conditions and the following disclaimer in the | |
| * documentation and/or other materials provided with the distribution. | |
| * * Neither the name of Metron, Inc. nor the | |
| * names of its contributors may be used to endorse or promote products | |
| * derived from this software without specific prior written permission. | |
| * | |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| * DISCLAIMED. IN NO EVENT SHALL METRON, INC. BE LIABLE FOR ANY | |
| * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| package com.metsci.glimpse.examples.stacked; | |
| import java.io.IOException; | |
| import javax.media.opengl.GL; | |
| import com.metsci.glimpse.axis.Axis1D; | |
| import com.metsci.glimpse.context.GlimpseBounds; | |
| import com.metsci.glimpse.examples.Example; | |
| import com.metsci.glimpse.examples.icon.TextureAtlasExample; | |
| import com.metsci.glimpse.layout.GlimpseLayout; | |
| import com.metsci.glimpse.painter.base.GlimpseDataPainter1D; | |
| import com.metsci.glimpse.plot.timeline.StackedTimePlot2D; | |
| import com.metsci.glimpse.plot.timeline.data.Epoch; | |
| import com.metsci.glimpse.support.atlas.TextureAtlas; | |
| import com.metsci.glimpse.support.atlas.support.ImageData; | |
| import com.metsci.glimpse.util.units.time.Time; | |
| /** | |
| * An example demonstrating painting onto the StackedTimePlot2D overlay layout and | |
| * painting onto a GlimpseAxisLayout1D using a TextureAtlas. | |
| * | |
| * @author ulman | |
| */ | |
| public class IconTimelinePlotExample extends HorizontalTimelinePlotExample | |
| { | |
| public static void main( String[] args ) throws Exception | |
| { | |
| Example.showWithSwing( new IconTimelinePlotExample( ) ); | |
| } | |
| @Override | |
| public GlimpseLayout getLayout( ) | |
| { | |
| StackedTimePlot2D plot = ( StackedTimePlot2D ) super.getLayout( ); | |
| // create a texture atlas | |
| TextureAtlas atlas = new TextureAtlas( 512, 512 ); | |
| // load an images into the atlas | |
| try | |
| { | |
| TextureAtlasExample.loadTextureAtlas( atlas ); | |
| } | |
| catch ( IOException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| // add a simple painter to the StackedTimePlot2D overlay layout, which | |
| // sits on top of the timeline and plots | |
| plot.getOverlayLayout( ).addPainter( new SimpleIconPainter( atlas, plot.getEpoch( ) ) ); | |
| return plot; | |
| } | |
| public class SimpleIconPainter extends GlimpseDataPainter1D | |
| { | |
| protected TextureAtlas atlas; | |
| protected Epoch epoch; | |
| public SimpleIconPainter( TextureAtlas atlas, Epoch epoch ) | |
| { | |
| this.atlas = atlas; | |
| this.epoch = epoch; | |
| } | |
| @Override | |
| public void paintTo( GL gl, GlimpseBounds bounds, Axis1D axis ) | |
| { | |
| this.atlas.beginRendering( ); | |
| try | |
| { | |
| // draw the icon centered at 1 hour past the time plot epoch | |
| float positionX = ( float ) epoch.fromTimeStamp( epoch.getTimeStamp( ).add( Time.fromHours( 1 ) ) ); | |
| // scale the icon to take up 1/3 of the total size of the plot | |
| ImageData data = this.atlas.getImageData( "glimpse" ); | |
| float imageHeight = data.getHeight( ); | |
| float layoutHeight = bounds.getHeight( ) / 3.0f; | |
| float scale = layoutHeight / imageHeight; | |
| // treat pixel (0, data.getHeight( )), measured from the top left corner of the icon, as the icon center | |
| // draw that center pixel at (positionX, 0) on the plot (where positionX is measured in axis | |
| // space and 0 is measured in pixel space, i.e. the bottom of the screen) | |
| this.atlas.drawImageAxisX( gl, "glimpse", axis, positionX, 0, scale, scale, 0, data.getHeight( ) ); | |
| } | |
| finally | |
| { | |
| this.atlas.endRendering( ); | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment