src/modeleditor.cpp

Wed, 25 May 2022 20:36:34 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 20:36:34 +0300
changeset 199
6988973515d2
parent 153
2f79053c2e9a
permissions
-rw-r--r--

Fix pick() picking from weird places on the screen with high DPI scaling

glReadPixels reads data from the frame buffer, which contains data after
high DPI scaling, so any reads to that need to take this scaling into account

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 13
diff changeset
18
153
2f79053c2e9a Renamed modeleditcontext.cpp -> modeleditor.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 152
diff changeset
19 #include "modeleditor.h"
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
20 #include "linetypes/triangle.h"
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
21 #include "linetypes/quadrilateral.h"
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
23 ModelEditor::ModelEditor(Model& model) :
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
24 storedModel{model}
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
28 ModelEditor::~ModelEditor()
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
29 {
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
30 for (ldraw::id_t id : this->modifiedObjects)
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
31 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
32 const QModelIndex index = this->model().find(id);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
33 if (index.isValid())
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
34 {
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
35 Q_EMIT this->objectModified(index.row());
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
36 }
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
37 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
38 }
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
39
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
40 ldraw::id_t ModelEditor::append(std::unique_ptr<ldraw::Object>&& object)
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
41 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
42 this->storedModel.append(std::move(object));
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
43 Q_EMIT this->objectAdded(this->model().size() - 1);
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
44 return object->id;
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
45 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
46
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
47 void ModelEditor::remove(int position)
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
48 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
49 this->storedModel.remove(position);
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
50 }
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
51
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
52 auto ModelEditor::setObjectProperty(
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
53 const ldraw::id_t id,
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
54 const ldraw::Property property,
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
55 const QVariant& value)
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
56 -> ldraw::Object::SetPropertyResult
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
57 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
58 ldraw::Object* const object = this->storedModel.findObjectById(id);
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
59 if (object != nullptr)
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
60 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
61 const ldraw::Object::SetPropertyResult result = object->setProperty(ldraw::PropertyKeyValue{property, value});
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
62 modifiedObjects.insert(id);
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
63 return result;
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
64 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
65 else
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
66 {
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
67 return ldraw::Object::SetPropertyResult::PropertyNotHandled;
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
68 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
69 }
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 87
diff changeset
70
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
71 void ModelEditor::setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value)
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 {
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
73 ldraw::Object* object = this->storedModel.findObjectById(id);
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
74 if (object != nullptr)
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
75 {
86
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
76 object->setProperty(ldraw::PropertyKeyValue{ldraw::pointProperty(pointId), QVariant::fromValue(value)});
4bec0525ef1b PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents: 77
diff changeset
77 modifiedObjects.insert(id);
76
7c4a63a02632 finished splitQuadrilateral theoretically (untested)
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
78 }
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
80
152
03f8e6d42e13 Major refactoring
Teemu Piippo <teemu@hecknology.net>
parents: 151
diff changeset
81 const Model &ModelEditor::model()
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
82 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
83 return this->storedModel;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
84 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
85

mercurial