fix animations target and compiler warnings

This commit is contained in:
hippoz 2023-04-13 03:47:38 +03:00
parent 88aa8f8202
commit 7b75cdb768
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
16 changed files with 41 additions and 37 deletions

View file

@ -1,6 +1,6 @@
CC?=cc CC=clang
CFLAGS_LIBS:=`pkg-config --cflags --libs xcb cairo pangocairo` -lm LIBS:=`pkg-config --libs xcb cairo pangocairo` -lm
CFLAGS:=$(CFLAGS) -pipe -Wall -Wextra -Wshadow -std=c99 -pedantic $(CFLAGS_LIBS) CFLAGS:=$(CFLAGS) -pipe -Wall -Wextra -Wshadow -std=c99 -pedantic `pkg-config --cflags xcb cairo pangocairo`
BUILD=build BUILD=build
OBJ=$(BUILD) OBJ=$(BUILD)
@ -20,7 +20,7 @@ release: CFLAGS+=-O2 -flto=auto -DNDEBUG
release: clean $(BUILD) $(BIN) release: clean $(BUILD) $(BIN)
$(BIN): $(OBJS) $(BIN): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $@ $(CC) $(CFLAGS) $(LIBS) $(OBJS) -o $@
-include $(DEPS) -include $(DEPS)

View file

@ -91,4 +91,4 @@ UIBackgroundNode *background_node_new(UINode *parent, UIRGBA normal, double bord
n->border_radius = border_radius; n->border_radius = border_radius;
node_attach(parent, (UINode*)n); node_attach(parent, (UINode*)n);
return n; return n;
} }

View file

@ -15,4 +15,4 @@ int background_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p);
UIBackgroundNode *state_background_node_new(UINode *parent, UIRGBA normal, UIRGBA hovered, UIRGBA pressed, double border_radius); UIBackgroundNode *state_background_node_new(UINode *parent, UIRGBA normal, UIRGBA hovered, UIRGBA pressed, double border_radius);
UIBackgroundNode *background_node_new(UINode *parent, UIRGBA normal, double border_radius); UIBackgroundNode *background_node_new(UINode *parent, UIRGBA normal, double border_radius);
#endif // _UI__BACKGROUND_NODE_H #endif // _UI__BACKGROUND_NODE_H

View file

@ -12,4 +12,4 @@ typedef struct UIBoxLayoutNode {
UIBoxLayoutNode *box_layout_new(UINode *parent, enum UIDirection direction); UIBoxLayoutNode *box_layout_new(UINode *parent, enum UIDirection direction);
int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p); int box_layout_handle(UINode *component, enum UIEvent ev, size_t d, void *p);
#endif // _UI__BOX_LAYOUT_NODE_H #endif // _UI__BOX_LAYOUT_NODE_H

View file

@ -8,4 +8,4 @@ typedef struct UIRGBA {
#define UI_HEX_TO_COLOR_NORMAL(hex) ((((hex) & 0xFF0000) >> 16) / 255.0), ((((hex) & 0xFF00) >> 8) / 255.0), (((hex) & 0xFF) / 255.0) #define UI_HEX_TO_COLOR_NORMAL(hex) ((((hex) & 0xFF0000) >> 16) / 255.0), ((((hex) & 0xFF00) >> 8) / 255.0), (((hex) & 0xFF) / 255.0)
#define UI_HEX_TO_RGBA(hex) (UIRGBA){UI_HEX_TO_COLOR_NORMAL(hex), 1.0} #define UI_HEX_TO_RGBA(hex) (UIRGBA){UI_HEX_TO_COLOR_NORMAL(hex), 1.0}
#endif // _UI__COLOR_H #endif // _UI__COLOR_H

View file

@ -14,4 +14,4 @@ typedef struct UIDispatcherNode {
UIDispatcherNode *dispatcher_new(UINode *parent, enum UIEvent node_event_type, int event_type, void *data, int (*handle)(struct UINode *node, void *data, int event_type, size_t d, void *p)); UIDispatcherNode *dispatcher_new(UINode *parent, enum UIEvent node_event_type, int event_type, void *data, int (*handle)(struct UINode *node, void *data, int event_type, size_t d, void *p));
int dispatcher_handle(UINode *node, enum UIEvent ev, size_t d, void *p); int dispatcher_handle(UINode *node, enum UIEvent ev, size_t d, void *p);
#endif // _UI__DISPATCHER_NODE_H #endif // _UI__DISPATCHER_NODE_H

View file

@ -22,9 +22,9 @@ typedef struct AppState {
int count; int count;
char counter_text[12]; char counter_text[12];
uint64_t sidebar_animate_duration_ms; int64_t sidebar_animate_duration_ms;
uint64_t sidebar_animate_time_ms; int64_t sidebar_animate_time_ms;
double sidebar_animate_target_w; double sidebar_animate_target_w;
double sidebar_animate_start_w; double sidebar_animate_start_w;
} AppState; } AppState;
@ -60,7 +60,7 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void *
/* sidebar */ /* sidebar */
{ {
UINode *sidebar = node_new(node, "sidebar"); UINode *sidebar = node_new(node, "sidebar");
sidebar->width_policy = UI_SIZE_POLICY_STATIC; sidebar->width_policy = UI_SIZE_POLICY_FIXED;
sidebar->height_policy = UI_SIZE_POLICY_GROW; sidebar->height_policy = UI_SIZE_POLICY_GROW;
sidebar->rect.w = 200; sidebar->rect.w = 200;
dispatcher_new(sidebar, UI_EVENT_TIMER_END, SIDEBAR_ANIAMTE, state, app_handle); dispatcher_new(sidebar, UI_EVENT_TIMER_END, SIDEBAR_ANIAMTE, state, app_handle);
@ -108,19 +108,24 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void *
break; break;
} }
case SIDEBAR_ANIAMTE: { case SIDEBAR_ANIAMTE: {
uint64_t dt = *(uint64_t*)p; int64_t dt = *(int64_t*)p;
state->sidebar_animate_time_ms += dt; state->sidebar_animate_time_ms += dt;
state->sidebar_node->rect.w = lerp(state->sidebar_animate_start_w, state->sidebar_animate_target_w, (double)state->sidebar_animate_time_ms / (double)state->sidebar_animate_duration_ms); state->sidebar_node->rect.w = lerp(state->sidebar_animate_start_w, state->sidebar_animate_target_w, (double)state->sidebar_animate_time_ms / (double)state->sidebar_animate_duration_ms);
if (state->sidebar_node->rect.w < 0) { if (state->sidebar_node->rect.w < 0) {
state->sidebar_node->rect.w = 0; state->sidebar_node->rect.w = 0;
} }
node_request_relayout(state->sidebar_node->parent);
if (state->sidebar_animate_time_ms >= state->sidebar_animate_duration_ms) { if (state->sidebar_animate_time_ms >= state->sidebar_animate_duration_ms) {
state->sidebar_node->rect.w = state->sidebar_animate_target_w;
}
node_request_relayout(state->sidebar_node);
if (state->sidebar_node->rect.w == state->sidebar_animate_target_w) {
state->sidebar_animate_time_ms = 0; state->sidebar_animate_time_ms = 0;
return 0; return 0;
} }
window_sched_timer(node->window, state->sidebar_node, 10); window_sched_timer(node->window, state->sidebar_node, 10);
return 1; return 1;
} }
case INCREMENT_COUNT: { case INCREMENT_COUNT: {
@ -142,7 +147,7 @@ int app_handle(struct UINode *node, void *data, int event_type, size_t d, void *
return 0; return 0;
} }
int main() int main(void)
{ {
UIWindow *window = window_new(800, 600); UIWindow *window = window_new(800, 600);
if (!window) { if (!window) {

View file

@ -65,4 +65,4 @@ UINode *node_new(UINode *parent, const char *text);
void node_dump(UINode *node, int depth); void node_dump(UINode *node, int depth);
void node_request_relayout(UINode *node); void node_request_relayout(UINode *node);
#endif // _UI__NODE_H #endif // _UI__NODE_H

View file

@ -158,4 +158,4 @@ void dump_summary(FILE *stream)
#define end_clock(...) #define end_clock(...)
#define dump_summary(...) #define dump_summary(...)
#define clear_summary(...) #define clear_summary(...)
#endif #endif

View file

@ -13,4 +13,4 @@ bool ui_rect_overlap_rect(UIRect *a, UIRect *b);
UIRect ui_rect_united(UIRect *a, UIRect *b); UIRect ui_rect_united(UIRect *a, UIRect *b);
bool ui_rect_equals(UIRect *a, UIRect *b); bool ui_rect_equals(UIRect *a, UIRect *b);
#endif // _UI__RECT_H #endif // _UI__RECT_H

View file

@ -12,4 +12,4 @@ typedef struct UIScrollableNode {
UIScrollableNode *scrollable_new(UINode *parent, UINode *target); UIScrollableNode *scrollable_new(UINode *parent, UINode *target);
int scrollable_handle(UINode *node, enum UIEvent ev, size_t d, void *p); int scrollable_handle(UINode *node, enum UIEvent ev, size_t d, void *p);
#endif // _UI__SCROLL_CONTAINER_NODE_H #endif // _UI__SCROLL_CONTAINER_NODE_H

View file

@ -58,7 +58,6 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
pango_layout_get_size(n->layout, &w, NULL); pango_layout_get_size(n->layout, &w, NULL);
*(double*)p = pango_units_to_double(w); *(double*)p = pango_units_to_double(w);
return 1; return 1;
break;
} }
case UI_EVENT_GET_HEIGHT: { case UI_EVENT_GET_HEIGHT: {
if (!n->layout) { if (!n->layout) {
@ -68,7 +67,6 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
pango_layout_get_size(n->layout, NULL, &h); pango_layout_get_size(n->layout, NULL, &h);
*(double*)p = pango_units_to_double(h); *(double*)p = pango_units_to_double(h);
return 1; return 1;
break;
} }
case UI_EVENT_RELAYOUT: { case UI_EVENT_RELAYOUT: {
if (!n->layout) { if (!n->layout) {
@ -91,4 +89,3 @@ int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p)
} }
return 0; return 0;
} }

View file

@ -18,4 +18,4 @@ typedef struct UITextNode {
int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p); int text_node_handle(UINode *node, enum UIEvent ev, size_t d, void *p);
UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA color, char *text); UITextNode *text_node_new(UINode *parent, PangoFontDescription *desc, UIRGBA color, char *text);
#endif // _UI__TEXT_NODE_H #endif // _UI__TEXT_NODE_H

View file

@ -5,4 +5,4 @@
int64_t time_current_ms(void); int64_t time_current_ms(void);
#endif // _UI__TIMEUTIL_H #endif // _UI__TIMEUTIL_H

View file

@ -10,6 +10,8 @@
#include <time.h> #include <time.h>
#include <xcb/xcb.h> #include <xcb/xcb.h>
void _window_process_xcb_event(UIWindow *window, xcb_generic_event_t *event);
int window_invalidate_node(UIWindow *window, UINode *node) int window_invalidate_node(UIWindow *window, UINode *node)
{ {
if (node->flags & UI_NODE_COMPONENT) { if (node->flags & UI_NODE_COMPONENT) {
@ -285,14 +287,14 @@ int window_turn(UIWindow *window)
fds[0].fd = xcb_get_file_descriptor(window->_xcb_connection); fds[0].fd = xcb_get_file_descriptor(window->_xcb_connection);
fds[0].events = POLLIN; fds[0].events = POLLIN;
uint64_t frame_peak_ms = 0; int64_t frame_peak_ms = 0;
uint64_t frame_peak_last_measurement_ms = 0; int64_t frame_peak_last_measurement_ms = 0;
for (;;) { for (;;) {
int64_t poll_timeout; int64_t poll_timeout;
/* compute `poll_timeout` based on active timers */ /* compute `poll_timeout` based on active timers */
{ {
uint64_t now = time_current_ms(); int64_t now = time_current_ms();
int64_t lowest = 0; int64_t lowest = 0;
bool has_timer = false; bool has_timer = false;
for (int i = 0; i < UI_WINDOW_MAX_TIMERS; i++) { for (int i = 0; i < UI_WINDOW_MAX_TIMERS; i++) {
@ -325,7 +327,7 @@ int window_turn(UIWindow *window)
clear_summary(); clear_summary();
uint64_t frame_start_ms = time_current_ms(); int64_t frame_start_ms = time_current_ms();
for (int i = 0; i < UI_WINDOW_MAX_POLL; i++) { for (int i = 0; i < UI_WINDOW_MAX_POLL; i++) {
if (fds[i].revents & POLLNVAL) { if (fds[i].revents & POLLNVAL) {
fprintf(stderr, "err: window_turn(): poll got POLLNVAL\n"); fprintf(stderr, "err: window_turn(): poll got POLLNVAL\n");
@ -354,10 +356,10 @@ int window_turn(UIWindow *window)
for (int j = 0; j < UI_WINDOW_MAX_TIMERS; j++) { for (int j = 0; j < UI_WINDOW_MAX_TIMERS; j++) {
UIWindowTimer *timer = &window->timers[j]; UIWindowTimer *timer = &window->timers[j];
if (timer->present) { if (timer->present) {
uint64_t now = time_current_ms(); int64_t now = time_current_ms();
int64_t distance = (timer->started_at_ms + timer->duration_ms) - now; int64_t distance = (timer->started_at_ms + timer->duration_ms) - now;
if (distance <= 0) { if (distance <= 0) {
uint64_t dt = time_current_ms() - timer->started_at_ms; int64_t dt = time_current_ms() - timer->started_at_ms;
UINode *target = timer->target; UINode *target = timer->target;
timer->present = false; timer->present = false;
timer->started_at_ms = 0; timer->started_at_ms = 0;
@ -381,8 +383,8 @@ int window_turn(UIWindow *window)
has_looped_once = true; has_looped_once = true;
uint64_t frame_end_ms = time_current_ms(); int64_t frame_end_ms = time_current_ms();
uint64_t frame_delta_ms = frame_end_ms - frame_start_ms; int64_t frame_delta_ms = frame_end_ms - frame_start_ms;
if (frame_delta_ms > frame_peak_ms || frame_end_ms - frame_peak_last_measurement_ms >= 3000) { if (frame_delta_ms > frame_peak_ms || frame_end_ms - frame_peak_last_measurement_ms >= 3000) {
frame_peak_last_measurement_ms = frame_end_ms; frame_peak_last_measurement_ms = frame_end_ms;
frame_peak_ms = frame_delta_ms; frame_peak_ms = frame_delta_ms;
@ -394,7 +396,7 @@ int window_turn(UIWindow *window)
return 0; return 0;
} }
UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, uint64_t duration_ms) UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, int64_t duration_ms)
{ {
for (int i = 0; i < UI_WINDOW_MAX_TIMERS; i++) { for (int i = 0; i < UI_WINDOW_MAX_TIMERS; i++) {
if (!window->timers[i].present) { if (!window->timers[i].present) {

View file

@ -13,8 +13,8 @@
typedef struct UIWindowTimer { typedef struct UIWindowTimer {
bool present; bool present;
uint64_t started_at_ms; int64_t started_at_ms;
uint64_t duration_ms; int64_t duration_ms;
UINode *target; // TODO: what if the node gets deleted while the timer is still running? UINode *target; // TODO: what if the node gets deleted while the timer is still running?
} UIWindowTimer; } UIWindowTimer;
@ -38,7 +38,7 @@ UIWindow *window_new(int width, int height);
bool window_flush_invalidated(UIWindow *window); bool window_flush_invalidated(UIWindow *window);
int window_attach_root(UIWindow *window, UINode *root); int window_attach_root(UIWindow *window, UINode *root);
int window_turn(UIWindow *window); int window_turn(UIWindow *window);
UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, uint64_t duration_ms); UIWindowTimer *window_sched_timer(UIWindow *window, UINode *target, int64_t duration_ms);
#endif // _UI__WINDOW_H #endif // _UI__WINDOW_H