src/ui/canvas.cpp

changeset 71
198d25fe4e21
parent 70
f21b800b02a4
child 73
97df974b5ed5
equal deleted inserted replaced
70:f21b800b02a4 71:198d25fe4e21
178 const QPointF pos = this->modelToScreenCoordinates(*this->worldPosition); 178 const QPointF pos = this->modelToScreenCoordinates(*this->worldPosition);
179 painter.drawEllipse(pos, 5, 5); 179 painter.drawEllipse(pos, 5, 5);
180 painter.setPen(Qt::white); 180 painter.setPen(Qt::white);
181 painter.drawText(pos + QPointF{5, 5}, vectorToString(*this->worldPosition)); 181 painter.drawText(pos + QPointF{5, 5}, vectorToString(*this->worldPosition));
182 } 182 }
183 {
184 QPainter painter{this};
185 QFont font;
186 //font.setStyle(QFont::StyleItalic);
187 painter.setFont(font);
188 QFontMetrics fontMetrics{font};
189 const auto renderText = [&](const QString& text, const geom::PointOnRectagle& intersection)
190 {
191 QPointF position = toQPointF(intersection.position);
192 const geom::RectangleSide side = intersection.side;
193 switch (side)
194 {
195 case geom::RectangleSide::Top:
196 position += QPointF{0, static_cast<qreal>(fontMetrics.ascent())};
197 break;
198 case geom::RectangleSide::Left:
199 break;
200 case geom::RectangleSide::Bottom:
201 position += QPointF{0, static_cast<qreal>(-fontMetrics.descent())};
202 break;
203 case geom::RectangleSide::Right:
204 position += QPointF{static_cast<qreal>(-fontMetrics.width(text)), 0};
205 break;
206 }
207 painter.drawText(position, text);
208 };
209 const QRectF box {
210 QPointF{0, 0},
211 QPointF{static_cast<qreal>(this->width()), static_cast<qreal>(this->height())}
212 };
213 const QPointF p1 = this->modelToScreenCoordinates(glm::vec3{0, 0, 0});
214
215 static const struct
216 {
217 QString text;
218 glm::vec3 direction;
219 } directions[] =
220 {
221 {"+𝑥", {1, 0, 0}},
222 {"-𝑥", {-1, 0, 0}},
223 {"+𝑦", {0, 1, 0}},
224 {"-𝑦", {0, -1, 0}},
225 {"+𝑧", {0, 0, 1}},
226 {"-𝑧", {0, 0, -1}},
227 };
228 for (const auto& axis : directions)
229 {
230 const QPointF x_p = this->modelToScreenCoordinates(axis.direction);
231 const auto intersection = geom::rayRectangleIntersection(geom::rayFromPoints(toVec2(p1), toVec2(x_p)), box);
232 if (intersection.has_value())
233 {
234 renderText(axis.text, *intersection);
235 }
236 }
237 }
183 } 238 }
184 239
185 void Canvas::updateGridMatrix() 240 void Canvas::updateGridMatrix()
186 { 241 {
187 const geom::Triangle triangle { 242 const geom::Triangle triangle {

mercurial