Skip to content

Commit

Permalink
for #383 keyup, keydown, motion, release, wheel events
Browse files Browse the repository at this point in the history
* can be sent to the event block (if there is one)
* clean up the C code a bit.
  • Loading branch information
Cecil committed Dec 11, 2017
1 parent bdc03c0 commit 434b8dc
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 31 deletions.
10 changes: 10 additions & 0 deletions Tests/events/event6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
when :keyup
$stderr.puts "keyup for #{evt.key}"
evt.accept = $ck.checked?
when :motion
evt.accept = false
when :release
evt.accept = false
when :wheel
$stderr.puts "wheel handler called: #{evt.type} #{evt.button}, #{evt.x} #{evt.y} #{evt.modifiers}"
evt.accept = true
else
puts "Other: #{evt.type.inspect}"
evt.accept = true
Expand All @@ -38,4 +45,7 @@
keydown do |key|
@eb.append "down: #{key}\n"
end
wheel do |d, x, y, mods|
@eb.append "wheel dir: #{d} at #{x},#{y}, with #{mods}\n"
end
end
79 changes: 64 additions & 15 deletions shoes/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,11 @@ VALUE shoes_app_set_event_handler(VALUE self, VALUE block) {
}



shoes_code shoes_app_motion(shoes_app *app, int x, int y, int mods) {
app->mousex = x;
app->mousey = y;
VALUE sendevt = Qtrue;
VALUE modifiers = Qnil;
if (mods & SHOES_MODIFY_CTRL) {
if (mods & SHOES_MODIFY_SHIFT)
Expand All @@ -566,7 +568,24 @@ shoes_code shoes_app_motion(shoes_app *app, int x, int y, int mods) {
}
else if (mods & SHOES_MODIFY_SHIFT)
modifiers = rb_utf8_str_new_cstr("shift");
shoes_canvas_send_motion(app->canvas, x, y, Qnil, modifiers);

if (app->use_event_handler) {
VALUE evt = shoes_event_create_event(app, s_motion, 0, x, y, modifiers, Qnil);
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
VALUE event = ATTR(canvas->attr, event);
if (! NIL_P(event)) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = shoes_event_contrain_TF(tevent->accept);
} else {
fprintf(stderr, "click: don't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
}
}
if (sendevt == Qtrue)
shoes_canvas_send_motion(app->canvas, x, y, Qnil, modifiers);
return SHOES_OK;
}
EXTERN ID s_shift_key, s_control_key;
Expand All @@ -583,12 +602,7 @@ shoes_code shoes_app_click(shoes_app *app, int button, int x, int y, int mods) {
}
else if (mods & SHOES_MODIFY_SHIFT)
modifiers = rb_utf8_str_new_cstr("shift");
/*
if (mods & SHOES_MODIFY_ALT)
rb_ary_push(modifiers, rb_str_new_cstr("alt"));
if (mods & SHOES_MODIFY_META)
rb_ary_push(modifiers, rb_str_new_cstr("meta"));
*/

if (app->use_event_handler) {
VALUE evt = shoes_event_create_event(app, s_click, button, x, y, modifiers, Qnil);
shoes_canvas *canvas;
Expand All @@ -598,7 +612,7 @@ shoes_code shoes_app_click(shoes_app *app, int button, int x, int y, int mods) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = (tevent->accept == 1) ? Qtrue : Qfalse;
sendevt = shoes_event_contrain_TF(tevent->accept);
} else {
fprintf(stderr, "click: don't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
Expand All @@ -612,6 +626,7 @@ shoes_code shoes_app_click(shoes_app *app, int button, int x, int y, int mods) {
shoes_code shoes_app_release(shoes_app *app, int button, int x, int y, int mods) {
app->mouseb = 0;
VALUE modifiers = Qnil;
VALUE sendevt = Qtrue;
if (mods & SHOES_MODIFY_CTRL) {
if (mods & SHOES_MODIFY_SHIFT)
modifiers = rb_utf8_str_new_cstr("control_shift");
Expand All @@ -620,15 +635,33 @@ shoes_code shoes_app_release(shoes_app *app, int button, int x, int y, int mods)
}
else if (mods & SHOES_MODIFY_SHIFT)
modifiers = rb_utf8_str_new_cstr("shift");
shoes_canvas_send_release(app->canvas, button, x, y, modifiers);

if (app->use_event_handler) {
VALUE evt = shoes_event_create_event(app, s_release, button, x, y, modifiers, Qnil);
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
VALUE event = ATTR(canvas->attr, event);
if (! NIL_P(event)) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = shoes_event_contrain_TF(tevent->accept);
} else {
fprintf(stderr, "release: doen't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
}
}
if (sendevt == Qtrue)
shoes_canvas_send_release(app->canvas, button, x, y, modifiers);
return SHOES_OK;
}

shoes_code shoes_app_wheel(shoes_app *app, ID dir, int x, int y, int mods) {
VALUE sendevt = Qtrue;
VALUE modifiers = Qnil;
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
if (canvas->slot->vscroll) shoes_canvas_wheel_way(canvas, dir);
VALUE modifiers = Qnil;
if (mods & SHOES_MODIFY_CTRL) {
if (mods & SHOES_MODIFY_SHIFT)
modifiers = rb_utf8_str_new_cstr("control_shift");
Expand All @@ -637,7 +670,24 @@ shoes_code shoes_app_wheel(shoes_app *app, ID dir, int x, int y, int mods) {
}
else if (mods & SHOES_MODIFY_SHIFT)
modifiers = rb_utf8_str_new_cstr("shift");
shoes_canvas_send_wheel(app->canvas, dir, x, y, modifiers) ;

if (app->use_event_handler) {
VALUE evt = shoes_event_create_event(app, s_wheel, (dir == s_up), x, y, modifiers, Qnil);
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
VALUE event = ATTR(canvas->attr, event);
if (! NIL_P(event)) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = shoes_event_contrain_TF(tevent->accept);
} else {
fprintf(stderr, "wheel: doen't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
}
}
if (sendevt == Qtrue)
shoes_canvas_send_wheel(app->canvas, dir, x, y, modifiers) ;
return SHOES_OK;
}

Expand All @@ -655,7 +705,6 @@ shoes_code shoes_app_keypress(shoes_app *app, VALUE key) {
rb_eval_string("Shoes.remote_debug");
else {
if (app->use_event_handler) {
VALUE evt = shoes_event_create_event(app, s_click, button, x, y, modifiers, key);
VALUE evt = shoes_event_new_key(cShoesEvent, s_keypress, key);
shoes_canvas *canvas;
Data_Get_Struct(app->canvas, shoes_canvas, canvas);
Expand All @@ -664,7 +713,7 @@ shoes_code shoes_app_keypress(shoes_app *app, VALUE key) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = (tevent->accept == 1) ? Qtrue : Qfalse;
sendevt = shoes_event_contrain_TF(tevent->accept);
} else {
fprintf(stderr, "keypress: doen't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
Expand All @@ -689,7 +738,7 @@ shoes_code shoes_app_keydown(shoes_app *app, VALUE key) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = (tevent->accept == 1) ? Qtrue : Qfalse;
sendevt = shoes_event_contrain_TF(tevent->accept);
} else {
fprintf(stderr, "keydown: doen't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
Expand All @@ -715,7 +764,7 @@ shoes_code shoes_app_keyup(shoes_app *app, VALUE key) {
shoes_safe_block(app->canvas, event, rb_ary_new3(1, evt));
shoes_event *tevent;
Data_Get_Struct(evt, shoes_event, tevent);
sendevt = (tevent->accept == 1) ? Qtrue : Qfalse;
sendevt = shoes_event_contrain_TF(tevent->accept);
} else {
fprintf(stderr, "keyup: doen't have event - but should - dump hash\n");
shoes_hash_debug(canvas->attr);
Expand Down
35 changes: 22 additions & 13 deletions shoes/types/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ void shoes_event_mark(shoes_event *event) {
rb_gc_mark_maybe(event->type);
rb_gc_mark_maybe(event->object);
rb_gc_mark_maybe(event->key);
rb_gc_mark_maybe(event->modifiers);
rb_gc_mark_maybe(event->accept);
rb_gc_mark_maybe(event->modifiers);
}

void shoes_event_free(shoes_event *event) {
Expand All @@ -38,27 +39,28 @@ void shoes_event_free(shoes_event *event) {

// users should not create events but something has to be visible.
// click calls here.
VALUE shoes_event_new(VALUE klass, ID type, VALUE widget, int x, int y, int btn, VALUE mods) {
VALUE shoes_event_new(VALUE klass, ID type, VALUE widget, int x, int y, int btn, VALUE mods, VALUE key) {
shoes_event *event;
VALUE obj = shoes_event_alloc(klass);
Data_Get_Struct(obj, shoes_event, event);
event->accept = 1;
event->accept = Qtrue;
event->type = type;
event->object = widget;
event->x = x;
event->y = y;
event->btn = btn;
event->key = Qnil;
event->modifiers = mods;
event->key = key;
return obj;
}
// Or here
VALUE shoes_event_new_widget(VALUE klass, ID type, VALUE widget, int btn, int x,
int y, int w, int h, VALUE modifiers) {
int y, int w, int h, VALUE modifiers, VALUE key) {
shoes_event *event;
VALUE obj = shoes_event_alloc(klass);
Data_Get_Struct(obj, shoes_event, event);
event->accept = 1;
event->accept = Qtrue;
event->type = type;
event->object = widget;
event->btn = btn;
Expand All @@ -75,7 +77,7 @@ VALUE shoes_event_new_key(VALUE klass, ID type, VALUE key) {
shoes_event *event;
VALUE obj = shoes_event_alloc(klass);
Data_Get_Struct(obj, shoes_event, event);
event->accept = 1;
event->accept = Qtrue;
event->type = type;
event->object = Qnil;
event->btn = 0;
Expand Down Expand Up @@ -120,7 +122,7 @@ VALUE shoes_canvas_shoesevent(int argc, VALUE *argv, VALUE self) {
int x = NUM2INT(shoes_hash_get(hsh, rb_intern("x")));
int y = NUM2INT(shoes_hash_get(hsh, rb_intern("y")));
int btn = NUM2INT(shoes_hash_get(hsh, rb_intern("button")));
ev = shoes_event_new(cShoesEvent,type,obj,x,y,btn,Qnil);
ev = shoes_event_new(cShoesEvent,type,obj,x,y,btn,Qnil,Qnil);
return ev;
}

Expand All @@ -130,20 +132,20 @@ VALUE shoes_canvas_shoesevent(int argc, VALUE *argv, VALUE self) {
*/
extern ID cTextBlock, cImage, cShape, cCanvas;

VALUE shoes_event_create_event(shoes_app *app, ID etype, int button, int x, int y, VALUE modifiers) {
VALUE shoes_event_create_event(shoes_app *app, ID etype, int button, int x, int y, VALUE modifiers,VALUE key) {
VALUE nobj;
VALUE ps_widget = shoes_event_find_psuedo (app->canvas, x, y, &nobj);
VALUE evt;
if (NIL_P(ps_widget)) {
evt = shoes_event_new(cShoesEvent, etype, Qnil, x, y, button, modifiers);
evt = shoes_event_new(cShoesEvent, etype, Qnil, x, y, button, modifiers,key);
} else {
// TODO: ASSUME all the ps_widget have the canvas place
int w,h;
shoes_canvas *cvs;
Data_Get_Struct(nobj, shoes_canvas, cvs);
w = cvs->place.w;
h = cvs->place.h;
evt = shoes_event_new_widget(cShoesEvent, etype, nobj, button, x, y, w, h, modifiers, );
evt = shoes_event_new_widget(cShoesEvent, etype, nobj, button, x, y, w, h, modifiers, key);
}
return evt;
}
Expand Down Expand Up @@ -232,12 +234,13 @@ VALUE shoes_event_object(VALUE self) {
VALUE shoes_event_get_accept(VALUE self) {
shoes_event *event;
Data_Get_Struct(self, shoes_event, event);
return (event->accept) ? Qtrue : Qfalse;
return event->accept;
}

VALUE shoes_event_set_accept(VALUE self, VALUE tf) {
shoes_event *event;
Data_Get_Struct(self, shoes_event, event);
event->accept = (NIL_P(tf) || tf == Qfalse ) ? 0 : 1;
event->accept = tf;
return tf;
}
VALUE shoes_event_button(VALUE self) {
Expand Down Expand Up @@ -267,7 +270,6 @@ VALUE shoes_event_width(VALUE self) {
return INT2NUM(event->width);
}

// TODO keys: strings, not ints. Beware UTF8
VALUE shoes_event_key(VALUE self) {
shoes_event *event;
Data_Get_Struct(self, shoes_event, event);
Expand All @@ -283,3 +285,10 @@ VALUE shoes_event_modifiers(VALUE self) {
Data_Get_Struct(self, shoes_event, event);
return event->modifiers;
}

// returns Qtrue or Qfalse
VALUE shoes_event_contrain_TF(VALUE val) {
if (NIL_P(val) || val == Qfalse)
return Qfalse;
return Qtrue;
}
8 changes: 5 additions & 3 deletions shoes/types/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extern shoes_app _shoes_app;
typedef struct {
VALUE type;
VALUE object;
int accept;
VALUE accept;
int btn;
int x;
int y;
Expand Down Expand Up @@ -51,12 +51,14 @@ VALUE shoes_event_modifiers(VALUE self);
void shoes_event_mark(shoes_event *event);
void shoes_event_free(shoes_event *event);
VALUE shoes_event_new(VALUE klass, ID type, VALUE widget, int x, int y,
int btn, VALUE modifiers);
int btn, VALUE modifiers, VALUE key);
VALUE shoes_event_alloc(VALUE klass);
VALUE shoes_event_create_event(shoes_app *app, ID action, int button, int x, int y, VALUE modifiers);
VALUE shoes_event_create_event(shoes_app *app, ID action, int button, int x, int y, VALUE modifiers, VALUE key);
VALUE shoes_canvas_shoesevent(int argc, VALUE *argv, VALUE self);
VALUE shoes_event_new_widget(VALUE klass, ID type, VALUE widget, int btn, int x, \
int y, int w, int h, VALUE modifiers, VALUE key);
VALUE shoes_event_find_psuedo (VALUE self, int x, int y, VALUE *pswidget);
VALUE shoes_event_new_key(VALUE klass, ID type, VALUE key);
// helper
VALUE shoes_event_contrain_TF(VALUE evt);
#endif

0 comments on commit 434b8dc

Please sign in to comment.