~keith-penguin/kdegames/trunk

« back to all changes in this revision

Viewing changes to kbattleship/src/button.cpp

  • Committer: Keith Worrell
  • Date: 2009-03-18 05:35:28 UTC
  • Revision ID: keith.worrell@gmail.com-20090318053528-mx6x9c0ngmg0kg6p
imported project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2007 Riccardo Iaconelli <ruphy@fsfe.org>
 
3
            (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
 
4
            
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU General Public License as published by
 
7
  the Free Software Foundation; either version 2 of the License, or
 
8
  (at your option) any later version.
 
9
*/
 
10
 
 
11
#include "button.h"
 
12
 
 
13
#include <QImage>
 
14
#include <kdebug.h>
 
15
#include <math.h> // fabs
 
16
 
 
17
#include "animator.h"
 
18
 
 
19
Button::Button(WelcomeScreen* parent, const QIcon& icon, 
 
20
               const QFont& font, const QString& text)
 
21
: QObject(parent)
 
22
, KGameCanvasPixmap(parent)
 
23
, m_icon(icon)
 
24
, m_font(font)
 
25
, m_text(text)
 
26
, m_fixed_width(false)
 
27
, m_down(false)
 
28
, m_hover(false)
 
29
, m_brightness(BRIGHTNESS_NORMAL)
 
30
, m_editor(0)
 
31
{
 
32
    computeSize();
 
33
    repaint();
 
34
}
 
35
 
 
36
Button::~Button()
 
37
{
 
38
    if (m_animation) {
 
39
        m_animation->abort();
 
40
    }
 
41
    delete m_editor;
 
42
}
 
43
 
 
44
void Button::setWidth(int width)
 
45
{
 
46
    m_fixed_width = width != -1;
 
47
    m_size.setWidth(width);
 
48
    computeSize();
 
49
}
 
50
 
 
51
void Button::computeSize()
 
52
{
 
53
    QFontMetrics fm(m_font);
 
54
    m_text_width = fm.width(m_text);
 
55
    int h = fm.height();
 
56
    if (h < 32) {
 
57
        h = 32;
 
58
    }
 
59
    if (!m_fixed_width) {        
 
60
        m_size = QSize(m_text_width, h);
 
61
        m_size.rwidth() += 10 + 32 + 10 + 10;
 
62
    }
 
63
    else {
 
64
        m_size.setHeight(h);
 
65
    }
 
66
    
 
67
    m_size.rheight() += 10 + 10;
 
68
}
 
69
 
 
70
void Button::repaint()
 
71
{
 
72
    QImage tmp(m_size, QImage::Format_ARGB32_Premultiplied);
 
73
    tmp.fill(0);
 
74
    {
 
75
        QPainter p(&tmp);
 
76
        p.setRenderHint(QPainter::Antialiasing);
 
77
        QPen pen(QColor(200, 200, 220, 255));
 
78
        pen.setWidth(2);
 
79
        p.setPen(pen);
 
80
        p.setBrush(QBrush(QColor(
 
81
            static_cast<int>(m_brightness),
 
82
            static_cast<int>(m_brightness), 
 
83
            static_cast<int>(m_brightness), 100)));
 
84
        p.drawRoundRect(1, 1, m_size.width() - 2, m_size.height() -2, 
 
85
            2000 / m_size.width(), 2000 / m_size.height());
 
86
        p.drawPixmap(10, 
 
87
                    m_size.height() / 2 - 16, 
 
88
                    32, 
 
89
                    32, 
 
90
                    m_icon.pixmap(32, 32));
 
91
    
 
92
        if (!m_editor) {
 
93
            p.setFont(m_font);
 
94
            p.drawText(textPos(), m_text);
 
95
        }
 
96
    }
 
97
    
 
98
    setPixmap(QPixmap::fromImage(tmp));
 
99
    updateEditor();
 
100
}
 
101
 
 
102
QSize Button::size() const
 
103
{
 
104
    return m_size;
 
105
}
 
106
 
 
107
QPoint Button::textPos() const
 
108
{
 
109
    return QPoint(32 + 10 + (m_size.width() - 32 - 10 - m_text_width) / 2, 
 
110
        m_size.height() / 2 + 6);
 
111
}
 
112
 
 
113
void Button::onMousePress(const QPoint&)
 
114
{
 
115
    if (!m_editor && !m_down) {
 
116
        m_down = true;
 
117
        if (m_animation) {
 
118
            m_animation->abort();
 
119
        }
 
120
        m_brightness = BRIGHTNESS_DOWN;
 
121
        
 
122
        repaint();
 
123
    }
 
124
}
 
125
 
 
126
void Button::onMouseRelease(const QPoint&)
 
127
{
 
128
    if (!m_editor && m_down) {
 
129
        m_down = false;
 
130
        if (m_animation) {
 
131
            m_animation->abort();
 
132
        }
 
133
        m_brightness = BRIGHTNESS_NORMAL;
 
134
        repaint();
 
135
    }
 
136
}
 
137
 
 
138
void Button::onMouseMove(const QPoint&)
 
139
{
 
140
    if (!m_editor && !m_hover) {
 
141
        m_hover = true;
 
142
        
 
143
        if (m_down) {
 
144
            if (m_animation) {
 
145
                m_animation->abort();
 
146
            }
 
147
            m_brightness = BRIGHTNESS_HOVER;
 
148
        }
 
149
        else if (m_animation) {
 
150
            m_animation->setBrightness(BRIGHTNESS_HOVER);
 
151
        }
 
152
        else {
 
153
            m_animation = new ButtonAnimation(this, BRIGHTNESS_HOVER);
 
154
            Animator::instance()->add(m_animation);
 
155
        }
 
156
        
 
157
        repaint();
 
158
    }
 
159
}
 
160
 
 
161
void Button::onMouseLeave()
 
162
{
 
163
    if (m_hover) {
 
164
        m_hover = false;
 
165
        
 
166
        if (m_down) {
 
167
            if (m_animation) {
 
168
                m_animation->abort();
 
169
            }
 
170
            m_brightness = BRIGHTNESS_NORMAL;
 
171
        }
 
172
        else if (m_animation) {
 
173
            m_animation->setBrightness(BRIGHTNESS_NORMAL);
 
174
        }
 
175
        else {
 
176
            m_animation = new ButtonAnimation(this, BRIGHTNESS_NORMAL);
 
177
            Animator::instance()->add(m_animation);
 
178
        }
 
179
        
 
180
        repaint();
 
181
    }
 
182
}
 
183
 
 
184
bool Button::onClicked()
 
185
{
 
186
    if (!m_editor) {
 
187
        kDebug() << "clicked";
 
188
        emit clicked();
 
189
        return true;
 
190
    }
 
191
    else {
 
192
        return false;
 
193
    }
 
194
}
 
195
 
 
196
void Button::setText(const QString& text)
 
197
{
 
198
    m_text = text;
 
199
    repaint();
 
200
}
 
201
 
 
202
double Button::brightness() const
 
203
{
 
204
    return m_brightness;
 
205
}
 
206
 
 
207
void Button::setBrightness(double value)
 
208
{
 
209
    m_brightness = value;
 
210
    repaint();
 
211
}
 
212
 
 
213
KGameCanvasPixmap* Button::extractIcon()
 
214
{
 
215
    KGameCanvasPixmap* res = new KGameCanvasPixmap(
 
216
        KGameCanvasPixmap::canvas());
 
217
    res->moveTo(pos() + QPoint(10, 10));
 
218
    res->setPixmap(m_icon.pixmap(32, 32));
 
219
    
 
220
    m_icon = QIcon();
 
221
    repaint();
 
222
    
 
223
    return res;
 
224
}
 
225
 
 
226
void Button::setEditor(EditorFactory& factory)
 
227
{
 
228
    // remove old editor if existent
 
229
    delete m_editor;
 
230
    
 
231
    // create a new editor
 
232
    m_editor = factory.createEditor(topLevelCanvas());
 
233
    
 
234
    // update button
 
235
    m_size.setWidth(32 * 6);
 
236
    emit needsUpdate();
 
237
    repaint();
 
238
    m_editor->show();
 
239
    
 
240
    m_editor->setFocus();
 
241
}
 
242
 
 
243
void Button::updateEditor()
 
244
{
 
245
    if (m_editor) {
 
246
        const int SPACE = 9;
 
247
        m_editor->setGeometry(QRect(absolutePosition() + QPoint(textPos().x(), SPACE),
 
248
            QSize(m_size.width() - 10 - 10 - 32 - 10, m_size.height() - SPACE * 2)));
 
249
    }
 
250
}
 
251
 
 
252
void Button::removeEditor()
 
253
{
 
254
    m_editor->hide();
 
255
    m_editor->deleteLater();
 
256
    m_editor = 0;
 
257
    
 
258
    computeSize();
 
259
    repaint();
 
260
    emit needsUpdate();
 
261
}
 
262
 
 
263
// ------------
 
264
 
 
265
double ButtonAnimation::m_speed = 0.46;
 
266
 
 
267
ButtonAnimation::ButtonAnimation(Button* button, int brightness)
 
268
: m_button(button)
 
269
, m_brightness(brightness)
 
270
{
 
271
    m_last = -1;
 
272
}
 
273
 
 
274
void ButtonAnimation::start(int t)
 
275
{
 
276
    m_last = t;
 
277
}
 
278
 
 
279
bool ButtonAnimation::step(int t)
 
280
{
 
281
    if (m_last == -1) {
 
282
        return true;
 
283
    }
 
284
    
 
285
    int sign = (m_button->brightness() > m_brightness) ? -1 : 1;
 
286
    double delta = (t - m_last) * m_speed;
 
287
//     kDebug() << "button step t =" << t << "sign =" << sign <<  "delta =" << delta;
 
288
    m_last = t;
 
289
    if (fabs(m_button->brightness() - m_brightness) <= delta) {
 
290
        m_button->setBrightness(m_brightness);
 
291
        return true;
 
292
    }
 
293
    else {
 
294
        m_button->setBrightness(m_button->brightness() + sign * delta);
 
295
        return false;
 
296
    }
 
297
}
 
298
 
 
299
void ButtonAnimation::abort()
 
300
{
 
301
    m_last = -1;
 
302
}
 
303
 
 
304
void ButtonAnimation::setBrightness(int value)
 
305
{
 
306
    m_brightness = value;
 
307
}
 
308
 
 
309
ButtonAnimation::~ButtonAnimation()
 
310
{
 
311
}
 
312
 
 
313
 
 
314
#include "button.moc"
 
315