Kstars

ellipsenode.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Artem Fedoskin <[email protected]>
3  SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "ellipsenode.h"
7 
8 #include <QSGFlatColorMaterial>
9 #include <QSGGeometry>
10 #include <QSGGeometryNode>
11 
12 #include <cmath>
13 
14 EllipseNode::EllipseNode(const QColor &color, int width)
15  : m_geometryNode(new QSGGeometryNode), m_material(new QSGFlatColorMaterial)
16 {
18  m_geometry->allocate(60);
19  m_geometryNode->setGeometry(m_geometry);
20  m_geometryNode->setFlag(QSGNode::OwnsGeometry);
21 
22  m_geometryNode->setOpaqueMaterial(m_material);
23  m_geometryNode->setFlag(QSGNode::OwnsMaterial);
24 
25  if (color.isValid())
26  {
27  setColor(color);
28  }
29  setLineWidth(width);
30 
31  appendChildNode(m_geometryNode);
32 }
33 
34 void EllipseNode::setColor(QColor color)
35 {
36  if (color != m_material->color())
37  {
38  m_material->setColor(color);
39  m_geometryNode->markDirty(QSGNode::DirtyMaterial);
40  }
41 }
42 
43 void EllipseNode::setLineWidth(int width)
44 {
45  if (width != m_geometry->lineWidth())
46  {
47  m_geometry->setLineWidth(width);
48  m_geometryNode->markDirty(QSGNode::DirtyGeometry);
49  }
50 }
51 
52 void EllipseNode::updateGeometry(float x, float y, int width, int height, bool filled)
53 {
54  if (filled)
55  {
56  m_geometry->setDrawingMode(GL_TRIANGLE_FAN);
57  }
58  else
59  {
60  m_geometry->setDrawingMode(GL_LINE_LOOP);
61  }
62 
63  QSGGeometry::Point2D *vertex = m_geometry->vertexDataAsPoint2D();
64 
65  float rad = M_PI / 180;
66 
67  width /= 2;
68  height /= 2;
69 
70  if (m_width != width || m_height != height)
71  {
72  for (int i = 0; i < 360; i += 6)
73  {
74  vertex[i / 6].x = width * cos(i * rad);
75  vertex[i / 6].y = height * sin(i * rad);
76  }
77  m_geometryNode->markDirty(QSGNode::DirtyGeometry);
78 
79  m_width = width;
80  m_height = height;
81  }
82  if (m_x != x || m_y != y)
83  {
84  QMatrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, 0, 0, 0, 0, 1);
85  setMatrix(m);
87 
88  m_x = x;
89  m_y = y;
90  }
91 }
void setDrawingMode(unsigned int mode)
const QColor & color() const const
const QSGGeometry::AttributeSet & defaultAttributes_Point2D()
void setMatrix(const QMatrix4x4 &matrix)
void updateGeometry(float x, float y, int width, int height, bool filled)
Redraw ellipse with the given width, height and positions (x,y)
Definition: ellipsenode.cpp:52
void markDirty(QSGNode::DirtyState bits)
void setColor(const QColor &color)
float lineWidth() const const
bool isValid() const const
QSGGeometry::Point2D * vertexDataAsPoint2D()
void setLineWidth(float width)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Aug 11 2022 03:59:58 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.