require 'fltk' class Point < FLTK::Box def initialize(parent,x,y) super(x,y,10,10) @parent = parent @enter = false end def draw super text = "(#{x},#{y})" if( @enter ) @parent.color(FLTK::RED) else @parent.color(FLTK::BLACK) end @parent.text_draw(text, x, y) end def handle(e) case e when FLTK::ENTER @parent.mark_item(self) @enter = true @parent.redraw return true when FLTK::LEAVE @parent.mark_item(nil) @enter = false @parent.redraw return true else return false end end end class Line def initialize(parent, x1, y1, x2, y2) @parent = parent box1 = Point.new(parent,x1,y1) box2 = Point.new(parent,x2,y2) @corner = [box1,box2] add_corner((x1 + x2) / 2, (y1 + y2) / 2) end def add_corner(x, y) box = Point.new(@parent,x,y) @corner[1,0] = box end def draw for i in 0..(@corner.length - 2) box1 = @corner[i] box2 = @corner[i+1] box1.redraw box2.redraw @parent.line(box1.x, box1.y , box2.x, box2.y) end end end class MyCanvas < FLTK::Widget include FLTK::Drawable def initialize(label) super(400,400,label) @cur = nil @lines = [ Line.new(self, 100, 100, 200, 200), Line.new(self, 100, 200, 200, 300), Line.new(self, 200, 100, 300, 200), ] end def draw super() clear color(FLTK::BLACK) @lines.each{|line| line.draw} end def handle(e) case e when FLTK::PUSH return true when FLTK::DRAG if( @cur ) @cur.position(FLTK.event_x, FLTK.event_y) end redraw return true else return false end end def mark_item(obj) print "mark_item(#{obj.inspect})\n" @cur = obj end end class MyWindow < FLTK::Window def initialize(label) super(400,400,label) @canvas = MyCanvas.new("#{label} Canvas") end end w = MyWindow.new("MyWindow") w.show FLTK.run