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 # QtCtreator CMake
CMakeLists.txt.user* CMakeLists.txt.user*
*.db
*.bmp
release

View File

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

View File

@ -112,8 +112,13 @@ void Viewer::mouseMoveEvent(QMouseEvent* e) {
void Viewer::mousePressEvent(QMouseEvent* e) { void Viewer::mousePressEvent(QMouseEvent* e) {
// Auto Return, but old code left in as reference // Auto Return, but old code left in as reference
//std::cout << "Picking shape at " << e->pos().x() << ", " << e->pos().y() << endl; int x = this->x() + e->x() + 10;
Shape* selectedShape = pickGeom(e->pos().x()+20, e->pos().y()+20); 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(){ void Viewer::deselect(){
@ -164,7 +169,7 @@ void Viewer::init()
c->setColor(*color); c->setColor(*color);
SceneGroup* cell = new SceneGroup; SceneGroup* cell = new SceneGroup;
cell->addChild(c); cell->addChild(c);
root.addChild(cell);*/ root.addChild(cell);//*/
//update(); //update();
} }
@ -359,7 +364,7 @@ void Viewer::changeColor(QColor c){
Shape* Viewer::pickGeom(int x, int y){ Shape* Viewer::pickGeom(int x, int y){
QMap<QRgb, Shape*> mapColorToShape; QMap<QRgb, Shape*> mapColorToShape;
QColor c; 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]; unsigned char pixelData[3];
// Traverse tree // Traverse tree
/* TODO: Make this recurse through SceneGroups, with like "populateMap(map, root, color)" /* TODO: Make this recurse through SceneGroups, with like "populateMap(map, root, color)"
@ -397,13 +402,28 @@ Shape* Viewer::pickGeom(int x, int y){
draw(); draw();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 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]); QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]);
Shape* pickedShape = mapColorToShape.value(pickedColor->rgba()); Shape* pickedShape = mapColorToShape.value(pickedColor->rgba());
std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl; std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl;
std::cout << "Picked Shape: " << pickedShape << endl; std::cout << "Picked Shape: " << pickedShape << endl;
//lastshader->bind(); //lastshader->bind();
return pickedShape; return pickedShape;
} }