Skip to content

Commit

Permalink
Chart tooltip and some other fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
icza committed Jan 29, 2010
1 parent 5cb8f30 commit 4072b61
Showing 1 changed file with 96 additions and 37 deletions.
133 changes: 96 additions & 37 deletions src/hu/belicza/andras/bwhfagent/view/charts/ChartsComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import hu.belicza.andras.bwhfagent.view.PlayerCheckerTab.ListedAs;
import hu.belicza.andras.hackerdb.ServerApiConsts;

import java.awt.event.InputEvent;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
Expand Down Expand Up @@ -45,6 +44,7 @@
import swingwt.awt.event.ActionListener;
import swingwt.awt.event.AdjustmentEvent;
import swingwt.awt.event.AdjustmentListener;
import swingwt.awt.event.InputEvent;
import swingwt.awt.event.KeyAdapter;
import swingwt.awt.event.KeyEvent;
import swingwt.awt.event.MouseAdapter;
Expand All @@ -66,6 +66,7 @@
import swingwtx.swing.JTextArea;
import swingwtx.swing.JTextField;
import swingwtx.swing.SwingUtilities;
import swingwtx.swing.ToolTipManager;
import swingwtx.swing.event.ChangeEvent;
import swingwtx.swing.event.ChangeListener;

Expand Down Expand Up @@ -292,19 +293,27 @@ public void mousePressed( final MouseEvent event ) {
addMouseMotionListener( new MouseMotionAdapter() {
@Override
public void mouseDragged( final MouseEvent event ) {
final int mx = event.getX();
final int my = event.getY();
if ( chartsParams != null ) {
if ( ( event.getModifiers() & InputEvent.BUTTON3_DOWN_MASK ) == 0 ) {
if ( ( event.getModifiers() & InputEvent.BUTTON1_DOWN_MASK ) != 0 ) {
// If the delta is less than the scroll unit, it won't scroll, and the little delta would vanish, so we accumulate them
dragDestinationX -= ( event.getX() - dragStartX ) * 2;
dragDestinationY -= ( event.getY() - dragStartY ) * 2;
dragDestinationX -= ( mx - dragStartX ) * 2;
dragDestinationY -= ( my - dragStartY ) * 2;
scrollToPoint( dragDestinationX, dragDestinationY );
dragStartX = event.getX();
dragStartY = event.getY();
dragStartX = mx;
dragStartY = my;
repaint();
}
}
updateTooltipText( mx, my );
}
@Override
public void mouseMoved( final MouseEvent event ) {
updateTooltipText( event.getX(), event.getY() );
};
} );
ToolTipManager.sharedInstance().setInitialDelay( 0 );
final JPanel chartsHolderPanel = new JPanel( new BorderLayout() );
chartsHolderPanel.add( this, BorderLayout.CENTER );
chartScrollBar.addAdjustmentListener( new AdjustmentListener() {
Expand Down Expand Up @@ -438,6 +447,45 @@ public void stateChanged( final ChangeEvent event ) {
showBuildingImagesCheckBox.addChangeListener( repainterChangeListener );
}

/**
* Updates the tooltip text over the chart.
* @param x x coordinate of the point to show tooltip for
* @param y y coordinate of the point to show tooltip for
*/
private void updateTooltipText( final int x, final int y ) {
if ( chartsParams == null )
return;

final ChartType chartType = (ChartType) chartsTab.chartTypeComboBox.getSelectedItem();
if ( chartType == ChartType.MAP_VIEW ) {
final int mapWidth = replay.replayHeader.mapWidth * 32; // 1 tile is 32 pixel width in the game (this is independent of our tile width )
final int mapHeight = replay.replayHeader.mapHeight * 32; // 1 tile is 32 pixel height in the game (this is independent of our tile height)

final int mapCoordX = ( chartsParams.dx + x ) * ChartsTab.MAX_ZOOM / chartsParams.zoom;
final int mapCoordY = ( chartsParams.dy + y ) * ChartsTab.MAX_ZOOM / chartsParams.zoom;

if ( mapCoordX < mapWidth && mapCoordY < mapHeight ) {
final int tileWidth = MapImagesManager.TILE_IMAGE_WIDTH * chartsParams.zoom / ChartsTab.MAX_ZOOM; // At max zoom tiles are shown in real size
final int tileHeight = MapImagesManager.TILE_IMAGE_HEIGHT * chartsParams.zoom / ChartsTab.MAX_ZOOM; // At max zoom tiles are shown in real size
setToolTipText( "Position: " + mapCoordX + "," + mapCoordY + " (" + ( (chartsParams.dx + x) / tileWidth ) + "," + ( (chartsParams.dy + y) / tileHeight ) + ")" );
}
else
setToolTipText( null );
}
else {
final int coordX = chartsParams.dx - chartsParams.x1 + x;
if ( coordX >= 0 ) {
final int maxX = chartsParams.componentWidth * chartsParams.zoom - chartsParams.x1;
final int iteration = ( coordX * replay.replayHeader.gameFrames / maxX );

final StringBuilder tooltipTextBuilder = new StringBuilder( "Frame: " ).append( iteration ).append( ", time: " );
setToolTipText( ReplayHeader.formatFrames( iteration, tooltipTextBuilder, true ).toString() );
}
else
setToolTipText( null );
}
}

/**
* Sets the zoom level.
* @param zoom zoom level to be set.
Expand Down Expand Up @@ -1116,7 +1164,7 @@ private void paintApmCharts( final Graphics graphics, final boolean overall ) {

graphics.setFont( CHART_MAIN_FONT );
graphics.setColor( CHART_AXIS_COLOR );
graphics.drawString( overall ? "Overall APM" : "APM", 1, 0 );
graphics.drawString( overall ? "Overall APM" : "APM", chartsParams.dx + 1, 0 );

// First count the actions
final int[] effectiveActionsCounts = eapm ? new int[ chartsParams.playersCount ] : null;
Expand Down Expand Up @@ -1212,20 +1260,12 @@ private void paintApmCharts( final Graphics graphics, final boolean overall ) {

drawAxisAndTimeLabels( graphics, i );

// Draw APM axis labels and assist lines
if ( !chartsParams.allPlayersOnOneChart || i == 0 ) { // We draw labels once if all players are on one chart
graphics.setFont( CHART_AXIS_LABEL_FONT );
// if no actions, let's define axis labels from zero to ASSIST_LINES_COUNT
final int maxApm = maxActionss[ i ] > 0 ? maxActionss[ i ] * ( chartPoints - 1 ) * 60 / Math.max( ReplayHeader.convertFramesToSeconds( chartsParams.frames ), 1 ) : ASSIST_LINES_COUNT;
for ( int j = 0; j <= ASSIST_LINES_COUNT; j++ ) {
final int y = y1 + chartsParams.maxYInChart - ( chartsParams.maxYInChart * j / ASSIST_LINES_COUNT );
final int apm = maxApm * j / ASSIST_LINES_COUNT;
graphics.setColor( CHART_AXIS_LABEL_COLOR );
graphics.drawString( ( apm < 100 ? ( apm < 10 ? " " : " " ) : "" ) + apm, 1, y - 7 );
if ( j > 0 ) {
graphics.setColor( CHART_ASSIST_LINES_COLOR );
graphics.drawLine( chartsParams.x1 + 1, y, chartsParams.x1 + chartsParams.maxXInChart, y );
}
// Draw assist lines
if ( !chartsParams.allPlayersOnOneChart || i == 0 ) { // We draw assist lines once if all players are on one chart
for ( int j = 1; j <= ASSIST_LINES_COUNT; j++ ) {
final int y = y1 + chartsParams.maxYInChart - ( chartsParams.maxYInChart * j / ASSIST_LINES_COUNT );
graphics.setColor( CHART_ASSIST_LINES_COLOR );
graphics.drawLine( chartsParams.x1 + 1, y, chartsParams.x1 + chartsParams.maxXInChart, y );
}
}

Expand Down Expand Up @@ -1270,6 +1310,20 @@ private void paintApmCharts( final Graphics graphics, final boolean overall ) {
eapmString += "0%";
}
drawPlayerDescription( graphics, chartsParams, i, inGameColor, eapmString );

// Draw APM axis labels
if ( !chartsParams.allPlayersOnOneChart || i == 0 ) { // We draw labels once if all players are on one chart
graphics.setFont( CHART_AXIS_LABEL_FONT );
// If no actions, let's define axis labels from zero to ASSIST_LINES_COUNT
final int maxApm = maxActionss[ i ] > 0 ? maxActionss[ i ] * ( chartPoints - 1 ) * 60 / Math.max( ReplayHeader.convertFramesToSeconds( chartsParams.frames ), 1 ) : ASSIST_LINES_COUNT;
for ( int j = 0; j <= ASSIST_LINES_COUNT; j++ ) {
final int y = y1 + chartsParams.maxYInChart - ( chartsParams.maxYInChart * j / ASSIST_LINES_COUNT );
final int apm = maxApm * j / ASSIST_LINES_COUNT;
graphics.setColor( CHART_AXIS_LABEL_COLOR );
graphics.drawString( ( apm < 100 ? ( apm < 10 ? " " : " " ) : "" ) + apm, chartsParams.dx + 1, y - 7 );
}
}

}
}

Expand Down Expand Up @@ -1452,7 +1506,7 @@ private void paintActionSequencesCharts( final Graphics graphics ) {

graphics.setFont( CHART_MAIN_FONT );
graphics.setColor( CHART_AXIS_COLOR );
graphics.drawString( "Pairs/sec", 1, 0 );
graphics.drawString( "Pairs/sec", chartsParams.dx + 1, 0 );

// First identify Action Sequences
final List< int[] >[] actionSequenceLists = new List[ chartsParams.playersCount ]; // An action sequence has 4 parameters: its first frame; its last frame; pairs count, and whether it is hotkey sequence (0) or not (1)
Expand Down Expand Up @@ -1532,25 +1586,18 @@ private void paintActionSequencesCharts( final Graphics graphics ) {

drawAxisAndTimeLabels( graphics, i );

// Draw Pairs/sec axis labels and assist lines
if ( !chartsParams.allPlayersOnOneChart || i == 0 ) { // We draw labels once if all players are on one chart
graphics.setFont( CHART_AXIS_LABEL_FONT );
// if no action sequences, let's define axis labels from zero to ASSIST_LINES_COUNT
final float maxValue = maxValues[ i ] > 0 ? maxValues[ i ] : ASSIST_LINES_COUNT;
for ( int j = 0; j <= ASSIST_LINES_COUNT; j++ ) {
final int y = y1 + chartsParams.maxYInChart - ( chartsParams.maxYInChart * j / ASSIST_LINES_COUNT );
final float value = maxValue * j / ASSIST_LINES_COUNT;
graphics.setColor( CHART_AXIS_LABEL_COLOR );
graphics.drawString( new Formatter( Locale.ENGLISH ).format( "%.1f", value ).toString(), 1, y - 7 );
if ( j > 0 ) {
graphics.setColor( CHART_ASSIST_LINES_COLOR );
graphics.drawLine( chartsParams.x1 + 1, y, chartsParams.x1 + chartsParams.maxXInChart, y );
}
// Draw assist lines
if ( !chartsParams.allPlayersOnOneChart || i == 0 ) { // We draw assist lines once if all players are on one chart
for ( int j = 1; j <= ASSIST_LINES_COUNT; j++ ) {
final int y = y1 + chartsParams.maxYInChart - ( chartsParams.maxYInChart * j / ASSIST_LINES_COUNT );
graphics.setColor( CHART_ASSIST_LINES_COLOR );
graphics.drawLine( chartsParams.x1 + 1, y, chartsParams.x1 + chartsParams.maxXInChart, y );
}
}

// Now draw the bars of the action sequences
final float maxValue = maxValues[ i ] > 0 ? maxValues[ i ] : 1;
// If no action sequences, let's define axis labels from zero to ASSIST_LINES_COUNT
final float maxValue = maxValues[ i ] > 0 ? maxValues[ i ] : ASSIST_LINES_COUNT;
for ( final int[] actionSequence : actionSequenceLists[ i ] ) {
final int x1 = chartsParams.getXForIteration( actionSequence[ 0 ] );
final int x2 = chartsParams.getXForIteration( actionSequence[ 1 ] );
Expand All @@ -1564,6 +1611,18 @@ private void paintActionSequencesCharts( final Graphics graphics ) {
graphics.fillRect( -5, -5, 1, 1 );

drawPlayerDescription( graphics, chartsParams, i, inGameColor, new Formatter( Locale.ENGLISH ).format( "Avg %.1f pairs/sec", summaDuration[ i ] > 0 ? summaPairs[ i ] * 1000f / ( summaDuration[ i ] * 42f ) : 0f ).toString() );

// Draw Pairs/sec axis labels
if ( !chartsParams.allPlayersOnOneChart || i == 0 ) { // We draw labels once if all players are on one chart
graphics.setFont( CHART_AXIS_LABEL_FONT );
for ( int j = 0; j <= ASSIST_LINES_COUNT; j++ ) {
final int y = y1 + chartsParams.maxYInChart - ( chartsParams.maxYInChart * j / ASSIST_LINES_COUNT );
final float value = maxValue * j / ASSIST_LINES_COUNT;
graphics.setColor( CHART_AXIS_LABEL_COLOR );
graphics.drawString( new Formatter( Locale.ENGLISH ).format( "%.1f", value ).toString(), chartsParams.dx + 1, y - 7 );
}
}

}
}

Expand Down

0 comments on commit 4072b61

Please sign in to comment.