Skip to content

Commit

Permalink
Platform/Qt: cleanup palette viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Feb 18, 2024
1 parent 378f156 commit df075ee
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 123 deletions.
2 changes: 2 additions & 0 deletions src/platform/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(SOURCES
src/widget/debugger/ppu/sprite_viewer.cpp
src/widget/debugger/ppu/sprite_viewer_window.cpp
src/widget/debugger/ppu/color_grid.cpp
src/widget/debugger/ppu/palette_viewer.cpp
src/widget/debugger/ppu/palette_viewer_window.cpp
src/widget/debugger/ppu/tile_viewer.cpp
src/widget/debugger/ppu/tile_viewer_window.cpp
Expand All @@ -22,6 +23,7 @@ set(HEADERS
src/widget/debugger/ppu/background_viewer_window.hpp
src/widget/debugger/ppu/background_viewer.hpp
src/widget/debugger/ppu/color_grid.hpp
src/widget/debugger/ppu/palette_viewer.hpp
src/widget/debugger/ppu/palette_viewer_window.hpp
src/widget/debugger/ppu/sprite_viewer.hpp
src/widget/debugger/ppu/sprite_viewer_window.hpp
Expand Down
113 changes: 113 additions & 0 deletions src/platform/qt/src/widget/debugger/ppu/palette_viewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (C) 2024 fleroviux
*
* Licensed under GPLv3 or any later version.
* Refer to the included LICENSE file.
*/

#include <fmt/format.h>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QVBoxLayout>

#include "widget/debugger/utility.hpp"
#include "palette_viewer.hpp"

PaletteViewer::PaletteViewer(nba::CoreBase* core, QWidget* parent) : QWidget(parent) {
QVBoxLayout* vbox = new QVBoxLayout{};

vbox->addWidget(new QLabel{tr("Select a color for detailed information:")});
vbox->addLayout(CreatePaletteGrids());
vbox->addLayout(CreateColorInfoArea());
setLayout(vbox);

m_pram = (u16*)core->GetPRAM();
}

QLayout* PaletteViewer::CreatePaletteGrids() {
QHBoxLayout* hbox = new QHBoxLayout{};

for(int grid = 0; grid < 2; grid++) {
QVBoxLayout* vbox = new QVBoxLayout{};

QGroupBox* group_box = new QGroupBox{};
group_box->setTitle(grid == 0 ? tr("Background") : tr("Sprite"));
group_box->setLayout(vbox);

m_palette_color_grids[grid] = new ColorGrid{16, 16};

connect(m_palette_color_grids[grid], &ColorGrid::selected, [=](int x, int y) {
m_palette_color_grids[grid]->SetHighlightedPosition(x, y);
m_palette_color_grids[grid ^ 1]->ClearHighlight();
ShowColorInfo(grid << 8 | y << 4 | x);
});

vbox->addWidget(m_palette_color_grids[grid]);
hbox->addWidget(group_box);
}

return hbox;
}

QLayout* PaletteViewer::CreateColorInfoArea() {
QHBoxLayout* hbox = new QHBoxLayout{};

m_label_color_address = CreateMonospaceLabel();
m_label_color_r_component = CreateMonospaceLabel();
m_label_color_g_component = CreateMonospaceLabel();
m_label_color_b_component = CreateMonospaceLabel();
m_label_color_value = CreateMonospaceLabel();

QGridLayout* grid = new QGridLayout{};
grid->addWidget(new QLabel{tr("Address:")}, 0, 0);
grid->addWidget(m_label_color_address, 0, 1);
grid->addWidget(new QLabel{tr("R:")}, 1, 0);
grid->addWidget(m_label_color_r_component, 1, 1);
grid->addWidget(new QLabel{tr("G:")}, 2, 0);
grid->addWidget(m_label_color_g_component, 2, 1);
grid->addWidget(new QLabel{tr("B:")}, 3, 0);
grid->addWidget(m_label_color_b_component, 3, 1);
grid->addWidget(new QLabel{tr("Value:")}, 4, 0);
grid->addWidget(m_label_color_value, 4, 1);
grid->setColumnStretch(1, 1);

m_box_color_preview = new QWidget{};
m_box_color_preview->setStyleSheet("background-color: black;");
m_box_color_preview->setFixedSize(64, 64);

hbox->addLayout(grid);
hbox->addWidget(m_box_color_preview);
return hbox;
}

void PaletteViewer::Update() {
if(!isVisible()) {
return;
}

m_palette_color_grids[0]->Draw(&m_pram[0], 16);
m_palette_color_grids[1]->Draw(&m_pram[256], 16);
}

void PaletteViewer::ShowColorInfo(int color_index) {
const u32 address = 0x05000000 + (color_index << 1);
const int x = color_index & 15;
const int y = (color_index >> 4) & 15;
const u16 color = m_pram[color_index];

const int r = (color << 1) & 62;
const int g = ((color >> 4) & 62) | (color >> 15);
const int b = (color >> 9) & 62;

const u32 rgb = Rgb565ToArgb8888(color);

m_label_color_address->setText(QString::fromStdString(fmt::format("0x{:08X} ({}, {})", address, x, y)));
m_label_color_r_component->setText(QStringLiteral("%1").arg(r));
m_label_color_g_component->setText(QStringLiteral("%1").arg(g));
m_label_color_b_component->setText(QStringLiteral("%1").arg(b));
m_label_color_value->setText(QString::fromStdString(fmt::format("0x{:04X}", color)));

m_box_color_preview->setStyleSheet(QString::fromStdString(fmt::format("background-color: #{:06X};", rgb)));
}
38 changes: 38 additions & 0 deletions src/platform/qt/src/widget/debugger/ppu/palette_viewer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2024 fleroviux
*
* Licensed under GPLv3 or any later version.
* Refer to the included LICENSE file.
*/

#pragma once

#include <nba/core.hpp>
#include <QDialog>
#include <QLabel>

#include "color_grid.hpp"

class PaletteViewer : public QWidget {
public:
PaletteViewer(nba::CoreBase* core, QWidget* parent = nullptr);

void Update();

private:
QLayout* CreatePaletteGrids();
QLayout* CreateColorInfoArea();

void ShowColorInfo(int color_index);

u16* m_pram;
ColorGrid* m_palette_color_grids[2];
QLabel* m_label_color_address;
QLabel* m_label_color_r_component;
QLabel* m_label_color_g_component;
QLabel* m_label_color_b_component;
QLabel* m_label_color_value;
QWidget* m_box_color_preview;

Q_OBJECT
};
108 changes: 4 additions & 104 deletions src/platform/qt/src/widget/debugger/ppu/palette_viewer_window.cpp
Original file line number Diff line number Diff line change
@@ -1,127 +1,27 @@
/*
* Copyright (C) 2023 fleroviux
* Copyright (C) 2024 fleroviux
*
* Licensed under GPLv3 or any later version.
* Refer to the included LICENSE file.
*/

#include <fmt/format.h>
#include <QFontDatabase>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QVBoxLayout>

#include "palette_viewer_window.hpp"

PaletteViewerWindow::PaletteViewerWindow(nba::CoreBase* core, QWidget* parent) : QDialog(parent) {
const auto vbox = new QVBoxLayout{};

m_palette_viewer = new PaletteViewer{core};
vbox->addWidget(m_palette_viewer);
vbox->setSizeConstraint(QLayout::SetFixedSize);

const auto palettes_hbox = new QHBoxLayout{};

for(int box = 0; box < 2; box++) {
const auto group_box = new QGroupBox{};
const auto group_box_layout = new QVBoxLayout{};

m_palette_color_grids[box] = new ColorGrid{16, 16};

connect(m_palette_color_grids[box], &ColorGrid::selected, [=](int x, int y) {
m_palette_color_grids[box]->SetHighlightedPosition(x, y);
m_palette_color_grids[box ^ 1]->ClearHighlight();
ShowColorInformation(box << 8 | y << 4 | x);
});

group_box->setTitle(box == 0 ? tr("Background") : tr("Sprite"));
group_box_layout->addWidget(m_palette_color_grids[box]);
group_box->setLayout(group_box_layout);

palettes_hbox->addWidget(group_box);
}

const auto monospace_font = QFontDatabase::systemFont(QFontDatabase::FixedFont);

address_label = new QLabel{};
r_component_label = new QLabel{};
g_component_label = new QLabel{};
b_component_label = new QLabel{};
value_label = new QLabel{};

address_label->setFont(monospace_font);
r_component_label->setFont(monospace_font);
g_component_label->setFont(monospace_font);
b_component_label->setFont(monospace_font);
value_label->setFont(monospace_font);

address_label->setTextInteractionFlags(Qt::TextSelectableByMouse);
r_component_label->setTextInteractionFlags(Qt::TextSelectableByMouse);
g_component_label->setTextInteractionFlags(Qt::TextSelectableByMouse);
b_component_label->setTextInteractionFlags(Qt::TextSelectableByMouse);
value_label->setTextInteractionFlags(Qt::TextSelectableByMouse);

const auto info_hbox = new QHBoxLayout{};

const auto info_grid = new QGridLayout{};
info_grid->addWidget(new QLabel{tr("Address:")}, 0, 0);
info_grid->addWidget(address_label, 0, 1);
info_grid->addWidget(new QLabel{tr("R:")}, 1, 0);
info_grid->addWidget(r_component_label, 1, 1);
info_grid->addWidget(new QLabel{tr("G:")}, 2, 0);
info_grid->addWidget(g_component_label, 2, 1);
info_grid->addWidget(new QLabel{tr("B:")}, 3, 0);
info_grid->addWidget(b_component_label, 3, 1);
info_grid->addWidget(new QLabel{tr("Value:")}, 4, 0);
info_grid->addWidget(value_label, 4, 1);
info_grid->setColumnStretch(1, 1);

info_color = new QWidget{};
info_color->setStyleSheet("background-color: black;");
info_color->setFixedSize(64, 64);

info_hbox->addLayout(info_grid);
info_hbox->addWidget(info_color);

vbox->addWidget(new QLabel{tr("Select a color for detailed information:")});
vbox->addLayout(palettes_hbox);
vbox->addLayout(info_hbox);

setLayout(vbox);
setWindowTitle(tr("Palette Viewer"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

pram = (u16*)core->GetPRAM();
}

void PaletteViewerWindow::Update() {
if(!isVisible()) {
return;
}

m_palette_color_grids[0]->Draw(&pram[0], 16);
m_palette_color_grids[1]->Draw(&pram[256], 16);
m_palette_viewer->Update();
}

void PaletteViewerWindow::ShowColorInformation(int color_index) {
const u32 address = 0x05000000 + (color_index << 1);
const int x = color_index & 15;
const int y = (color_index >> 4) & 15;
const u16 color = pram[color_index];

const int r = (color << 1) & 62;
const int g = ((color >> 4) & 62) | (color >> 15);
const int b = (color >> 9) & 62;

const u32 rgb = (r << 2 | r >> 3) << 16 |
(g << 2 | g >> 4) << 8 |
(b << 2 | b >> 3);

address_label->setText(QString::fromStdString(fmt::format("0x{:08X} ({}, {})", address, x, y)));
r_component_label->setText(QStringLiteral("%1").arg(r));
g_component_label->setText(QStringLiteral("%1").arg(g));
b_component_label->setText(QStringLiteral("%1").arg(b));
value_label->setText(QString::fromStdString(fmt::format("0x{:04X}", color)));

info_color->setStyleSheet(QString::fromStdString(fmt::format("background-color: #{:06X};", rgb)));
}
27 changes: 9 additions & 18 deletions src/platform/qt/src/widget/debugger/ppu/palette_viewer_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,18 @@

#include <nba/core.hpp>
#include <QDialog>
#include <QLabel>

#include "color_grid.hpp"
#include "palette_viewer.hpp"

struct PaletteViewerWindow : QDialog {
PaletteViewerWindow(nba::CoreBase* core, QWidget* parent = nullptr);
class PaletteViewerWindow : public QDialog {
public:
PaletteViewerWindow(nba::CoreBase* core, QWidget* parent = nullptr);

public slots:
void Update();
public slots:
void Update();

private:
void ShowColorInformation(int color_index);
private:
PaletteViewer* m_palette_viewer;

u16* pram;
ColorGrid* m_palette_color_grids[2];
QLabel* address_label;
QLabel* r_component_label;
QLabel* g_component_label;
QLabel* b_component_label;
QLabel* value_label;
QWidget* info_color;

Q_OBJECT
Q_OBJECT
};
2 changes: 1 addition & 1 deletion src/platform/qt/src/widget/debugger/ppu/tile_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ QWidget* TileViewer::CreatePaletteInput() {
m_spin_palette_index = new QSpinBox{};
m_spin_palette_index->setMinimum(0);
m_spin_palette_index->setMaximum(15);
return m_spin_magnification;
return m_spin_palette_index;
}

QWidget* TileViewer::CreateTileBaseGroupBox() {
Expand Down

0 comments on commit df075ee

Please sign in to comment.