QML Moving by coordinates using animation

Viewed 26

I need to move the cube according to the coordinates that I enter in ScrollView.

import QtQuick 2.15
import QtQuick.Window 2.14
import QtQuick3D 1.15
import QtQuick.Controls 2.14

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    visibility: "Maximized"
    property int move_x: 5
    property int move_y: 5
    property int move_z: 5
    Node{
        id: standAloneScene
        DirectionalLight {
            ambientColor: Qt.rgba(1.0, 1.0, 1.0, 1.0)
        }
        Node{
            Model {
                id: model
                source: "#Cube"
                x: 0
                y: 0
                z: 0
                materials: [
                    DefaultMaterial {
                        diffuseColor: Qt.rgba(0.053, 0.130, 0.219, 0.75)
                    }
                ]
            }
        }
        ParallelAnimation{
            id: moving
            NumberAnimation {
                target: model
                property: "x"
                from: 0
                to: move_x
                duration: 2000
                easing.type: Easing.InOutQuad
            }
            NumberAnimation {
                target: model
                property: "y"
                from: 0
                to: move_y
                duration: 2000
                easing.type: Easing.InOutQuad
            }
            NumberAnimation {
                target: model
                property: "z"
                from: 0
                to: move_z
                duration: 2000
                easing.type: Easing.InOutQuad
            }
        }

        OrthographicCamera {
            id: cameraOrthographicFront
            eulerRotation.y: 45
            eulerRotation.x: -45
            x: 600
            y: 800
            z: 600
        }
    }
    Rectangle {
        id: view
        anchors.top: parent.top
        anchors.left: parent.left
        width: parent.width
        height: parent.height
        color: "#848895"

        View3D {
            id: isometria
            anchors.fill: parent
            importScene: standAloneScene
            camera: cameraOrthographicFront
        }

        Rectangle{
            id: control
            color: "grey"
            width: 400
            anchors.top: view.top
            anchors.bottom: view.bottom
            anchors.right: view.right
            Button {
                id: start
                height: control.width/4
                anchors.top: control.top
                anchors.left: control.left
                anchors.right: control.right
                text: "start"
                font.pixelSize: height
                onClicked: {
                    move.getListCoordinates(hellotext.text)
                    for(var i=0; i<move.get_number_crds(); i++){
                        move.next(i);
                        move_x = move.get_x()
                        move_y = move.get_y()
                        move_z = move.get_z()
                        moving.restart();
                    }

                }
            }
            ScrollView{
                id: textarea
                anchors.top: start.bottom
                anchors.right: control.right
                anchors.left: control.left
                anchors.bottom: control.bottom
                TextArea{
                    id: hellotext
                    font.pixelSize: 40
                }
            }
        }
    }
}

When the button is clicked, coordinates are read from ScrollView and written to a vector using the move class function. Then the cycle starts from 0 to the number of elements in the vector. Coordinates are passed in turn to move_x, move_y and move_z, then the animation is restarted. The implementation of the move class is shown below

move.h
#ifndef MOVE_H
#define MOVE_H

#include <QObject>
#include <QDebug>
#include <QVector>

struct Position
{
    int x;
    int y;
    int z;
};

class Move: public QObject
{
    Q_OBJECT

public:
    Move(QObject *parent = nullptr);

private:
    QVector<Position> coordinates;
    Position current_position;

signals:
private:
    Position StringParsing(QString st);

public slots:
    void getListCoordinates(QString list_crd);
    int get_number_crds();
    void next(int i);
    int get_x();
    int get_y();
    int get_z();
};

#endif // MOVE_H


move.cpp
#include "move.h"
#include <QString>

Move::Move(QObject *parent): QObject(parent) {}

Position Move::StringParsing(QString crd_st){
    Position crd;
    crd.x = (crd_st.mid(1, crd_st.indexOf('Y') - 1)).toInt();
    crd.y = crd_st.mid(crd_st.indexOf('Y') + 1, crd_st.indexOf('Z') - crd_st.indexOf('Y') - 1).toInt();
    crd.z = crd_st.mid(crd_st.indexOf('Z') + 1, crd_st.length() - crd_st.indexOf('Z') - 1).toInt();
    return crd;
}

void Move::getListCoordinates(QString list_crd){
    list_crd += "\n";
    while(list_crd.length() != 0){
        QString crd =  list_crd.mid(0, list_crd.indexOf('\n')+1);
        list_crd.remove(0,  list_crd.indexOf('\n')+1);
        coordinates.push_back(StringParsing(crd));
        qDebug() << crd << Qt::endl;
    }
}

int Move::get_number_crds(){
    return coordinates.size();
}

void Move::next(int i){
    current_position = coordinates[i];
}

int Move::get_x(){
    qDebug() << current_position.x << Qt::endl;
    return current_position.x;
}

int Move::get_y(){
    qDebug() << current_position.y << Qt::endl;
    return current_position.y;
}

int Move::get_z(){
    qDebug() << current_position.z << Qt::endl;
    return current_position.z;
}

But the object is moved only to the last coordinates I recorded. Can you tell me how I can implement this? Thanks

0 Answers
Related