[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdegames/kbattleship/src
From:       Paolo Capriotti <paolo.capriotti () gmail ! com>
Date:       2007-05-01 0:31:50
Message-ID: 1177979510.782006.12388.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 659896 by capriotti:

Added nice animation effects in WelcomeScreen


 M  +14 -3     animation.cpp  
 M  +4 -1      animation.h  
 M  +111 -17   button.cpp  
 M  +34 -1     button.h  
 M  +9 -1      welcomescreen.cpp  
 M  +3 -0      welcomescreen.h  


--- trunk/KDE/kdegames/kbattleship/src/animation.cpp #659895:659896
@@ -12,6 +12,11 @@
 #include <kgamecanvas.h>
 #include <kdebug.h>
 
+Animation::~Animation()
+{
+    emit done();
+}
+
 AnimationGroup::AnimationGroup()
 : m_running(-1)
 {
@@ -78,9 +83,15 @@
 
 bool FadeAnimation::step(int t)
 {
-    int opacity = int(m_from + (m_to - m_from) * (t - m_start) / (double)m_time);
-    m_sprite->setOpacity(opacity);
-    return opacity >= m_to;
+    if (t >= m_time + m_start) {
+        m_sprite->setOpacity(m_to);
+        return true;
+    }
+    else {
+        int opacity = int(m_from + (m_to - m_from) * (t - m_start) / (double)m_time);
+        m_sprite->setOpacity(opacity);
+        return false;
+    }
 }
 
 
--- trunk/KDE/kdegames/kbattleship/src/animation.h #659895:659896
@@ -20,9 +20,12 @@
 {
 Q_OBJECT
 public:
-    virtual ~Animation() { }
+    virtual ~Animation();
     virtual void start(int t) = 0;
     virtual bool step(int t) = 0;
+    
+signals:
+    void done();
 };
 
 class AnimationGroup : public Animation
--- trunk/KDE/kdegames/kbattleship/src/button.cpp #659895:659896
@@ -11,7 +11,10 @@
 
 #include <QImage>
 #include <kdebug.h>
+#include <math.h> // fabs
 
+#include "animator.h"
+
 Button::Button(WelcomeScreen* parent, const QIcon& icon, 
                const QFont& font, const QString& text)
 : QObject(parent)
@@ -21,6 +24,7 @@
 , m_text(text)
 , m_down(false)
 , m_hover(false)
+, m_brightness(BRIGHTNESS_NORMAL)
 {
     QFontMetrics fm(m_font);
     int h = fm.height();
@@ -44,20 +48,10 @@
         QPen pen(QColor(200, 200, 220, 255));
         pen.setWidth(2);
         p.setPen(pen);
-        int brightness;
-        if (m_hover) {            
-            if (m_down) {
-                brightness = 150;
-            }
-            else {
-                brightness = 80;
-            }
-        }
-        else {
-            brightness = 0;
-        }
-
-        p.setBrush(QBrush(QColor(brightness, brightness, brightness, 100)));
+        p.setBrush(QBrush(QColor(
+            static_cast<int>(m_brightness),
+            static_cast<int>(m_brightness), 
+            static_cast<int>(m_brightness), 100)));
         p.drawRoundRect(1, 1, m_size.width() - 2, m_size.height() -2, 8, 40);
         p.drawPixmap(10, 
                     m_size.height() / 2 - 16, 
@@ -79,26 +73,50 @@
     return m_size;
 }
 
-void Button::onMousePress(const QPoint& p)
+void Button::onMousePress(const QPoint&)
 {
     if (!m_down) {
         m_down = true;
+        if (m_animation) {
+            m_animation->abort();
+        }
+        m_brightness = BRIGHTNESS_DOWN;
+        
         repaint();
     }
 }
 
-void Button::onMouseRelease(const QPoint& p)
+void Button::onMouseRelease(const QPoint&)
 {
     if (m_down) {
         m_down = false;
+        if (m_animation) {
+            m_animation->abort();
+        }
+        m_brightness = BRIGHTNESS_NORMAL;
         repaint();
     }
 }
 
-void Button::onMouseMove(const QPoint& p)
+void Button::onMouseMove(const QPoint&)
 {
     if (!m_hover) {
         m_hover = true;
+        
+        if (m_down) {
+            if (m_animation) {
+                m_animation->abort();
+            }
+            m_brightness = BRIGHTNESS_HOVER;
+        }
+        else if (m_animation) {
+            m_animation->setBrightness(BRIGHTNESS_HOVER);
+        }
+        else {
+            m_animation = new ButtonAnimation(this, BRIGHTNESS_HOVER);
+            Animator::instance()->add(m_animation);
+        }
+        
         repaint();
     }
 }
@@ -107,6 +125,21 @@
 {
     if (m_hover) {
         m_hover = false;
+        
+        if (m_down) {
+            if (m_animation) {
+                m_animation->abort();
+            }
+            m_brightness = BRIGHTNESS_NORMAL;
+        }
+        else if (m_animation) {
+            m_animation->setBrightness(BRIGHTNESS_NORMAL);
+        }
+        else {
+            m_animation = new ButtonAnimation(this, BRIGHTNESS_NORMAL);
+            Animator::instance()->add(m_animation);
+        }
+        
         repaint();
     }
 }
@@ -117,5 +150,66 @@
     emit clicked();
 }
 
+double Button::brightness() const
+{
+    return m_brightness;
+}
+
+void Button::setBrightness(double value)
+{
+    m_brightness = value;
+    repaint();
+}
+
+// ------------
+
+double ButtonAnimation::m_speed = 0.38;
+
+ButtonAnimation::ButtonAnimation(Button* button, int brightness)
+: m_button(button)
+, m_brightness(brightness)
+{
+    m_last = -1;
+}
+
+void ButtonAnimation::start(int t)
+{
+    m_last = t;
+}
+
+bool ButtonAnimation::step(int t)
+{
+    if (m_last == -1) {
+        return true;
+    }
+    
+    int sign = (m_button->brightness() > m_brightness) ? -1 : 1;
+    double delta = (t - m_last) * m_speed;
+    m_last = t;
+    if (fabs(m_button->brightness() - m_brightness) <= delta) {
+        m_button->setBrightness(m_brightness);
+        return true;
+    }
+    else {
+        m_button->setBrightness(m_button->brightness() + sign * delta);
+        return false;
+    }
+}
+
+void ButtonAnimation::abort()
+{
+    m_last = -1;
+}
+
+void ButtonAnimation::setBrightness(int value)
+{
+    m_brightness = value;
+}
+
+ButtonAnimation::~ButtonAnimation()
+{
+}
+
+
 #include "button.moc"
 
--- trunk/KDE/kdegames/kbattleship/src/button.h #659895:659896
@@ -10,14 +10,23 @@
 #ifndef BUTTON_H
 #define BUTTON_H
 
-#include "welcomescreen.h"
 #include <QIcon>
 #include <QFont>
 #include <QSize>
+#include <QPointer>
+#include "welcomescreen.h"
+#include "animation.h"
 
+class ButtonAnimation;
+
 class Button : public QObject, public KGameCanvasPixmap
 {
 Q_OBJECT
+    enum {
+        BRIGHTNESS_NORMAL = 0,
+        BRIGHTNESS_HOVER = 120,
+        BRIGHTNESS_DOWN = 180
+    };
     QIcon m_icon;
     QFont m_font;
     QString m_text;
@@ -25,6 +34,8 @@
     
     bool m_down;
     bool m_hover;
+    double m_brightness;
+    QPointer<ButtonAnimation> m_animation;
     
     virtual void repaint();
 public:
@@ -38,8 +49,30 @@
     void onMouseMove(const QPoint& p);
     void onMouseLeave();
     void onClicked();
+    
+    void setBrightness(double value);
+    double brightness() const;
 signals:
     void clicked();
 };
 
+class ButtonAnimation : public Animation
+{
+Q_OBJECT
+public:
+    Button* m_button;
+    int m_brightness;
+    static double m_speed;
+    double m_current;
+    int m_last;
+public:
+    ButtonAnimation(Button* button, int brightness);
+    ~ButtonAnimation();
+    virtual void start(int t);
+    virtual bool step(int t);
+    virtual void abort();
+    
+    void setBrightness(int value);
+};
+
 #endif // BUTTON_H
--- trunk/KDE/kdegames/kbattleship/src/welcomescreen.cpp #659895:659896
@@ -12,6 +12,7 @@
 #include <kdebug.h>
 
 #include "button.h"
+#include "animator.h"
 
 WelcomeScreen::WelcomeScreen(KGameCanvasAbstract* parent, const QFont& font)
 : KGameCanvasGroup(parent)
@@ -120,12 +121,19 @@
         // actual click event
         m_clicked->onClicked();
         
-        hide();
+        Animation* hideAnimation = new FadeAnimation(this, opacity(), 0, 500);
+//         connect(hideAnimation, SIGNAL(done()), this, SLOT(hide()));
+        Animator::instance()->add(hideAnimation);
     }
     
     m_clicked = 0;
 }
 
+void WelcomeScreen::hide()
+{
+    KGameCanvasGroup::hide();
+}
+
 void WelcomeScreen::onMouseLeave()
 {
     if (m_hover) {
--- trunk/KDE/kdegames/kbattleship/src/welcomescreen.h #659895:659896
@@ -40,6 +40,9 @@
     void onMouseRelease(const QPoint& p);
     void onMouseMove(const QPoint& p);
     void onMouseLeave();
+    
+public slots:
+    void hide();
 };
 
 #endif // WELCOMESCREEN_H
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic