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 "CoreController.h"
9#include "GBAApp.h"
10
11#include <QFontDatabase>
12#include <QTimer>
13
14#ifdef M_CORE_GB
15#include <mgba/internal/gb/gb.h>
16#endif
17
18using namespace QGBA;
19
20TileView::TileView(std::shared_ptr<CoreController> controller, QWidget* parent)
21 : AssetView(controller, parent)
22 , m_controller(controller)
23{
24 m_ui.setupUi(this);
25 m_ui.tile->setController(controller);
26
27 connect(m_ui.tiles, &TilePainter::indexPressed, m_ui.tile, &AssetTile::selectIndex);
28 connect(m_ui.paletteId, &QAbstractSlider::valueChanged, this, &TileView::updatePalette);
29
30 int max = 1024;
31 int boundary = 1024;
32 switch (m_controller->platform()) {
33#ifdef M_CORE_GBA
34 case PLATFORM_GBA:
35 max = 3072;
36 boundary = 2048;
37 break;
38#endif
39#ifdef M_CORE_GB
40 case PLATFORM_GB:
41 max = 1024;
42 boundary = 512;
43 m_ui.palette256->setEnabled(false);
44 break;
45#endif
46 default:
47 return;
48 }
49 m_ui.tile->setPaletteSet(0, boundary, max);
50
51 connect(m_ui.palette256, &QAbstractButton::toggled, [this](bool selected) {
52 if (selected) {
53 m_ui.paletteId->setValue(0);
54 }
55 int max = 1024;
56 int boundary = 1024;
57 switch (m_controller->platform()) {
58#ifdef M_CORE_GBA
59 case PLATFORM_GBA:
60 max = 3072 >> selected;
61 boundary = 2048 >> selected;
62 break;
63#endif
64#ifdef M_CORE_GB
65 case PLATFORM_GB:
66 return;
67#endif
68 default:
69 break;
70 }
71 m_ui.tile->setPaletteSet(selected, boundary, max);
72 updateTiles(true);
73 });
74 connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
75 updateTiles(true);
76 });
77}
78
79#ifdef M_CORE_GBA
80void TileView::updateTilesGBA(bool force) {
81 if (m_ui.palette256->isChecked()) {
82 m_ui.tiles->setTileCount(1536);
83 mTileCacheSetPalette(m_tileCache, 1);
84 for (int i = 0; i < 1024; ++i) {
85 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache, &m_tileStatus[32 * i], i, 0);
86 if (data) {
87 m_ui.tiles->setTile(i, data);
88 } else if (force) {
89 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache, i, 0));
90 }
91 }
92 for (int i = 1024; i < 1536; ++i) {
93 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache, &m_tileStatus[32 * i], i, 1);
94 if (data) {
95 m_ui.tiles->setTile(i, data);
96 } else if (force) {
97 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache, i, 1));
98 }
99 }
100 } else {
101 m_ui.tiles->setTileCount(3072);
102 mTileCacheSetPalette(m_tileCache, 0);
103 for (int i = 0; i < 2048; ++i) {
104 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache, &m_tileStatus[32 * i], i, m_paletteId);
105 if (data) {
106 m_ui.tiles->setTile(i, data);
107 } else if (force) {
108 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache, i, m_paletteId));
109 }
110 }
111 for (int i = 2048; i < 3072; ++i) {
112 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache, &m_tileStatus[32 * i], i, m_paletteId + 16);
113 if (data) {
114 m_ui.tiles->setTile(i, data);
115 } else if (force) {
116 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache, i, m_paletteId + 16));
117 }
118 }
119 }
120}
121#endif
122
123#ifdef M_CORE_GB
124void TileView::updateTilesGB(bool force) {
125 const GB* gb = static_cast<const GB*>(m_controller->thread()->core->board);
126 int count = gb->model >= GB_MODEL_CGB ? 1024 : 512;
127 m_ui.tiles->setTileCount(count);
128 mTileCacheSetPalette(m_tileCache, 0);
129 for (int i = 0; i < count; ++i) {
130 const uint16_t* data = mTileCacheGetTileIfDirty(m_tileCache, &m_tileStatus[16 * i], i, m_paletteId);
131 if (data) {
132 m_ui.tiles->setTile(i, data);
133 } else if (force) {
134 m_ui.tiles->setTile(i, mTileCacheGetTile(m_tileCache, i, m_paletteId));
135 }
136 }
137}
138#endif
139
140void TileView::updatePalette(int palette) {
141 m_paletteId = palette;
142 m_ui.tile->setPalette(palette);
143 updateTiles(true);
144}