diff --git a/src/materialeditor.cpp b/src/materialeditor.cpp index 2d3150d..a89434b 100755 --- a/src/materialeditor.cpp +++ b/src/materialeditor.cpp @@ -248,7 +248,7 @@ bool MaterialEditor::validateScript(Ogre::DataStreamPtr& dataStream) Ogre::Exception e{0,msg,"ScriptCompilerListener","error",file.c_str(),line}; errors.push_back(e); } - const std::vector &getErrors(){return errors;}; + const std::vector &getErrors() const{return errors;}; }; if(!Ogre::ResourceGroupManager::getSingleton().resourceGroupExists("Test_Script")) @@ -283,7 +283,6 @@ bool MaterialEditor::validateScript(Ogre::DataStreamPtr& dataStream) mBox.exec(); return false; } - return true; } void MaterialEditor::on_techComboBox_currentIndexChanged(int index) @@ -351,7 +350,7 @@ void MaterialEditor::on_ComboTextureUnit_currentIndexChanged(int index) ui->removeTexture->setEnabled(true); const auto effects = mSelectedTextureUnit->getEffects(); - for(auto effectPair : effects){ + for(const auto effectPair : effects){ if(effectPair.first==Ogre::TextureUnitState::ET_UVSCROLL || effectPair.first==Ogre::TextureUnitState::ET_USCROLL) ui->scrollAnimUSpeed->setValue(effectPair.second.arg1); @@ -478,13 +477,13 @@ void MaterialEditor::on_srcSceneBlendBox_currentIndexChanged(int index) void MaterialEditor::on_dstSceneBlendBox_currentIndexChanged(int index) { - if(mSelectedPass) + if(!mSelectedPass) return; + + if(index>0) { - if(index>0) - { - mSelectedPass->setSceneBlending(mSelectedPass->getSourceBlendFactor(),(Ogre::SceneBlendFactor)--index); - } + mSelectedPass->setSceneBlending(mSelectedPass->getSourceBlendFactor(),(Ogre::SceneBlendFactor)--index); } + updateMaterialText(); } @@ -584,17 +583,16 @@ void MaterialEditor::on_passNewButton_clicked() QString text = QInputDialog::getText(this, tr("New Pass"), tr("Pass name:"), QLineEdit::Normal, "", &ok); - if (ok && mSelectedTechnique) - { - Ogre::Pass *p = mSelectedTechnique->createPass(); - p->setName(text.toStdString().data()); + if (!(ok && mSelectedTechnique)) return; - mPassMap.insert(mPassMap.size(),p); + Ogre::Pass *p = mSelectedTechnique->createPass(); + p->setName(text.toStdString().data()); - ui->passComboBox->addItem(text.toStdString().data()); + mPassMap.insert(mPassMap.size(),p); - updateMaterialText(); - } + ui->passComboBox->addItem(text.toStdString().data()); + + updateMaterialText(); } @@ -604,15 +602,14 @@ void MaterialEditor::on_TextureUnitNewButton_clicked() QString text = QInputDialog::getText(this, tr("New Texture Unit"), tr("Texture unit name:"), QLineEdit::Normal, "", &ok); - if (ok && mSelectedPass) - { - Ogre::TextureUnitState *t = mSelectedPass->createTextureUnitState(); - t->setName(text.toStdString().data()); + if (!(ok && mSelectedPass)) return; + + Ogre::TextureUnitState *t = mSelectedPass->createTextureUnitState(); + t->setName(text.toStdString().data()); - ui->ComboTextureUnit->addItem(text.toStdString().data()); + ui->ComboTextureUnit->addItem(text.toStdString().data()); - updateMaterialText(); - } + updateMaterialText(); } @@ -623,61 +620,62 @@ void MaterialEditor::on_selectTexture_clicked() tr("Image File (*.bmp *.jpg *.gif *.raw *.png *.tga *.dds)"), nullptr, QFileDialog::DontUseNativeDialog); - if(filePath.size()&&mSelectedTextureUnit) - { + if(!(filePath.size()&&mSelectedTextureUnit)) return; - QFileInfo file; - file.setFile(filePath); + QFileInfo file; + file.setFile(filePath); - try { - Ogre::TextureManager::getSingleton().getByName(file.fileName().toStdString().data(),file.path().toStdString().data()); - } catch (...) { - Ogre::ResourceGroupManager::getSingleton().addResourceLocation(file.path().toStdString().data(),"FileSystem",file.path().toStdString().data()); - Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); + try { + Ogre::TextureManager::getSingleton().getByName(file.fileName().toStdString().data(),file.path().toStdString().data()); + } catch (...) { + Ogre::ResourceGroupManager::getSingleton().addResourceLocation(file.path().toStdString().data(),"FileSystem",file.path().toStdString().data()); + Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); - Ogre::Image i; - i.load(file.fileName().toStdString().data(),file.path().toStdString().data()); - Ogre::TextureManager::getSingleton().loadImage(file.fileName().toStdString().data(),file.path().toStdString().data(),i); - } - ui->textureName->setText(file.fileName()); - mSelectedTextureUnit->setTextureName(file.fileName().toStdString().data()); + Ogre::Image i; + i.load(file.fileName().toStdString().data(),file.path().toStdString().data()); + Ogre::TextureManager::getSingleton().loadImage(file.fileName().toStdString().data(),file.path().toStdString().data(),i); } + ui->textureName->setText(file.fileName()); + mSelectedTextureUnit->setTextureName(file.fileName().toStdString().data()); } void MaterialEditor::on_removeTexture_clicked() { - if(mSelectedTextureUnit) - { - mSelectedTextureUnit->setTextureName(""); - ui->textureName->setText("*Select a texture*"); - } + if(!mSelectedTextureUnit) return; + + mSelectedTextureUnit->setTextureName(""); + ui->textureName->setText("*Select a texture*"); } void MaterialEditor::on_checkBoxLightning_toggled(bool checked) { - if(mSelectedPass) - mSelectedPass->setLightingEnabled(checked); + if(!mSelectedPass) return; + + mSelectedPass->setLightingEnabled(checked); updateMaterialText(); } void MaterialEditor::on_checkBoxDepthWrite_toggled(bool checked) { - if(mSelectedPass) - mSelectedPass->setDepthWriteEnabled(checked); + if(!mSelectedPass) return; + + mSelectedPass->setDepthWriteEnabled(checked); updateMaterialText(); } void MaterialEditor::on_checkBoxDepthCheck_toggled(bool checked) { - if(mSelectedPass) - mSelectedPass->setDepthCheckEnabled(checked); + if(!mSelectedPass) return; + + mSelectedPass->setDepthCheckEnabled(checked); updateMaterialText(); } void MaterialEditor::on_comboPolygonMode_currentIndexChanged(int index) { - if(mSelectedPass) - mSelectedPass->setPolygonMode(static_cast(index+1)); + if(!mSelectedPass) return; + + mSelectedPass->setPolygonMode(static_cast(index+1)); updateMaterialText(); } diff --git a/src/materialeditor_test.cpp b/src/materialeditor_test.cpp index 8bce29f..5b3111d 100644 --- a/src/materialeditor_test.cpp +++ b/src/materialeditor_test.cpp @@ -2,7 +2,9 @@ #include #include "materialeditor.h" #include "ui_materialeditor.h" +#include "Manager.h" #include +#include class MaterialEditorTest : public ::testing::Test { protected: @@ -53,7 +55,21 @@ TEST_F(MaterialEditorTest, SetTechFieldsTestWithEmptyList) { editor->setTechFields(techMap, passList); - // Verify that the passComboBox is in the default state + // Call the methods without selecting a tech, pass or texture unit + editor->getUI()->removeTexture->click(); + editor->on_checkBoxLightning_toggled(false); + editor->on_checkBoxDepthWrite_toggled(false); + editor->on_checkBoxDepthCheck_toggled(false); + editor->on_checkBoxUseVertexColorToAmbient_toggled(true); + editor->on_checkBoxUseVertexColorToDifuse_toggled(true); + editor->on_checkBoxUseVertexColorToSpecular_toggled(true); + editor->on_checkBoxUseVertexColorToEmissive_toggled(true); + editor->on_alphaDifuse_valueChanged(0.5); + editor->on_alphaSpecular_valueChanged(0.5); + editor->on_shineSpecular_valueChanged(0.5); + editor->on_comboPolygonMode_currentIndexChanged(1); + + // Verify that the fields are in the default state ASSERT_EQ(editor->getUI()->passComboBox->count(), 1); ASSERT_EQ(editor->getUI()->passComboBox->itemText(0), ""); ASSERT_TRUE(editor->getUI()->passComboBox->isEnabled()); @@ -446,15 +462,17 @@ TEST_F(MaterialEditorTest, onAlphaSpecularValueChanged) { //Create test material Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); - ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getSpecular().a, 1.0f); + + // Don't change the value if the pass is not selected + editor->on_alphaSpecular_valueChanged(0.5); + ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getSpecular().a, 1.0); // Set material editor->setMaterial("TestMaterial"); + ASSERT_EQ(editor->getMaterialName(), "TestMaterial"); // Set alpha specular editor->on_alphaSpecular_valueChanged(0.5); - - ASSERT_EQ(editor->getMaterialName(), "TestMaterial"); ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getSpecular().a, 0.5); // Set alpha specular to 0 @@ -471,15 +489,17 @@ TEST_F(MaterialEditorTest, onShineSpecularValueChanged) { //Create test material Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); + + // Don't change the value if the pass is not selected + editor->on_shineSpecular_valueChanged(0.5); ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getShininess(), 0.0); // Set material editor->setMaterial("TestMaterial"); + ASSERT_EQ(editor->getMaterialName(), "TestMaterial"); // Set shine specular editor->on_shineSpecular_valueChanged(0.5); - - ASSERT_EQ(editor->getMaterialName(), "TestMaterial"); ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getShininess(), 0.5); // Set shine specular back @@ -491,29 +511,70 @@ TEST_F(MaterialEditorTest, onShineSpecularValueChanged) { Ogre::MaterialManager::getSingleton().remove(material); } -/* TEST_F(MaterialEditorTest, onScrollAnimSpeedValueChanged) { auto editor = std::make_unique(); //Create test material - Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); - ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getTextureUnitState(0)->getTextureUScroll(), 0.0f); - ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getTextureUnitState(0)->getTextureVScroll(), 0.0f); + Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("TestScrollAnimSpeedMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); // Set material - editor->setMaterial("TestMaterial"); + editor->setMaterial("TestScrollAnimSpeedMaterial"); + editor->setMaterialText("\nmaterial TestScrollAnimSpeedMaterial\n{\n\ttechnique\n\t{\n\t\tpass test_pass \n\t\t{\n\t\tlighting off\n\t\ttexture_unit testTU \n\t\t{\n\t\t}\n\t\t}\n\n\t}\n\n}\n"); + ASSERT_EQ(editor->getMaterialName(), "TestScrollAnimSpeedMaterial"); + + // Apply + editor->getUI()->applyButton->setEnabled(true); + editor->getUI()->applyButton->click(); + + // Select the first pass + editor->getUI()->passComboBox->setCurrentIndex(1); + + // Create the texture unity + editor->getUI()->ComboTextureUnit->setCurrentIndex(1); + ASSERT_EQ(editor->getUI()->ComboTextureUnit->currentText(), "testTU"); + + // Assert initial state + ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestScrollAnimSpeedMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getTextureUnitState(0)->getTextureUScroll(), 0.0f); + ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestScrollAnimSpeedMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getTextureUnitState(0)->getTextureVScroll(), 0.0f); // Set animation u speed - editor->getUI()->scrollAnimUSpeed->setValue(1.0); - editor->getUI()->scrollAnimVSpeed->setValue(1.0); + editor->getUI()->scrollAnimUSpeed->setValue(1.0f); + editor->getUI()->scrollAnimVSpeed->setValue(1.0f); + ASSERT_GT(editor->getMaterialText().find("1.0 1.0"),0); - ASSERT_EQ(editor->getMaterialName(), "TestMaterial"); - ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getTextureUnitState(0)->getTextureUScroll(), 1.0f); - ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getTextureUnitState(0)->getTextureVScroll(), 1.0f); // Set animation u speed back - editor->getUI()->scrollAnimUSpeed->setValue(0.0); - editor->getUI()->scrollAnimVSpeed->setValue(0.0); - ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getTextureUnitState(0)->getTextureUScroll(), 0.0f); - ASSERT_EQ(Ogre::MaterialManager::getSingleton().getByName("TestMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME)->getTechniques()[0]->getPasses()[0]->getTextureUnitState(0)->getTextureVScroll(), 0.0f); + editor->getUI()->scrollAnimUSpeed->setValue(0.0f); + editor->getUI()->scrollAnimVSpeed->setValue(0.0f); + ASSERT_EQ(editor->getMaterialText().find("1.0 1.0"),-1); Ogre::MaterialManager::getSingleton().remove(material); -} Enable after adding textures to the test*/ +} + +TEST_F(MaterialEditorTest, onSrcBlendBoxCurrentIndexChanged) { + auto editor = std::make_unique(); + + //Create test material + Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("TestSrcBlendBoxMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); + + // Set material + editor->setMaterial("TestSrcBlendBoxMaterial"); + editor->setMaterialText("\nmaterial TestSrcBlendBoxMaterial\n{\n\ttechnique\n\t{\n\t\tpass test_pass \n\t\t{\n\t\tlighting off\n\t\ttexture_unit testTU \n\t\t{\n\t\t}\n\t\t}\n\n\t}\n\n}\n"); + + ASSERT_EQ(editor->getUI()->srcSceneBlendBox->currentIndex(), 6); + + // Change the src blend box + editor->getUI()->srcSceneBlendBox->setCurrentIndex(1); + + ASSERT_EQ(editor->getUI()->srcSceneBlendBox->currentIndex(), 1); + ASSERT_GT(editor->getMaterialText().find("alpha_blend"), 0); + + // Don't change when selecting null + editor->getUI()->srcSceneBlendBox->setCurrentIndex(0); + ASSERT_GT(editor->getMaterialText().find("alpha_blend"), 0); + + // Change the src blend box back + editor->getUI()->srcSceneBlendBox->setCurrentIndex(6); + + ASSERT_EQ(editor->getUI()->srcSceneBlendBox->currentIndex(), 6); + ASSERT_EQ(editor->getMaterialText().find("alpha_blend"), -1); + ASSERT_GT(editor->getMaterialText().find("one"), 0); +}