1 /*! 2 @file 3 @author Albert Semenov 4 @date 12/2010 5 */ 6 7 #include "Precompiled.h" 8 #include "PropertyFieldColour.h" 9 #include "Localise.h" 10 #include "UndoManager.h" 11 #include "Parse.h" 12 13 namespace tools 14 { 15 16 const std::string DEFAULT_STRING = "[DEFAULT]"; 17 PropertyFieldColour(MyGUI::Widget * _parent)18 PropertyFieldColour::PropertyFieldColour(MyGUI::Widget* _parent) : 19 BaseLayout("PropertyFieldColour.layout", _parent), 20 mText(nullptr), 21 mField(nullptr), 22 mColourPlace(nullptr), 23 mCurrentWidget(nullptr), 24 mColourPanel(nullptr), 25 mGoodData(false) 26 { 27 assignWidget(mText, "Text"); 28 assignWidget(mField, "Field"); 29 assignWidget(mColourPlace, "ColourPlace"); 30 31 mField->eventEditTextChange += newDelegate (this, &PropertyFieldColour::notifyTryApplyProperties); 32 mField->eventEditSelectAccept += newDelegate (this, &PropertyFieldColour::notifyForceApplyProperties); 33 34 mColourPlace->eventMouseButtonPressed += MyGUI::newDelegate(this, &PropertyFieldColour::notifyMouseButtonPressed); 35 36 mColourPanel = new ColourPanel(); 37 mColourPanel->Initialise(); 38 mColourPanel->eventEndDialog.connect(this, &PropertyFieldColour::notifyEndDialog); 39 mColourPanel->eventPreviewColour.connect(this, &PropertyFieldColour::notifyPreviewColour); 40 } 41 ~PropertyFieldColour()42 PropertyFieldColour::~PropertyFieldColour() 43 { 44 delete mColourPanel; 45 mColourPanel = nullptr; 46 } 47 initialise(const std::string & _type)48 void PropertyFieldColour::initialise(const std::string& _type) 49 { 50 mType = _type; 51 } 52 setTarget(MyGUI::Widget * _currentWidget)53 void PropertyFieldColour::setTarget(MyGUI::Widget* _currentWidget) 54 { 55 mCurrentWidget = _currentWidget; 56 } 57 notifyApplyProperties(MyGUI::Widget * _sender,bool _force)58 void PropertyFieldColour::notifyApplyProperties(MyGUI::Widget* _sender, bool _force) 59 { 60 bool goodData = onCheckValue(); 61 62 if (goodData || _force) 63 { 64 std::string DEFAULT_VALUE = replaceTags("ColourDefault") + DEFAULT_STRING; 65 66 std::string value = mField->getOnlyText(); 67 if (value == DEFAULT_STRING && mField->getCaption() == DEFAULT_VALUE) 68 value = ""; 69 70 onAction(value, true); 71 } 72 } 73 onAction(const std::string & _value,bool _final)74 void PropertyFieldColour::onAction(const std::string& _value, bool _final) 75 { 76 eventAction(mName, _value, _final); 77 } 78 notifyTryApplyProperties(MyGUI::EditBox * _sender)79 void PropertyFieldColour::notifyTryApplyProperties(MyGUI::EditBox* _sender) 80 { 81 notifyApplyProperties(_sender, false); 82 } 83 notifyForceApplyProperties(MyGUI::EditBox * _sender)84 void PropertyFieldColour::notifyForceApplyProperties(MyGUI::EditBox* _sender) 85 { 86 notifyApplyProperties(_sender, true); 87 } 88 onCheckValue()89 bool PropertyFieldColour::onCheckValue() 90 { 91 bool success = utility::checkParse<float>(mField, 3); 92 93 updateColourPlace(success); 94 95 return success; 96 } 97 updateColourPlace(bool _success)98 void PropertyFieldColour::updateColourPlace(bool _success) 99 { 100 if (_success) 101 { 102 mColourPlace->setAlpha(1); 103 MyGUI::Colour colour = MyGUI::Colour::parse(mField->getOnlyText()); 104 mColourPlace->setColour(colour); 105 } 106 else 107 { 108 mColourPlace->setAlpha(0); 109 } 110 } 111 getContentSize()112 MyGUI::IntSize PropertyFieldColour::getContentSize() 113 { 114 return MyGUI::IntSize(0, mMainWidget->getHeight()); 115 } 116 setCoord(const MyGUI::IntCoord & _coord)117 void PropertyFieldColour::setCoord(const MyGUI::IntCoord& _coord) 118 { 119 mMainWidget->setCoord(_coord); 120 } 121 setValue(const std::string & _value)122 void PropertyFieldColour::setValue(const std::string& _value) 123 { 124 std::string DEFAULT_VALUE = replaceTags("ColourDefault") + DEFAULT_STRING; 125 126 if (_value.empty()) 127 { 128 mField->setCaption(DEFAULT_VALUE); 129 updateColourPlace(false); 130 } 131 else 132 { 133 mField->setOnlyText(_value); 134 onCheckValue(); 135 } 136 } 137 setName(const std::string & _value)138 void PropertyFieldColour::setName(const std::string& _value) 139 { 140 mName = _value; 141 mText->setCaption(_value); 142 } 143 notifyMouseButtonPressed(MyGUI::Widget * _sender,int _left,int _top,MyGUI::MouseButton _id)144 void PropertyFieldColour::notifyMouseButtonPressed(MyGUI::Widget* _sender, int _left, int _top, MyGUI::MouseButton _id) 145 { 146 showColourDialog(); 147 } 148 notifyPreviewColour(const MyGUI::Colour & _value)149 void PropertyFieldColour::notifyPreviewColour(const MyGUI::Colour& _value) 150 { 151 setColour(_value, false); 152 } 153 notifyEndDialog(Dialog * _sender,bool _result)154 void PropertyFieldColour::notifyEndDialog(Dialog* _sender, bool _result) 155 { 156 mColourPanel->endModal(); 157 158 if (_result) 159 { 160 setColour(mColourPanel->getColour(), true); 161 } 162 else 163 { 164 if (mGoodData) 165 { 166 setColour(mPreviewColour, true); 167 } 168 else 169 { 170 std::string DEFAULT_VALUE = replaceTags("ColourDefault") + DEFAULT_STRING; 171 mField->setCaption(DEFAULT_VALUE); 172 updateColourPlace(false); 173 onAction("", true); 174 } 175 } 176 } 177 showColourDialog()178 void PropertyFieldColour::showColourDialog() 179 { 180 mGoodData = onCheckValue(); 181 if (mGoodData) 182 mPreviewColour = getColour(); 183 else 184 mPreviewColour = MyGUI::Colour::White; 185 186 mColourPanel->setColour(mPreviewColour); 187 mColourPanel->setAlphaSupport(false); 188 mColourPanel->doModal(); 189 } 190 setColour(const MyGUI::Colour & _color,bool _final)191 void PropertyFieldColour::setColour(const MyGUI::Colour& _color, bool _final) 192 { 193 std::string value = MyGUI::utility::toString(_color.red, " ", _color.green, " ", _color.blue); 194 mField->setOnlyText(value); 195 onCheckValue(); 196 onAction(value, _final); 197 } 198 getColour()199 MyGUI::Colour PropertyFieldColour::getColour() 200 { 201 return MyGUI::Colour::parse(mField->getOnlyText()); 202 } 203 setVisible(bool _value)204 void PropertyFieldColour::setVisible(bool _value) 205 { 206 mMainWidget->setVisible(_value); 207 } 208 getVisible()209 bool PropertyFieldColour::getVisible() 210 { 211 return mMainWidget->getVisible(); 212 } 213 214 } 215