Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_gui_cursor.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
37
38#define ALLEGRO_UNSTABLE 1
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43
44#include "nilorea/n_common.h"
45#include "nilorea/n_gui.h"
46#include "nilorea/n_log.h"
47
49static int failures = 0;
50
56static void expect_true(const char* label, int cond) {
57 if (!cond) {
58 n_log(LOG_ERR, "%s: FAILED", label);
59 failures++;
60 } else {
61 n_log(LOG_INFO, "%s: OK", label);
62 }
63}
64
72static void mouse(N_GUI_CTX* gui, int type, int x, int y) {
73 ALLEGRO_EVENT ev;
74 memset(&ev, 0, sizeof(ev));
75 ev.type = (unsigned int)type;
76 ev.mouse.x = x;
77 ev.mouse.y = y;
78 ev.mouse.button = 1;
80}
81
82int main(void) {
83 ALLEGRO_FONT* font;
85 int win, sp;
86 ALLEGRO_MOUSE_CURSOR* custom = (ALLEGRO_MOUSE_CURSOR*)1;
87
89 if (!al_init() || !al_init_font_addon()) {
90 n_log(LOG_ERR, "allegro init failed");
91 return 1;
92 }
93 font = al_create_builtin_font(); /* no display needed */
94 if (!font) {
95 n_log(LOG_ERR, "al_create_builtin_font failed");
96 return 1;
97 }
99 if (!gui) {
100 n_log(LOG_ERR, "n_gui_new_ctx failed");
101 al_destroy_font(font);
102 return 1;
103 }
104 n_gui_set_display_size(gui, 800.0f, 600.0f);
105 /* A window with a vertical split pane down the middle of its left half,
106 and nothing of the GUI's on the right: the right is "the application's
107 own area", where its picking tool would be. */
108 win = n_gui_add_window(gui, "W", 0.0f, 0.0f, 400.0f, 600.0f);
110 sp = n_gui_add_splitpane(gui, win, 0.0f, 0.0f, 400.0f, 600.0f, N_GUI_SPLIT_VERTICAL, 0.5f, NULL, NULL);
112 expect_true("split pane created", sp >= 0);
113
114 n_gui_get_cursor(gui, &custom);
115 expect_true("the default until asked", n_gui_get_cursor(gui, NULL) == ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT && custom == NULL);
116
117 n_gui_set_cursor(gui, ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION, NULL);
118 expect_true("applied at once", n_gui_get_cursor(gui, NULL) == ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION);
119 mouse(gui, ALLEGRO_EVENT_MOUSE_AXES, 600, 300);
120 expect_true("kept over the application's area", n_gui_get_cursor(gui, NULL) == ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION);
121
122 /* Over the divider the GUI's own resize cursor wins, and the
123 application's comes back when the pointer leaves it. */
124 mouse(gui, ALLEGRO_EVENT_MOUSE_AXES, 200, 300);
125 expect_true("the divider shows its resize cursor", n_gui_get_cursor(gui, NULL) == ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E);
126 mouse(gui, ALLEGRO_EVENT_MOUSE_AXES, 600, 300);
127 expect_true("and the application's comes back", n_gui_get_cursor(gui, NULL) == ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION);
128
129 /* A press held is a drag, which keeps the cursor it started with. */
130 mouse(gui, ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, 600, 300);
131 n_gui_set_cursor(gui, ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE, NULL);
132 expect_true("not changed under a held press", n_gui_get_cursor(gui, NULL) == ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION);
133 mouse(gui, ALLEGRO_EVENT_MOUSE_BUTTON_UP, 600, 300);
134 mouse(gui, ALLEGRO_EVENT_MOUSE_AXES, 601, 300);
135 expect_true("changed once it is let go", n_gui_get_cursor(gui, NULL) == ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE);
136
137 n_gui_set_cursor(gui, ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT, NULL);
138 mouse(gui, ALLEGRO_EVENT_MOUSE_AXES, 600, 300);
139 n_gui_get_cursor(gui, &custom);
140 expect_true("the default hands back to the GUI", n_gui_get_cursor(gui, NULL) == ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT && custom == NULL);
141
143 al_destroy_font(font);
144 if (failures) {
145 n_log(LOG_ERR, "ex_gui_cursor: %d failure(s)", failures);
146 return 1;
147 }
148 n_log(LOG_NOTICE, "ex_gui_cursor: all checks passed");
149 return 0;
150}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static void mouse(N_GUI_CTX *gui, int type, int x, int y)
Send one mouse event.
static N_GUI_CTX * gui
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.
Definition n_gui.c:12348
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
Definition n_gui.c:13611
int n_gui_add_splitpane(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int orientation, float ratio, void(*on_change)(int, float, void *), void *user_data)
Add a split pane widget: a draggable divider between two regions.
Definition n_gui.c:3331
#define N_GUI_WIN_FRAMELESS
frameless window: no title bar drawn, drag via window body unless N_GUI_WIN_FIXED_POSITION is also se...
Definition n_gui.h:276
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.
Definition n_gui.c:1277
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
Definition n_gui.c:1103
#define N_GUI_SPLIT_VERTICAL
split pane: vertical divider, regions are left and right
Definition n_gui.h:146
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
Definition n_gui.c:1209
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
Definition n_gui.c:1436
int n_gui_get_cursor(N_GUI_CTX *ctx, ALLEGRO_MOUSE_CURSOR **custom)
The cursor the GUI last applied to the display.
Definition n_gui.c:491
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
Definition n_gui.c:2515
void n_gui_set_cursor(N_GUI_CTX *ctx, int system_shape, ALLEGRO_MOUSE_CURSOR *custom)
Say which mouse cursor the application wants.
Definition n_gui.c:479
The top-level GUI context that holds all windows.
Definition n_gui.h:1669
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_INFO
informational
Definition n_log.h:106
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Generic log system.