require 'tkmove4' require 'tkdrawcom.rb' def color(c) color = Tk.chooseColor break if color == '' items = c.find_withtag('item') items.each{|itm| itm.fill( color )} end def instruction(c) TkcText.new(c, 50, 300, 'text'=>'Button-1: Move the piece with drugging', 'anchor'=>'w') TkcText.new(c, 50, 320, 'text'=>'Button-2: Rotate the piece with drugging', 'anchor'=>'w') TkcText.new(c, 50, 340, 'text'=>'Button-3: Reverse the piece with a click', 'anchor'=>'w') end def tangram( c ) x1, x2, x3, x4, x5 = 50, 100, 150, 200, 250 y1, y2, y3, y4, y5 = 50, 100, 150, 200, 250 t1 = TkcPolygon.new(c, x1, y1, x3, y1, x1, y3, 'width'=>2, 'outline'=>'black', 'fill'=>'orange') t1.addtag('item'); t1.addtag( t1.id ) t2 = TkcPolygon.new(c, x3, y1, x2, y2, x3, y3, x4, y2, 'width'=>2, 'outline'=>'black', 'fill'=>'orange') t2.addtag('item'); t2.addtag( t2.id ) t3 = TkcPolygon.new(c, x3, y1, x5, y1, x4, y2, 'width'=>2, 'outline'=>'black', 'fill'=>'orange') t3.addtag('item'); t3.addtag( t3.id ) t5 = TkcPolygon.new(c, x1, y3, x1, y5, x2, y4, x2, y2, 'width'=>2, 'outline'=>'black', 'fill'=>'orange') t5.addtag('item'); t5.addtag( t5.id ) t6 = TkcPolygon.new(c, x2, y2, x2, y4, x3, y3, 'width'=>2, 'outline'=>'black', 'fill'=>'orange') t6.addtag('item'); t6.addtag( t6.id ) t8 = TkcPolygon.new(c, x5, y1, x3, y3, x5, y5, 'width'=>2, 'outline'=>'black', 'fill'=>'orange') t8.addtag('item'); t8.addtag( t8.id ) t9 = TkcPolygon.new(c, x1, y5, x3, y3, x5, y5, 'width'=>2, 'outline'=>'black', 'fill'=>'orange') t9.addtag('item'); t9.addtag( t9.id ) end c = TkCanvas.new(nil, 'width'=>600, 'height'=>400, 'bg'=>'white') m = TkMove.new( c ) g = TkdGroup.new( c ) cl = TkdColor.new( c ) rot = TkdRotate.new( c ) mirr = TkdMirror.new( c ) menu_spec = [ [ ['File',0], ['Color', proc{ color(c) }], ['Exit', proc{ exit }] ] ] mb = TkMenubar.new(nil, menu_spec, 'tearoff'=>false).pack('fill'=>'x', 'side'=>'top') def rot.bind @canvas.itembind('item', '2', proc{|x, y| button_down(x, y)}, "%x %y") @canvas.itembind('item', 'B2-Motion', proc{|x, y| button_motion(x, y)}, "%x %y") end def mirr.bind @canvas.itembind('item', '3', proc{ transform_at_cursor( mirror ) }) end m.bind rot.bind mirr.bind TkRoot.new.title('Tangram') TkRoot.new.cursor('hand2') instruction(c) tangram(c) c.pack Tk.mainloop