#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QImage>
#include "m3dme.h"
#include <QListWidget>
#include <iostream>
#include <QMouseEvent>
#include <map>
#include "file.h"
#include "m3dm.h"

#include <math.h>

M3D::M3DME LoadedMesh;
M3D::M3DM LandscapeModel;
bool loaded = false;
int water_level = 0;
float scale = 1.0;
float *pixmap = 0;
int object_count = 0;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    progressbar.hide();
    ui->statusBar->addWidget(&progressbar, 1);

    connect(&timer, SIGNAL(timeout()), this, SLOT(update_fps()));
    timer.start(1000);

    ui->HeightmapPreview->set_engine_preview(ui->EngineWidget);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionOpen_triggered()
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::AnyFile);
    QString fileName = dialog.getOpenFileName(this, tr("Open landscape model"), "", tr("Magic3D model files (*.m3dm)"));
    if(fileName.isEmpty())return;
    LandscapeModel.load( fileName.toUtf8().data());
    std::cerr<<"BasePath:"<<LandscapeModel.get_data()->base_path<<std::endl;
    std::cerr<<"ModelName:"<<LandscapeModel.get_data()->mesh_name<<std::endl;
    LoadedMesh.load(std::string(LandscapeModel.get_data()->base_path) + LandscapeModel.get_data()->mesh_name);
    ui->EngineWidget->set_landscape(fileName.toUtf8().data());
    loaded = true;
    ui->EngineWidget->clear_object_list();
    draw_heightmap();
}

void MainWindow::draw_heightmap()
{
    if(!loaded)return;
    int image_size = LoadedMesh.get_header()->vertex_data_size/sizeof(M3D::M3DME_Vertex_Data);
    unsigned int w = (int)sqrt(image_size);
    unsigned int h = w;
    rebuild_pixmap( w, h );
    QImage image( w, h, QImage::Format_RGB32);

    QString text = QString("%1(%2x%3)").arg(image_size).arg(w).arg(h);

    for(unsigned int y=0;y<h;++y)
       for(unsigned int x=0;x<w;++x)
        {
            float c = pixmap[y*w+x];
            unsigned int color = (int)(16777216.*c);
            if(c > (float)water_level/16777216.)image.setPixel(x, y, color/*qRgb(color, color, color)*/);
            else image.setPixel(x, y, qRgb(0, 0, 255));
        }
    clean_image = QPixmap::fromImage(image);

    w = (int)(sqrt(image_size)*scale);
    h = w;

    QImage img = scale_heightmap(scale, clean_image.toImage());
    clean_image = QPixmap::fromImage(img);

    QPixmap pix = QPixmap::fromImage(image);
    pix = pix.scaled(w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    ui->HeightmapPreview->update_preview(pix);
}

void MainWindow::on_ScaleSlider_sliderMoved(int value)
{
    ui->ScaleAmount->display((float)value*0.01);
    scale = (float)value*0.01;
    draw_heightmap();
}
void MainWindow::rebuild_pixmap(int w, int h)
{
    if(pixmap){delete[] pixmap; pixmap=0;}
    int image_size = LoadedMesh.get_header()->vertex_data_size/sizeof(M3D::M3DME_Vertex_Data);
    pixmap = new float[w*h];
    for(int a=0;a<w*h;++a)pixmap[a] = -10.0;

    for(int a=0; a<image_size; ++a)
    {
        float x = LoadedMesh.get_vertex_data()[a].x + 0.5;
        float y = LoadedMesh.get_vertex_data()[a].y;
        float z = LoadedMesh.get_vertex_data()[a].z + 0.5;
        int xi = (int)round(x*w);
        int yi = (int)round(z*h);
        if(xi>=w)xi=w-1;
        if(yi>=h)yi=h-1;
        pixmap[yi*w+xi]=y;
        std::cerr<<"Writing to :"<<yi*w+xi <<"of:"<<image_size<<std::endl;
    }
}
void MainWindow::on_MouseMove(QMouseEvent *event)
{
    int x, y;
    x = event->x();
    y = event->y();
    QImage img = clean_image.toImage();
    int w = img.width();
    int h = img.height();
    float pos_x = (float)x/(float)w - 0.5;
    float pos_y = (float)y/(float)h - 0.5;
    if(event->x() >= img.width() || event->y() >= img.height())return;
    unsigned int c = (img.pixel(event->x(), event->y()))^0xff000000;
    float scale = ui->NextObjectScale->value();
    float cf = (float)c/16777216. + 0.020*scale;
    //Посадим елку если нажали ЛКМ
    if( event->buttons() & Qt::LeftButton && event->modifiers()==Qt::NoModifier)
    {
        QTreeWidgetItem *item = ui->ObjectTree->currentItem();
        if(!item)return;
        char *current_object = item->text(0).toUtf8().data();
        if(!ui->EngineWidget->check_base_object(current_object))return;
        if(c==255.)return;

        int id = ui->EngineWidget->add_object(current_object, pos_x, cf, pos_y, scale);
        if(id!=-1)
        {
            QString name = QString("%1").arg(id);
            item->addChild(new QTreeWidgetItem(item, QStringList(name)));
        }
        object_count++;

        draw_heightmap();
        ui->ObjectCount->display(object_count);
    }
    //Выделяем елку если нажато шифт+лкм
    if( event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier)
    {
        QTreeWidgetItem *item = ui->ObjectTree->currentItem();
        if(!item)return;
        char *current_object = item->text(0).toUtf8().data();
        if(!ui->EngineWidget->check_base_object(current_object))return;
        if(c==255.)return;

        int id = ui->EngineWidget->find_nearest_object(current_object, pos_x, pos_y);
        if(id!=-1)
        {
            QString name = QString("%1").arg(id);
            int c_count = item->childCount();
            for(int a=0; a<c_count; ++a)
            {
                QTreeWidgetItem *child = item->child(a);
                if(child->text(0) == name)
                {
                    child->setSelected(true);
                    break;
                }
            }
        }

        draw_heightmap();
        ui->ObjectCount->display(object_count);
    }

    if(event->buttons() & Qt::RightButton && event->modifiers()==Qt::NoModifier)
    {
        ui->EngineWidget->camera_position.x_pos= - pos_x;
        ui->EngineWidget->camera_position.z_pos= - pos_y;
        ui->CamX->display(ui->EngineWidget->camera_position.x_pos);
        ui->CamY->display(ui->EngineWidget->camera_position.y_pos);
        ui->HeightmapPreview->update_preview();
    }
    if(event->buttons() & Qt::RightButton && event->modifiers()&Qt::ShiftModifier)
    {
        int id = ui->EngineWidget->add_cam_path_point(-pos_x, -cf-0.01, -pos_y, ui->CamXAngle->value(), ui->CamYAngle->value(), 0, false, 1.0);
        ui->HeightmapPreview->update_preview();
        QString name = QString("%1").arg(id);
        ui->CameraPath->addTopLevelItem(new QTreeWidgetItem((QTreeWidget*)0, QStringList(name)));
    }
    if(event->buttons() & Qt::MidButton)
    {
        ui->EngineWidget->light_position.x_pos = pos_x;
        ui->EngineWidget->light_position.z_pos = pos_y;
        ui->LightX->display(pos_x);
        ui->LightY->display(pos_y);
    }

}

void MainWindow::on_horizontalSlider_sliderMoved(int position)
{
    ui->WaterLevel->display((float)position/16777216.);
    water_level = position;
    ui->EngineWidget->water_position.y_pos = (float)position/16777216.;
    draw_heightmap();
}

void MainWindow::on_verticalSlider_sliderMoved(int position)
{
    ui->EngineWidget->light_position.y_pos = (float)position/1000.;
    ui->LightZ->display( ui->EngineWidget->light_position.y_pos );
}

void MainWindow::on_actionSave_triggered()
{
    QFileDialog dialog;
    dialog.setDefaultSuffix("nyps");
    dialog.setNameFilter(tr("NYP2010 scene file(*.nyps)"));
    dialog.setLabelText(QFileDialog::Accept, tr("Save"));
    dialog.setAcceptMode(QFileDialog::AcceptSave);
    dialog.setViewMode(QFileDialog::Detail);
    QString saveFileName;
    if(dialog.exec())
    {
        QStringList saveFileNames = dialog.selectedFiles();
        saveFileName = saveFileNames.at(0);
        ui->EngineWidget->save(saveFileName.toUtf8().data());
    }

}

void MainWindow::on_actionOpen_data_triggered()
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::AnyFile);
    QString fileName = dialog.getOpenFileName(this, tr("Open mesh"), "", tr("NYP2010 scene files (*.nyps)"));
    if(fileName.isEmpty())return;

    ui->EngineWidget->load(fileName.toUtf8().data());

    draw_heightmap();
    ui->XScale->setValue(ui->EngineWidget->base_scale.x_scale);
    ui->YScale->setValue(ui->EngineWidget->base_scale.y_scale);
    ui->ZScale->setValue(ui->EngineWidget->base_scale.z_scale);
    ui->CamXDial->setValue(ui->EngineWidget->camera_rotation.x_angle*10.0);
    ui->CamYDial->setValue(ui->EngineWidget->camera_rotation.y_angle*10.0);
    ui->verticalSlider_2->setValue(ui->EngineWidget->camera_position.y_pos * 100000.);
    ui->CamX->display(ui->EngineWidget->camera_position.x_pos);
    ui->CamY->display(ui->EngineWidget->camera_position.z_pos);
    ui->CamZ->display(ui->EngineWidget->camera_position.y_pos);
    ui->LightX->display( ui->EngineWidget->light_position.x_pos);
    ui->LightY->display( ui->EngineWidget->light_position.z_pos);
    ui->LightZ->display( ui->EngineWidget->light_position.y_pos);
    ui->verticalSlider->setValue(ui->EngineWidget->camera_position.y_pos * 1000.);
    ui->WaterLevel->display(ui->EngineWidget->water_position.y_pos);
    water_level = ui->EngineWidget->water_position.y_pos * 16777216.;
    ui->horizontalSlider->setValue(water_level);

//Build object tree
    object_count = 0;
    for(std::map<std::string, BaseObject>::iterator base_it=ui->EngineWidget->models.begin(); base_it!=ui->EngineWidget->models.end(); ++base_it)
    {
        ui->ObjectTree->clear();
        QTreeWidgetItem *item = new QTreeWidgetItem((QTreeWidget*)0, QStringList(base_it->second.name.c_str()));
        if(!item)return;
        ui->ObjectTree->addTopLevelItem(item);
        BaseObject *base = &(base_it->second);
        for(std::map<M3D::ModelInterface, ObjectInfo>::iterator it=base->model_list.begin(); it!=base->model_list.end();++it)
        {
            QString name = QString("%1").arg(it->first);
            item->addChild(new QTreeWidgetItem(item, QStringList(name)));
            object_count++;
        }
    }

    //Build campath
    for(int a=0; a<ui->EngineWidget->camera_path.size(); ++a)
    {
        QString name = QString("%1").arg(a);
        ui->CameraPath->addTopLevelItem(new QTreeWidgetItem((QTreeWidget*)0, QStringList(name)));
    }

    ui->ObjectCount->display(object_count);
}

void MainWindow::on_CamXDial_sliderMoved(int position)
{
    float angle = (float)position/10.0;
    ui->CamXAngle->display(angle);
    ui->EngineWidget->camera_rotation.x_angle = angle;
}

void MainWindow::on_CamYDial_sliderMoved(int position)
{
    float angle = (float)position/10.0;
    ui->CamYAngle->display(angle);
    ui->EngineWidget->camera_rotation.y_angle = angle;
    ui->HeightmapPreview->update_preview();
}

void MainWindow::on_verticalSlider_2_sliderMoved(int position)
{
    ui->EngineWidget->camera_position.y_pos = -(float)position/100000.;
    ui->CamZ->display( ui->EngineWidget->camera_position.y_pos );
}

void MainWindow::on_XScale_valueChanged(double value)
{
    ui->EngineWidget->base_scale.x_scale = value;
}

void MainWindow::on_YScale_valueChanged(double value)
{
    ui->EngineWidget->base_scale.y_scale = value;
}

void MainWindow::on_ZScale_valueChanged(double value)
{
    ui->EngineWidget->base_scale.z_scale = value;
}

void MainWindow::on_actionLoad_water_object_triggered()
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::AnyFile);
    QString fileName = dialog.getOpenFileName(this, tr("Open water model"), "", tr("Magic3D model files (*.m3dm)"));
    if(fileName.isEmpty())return;
    ui->EngineWidget->set_water(fileName.toUtf8().data());
}

void MainWindow::on_actionLoadObject_triggered()
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::AnyFile);
    QString fileName = dialog.getOpenFileName(this, tr("Open water model"), "", tr("Magic3D model files (*.m3dm)"));
    if(fileName.isEmpty())return;

    M3D::M3DM model;
    model.load(fileName.toUtf8().data());
    ui->ObjectTree->addTopLevelItem(new QTreeWidgetItem((QTreeWidget*)0, QStringList(model.get_data()->model_name)));
    ui->EngineWidget->load_base_object(fileName.toUtf8().data(), model.get_data()->model_name);
}


void MainWindow::on_spinBox_valueChanged(int value )
{
    ui->EngineWidget->frame_delay = value;
}

void MainWindow::on_ObjectTree_itemClicked(QTreeWidgetItem* item, int column)
{
    if(!item->parent())return;
    QTreeWidgetItem *parent = item->parent();
    char *base_object = parent->text(0).toUtf8().data();
    char *current_object = item->text(0).toUtf8().data();
    int o_id = atoi(current_object);
    current_object_info = ui->EngineWidget->get_object_info(base_object, o_id);
    if(!current_object_info)return;
    ui->ObjectXPos->setValue(current_object_info->position.x_pos);
    ui->ObjectYPos->setValue(current_object_info->position.y_pos);
    ui->ObjectZPos->setValue( current_object_info->position.z_pos);
    ui->ObjectXRotation->setValue( current_object_info->rotation.x_angle);
    ui->ObjectYRotation->setValue( current_object_info->rotation.y_angle);
    ui->ObjectZRotation->setValue( current_object_info->rotation.z_angle);
    ui->ObjectXScale->setValue( current_object_info->scale.x_scale);
    ui->ObjectYScale->setValue( current_object_info->scale.y_scale);
    ui->ObjectZScale->setValue( current_object_info->scale.z_scale);
}


void MainWindow::on_ObjectXPos_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->position.x_pos = value;
}

void MainWindow::on_ObjectYPos_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->position.y_pos = value;
}

void MainWindow::on_ObjectZPos_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->position.z_pos = value;
}

void MainWindow::on_ObjectXScale_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->scale.x_scale= value;
}

void MainWindow::on_ObjectYScale_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->scale.y_scale= value;
}

void MainWindow::on_ObjectZScale_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->scale.z_scale= value;
}

void MainWindow::on_ObjectXRotation_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->rotation.x_angle = value;
}

void MainWindow::on_ObjectYRotation_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->rotation.y_angle = value;
}
void MainWindow::on_ObjectZRotation_valueChanged(double value)
{
    if(!current_object_info)return;
    current_object_info->rotation.z_angle = value;
}
QImage MainWindow::scale_heightmap(float scale, QImage input)
{
   float sx = scale, sy = scale;
   int w = (float)input.width() * sx + 1;
   int h = (float)input.height() * sy + 1;
   if(sx==1 && sy==1)return input;
   QImage output(w+1, h+1, QImage::Format_RGB32);
   float sx1,sy1;
   sx1=1./sx;
   sy1=1./sy;
   unsigned int NC;
   for(short int y=0;y<h;y++)
    for(short int x=0;x<w;x++)
    {
      NC=Bilinear((float)x*sx1, (float)y*sy1, &input);
      output.setPixel(x, y, NC);
    }
  return output;
}
unsigned int MainWindow::Bilinear(float x1,float y1,QImage *surface)
{
  short int x11,y11;
  float rx,ry;
  unsigned int c[4],pixel;
  unsigned int oc[2];
  x11=x1;
  y11=y1;
  rx=(float)x1-x11;
  ry=(float)y1-y11;
  if(x11>=surface->width() || y11>=surface->height())return 0;
  c[0]=surface->pixel( x11, y11 )^0xff000000;

  if(x11+1>=surface->width())c[1] = c[0];
  else c[1]=surface->pixel(x11+1,y11)^0xff000000;

  if(y11+1>=surface->height())c[2] = c[0];
  else c[2]=surface->pixel(x11,y11+1)^0xff000000;

  if(x11+1<surface->width() && y11+1<surface->height())c[3]=surface->pixel(x11+1,y11+1)^0xff000000;
  else if(x11+1<surface->width())c[3] = c[1];
  else if(y11+1<surface->height())c[3] = c[2];
  else c[3] = c[0];

  if(c[2]>c[0])oc[0] = c[0] + (float)(c[2]-c[0])*ry;
  else oc[0] = c[0] - (float)(c[0]-c[2])*ry;
  if(c[3]>c[1])oc[1] = c[1] + (float)(c[3]-c[1])*ry;
  else oc[1] = c[1] - (float)(c[1]-c[3])*ry;
  if(oc[1]>oc[0])pixel = oc[0] + (float)(oc[1]-oc[0])*rx;
  else pixel = oc[0] - (float)(oc[0]-oc[1])*rx;
  return pixel;
}


void MainWindow::on_actionExit_triggered()
{
}

void MainWindow::on_pushButton_pressed()
{
    QList<QTreeWidgetItem*> items = ui->ObjectTree->selectedItems();
    if(items.isEmpty())return;
    QList<QTreeWidgetItem*>::iterator it;
    progressbar.setMaximum(items.size());
    progressbar.setValue(0);
    progressbar.show();
    int a = 0;
    for(it=items.begin(); it!=items.end(); ++it)
    {
        progressbar.setValue(++a);

        QTreeWidgetItem *item = *it;
        if(!item)continue;
        QTreeWidgetItem *base_object = item->parent();
        if(!base_object)continue;
        char *current_object = item->text(0).toUtf8().data();
        char *base_object_name = base_object->text(0).toUtf8().data();
        int o_id = atoi(current_object);

        if(!ui->EngineWidget->remove_object(base_object_name, o_id))continue;
        base_object->removeChild(item);
        --object_count;
    }
    draw_heightmap();
    ui->ObjectCount->display(object_count);
}

void MainWindow::on_Generate_pressed()
{
    QTreeWidgetItem *item = ui->ObjectTree->currentItem();
    if(!item)return;
    std::string current_object = item->text(0).toUtf8().data();
    if(!ui->EngineWidget->check_base_object(current_object.c_str()))return;

    float min_scale = ui->GenMinScale->value();
    float max_scale = ui->GenMaxScale->value();
    int count = ui->GenCount->value();
    progressbar.setMaximum(count/10);
    progressbar.setValue(0);
    progressbar.show();
    QRectF work_rect = ui->HeightmapPreview->get_selected_rect();
    for(int a=0;a<count;++a)
    {
        float x_pos = (float)rand()/(float)RAND_MAX;
        float z_pos = (float)rand()/(float)RAND_MAX;
        float scale = (float)rand()/(float)RAND_MAX;
        scale *= max_scale - min_scale;
        scale += min_scale;
        x_pos *= work_rect.width();
        x_pos += work_rect.x();
        x_pos -= 0.5;
        z_pos *= work_rect.height();
        z_pos += work_rect.y();
        z_pos -= 0.5;


        QImage img = clean_image.toImage();
        int w = img.width();
        int h = img.height();
        int x = round((x_pos + 0.5)*(float)w);
        int y = round((z_pos + 0.5)*(float)h);
        if(x >= img.width() || y >= img.height()){a--; continue;}
        unsigned int c = (img.pixel(x, y))^0xff000000;
        float y_pos = (float)c/16777216. + 0.020*scale;
        if(c==255.){a--; continue;}

        int id = ui->EngineWidget->add_object(current_object.c_str(), x_pos, y_pos, z_pos, scale);
        if(id!=-1)
        {
            QString name = QString("%1").arg(id);
            item->addChild(new QTreeWidgetItem(item, QStringList(name)));
        }
        else
        {
            std::cerr<<"Error on object: "<<a<<std::endl;
        }
        object_count++;
        if(a%10)progressbar.setValue(a/10);
    }
    progressbar.hide();
    draw_heightmap();
    ui->ObjectCount->display(object_count);
}
void MainWindow::update_fps()
{
    static int old_frame = 0;
    int df = ui->EngineWidget->frame_no - old_frame;
    old_frame = ui->EngineWidget->frame_no;
    ui->FPSCounter->display((float)df);
}

void MainWindow::on_CameraPath_itemClicked(QTreeWidgetItem* item, int column)
{
    char *current_object = item->text(0).toUtf8().data();
    int o_id = atoi(current_object);
    current_cam_path_point = ui->EngineWidget->get_cam_path_point(o_id);
    if(!current_cam_path_point)return;
    ui->CamPathPointX->setValue(current_cam_path_point->position.x_pos);
    ui->CamPathPointY->setValue(current_cam_path_point->position.y_pos);
    ui->CamPathPointZ->setValue(current_cam_path_point->position.z_pos);

    ui->CamPathXAng->display(current_cam_path_point->rotation.x_angle);
    ui->CamPathYAng->display(current_cam_path_point->rotation.y_angle);
    ui->CamPathXAngSlider->setValue(current_cam_path_point->rotation.x_angle*10);
    ui->CamPathYAngSlider->setValue(current_cam_path_point->rotation.y_angle*10);

    ui->CamPathIsLoopStart->setChecked(current_cam_path_point->is_loop_start);
    ui->CamPathLength->setValue(current_cam_path_point->length);
}

void MainWindow::on_CamPathPointX_valueChanged(double value)
{
    if(!current_cam_path_point)return;
    current_cam_path_point->position.x_pos = value;
    ui->HeightmapPreview->update_preview();
}

void MainWindow::on_CamPathPointY_valueChanged(double value)
{
    if(!current_cam_path_point)return;
    current_cam_path_point->position.y_pos = value;
    ui->HeightmapPreview->update_preview();
}

void MainWindow::on_CamPathPointZ_valueChanged(double value)
{
    if(!current_cam_path_point)return;
    current_cam_path_point->position.z_pos = value;
    ui->HeightmapPreview->update_preview();
}

void MainWindow::on_CamPathXAngSlider_valueChanged(int value)
{
    if(!current_cam_path_point)return;
    current_cam_path_point->rotation.x_angle = (float)value/10.;
    ui->CamPathXAng->display(current_cam_path_point->rotation.x_angle);
    ui->HeightmapPreview->update_preview();
}

void MainWindow::on_CamPathYAngSlider_valueChanged(int value)
{
    if(!current_cam_path_point)return;
    current_cam_path_point->rotation.y_angle = (float)value/10.;
    ui->CamPathYAng->display(current_cam_path_point->rotation.y_angle);
    ui->HeightmapPreview->update_preview();
}

void MainWindow::on_CamPathIsLoopStart_stateChanged(int )
{
    if(!current_cam_path_point)return;
    current_cam_path_point->is_loop_start = ui->CamPathIsLoopStart->isChecked();
    ui->HeightmapPreview->update_preview();
}

void MainWindow::on_CamPathLength_valueChanged(double value)
{
    if(!current_cam_path_point)return;
    current_cam_path_point->length = value;
}

void MainWindow::on_pushButton_4_pressed()
{
    if(!current_cam_path_point)return;
    ui->EngineWidget->camera_position = current_cam_path_point->position;
    ui->EngineWidget->camera_rotation = current_cam_path_point->rotation;

    ui->CamXDial->setValue(ui->EngineWidget->camera_rotation.x_angle*10.0);
    ui->CamYDial->setValue(ui->EngineWidget->camera_rotation.y_angle*10.0);
    ui->verticalSlider_2->setValue(ui->EngineWidget->camera_position.y_pos * 100000.);
    ui->CamX->display(ui->EngineWidget->camera_position.x_pos);
    ui->CamY->display(ui->EngineWidget->camera_position.z_pos);
    ui->CamZ->display(ui->EngineWidget->camera_position.y_pos);
}

void MainWindow::on_pushButton_5_pressed()
{
    if(!current_cam_path_point)return;
    current_cam_path_point->position = ui->EngineWidget->camera_position;
    current_cam_path_point->rotation = ui->EngineWidget->camera_rotation;

    ui->CamPathPointX->setValue(current_cam_path_point->position.x_pos);
    ui->CamPathPointY->setValue(current_cam_path_point->position.y_pos);
    ui->CamPathPointZ->setValue(current_cam_path_point->position.z_pos);

    ui->CamPathXAng->display(current_cam_path_point->rotation.x_angle);
    ui->CamPathYAng->display(current_cam_path_point->rotation.y_angle);
    ui->CamPathXAngSlider->setValue(current_cam_path_point->rotation.x_angle*10);
    ui->CamPathYAngSlider->setValue(current_cam_path_point->rotation.y_angle*10);
}

void MainWindow::on_pushButton_2_pressed()
{
    ui->EngineWidget->current_point = 0;
    ui->EngineWidget->animation_timer.reset();
    ui->EngineWidget->isAnimationMode = true;
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->EngineWidget->isAnimationMode = false;
    ui->EngineWidget->current_point = 0;
}
