1 /*
2     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "welcomescreen.h"
8 
9 #include "knavalbattle_debug.h"
10 #include <QIcon>
11 
12 #include "button.h"
13 #include "animator.h"
14 
WelcomeScreen(const QFont & font)15 WelcomeScreen::WelcomeScreen(const QFont& font)
16 : QObject()
17 , QGraphicsRectItem()
18 , m_font(font)
19 , m_clicked(nullptr)
20 , m_hover(nullptr)
21 , m_active(true)
22 {
23     QBrush brush(QColor(0, 0, 0, 80));
24     setBrush(brush);
25 }
26 
resize(const QSizeF & size)27 void WelcomeScreen::resize(const QSizeF& size)
28 {
29     m_size = size;
30 
31     // background
32     QRectF rect(QPoint(0, 0), m_size);
33     setRect(rect);
34 
35     refresh();
36 }
37 
refresh()38 void WelcomeScreen::refresh()
39 {
40     // find dimensions
41     int max_x = 0;
42     int max_y = 0;
43     for (Buttons::const_iterator i = m_buttons.constBegin();
44          i != m_buttons.constEnd();
45          ++i) {
46         if (i.key().x > max_x) {
47             max_x = i.key().x;
48         }
49         if (i.key().y > max_y) {
50             max_y = i.key().y;
51         }
52     }
53     max_x++;
54     max_y++;
55 
56     // place buttons
57     QSizeF size = m_size;
58     size.rwidth() /= max_x;
59     size.rheight() /= max_y;
60 
61     for (Buttons::const_iterator i = m_buttons.constBegin();
62          i != m_buttons.constEnd();
63          ++i) {
64         QPoint pos(size.width() * i.key().x,
65                    size.height() * i.key().y);
66         QPoint delta((size.width() - i.value()->size().width()) / 2,
67                      (size.height() - i.value()->size().height()) / 2);
68         i.value()->setPos(pos + delta);
69         i.value()->update();
70     }
71 }
72 
addButton(int x,int y,const QIcon & icon,const QString & text)73 Button* WelcomeScreen::addButton(int x, int y, const QIcon& icon, const QString& text)
74 {
75     if (m_buttons.contains(Coord(x, y))) {
76         return m_buttons[Coord(x, y)];
77     }
78     else {
79         Button* button = new Button(this, icon, m_font, text);
80         if (!m_buttons.isEmpty()) {
81             Button* other = *m_buttons.begin();
82             if (other->size().width() >= button->size().width()) {
83                 button->setWidth(other->size().width());
84             }
85             else {
86                 for (Buttons::const_iterator i = m_buttons.constBegin();
87                      i != m_buttons.constEnd(); ++i) {
88                     (*i)->setWidth(button->size().width());
89                 }
90             }
91         }
92         m_buttons.insert(Coord(x, y), button);
93         refresh();
94         connect(button, &Button::needsUpdate, this, &WelcomeScreen::refresh);
95 
96         //qCDebug(KNAVALBATTLE_LOG) << "added button" << button;
97 
98         return button;
99     }
100 }
101 
removeButton(int x,int y)102 void WelcomeScreen::removeButton(int x, int y)
103 {
104     Button* button = m_buttons.take(Coord(x, y));
105     delete button;
106     refresh();
107 }
108 
moveButton(int x1,int y1,int x2,int y2)109 void WelcomeScreen::moveButton(int x1, int y1, int x2, int y2)
110 {
111     Coord from(x1, y1);
112     Coord to(x2, y2);
113 
114     if (m_buttons.contains(from) && !m_buttons.contains(to)) {
115         Button* button = m_buttons.value(from);
116         m_buttons.insert(to, button);
117         m_buttons.remove(from);
118         refresh();
119     }
120 }
121 
clearButtons()122 void WelcomeScreen::clearButtons()
123 {
124     m_clicked = nullptr;
125     m_hover = nullptr;
126     for (Buttons::const_iterator i = m_buttons.constBegin();
127          i != m_buttons.constEnd();
128          ++i) {
129         delete *i;
130     }
131     m_buttons.clear();
132 
133 }
134 
onMouseMove(Button * button)135 void WelcomeScreen::onMouseMove(Button *button)
136 {
137     if (m_hover && m_hover != button) {
138         m_hover->onMouseLeave();
139     }
140 
141     m_hover = button;
142     if (!m_clicked || button == m_clicked) {
143         button->onMouseMove();
144     }
145 }
146 
onMousePress(Button * button)147 void WelcomeScreen::onMousePress(Button *button)
148 {
149     qCDebug(KNAVALBATTLE_LOG) << "on mouse press";
150 
151     button->onMousePress();
152     m_clicked = button;
153 }
154 
onMouseRelease(Button * button)155 void WelcomeScreen::onMouseRelease(Button *button)
156 {
157     if (m_clicked) {
158         m_clicked->onMouseRelease();
159     }
160 
161     if (m_clicked && m_clicked == button) {
162         // actual click event
163         if (m_clicked->onClicked()) {
164             Q_EMIT clicked(button);
165         }
166     }
167 
168     m_clicked = nullptr;
169 }
170 
fadeOut()171 void WelcomeScreen::fadeOut()
172 {
173     Animation* hideAnimation = new FadeAnimation(this, 1, 0, 500);
174     connect(hideAnimation, &Animation::done, this, &WelcomeScreen::hide);
175     Animator::instance()->add(hideAnimation);
176 }
177 
show()178 void WelcomeScreen::show()
179 {
180     m_active = true;
181     setOpacity(1);
182     QGraphicsRectItem::show();
183     Q_EMIT shown();
184 }
185 
hide()186 void WelcomeScreen::hide()
187 {
188     m_active = false;
189     QGraphicsRectItem::hide();
190     clearButtons();
191     Q_EMIT hidden();
192 }
193 
onMouseLeave()194 void WelcomeScreen::onMouseLeave()
195 {
196     if (m_hover) {
197         m_hover->onMouseLeave();
198     }
199 }
200 
201 
202 
203 
204 
205