29#define ALLEGRO_UNSTABLE 1
36#include <allegro5/allegro_primitives.h>
71 while ((opt = getopt(argc, argv,
"V:")) != -1) {
85static void on_change(
int widget_id,
int dragging,
void* user_data) {
97static ALLEGRO_COLOR
grey(
float u,
float v,
void* user_data) {
102 return al_map_rgb_f(0.5f, 0.5f, 0.5f);
114 memset(&ev, 0,
sizeof(ev));
115 ev.type = (ALLEGRO_EVENT_TYPE)
type;
118 ev.mouse.dx =
type == ALLEGRO_EVENT_MOUSE_AXES ? 1 : 0;
119 ev.mouse.button = (
unsigned int)button;
125#define AT_X(u) ((int)(150.0f + (u) * 98.0f + 0.5f))
127#define AT_Y(v) ((int)(150.0f - (v) * 98.0f + 0.5f))
146static int near(
float a,
float b) {
147 return a - b < 0.02f && b - a < 0.02f;
154int main(
int argc,
char** argv) {
155 float u = 0.0f, v = 0.0f;
159 fprintf(stderr,
"Usage: %s [-V LOG_LEVEL]\n", argv[0]);
163 if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
167 ALLEGRO_FONT*
font = al_create_builtin_font();
171 al_destroy_font(
font);
183 drag(
gui, 0.0f, 0.0f, 0.5f, 0.5f);
188 drag(
gui, 0.5f, 0.5f, 1.2f, 0.0f);
193 feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN,
AT_X(0.3f),
AT_Y(-0.3f), 2);
196 expect_true(
"a right press puts it back in the centre", u == 0.0f && v == 0.0f);
211 expect_true(
"a double click puts it back in the centre, and the drag after does not move it", u == 0.0f && v == 0.0f);
216 expect_true(
"reads back", u == -0.25f && v == 0.5f);
228 float r = 0.0f, g = 0.0f, b = 0.0f;
230 al_unmap_rgb_f(c, &r, &g, &b);
231 expect_true(
"the default disc is red to the right", r > 0.99f && g < 0.01f && b < 0.01f);
233 al_unmap_rgb_f(c, &r, &g, &b);
234 expect_true(
"and white in the centre", r > 0.99f && g > 0.99f && b > 0.99f);
242 ALLEGRO_BITMAP* target = NULL;
243 int old = al_get_new_bitmap_flags();
246 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
247 target = al_create_bitmap(400, 300);
248 al_set_new_bitmap_flags(old);
249 al_set_target_bitmap(target);
250 al_clear_to_color(al_map_rgb(0, 0, 0));
252 al_set_target_bitmap(NULL);
253 al_destroy_bitmap(target);
258 al_destroy_font(
font);
260 fprintf(stderr,
"ex_gui_colorwheel: %d failure(s)\n",
failures);
263 printf(
"ex_gui_colorwheel: ok\n");
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
static int changes
how many times the picker called back, and with what
static void on_change(const float rgba[4], void *user_data)
the picker's callback
static void drag(N_GUI_CTX *gui, float fu, float fv, float tu, float tv)
press at one point, drag to another, release
static void feed(N_GUI_CTX *gui, int type, int x, int y, int button)
feed one mouse event at display coordinates
static int near(float a, float b)
is a near b
#define AT_X(u)
the wheel sits at 50,50 and is 200 by 200, its disc of radius 98 centred at 150,150: display position...
static int colour_asked
set when the caller's colour was asked for
#define AT_Y(v)
as AT_X, for v, which goes up
static ALLEGRO_COLOR grey(float u, float v, void *user_data)
a caller's colour: grey everywhere
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
FONT * font
the default 8x8 font, valid after allegro_init()
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
set the display (viewport) size so global scrollbars can be computed.
#define N_GUI_ALIGN_LEFT
left aligned text
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
void n_gui_set_double_click_time(N_GUI_CTX *ctx, double seconds)
set how long after a press a second one still counts as a double click
void n_gui_draw(N_GUI_CTX *ctx)
draw all visible GUI windows and widgets
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
The top-level GUI context that holds all windows.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_INFO
informational
ALLEGRO_COLOR n_gui_colorwheel_hue(float u, float v, void *user_data)
The default disc colour: HSV, hue by angle with red to the right and going counter clockwise,...
int n_gui_colorwheel_get(N_GUI_CTX *ctx, int widget_id, float *u, float *v)
Read the point.
int n_gui_colorwheel_new(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, N_GUI_COLORWHEEL_CHANGE on_change, void *user_data)
Add a colour wheel, its point in the centre.
void n_gui_colorwheel_set_colour(N_GUI_CTX *ctx, int widget_id, N_GUI_COLORWHEEL_COLOUR colour, void *user_data)
Paint the disc with the caller's colours.
int n_gui_colorwheel_set(N_GUI_CTX *ctx, int widget_id, float u, float v)
Set the point.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
A colour wheel for N_GUI: one point in a disc, dragged.