Use a real indexed palette

Instead of copying attributes from the screen.

And other minor changes, e.g. don't select a colour when the mouse
is dragged over the palette, instead of clicking on it.
This commit is contained in:
Přemysl Eric Janouch 2014-10-23 09:22:17 +02:00
parent de8b897b31
commit 47e6e7fa23
1 changed files with 44 additions and 29 deletions

View File

@ -16,17 +16,25 @@
#include "config.h" #include "config.h"
typedef struct app_data app_data_t; #define PALETTE_WIDTH 9 ///< Width of the palette
struct app_data
{
termo_t *tk;
// Current attributes for the left mouse button typedef struct app_context app_context_t;
int current_attrs_left; struct app_context
// Current attributes for the right mouse button {
int current_attrs_right; termo_t *tk; ///< Termo instance
chtype palette[2 * 9]; ///< Attribute palette
uint8_t current_color_left; ///< Left mouse button color
uint8_t current_color_right; ///< Right mouse button color
}; };
static void
app_init (app_context_t *self)
{
memset (self, 0, sizeof *self);
}
static int g_winch_pipe[2]; static int g_winch_pipe[2];
static void static void
@ -46,7 +54,7 @@ display (const char *format, ...)
} }
static void static void
init_palette (app_data_t *app) init_palette (app_context_t *app)
{ {
start_color (); start_color ();
@ -63,13 +71,19 @@ init_palette (app_data_t *app)
init_pair (10 + i, COLOR_BLACK + i, COLOR_WHITE); init_pair (10 + i, COLOR_BLACK + i, COLOR_WHITE);
} }
// Initialize the palette of characters with attributes
for (int i = 0; i < PALETTE_WIDTH; i++)
{
app->palette[i] = ' ' | COLOR_PAIR (i);
app->palette[i + 9] = ' ' | COLOR_PAIR (i + 9) | A_REVERSE | A_BOLD;
}
// This usually creates a solid black or white. // This usually creates a solid black or white.
app->current_attrs_left = app->current_color_left = app->current_color_right = 9;
app->current_attrs_right = COLOR_PAIR (9) | A_REVERSE | A_BOLD;
} }
static void static void
redraw (void) redraw (app_context_t *app)
{ {
int i; int i;
@ -78,9 +92,9 @@ redraw (void)
for (i = 0; i < COLS; i++) for (i = 0; i < COLS; i++)
{ {
int pair = (float) i / COLS * 9; int pair = (float) i / COLS * PALETTE_WIDTH;
mvaddch (1, i, ' ' | COLOR_PAIR (pair)); mvaddch (1, i, app->palette[pair]);
mvaddch (2, i, ' ' | COLOR_PAIR (pair + 9) | A_REVERSE | A_BOLD); mvaddch (2, i, app->palette[pair + PALETTE_WIDTH]);
} }
display ("Choose a color from the palette and draw. " display ("Choose a color from the palette and draw. "
@ -89,7 +103,7 @@ redraw (void)
} }
static bool static bool
on_key (app_data_t *app, termo_key_t *key) on_key (app_context_t *app, termo_key_t *key)
{ {
if (key->type == TERMO_TYPE_KEYSYM if (key->type == TERMO_TYPE_KEYSYM
&& key->code.sym == TERMO_SYM_ESCAPE) && key->code.sym == TERMO_SYM_ESCAPE)
@ -110,24 +124,25 @@ on_key (app_data_t *app, termo_key_t *key)
if (event != TERMO_MOUSE_PRESS && event != TERMO_MOUSE_DRAG) if (event != TERMO_MOUSE_PRESS && event != TERMO_MOUSE_DRAG)
return true; return true;
int *attrs; uint8_t *color;
if (button == 1) if (button == 1)
attrs = &app->current_attrs_left; color = &app->current_color_left;
else if (button == 3) else if (button == 3)
attrs = &app->current_attrs_right; color = &app->current_color_right;
else else
return true; return true;
chtype ch = mvwinch (stdscr, line, col); move (line, col);
if (line >= 3) if (line >= 3)
{ {
// Paste the attributes where the user clicked. addch (app->palette[*color]);
addch (' ' | *attrs);
refresh (); refresh ();
} }
else if (line > 0) else if (line > 0 && event != TERMO_MOUSE_DRAG)
// Copy attributes from the pallete. {
*attrs = ch & (A_COLOR | A_ATTRIBUTES); int pair = (float) col / COLS * PALETTE_WIDTH;
*color = pair + (line - 1) * PALETTE_WIDTH;
}
return true; return true;
} }
@ -177,12 +192,12 @@ main (int argc, char *argv[])
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
app_data_t app; app_context_t app;
memset (&app, 0, sizeof app); app_init (&app);
app.tk = tk; app.tk = tk;
init_palette (&app); init_palette (&app);
redraw (); redraw (&app);
termo_result_t ret; termo_result_t ret;
termo_key_t key; termo_key_t key;
@ -213,7 +228,7 @@ main (int argc, char *argv[])
endwin (); endwin ();
refresh (); refresh ();
redraw (); redraw (&app);
} }
if (fds[0].revents & (POLLIN | POLLHUP | POLLERR)) if (fds[0].revents & (POLLIN | POLLHUP | POLLERR))
termo_advisereadable (tk); termo_advisereadable (tk);