/*
    QGLView
    Copyright (C) 2017  Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "globject_editor.h"


GLObjectEditor::GLObjectEditor(QWidget * parent): QWidget(parent) {
	setupUi(this);
	active = true;
	object = 0;
	rmodes << GLObjectBase::View << GLObjectBase::Point << GLObjectBase::Line << GLObjectBase::Fill;
	groupLight->setEnabled(false);
}


void GLObjectEditor::changeEvent(QEvent * e) {
	return;
	QWidget::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		retranslateUi(this);
		break;
	default:
		break;
	}
}


void GLObjectEditor::setObject(GLObjectBase * o) {
	object = o;
	if (object == 0) {
		groupLight->setEnabled(false);
		return;
	}
	active = false;
	spinPosX->setValue(object->posX());
	spinPosY->setValue(object->posY());
	spinPosZ->setValue(object->posZ());
	spinRotationX->setValue(object->rotationX());
	spinRotationY->setValue(object->rotationY());
	spinRotationZ->setValue(object->rotationZ());
	spinScaleX->setValue(object->scaleX());
	spinScaleY->setValue(object->scaleY());
	spinScaleZ->setValue(object->scaleZ());
	spinLineWidth->setValue(object->lineWidth());
	checkVisible->setChecked(object->isVisible());
	checkAcceptLight->setChecked(object->isAcceptLight());
	checkAcceptFog->setChecked(object->isAcceptFog());
	checkCastShadows->setChecked(object->isCastShadows());
	checkReceiveShadows->setChecked(object->isReceiveShadows());
	comboRenderMode->setCurrentIndex(rmodes.indexOf(object->renderMode()));
	groupLight->setEnabled(object->type() == GLObjectBase::glLight);
	if (object->type() == GLObjectBase::glLight) {
		Light * l = globject_cast<Light * >(object);
		//bool is_dir = l->light_type == Light::Directional, is_cone = l->light_type == Light::Cone;
		buttonLightColor->setColor(l->color());
		comboLightType->setCurrentIndex(l->light_type);
		spinLightIntensity->setValue(l->intensity);
		spinLightDecayConst->setValue(l->decay_const);
		spinLightDecayLinear->setValue(l->decay_linear);
		spinLightDecayQuadratic->setValue(l->decay_quadratic);
		spinLightAngleStart->setValue(l->angle_start);
		spinLightAngleEnd->setValue(l->angle_end);
		spinLightDirectionX->setValue(l->direction.x());
		spinLightDirectionY->setValue(l->direction.y());
		spinLightDirectionZ->setValue(l->direction.z());
	}
	active = true;
}


void GLObjectEditor::objectChanged() {
	if (!active || object == 0) return;
	object->setPosX(spinPosX->value());
	object->setPosY(spinPosY->value());
	object->setPosZ(spinPosZ->value());
	object->setRotationX(spinRotationX->value());
	object->setRotationY(spinRotationY->value());
	object->setRotationZ(spinRotationZ->value());
	object->setScaleX(spinScaleX->value());
	object->setScaleY(spinScaleY->value());
	object->setScaleZ(spinScaleZ->value());
	object->setLineWidth(spinLineWidth->value());
	object->setVisible(checkVisible->isChecked());
	object->setAcceptLight(checkAcceptLight->isChecked());
	object->setAcceptFog(checkAcceptFog->isChecked());
	object->setCastShadows(checkCastShadows->isChecked());
	object->setReceiveShadows(checkReceiveShadows->isChecked());
	object->setRenderMode((GLObjectBase::RenderMode)rmodes[comboRenderMode->currentIndex()]);
	if (object->type() == GLObjectBase::glLight) {
		Light * l = globject_cast<Light * >(object);
		//bool is_dir = l->light_type == Light::Directional, is_cone = l->light_type == Light::Cone;
		l->setColor(buttonLightColor->color());
		l->light_type = (Light::Type)comboLightType->currentIndex();
		l->intensity = spinLightIntensity->value();
		l->decay_const = spinLightDecayConst->value();
		l->decay_linear = spinLightDecayLinear->value();
		l->decay_quadratic = spinLightDecayQuadratic->value();
		l->angle_start = spinLightAngleStart->value();
		l->angle_end = spinLightAngleEnd->value();
		l->direction = QVector3D(spinLightDirectionX->value(), spinLightDirectionY->value(), spinLightDirectionZ->value()).normalized();
	}
	emit changed();
}


void GLObjectEditor::on_spinLightAngleStart_valueChanged(double v) {
	if (spinLightAngleEnd->value() < v)
		spinLightAngleEnd->setValue(v);
}


void GLObjectEditor::on_spinLightAngleEnd_valueChanged(double v) {
	if (spinLightAngleStart->value() > v)
		spinLightAngleStart->setValue(v);
}
