~ubuntu-branches/ubuntu/vivid/mygui/vivid

« back to all changes in this revision

Viewing changes to Tools/ImageEditor/AnimationViewControl.cpp

  • Committer: Package Import Robot
  • Author(s): Scott Howard, Bret Curtis, Scott Howard
  • Date: 2014-09-18 17:57:48 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140918175748-dd8va78mvpw1jbes
Tags: 3.2.1-1
[ Bret Curtis ]
* Updated license for majority of files from LGPL to Expat (MIT)

[ Scott Howard ]
* New upstream release
* Updated patch to add build option for system GLEW libraries
* All patches accepted upstream except shared_libraries.patch
* Bumped SONAME due to dropped symbols, updated *.symbols and package
  names
* Updated license of debian/* to Expat with permission of all authors
* Don't install Doxygen autogenerated md5 and map files (thanks
  lintian)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
        @file
 
3
        @author         Albert Semenov
 
4
        @date           07/2012
 
5
*/
 
6
 
 
7
#include "Precompiled.h"
 
8
#include "AnimationViewControl.h"
 
9
#include "FactoryManager.h"
 
10
#include "DataSelectorManager.h"
 
11
#include "DataManager.h"
 
12
#include "DataUtility.h"
 
13
 
 
14
namespace tools
 
15
{
 
16
 
 
17
        FACTORY_ITEM_ATTRIBUTE(AnimationViewControl)
 
18
 
 
19
        AnimationViewControl::AnimationViewControl() :
 
20
                mImage(nullptr),
 
21
                mFrameInfo(nullptr),
 
22
                mButtonPlay(nullptr),
 
23
                mButtonLeft(nullptr),
 
24
                mButtonRight(nullptr),
 
25
                mParentData(nullptr),
 
26
                mCurrentFrame(0),
 
27
                mTime(0),
 
28
                mPlay(true)
 
29
        {
 
30
        }
 
31
 
 
32
        AnimationViewControl::~AnimationViewControl()
 
33
        {
 
34
        }
 
35
 
 
36
        void AnimationViewControl::OnInitialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName)
 
37
        {
 
38
                Control::OnInitialise(_parent, _place, _layoutName);
 
39
 
 
40
                InitialiseByAttributes(this);
 
41
 
 
42
                std::string parentType = "Index";
 
43
                DataSelectorManager::getInstance().getEvent(parentType)->connect(this, &AnimationViewControl::notifyChangeDataSelector);
 
44
                mParentData = DataUtility::getSelectedDataByType(parentType);
 
45
                notifyChangeDataSelector(mParentData, false);
 
46
 
 
47
                mImage->getParent()->eventChangeCoord += MyGUI::newDelegate(this, &AnimationViewControl::notifyChangeCoord);
 
48
                updateImageCoord();
 
49
 
 
50
                MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate(this, &AnimationViewControl::notifyFrameStart);
 
51
 
 
52
                mButtonPlay->eventMouseButtonClick += MyGUI::newDelegate(this, &AnimationViewControl::notifyMouseButtonClick);
 
53
                mButtonLeft->eventMouseButtonClick += MyGUI::newDelegate(this, &AnimationViewControl::notifyMouseButtonClick);
 
54
                mButtonRight->eventMouseButtonClick += MyGUI::newDelegate(this, &AnimationViewControl::notifyMouseButtonClick);
 
55
        }
 
56
 
 
57
        void AnimationViewControl::notifyChangeDataSelector(DataPtr _parent, bool _changeSelectOnly)
 
58
        {
 
59
                mParentData = _parent;
 
60
 
 
61
                if (!_changeSelectOnly)
 
62
                {
 
63
                        connectToProperties();
 
64
                        rebuildAnimations();
 
65
                }
 
66
                else
 
67
                {
 
68
                        updateSelectedFrame();
 
69
                }
 
70
        }
 
71
 
 
72
        void AnimationViewControl::connectToProperties()
 
73
        {
 
74
                if (mParentData != nullptr)
 
75
                {
 
76
                        // Index
 
77
                        PropertyPtr property = mParentData->getProperty("Rate");
 
78
                        if (!property->eventChangeProperty.exist(this, &AnimationViewControl::notifyChangeProperty))
 
79
                                property->eventChangeProperty.connect(this, &AnimationViewControl::notifyChangeProperty);
 
80
 
 
81
                        // Group
 
82
                        property = mParentData->getParent()->getProperty("Size");
 
83
                        if (!property->eventChangeProperty.exist(this, &AnimationViewControl::notifyChangeProperty))
 
84
                                property->eventChangeProperty.connect(this, &AnimationViewControl::notifyChangeProperty);
 
85
 
 
86
                        // Group
 
87
                        property = mParentData->getParent()->getProperty("Texture");
 
88
                        if (!property->eventChangeProperty.exist(this, &AnimationViewControl::notifyChangeProperty))
 
89
                                property->eventChangeProperty.connect(this, &AnimationViewControl::notifyChangeProperty);
 
90
 
 
91
                        for (Data::VectorData::const_iterator child = mParentData->getChilds().begin(); child != mParentData->getChilds().end(); child ++)
 
92
                        {
 
93
                                property = (*child)->getProperty("Point");
 
94
                                if (!property->eventChangeProperty.exist(this, &AnimationViewControl::notifyChangeProperty))
 
95
                                        property->eventChangeProperty.connect(this, &AnimationViewControl::notifyChangeProperty);
 
96
 
 
97
                                property = (*child)->getProperty("Count");
 
98
                                if (!property->eventChangeProperty.exist(this, &AnimationViewControl::notifyChangeProperty))
 
99
                                        property->eventChangeProperty.connect(this, &AnimationViewControl::notifyChangeProperty);
 
100
                        }
 
101
                }
 
102
        }
 
103
 
 
104
        void AnimationViewControl::rebuildAnimations()
 
105
        {
 
106
                mAnimation.setTextureName("");
 
107
                mAnimation.clearFrames();
 
108
                mCurrentFrame = 0;
 
109
                mTime = 0;
 
110
 
 
111
                if (mParentData != nullptr)
 
112
                {
 
113
                        mAnimation.setTextureName(mParentData->getParent()->getPropertyValue("Texture"));
 
114
                        mAnimation.setSize(mParentData->getParent()->getPropertyValue<MyGUI::IntCoord>("Size").size());
 
115
                        mAnimation.setRate(mParentData->getPropertyValue<float>("Rate"));
 
116
 
 
117
                        for (Data::VectorData::const_iterator child = mParentData->getChilds().begin(); child != mParentData->getChilds().end(); child ++)
 
118
                        {
 
119
                                size_t count = (*child)->getPropertyValue<size_t>("Count");
 
120
                                MyGUI::IntPoint point = (*child)->getPropertyValue<MyGUI::IntPoint>("Point");
 
121
                                mAnimation.addFrame(point, count);
 
122
                        }
 
123
 
 
124
                        if (!mPlay)
 
125
                                updateSelectedFrame();
 
126
                }
 
127
 
 
128
                mImage->setImageTexture(mAnimation.getTextureName());
 
129
 
 
130
                if (mAnimation.getFrames().size() == 0)
 
131
                        mImage->setImageCoord(MyGUI::IntCoord());
 
132
                else
 
133
                        updateFrame();
 
134
 
 
135
                mFrameInfo->setCaption(MyGUI::utility::toString(mCurrentFrame, " : ", mAnimation.getFrames().size()));
 
136
 
 
137
                updateImageCoord();
 
138
        }
 
139
 
 
140
        void AnimationViewControl::notifyChangeProperty(PropertyPtr _sender)
 
141
        {
 
142
                rebuildAnimations();
 
143
        }
 
144
 
 
145
        void AnimationViewControl::updateImageCoord()
 
146
        {
 
147
                MyGUI::IntSize size = mAnimation.getSize();
 
148
                MyGUI::IntSize parentSize = mImage->getParentSize();
 
149
 
 
150
                mImage->setCoord((parentSize.width - size.width) / 2, (parentSize.height - size.height) / 2, size.width, size.height);
 
151
        }
 
152
 
 
153
        void AnimationViewControl::notifyChangeCoord(MyGUI::Widget* _sender)
 
154
        {
 
155
                updateImageCoord();
 
156
        }
 
157
 
 
158
        void AnimationViewControl::notifyFrameStart(float _frame)
 
159
        {
 
160
                if (mAnimation.getFrames().size() == 0 || !mPlay)
 
161
                        return;
 
162
 
 
163
                mTime += _frame;
 
164
 
 
165
                float len = mAnimation.getFrames()[mCurrentFrame].second * mAnimation.getRate();
 
166
 
 
167
                if (mTime > len)
 
168
                {
 
169
                        mTime -= len;
 
170
                        mCurrentFrame ++;
 
171
 
 
172
                        updateFrame();
 
173
                }
 
174
        }
 
175
 
 
176
        void AnimationViewControl::notifyMouseButtonClick(MyGUI::Widget* _sender)
 
177
        {
 
178
                if (_sender == mButtonPlay)
 
179
                {
 
180
                        mPlay = !mPlay;
 
181
                        mButtonPlay->setStateSelected(!mPlay);
 
182
 
 
183
                        if (!mPlay)
 
184
                                updateSelectedFrame();
 
185
                }
 
186
                else if (_sender == mButtonLeft)
 
187
                {
 
188
                        if (mAnimation.getFrames().size() != 0 && !mPlay)
 
189
                        {
 
190
                                mCurrentFrame += mAnimation.getFrames().size();
 
191
 
 
192
                                mCurrentFrame --;
 
193
                                updateFrame();
 
194
                        }
 
195
                }
 
196
                else if (_sender == mButtonRight && !mPlay)
 
197
                {
 
198
                        if (mAnimation.getFrames().size() != 0)
 
199
                        {
 
200
                                mCurrentFrame ++;
 
201
                                updateFrame();
 
202
                        }
 
203
                }
 
204
        }
 
205
 
 
206
        void AnimationViewControl::updateFrame()
 
207
        {
 
208
                mCurrentFrame %= mAnimation.getFrames().size();
 
209
                MyGUI::IntPoint point = mAnimation.getFrames()[mCurrentFrame].first;
 
210
 
 
211
                mImage->setImageCoord(MyGUI::IntCoord(point.left, point.top, mAnimation.getSize().width, mAnimation.getSize().height));
 
212
                mImage->setImageTile(mAnimation.getSize());
 
213
                mImage->setImageIndex(0);
 
214
                mFrameInfo->setCaption(MyGUI::utility::toString(mCurrentFrame, " : ", mAnimation.getFrames().size()));
 
215
        }
 
216
 
 
217
        void AnimationViewControl::updateSelectedFrame()
 
218
        {
 
219
                if (mParentData == nullptr || mParentData->getChildSelected() == nullptr)
 
220
                {
 
221
                        mImage->setImageCoord(MyGUI::IntCoord());
 
222
                        mCurrentFrame = 0;
 
223
                        mFrameInfo->setCaption(MyGUI::utility::toString(mCurrentFrame, " : ", mAnimation.getFrames().size()));
 
224
                }
 
225
                else
 
226
                {
 
227
                        DataPtr selected = mParentData->getChildSelected();
 
228
 
 
229
                        mCurrentFrame = mParentData->getChildIndex(selected);
 
230
                        updateFrame();
 
231
                }
 
232
        }
 
233
 
 
234
}