From a8934f323fd7e48014addb55ff25ef0486aebbd1 Mon Sep 17 00:00:00 2001 From: Samu Laaksonen Date: Sat, 29 Sep 2012 22:21:14 +0300 Subject: MenuScene changes Changed MenuScene to use buttons based on new AnimatingUiElement - buttons now have scale animation allowing them to be a bit more "lively" Added hero_concept as png ":D" --- data/gfx/hero_concept.png | Bin 0 -> 2250 bytes src/AnimatingUiElement.cpp | 146 +++++++++++++++++++++++++++++++++++++++++++++ src/AnimatingUiElement.h | 42 +++++++++++++ src/MenuScene.cpp | 27 +++++++++ src/src.pro | 6 +- 5 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 data/gfx/hero_concept.png create mode 100644 src/AnimatingUiElement.cpp create mode 100644 src/AnimatingUiElement.h diff --git a/data/gfx/hero_concept.png b/data/gfx/hero_concept.png new file mode 100644 index 0000000..2021c34 Binary files /dev/null and b/data/gfx/hero_concept.png differ diff --git a/src/AnimatingUiElement.cpp b/src/AnimatingUiElement.cpp new file mode 100644 index 0000000..de7c231 --- /dev/null +++ b/src/AnimatingUiElement.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include + +#include "AnimatingUiElement.h" + +QPixmap* AnimatingUiElement::s_tmpGfx = 0; +int AnimatingUiElement::s_ref = 0; + +AnimatingUiElement::AnimatingUiElement(const QString& text, QObject *parent, QGraphicsItem* graphicsParent) : + QObject(parent), QGraphicsPixmapItem(graphicsParent) +{ + m_durationPressed = 500; + m_durationReleased = 400; + m_text = text; + + m_animation = 0; + + initialize(); +} + +AnimatingUiElement::~AnimatingUiElement() +{ + s_ref--; + + if (s_tmpGfx && s_ref == 0) + { + delete s_tmpGfx; + s_tmpGfx = 0; + } +} + +void AnimatingUiElement::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (m_animation) + { + m_animation->stop(); + delete m_animation; + m_animation = 0; + } + + m_animation = new QPropertyAnimation(this, "scale"); + connect(m_animation, SIGNAL(finished()), this, SLOT(animationFinished())); + m_animation->setDuration(m_durationPressed); + m_animation->setEndValue(1.3); + m_animation->setEasingCurve(QEasingCurve::OutBounce); //or OutBounce, OutQuint... + + m_animation->start(); + + m_released = false; +} + +void AnimatingUiElement::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + m_released = true; + + if (m_animation) + { + m_animation->stop(); + delete m_animation; + m_animation = 0; + } + + m_animation = new QPropertyAnimation(this, "scale"); + connect(m_animation, SIGNAL(finished()), this, SLOT(animationFinished())); + m_animation->setDuration(m_durationReleased); + m_animation->setEndValue(1.0); + m_animation->setEasingCurve(QEasingCurve::OutBounce); //or OutBounce, OutQuint... + + m_animation->start(); + + m_released = true; +} + +void AnimatingUiElement::initialize() +{ + s_ref++; + + int pw = 280; + int ph = 60; + + // Draw the gradients only once + if (!s_tmpGfx) + { + //DBG("generating button gradients"); + + s_tmpGfx = new QPixmap(pw, ph); + QPainter painter(s_tmpGfx); + + painter.setPen(Qt::NoPen); + + painter.setBrush(Qt::white); + painter.drawRect(s_tmpGfx->rect()); + + QLinearGradient grad; + + grad.setCoordinateMode(QGradient::ObjectBoundingMode); + + grad.setStart(0, 0); + grad.setFinalStop(0, 1); + + grad.setColorAt(0, Qt::red); + grad.setColorAt(0.20, QColor(255, 127, 0)); + grad.setColorAt(0.35, QColor(255, 255, 0)); + grad.setColorAt(0.5, QColor(0, 255, 0)); + grad.setColorAt(0.65, QColor(0, 0, 255)); + grad.setColorAt(0.80, QColor(111, 0, 255)); + grad.setColorAt(1, QColor(143, 0, 255)); + + painter.setBrush(QBrush(grad)); + painter.drawRect(4, 4, pw-8, ph-8); + } + + QPixmap buttonImage = QPixmap(*s_tmpGfx); + QPainter painter(&buttonImage); + + QFont font("Arial", 36); + font.setLetterSpacing(QFont::PercentageSpacing, 95); + + QFontMetrics metrics(font); + QRect bbox = metrics.boundingRect(m_text); + + QPainterPath path; + + int x = (ph - metrics.height()) / 2; + path.addText(pw/2-bbox.width()/2, x + metrics.ascent(), font, m_text); + + painter.setRenderHint(QPainter::Antialiasing); + painter.setPen(QPen(Qt::black, 3)); + painter.setBrush(Qt::white); + painter.drawPath(path); + + setPixmap(buttonImage); +} + +void AnimatingUiElement::animationFinished() +{ + delete m_animation; + m_animation = 0; + if (m_released) + { + m_released = false; + emit clicked(); + } +} diff --git a/src/AnimatingUiElement.h b/src/AnimatingUiElement.h new file mode 100644 index 0000000..c02c7af --- /dev/null +++ b/src/AnimatingUiElement.h @@ -0,0 +1,42 @@ +#ifndef AnimatingUiElement_h +#define AnimatingUiElement_h + +#include +#include + +class QPropertyAnimation; + +class AnimatingUiElement : public QObject, public QGraphicsPixmapItem +{ + Q_OBJECT + + Q_PROPERTY(qreal scale READ scale WRITE setScale) +public: + AnimatingUiElement(const QString& text, QObject *parent = 0, QGraphicsItem* graphicsParent = 0); + virtual ~AnimatingUiElement(); + + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + + void initialize(); + +signals: + void clicked(); + +public slots: + +private slots: + void animationFinished(); + +private: + QPropertyAnimation* m_animation; + QString m_text; + bool m_released; + int m_durationPressed; + int m_durationReleased; + + static QPixmap *s_tmpGfx; + static int s_ref; +}; + +#endif // AnimatingUiElement_h diff --git a/src/MenuScene.cpp b/src/MenuScene.cpp index 241f423..a7c2a75 100644 --- a/src/MenuScene.cpp +++ b/src/MenuScene.cpp @@ -4,6 +4,8 @@ #include "GraphicsButtonObject.h" #include "GameView.h" +#include "AnimatingUiElement.h" + #include "MenuScene.h" MenuScene::MenuScene(const QString &name, const QRectF &rect, GameView *parent) @@ -40,6 +42,30 @@ void MenuScene::initializeScene() { int yoff = 200; + AnimatingUiElement *btn1 = new AnimatingUiElement("Play"); + + btn1->setOffset(-btn1->boundingRect().width() / 2, + -btn1->boundingRect().height() / 2); + btn1->setPos(sceneRect().width() / 2, yoff); + addItem(btn1); + + AnimatingUiElement *btn2 = new AnimatingUiElement("Credits"); + btn2->setOffset(-btn2->boundingRect().width() / 2, + -btn2->boundingRect().height() / 2); + btn2->setPos(sceneRect().width() / 2, btn1->pos().y() + btn1->boundingRect().height() + 20); + addItem(btn2); + + AnimatingUiElement *btn3 = new AnimatingUiElement("Quit"); + btn3->setOffset(-btn3->boundingRect().width() / 2, + -btn3->boundingRect().height() / 2); + btn3->setPos(sceneRect().width() / 2, btn2->pos().y() + btn2->boundingRect().height() + 20); + addItem(btn3); + + connect(btn1, SIGNAL(clicked()), gameView(), SLOT(showLevelSelectionScene())); + connect(btn2, SIGNAL(clicked()), gameView(), SLOT(showCreditsScene())); + connect(btn3, SIGNAL(clicked()), qApp, SLOT(quit())); + + /* GraphicsButtonObject *btn1 = new GraphicsButtonObject("Play", 0, this); btn1->setPos(260, yoff); @@ -52,6 +78,7 @@ void MenuScene::initializeScene() connect(btn1, SIGNAL(clicked()), gameView(), SLOT(showLevelSelectionScene())); connect(btn2, SIGNAL(clicked()), gameView(), SLOT(showCreditsScene())); connect(btn3, SIGNAL(clicked()), qApp, SLOT(quit())); + */ addTitle("prism"); } diff --git a/src/src.pro b/src/src.pro index de37d2e..397b1bc 100644 --- a/src/src.pro +++ b/src/src.pro @@ -36,7 +36,8 @@ HEADERS += MainWindow.h \ BasicEnemy.h \ BarDisplay.h \ CircularDisplay.h \ - HeadsUpDisplay.h + HeadsUpDisplay.h \ + AnimatingUiElement.h \ SOURCES += main.cpp \ MainWindow.cpp \ @@ -58,4 +59,5 @@ SOURCES += main.cpp \ BasicEnemy.cpp \ BarDisplay.cpp \ CircularDisplay.cpp \ - HeadsUpDisplay.cpp + HeadsUpDisplay.cpp \ + AnimatingUiElement.cpp \ -- cgit v1.2.3