File indexing completed on 2024-04-21 04:02:06

0001 /***************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                *
0003 *   SPDX-FileCopyrightText: 2010 Mauricio Piacentini <mauricio@tabuleiro.com>       *
0004 *                      Zhongjie Cai <squall.leonhart.cai@gmail.com>       *
0005 *                                                                         *
0006 *   SPDX-License-Identifier: GPL-2.0-or-later
0007 ***************************************************************************/
0008 #include "KBlocksGraphics.h"
0009 #include "kblocks_graphics_debug.h"
0010 
0011 #include <KGameTheme>
0012 
0013 #include <QPixmapCache>
0014 
0015 KBlocksGraphics::KBlocksGraphics(const KGameTheme *theme)
0016 {
0017     m_renderer = new QSvgRenderer(theme->graphicsPath());
0018     readThemeValues(theme);
0019 }
0020 
0021 KBlocksGraphics::~KBlocksGraphics()
0022 {
0023     delete m_renderer;
0024 }
0025 
0026 bool KBlocksGraphics::loadTheme(const KGameTheme *theme)
0027 {
0028     if (!m_renderer->load(theme->graphicsPath())) {
0029         qCWarning(KBGraphics) << "Error loading SVG theme"
0030                                    << theme->graphicsPath();
0031         return false;
0032     }
0033     //clear the cache or pixmaps from the old theme will be returned
0034     //QPixmapCache::clear();
0035     readThemeValues(theme);
0036 
0037     return true;
0038 }
0039 
0040 void KBlocksGraphics::readThemeValues(const KGameTheme *theme)
0041 {
0042     //Extract values from SVG elements
0043     QRectF bounds;
0044     bounds = m_renderer->boundsOnElement(QStringLiteral("BLOCK_SIZE"));
0045     m_Block_Size = bounds.width();
0046     bounds = m_renderer->boundsOnElement(QStringLiteral("VIEW"));
0047     m_View_Size_Width = bounds.width();
0048     m_View_Size_Height = bounds.height();
0049     bounds = m_renderer->boundsOnElement(QStringLiteral("PLAY_AREA"));
0050     m_PlayArea_OffsetPoint_X = bounds.x();
0051     m_PlayArea_OffsetPoint_Y = bounds.y();
0052     m_PlayArea_NumberOfBlocks_X = bounds.width() / (double)m_Block_Size;
0053     m_PlayArea_NumberOfBlocks_Y = bounds.height() / (double)m_Block_Size;
0054     bounds = m_renderer->boundsOnElement(QStringLiteral("NEXTPIECE_AREA"));
0055     m_PreviewArea_CenterPoint_X = bounds.center().x();
0056     m_PreviewArea_CenterPoint_Y = bounds.center().y();
0057 
0058     const QString backgroundLocation = theme->customData(QStringLiteral("BackgroundLocation"), QStringLiteral("Stretch"));
0059     if(backgroundLocation == QStringLiteral("Stretch")) {
0060         m_BackgroundLocation = BackgroundLocation::Stretch;
0061     } else if(backgroundLocation == QStringLiteral("TopLeft")) {
0062         m_BackgroundLocation = BackgroundLocation::TopLeft;
0063     } else if(backgroundLocation == QStringLiteral("Center")) {
0064         m_BackgroundLocation = BackgroundLocation::Center;
0065     }
0066 }
0067