roxdokuview.cpp 16.4 KB
Newer Older
1 2 3
/***************************************************************************
 *   Copyright 2005-2007 Francesco Rossi <redsh@email.it>                  *
 *   Copyright 2006-2007 Mick Kappenburg <ksudoku@kappendburg.net>         *
4
 *   Copyright 2006-2008 Johannes Bergmeier <johannes.bergmeier@gmx.net>   *
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
Johannes Bergmeier's avatar
Johannes Bergmeier committed
19
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
20 21
 ***************************************************************************/

22 23 24 25
#include "roxdokuview.h"

#include "puzzle.h"
#include "ksudoku.h"
26
#include "skgraph.h"
27 28 29 30 31 32 33 34

#include <kmessagebox.h>
#include <qcursor.h>
//Added by qt3to4:
#include <QPixmap>
#include <QMouseEvent>
#include <klocale.h>

35 36
#include "settings.h"

37 38
#include "renderer.h"

39
#include <QDebug> // IDW test.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

namespace ksudoku{

GLUquadricObj *quadratic; // Used For Our Quadric

//const float  = 2.0*3.1415926535f; // PI Squared



GLfloat LightAmbient[]  = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]  = { 0.8f, 1.0f, 1.0f, 1.0f };	
GLfloat LightPosition[] = { 0.0f, 0.0f, -10.0f, 5.0f };		

Matrix4fT Transform   =  {{ {1.0f},  {0.0f},  {0.0f},  {0.0f}, // NEW: Final Transform
                            {0.0f},  {1.0f},  {0.0f},  {0.0f},
                            {0.0f},  {0.0f},  {1.0f},  {0.0f},
                            {0.0f},  {0.0f},  {0.0f},  {1.0f} }};

Matrix3fT LastRot     = {{  {1.0f},  {0.0f},  {0.0f},          // NEW: Last Rotation
                            {0.0f},  {1.0f},  {0.0f},
                            {0.0f},  {0.0f},  {1.0f} }};

Matrix3fT ThisRot     = {{  {1.0f},  {0.0f},  {0.0f},          // NEW: This Rotation
                            {0.0f},  {1.0f},  {0.0f},
                            {0.0f},  {0.0f},  {1.0f} }};


67
RoxdokuView::RoxdokuView(ksudoku::Game game, Symbols* symbols, QWidget *parent)
68
	: QGLWidget(parent)
Johannes Bergmeier's avatar
Johannes Bergmeier committed
69
	, m_symbols(symbols)
70 71
	
{
72 73 74 75 76 77 78 79 80 81
	m_game   = game;
	m_graph  = m_game.puzzle()->graph();

	m_order  = m_graph->order();
	m_base   = m_graph->base();
	m_size   = m_graph->size();
	m_width  = m_graph->sizeX();
	m_height = m_graph->sizeY();
	m_depth  = m_graph->sizeZ();

82 83
	connect(m_game.interface(), SIGNAL(cellChange(int)), this, SLOT(updateGL()));
	connect(m_game.interface(), SIGNAL(fullChange()), this, SLOT(updateGL()));
84

85 86 87 88
	// IDW test. m_wheelmove = 0.0f;
	m_wheelmove = -5.0f; // IDW test. Makes the viewport bigger, can see more.
	m_dist = 5.3f;
	m_selected_number = 1;
89

90
	loadSettings();
91

92 93 94
	m_isClicked  = false;
	m_isRClicked = false;	
	m_isDragging = false;	
95

96 97 98 99 100 101
	m_selection = -1;
	m_lastSelection = -1;
	m_highlights.fill(0, m_size);
	m_timeDelay = false;
	m_delayTimer = new QTimer(this);
	connect(m_delayTimer, SIGNAL(timeout()), SLOT(delayOver()));
102 103 104 105
}

RoxdokuView::~RoxdokuView()
{
106 107
	glDeleteTextures(10, m_texture[0]);
	glDeleteTextures(25, m_texture[1]);
108 109 110 111 112 113
}

QString RoxdokuView::status() const
{
	QString m;

114 115 116
// 	int secs = QTime(0,0).secsTo(m_game.time());
// 	if(secs % 36 < 12)
// 		m = i18n("Selected item %1, Time elapsed %2. DRAG to rotate. MOUSE WHEEL to zoom in/out.",
117
// 				 m_symbols->value2Symbol(m_selected_number, m_game.order()),
118 119 120
// 		         m_game.time().toString("hh:mm:ss"));
// 	else  if(secs % 36 < 24)
// 		m = i18n("Selected item %1, Time elapsed %2. DOUBLE CLICK on a cube to insert selected number.",
121
// 				 m_symbols->value2Symbol(m_selected_number, m_game.order()),
122 123 124
// 		         m_game.time().toString("hh:mm:ss"));
// 	else
// 		m = i18n("Selected item %1, Time elapsed %2. Type in a cell (zero to delete) to place that number in it.",
125
// 				 m_symbols->value2Symbol(m_selected_number, m_game.order()),
126
// 		         m_game.time().toString("hh:mm:ss"));
127 128 129 130 131 132 133 134

	return m;
}


void RoxdokuView::initializeGL()
{
	glClearColor( 0.0, 0.0, 0.0, 0.5 );
135 136 137 138 139 140
	glEnable(GL_TEXTURE_2D);	// Enable Texture Mapping ( NEW )
	//glShadeModel(GL_SMOOTH);	// Enable Smooth Shading
	//glClearColor(0.0f, 0.0f, 0.0f, 0.5f);	// Black Background
	//glClearDepth(1.0f);		// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);	// Enables Depth Testing
	//glDepthFunc(GL_LEQUAL);	// The Type Of Depth Testing To Do
141 142 143 144 145 146 147
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	

	setMouseTracking(true);
	
	for(int o=0; o<2; o++) 
		for(int i=0; i<=9+o*16; i++)
		{
148 149 150
			int sz = 64;
			QPixmap pic = Renderer::instance()->renderSpecial3D(SpecialCell, sz);
			if(i != 0) {
151
				pic = Renderer::instance()->renderSymbolOn(pic, i, 0, 9+o*16, SymbolPreset);
152
			}
153
			QImage pix = convertToGLFormat(pic.toImage());
154
	
155 156
			glGenTextures(1, &m_texture[o][i]);
			glBindTexture(GL_TEXTURE_2D, m_texture[o][i]);
157 158 159 160 161 162
			glTexImage2D(GL_TEXTURE_2D, 0,4, sz,sz, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) pix.bits());
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
		}
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
void  RoxdokuView::resizeGL(int w, int h ) {
	if (w == 0) w = 1;	
	if (h == 0) h = 1;
	m_arcBall = new ArcBallT((GLfloat)w, (GLfloat)h);

	glViewport(0, 0, (GLint)w, (GLint)h);
	glMatrixMode(GL_PROJECTION); // Select the Projection Matrix
	glLoadIdentity();            // Reset the Projection Matrix

	gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW); // Select the Modelview Matrix
	glLoadIdentity();
}
	

179 180
	void RoxdokuView::mouseDoubleClickEvent ( QMouseEvent * /*e*/ )
	{
181 182 183 184
		if(m_selection == -1) return;
		if(m_selected_number == -1) return;
		if(m_game.given(m_selection)) return;
		m_game.setValue(m_selection, m_selected_number);
185
//		updateGL();
186
		if(m_isDragging) releaseMouse();
187 188 189 190
	}

void RoxdokuView::Selection(int mouse_x, int mouse_y)
{
191
	if(m_isDragging)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		return;
	
	makeCurrent();
	
	GLuint	buffer[512];
	GLint	hits;

	GLint	viewport[4];

	glGetIntegerv(GL_VIEWPORT, viewport);
	glSelectBuffer(512, buffer);
	(void) glRenderMode(GL_SELECT);

	glInitNames();
	glPushName(0);

	glMatrixMode(GL_PROJECTION);     // Selects The Projection Matrix
	glPushMatrix();                  // Push The Projection Matrix
	glLoadIdentity();                // Resets The Matrix

	// This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, Where The Mouse Is.
	gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y), 1.0f, 1.0f, viewport);
	gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f);
	glMatrixMode(GL_MODELVIEW);
	paintGL();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);	
	hits=glRenderMode(GL_RENDER);

	if (hits > 0){
		int	choose = buffer[3];
		int depth = buffer[1];

		for (int loop = 1; loop < hits; loop++){
			// If This Object Is Closer To Us Than The One We Have Selected
			if (buffer[loop*4+1] < GLuint(depth)){
				choose = buffer[loop*4+3];
				depth  = buffer[loop*4+1];
			}
		}

234 235
		if(choose <= m_size && choose > 0)
			m_selection  = choose-1;
236

237 238 239 240 241
		// Stop the timer if the selection is on a cube.
		if (m_timeDelay) {
			m_delayTimer->stop();
			m_timeDelay = false;
		}
242 243 244
		setFocus();
		paintGL();
	}
245 246 247 248
	else if ((! m_timeDelay) && (m_selection != -1)) {
		// Avoid flickering when the pointer passes between cubes.
		m_delayTimer->start(300);
		m_timeDelay = true;
249 250 251
	}
}

252 253 254 255 256 257 258 259
void RoxdokuView::delayOver()
{
	// Remove the highlighting, etc. when the pointer rests between cubes.
	m_delayTimer->stop();
	m_timeDelay = false;
	m_selection = -1;
	paintGL();
}
260 261 262 263 264 265 266 267 268

void RoxdokuView::mouseMoveEvent ( QMouseEvent * e )
{
	Point2fT f;
	f.T[0] = e->x();
	f.T[1] = e->y();
	
	Selection(e->x(), e->y());

269
	if (m_isRClicked){                      // If Right Mouse Clicked, Reset All Rotations
270 271 272 273 274
		Matrix3fSetIdentity(&LastRot);      // Reset Rotation
		Matrix3fSetIdentity(&ThisRot);      // Reset Rotation
			Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot);		// Reset Rotation
	}

275 276 277
	if (!m_isDragging){          // Not Dragging
		if (m_isClicked){          // First Click	
		m_isDragging = true;       // Prepare For Dragging
278
		LastRot = ThisRot;       // Set Last Static Rotation To Last Dynamic One
279
		m_arcBall->click(&f);      // Update Start Vector And Prepare For Dragging
280 281 282 283 284
		grabMouse(/*QCursor(Qt::SizeAllCursor)*/);
		}
		updateGL();
	}
	else{
285
		if (m_isClicked){          // Still Clicked, So Still Dragging
286 287
			Quat4fT     ThisQuat;

288
			m_arcBall->drag(&f, &ThisQuat);                           // Update End Vector And Get Rotation As Quaternion
289 290 291 292 293
			Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat);     // Convert Quaternion Into Matrix3fT
			Matrix3fMulMatrix3f(&ThisRot, &LastRot);                // Accumulate Last Rotation Into This One
			Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot);  // Set Our Final Transform's Rotation From This One
		}
		else{                   // No Longer Dragging
294
			m_isDragging = false;
295 296 297 298 299 300
			releaseMouse ();
		}
		updateGL();
	}
}

301
void RoxdokuView::selectValue(int value) {
302
	m_selected_number = value;
303
}
304

305 306
void RoxdokuView::loadSettings() {
	m_guidedMode = Settings::showErrors();
307 308
	m_showHighlights = Settings::showHighlights();
	qDebug() << "Show errors" << m_guidedMode << "Show highlights" << m_showHighlights;
309 310 311 312
}

void RoxdokuView::settingsChanged() {
	loadSettings();
Johannes Bergmeier's avatar
Johannes Bergmeier committed
313 314 315
	updateGL();
}

316 317
void RoxdokuView::myDrawCube(bool highlight, int name,
				GLfloat x, GLfloat y, GLfloat z, bool outside)
318 319 320 321 322
{
	glPushMatrix();
	glLoadName(name+1);
	glTranslatef(x,y,z);

323
	glBindTexture(GL_TEXTURE_2D, m_texture[m_order >= 16][m_game.value(name)]);
324 325
	
	float sz = 1.0f;
326 327 328 329 330 331 332 333
	// float s = 0.15f;
	float s = 0.2f;
	if(m_selection != -1 && m_selection != name && highlight) {
		// IDW test. s = -0.25f;
		s = +0.2;
		// IDW test. Keep highlighted cells at same size. sz = 0.52f;
		sz = 0.7f;

334 335
		switch(m_game.buttonState(name)) {
			case ksudoku::GivenValue:
336 337 338
				// IDW test. glColor3f(0.4f,0.4f,0.8f);
				glColor3f(0.85f,1.0f,0.4f);	// Green/Gold.
				// IDW cancel. sz+=0.15;
339 340 341
				break;
			case ksudoku::ObviouslyWrong:
			case ksudoku::WrongValue:
Johannes Bergmeier's avatar
Johannes Bergmeier committed
342
				if(m_guidedMode && m_game.puzzle()->hasSolution())
343
					glColor3f(0.75f,0.25f,0.25f);	// Red.
344
				else
345 346
					// IDW test. glColor3f(0.5f+s,0.5f+s,1.0f+s);
					glColor3f(0.75f+s,0.75f+s,0.25f+s);
347 348 349
				break;
			case ksudoku::Marker:
			case ksudoku::CorrectValue:
350 351
				// IDW test. glColor3f(0.5f+s,0.5f+s,1.0f+s);	
				glColor3f(0.75f+s,0.75f+s,0.25f+s);	// Gold.
352 353 354
				break;
		}
	} else {
355 356
		// IDW test. sz = 1.0f;
		// IDW test. s = 0.1f;
357
		s = 0.1f;
358 359 360 361 362
		if (outside && (m_selection != -1)) {
		    // Shrink and darken cells outside the selection-volume.
		    sz = 0.52f;
		    s  = -0.24;
		}
363 364
		switch(m_game.buttonState(name)) {
			case ksudoku::GivenValue:
365 366
				// IDW test. glColor3f(0.35f,0.70f,0.45f);
				glColor3f(0.6f+s,0.9f+s,0.6f+s);	// Green.
367 368 369
				break;
			case ksudoku::ObviouslyWrong:
			case ksudoku::WrongValue:
Johannes Bergmeier's avatar
Johannes Bergmeier committed
370
				if(m_guidedMode && m_game.puzzle()->hasSolution())
371
	 				glColor3f(0.75f,0.25f,0.25f);	// Red.
372
				else
373 374
					// IDW test. glColor3f(0.5f+s,0.5f+s,1.0f+s);
					glColor3f(0.6f+s,1.0f+s,1.0f+s);// Blue.
375 376 377
				break;
			case ksudoku::Marker:
			case ksudoku::CorrectValue:
378 379
				// IDW test. glColor3f(0.5f+s,0.5f+s,1.0f+s);	
				glColor3f(0.6f+s,1.0f+s,1.0f+s);	// Blue.
380 381 382 383
				break;
		}
	}

384 385 386
	if(m_selection == name)
		// IDW test. glColor3f(0.75f,0.25f,0.25f);
		glColor3f(1.0f,0.8f,0.4f);	// Orange.
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

	glBegin(GL_QUADS);
	/* front face */
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sz, -sz, sz); 
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(sz, -sz, sz);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(sz, sz, sz);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-sz, sz, sz);
		/* back face */
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-sz, -sz, -sz); 
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-sz, sz, -sz);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(sz, sz, -sz);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(sz, -sz, -sz);
		/* right face */
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(sz, -sz, -sz); 
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(sz, sz, -sz);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(sz, sz, sz);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(sz, -sz, sz);
		/* left face */
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-sz, -sz, sz); 
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-sz, sz, sz);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-sz, sz, -sz);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sz, -sz, -sz);
		/* top face */
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(sz, sz, sz); 
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(sz, sz, -sz);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-sz, sz, -sz);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sz, sz, sz);
		/* bottom face */
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(sz, -sz, -sz); 
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(sz, -sz, sz);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-sz, -sz, sz);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sz, -sz, -sz);
	glEnd();
	glPopMatrix();
}

void RoxdokuView::paintGL()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
453
	glTranslatef(0.0f, 0.0f, -m_dist*(m_width+3)+m_wheelmove);
454 455 456

	glMultMatrixf(Transform.M);

457
	enum {Outside, Inside, Highlight};
458
	int selX = -1, selY = -1, selZ = -1;
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

	// If a cell is newly selected work out the highlights and lowlights.
	if ((m_selection != -1) && (m_selection != m_lastSelection)) {
	    m_lastSelection = m_selection;
	    selX = m_graph->cellPosX (m_selection);
	    selY = m_graph->cellPosY (m_selection);
	    selZ = m_graph->cellPosZ (m_selection);

	    // Note: m_highlights persists through many frame-paints per second.
	    m_highlights.fill(Outside, m_size);

	    // Mark the cells to be highlighted when highlighting is on.
	    QList<int> groupsToHighlight = m_graph->cliqueList(m_selection);
	    for(int g = 0; g < groupsToHighlight.count(); g++) {
		QVector<int> cellList =
				m_graph->clique(groupsToHighlight.at(g));
		for (int n = 0; n < m_order; n++) {
		    m_highlights[cellList.at(n)] = Highlight;
		}
	    }

	    // Mark non-highlighted cells that are inside cubes containing
	    // the selected cell.  In custom Roxdoku puzzles with > 1 cube,
	    // cells outside are shrunk and darkened.
	    for (int n = 0; n < m_graph->structureCount(); n++) {
		int cubePos = m_graph->structurePosition(n);
		int cubeX   = m_graph->cellPosX(cubePos);
		int cubeY   = m_graph->cellPosY(cubePos);
		int cubeZ   = m_graph->cellPosZ(cubePos);
		if (m_graph->structureType(n) != SKGraph::RoxdokuGroups) {
		    continue;
		}
		if ((selX >= cubeX) && (selX < (cubeX + m_base)) &&
		    (selY >= cubeY) && (selY < (cubeY + m_base)) &&
		    (selZ >= cubeZ) && (selZ < (cubeZ + m_base))) {
		    for (int x = cubeX; x < cubeX + m_base; x++) {
			for (int y = cubeY; y < cubeY + m_base; y++) {
			    for (int z = cubeZ; z < cubeZ + m_base; z++) {
				int pos = m_graph->cellIndex(x, y, z);
				if (m_highlights.at(pos) == Outside) {
				    m_highlights[pos] = Inside;
				}
			    }
			}
		    }
		}
	    }
506 507
	}

508
	int c = 0;
509

510 511 512 513 514 515 516
	for(int xx = 0; xx < m_width; ++xx) {
		for(int yy = 0; yy < m_height; ++yy) {
			for(int zz = 0; zz < m_depth; ++zz) {
				if(m_game.value(c) == UNUSABLE) {
				    c++;
				    continue;	// Do not paint unusable cells.
				}
517
				glPushMatrix();
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

				// Centre the puzzle in the viewport.
				glTranslatef(-(m_dist * m_width  - m_dist) / 2,
					     -(m_dist * m_height - m_dist) / 2,
					     -(m_dist * m_depth  - m_dist) / 2);

				// Highlight cells in the three planes through
				// the selected cell. Unhighlight cells outside
				// the cubical volume of the selection.
				bool highlight = m_showHighlights &&
					     (m_highlights.at(c) == Highlight);
				bool outside = (m_highlights.at(c) == Outside);

				myDrawCube(highlight, c++,
					    (GLfloat)(m_dist * xx),
					    (GLfloat)(m_dist * yy),
					    (GLfloat)(m_dist * zz), outside);

536 537
				glPopMatrix();
			}
538 539
		}
	}
540 541 542 543
	swapBuffers();
}

}
544

545
#include "roxdokuview.moc"