Qt: add support for the palette config option

Depends on CHANGE_COLOR compile-time option.
This commit is contained in:
Pasi Kallinen
2024-11-14 17:24:59 +02:00
parent c7591c0e08
commit 62971c6f09
5 changed files with 77 additions and 108 deletions

View File

@@ -2207,7 +2207,7 @@ curses: if user's terminal was set to 'application keypad mode' (DEC VTxxx
"ESC SPC G"), nethack wasn't recognizing number pad keys
curses: change petattr attributes, dropping support for curses-only ones
curses: swap the grey and no-color color initialization
curses: allow changing default colors with the 'palette' config option
curses+Qt: allow changing default colors with the 'palette' config option
(only if compiled with CHANGE_COLOR)
curses: if messages have been issued during start-up (for instance, warnings
about issues in run-time config file), prompt user to press <return>

View File

@@ -70,6 +70,8 @@ static struct key_macro_rec {
{ 0, 0U, (const char *) 0, (const char *) 0 }
};
static QPen *pen = (QPen *) 0;
NetHackQtBind::NetHackQtBind(int& argc, char** argv) :
#ifdef KDE
KApplication(argc,argv)
@@ -578,6 +580,45 @@ void NetHackQtBind::qt_raw_print_bold(const char *str)
qt_raw_print(str);
}
const QPen NetHackQtBind::nhcolor_to_pen(uint32_t c)
{
if (!pen) {
pen = new QPen[17];
pen[ 0] = QColor(64, 64, 64); // black
pen[ 1] = QColor(Qt::red);
pen[ 2] = QColor(0, 191, 0); // green
pen[ 3] = QColor(127, 127, 0); // brownish
pen[ 4] = QColor(Qt::blue);
pen[ 5] = QColor(Qt::magenta);
pen[ 6] = QColor(Qt::cyan);
pen[ 7] = QColor(Qt::gray);
// on tty, "light" variations are "bright" instead; here they're paler
pen[ 8] = QColor(Qt::white); // no color
pen[ 9] = QColor(255, 127, 0); // orange
pen[10] = QColor(127, 255, 127); // light green
pen[11] = QColor(Qt::yellow);
pen[12] = QColor(127, 127, 255); // light blue
pen[13] = QColor(255, 127, 255); // light magenta
pen[14] = QColor(127, 255, 255); // light cyan
pen[15] = QColor(Qt::white);
// ? out of range for 0..15
pen[16] = QColor(Qt::black);
}
#ifdef ENHANCED_SYMBOLS
if (c & 0x80000000) {
return QColor(
(c >> 16) & 0xFF,
(c >> 8) & 0xFF,
(c >> 0) & 0xFF);
} else
#endif
{
return pen[c];
}
}
int NetHackQtBind::qt_nhgetch()
{
if (main)
@@ -901,6 +942,28 @@ void NetHackQtBind::qt_delay_output()
#endif
}
#ifdef CHANGE_COLOR
void NetHackQtBind::qt_change_color(int color, long rgb, int reverse UNUSED)
{
int r, g, b;
r = (rgb >> 16) & 0xFF;
g = (rgb >> 8) & 0xFF;
b = rgb & 0xFF;
if (!pen) {
(void) NetHackQtBind::nhcolor_to_pen(0); /* init pen[] */
}
pen[color % 16] = QColor(r, g, b);
}
char *
NetHackQtBind::qt_get_color_string(void)
{
return (char *) 0;
}
#endif
void NetHackQtBind::qt_start_screen()
{
// Ignore.
@@ -1158,11 +1221,13 @@ struct window_procs Qt_procs = {
nethack_qt_::NetHackQtBind::qt_get_ext_cmd,
nethack_qt_::NetHackQtBind::qt_number_pad,
nethack_qt_::NetHackQtBind::qt_delay_output,
#ifdef CHANGE_COLOR /* only a Mac option currently */
donull,
donull,
#ifdef CHANGE_COLOR
nethack_qt_::NetHackQtBind::qt_change_color,
#ifdef MAC /* old OS 9, not OSX */
donull,
donull,
#endif
nethack_qt_::NetHackQtBind::qt_get_color_string,
#endif
/* other defs that really should go away (they're tty specific) */
nethack_qt_::NetHackQtBind::qt_start_screen,

View File

@@ -71,6 +71,9 @@ public:
const glyph_info *bkglyphinfo);
static void qt_raw_print(const char *str);
static void qt_raw_print_bold(const char *str);
static const QPen nhcolor_to_pen(uint32_t c);
static void qt_change_color(int color, long rgb, int reverse UNUSED);
static char *qt_get_color_string(void);
static int qt_nhgetch();
static int qt_nh_poskey(coordxy *x, coordxy *y, int *mod);
static void qt_nhbell();

View File

@@ -77,47 +77,6 @@ extern int qt_compact_mode;
namespace nethack_qt_ {
static const QPen nhcolor_to_pen(uint32_t c)
{
static QPen *pen = (QPen *) 0;
if (!pen) {
pen = new QPen[17];
//
// FIXME: these are duplicated in qt_menu.cpp
//
pen[ 0] = QColor(64, 64, 64); // black
pen[ 1] = QColor(Qt::red);
pen[ 2] = QColor(0, 191, 0); // green
pen[ 3] = QColor(127, 127, 0); // brownish
pen[ 4] = QColor(Qt::blue);
pen[ 5] = QColor(Qt::magenta);
pen[ 6] = QColor(Qt::cyan);
pen[ 7] = QColor(Qt::gray);
// on tty, "light" variations are "bright" instead; here they're paler
pen[ 8] = QColor(Qt::white); // no color
pen[ 9] = QColor(255, 127, 0); // orange
pen[10] = QColor(127, 255, 127); // light green
pen[11] = QColor(Qt::yellow);
pen[12] = QColor(127, 127, 255); // light blue
pen[13] = QColor(255, 127, 255); // light magenta
pen[14] = QColor(127, 255, 255); // light cyan
pen[15] = QColor(Qt::white);
// ? out of range for 0..15
pen[16] = QColor(Qt::black);
}
#ifdef ENHANCED_SYMBOLS
if (c & 0x80000000) {
return QColor(
(c >> 16) & 0xFF,
(c >> 8) & 0xFF,
(c >> 0) & 0xFF);
} else
#endif
{
return pen[c];
}
}
NetHackQtMapViewport::NetHackQtMapViewport(NetHackQtClickBuffer& click_sink) :
QWidget(NULL),
@@ -202,7 +161,7 @@ void NetHackQtMapViewport::paintEvent(QPaintEvent* event)
ch = cp437(ch);
}
color = Glyphcolor(i, j);
painter.setPen(nhcolor_to_pen(color));
painter.setPen(NetHackQtBind::nhcolor_to_pen(color));
if (!DrawWalls(painter, i * gW, j * gH, gW, gH, ch)) {
ushort utf16[3];
if (ch < 0x10000) {
@@ -226,7 +185,7 @@ void NetHackQtMapViewport::paintEvent(QPaintEvent* event)
}
framecolor = GlyphFramecolor(i, j);
if (framecolor != NO_COLOR) {
painter.setPen(nhcolor_to_pen(framecolor));
painter.setPen(NetHackQtBind::nhcolor_to_pen(framecolor));
painter.drawRect(i * qt_settings->glyphs().width(),
j * qt_settings->glyphs().height(),
qt_settings->glyphs().width() - 1,
@@ -255,7 +214,7 @@ void NetHackQtMapViewport::paintEvent(QPaintEvent* event)
}
framecolor = GlyphFramecolor(i, j);
if (framecolor != NO_COLOR) {
painter.setPen(nhcolor_to_pen(framecolor));
painter.setPen(NetHackQtBind::nhcolor_to_pen(framecolor));
painter.drawRect(i * qt_settings->glyphs().width(),
j * qt_settings->glyphs().height(),
qt_settings->glyphs().width() - 1,

View File

@@ -483,69 +483,11 @@ void NetHackQtMenuWindow::UpdateCountColumn(long newcount)
table->repaint();
}
struct qcolor {
QColor q;
const char *nm;
};
// these match the tty colors, or better versions of same;
// [0] is used for black, and [8] (the first white) corresponds to "no color"
static const struct qcolor colors[] = {
{ QColor(64, 64, 64), "64,64,64" }, // black
{ QColor(Qt::red), "red" },
{ QColor(0, 191, 0), "0,191,0" }, // green
{ QColor(127, 127, 0), "127,127,0" }, // brownish
{ QColor(Qt::blue), "blue" },
{ QColor(Qt::magenta), "magenta" },
{ QColor(Qt::cyan), "cyan" },
{ QColor(Qt::gray), "gray" },
// on tty, the "light" variations are "bright" instead; here they're paler
{ QColor(Qt::white), "white" }, // no-color, so not rendered
{ QColor(255, 127, 0), "255,127,0" }, // orange
{ QColor(127, 255, 127), "127,255,127" }, // light green
{ QColor(Qt::yellow), "yellow" },
{ QColor(127, 127, 255), "127,127,255" }, // light blue
{ QColor(255, 127, 255), "255,127,255" }, // light magenta
{ QColor(127, 255, 255), "127,255,255" }, // light cyan
{ QColor(Qt::white), "white" },
};
#if 0 /* available for debugging */
static const char *color_name(const QColor q)
{
for (int i = 0; i < SIZE(colors); ++i)
if (q == colors[i].q)
return colors[i].nm;
// these are all the enum GlobalColor values <qt5/QtCore/qnamespace.h>;
// black and white have been moved in front of color0 and color1 here
const char *nm = (q == Qt::black) ? "black"
: (q == Qt::white) ? "white"
: (q == Qt::color0) ? "color0" // doesn't duplicate white?
: (q == Qt::color1) ? "color1" // does duplicate black
: (q == Qt::darkGray) ? "darkGray"
: (q == Qt::gray) ? "gray"
: (q == Qt::lightGray) ? "lightGray"
: (q == Qt::red) ? "red"
: (q == Qt::green) ? "green"
: (q == Qt::blue) ? "blue"
: (q == Qt::cyan) ? "cyan"
: (q == Qt::magenta) ? "magenta"
: (q == Qt::yellow) ? "yellow"
: (q == Qt::darkRed) ? "darkRed"
: (q == Qt::darkGreen) ? "darkGreen"
: (q == Qt::darkBlue) ? "darkBlue"
: (q == Qt::darkCyan) ? "darkCyan"
: (q == Qt::darkMagenta) ? "darkMagenta"
: (q == Qt::darkYellow) ? "darkYellow"
: (q == Qt::transparent) ? "transparent"
: "other";
return nm;
}
#endif
void NetHackQtMenuWindow::SetTwiAttr(QTableWidgetItem *twi, int color, int attr)
{
if (color != NO_COLOR) {
twi->setForeground(colors[color].q);
const QPen qp = NetHackQtBind::nhcolor_to_pen(color);
twi->setForeground(qp.color());
}
if (attr != ATR_NONE) {