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:
parent
de8b897b31
commit
47e6e7fa23
73
autistdraw.c
73
autistdraw.c
|
@ -16,17 +16,25 @@
|
|||
|
||||
#include "config.h"
|
||||
|
||||
typedef struct app_data app_data_t;
|
||||
struct app_data
|
||||
{
|
||||
termo_t *tk;
|
||||
#define PALETTE_WIDTH 9 ///< Width of the palette
|
||||
|
||||
// Current attributes for the left mouse button
|
||||
int current_attrs_left;
|
||||
// Current attributes for the right mouse button
|
||||
int current_attrs_right;
|
||||
typedef struct app_context app_context_t;
|
||||
struct app_context
|
||||
{
|
||||
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 void
|
||||
|
@ -46,7 +54,7 @@ display (const char *format, ...)
|
|||
}
|
||||
|
||||
static void
|
||||
init_palette (app_data_t *app)
|
||||
init_palette (app_context_t *app)
|
||||
{
|
||||
start_color ();
|
||||
|
||||
|
@ -63,13 +71,19 @@ init_palette (app_data_t *app)
|
|||
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.
|
||||
app->current_attrs_left =
|
||||
app->current_attrs_right = COLOR_PAIR (9) | A_REVERSE | A_BOLD;
|
||||
app->current_color_left = app->current_color_right = 9;
|
||||
}
|
||||
|
||||
static void
|
||||
redraw (void)
|
||||
redraw (app_context_t *app)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
@ -78,9 +92,9 @@ redraw (void)
|
|||
|
||||
for (i = 0; i < COLS; i++)
|
||||
{
|
||||
int pair = (float) i / COLS * 9;
|
||||
mvaddch (1, i, ' ' | COLOR_PAIR (pair));
|
||||
mvaddch (2, i, ' ' | COLOR_PAIR (pair + 9) | A_REVERSE | A_BOLD);
|
||||
int pair = (float) i / COLS * PALETTE_WIDTH;
|
||||
mvaddch (1, i, app->palette[pair]);
|
||||
mvaddch (2, i, app->palette[pair + PALETTE_WIDTH]);
|
||||
}
|
||||
|
||||
display ("Choose a color from the palette and draw. "
|
||||
|
@ -89,7 +103,7 @@ redraw (void)
|
|||
}
|
||||
|
||||
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
|
||||
&& 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)
|
||||
return true;
|
||||
|
||||
int *attrs;
|
||||
uint8_t *color;
|
||||
if (button == 1)
|
||||
attrs = &app->current_attrs_left;
|
||||
color = &app->current_color_left;
|
||||
else if (button == 3)
|
||||
attrs = &app->current_attrs_right;
|
||||
color = &app->current_color_right;
|
||||
else
|
||||
return true;
|
||||
|
||||
chtype ch = mvwinch (stdscr, line, col);
|
||||
move (line, col);
|
||||
if (line >= 3)
|
||||
{
|
||||
// Paste the attributes where the user clicked.
|
||||
addch (' ' | *attrs);
|
||||
addch (app->palette[*color]);
|
||||
refresh ();
|
||||
}
|
||||
else if (line > 0)
|
||||
// Copy attributes from the pallete.
|
||||
*attrs = ch & (A_COLOR | A_ATTRIBUTES);
|
||||
else if (line > 0 && event != TERMO_MOUSE_DRAG)
|
||||
{
|
||||
int pair = (float) col / COLS * PALETTE_WIDTH;
|
||||
*color = pair + (line - 1) * PALETTE_WIDTH;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -177,12 +192,12 @@ main (int argc, char *argv[])
|
|||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
app_data_t app;
|
||||
memset (&app, 0, sizeof app);
|
||||
app_context_t app;
|
||||
app_init (&app);
|
||||
app.tk = tk;
|
||||
|
||||
init_palette (&app);
|
||||
redraw ();
|
||||
redraw (&app);
|
||||
|
||||
termo_result_t ret;
|
||||
termo_key_t key;
|
||||
|
@ -213,7 +228,7 @@ main (int argc, char *argv[])
|
|||
endwin ();
|
||||
refresh ();
|
||||
|
||||
redraw ();
|
||||
redraw (&app);
|
||||
}
|
||||
if (fds[0].revents & (POLLIN | POLLHUP | POLLERR))
|
||||
termo_advisereadable (tk);
|
||||
|
|
Loading…
Reference in New Issue