Ejemplo n.º 1
0
/****************************************************************************
**
** Copyright (C) 2014
**
** This file is generated by the Magus toolkit
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
****************************************************************************/

// Include
#include <QMessageBox>
#include "ogre_asset_material.h"
#include "asset_propertywidget_xyz.h"
#include "asset_propertywidget_string.h"
#include "asset_propertywidget_checkbox.h"
#include "asset_propertywidget_select.h"
#include "asset_propertywidget_slider.h"
#include "asset_propertywidget_decimal.h"
#include "asset_propertywidget_color.h"
#include "asset_propertywidget_texture.h"

namespace Magus
{
    //****************************************************************************/
    QtOgreAssetMaterial::QtOgreAssetMaterial(const QString& iconDir, QWidget* parent) : QWidget(parent)
    {
        mOgreMaterial = 0;
        mIconDir = iconDir;

        // Create QtAssetWidget
        QVBoxLayout* mainLayout = new QVBoxLayout;
        mainLayout->setContentsMargins(0, 0, 0, 0);
        mAssetWidget = new QtAssetWidget(QString("Material"),
                                         iconDir + QString("material.png"),
                                         this);
        mAssetWidget->setFileNameIconCollapsed(iconDir + QString("collapse.png"));
        mAssetWidget->setFileNameIconExpanded(iconDir + QString("expand.png"));
        mAssetWidget->setHeaderTitleBold();
        connect(mAssetWidget, SIGNAL(valueChanged(QtProperty*)), this, SLOT(propertyValueChanged(QtProperty*)));

        // Create container - General
        QtContainerWidget* container = 0;
        container = mAssetWidget->createContainer(CONTAINER_GENERAL, QString("General"));
        container->setTitleIcon(iconDir + QString("cube_bold.png"));
        container->setTitleBold();

        mAssetWidget->createProperty(CONTAINER_GENERAL, MATERIAL_NAME, QString("Name"), QtProperty::STRING, true);
        QStringList stringListSceneBlend;
        stringListSceneBlend << QString("Transparent alpha") <<
                      QString("Transparent color") <<
                      QString("Add") <<
                      QString("Modulate")  <<
                      QString("Replace");
        QtSelectProperty* selectProperty = static_cast<QtSelectProperty*>
                                            (mAssetWidget->createProperty(CONTAINER_GENERAL,
                                            MATERIAL_SCENE_BLENDING,
                                            QString("Scene Blending"),
                                            QtProperty::SELECT));
        selectProperty->addValues(stringListSceneBlend);

         // Layout
        mainLayout->addWidget(mAssetWidget);
        setLayout(mainLayout);
    }

    //****************************************************************************/
    QtOgreAssetMaterial::~QtOgreAssetMaterial(void)
    {
    }

    //****************************************************************************/
    void QtOgreAssetMaterial::expandAll(void)
    {
        if (mAssetWidget)
            mAssetWidget->expandAllContainers();
    }

    //****************************************************************************/
    void QtOgreAssetMaterial::collapseAll(void)
    {
        if (mAssetWidget)
            mAssetWidget->collapseAllContainers();
    }

    //****************************************************************************/
    void QtOgreAssetMaterial::bindObject (Ogre::Material* material)
    {
        mOgreMaterial = material; // Allow null value materials
        if (!material)
            return;

        QtStringProperty* stringProperty;
        QtDecimalProperty* decimalProperty;
        QtCheckBoxProperty* checkboxProperty;
        QtColorProperty* colorProperty;
        QtSliderProperty* sliderProperty;
        QtSelectProperty* selectProperty;
        Ogre::ColourValue color;

        // Material name
        stringProperty = static_cast<QtStringProperty*>(mAssetWidget->getPropertyWidget(MATERIAL_NAME));
        stringProperty->setString(material->getName().c_str());

        // Create containers - Technique
        const char* ch;
        unsigned int numTechniques = material->getNumTechniques();
        QtContainerWidget* techniqueContainer;
        QtContainerWidget* passContainer;
        QtContainerWidget* textureUnitContainer;
        Ogre::Technique* technique;
        Ogre::Pass* pass;
        Ogre::TextureUnitState* textureUnit;
        for (unsigned int t = 0; t < numTechniques; ++t)
        {
            int techniqueContainerId = t + TECHNIQUE_BASE;
            technique = material->getTechnique(t);
            techniqueContainer = mAssetWidget->createContainer(techniqueContainerId, QString("Technique ") + QVariant(t).toString());
            techniqueContainer->setTitleBold();
            techniqueContainer->setTitleIcon(mIconDir + QString("technique_bold.png"));

            // Create containers - Pass
            unsigned int numPasses = technique->getNumPasses();
            for (unsigned int p = 0; p < numPasses; ++p)
            {
                int passContainerId = p + PASS_BASE;
                pass = technique->getPass(p);
                passContainer = techniqueContainer->createContainer(passContainerId, QString("Pass ") + QVariant(p).toString());
                passContainer->setTitleBold();
                passContainer->setTitleIcon(mIconDir + QString("pass_bold.png"));

                // Name fragment program
                stringProperty = static_cast<QtStringProperty*>
                        (passContainer->createProperty(PASS_FRAGMENT_NAME, QString("Name fragment program"), QtProperty::STRING, true));
                ch = pass->getFragmentProgramName().c_str();
                stringProperty->setString(QString(ch));
                stringProperty->mUserData = pass;

                // Name vertex program
                stringProperty = static_cast<QtStringProperty*>
                        (passContainer->createProperty(PASS_VERTEX_NAME, QString("Name vertex program"), QtProperty::STRING, true));
                ch = pass->getVertexProgramName().c_str();
                stringProperty->setString(QString(ch));
                stringProperty->mUserData = pass;

                // Name geometry program
                stringProperty = static_cast<QtStringProperty*>
                        (passContainer->createProperty(PASS_GEOMETRY_NAME, QString("Name geometry program"), QtProperty::STRING, true));
                ch = pass->getVertexProgramName().c_str();
                stringProperty->setString(QString(ch));
                stringProperty->mUserData = pass;

                // Depth check enabled
                checkboxProperty = static_cast<QtCheckBoxProperty*>
                        (passContainer->createProperty(PASS_DEPTH_CHECK, QString("Depth check"), QtProperty::CHECKBOX));
                checkboxProperty->setValue(pass->getDepthCheckEnabled());
                checkboxProperty->mUserData = pass;

                // Depth write enabled
                checkboxProperty = static_cast<QtCheckBoxProperty*>
                        (passContainer->createProperty(PASS_DEPTH_WRITE, QString("Depth write"), QtProperty::CHECKBOX));
                checkboxProperty->setValue(pass->getDepthWriteEnabled());
                checkboxProperty->mUserData = pass;

                // Lighting enabled
                checkboxProperty = static_cast<QtCheckBoxProperty*>
                        (passContainer->createProperty(PASS_LIGHTING, QString("Lighting"), QtProperty::CHECKBOX));
                checkboxProperty->setValue(pass->getLightingEnabled());
                checkboxProperty->mUserData = pass;

                // Ambient
                colorProperty = static_cast<QtColorProperty*>
                        (passContainer->createProperty(PASS_AMBIENT, QString("Ambient"), QtProperty::COLOR));
                color = pass->getAmbient();
                colorProperty->setColor(255.0f * color.r, 255.0f * color.g, 255.0f * color.b, 255.0f * color.a);
                colorProperty->setAlphaVisible(false);
                colorProperty->mUserData = pass;

                // Diffuse
                colorProperty = static_cast<QtColorProperty*>
                        (passContainer->createProperty(PASS_DIFFUSE, QString("Diffuse"), QtProperty::COLOR));
                color = pass->getDiffuse();
                colorProperty->setColor(255.0f * color.r, 255.0f * color.g, 255.0f * color.b, 255.0f * color.a);
                colorProperty->mUserData = pass;

                // Specular
                colorProperty = static_cast<QtColorProperty*>
                        (passContainer->createProperty(PASS_SPECULAR, QString("Specular"), QtProperty::COLOR));
                color = pass->getSpecular();
                colorProperty->setColor(255.0f * color.r, 255.0f * color.g, 255.0f * color.b, 255.0f * color.a);
                colorProperty->mUserData = pass;

                // Emissive
                colorProperty = static_cast<QtColorProperty*>
                        (passContainer->createProperty(PASS_EMISSIVE, QString("Emissive"), QtProperty::COLOR));
                color = pass->getEmissive();
                colorProperty->setColor(255.0f * color.r, 255.0f * color.g, 255.0f * color.b, 255.0f * color.a);
                colorProperty->setAlphaVisible(false);
                colorProperty->mUserData = pass;

                // Self illumination
                colorProperty = static_cast<QtColorProperty*>
                        (passContainer->createProperty(PASS_SELF_ILLUMINATION, QString("Self illumination"), QtProperty::COLOR));
                color = pass->getSelfIllumination();
                colorProperty->setColor(255.0f * color.r, 255.0f * color.g, 255.0f * color.b, 255.0f * color.a);
                colorProperty->setAlphaVisible(false);
                colorProperty->mUserData = pass;

                // Shininess
                decimalProperty = static_cast<QtDecimalProperty*>
                        (passContainer->createProperty(PASS_SHININESS, QString("Shininess"), QtProperty::DECIMAL));
                decimalProperty->setValue(pass->getShininess());
                decimalProperty->mUserData = pass;

                // Create containers - Texture Unit State
                unsigned int numTus = pass->getNumTextureUnitStates();
                for (unsigned int tus = 0; tus < numTus; ++tus)
                {
                    int tusContainerId = tus + TUS_BASE;
                    textureUnit = pass->getTextureUnitState(tus);
                    textureUnitContainer = passContainer->createContainer(tusContainerId, QString("Texture ") + QVariant(p).toString());
                    textureUnitContainer->setTitleBold();
                    textureUnitContainer->setTitleIcon(mIconDir + QString("texture_bold.png"));

                    // Texture name
                    stringProperty = static_cast<QtStringProperty*>
                            (textureUnitContainer->createProperty(TUS_TEXTURE_NAME, QString("Texture name"), QtProperty::STRING, true));
                    if (textureUnit->getNumFrames() > 0)
                    {
                        ch = textureUnit->getFrameTextureName(0).c_str(); // Get the first one; this is usually ok
                        stringProperty->setString(QString(ch));
                        stringProperty->mUserData = textureUnit;
                    }

                    // Texture filtering
                    QStringList stringListFiltering;
                    stringListFiltering << QString("None") <<
                                           QString("Point") <<
                                           QString("Linear") <<
                                           QString("Anisotropic");
                    selectProperty = static_cast<QtSelectProperty*>
                            (textureUnitContainer->createProperty(TUS_FILTERING, QString("Texture filtering (magnify)"), QtProperty::SELECT));
                    selectProperty->addValues(stringListFiltering);
                    selectProperty->mUserData = textureUnit;
                    Ogre::FilterOptions option = textureUnit->getTextureFiltering(Ogre::FT_MAG);
                    switch (option)
                    {
                        case Ogre::FO_NONE:
                            selectProperty->setCurentIndex(0);
                        break;
                        case Ogre::FO_POINT:
                            selectProperty->setCurentIndex(1);
                        break;
                        case Ogre::FO_LINEAR:
                            selectProperty->setCurentIndex(2);
                        break;
                        case Ogre::FO_ANISOTROPIC:
                            selectProperty->setCurentIndex(3);
                        break;
                    }

                    // Number of mipmaps
                    sliderProperty = static_cast<QtSliderProperty*>
                            (textureUnitContainer->createProperty(TUS_MIPMAPS, QString("Number of mipmaps"), QtProperty::SLIDER, true));
                    sliderProperty->setSliderRange(-1, 20, 1);
                    sliderProperty->setValue(textureUnit->getNumMipmaps());
                    sliderProperty->mUserData = textureUnit;

                    // U-sale
                    decimalProperty = static_cast<QtDecimalProperty*>
                        (textureUnitContainer->createProperty(TUS_USCALE, QString("U-scale"), QtProperty::DECIMAL));
                    decimalProperty->setValue(textureUnit->getTextureUScale());
                    decimalProperty->mUserData = textureUnit;

                    // V-sale
                    decimalProperty = static_cast<QtDecimalProperty*>
                            (textureUnitContainer->createProperty(TUS_VSCALE, QString("V-scale"), QtProperty::DECIMAL));
                    decimalProperty->setValue(textureUnit->getTextureVScale());
                    decimalProperty->mUserData = textureUnit;

                    // U-scroll
                    decimalProperty = static_cast<QtDecimalProperty*>
                            (textureUnitContainer->createProperty(TUS_USCROLL, QString("U-scroll"), QtProperty::DECIMAL));
                    decimalProperty->setValue(textureUnit->getTextureUScroll());
                    decimalProperty->mUserData = textureUnit;

                    // V-scroll
                    decimalProperty = static_cast<QtDecimalProperty*>
                            (textureUnitContainer->createProperty(TUS_VSCROLL, QString("V-scroll"), QtProperty::DECIMAL));
                    decimalProperty->setValue(textureUnit->getTextureVScroll());
                    decimalProperty->mUserData = textureUnit;
                }
            }
        }
    }

    //****************************************************************************/
    void QtOgreAssetMaterial::propertyValueChanged(QtProperty* property)
    {
        if (!mOgreMaterial || !property)
            return;

        //QtStringProperty* stringProperty;
        QtDecimalProperty* decimalProperty;
        QtCheckBoxProperty* checkboxProperty;
        QtColorProperty* colorProperty;
        //QtSliderProperty* sliderProperty;
        QtSelectProperty* selectProperty;
        Ogre::Pass* pass;
        Ogre::TextureUnitState* textureUnit;
        switch (property->mPropertyId)
        {
            // Material properties
            case MATERIAL_SCENE_BLENDING:
            {
                selectProperty = static_cast<QtSelectProperty*>(property);
                Ogre::SceneBlendType type = Ogre::SBT_ADD;
                if (selectProperty->getCurrentIndex() == 0)
                    type = Ogre::SBT_TRANSPARENT_ALPHA;
                else if (selectProperty->getCurrentIndex() == 1)
                    type = Ogre::SBT_TRANSPARENT_COLOUR;
                else if (selectProperty->getCurrentIndex() == 2)
                    type = Ogre::SBT_ADD;
                else if (selectProperty->getCurrentIndex() == 3)
                    type = Ogre::SBT_MODULATE;
                else if (selectProperty->getCurrentIndex() == 4)
                    type = Ogre::SBT_REPLACE;
                mOgreMaterial->setSceneBlending(type);
            }
            break;

            // Pass properties
            case PASS_DEPTH_CHECK:
            {
                checkboxProperty = static_cast<QtCheckBoxProperty*>(property);
                pass = static_cast<Ogre::Pass*>(checkboxProperty->mUserData);
                if (pass)
                    pass->setDepthCheckEnabled(checkboxProperty->getValue());
            }
            break;
            case PASS_DEPTH_WRITE:
            {
                checkboxProperty = static_cast<QtCheckBoxProperty*>(property);
                pass = static_cast<Ogre::Pass*>(checkboxProperty->mUserData);
                if (pass)
                    pass->setDepthWriteEnabled(checkboxProperty->getValue());
            }
            break;
            case PASS_LIGHTING:
            {
                checkboxProperty = static_cast<QtCheckBoxProperty*>(property);
                pass = static_cast<Ogre::Pass*>(checkboxProperty->mUserData);
                if (pass)
                    pass->setLightingEnabled(checkboxProperty->getValue());
            }
            break;
            case PASS_AMBIENT:
            {
                colorProperty = static_cast<QtColorProperty*>(property);
                pass = static_cast<Ogre::Pass*>(colorProperty->mUserData);
                if (pass)
                    pass->setAmbient(colorProperty->getRed()/255.0f,
                                     colorProperty->getGreen()/255.0f,
                                     colorProperty->getBlue()/255.0f);
            }
            break;
            case PASS_DIFFUSE:
            {
                colorProperty = static_cast<QtColorProperty*>(property);
                pass = static_cast<Ogre::Pass*>(colorProperty->mUserData);
                if (pass)
                    pass->setDiffuse(colorProperty->getRed()/255.0f,
                                     colorProperty->getGreen()/255.0f,
                                     colorProperty->getBlue()/255.0f,
                                     colorProperty->getAlpha()/255.0f);
            }
            break;
            case PASS_SPECULAR:
            {
                colorProperty = static_cast<QtColorProperty*>(property);
                pass = static_cast<Ogre::Pass*>(colorProperty->mUserData);
                if (pass)
                    pass->setSpecular(colorProperty->getRed()/255.0f,
                                      colorProperty->getGreen()/255.0f,
                                      colorProperty->getBlue()/255.0f,
                                      colorProperty->getAlpha()/255.0f);
            }
            break;
            case PASS_EMISSIVE:
            {
                colorProperty = static_cast<QtColorProperty*>(property);
                pass = static_cast<Ogre::Pass*>(colorProperty->mUserData);
                if (pass)
                    pass->setEmissive(colorProperty->getRed()/255.0f,
                                      colorProperty->getGreen()/255.0f,
                                      colorProperty->getBlue()/255.0f);
            }
            break;
            case PASS_SELF_ILLUMINATION:
            {
                colorProperty = static_cast<QtColorProperty*>(property);
                pass = static_cast<Ogre::Pass*>(colorProperty->mUserData);
                if (pass)
                    pass->setSelfIllumination(colorProperty->getRed()/255.0f,
                                              colorProperty->getGreen()/255.0f,
                                              colorProperty->getBlue()/255.0f);
            }
            break;
            case PASS_SHININESS:
            {
                decimalProperty = static_cast<QtDecimalProperty*>(property);
                pass = static_cast<Ogre::Pass*>(decimalProperty->mUserData);
                if (pass)
                    pass->setShininess(decimalProperty->getValue());
            }
            break;

            // Texture unit properties
            case TUS_FILTERING:
            {
                selectProperty = static_cast<QtSelectProperty*>(property);
                textureUnit = static_cast<Ogre::TextureUnitState*>(selectProperty->mUserData);
                if (textureUnit)
                {
                    if (selectProperty->getCurrentIndex() == 0)
                    {
                        textureUnit->setTextureFiltering(Ogre::FT_MIN, Ogre::FO_NONE);
                        textureUnit->setTextureFiltering(Ogre::FT_MAG, Ogre::FO_NONE);
                    }
                    else if (selectProperty->getCurrentIndex() == 1)
                    {
                        textureUnit->setTextureFiltering(Ogre::FT_MIN, Ogre::FO_POINT);
                        textureUnit->setTextureFiltering(Ogre::FT_MAG, Ogre::FO_POINT);
                    }
                    else if (selectProperty->getCurrentIndex() == 2)
                    {
                        textureUnit->setTextureFiltering(Ogre::FT_MIN, Ogre::FO_LINEAR);
                        textureUnit->setTextureFiltering(Ogre::FT_MAG, Ogre::FO_LINEAR);
                    }
                    else if (selectProperty->getCurrentIndex() == 3)
                    {
                        textureUnit->setTextureFiltering(Ogre::FT_MIN, Ogre::FO_ANISOTROPIC);
                        textureUnit->setTextureFiltering(Ogre::FT_MAG, Ogre::FO_ANISOTROPIC);
                    }
                }
            }
            break;

            case TUS_USCALE:
            {
                decimalProperty = static_cast<QtDecimalProperty*>(property);
                textureUnit = static_cast<Ogre::TextureUnitState*>(decimalProperty->mUserData);
                if (textureUnit)
                    textureUnit->setTextureUScale(decimalProperty->getValue());
            }
            break;

            case TUS_VSCALE:
            {
                decimalProperty = static_cast<QtDecimalProperty*>(property);
                textureUnit = static_cast<Ogre::TextureUnitState*>(decimalProperty->mUserData);
                if (textureUnit)
                    textureUnit->setTextureVScale(decimalProperty->getValue());
            }
            break;

            case TUS_USCROLL:
            {
                decimalProperty = static_cast<QtDecimalProperty*>(property);
                textureUnit = static_cast<Ogre::TextureUnitState*>(decimalProperty->mUserData);
                if (textureUnit)
                    textureUnit->setTextureUScroll(decimalProperty->getValue());
            }
            break;

            case TUS_VSCROLL:
            {
                decimalProperty = static_cast<QtDecimalProperty*>(property);
                textureUnit = static_cast<Ogre::TextureUnitState*>(decimalProperty->mUserData);
                if (textureUnit)
                    textureUnit->setTextureVScroll(decimalProperty->getValue());
            }
            break;
        }
    }