17#define ALLEGRO_UNSTABLE 1
24#include <allegro5/allegro_primitives.h>
58 while ((opt = getopt(argc, argv,
"V:")) != -1) {
69static void draw_cell(
int widget_id,
size_t index,
float x,
float y,
float w,
float h,
void* user_data) {
74 al_draw_filled_rectangle(x, y, x + w, y + h, al_map_rgb(90, 90, 90));
78static void draw_background(
int widget_id,
float x,
float y,
float w,
float h,
void* user_data) {
82 al_draw_filled_rectangle(x, y, x + w, y + h, al_map_rgb(20, 20, 20));
86static void visible(
int widget_id,
size_t first,
size_t last,
void* user_data) {
94static void press(
int widget_id,
size_t index,
int button,
float x,
float y,
void* user_data) {
105static int wheel(
int widget_id,
int dz,
int modifiers,
void* user_data) {
109 if (!(modifiers & ALLEGRO_KEYMOD_CTRL))
return 0;
119 memset(&ev, 0,
sizeof(ev));
120 ev.type = (ALLEGRO_EVENT_TYPE)
type;
121 ev.keyboard.keycode = keycode;
129 memset(&ev, 0,
sizeof(ev));
130 ev.type = (ALLEGRO_EVENT_TYPE)
type;
133 ev.mouse.dx = (
type == ALLEGRO_EVENT_MOUSE_AXES && dz == 0) ? 1 : 0;
135 ev.mouse.button = (
unsigned int)button;
141 ALLEGRO_BITMAP* target = NULL;
142 int old = al_get_new_bitmap_flags();
144 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
145 target = al_create_bitmap(400, 300);
146 al_set_new_bitmap_flags(old);
147 al_set_target_bitmap(target);
148 al_clear_to_color(al_map_rgb(0, 0, 0));
151 al_set_target_bitmap(NULL);
152 al_destroy_bitmap(target);
155int main(
int argc,
char** argv) {
158 float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;
162 fprintf(stderr,
"Usage: %s [-V LOG_LEVEL]\n", argv[0]);
166 if (!al_init() || !al_init_font_addon() || !al_init_primitives_addon()) {
170 ALLEGRO_FONT*
font = al_create_builtin_font();
174 al_destroy_font(
font);
182 memset(&cb, 0,
sizeof(cb));
209 feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 50.0f, 50.0f, 0, -1);
230 feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 105.0f, 120.0f, 0, 0);
231 feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 105.0f, 120.0f, 1, 0);
232 feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 105.0f, 120.0f, 1, 0);
235 feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 105.0f, 120.0f, 2, 0);
236 feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 105.0f, 120.0f, 2, 0);
239 feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 245.0f, 190.0f, 0, 0);
241 feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 245.0f, 170.0f, 1, 0);
242 feed(
gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 245.0f, 170.0f, 1, 0);
247 expect_true(
"rows that fit hide the scrollbar, go back to the top and widen again",
254 memset(&cb, 0,
sizeof(cb));
259 expect_true(
"a grid with a wheel callback is made", zoomed >= 0);
263 feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 50.0f, 50.0f, 0, -1);
266 feed_key(
gui, ALLEGRO_EVENT_KEY_DOWN, ALLEGRO_KEY_RCTRL);
268 feed(
gui, ALLEGRO_EVENT_MOUSE_AXES, 50.0f, 50.0f, 0, 2);
271 feed_key(
gui, ALLEGRO_EVENT_KEY_DOWN, ALLEGRO_KEY_LCTRL);
272 feed_key(
gui, ALLEGRO_EVENT_KEY_UP, ALLEGRO_KEY_RCTRL);
274 feed_key(
gui, ALLEGRO_EVENT_DISPLAY_SWITCH_OUT, 0);
278 al_destroy_font(
font);
280 fprintf(stderr,
"ex_gui_grid: %d failure(s)\n",
failures);
283 printf(
"ex_gui_grid: ok\n");
void process_args(int argc, char **argv)
static void expect_true(const char *label, int cond)
static void feed(N_GUI_CTX *gui, int type, int x, int y, int button)
feed one mouse event at display coordinates
static N_GUI_MOUSE_EVENT last
the last event the widget was told of, and how many
static void press(int widget_id, size_t index, int button, float x, float y, void *user_data)
a press on a cell
static size_t pressed_index
static int cells_drawn
what the callbacks saw
static void draw(N_GUI_CTX *gui)
draw the whole context into a memory bitmap
static int pressed_button
static N_GUI_CTX * zoom_gui
the context the wheel callback resizes the grid in
static void visible(int widget_id, size_t first, size_t last, void *user_data)
the range to prepare
static int wheels
what the wheel callback saw, and the cell size it keeps
static void draw_background(int widget_id, float x, float y, float w, float h, void *user_data)
draw behind
static void draw_cell(int widget_id, size_t index, float x, float y, float w, float h, void *user_data)
draw one cell
static void feed_key(N_GUI_CTX *gui, int type, int keycode)
feed one key event
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.
double n_gui_scrollbar_get_pos(N_GUI_CTX *ctx, int widget_id)
get the current scroll position of a scrollbar widget
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
#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_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.
int n_gui_get_key_modifiers(const N_GUI_CTX *ctx)
the modifier keys held down now, as the GUI has seen them
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
void(* draw_background)(int widget_id, float x, float y, float w, float h, void *user_data)
Draw behind the cells, over the whole grid, before any cell; with no cells, the only thing drawn.
void(* press)(int widget_id, size_t index, int button, float x, float y, void *user_data)
A press of button (1 or 2) on cell index, at screen x, y.
void(* draw_cell)(int widget_id, size_t index, float x, float y, float w, float h, void *user_data)
Draw cell index in the box at x, y, w by h, on screen.
int(* wheel)(int widget_id, int dz, int modifiers, void *user_data)
A wheel turn of dz notches over the grid, positive away from the user, with the modifier keys held (A...
void(* visible)(int widget_id, size_t first, size_t last, void *user_data)
Called after each draw with the cells drawn and one row beyond, first included and last excluded: wha...
void n_gui_grid_set_rect(N_GUI_CTX *ctx, int widget_id, float x, float y, float w, float h, float bar_w)
Place the grid and its scrollbar in a rectangle of the window.
void n_gui_grid_set_scroll(N_GUI_CTX *ctx, int widget_id, float scroll)
Set the scroll offset, clamped to the content.
int n_gui_grid_new(N_GUI_CTX *ctx, int window_id, const N_GUI_GRID_CALLBACKS *callbacks, void *user_data)
Add a grid, and its scrollbar, to a window.
int n_gui_grid_index_at(N_GUI_CTX *ctx, int widget_id, float x, float y, size_t *index)
Which cell is at a point on screen.
void n_gui_grid_set_count(N_GUI_CTX *ctx, int widget_id, size_t count)
Set how many cells there are.
int n_gui_grid_columns(N_GUI_CTX *ctx, int widget_id)
How many cells a row holds at the grid's width.
int n_gui_grid_cell_rect(N_GUI_CTX *ctx, int widget_id, size_t index, float *x, float *y, float *w, float *h)
Where a cell is on screen, whether or not it is scrolled into view.
void n_gui_grid_scroll_to(N_GUI_CTX *ctx, int widget_id, size_t index)
Scroll the least that brings a cell's whole row into view.
int n_gui_grid_scrollbar_showing(N_GUI_CTX *ctx, int widget_id)
Whether the scrollbar is shown, which is whether the rows overflow.
float n_gui_grid_get_scroll(N_GUI_CTX *ctx, int widget_id)
The scroll offset, in pixels from the top of the content.
void n_gui_grid_set_cell_size(N_GUI_CTX *ctx, int widget_id, float cell_w, float cell_h, float gap)
Set the size of a cell and the gap around cells.
void n_gui_grid_set_visible(N_GUI_CTX *ctx, int widget_id, int visible)
Show or hide the grid and its scrollbar together.
int n_gui_grid_scrollbar(N_GUI_CTX *ctx, int widget_id)
The grid's scrollbar widget.
What the grid asks of its caller.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
A virtualised grid of cells for N_GUI, scrolled vertically.