Skip to content

Commit

Permalink
for #383 - image, svg, plot non-native widgets working
Browse files Browse the repository at this point in the history
* next up, textblocks and shapes
  • Loading branch information
Cecil committed Dec 9, 2017
1 parent 3230544 commit 0f7a253
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 67 deletions.
8 changes: 5 additions & 3 deletions Tests/events/event4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def click(btn,x,y,mods)
end
end

Shoes.app height: 700 do
Shoes.app height: 650 do
stack do
tagline "Clicks for non-native widgets"
flow do
Expand All @@ -31,7 +31,9 @@ def click(btn,x,y,mods)
# click only works on TimeSeries plots
@plt = plot 200, 200, title: "Test plot", chart: "timeseries"
@plt.click {|btn,x,y,mods| @eb.append "Plot clicked btn: #{btn} at #{x},#{y} with #{mods}\n"}

@btn = button "button" do
@eb.append "Button clicked\n"
end
#@tw = test_widget {|btn,x,y,mods| @eb.append "User Widget: #{btn} at #{x},#{y} with #{mods}\n"}
end
@eb = edit_box width: 500, height: 200
Expand All @@ -40,7 +42,7 @@ def click(btn,x,y,mods)
event do |evt|
$stderr.puts "event called: #{evt.type} at #{evt.x},#{evt.y} mods: #{evt.modifiers}"
if evt.object
$stderr.puts " for non-native: #{evt.object}"
$stderr.puts " for widget: #{evt.object} width #{evt.width} height #{evt.height}"
end
evt.accept = true #$ck.checked?
end
Expand Down
9 changes: 9 additions & 0 deletions shoes/plot/plot.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,15 @@ VALUE shoes_plot_send_click(VALUE self, int button, int x, int y) {
return v;
}

VALUE shoes_plot_event_is_here(VALUE self, int x, int y) {
shoes_plot *plot;
Data_Get_Struct(self, shoes_plot, plot);
if (IS_INSIDE(plot, x, y))
return Qtrue;
else
return Qnil;
}

// called by shoes_canvas_send_release
void shoes_plot_send_release(VALUE self, int button, int x, int y) {
GET_STRUCT(plot, self_t);
Expand Down
128 changes: 66 additions & 62 deletions shoes/types/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,19 @@ 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 ps;
VALUE ps_widget = shoes_event_find_psuedo (app->canvas, x, y);
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, s_click, Qnil, x, y, button, modifiers);
} else {
int w, h; // get from ?
evt = shoes_event_new_widget(cShoesEvent, s_click, ps_widget, button, x, y, w, h, modifiers);
// 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, s_click, nobj, button, x, y, w, h, modifiers);
}
return evt;
}
Expand All @@ -132,66 +137,65 @@ VALUE shoes_event_create_event(shoes_app *app, ID etype, int button, int x, int
* Beware all these non-widgets are canvas based! Return Qnil if no non-natives
* match the x,y. Return the matching non-native (not it's containing canvas/slot)
*/
VALUE shoes_event_find_psuedo (VALUE self, int x, int y) {
long i;
int ox = x, oy = y;
shoes_canvas *self_t;
Data_Get_Struct(self, shoes_canvas, self_t);
VALUE v = Qnil; // Returned object (svg,image, ...) a non native widget
if (ATTR(self_t->attr, hidden) != Qtrue) {
if (self_t->app->canvas == self) // when we are the app's slot
y -= self_t->slot->scrolly;

if (IS_INSIDE(self_t, x, y)) {
if (ORIGIN(self_t->place))
y += self_t->slot->scrolly;
for (i = RARRAY_LEN(self_t->contents) - 1; i >= 0; i--) {
VALUE ele = rb_ary_entry(self_t->contents, i);
if (rb_obj_is_kind_of(ele, cTextBlock)) {
fprintf(stderr, "found cTextblock\n");
//v = ele;
} else if (rb_obj_is_kind_of(ele, cImage)) {
// TODO: assumes image, has .place in the same spot as canvas (it does)
shoes_image *img;
Data_Get_Struct(ele, shoes_image, img);
if (IS_INSIDE(img, x, y)) {
fprintf(stderr, "found cImage at click\n");
return ele;
}
} else if (rb_obj_is_kind_of(ele, cSvg)) {
// TODO: assumes svg, has .place in the same spot as canvas (it does)
shoes_svg *svg;
Data_Get_Struct(ele, shoes_svg, svg);
if (IS_INSIDE(svg, x, y)) {
fprintf(stderr, "found cSvg at click\n");
return ele;
}
} else if (rb_obj_is_kind_of(ele, cPlot)) {
// TODO: assumes plot, has .place in the same spot as canvas (it does)
Data_Get_Struct(ele, shoes_canvas, self_t);
if (IS_INSIDE(self_t, x, y)) {
fprintf(stderr, "found cPlot at click\n");
return ele;
}
} else if (rb_obj_is_kind_of(ele, cShape)) {
fprintf(stderr, "found cShape\n");
//v = ele;
} else if (rb_obj_is_kind_of(ele, cCanvas)) {
shoes_canvas *cvs;
Data_Get_Struct(ele, shoes_canvas, cvs);
if (IS_INSIDE(cvs, x, y)) {
fprintf(stderr, "recurse canvas\n");
v = shoes_event_find_psuedo(ele, ox, oy);
}
} else {
v = Qnil;
VALUE shoes_event_find_psuedo (VALUE self, int x, int y, VALUE *hitobj) {
long i;
int ox = x, oy = y;
VALUE v = Qnil; // v is t/f, Qtrue/Qnil
shoes_canvas *self_t;
Data_Get_Struct(self, shoes_canvas, self_t);

if (ORIGIN(self_t->place)) {
oy = y + self_t->slot->scrolly;
ox = x - self_t->place.ix + self_t->place.dx;
oy = oy - (self_t->place.iy + self_t->place.dy);
if (oy < self_t->slot->scrolly || ox < 0 || oy > self_t->slot->scrolly + self_t->place.ih || ox > self_t->place.iw)
return Qnil;
}
if (ATTR(self_t->attr, hidden) != Qtrue) {
if (self_t->app->canvas == self) // when we are the app's slot
y -= self_t->slot->scrolly;

if (IS_INSIDE(self_t, x, y)) {
// TODO: something
VALUE click = ATTR(self_t->attr, click);
if (!NIL_P(click)) {
if (ORIGIN(self_t->place))
y += self_t->slot->scrolly;
//hoes_safe_block(self, click, rb_ary_new3(4, INT2NUM(button), INT2NUM(x), INT2NUM(y), mods));
}
}

for (i = RARRAY_LEN(self_t->contents) - 1; i >= 0; i--) {
VALUE ele = rb_ary_entry(self_t->contents, i);
if (rb_obj_is_kind_of(ele, cCanvas)) {
v = shoes_event_find_psuedo(ele, ox, oy, hitobj);
/*
} else if (rb_obj_is_kind_of(ele, cTextBlock)) {
v = shoes_textblock_send_click(ele, button, ox, oy, clicked);
*/
} else if (rb_obj_is_kind_of(ele, cImage)) {
v = shoes_image_event_is_here(ele, ox, oy);
*hitobj = ele;
} else if (rb_obj_is_kind_of(ele, cSvg)) {
v = shoes_svg_event_is_here(ele, ox, oy);
*hitobj = ele;

} else if (rb_obj_is_kind_of(ele, cPlot)) {
v = shoes_plot_event_is_here(ele, ox, oy);
*hitobj = ele;
/*
} else if (rb_obj_is_kind_of(ele, cShape)) {
v = shoes_shape_send_click(ele, button, ox, oy);
*clicked = ele;
*/
}

if (!NIL_P(v))
return v;
}
if (! NIL_P(v))
return v;
}
}
}
return Qnil;

return Qnil;
}


Expand Down
2 changes: 1 addition & 1 deletion shoes/types/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ VALUE shoes_event_create_event(shoes_app *app, ID action, int button, int x, int
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 shoes_event_find_psuedo (VALUE self, int x, int y);
VALUE shoes_event_find_psuedo (VALUE self, int x, int y, VALUE *pswidget);
#endif
10 changes: 10 additions & 0 deletions shoes/types/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,16 @@ VALUE shoes_image_send_click(VALUE self, int button, int x, int y) {
return v;
}

// Return t/f if x,y points to the image
VALUE shoes_image_event_is_here(VALUE self, int x, int y) {
shoes_image *img;
Data_Get_Struct(self, shoes_image, img);
if (IS_INSIDE(img, x, y))
return Qtrue;
else
return Qnil;
}

void shoes_image_send_release(VALUE self, int button, int x, int y) {
GET_STRUCT(image, self_t);
if (button > 0 && (self_t->hover & HOVER_CLICK)) {
Expand Down
10 changes: 9 additions & 1 deletion shoes/types/svg.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,14 @@ void shoes_svg_send_release(VALUE self, int button, int x, int y) {
shoes_safe_block(self, proc, rb_ary_new3(3, INT2NUM(button), INT2NUM(x), INT2NUM(y)));
}
}
VALUE shoes_svg_event_is_here(VALUE self, int x, int y) {
shoes_svg *svg;
Data_Get_Struct(self, shoes_svg, svg);
if (IS_INSIDE(svg, x, y))
return Qtrue;
else
return Qnil;
}

// svghandle
void shoes_svghandle_mark(shoes_svghandle *handle) {
Expand Down Expand Up @@ -741,4 +749,4 @@ VALUE shoes_svghandle_has_group(VALUE self, VALUE group) {
} else {
rb_raise(rb_eArgError, "bad argument, expecting a String \n");
}
}
}

0 comments on commit 0f7a253

Please sign in to comment.