2 * evalgraph.c - Evaluation graph back-end part
4 * Author: Alessandro Scotti (Dec 2005)
6 * Copyright 2005 Alessandro Scotti
8 * Enhancments Copyright 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
10 * ------------------------------------------------------------------------
12 * GNU XBoard is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or (at
15 * your option) any later version.
17 * GNU XBoard is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see http://www.gnu.org/licenses/. *
25 *------------------------------------------------------------------------
26 ** See the file ChangeLog for a revision history. */
28 // code refactored by HGM to obtain front-end / back-end separation
37 #else /* not STDC_HEADERS */
40 # else /* not HAVE_STRING_H */
42 # endif /* not HAVE_STRING_H */
43 #endif /* not STDC_HEADERS */
48 #include "evalgraph.h"
51 ChessProgramStats_Move * currPvInfo;
67 DrawLine (int x1, int y1, int x2, int y2, int penType)
69 DrawSegment( x1, y1, NULL, NULL, PEN_NONE );
70 DrawSegment( x2, y2, NULL, NULL, penType );
75 DrawLineEx (int x1, int y1, int x2, int y2, int penType)
78 DrawSegment( x1, y1, &savX, &savY, PEN_NONE );
79 DrawSegment( x2, y2, NULL, NULL, penType );
80 DrawSegment( savX, savY, NULL, NULL, PEN_NONE );
85 GetPvScore (int index)
87 int score = currPvInfo[ index ].score;
89 if(differentialView) score = index < currLast-1 ? -currPvInfo[ index+1 ].score - score : 0;
90 if( index & 1 ) score = -score; /* Flip score for black */
96 MakeEvalTitle (char *title)
99 static char buf[MSG_SIZ];
101 if( currCurrent <0 ) return title; // currCurrent = -1 crashed WB on start without ini file!
102 score = currPvInfo[ currCurrent ].score;
103 depth = currPvInfo[ currCurrent ].depth;
105 if( depth <=0 ) return title;
106 if( currCurrent & 1 ) score = -score; /* Flip score for black */
107 snprintf(buf, MSG_SIZ, "%s {%d: %s%.2f/%-2d %d}", title, currCurrent/2+1,
108 score>0 ? "+" : " ", score/100., depth, (currPvInfo[currCurrent].time+50)/100);
115 For a centipawn value, this function returns the height of the corresponding
116 histogram, centered on the reference axis.
118 Note: height can be negative!
121 GetValueY (int value)
123 if( value < -range*700 ) value = -range*700;
124 if( value > +range*700 ) value = +range*700;
125 if(value > 100*range) value += (appData.zoom - 1)*100*range; else
126 if(value < -100*range) value -= (appData.zoom - 1)*100*range; else
127 value *= appData.zoom;
128 return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / ((1200. + 200.*appData.zoom)*range));
131 // the brush selection is made part of the DrawLine, by passing a style argument
132 // the wrapper for doing the text output makes this back-end
134 DrawAxisSegmentHoriz (int value, Boolean drawValue)
136 int y = GetValueY( range*value*100 );
139 char buf[MSG_SIZ], *b = buf;
141 if( value > 0 ) *b++ = '+';
142 sprintf(b, "%d", range*value);
144 DrawEvalText(buf, strlen(buf), y);
146 // [HGM] counts on DrawEvalText to have select transparent background for dotted line!
147 DrawLine( MarginX, y, MarginX + MarginW, y, PEN_BLACK ); // Y-axis tick marks
148 DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y, PEN_DOTTED ); // hor grid
151 // The DrawLines again must select their own brush.
152 // the initial brush selection is useless? BkMode needed for dotted line and text
156 int cy = nHeightPB / 2, space = nHeightPB/(6 + appData.zoom);
158 DrawAxisSegmentHoriz( +5, TRUE );
159 DrawAxisSegmentHoriz( +3, space >= 20 );
160 DrawAxisSegmentHoriz( +1, space >= 20 && space*appData.zoom >= 40 );
161 DrawAxisSegmentHoriz( 0, TRUE );
162 DrawAxisSegmentHoriz( -1, space >= 20 && space*appData.zoom >= 40 );
163 DrawAxisSegmentHoriz( -3, space >= 20 );
164 DrawAxisSegmentHoriz( -5, TRUE );
166 DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy, PEN_BLACK ); // x-axis
167 DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH, PEN_BLACK ); // y-axis
172 DrawHistogram (int x, int y, int width, int value, int side)
174 int left, top, right, bottom;
176 if( value > -appData.evalThreshold*range && value < +appData.evalThreshold*range ) return;
179 right = left + width + 1;
182 top = GetValueY( value );
187 bottom = GetValueY( value ) + 1;
191 if( width == MIN_HIST_WIDTH ) {
193 DrawRectangle( left, top, right, bottom, side, FILLED );
196 DrawRectangle( left, top, right, bottom, side, OPEN );
202 DrawSeparator (int index, int x)
205 if( index == currCurrent ) {
206 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_BLUEDOTTED );
208 else if( (index % 20) == 0 ) {
209 DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_DOTTED );
214 // made back-end by replacing MoveToEx and LineTo by DrawSegment
215 /* Actually draw histogram as a diagram, cause there's too much data */
217 DrawHistogramAsDiagram (int cy, int paint_width, int hist_count)
222 /* Rescale the graph every few moves (as opposed to every move) */
223 hist_count -= hist_count % 8;
227 step = (double) paint_width / (hist_count + 1);
229 for( i=0; i<2; i++ ) {
230 int index = currFirst;
231 int side = (currCurrent + i + 1) & 1; /* Draw current side last */
232 double x = MarginX + MarginW;
234 if( (index & 1) != side ) {
239 DrawSegment( (int) x, cy, NULL, NULL, PEN_NONE );
243 while( index < currLast ) {
246 DrawSeparator( index, (int) x );
248 /* Extend line up to current point */
249 if( currPvInfo[index].depth > 0 ) {
250 DrawSegment((int) x, GetValueY( GetPvScore(index) ), NULL, NULL, (side==0 ? PEN_BOLDWHITE: PEN_BOLDBLACK) );
258 // back-end, delete pen selection
260 DrawHistogramFull (int cy, int hist_width, int hist_count)
264 // SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
266 for( i=0; i<hist_count; i++ ) {
267 int index = currFirst + i;
268 int x = MarginX + MarginW + index * hist_width;
270 /* Draw a separator every 10 moves */
271 DrawSeparator( index, x );
274 if( currPvInfo[i].depth > 0 ) {
275 DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
289 InitVisualization (VisualizationData *vd)
291 Boolean result = FALSE;
293 vd->cy = nHeightPB / 2;
294 vd->hist_width = MIN_HIST_WIDTH;
295 vd->hist_count = currLast - currFirst;
296 vd->paint_width = nWidthPB - MarginX - 2*MarginW;
298 if( vd->hist_count > 0 ) {
302 vd->hist_width = vd->paint_width / vd->hist_count;
304 if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
306 vd->hist_width -= vd->hist_width % 2;
316 VisualizationData vd;
317 int i; double step = 1;
319 if( InitVisualization( &vd ) ) {
320 if( vd.hist_width < MIN_HIST_WIDTH ) {
321 DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
322 step = 0.5*vd.paint_width / (((vd.hist_count | 7) + 1)/2 + 1.);
325 DrawHistogramFull( vd.cy, step = vd.hist_width, vd.hist_count );
328 if(!differentialView) return;
329 differentialView = 0;
330 DrawSegment( MarginX + MarginW, vd.cy, NULL, NULL, PEN_NONE );
331 for( i=0; i<vd.hist_count; i++ ) {
332 int index = currFirst + i;
333 int x = MarginX + MarginW + index * step + step/2;
334 DrawSegment((int) x, GetValueY( GetPvScore(index) ), NULL, NULL, PEN_ANY );
336 differentialView = 1;
341 GetMoveIndexFromPoint (int x, int y)
344 int start_x = MarginX + MarginW;
345 VisualizationData vd;
347 if( x >= start_x && InitVisualization( &vd ) ) {
348 /* Almost an hack here... we duplicate some of the paint logic */
349 if( vd.hist_width < MIN_HIST_WIDTH ) {
352 vd.hist_count -= vd.hist_count % 8;
356 step = (double) vd.paint_width / (vd.hist_count + 1);
359 result = (int) (0.5 + (double) (x - start_x) / step);
362 result = (x - start_x) / vd.hist_width;
366 if( result >= currLast ) {
373 // init and display part split of so they can be moved to front end
375 PaintEvalGraph (void)
377 VariantClass v = gameInfo.variant;
378 range = (gameInfo.holdingsWidth && v != VariantSuper && v != VariantGreat && v != VariantSChess) ? 2 : 1; // [HGM] double range in drop games
380 DrawRectangle(0, 0, nWidthPB, nHeightPB, 2, FILLED);