From a4a7ed699792ec173779e921cf7ef3cea4cf0e19 Mon Sep 17 00:00:00 2001 From: consti10 Date: Thu, 26 Oct 2023 15:22:32 +0200 Subject: [PATCH] performancehorizonladder - new simple approach --- app/osd/performancehorizonladder.cpp | 234 ------------------- app/osd/performancehorizonladder.h | 47 ---- qml/ui/HUDOverlayGrid.qml | 2 +- qml/ui/widgets/PerformanceHorizonWidget.qml | 98 -------- qml/ui/widgets/PerformanceHorizonWidget2.qml | 122 ++++++++++ 5 files changed, 123 insertions(+), 380 deletions(-) delete mode 100644 app/osd/performancehorizonladder.cpp delete mode 100644 app/osd/performancehorizonladder.h delete mode 100644 qml/ui/widgets/PerformanceHorizonWidget.qml create mode 100644 qml/ui/widgets/PerformanceHorizonWidget2.qml diff --git a/app/osd/performancehorizonladder.cpp b/app/osd/performancehorizonladder.cpp deleted file mode 100644 index e0889664f..000000000 --- a/app/osd/performancehorizonladder.cpp +++ /dev/null @@ -1,234 +0,0 @@ -#include "performancehorizonladder.h" - -#include -#include -#include -#include - -PerformanceHorizonLadder::PerformanceHorizonLadder(QQuickItem *parent) - : QQuickItem(parent) -{ - setFlags(ItemHasContents); - setClip(false); -} - -PerformanceHorizonLadder::~PerformanceHorizonLadder() -{ - if(m_base_node){ - delete m_base_node; - } - if(m_center_indicator){ - delete m_center_indicator; - } - if(m_ladders_geom_node){ - delete m_ladders_geom_node; - } - if(m_tf_node){ - delete m_tf_node; - } - if(m_flat_color_material){ - delete m_flat_color_material; - } -} - -void PerformanceHorizonLadder::setRoll(int roll) { - m_roll = roll; - emit rollChanged(m_roll); - update(); -} - - -void PerformanceHorizonLadder::setPitch(int pitch) { - m_pitch = pitch; - emit pitchChanged(m_pitch); - update(); -} - - -static QSGGeometry* make_rectangle(int height,int width){ - QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4); - geometry->setDrawingMode(GL_LINES); - geometry->setLineWidth(10); - geometry->vertexDataAsPoint2D()[0].set(0, 0); - geometry->vertexDataAsPoint2D()[1].set(0,width); - geometry->vertexDataAsPoint2D()[2].set(height,width); - geometry->vertexDataAsPoint2D()[3].set(height,0); - return geometry; -} - -struct Vec2{ - float x=0; - float y=0; -}; -static void append_horizontal_line_with_middle_space(std::vector& buff,float x_off,float width,float y_off,float middle_space,float height){ - const float middle_space_half=middle_space/2; - //left up line - buff.push_back({x_off+0,y_off}); - buff.push_back({x_off+0,y_off+height}); - //left to middle - buff.push_back({x_off+0,y_off}); - buff.push_back({x_off+width/2.0f-middle_space_half,y_off}); - // middle to right - buff.push_back({x_off+width/2.0f+middle_space_half,y_off}); - buff.push_back({x_off+width,y_off}); - // right up - buff.push_back({x_off+width,y_off}); - buff.push_back({x_off+width,y_off+height}); -} -static void append_horizontal_line_with_middle_space_and_dashes(std::vector& buff,float x_off,float width,float y_off,float middle_space,float height){ - const float middle_space_half=middle_space/2; - const float horizontal_line_length=width/2.0f-middle_space_half; - const float dash_length=horizontal_line_length/5.0f; - //left down line - buff.push_back({x_off+0,y_off}); - buff.push_back({x_off+0,y_off-height}); - //left to middle,dashes - int count=0; - for(int i=0;i<3;i++){ - buff.push_back({x_off+dash_length*count,y_off}); - count++; - buff.push_back({x_off+dash_length*count,y_off}); - count++; - } - // middle to right - count=0; - for(int i=0;i<3;i++){ - const float start=x_off+width/2.0f+middle_space_half; - buff.push_back({start+dash_length*count,y_off}); - count++; - buff.push_back({start+dash_length*count,y_off}); - count++; - } - // right down line - buff.push_back({x_off+width,y_off}); - buff.push_back({x_off+width,y_off-height}); -} - -static void append_horizontal_line(std::vector& buff,float x,float y,float width){ - buff.push_back({x,y}); - buff.push_back({x+width,y}); -} - -static std::vector make_circle(int segments,float center_x,float center_y,float radius){ - std::vector vertices; - float step = 6.283185f/segments; - float angle = 0.0f; - - vertices.reserve(segments + 1); - for (int i = 0; i < segments; ++i, angle+=step) { - float vertX = center_x + cosf(angle)*radius; - float vertY = center_y + sinf(angle)*radius; - vertices.push_back({vertX, vertY}); - } - vertices.push_back({center_x+radius, center_y}); - return vertices; -} - -static QSGGeometry* qsggeometry_from_array(const std::vector& vec){ - QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vec.size()); - for(int i=0;ivertexDataAsPoint2D()[i].set(vec[i].x,vec[i].y); - } - geometry->setDrawingMode(GL_LINES); - geometry->setLineWidth(1); - return geometry; -} - -static QSGGeometry* make_line(int width,int height){ - QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 2); - geometry->setDrawingMode(GL_LINES); - geometry->setLineWidth(3); - /*geometry->vertexDataAsPoint2D()[0].set(-width/2, 0); //height/2 - geometry->vertexDataAsPoint2D()[1].set(width/2,0);*/ - geometry->vertexDataAsPoint2D()[0].set(0, height/2); //height/2 - geometry->vertexDataAsPoint2D()[1].set(width,height/2); - return geometry; -} - -QSGNode *PerformanceHorizonLadder::updatePaintNode(QSGNode *n, QQuickItem::UpdatePaintNodeData *) -{ - if(!m_base_node){ - m_base_node = new QSGNode(); - m_ladders_geom_node = new QSGGeometryNode(); - m_tf_node = new QSGTransformNode(); - m_center_indicator = new QSGGeometryNode(); - - m_flat_color_material = new QSGFlatColorMaterial(); - m_flat_color_material->setColor(QColor(255, 255, 255)); - - const auto m_width=width(); - const auto m_height=height(); - - { - //auto geometry=make_rectangle(width(),height()); - //auto geometry=make_line(width(),height()); - auto vertices=std::vector(); - // the middle, big line - append_horizontal_line(vertices,0,height()/2,width()); - // lower/ upper lines - const auto l_width=m_width * 0.6f; - const auto l_x_offset= m_width/2.0f-l_width/2.0f; - // upper lines - for(int i=1;i<9;i++){ //9 = 9 times 10 degree - const auto l_y_offset=height()/2.0f-height()/2.0f*i*1.2; - append_horizontal_line_with_middle_space(vertices,l_x_offset,l_width,l_y_offset,width()/4.0f,height()/30.0f); - } - // lower lines - for(int i=1;i<9;i++){ //9 = 9 times 10 degree - const auto l_y_offset=height()/2.0f+height()/2.0f*i*1.2; - append_horizontal_line_with_middle_space_and_dashes(vertices,l_x_offset,l_width,l_y_offset,width()/4.0f,height()/20.0f); - } - //append_horizontal_line_with_middle_space(vertices,width(),width()/2,height()/10,-height()/2); - - auto geometry=qsggeometry_from_array(vertices); - - m_ladders_geom_node->setGeometry(geometry); - m_ladders_geom_node->setFlag(QSGNode::OwnsGeometry); - m_ladders_geom_node->setMaterial(m_flat_color_material); - m_ladders_geom_node->setFlag(QSGNode::OwnsMaterial); - } - { - //auto vertices=std::vector(); - //const auto center_line_width=width()/8; - //append_horizontal_line(vertices,width()/2.0f-center_line_width/2.0f,height()/2,center_line_width); - const auto radius=width()/38; - auto vertices = make_circle(20,width()/2,height()/2,radius); - auto geometry=qsggeometry_from_array(vertices); - geometry->setDrawingMode(GL_LINE_STRIP); - geometry->setLineWidth(1); - m_center_indicator->setGeometry(geometry); - m_center_indicator->setFlag(QSGNode::OwnsGeometry); - m_center_indicator->setMaterial(m_flat_color_material); - m_center_indicator->setFlag(QSGNode::OwnsMaterial); - } - - m_base_node->appendChildNode(m_tf_node); - m_base_node->appendChildNode(m_center_indicator); - m_tf_node->appendChildNode(m_ladders_geom_node); - } - //qDebug()<<"LOOOL"; - - QRectF rect(boundingRect()); - - //QSGGeometry::updateColoredRectGeometry(node->geometry(),rect); - //QSGGeometry::updateTexturedRectGeometry(node->geometry(), rect, texture_coords); - m_ladders_geom_node->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial); - - { - auto matrix=QMatrix4x4(); - matrix.setToIdentity(); - matrix.translate(width()/2,height()/2,0); - matrix.rotate(m_roll*-1,QVector3D(0,0,1)); - matrix.translate(-width()/2,-height()/2,0); - - matrix.translate({0.0f,m_pitch*18.0f,0.0f}); - m_tf_node->setMatrix(matrix); - } - - //QTransform transform_centerOfWindow( 1, 0, 0, 1, width()/2, height()/2 ); - //transform_centerOfWindow.rotate(m_pitch*18.0f); - - //QMetaObject::invokeMethod(node, "update", Qt::QueuedConnection); - //return node; - return m_base_node; -} diff --git a/app/osd/performancehorizonladder.h b/app/osd/performancehorizonladder.h deleted file mode 100644 index beb09112c..000000000 --- a/app/osd/performancehorizonladder.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef PERFORMANCEHORIZONLADDER_H -#define PERFORMANCEHORIZONLADDER_H - -#include -#include -#include -//#include "../../../lib/lqtutils_master/lqtutils_prop.h" -#include "lib/lqtutils_master/lqtutils_prop.h" - -#include "horizonladder.h" - -class PerformanceHorizonLadder : public QQuickItem -{ - Q_OBJECT -public: - PerformanceHorizonLadder(QQuickItem *parent = nullptr); - ~ PerformanceHorizonLadder(); - - Q_PROPERTY(int pitch MEMBER m_pitch WRITE setPitch NOTIFY pitchChanged) - Q_PROPERTY(int roll MEMBER m_roll WRITE setRoll NOTIFY rollChanged) - // - void setRoll(int roll); - void setPitch(int pitch); -signals: - void rollChanged(int roll); - void pitchChanged(int pitch); - // QQuickItem interface -protected: - QSGNode *updatePaintNode(QSGNode *n, UpdatePaintNodeData *) override; - -private: - QSGNode *m_base_node=nullptr; - // node for the center indicator, never translated - QSGGeometryNode *m_center_indicator=nullptr; - // node for the ladder lines, translated - QSGGeometryNode *m_ladders_geom_node=nullptr; - QSGTransformNode* m_tf_node=nullptr; - // - QSGFlatColorMaterial *m_flat_color_material=nullptr; -private: - int m_roll; - int m_pitch; -private: - HorizonLadder* m_hl=nullptr; -}; - -#endif // PERFORMANCEHORIZONLADDER_H diff --git a/qml/ui/HUDOverlayGrid.qml b/qml/ui/HUDOverlayGrid.qml index f4779450f..afe49440a 100644 --- a/qml/ui/HUDOverlayGrid.qml +++ b/qml/ui/HUDOverlayGrid.qml @@ -368,7 +368,7 @@ Item { id: horizonWidget } - PerformanceHorizonWidget{ + PerformanceHorizonWidget2{ id: performanceHorizonWidget } diff --git a/qml/ui/widgets/PerformanceHorizonWidget.qml b/qml/ui/widgets/PerformanceHorizonWidget.qml deleted file mode 100644 index e36f74326..000000000 --- a/qml/ui/widgets/PerformanceHorizonWidget.qml +++ /dev/null @@ -1,98 +0,0 @@ -import QtQuick 2.12 -import QtQuick.Controls 2.12 -import QtQuick.Layouts 1.12 -import QtGraphicalEffects 1.12 -import Qt.labs.settings 1.0 - -import OpenHD 1.0 - -BaseWidget { - id: horizonWidget - width: 250 - height: 48 - - visible: settings.show_performance_horizon - - widgetIdentifier: "performance_horizon_widget" - bw_verbose_name: "PERFORMANCE HORIZON" - - defaultHCenter: true - defaultVCenter: true - - hasWidgetDetail: true - - m_show_grid_when_dragging : true - - widgetDetailComponent: ScrollView { - - contentHeight: idBaseWidgetDefaultUiControlElements.height - ScrollBar.horizontal.policy: ScrollBar.AlwaysOff - clip: true - - BaseWidgetDefaultUiControlElements{ - id: idBaseWidgetDefaultUiControlElements - Item { - width: 230 - height: 32 - Text { - text: qsTr("Width") - color: "white" - height: parent.height - font.bold: true - font.pixelSize: detailPanelFontPixels - anchors.left: parent.left - verticalAlignment: Text.AlignVCenter - } - Slider { - id: horizon_width_Slider - orientation: Qt.Horizontal - from: 1 - value: 0 - to: 10 - stepSize: .1 - height: parent.height - anchors.rightMargin: 0 - anchors.right: parent.right - width: parent.width - 96 - - onValueChanged: { - - } - } - } - } - } - - Item { - id: widgetInner - anchors.fill: parent - opacity: bw_current_opacity - - Rectangle { - id: horizonLadder2 - visible: true - width: 400 - height: 300 - //border.width: 2 - //border.color: "green" - //color: "green" - color: "transparent" - - anchors.centerIn: parent - - PerformanceHorizonLadder{ - id: performanceHorizonLadder - width: 400 - height: 300 - anchors.centerIn: parent - - pitch: _fcMavlinkSystem.pitch - roll: _fcMavlinkSystem.roll - - Behavior on pitch {NumberAnimation { duration: settings.smoothing }} - Behavior on roll {NumberAnimation { duration: settings.smoothing }} - } - - } - } -} diff --git a/qml/ui/widgets/PerformanceHorizonWidget2.qml b/qml/ui/widgets/PerformanceHorizonWidget2.qml new file mode 100644 index 000000000..5dbbb307a --- /dev/null +++ b/qml/ui/widgets/PerformanceHorizonWidget2.qml @@ -0,0 +1,122 @@ +import QtQuick 2.15 +import QtQuick.Controls 2.12 +import QtQuick.Layouts 1.12 +import QtGraphicalEffects 1.12 +import Qt.labs.settings 1.0 +import QtQuick.Shapes 1.0 + +import OpenHD 1.0 + +BaseWidget { + id: horizonWidget + width: 250 + height: 48 + + visible: settings.show_performance_horizon + + widgetIdentifier: "horizon_performance2" + bw_verbose_name: "ARTIFICIAL HORIZON 2" + + defaultHCenter: true + defaultVCenter: true + + hasWidgetDetail: true + widgetDetailHeight: 250+250 + + m_show_grid_when_dragging : true + + widgetDetailComponent: ScrollView { + + contentHeight: idBaseWidgetDefaultUiControlElements.height + ScrollBar.horizontal.policy: ScrollBar.AlwaysOff + clip: true + + BaseWidgetDefaultUiControlElements{ + id: idBaseWidgetDefaultUiControlElements + + show_vertical_lock: true + show_horizontal_lock: true + + } + } + + Item { + id: widgetInner + anchors.fill: parent + opacity: bw_current_opacity + + // actual horizon + Item{ + id: horizon_item + width: parent.width; + height: 4 + anchors.centerIn: parent + // Left line + Rectangle{ + width: parent.width/3 + height: parent.height + anchors.left: parent.left + color: "green" + //color: settings.color_text + //border.color: settings.color_glow + //border.width: 1 + } + // + // Here is the middle, which isn't rotated and therefore not placed in here + // + // Right line + Rectangle{ + width: parent.width/3 + height: parent.height + anchors.right: parent.right + color: "green" + } + transform: [ + // First rotate (roll) + Rotation { + origin.x: horizon_item.width/2; + origin.y: horizon_item.height/2; + //angle: _fcMavlinkSystem.roll*-1 + angle: 0 + }, + // Then translate (pitch) + Translate{ + y: 0 + } + ] + } + // The middle indicator + Rectangle { + id: middle_indicator + height: 20 + width: 20 + radius: width/2; + anchors.centerIn: parent + color: "transparent" + border.color: "green" + border.width: 2 + } + Rectangle{ + id: middle_indicator2 + height: 2; + width: 30 + anchors.centerIn: parent + color: "green" + } + + /*Rectangle{ + width: 3; + height: 2; + color: "green" + anchors.left: middle_indicator.left + anchors.top: middle_indicator.top + }*/ + } + /*Rectangle{ + id: debug_rect + implicitWidth: widgetInner.width + implicitHeight: widgetInner.height + color: "red" + opacity: 0.8 + }*/ +}