src/platform/qt/TileView.cpp (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "TileView.h"
7
8#include "GBAApp.h"
9
10#include <QFontDatabase>
11#include <QTimer>
12
13using namespace QGBA;
14
15TileView::TileView(GameController* controller, QWidget* parent)
16 : AssetView(controller, parent)
17 , m_controller(controller)
18 , m_tileStatus{}
19 , m_paletteId(0)
20{
21 m_ui.setupUi(this);
22 m_ui.tile->setController(controller);
23
24 connect(m_ui.tiles, SIGNAL(indexPressed(int)), m_ui.tile, SLOT(selectIndex(int)));
25 connect(m_ui.paletteId, SIGNAL(valueChanged(int)), this, SLOT(updatePalette(int)));
26
27 int max = 1024;
28 int boundary = 1024;
29 switch (m_controller->platform()) {
30#ifdef M_CORE_GBA
31 case PLATFORM_GBA:
32 max = 3072;
33 boundary = 2048;
34 break;
35#endif
36#ifdef M_CORE_GB
37 case PLATFORM_GB:
38 max = 1024;
39 boundary = 1024;
40 m_ui.palette256->setEnabled(false);
41 break;
42#endif
43 default:
44 return;
45 }
46 m_ui.tile->setPaletteSet(0, boundary, max);
47
48 connect(m_ui.palette256, &QAbstractButton::toggled, [this](bool selected) {
49 if (selected) {
50 m_ui.paletteId->setValue(0);
51 }
52 int max = 1024;
53 int boundary = 1024;
54 switch (m_controller->platform()) {
55#ifdef M_CORE_GBA
56 case PLATFORM_GBA:
57 max = 3072 >> selected;
58 boundary = 2048 >> selected;
59 break;
60#endif
61#ifdef M_CORE_GB
62 case PLATFORM_GB:
63 return;
64#endif
65 default:
66 break;
67 }
68 m_ui.tile->setPaletteSet(selected, boundary, max);
69 updateTiles(true);
70 });
71 connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
72 updateTiles(true);
73 });
74}
75
76#ifdef M_CORE_GBA
77void TileView::updateTilesGBA(bool force) {
78 if (m_ui.palette256->isChecked()) {
79 m_ui.tiles->setTileCount(1536);
80 mTileCacheSetPalette(m_tileCache.get(), 1);
81 for (int i = 0; i < 1024; ++i) {
82 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * i], i, 0);
83 if (data) {
84 m_ui.tiles->setTile(i, data);
85 } else if (force) {
86 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, 0));
87 }
88 }
89 for (int i = 1024; i < 1536; ++i) {
90 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * i], i, 1);
91 if (data) {
92 m_ui.tiles->setTile(i, data);
93 } else if (force) {
94 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, 1));
95 }
96 }
97 } else {
98 m_ui.tiles->setTileCount(3072);
99 mTileCacheSetPalette(m_tileCache.get(), 0);
100 for (int i = 0; i < 2048; ++i) {
101 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * i], i, m_paletteId);
102 if (data) {
103 m_ui.tiles->setTile(i, data);
104 } else if (force) {
105 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, m_paletteId));
106 }
107 }
108 for (int i = 2048; i < 3072; ++i) {
109 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[32 * i], i, m_paletteId + 16);
110 if (data) {
111 m_ui.tiles->setTile(i, data);
112 } else if (force) {
113 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, m_paletteId + 16));
114 }
115 }
116 }
117}
118#endif
119
120#ifdef M_CORE_GB
121void TileView::updateTilesGB(bool force) {
122 m_ui.tiles->setTileCount(1024);
123 mTileCacheSetPalette(m_tileCache.get(), 0);
124 for (int i = 0; i < 1024; ++i) {
125 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache.get(), &m_tileStatus[16 * i], i, m_paletteId);
126 if (data) {
127 m_ui.tiles->setTile(i, data);
128 } else if (force) {
129 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache.get(), i, m_paletteId));
130 }
131 }
132}
133#endif
134
135void TileView::updatePalette(int palette) {
136 m_paletteId = palette;
137 m_ui.tile->setPalette(palette);
138 updateTiles(true);
139}