#include <QKeyEvent>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->render->addWidget(&_gl);
    _gl.setFocus();
    ui->combo_scenes->addItem("Scene 1");
    ui->combo_scenes->addItem("Scene 2");
    ui->combo_scenes->addItem("Scene 3");
}

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

void MainWindow::on_combo_scenes_currentIndexChanged(const QString &arg1)
{
    _gl.setScene(QString(arg1).remove("Scene ").toInt());
    _gl.setFocus();
}

void MainWindow::on_btn_reset_scene_clicked()
{
    _gl.setScene(ui->combo_scenes->currentText().remove("Scene ").toInt());
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    cout << "key press event : " << event->key() << endl;

    switch (event->key()) {
    case Qt::Key_Escape:
        exit(EXIT_SUCCESS);
        break;
    }
    _gl.update();
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
    cout << "key release event : " << event->key() << endl;
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    cout << "mouse press event : " << event->button() << endl;

    // relative pos in the render frame
    int x = event->pos().x() - _gl.pos().x();
    int y = event->pos().y() - _gl.pos().y();

    // color picking
    if(x >= 0 && x < _gl.camera()->vpWidth())
        if(y >= 0 && y < _gl.camera()->vpHeight())
            _gl.colorPicking(Vector2i(x, y));
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    cout << "mouse move event : " << event->pos().x() << " " << event->pos().y() << endl;
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    cout << "mouse release event : " << event->button() << endl;
}

void MainWindow::wheelEvent(QWheelEvent *event)
{
    cout << "mouse wheel event : " << event->delta() << endl;
}
