Fixing mouse position issues by making the GL widget fill the screen

This commit is contained in:
Dmitri K 2016-11-04 03:08:58 -04:00
parent 379f310fde
commit 677a3ffff1
3 changed files with 71 additions and 23 deletions

3
.gitignore vendored
View File

@ -41,3 +41,6 @@ Makefile*
# QtCtreator CMake
CMakeLists.txt.user*
*.db
*.bmp
release

View File

@ -2,33 +2,54 @@
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>649</width>
<height>560</height>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item alignment="Qt::AlignLeft|Qt::AlignTop">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QFrame" name="frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>1</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>500</width>
<height>500</height>
<width>200</width>
<height>200</height>
</size>
</property>
<property name="cursor">
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="mouseTracking">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
@ -37,7 +58,7 @@
</property>
</widget>
</item>
<item>
<item row="0" column="1">
<layout class="QGridLayout" name="gridLayout">
<property name="horizontalSpacing">
<number>6</number>
@ -128,8 +149,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>649</width>
<height>20</height>
<width>800</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
@ -140,7 +161,11 @@
</widget>
<addaction name="menu_File"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<widget class="QStatusBar" name="statusBar">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
<action name="action_Quit">
<property name="text">
<string>&amp;Quit</string>

View File

@ -112,8 +112,13 @@ void Viewer::mouseMoveEvent(QMouseEvent* e) {
void Viewer::mousePressEvent(QMouseEvent* e) {
// Auto Return, but old code left in as reference
//std::cout << "Picking shape at " << e->pos().x() << ", " << e->pos().y() << endl;
Shape* selectedShape = pickGeom(e->pos().x()+20, e->pos().y()+20);
int x = this->x() + e->x() + 10;
int y = this->parentWidget()->height() - e->y() + 21;
std::cout << "--------------------------------------------------\nPicking shape at " << x << " (" << this->x() << " + " << e->x() << "), " << y << endl;
std::cout << "Window geom: " << this->window()->size().width() << "w, " << this->window()->size().height() << "h" << endl;
Shape* selectedShape = pickGeom(x, y);
// OpenGL coords start at the window's bottom-left, not the frame's. ugh.
}
void Viewer::deselect(){
@ -164,7 +169,7 @@ void Viewer::init()
c->setColor(*color);
SceneGroup* cell = new SceneGroup;
cell->addChild(c);
root.addChild(cell);*/
root.addChild(cell);//*/
//update();
}
@ -359,7 +364,7 @@ void Viewer::changeColor(QColor c){
Shape* Viewer::pickGeom(int x, int y){
QMap<QRgb, Shape*> mapColorToShape;
QColor c;
QRgb startColor = 0xFF000033; // alpha must be 100%, glReadPixels doesn't resturn alpha
QRgb startColor = 0xFFFF00ee; // alpha must be 100%, glReadPixels doesn't resturn alpha
unsigned char pixelData[3];
// Traverse tree
/* TODO: Make this recurse through SceneGroups, with like "populateMap(map, root, color)"
@ -397,13 +402,28 @@ Shape* Viewer::pickGeom(int x, int y){
draw();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// TODO: figure out how to get this weird offset ↓↓ frae position maybe?
glReadPixels(x, camera()->screenHeight() - 1 - y + 62, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
// for debugging purposes
/*QImage *im = new QImage(64, 64, QImage::Format_RGB32);
for(int i = 0; i< 64; i++){
for(int j = 0; j< 64; j++){
glReadPixels(x-31+i, y+31-j, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]);
if(i==32&&j==32) pickedColor->setRgba(0xFFFFFFFF);
im->setPixelColor(i, j, pickedColor->rgb());
}
}
im->save("./screenshot.bmp");//*/
// TODO: figure out how to get this weird offset ↓↓ frame position maybe?
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]);
Shape* pickedShape = mapColorToShape.value(pickedColor->rgba());
std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl;
std::cout << "Picked Shape: " << pickedShape << endl;
//lastshader->bind();
return pickedShape;
}