2 * evalgraph.c - Evaluation graph back-end part
4 * Author: Alessandro Scotti (Dec 2005)
6 * Copyright 2005 Alessandro Scotti
8 * ------------------------------------------------------------------------
10 * GNU XBoard is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at
13 * your option) any later version.
15 * GNU XBoard is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see http://www.gnu.org/licenses/. *
23 *------------------------------------------------------------------------
24 ** See the file ChangeLog for a revision history. */
26 // code refactored by HGM to obtain front-end / back-end separation
35 #else /* not STDC_HEADERS */
38 # else /* not HAVE_STRING_H */
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
46 #include "evalgraph.h"
49 ChessProgramStats_Move * currPvInfo;
62 static void DrawLine( int x1, int y1, int x2, int y2, int penType )
64 DrawSegment( x1, y1, NULL, NULL, PEN_NONE );
65 DrawSegment( x2, y2, NULL, NULL, penType );
69 static void DrawLineEx( int x1, int y1, int x2, int y2, int penType )
72 DrawSegment( x1, y1, &savX, &savY, PEN_NONE );
73 DrawSegment( x2, y2, NULL, NULL, penType );
74 DrawSegment( savX, savY, NULL, NULL, PEN_NONE );
78 static int GetPvScore( int index )
80 int score = currPvInfo[ index ].score;
82 if( index & 1 ) score = -score; /* Flip score for black */
89 For a centipawn value, this function returns the height of the corresponding
90 histogram, centered on the reference axis.
92 Note: height can be negative!
94 static int GetValueY( int value )
96 if( value < -700 ) value = -700;
97 if( value > +700 ) value = +700;
99 return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / 1400.0);
102 // the brush selection is made part of the DrawLine, by passing a style argument
103 // the wrapper for doing the text output makes this back-end
104 static void DrawAxisSegmentHoriz( int value, Boolean drawValue )
106 int y = GetValueY( value*100 );
109 char buf[MSG_SIZ], *b = buf;
111 if( value > 0 ) *b++ = '+';
112 sprintf(b, "%d", value);
114 DrawEvalText(buf, strlen(buf), y);
116 // [HGM] counts on DrawEvalText to have select transparent background for dotted line!
117 DrawLine( MarginX, y, MarginX + MarginW, y, PEN_BLACK ); // Y-axis tick marks
118 DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y, PEN_DOTTED ); // hor grid
121 // The DrawLines again must select their own brush.
122 // the initial brush selection is useless? BkMode needed for dotted line and text
123 static void DrawAxis()
125 int cy = nHeightPB / 2;
127 // SelectObject( hdcPB, GetStockObject(NULL_BRUSH) );
129 // SetBkMode( hdcPB, TRANSPARENT );
131 DrawAxisSegmentHoriz( +5, TRUE );
132 DrawAxisSegmentHoriz( +3, FALSE );
133 DrawAxisSegmentHoriz( +1, FALSE );
134 DrawAxisSegmentHoriz( 0, TRUE );
135 DrawAxisSegmentHoriz( -1, FALSE );
136 DrawAxisSegmentHoriz( -3, FALSE );
137 DrawAxisSegmentHoriz( -5, TRUE );
139 DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy, PEN_BLACK ); // x-axis
140 DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH, PEN_BLACK ); // y-axis
144 static void DrawHistogram( int x, int y, int width, int value, int side )
146 int left, top, right, bottom;
148 if( value > -25 && value < +25 ) return;
151 right = left + width + 1;
154 top = GetValueY( value );
159 bottom = GetValueY( value ) + 1;
163 if( width == MIN_HIST_WIDTH ) {
165 DrawRectangle( left, top, right, bottom, side, FILLED );
168 DrawRectangle( left, top, right, bottom, side, OPEN );
173 static void DrawSeparator( int index, int x )
176 if( index == currCurrent ) {
177 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_BLUEDOTTED );
179 else if( (index % 20) == 0 ) {
180 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_DOTTED );
185 // made back-end by replacing MoveToEx and LineTo by DrawSegment
186 /* Actually draw histogram as a diagram, cause there's too much data */
187 static void DrawHistogramAsDiagram( int cy, int paint_width, int hist_count )
192 /* Rescale the graph every few moves (as opposed to every move) */
193 hist_count -= hist_count % 8;
197 step = (double) paint_width / (hist_count + 1);
199 for( i=0; i<2; i++ ) {
200 int index = currFirst;
201 int side = (currCurrent + i + 1) & 1; /* Draw current side last */
202 double x = MarginX + MarginW;
204 if( (index & 1) != side ) {
209 DrawSegment( (int) x, cy, NULL, NULL, PEN_NONE );
213 while( index < currLast ) {
216 DrawSeparator( index, (int) x );
218 /* Extend line up to current point */
219 if( currPvInfo[index].depth > 0 ) {
220 DrawSegment((int) x, GetValueY( GetPvScore(index) ), NULL, NULL, PEN_BOLD + side );
228 // back-end, delete pen selection
229 static void DrawHistogramFull( int cy, int hist_width, int hist_count )
233 // SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
235 for( i=0; i<hist_count; i++ ) {
236 int index = currFirst + i;
237 int x = MarginX + MarginW + index * hist_width;
239 /* Draw a separator every 10 moves */
240 DrawSeparator( index, x );
243 if( currPvInfo[i].depth > 0 ) {
244 DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
257 static Boolean InitVisualization( VisualizationData * vd )
259 Boolean result = FALSE;
261 vd->cy = nHeightPB / 2;
262 vd->hist_width = MIN_HIST_WIDTH;
263 vd->hist_count = currLast - currFirst;
264 vd->paint_width = nWidthPB - MarginX - 2*MarginW;
266 if( vd->hist_count > 0 ) {
270 vd->hist_width = vd->paint_width / vd->hist_count;
272 if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
274 vd->hist_width -= vd->hist_width % 2;
281 static void DrawHistograms()
283 VisualizationData vd;
285 if( InitVisualization( &vd ) ) {
286 if( vd.hist_width < MIN_HIST_WIDTH ) {
287 DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
290 DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count );
296 int GetMoveIndexFromPoint( int x, int y )
299 int start_x = MarginX + MarginW;
300 VisualizationData vd;
302 if( x >= start_x && InitVisualization( &vd ) ) {
303 /* Almost an hack here... we duplicate some of the paint logic */
304 if( vd.hist_width < MIN_HIST_WIDTH ) {
307 vd.hist_count -= vd.hist_count % 8;
311 step = (double) vd.paint_width / (vd.hist_count + 1);
314 result = (int) (0.5 + (double) (x - start_x) / step);
317 result = (x - start_x) / vd.hist_width;
321 if( result >= currLast ) {
328 // init and display part split of so they can be moved to front end
329 void PaintEvalGraph( void )
332 DrawRectangle(0, 0, nWidthPB, nHeightPB, 2, FILLED);