Drawing app has buttons (working on a toolkit)
This commit is contained in:
parent
9d4e560b8b
commit
9987416fad
@ -880,6 +880,7 @@ void * process_requests(void * garbage) {
|
||||
int32_t _mouse_init_y;
|
||||
int32_t _mouse_win_x;
|
||||
int32_t _mouse_win_y;
|
||||
int8_t _mouse_moved = 0;
|
||||
|
||||
int32_t _mouse_win_x_p;
|
||||
int32_t _mouse_win_y_p;
|
||||
@ -949,6 +950,7 @@ void * process_requests(void * garbage) {
|
||||
click_y = mouse_y / MOUSE_SCALE - _mouse_win_y;
|
||||
|
||||
mouse_discard = 1;
|
||||
_mouse_moved = 0;
|
||||
|
||||
printf("Mouse down at @ %d,%d = %d,%d\n", mouse_x, mouse_y, click_x, click_y);
|
||||
}
|
||||
@ -995,6 +997,23 @@ void * process_requests(void * garbage) {
|
||||
|
||||
click_x = mouse_x / MOUSE_SCALE - _mouse_win_x;
|
||||
click_y = mouse_y / MOUSE_SCALE - _mouse_win_y;
|
||||
|
||||
if (!_mouse_moved) {
|
||||
printf("Finished a click!\n");
|
||||
w_mouse_t _packet;
|
||||
_packet.wid = _mouse_window->wid;
|
||||
_mouse_win_x = _mouse_window->x;
|
||||
_mouse_win_y = _mouse_window->y;
|
||||
click_x = mouse_x / MOUSE_SCALE - _mouse_win_x;
|
||||
click_y = mouse_y / MOUSE_SCALE - _mouse_win_y;
|
||||
_packet.new_x = click_x;
|
||||
_packet.new_y = click_y;
|
||||
_packet.old_x = -1;
|
||||
_packet.old_y = -1;
|
||||
_packet.buttons = packet->buttons;
|
||||
_packet.command = WE_MOUSECLICK;
|
||||
send_mouse_event(_mouse_window->owner, WE_MOUSEMOVE, &_packet);
|
||||
}
|
||||
|
||||
printf("Mouse up at @ %d,%d = %d,%d\n", mouse_x, mouse_y, click_x, click_y);
|
||||
#if 0 /* Resizing */
|
||||
@ -1017,6 +1036,7 @@ void * process_requests(void * garbage) {
|
||||
} else {
|
||||
/* Still down */
|
||||
|
||||
_mouse_moved = 1;
|
||||
mouse_discard--;
|
||||
if (mouse_discard < 1) {
|
||||
mouse_discard = MOUSE_DISCARD_LEVEL;
|
||||
@ -1037,6 +1057,7 @@ void * process_requests(void * garbage) {
|
||||
_packet.new_y = click_y;
|
||||
|
||||
_packet.buttons = packet->buttons;
|
||||
_packet.command = WE_MOUSEMOVE;
|
||||
|
||||
send_mouse_event(_mouse_window->owner, WE_MOUSEMOVE, &_packet);
|
||||
}
|
||||
|
122
userspace/draw.c
122
userspace/draw.c
@ -2,6 +2,107 @@
|
||||
|
||||
#include "lib/window.h"
|
||||
#include "lib/graphics.h"
|
||||
#include "lib/list.h"
|
||||
|
||||
/* XXX TOOLKIT FUNCTIONS */
|
||||
|
||||
static window_t * ttk_window = NULL;
|
||||
static list_t * ttk_objects = NULL;
|
||||
|
||||
typedef struct {
|
||||
uint32_t type;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
void (*render_func)(void *);
|
||||
void (*click_callback)(void *, w_mouse_t *);
|
||||
} ttk_object;
|
||||
|
||||
/* Button */
|
||||
typedef struct {
|
||||
ttk_object _super;
|
||||
char * title;
|
||||
uint32_t fill_color;
|
||||
} ttk_button;
|
||||
|
||||
void ttk_render_button(void * s) {
|
||||
ttk_object * self = (ttk_object *)s;
|
||||
for (uint16_t y = self->y + 1; y < self->y + self->height; y++) {
|
||||
draw_line(self->x, self->x + self->width, y, y, ((ttk_button *)self)->fill_color);
|
||||
}
|
||||
uint32_t border_color = rgb(0,0,0);
|
||||
draw_line(self->x, self->x + self->width, self->y, self->y, border_color);
|
||||
draw_line(self->x, self->x, self->y, self->y + self->height, border_color);
|
||||
draw_line(self->x + self->width, self->x + self->width, self->y, self->y + self->height, border_color);
|
||||
draw_line(self->x, self->x + self->width, self->y + self->height, self->y + self->height, border_color);
|
||||
}
|
||||
|
||||
ttk_button * ttk_new_button(char * title, void (*callback)(void *, w_mouse_t *)) {
|
||||
ttk_button * out = malloc(sizeof(ttk_button));
|
||||
out->title = title;
|
||||
out->fill_color = rgb(100,100,100);
|
||||
|
||||
/* Standard */
|
||||
ttk_object * obj = (ttk_object *)out;
|
||||
obj->click_callback = callback;;
|
||||
obj->render_func = ttk_render_button;
|
||||
|
||||
list_insert(ttk_objects, obj);
|
||||
return out;
|
||||
}
|
||||
|
||||
void ttk_position(ttk_object * obj, int x, int y, int width, int height) {
|
||||
obj->x = x;
|
||||
obj->y = y;
|
||||
obj->width = width;
|
||||
obj->height = height;
|
||||
}
|
||||
|
||||
int ttk_within(ttk_object * obj, w_mouse_t * evt) {
|
||||
if (evt->new_x >= obj->x && evt->new_x < obj->x + obj->width &&
|
||||
evt->new_y >= obj->y && evt->new_y < obj->y + obj->height) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ttk_check_click(w_mouse_t * evt) {
|
||||
if (evt->command == WE_MOUSECLICK) {
|
||||
printf("Mouse click occured at %d,%d\n", evt->new_x, evt->new_y);
|
||||
foreach(node, ttk_objects) {
|
||||
ttk_object * obj = (ttk_object *)node->value;
|
||||
if (ttk_within(obj, evt)) {
|
||||
printf("And I found where!\n");
|
||||
if (obj->click_callback) {
|
||||
obj->click_callback(obj, evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ttk_render() {
|
||||
foreach(node, ttk_objects) {
|
||||
ttk_object * obj = (ttk_object *)node->value;
|
||||
if (obj->render_func) {
|
||||
obj->render_func(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup_ttk(window_t * window) {
|
||||
ttk_window = window;
|
||||
ttk_objects = list_create();
|
||||
mouse_action_callback = ttk_check_click;
|
||||
}
|
||||
|
||||
uint32_t drawing_color = 0;
|
||||
|
||||
static void set_color(void * button, w_mouse_t * event) {
|
||||
ttk_button * self = (ttk_button *)button;
|
||||
drawing_color = self->fill_color;
|
||||
}
|
||||
|
||||
int main (int argc, char ** argv) {
|
||||
int left = 30;
|
||||
@ -19,6 +120,23 @@ int main (int argc, char ** argv) {
|
||||
|
||||
win_use_threaded_handler();
|
||||
|
||||
setup_ttk(wina);
|
||||
ttk_button * button_blue = ttk_new_button("Blue", set_color);
|
||||
ttk_position((ttk_object *)button_blue, 3, 3, 100, 20);
|
||||
button_blue->fill_color = rgb(0,0,255);
|
||||
|
||||
ttk_button * button_green = ttk_new_button("Grn", set_color);
|
||||
ttk_position((ttk_object *)button_green, 106, 3, 100, 20);
|
||||
button_green->fill_color = rgb(0,255,0);
|
||||
|
||||
ttk_button * button_red = ttk_new_button("Red", set_color);
|
||||
ttk_position((ttk_object *)button_red, 209, 3, 100, 20);
|
||||
button_red->fill_color = rgb(255,0,0);
|
||||
|
||||
drawing_color = rgb(255,0,0);
|
||||
|
||||
ttk_render();
|
||||
|
||||
while (1) {
|
||||
w_keyboard_t * kbd = poll_keyboard();
|
||||
if (kbd != NULL) {
|
||||
@ -29,10 +147,10 @@ int main (int argc, char ** argv) {
|
||||
}
|
||||
w_mouse_t * mouse = poll_mouse();
|
||||
if (mouse != NULL) {
|
||||
if (mouse->buttons & MOUSE_BUTTON_LEFT) {
|
||||
if (mouse->command == WE_MOUSEMOVE && mouse->buttons & MOUSE_BUTTON_LEFT) {
|
||||
if (mouse->old_x >= 0 && mouse->new_x >= 0 && mouse->old_y >= 0 && mouse->new_y >= 0 &&
|
||||
mouse->old_x < width && mouse->new_x < width && mouse->old_y < width && mouse->new_y < width) {
|
||||
draw_line(mouse->old_x, mouse->new_x, mouse->old_y, mouse->new_y, rgb(255,0,0));
|
||||
draw_line(mouse->old_x, mouse->new_x, mouse->old_y, mouse->new_y, drawing_color);
|
||||
}
|
||||
}
|
||||
free(mouse);
|
||||
|
@ -47,6 +47,8 @@ static window_t * get_window (wid_t wid) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void (*mouse_action_callback)(w_mouse_t *) = NULL;
|
||||
|
||||
/* Window Object Management */
|
||||
|
||||
window_t * init_window (process_windows_t * pw, wid_t wid, int32_t x, int32_t y, uint16_t width, uint16_t height, uint16_t index) {
|
||||
@ -390,6 +392,9 @@ static void process_mouse_evt (uint8_t command, w_mouse_t * evt) {
|
||||
free(n->value);
|
||||
free(n);
|
||||
}
|
||||
if (mouse_action_callback) {
|
||||
mouse_action_callback(evt);
|
||||
}
|
||||
list_insert(mouse_evt_buffer, evt);
|
||||
//UNLOCK(mouse_evt_buffer_lock);
|
||||
}
|
||||
|
@ -183,5 +183,6 @@ void init_graphics_window(window_t * window);
|
||||
void init_graphics_window_double_buffer(window_t * window);
|
||||
|
||||
void win_use_threaded_handler();
|
||||
void (*mouse_action_callback)(w_mouse_t *);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user