Commit 1f693732 authored by Mathieu Faverge's avatar Mathieu Faverge

fix(zoom): More fixes on issue #6

parent 9600e343
#include <assert.h>
#include "Zooming.hpp"
#include "../Helper.hpp"
......@@ -18,42 +19,28 @@ GLfloat Zooming::getColor(int x, int y)
return m_colors[x][y];
}
void Zooming::clean_move( double *xbeg, double *xend,
double *ybeg, double *yend )
{
double tmp;
// Cleanup postions
if ( *xbeg > *xend ) {
double tmp = *xend;
*xbeg = *xend;
*xend = tmp;
}
if ( *ybeg > *yend ) {
double tmp = *yend;
*ybeg = *yend;
*yend = tmp;
}
*xbeg = ( *xbeg < 0. ) ? 0. : *xbeg;
*xend = ( *xend > 1. ) ? 1. : *xend;
*ybeg = ( *ybeg < 0. ) ? 0. : *ybeg;
*yend = ( *yend > 1. ) ? 1. : *yend;
}
void Zooming::move(double xStart, double xEnd, double yStart, double yEnd)
{
symbol_cblk_t* cblk;
symbol_blok_t* blok;
int i, j, m, n;
clean_move( &xStart, &xEnd, &yStart, &yEnd );
// Check positions
assert( xEnd >= xStart );
assert( yEnd >= yStart );
assert( yEnd >= xStart );
// Check for out of bounds
assert( xStart >= 0. );
assert( yStart >= 0. );
assert( xEnd <= 1. );
assert( yEnd <= 1. );
// Convert to column/row indexes
int startCol = xStart * m_matrix->m_colsnbr;
int endCol = xEnd * m_matrix->m_colsnbr;
int endCol = xEnd * m_matrix->m_colsnbr - 1;
int startRow = yStart * m_matrix->m_rowsnbr;
int endRow = yEnd * m_matrix->m_rowsnbr;
int endRow = yEnd * m_matrix->m_rowsnbr - 1;
// Find first cblk
int startCblk = 0;
......@@ -107,28 +94,29 @@ void Zooming::move(double xStart, double xEnd, double yStart, double yEnd)
int x = (cblk->m_fcolnum - startCol) * xCoeff;
int xEnd = (cblk->m_lcolnum - startCol) * xCoeff;
xEnd = ( xEnd > x+1 ) ? xEnd : x+1;
blok = m_matrix->m_bloktab + fbloknum;
for (j = fbloknum; j < lbloknum; ++j, blok++)
{
if ( (blok->m_frownum >= startRow) &&
(blok->m_lrownum <= endRow ) )
if ( (blok->m_lrownum < startRow) ||
(blok->m_frownum > endRow ) )
{
// Get first block size in row from y to yEnd
int y = (blok->m_frownum - startRow) * yCoeff;
int yEnd = (blok->m_lrownum - startRow) * yCoeff;
continue;
}
m = x;
do
// Get first block size in row from y to yEnd
int y = (blok->m_frownum - startRow) * yCoeff;
int yEnd = (blok->m_lrownum - startRow) * yCoeff;
yEnd = ( yEnd > y+1 ) ? yEnd : y+1;
for( m=x; m<xEnd; m++ )
{
for( n=y; n<yEnd; n++ )
{
n = y;
do
{
m_colors[m][n] = 0.0f;
n++;
} while (n < yEnd);
m++;
} while (m < xEnd);
m_colors[m][n] = 0.0f;
}
}
}
}
......
......@@ -20,8 +20,6 @@ public:
void move(double xStart, double xEnd, double yStart, double yEnd);
private:
void clean_move( double *xs, double *xe, double *ys, double *ye );
symbol_matrix_t *m_matrix;
GLfloat m_colors[DEFAULT_LEVEL_POWER_2][DEFAULT_LEVEL_POWER_2];
};
......
......@@ -28,7 +28,7 @@ static void drawTemporarySelection(int x, int y, int dx, int dy)
glBegin(GL_QUADS);
// Setup color
glColor4f(1.f, 1.f, 1.f, .25f);
glColor4f(1.f, 1.f, 1.f, .5f);
// Setup vertex
glVertex2f(x, y);
......@@ -118,8 +118,9 @@ void MatrixGLWidget::paintGL()
initializeOpenGLFunctions();
#endif
if (m_frameCount == 0)
if (m_frameCount == 0) {
m_time.start();
}
else
{
sprintf(m_fpsString, "FPS: %f ms", m_time.elapsed() / float(m_frameCount));
......@@ -127,7 +128,10 @@ void MatrixGLWidget::paintGL()
}
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.f, 0.f, 0.f, 0.f);
glClearColor( BACKGROUND_COLOR_R,
BACKGROUND_COLOR_G,
BACKGROUND_COLOR_B,
0.f );
for (i = 0; i < Zooming::DEFAULT_LEVEL_POWER_2; ++i)
{
......@@ -135,13 +139,15 @@ void MatrixGLWidget::paintGL()
{
GLfloat grey = m_zooming->getColor(i, j);
if (grey != 0.f)
if (grey != 0.f) {
drawSquare(i, j, 1, 1, grey, grey, grey);
}
}
}
if (m_drawTempSelection == 1)
if (m_drawTempSelection == 1) {
drawTemporarySelection(m_tempSelectionX, m_tempSelectionY, m_tempSelectionDx, m_tempSelectionDy);
}
m_frameCount++;
}
......@@ -185,33 +191,18 @@ void MatrixGLWidget::mousePressEvent(QMouseEvent* mouseEvent)
void MatrixGLWidget::mouseMoveEvent(QMouseEvent* mouseEvent)
{
if (m_mouseXClicked != -1 && m_mouseYClicked != -1)
if ( (m_mouseXClicked != -1) &&
(m_mouseYClicked != -1) )
{
int glPosX = mouseEvent->x() * m_qtToGLWidthCoeff;
int glPosY = mouseEvent->y() * m_qtToGLHeightCoeff;
int firstPosX = m_mouseXClicked;
int firstPosY = m_mouseYClicked;
if (firstPosX > glPosX)
{
int tmp = firstPosX;
firstPosX = glPosX;
glPosX = tmp;
}
if (firstPosY > glPosY)
{
int tmp = firstPosY;
firstPosY = glPosY;
glPosY = tmp;
}
m_drawTempSelection = 1;
m_tempSelectionX = firstPosX;
m_tempSelectionY = firstPosY;
m_tempSelectionDx = glPosX - firstPosX;
m_tempSelectionDy = glPosY - firstPosY;
m_tempSelectionX = m_mouseXClicked;
m_tempSelectionY = m_mouseYClicked;
m_tempSelectionDx = glPosX - m_mouseXClicked;
m_tempSelectionDy = glPosY - m_mouseYClicked;
}
}
......@@ -219,34 +210,64 @@ void MatrixGLWidget::mouseReleaseEvent(QMouseEvent* mouseEvent)
{
if (m_mouseXClicked != -1 && m_mouseYClicked != -1)
{
int xmin, xmax, ymin, ymax;
int glPosX = mouseEvent->x() * m_qtToGLWidthCoeff;
int glPosY = mouseEvent->y() * m_qtToGLHeightCoeff;
m_savedPositions.push(m_camera);
m_drawTempSelection = 0;
if ( glPosX > m_mouseXClicked )
{
xmin = m_mouseXClicked;
xmax = glPosX;
}
else
{
xmin = glPosX;
xmax = m_mouseXClicked;
}
// Switch min max
if (m_mouseXClicked > glPosX)
if ( glPosY > m_mouseYClicked )
{
int tmp = m_mouseXClicked;
m_mouseXClicked = glPosX;
glPosX = tmp;
ymin = m_mouseYClicked;
ymax = glPosY;
}
if (m_mouseYClicked > glPosY)
else
{
int tmp = m_mouseXClicked;
m_mouseYClicked = glPosY;
glPosY = tmp;
ymin = glPosY;
ymax = m_mouseYClicked;
}
int diffX = glPosX - m_mouseXClicked;
int diffY = glPosY - m_mouseYClicked;
// Compute positions/dimensions in ratio of the current window
double X = (double)(xmin) / (double)Zooming::DEFAULT_LEVEL_POWER_2;
double Y = (double)(ymin) / (double)Zooming::DEFAULT_LEVEL_POWER_2;
double dX = (double)(xmax - xmin) / (double)Zooming::DEFAULT_LEVEL_POWER_2;
double dY = (double)(ymax - ymin) / (double)Zooming::DEFAULT_LEVEL_POWER_2;
// Check for out of bounds
X = ( X < 0. ) ? 0. : X;
Y = ( Y < 0. ) ? 0. : Y;
dX = ( (X+dX) > 1. ) ? 1. - X : dX;
dY = ( (Y+dY) > 1. ) ? 1. - Y : dY;
// Translate to the full display
X = X * m_camera.m_cameraDx + m_camera.m_cameraX;
Y = Y * m_camera.m_cameraDy + m_camera.m_cameraY;
dX = dX * m_camera.m_cameraDx;
dY = dY * m_camera.m_cameraDy;
// Return if in the upper triangular part
if ( (Y + dY) < X )
{
return;
}
m_camera.m_cameraX += ((double)m_mouseXClicked * m_camera.m_cameraDx) / (double)Zooming::DEFAULT_LEVEL_POWER_2;
m_camera.m_cameraY += ((double)m_mouseYClicked * m_camera.m_cameraDy) / (double)Zooming::DEFAULT_LEVEL_POWER_2;
// Position is valid, we save it
m_savedPositions.push(m_camera);
m_drawTempSelection = 0;
m_camera.m_cameraDx = ((double)diffX * m_camera.m_cameraDx) / (double)Zooming::DEFAULT_LEVEL_POWER_2;
m_camera.m_cameraDy = ((double)diffY * m_camera.m_cameraDy) / (double)Zooming::DEFAULT_LEVEL_POWER_2;
m_camera.m_cameraX = X;
m_camera.m_cameraY = Y;
m_camera.m_cameraDx = dX;
m_camera.m_cameraDy = dY;
refreshCamera();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment