require 'tk' class TkdControl def initialize( canvas ) @canvas = canvas @item = nil @coords = [] @x0, @y0 = 0, 0 end attr_writer :item def get_coords @coords = [] a = @item.coords while( a != []) x = a.shift y = a.shift @coords.push( [x, y] ) end @coords end def set_coords eval( "@item.coords(" + @coords.flatten.join(',') + ")" ) end def make_control @coords.each_index{|i| x, y = @coords[i][0], @coords[i][1] cont = TkcRectangle.new(@canvas, x-2, y-2, x+2, y+2, 'fill'=>'white') cont.addtag('control') cont.addtag( i ) } end def remove_control @canvas.delete('control') end def plotDown(x, y) @canvas.dtag 'selected' @canvas.addtag_withtag 'selected', 'current' @canvas.raise 'current' @x0, @y0 = x, y end def plotMove(x, y) @canvas.move 'selected', x-@x0, y-@y0 @x0, @y0 = x, y i = @canvas.find_withtag('selected').shift.gettags[1] @coords[i][0], @coords[i][1] = x, y set_coords end def bind @canvas.bind('1', proc{}) @canvas.bind('B1-Motion', proc{}) @canvas.itembind('control', '1', proc{|x,y| plotDown x,y }, "%x %y" ) @canvas.itembind('control', 'B1-Motion', proc{|x,y| plotMove x, y }, "%x %y") end def get_item( item ) @item = item get_coords make_control bind end def get_item_with_cursor item = @canvas.find_withtag('current').shift get_item( item ) end end c = TkCanvas.new.pack cont = TkdControl.new( c ) item = TkcLine.new(c, 50, 50, 100, 150, 150, 100, 'width'=>2) cont.get_item( item ) Tk.mainloop