|
| 1 | +#include "animationeditor.h" |
| 2 | + |
| 3 | +AnimationEditor::AnimationEditor(QWidget *parent) : QWidget(parent) |
| 4 | +{ |
| 5 | + isMouseHovering=false; |
| 6 | + isMouseDown = false; |
| 7 | + setMouseTracking(true); |
| 8 | + BiggestAnimation = 0; |
| 9 | + MouseHoverIndex =-1; |
| 10 | + PlusIconImg = new QPixmap(":/new/prefix1/add.png"); |
| 11 | +} |
| 12 | + |
| 13 | +AnimationEditor::~AnimationEditor() |
| 14 | +{ |
| 15 | + |
| 16 | +} |
| 17 | + |
| 18 | +void AnimationEditor::setTileBuffer(TileBuffer *tilebuffer) |
| 19 | +{ |
| 20 | + Buffer = tilebuffer; |
| 21 | + TileSizeX = Buffer->getTileSizeX(); |
| 22 | + TileSizeY = Buffer->getTileSizeY(); |
| 23 | + CursorTiles.clear(); |
| 24 | + this->repaint(); |
| 25 | +} |
| 26 | + |
| 27 | +void AnimationEditor::setAnimationBuffer(AnimationBuffer *animbuffer) |
| 28 | +{ |
| 29 | + AnimBuffer = animbuffer; |
| 30 | + CursorTiles.clear(); |
| 31 | + this->repaint(); |
| 32 | +} |
| 33 | + |
| 34 | +void AnimationEditor::paintEvent(QPaintEvent *) |
| 35 | +{ |
| 36 | + |
| 37 | + QPainter pt(this); |
| 38 | + |
| 39 | + pt.fillRect(this->rect(),Qt::SolidPattern); |
| 40 | + |
| 41 | + pt.drawLine(TileSizeX + 3, 0, TileSizeX + 2, this->height()); |
| 42 | + |
| 43 | + if (Buffer->getCount() > 0) |
| 44 | + { |
| 45 | + if (AnimBuffer->Animations.size() > 0){ |
| 46 | + |
| 47 | + for (int i=0;i<AnimBuffer->Animations.size(); i++) |
| 48 | + { |
| 49 | + if (AnimBuffer->Animations[i].Frames.size() > 0) |
| 50 | + pt.drawPixmap(0,i*TileSizeY, *AnimBuffer->getFramePixmap(i)); |
| 51 | + |
| 52 | + for (int j=0;j<AnimBuffer->Animations[i].Frames.size(); j++) |
| 53 | + { |
| 54 | + pt.drawPixmap(j*TileSizeX + TileSizeX + 5, i*TileSizeY, *Buffer->getTilePixmapAt( AnimBuffer->Animations[i].Frames[j] ) ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + pt.setPen(Qt::red); |
| 59 | + pt.drawRect(GrabbedAreaRect); |
| 60 | + if (isMouseHovering && ! isMouseDown) |
| 61 | + { |
| 62 | + //pt.setPen(Qt::darkRed); |
| 63 | + pt.drawRect(HiglightRect); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + if (MouseHoverIndex >= 0) |
| 68 | + { |
| 69 | + if (MouseHoverIndex >= AnimBuffer->Animations.size()) |
| 70 | + pt.drawPixmap(TileSizeX + 5 +TileSizeX/2 - PlusIconImg->width()/2 |
| 71 | + , AnimBuffer->Animations.size()*TileSizeY + TileSizeY/2 - PlusIconImg->height()/2 , |
| 72 | + *PlusIconImg); |
| 73 | + else |
| 74 | + pt.drawPixmap(AnimBuffer->Animations[MouseHoverIndex].Frames.size()*TileSizeX+TileSizeX+5 + TileSizeX/2 - PlusIconImg->width()/2 , |
| 75 | + MouseHoverIndex*TileSizeY + TileSizeY/2 - PlusIconImg->height()/2 , |
| 76 | + *PlusIconImg); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void AnimationEditor::mouseMoveEvent(QMouseEvent *ev) |
| 81 | +{ |
| 82 | + if (ev->pos().y()/TileSizeY >= AnimBuffer->Animations.size()) |
| 83 | + { |
| 84 | + MouseHoverIndex = AnimBuffer->Animations.size(); |
| 85 | + } else |
| 86 | + if (ev->pos().x() <= TileSizeX) |
| 87 | + { |
| 88 | + if (isMouseDown) |
| 89 | + { |
| 90 | + GrabbedAreaRect.setLeft(0/*(ev->x()/TileSizeX)*TileSizeX*/); |
| 91 | + GrabbedAreaRect.setTop((ev->y()/TileSizeY)*TileSizeY); |
| 92 | + GrabbedAreaRect.setWidth(TileSizeX); |
| 93 | + GrabbedAreaRect.setHeight(TileSizeY); |
| 94 | + }else |
| 95 | + { |
| 96 | + HiglightRect.setLeft(0/*(ev->x()/TileSizeX)*TileSizeX*/); |
| 97 | + HiglightRect.setTop((ev->y()/TileSizeY)*TileSizeY); |
| 98 | + HiglightRect.setWidth(TileSizeX); |
| 99 | + HiglightRect.setHeight(TileSizeY); |
| 100 | + } |
| 101 | + isMouseHovering = true; |
| 102 | + isMouseDown = ev->buttons() & Qt::LeftButton; |
| 103 | + MouseHoverIndex = -1; |
| 104 | + } else |
| 105 | + MouseHoverIndex = (ev->y()/TileSizeY); |
| 106 | + this->repaint(); |
| 107 | +} |
| 108 | + |
| 109 | +void AnimationEditor::mousePressEvent(QMouseEvent *ev) |
| 110 | +{ |
| 111 | + if (ev->pos().x() <= TileSizeX) |
| 112 | + { |
| 113 | + if (ev->pos().y()/TileSizeY >= AnimBuffer->Animations.size()) return; |
| 114 | + GrabbedAreaRect.setLeft(0/*(ev->x()/TileSizeX)*TileSizeX*/); |
| 115 | + GrabbedAreaRect.setTop((ev->y()/TileSizeY)*TileSizeY); |
| 116 | + GrabbedAreaRect.setWidth(TileSizeX); |
| 117 | + GrabbedAreaRect.setHeight(TileSizeY); |
| 118 | + isMouseDown = true; |
| 119 | + } else |
| 120 | + if (CursorTiles.size() > 0 ) |
| 121 | + { |
| 122 | + int index = ev->pos().y()/TileSizeY; |
| 123 | + |
| 124 | + if (index >= AnimBuffer->Animations.size()) |
| 125 | + { |
| 126 | + AddAnimation(); |
| 127 | + index = AnimBuffer->Animations.size()-1; |
| 128 | + } |
| 129 | + |
| 130 | + AddFramesFromCursorTo(index); |
| 131 | + |
| 132 | + if ( BiggestAnimation < AnimBuffer->Animations[index].Frames.size() ) |
| 133 | + BiggestAnimation = AnimBuffer->Animations[index].Frames.size(); |
| 134 | + } |
| 135 | + |
| 136 | + this->setMinimumHeight(TileSizeY*(AnimBuffer->Animations.size()+1)); |
| 137 | + this->setMinimumWidth(TileSizeX + 5 + BiggestAnimation*TileSizeX); |
| 138 | + |
| 139 | + MouseHoverIndex =-1; |
| 140 | + this->repaint(); |
| 141 | +} |
| 142 | + |
| 143 | +void AnimationEditor::mouseReleaseEvent(QMouseEvent *ev) |
| 144 | +{ |
| 145 | + isMouseDown = false; |
| 146 | + if (ev->pos().x() <= TileSizeX) |
| 147 | + { |
| 148 | + emit onAnimationSelected(); |
| 149 | + } |
| 150 | + MouseHoverIndex =-1; |
| 151 | + this->repaint(); |
| 152 | +} |
| 153 | + |
| 154 | +void AnimationEditor::leaveEvent(QEvent *) |
| 155 | +{ |
| 156 | + isMouseHovering = false; |
| 157 | + isMouseDown = false; |
| 158 | + MouseHoverIndex =-1; |
| 159 | + this->repaint(); |
| 160 | +} |
| 161 | + |
| 162 | +void AnimationEditor::setCursorTiles(QVector<QVector<int> > cur) |
| 163 | +{ |
| 164 | + CursorTiles.clear(); |
| 165 | + CursorTiles = cur; |
| 166 | +} |
| 167 | + |
| 168 | +int AnimationEditor::getSelectedAnimation() |
| 169 | +{ |
| 170 | + if (AnimBuffer->Animations.size() <= 0) return -1; |
| 171 | + return GrabbedAreaRect.top()/TileSizeY; |
| 172 | +} |
| 173 | + |
| 174 | +void AnimationEditor::AddAnimation() |
| 175 | +{ |
| 176 | + AnimBuffer->Animations.append(Animation()); |
| 177 | + int index = AnimBuffer->Animations.size()-1; |
| 178 | + AnimBuffer->Animations[index].Fps=5; |
| 179 | + AnimBuffer->Animations[index].isPingPong=false; |
| 180 | + AnimBuffer->Animations[index].CurrentFrame = 0; |
| 181 | + AnimBuffer->Animations[index].delay = 0; |
| 182 | + AnimBuffer->syncAllAnimations(); |
| 183 | +} |
| 184 | + |
| 185 | +void AnimationEditor::AddFramesFromCursorTo(int AnimationIndex) |
| 186 | +{ |
| 187 | + for (int j=0;j<CursorTiles.size(); j++) |
| 188 | + for (int i=0;i<CursorTiles[j].size(); i++) |
| 189 | + { |
| 190 | + AnimBuffer->Animations[AnimationIndex].Frames.append(CursorTiles[j][i]); |
| 191 | + } |
| 192 | + AnimBuffer->syncAllAnimations(); |
| 193 | +} |
| 194 | + |
0 commit comments