qtk/src/designer-plugins/toolbox.cpp
2025-03-23 15:31:47 -04:00

124 lines
4.2 KiB
C++

/*##############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2023 Shaun Reed, all rights reserved ##
## About: Toolbox plugin for object details and options ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
################################################################################
*/
#include "toolbox.h"
#include "qtkwidget.h"
#include "ui_toolbox.h"
#include <QFormLayout>
#include <QLabel>
using namespace Qtk;
ToolBox::ToolBox(QWidget * parent) :
QDockWidget(parent), objectDetails_(this),
transformPanel_(this, "Transform:"), scalePanel_(this, "Scale:"),
vertex_(this, "Vertex Shader:"), fragment_(this, "Fragment Shader:"),
properiesForm_(new QFormLayout), shaderForm_(new QFormLayout),
ui(new Ui::ToolBox)
{
ui->setupUi(this);
setMinimumWidth(350);
// Object Properties.
ui->page_properties->setLayout(properiesForm_);
properiesForm_->addRow(objectDetails_.name.label, objectDetails_.name.value);
properiesForm_->addRow(objectDetails_.objectType.label,
objectDetails_.objectType.value);
properiesForm_->addRow(reinterpret_cast<QWidget *>(&transformPanel_));
properiesForm_->addRow(reinterpret_cast<QWidget *>(&scalePanel_));
ui->toolBox->setCurrentWidget(ui->page_properties);
// Shader views.
ui->page_shaders->setLayout(shaderForm_);
shaderForm_->addRow(vertex_.path.label, vertex_.path.value);
shaderForm_->addRow(vertex_.editor);
shaderForm_->addRow(fragment_.path.label, fragment_.path.value);
shaderForm_->addRow(fragment_.editor);
}
void ToolBox::updateFocus(const QString & name)
{
auto object =
QtkWidget::mWidgetManager.get_widget()->getScene()->getObject(name);
if (object != Q_NULLPTR) {
refreshProperties(object);
refreshShaders(object);
}
}
ToolBox::SpinBox3D::SpinBox3D(QLayout * layout, const char * l) :
label(new QLabel(tr(l))), x(new QDoubleSpinBox), y(new QDoubleSpinBox),
z(new QDoubleSpinBox), fields({x, y, z})
{
// The layout owns the widget and will clean it up on destruction.
layout->addWidget(label);
for (const auto & f : fields) {
layout->addWidget(f);
f->setMinimum(std::numeric_limits<double>::lowest());
f->setSingleStep(0.1);
f->setFixedWidth(75);
}
}
ToolBox::~ToolBox()
{
delete ui;
}
void ToolBox::refreshProperties(const Object * object)
{
objectDetails_.setDetails(object);
// Reconnect transform panel controls to the new object.
connect(transformPanel_.spinBox.x,
&QDoubleSpinBox::valueChanged,
object,
&Object::setTranslationX);
connect(transformPanel_.spinBox.y,
&QDoubleSpinBox::valueChanged,
object,
&Object::setTranslationY);
connect(transformPanel_.spinBox.z,
&QDoubleSpinBox::valueChanged,
object,
&Object::setTranslationZ);
// Set the values in the spin box to the object's current X,Y,Z position.
auto transform = object->getTransform();
for (size_t i = 0; i < 3; i++) {
transformPanel_.spinBox.fields[i]->setValue(transform.getTranslation()[i]);
}
// Reconnect scale panel controls to the new object.
connect(scalePanel_.spinBox.x,
&QDoubleSpinBox::valueChanged,
object,
&Object::setScaleX);
connect(scalePanel_.spinBox.y,
&QDoubleSpinBox::valueChanged,
object,
&Object::setScaleY);
connect(scalePanel_.spinBox.z,
&QDoubleSpinBox::valueChanged,
object,
&Object::setScaleZ);
// Set the values in the spin box to the object's current X,Y,Z scale.
for (size_t i = 0; i < 3; i++) {
scalePanel_.spinBox.fields[i]->setValue(transform.getScale()[i]);
}
}
void ToolBox::refreshShaders(const Object * object)
{
vertex_.path.setItem(object->getVertexShader().c_str());
vertex_.editor->setText(object->getVertexShaderSourceCode().c_str());
fragment_.path.setItem(object->getFragmentShader().c_str());
fragment_.editor->setText(object->getFragmentShaderSourceCode().c_str());
}