Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
ForcesInspectorWidget.cpp
Go to the documentation of this file.
4
6 layout = new QFormLayout(this);
7 layout->setContentsMargins(6, 6, 6, 6);
8 layout->setSpacing(4);
9 setLayout(layout);
10}
11
12Physics::PhysicsBody* ForcesInspectorWidget::getBody() const {
13 if (!selectedObject) return nullptr;
14 return selectedObject->getPhysicsBody();
15}
16
18 selectedObject = object;
19 clearRows();
20
21 auto* body = getBody();
22 if (!body) {
23 setVisible(false);
24 return;
25 }
26
27 setVisible(true);
28
29 for (auto const& [name, val] : body->getAllForces(BodyLock::NOLOCK)) {
30 std::string forceName = name;
31
32 bool isReadOnly = (forceName == "Gravity" || forceName == "Normal");
33
34 InspectorRow row(QString::fromStdString(forceName), this);
35
36 std::function<void(glm::vec3)> setter = nullptr;
37 if (!isReadOnly) {
38 setter = [this, forceName](glm::vec3 v) {
39 if (auto* b = getBody()) b->setForce(forceName, v, BodyLock::NOLOCK);
40 };
41 }
42
43 row.addVec3(
44 [this, forceName]() {
45 auto* b = getBody();
46 return b ? b->getForce(forceName, BodyLock::NOLOCK) : glm::vec3(0.0f);
47 },
48 setter,
49 "N"
50 );
51
52 layout->addRow(row.getLabel(), row.getEditor());
53 rows.push_back(std::move(row));
54 }
55
56 {
57 InspectorRow net("Net Force", this);
58 net.addVec3(
59 [this]() {
60 auto* b = getBody();
61 if (!b) return glm::vec3(0.0f);
62
63 glm::vec3 sum(0.0f);
64 for (const auto& [_, f] : b->getAllForces(BodyLock::NOLOCK)) {
65 sum += f;
66 }
67 return sum;
68 },
69 nullptr,
70 "N"
71 );
72 layout->addRow(net.getLabel(), net.getEditor());
73 rows.push_back(std::move(net));
74 }
75}
76
78 selectedObject = nullptr;
79 clearRows();
80 setVisible(false);
81}
82
84 if (!getBody()) {
85 setVisible(false);
86 return;
87 }
88
89 for (auto& row : rows) {
90 row.refresh();
91 }
92}
93
94void ForcesInspectorWidget::clearRows() {
95 while (layout->rowCount() > 0) {
96 layout->removeRow(0);
97 }
98 rows.clear();
99}
void load(SceneObject *object) override
Loads and binds this section to a scene object.
ForcesInspectorWidget(QWidget *parent=nullptr)
void refresh() override
Updates UI widgets from the current object's state.
void unload() override
Unloads the current object and clears section state.
Abstract base class for inspector panel sections.
QString getLabel() const
InspectorRow & addVec3(const std::function< glm::vec3()> &get, const std::function< void(glm::vec3)> &set, const QString &unit="", const std::function< void(Vector3Widget *)> &onInit=nullptr)
QWidget * getEditor() const
Physics::PhysicsBody * getPhysicsBody() const
Definition SceneObject.h:39