require 'tk' class TkMove def initialize( canvas ) @x0, @y0 = 0, 0 @canvas = canvas @tag = 'item' end def bind @canvas.itembind( @tag, '1', proc{|x, y| plotDown x, y},"%x %y") @canvas.itembind( @tag, 'B1-Motion', proc{|x, y| plotMove x, y},"%x %y") end def unbind @canvas.itembind( @tag, '1', proc{} ) @canvas.itembind( @tag, 'B1-Motion', proc{} ) 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 end end class TkDraw def initialize( canvas ) @canvas = canvas @tag = 'item' @type = 'default' @x0, @y0 = 0, 0 end def bind @canvas.bind('1', proc{|x, y| create_item(x, y)}, "%x %y") @canvas.bind('B1-Motion', proc{|x, y| adjust_item(x, y)}, "%x %y") end def unbind @canvas.bind('1', proc{}) @canvas.bind('B1-Motion', proc{}) end def create_item(x, y) @item = new_item(x, y) @item.addtag( @tag ) @canvas.dtag 'selected' @canvas.addtag_withtag 'selected', @item.id @x0, @y0 = x, y end def new_item(x, y) 'not defined' end def adjust_item(x, y) @item.coords( @x0, @y0, x, y ) end end class TkdLine < TkDraw def initialize( canvas ) super @type = 'line' end def new_item(x, y) TkcLine.new( @canvas, x, y, x, y, 'width'=>2 ) end end class TkdRectangle < TkDraw def initialize( canvas ) super @type = 'rectangle' end def new_item(x, y) TkcRectangle.new( @canvas, x, y, x, y, 'fill'=>'white' ) end end class TkdOval < TkDraw def initialize( canvas ) super @type = 'oval' end def new_item(x, y) TkcOval.new( @canvas, x, y, x, y, 'fill'=>'red' ) end end class TkDelete def initialize( canvas ) @canvas = canvas @tag = 'item' end def bind @canvas.itembind( @tag, '1', proc{ delete } ) @canvas.itembind( @tag, 'B1-Motion', proc{} ) end def unbind @canvas.itembind( @tag, '1', proc{} ) @canvas.itembind( @tag, 'B1-Motion', proc{} ) end def delete @canvas.delete('current') end end # main routine # c = TkCanvas.new.pack m = TkMove.new( c ) l = TkdLine.new( c ) r = TkdRectangle.new( c ) o = TkdOval.new( c ) d = TkDelete.new( c ) TkFrame.new{|f| TkButton.new(f, 'text'=>'Lin', 'command'=>proc{m.unbind; l.bind}).pack('side'=>'left') TkButton.new(f, 'text'=>'Rct', 'command'=>proc{m.unbind; r.bind}).pack('side'=>'left') TkButton.new(f, 'text'=>'Ovl', 'command'=>proc{m.unbind; o.bind}).pack('side'=>'left') TkButton.new(f, 'text'=>'Mv', 'command'=>proc{l.unbind; m.bind}).pack('side'=>'left') TkButton.new(f, 'text'=>'Del', 'command'=>proc{l.unbind; m.unbind; d.bind}).pack('side'=>'left') TkButton.new(f, 'text'=>'Bye', 'command'=>proc{exit}).pack('side'=>'left') }.pack Tk.mainloop