| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2013 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE:: * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
#include <cassert> |
| 24 |
|
| 25 |
// 3rd party library includes |
| 26 |
|
| 27 |
// FIFE includes |
| 28 |
// These includes are split up in two parts, separated by one empty line |
| 29 |
// First block: files included from the FIFE root src directory |
| 30 |
// Second block: files included from the same folder |
| 31 |
#include "gui/fifechan/base/gui_image.h" |
| 32 |
#include "util/base/exception.h" |
| 33 |
#include "util/time/timemanager.h" |
| 34 |
|
| 35 |
#include "animationicon.h" |
| 36 |
|
| 37 |
namespace fcn { |
| 38 |
AnimationIcon::AnimationIcon() : |
| 39 |
mTimemanager(FIFE::TimeManager::instance()), |
| 40 |
mAnimation(FIFE::AnimationPtr()), |
| 41 |
mCurrentImage(NULL), |
| 42 |
mAnimtime(0), |
| 43 |
mFrameIndex(-1), |
| 44 |
mRepeat(true), |
| 45 |
mPlay(true) { |
| 46 |
|
| 47 |
setScaling(false); |
| 48 |
setTiling(false); |
| 49 |
setOpaque(true); |
| 50 |
adjustSize(); |
| 51 |
} |
| 52 |
|
| 53 |
AnimationIcon::AnimationIcon(FIFE::AnimationPtr animation) : |
| 54 |
mTimemanager(FIFE::TimeManager::instance()), |
| 55 |
mAnimation(animation), |
| 56 |
mCurrentImage(NULL), |
| 57 |
mAnimtime(0), |
| 58 |
mFrameIndex(-1), |
| 59 |
mRepeat(true), |
| 60 |
mPlay(true) { |
| 61 |
|
| 62 |
// set first frame as new image |
| 63 |
if (mAnimation->getFrameCount() > 0) { |
| 64 |
mFrameIndex = 0; |
| 65 |
mCurrentImage = new FIFE::GuiImage(mAnimation->getFrame(mFrameIndex)); |
| 66 |
setImage(mCurrentImage); |
| 67 |
} |
| 68 |
setScaling(false); |
| 69 |
setTiling(false); |
| 70 |
setOpaque(true); |
| 71 |
adjustSize(); |
| 72 |
} |
| 73 |
|
| 74 |
AnimationIcon::~AnimationIcon() { |
| 75 |
delete mCurrentImage; |
| 76 |
} |
| 77 |
|
| 78 |
void AnimationIcon::setAnimation(FIFE::AnimationPtr animation) { |
| 79 |
mAnimation = animation; |
| 80 |
if (mPlay) { |
| 81 |
mAnimtime = mTimemanager->getTime(); |
| 82 |
} else { |
| 83 |
mAnimtime = 0; |
| 84 |
} |
| 85 |
// set first frame as new image |
| 86 |
if (mAnimation->getFrameCount() > 0) { |
| 87 |
mFrameIndex = 0; |
| 88 |
if (mCurrentImage) { |
| 89 |
delete mCurrentImage; |
| 90 |
mCurrentImage = 0; |
| 91 |
} |
| 92 |
mCurrentImage = new FIFE::GuiImage(mAnimation->getFrame(mFrameIndex)); |
| 93 |
setImage(mCurrentImage); |
| 94 |
} |
| 95 |
adjustSize(); |
| 96 |
} |
| 97 |
|
| 98 |
FIFE::AnimationPtr AnimationIcon::getAnimation() const { |
| 99 |
return mAnimation; |
| 100 |
} |
| 101 |
|
| 102 |
void AnimationIcon::setRepeating(bool repeat) { |
| 103 |
mRepeat = repeat; |
| 104 |
} |
| 105 |
|
| 106 |
bool AnimationIcon::isRepeating() const { |
| 107 |
return mRepeat; |
| 108 |
} |
| 109 |
|
| 110 |
void AnimationIcon::play() { |
| 111 |
mPlay = true; |
| 112 |
mAnimtime = mTimemanager->getTime(); |
| 113 |
} |
| 114 |
|
| 115 |
bool AnimationIcon::isPlaying() const { |
| 116 |
return mPlay; |
| 117 |
} |
| 118 |
|
| 119 |
void AnimationIcon::pause() { |
| 120 |
mPlay = false; |
| 121 |
} |
| 122 |
|
| 123 |
void AnimationIcon::stop() { |
| 124 |
mPlay = false; |
| 125 |
// set first frame as new image |
| 126 |
if (mAnimation->getFrameCount() > 0) { |
| 127 |
mFrameIndex = 0; |
| 128 |
mCurrentImage = new FIFE::GuiImage(mAnimation->getFrame(mFrameIndex)); |
| 129 |
setImage(mCurrentImage); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void AnimationIcon::logic() { |
| 134 |
if (isPlaying()) { |
| 135 |
int32_t index = mFrameIndex; |
| 136 |
if (isRepeating()) { |
| 137 |
index = mAnimation->getFrameIndex((mTimemanager->getTime() - mAnimtime) % mAnimation->getDuration()); |
| 138 |
} else { |
| 139 |
index = mAnimation->getFrameIndex(mTimemanager->getTime() - mAnimtime); |
| 140 |
} |
| 141 |
if (index != mFrameIndex) { |
| 142 |
mFrameIndex = index; |
| 143 |
if (mCurrentImage) { |
| 144 |
delete mCurrentImage; |
| 145 |
mCurrentImage = 0; |
| 146 |
} |
| 147 |
if (mFrameIndex >= 0) { |
| 148 |
mCurrentImage = new FIFE::GuiImage(mAnimation->getFrame(mFrameIndex)); |
| 149 |
} |
| 150 |
setImage(mCurrentImage); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |