Skip to content

Commit

Permalink
Minor improvements (#78)
Browse files Browse the repository at this point in the history
* increase test coverage
  • Loading branch information
fernandotonon authored Jun 4, 2024
1 parent ef15a99 commit 17eed81
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 72 deletions.
100 changes: 49 additions & 51 deletions src/materialeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ogre::Exception> &getErrors(){return errors;};
const std::vector<Ogre::Exception> &getErrors() const{return errors;};
};

if(!Ogre::ResourceGroupManager::getSingleton().resourceGroupExists("Test_Script"))
Expand Down Expand Up @@ -283,7 +283,6 @@ bool MaterialEditor::validateScript(Ogre::DataStreamPtr& dataStream)
mBox.exec();
return false;
}
return true;
}

void MaterialEditor::on_techComboBox_currentIndexChanged(int index)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}


Expand All @@ -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();
}


Expand All @@ -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<Ogre::PolygonMode>(index+1));
if(!mSelectedPass) return;

mSelectedPass->setPolygonMode(static_cast<Ogre::PolygonMode>(index+1));
updateMaterialText();
}

Expand Down
103 changes: 82 additions & 21 deletions src/materialeditor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include <gmock/gmock.h>
#include "materialeditor.h"
#include "ui_materialeditor.h"
#include "Manager.h"
#include <QApplication>
#include <QInputDialog>

class MaterialEditorTest : public ::testing::Test {
protected:
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -491,29 +511,70 @@ TEST_F(MaterialEditorTest, onShineSpecularValueChanged) {
Ogre::MaterialManager::getSingleton().remove(material);
}

/*
TEST_F(MaterialEditorTest, onScrollAnimSpeedValueChanged) {
auto editor = std::make_unique<MaterialEditor>();

//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<MaterialEditor>();

//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);
}

0 comments on commit 17eed81

Please sign in to comment.