1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*!
@file
@author Albert Semenov
@date 08/2010
*/
#include "Precompiled.h"
#include "PropertyTexturesControl.h"
#include "Localise.h"
namespace tools
{
PropertyTexturesControl::PropertyTexturesControl() :
mImage(nullptr),
mName(nullptr),
mComboBox(nullptr),
mBrowse(nullptr),
mTextureBrowseControl(nullptr)
{
}
PropertyTexturesControl::~PropertyTexturesControl()
{
mBrowse->eventMouseButtonClick -= MyGUI::newDelegate(this, &PropertyTexturesControl::notifyMouseButtonClick);
mComboBox->eventComboChangePosition -= MyGUI::newDelegate(this, &PropertyTexturesControl::notifyComboChangePosition);
delete mTextureBrowseControl;
mTextureBrowseControl = nullptr;
}
void PropertyTexturesControl::OnInitialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName)
{
PropertyControl::OnInitialise(_parent, _place, "PropertyTextureBrowseControl.layout");
assignWidget(mName, "Name", false);
assignWidget(mComboBox, "ComboBox");
assignWidget(mBrowse, "Browse");
assignWidget(mImage, "Image");
fillTextures();
for (MyGUI::VectorString::const_iterator item = mTextures.begin(); item != mTextures.end(); ++item)
mComboBox->addItem((*item));
mComboBox->beginToItemFirst();
mTextureBrowseControl = new TextureBrowseControl();
mTextureBrowseControl->Initialise();
mTextureBrowseControl->eventEndDialog.connect(this, &PropertyTexturesControl::notifyEndDialog);
mTextureBrowseControl->setTextures(mTextures);
mComboBox->eventComboChangePosition += MyGUI::newDelegate(this, &PropertyTexturesControl::notifyComboChangePosition);
mBrowse->eventMouseButtonClick += MyGUI::newDelegate(this, &PropertyTexturesControl::notifyMouseButtonClick);
mMainWidget->eventChangeCoord += MyGUI::newDelegate(this, &PropertyTexturesControl::notifyChangeCoord);
}
void PropertyTexturesControl::updateCaption()
{
PropertyPtr proper = getProperty();
if (proper != nullptr)
mName->setCaption(proper->getType()->getName());
}
void PropertyTexturesControl::updateProperty()
{
PropertyPtr proper = getProperty();
if (proper != nullptr)
{
mComboBox->setEnabled(!proper->getType()->getReadOnly());
mBrowse->setEnabled(!proper->getType()->getReadOnly());
size_t index = getComboIndex(proper->getValue());
mComboBox->setIndexSelected(index);
if (index == MyGUI::ITEM_NONE)
mComboBox->setCaption(replaceTags("ColourError") + proper->getValue());
mImage->setVisible(true);
mImage->setImageTexture(proper->getValue());
mTextureSize = MyGUI::texture_utility::getTextureSize(proper->getValue());
updateTexture();
}
else
{
mComboBox->setIndexSelected(MyGUI::ITEM_NONE);
mComboBox->setEnabled(false);
mBrowse->setEnabled(false);
mImage->setVisible(false);
}
}
void PropertyTexturesControl::notifyComboChangePosition(MyGUI::ComboBox* _sender, size_t _index)
{
PropertyPtr proper = getProperty();
if (proper != nullptr)
{
std::string value = _index != MyGUI::ITEM_NONE ? mComboBox->getItemNameAt(_index) : "";
executeAction(value);
}
}
size_t PropertyTexturesControl::getComboIndex(const MyGUI::UString& _name)
{
size_t result = MyGUI::ITEM_NONE;
size_t count = mComboBox->getItemCount();
for (size_t index = 0; index < count; ++index)
{
if (mComboBox->getItemNameAt(index) == _name)
{
result = index;
break;
}
}
return result;
}
void PropertyTexturesControl::notifyMouseButtonClick(MyGUI::Widget* _sender)
{
PropertyPtr proper = getProperty();
if (proper != nullptr)
mTextureBrowseControl->setTextureName(proper->getValue());
else
mTextureBrowseControl->setTextureName("");
mTextureBrowseControl->doModal();
}
void PropertyTexturesControl::notifyEndDialog(Dialog* _sender, bool _result)
{
mTextureBrowseControl->endModal();
if (_result)
{
PropertyPtr proper = getProperty();
if (proper != nullptr)
executeAction(mTextureBrowseControl->getTextureName());
updateProperty();
}
}
void PropertyTexturesControl::fillTextures()
{
// FIXME потом вынести в загружаемые настройки
MyGUI::VectorString paths = MyGUI::DataManager::getInstance().getDataListNames("*.png");
for (MyGUI::VectorString::iterator iter = paths.begin(); iter != paths.end(); ++iter)
mTextures.push_back(*iter);
// FIXME потом вынести в загружаемые настройки
paths = MyGUI::DataManager::getInstance().getDataListNames("*.jpg");
for (MyGUI::VectorString::iterator iter = paths.begin(); iter != paths.end(); ++iter)
mTextures.push_back(*iter);
}
void PropertyTexturesControl::updateTexture()
{
if (mTextureSize.width != 0 && mTextureSize.height != 0)
{
mImage->setVisible(true);
const MyGUI::IntSize& targetSize = mImage->getParentSize();
float k1 = (float)targetSize.width / mTextureSize.width;
float k2 = (float)targetSize.height / mTextureSize.height;
float k = (std::min)(k1, k2);
MyGUI::IntSize size = MyGUI::IntSize((int)(k * (float)mTextureSize.width), (int)(k * (float)mTextureSize.height));
MyGUI::IntSize parentSize = mImage->getParentSize();
mImage->setCoord((parentSize.width - size.width) / 2, (parentSize.height - size.height) / 2, size.width, size.height);
}
else
{
mImage->setVisible(false);
}
}
void PropertyTexturesControl::notifyChangeCoord(MyGUI::Widget* _sender)
{
if (mImage->getVisible() && mImage->getInheritedVisible())
updateTexture();
}
}
|