#include "widget.h" #include "string.h" #include static widget_vtable_t const widget_vtable = { widget_draw, widget_get_focus, widget_model_to_screen, widget_contains_coordinate, widget_on_mouse_down, widget_on_mouse_up, widget_on_mouse_move, widget_on_key_down, widget_on_key_up }; void widget_init( widget_t *widget, widget_t *parent, const int x, const int y, const int w, const int h, const vga_color_t background_color ) { memset( widget, 0, sizeof( widget_t ) ); widget->parent = parent; widget->x = x; widget->y = y; widget->w = w; widget->h = h; widget->background_color = background_color; widget->vtable = &widget_vtable; } void widget_draw( void *obj, graphics_context_t *context ) { widget_t *widget = obj; int x = 0; int y = 0; widget->vtable->model_to_screen( widget, &x, &y ); vga_draw_rectangle( context, x, y, widget->w, widget->h, widget->background_color ); } void widget_get_focus( void *obj, widget_t *widget ) { widget_t *o = obj; if( o->parent != NULL ) { o->parent->vtable->get_focus( o->parent, widget ); } } void widget_model_to_screen( void *obj, int *x, int *y ) { widget_t *widget = obj; if( widget->parent != NULL ) { widget->parent->vtable->model_to_screen( widget->parent, x, y ); } *x += widget->x; *y += widget->y; } bool widget_contains_coordinate( void *obj, const int x, const int y ) { widget_t *widget = obj; return widget->x <= x && x < widget->x + widget->w && widget->y <= y && y < widget->y + widget->h; } void widget_on_mouse_down( void *obj, const int x, const int y ) { widget_t *widget = obj; if( widget->focusable ) { widget->vtable->get_focus( widget, widget ); } } void widget_on_mouse_up( void *obj, const int x, const int y ) { } void widget_on_mouse_move( void *obj, const int old_x, const int old_y, const int x, const int y ) { } void widget_on_key_down( void *obj, char c ) { } void widget_on_key_up( void *obj, char c ) { }