File indexing completed on 2024-09-15 06:37:37
0001 /* 0002 SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "welcomescreen.h" 0008 0009 #include "knavalbattle_debug.h" 0010 #include <QIcon> 0011 0012 #include "button.h" 0013 #include "animator.h" 0014 0015 WelcomeScreen::WelcomeScreen(const QFont& font) 0016 : QObject() 0017 , QGraphicsRectItem() 0018 , m_font(font) 0019 , m_clicked(nullptr) 0020 , m_hover(nullptr) 0021 , m_active(true) 0022 { 0023 QBrush brush(QColor(0, 0, 0, 80)); 0024 setBrush(brush); 0025 } 0026 0027 void WelcomeScreen::resize(const QSizeF& size) 0028 { 0029 m_size = size; 0030 0031 // background 0032 QRectF rect(QPoint(0, 0), m_size); 0033 setRect(rect); 0034 0035 refresh(); 0036 } 0037 0038 void WelcomeScreen::refresh() 0039 { 0040 // find dimensions 0041 int max_x = 0; 0042 int max_y = 0; 0043 for (Buttons::const_iterator i = m_buttons.constBegin(); 0044 i != m_buttons.constEnd(); 0045 ++i) { 0046 if (i.key().x > max_x) { 0047 max_x = i.key().x; 0048 } 0049 if (i.key().y > max_y) { 0050 max_y = i.key().y; 0051 } 0052 } 0053 max_x++; 0054 max_y++; 0055 0056 // place buttons 0057 QSizeF size = m_size; 0058 size.rwidth() /= max_x; 0059 size.rheight() /= max_y; 0060 0061 for (Buttons::const_iterator i = m_buttons.constBegin(); 0062 i != m_buttons.constEnd(); 0063 ++i) { 0064 QPoint pos(size.width() * i.key().x, 0065 size.height() * i.key().y); 0066 QPoint delta((size.width() - i.value()->size().width()) / 2, 0067 (size.height() - i.value()->size().height()) / 2); 0068 i.value()->setPos(pos + delta); 0069 i.value()->update(); 0070 } 0071 } 0072 0073 Button* WelcomeScreen::addButton(int x, int y, const QIcon& icon, const QString& text) 0074 { 0075 if (m_buttons.contains(Coord(x, y))) { 0076 return m_buttons[Coord(x, y)]; 0077 } 0078 else { 0079 Button* button = new Button(this, icon, m_font, text); 0080 if (!m_buttons.isEmpty()) { 0081 Button* other = *m_buttons.begin(); 0082 if (other->size().width() >= button->size().width()) { 0083 button->setWidth(other->size().width()); 0084 } 0085 else { 0086 for (Buttons::const_iterator i = m_buttons.constBegin(); 0087 i != m_buttons.constEnd(); ++i) { 0088 (*i)->setWidth(button->size().width()); 0089 } 0090 } 0091 } 0092 m_buttons.insert(Coord(x, y), button); 0093 refresh(); 0094 connect(button, &Button::needsUpdate, this, &WelcomeScreen::refresh); 0095 0096 //qCDebug(KNAVALBATTLE_LOG) << "added button" << button; 0097 0098 return button; 0099 } 0100 } 0101 0102 void WelcomeScreen::removeButton(int x, int y) 0103 { 0104 Button* button = m_buttons.take(Coord(x, y)); 0105 delete button; 0106 refresh(); 0107 } 0108 0109 void WelcomeScreen::moveButton(int x1, int y1, int x2, int y2) 0110 { 0111 Coord from(x1, y1); 0112 Coord to(x2, y2); 0113 0114 if (m_buttons.contains(from) && !m_buttons.contains(to)) { 0115 Button* button = m_buttons.value(from); 0116 m_buttons.insert(to, button); 0117 m_buttons.remove(from); 0118 refresh(); 0119 } 0120 } 0121 0122 void WelcomeScreen::clearButtons() 0123 { 0124 m_clicked = nullptr; 0125 m_hover = nullptr; 0126 for (Buttons::const_iterator i = m_buttons.constBegin(); 0127 i != m_buttons.constEnd(); 0128 ++i) { 0129 delete *i; 0130 } 0131 m_buttons.clear(); 0132 0133 } 0134 0135 void WelcomeScreen::onMouseMove(Button *button) 0136 { 0137 if (m_hover && m_hover != button) { 0138 m_hover->onMouseLeave(); 0139 } 0140 0141 m_hover = button; 0142 if (!m_clicked || button == m_clicked) { 0143 button->onMouseMove(); 0144 } 0145 } 0146 0147 void WelcomeScreen::onMousePress(Button *button) 0148 { 0149 qCDebug(KNAVALBATTLE_LOG) << "on mouse press"; 0150 0151 button->onMousePress(); 0152 m_clicked = button; 0153 } 0154 0155 void WelcomeScreen::onMouseRelease(Button *button) 0156 { 0157 if (m_clicked) { 0158 m_clicked->onMouseRelease(); 0159 } 0160 0161 if (m_clicked && m_clicked == button) { 0162 // actual click event 0163 if (m_clicked->onClicked()) { 0164 Q_EMIT clicked(button); 0165 } 0166 } 0167 0168 m_clicked = nullptr; 0169 } 0170 0171 void WelcomeScreen::fadeOut() 0172 { 0173 Animation* hideAnimation = new FadeAnimation(this, 1, 0, 500); 0174 connect(hideAnimation, &Animation::done, this, &WelcomeScreen::hide); 0175 Animator::instance()->add(hideAnimation); 0176 } 0177 0178 void WelcomeScreen::show() 0179 { 0180 m_active = true; 0181 setOpacity(1); 0182 QGraphicsRectItem::show(); 0183 Q_EMIT shown(); 0184 } 0185 0186 void WelcomeScreen::hide() 0187 { 0188 m_active = false; 0189 QGraphicsRectItem::hide(); 0190 clearButtons(); 0191 Q_EMIT hidden(); 0192 } 0193 0194 void WelcomeScreen::onMouseLeave() 0195 { 0196 if (m_hover) { 0197 m_hover->onMouseLeave(); 0198 } 0199 } 0200 0201 #include "moc_welcomescreen.cpp"