indexing description: "[ Create Polygon Tool ]" date: "$Date$" revision: "$Revision$" class CREATE_POLYGON_TOOL inherit TOOL create make feature -- Access cursor: MOUSE_CURSOR is -- appropriate cursor. do if not is_mouse_over_closing_polygon then Result := arrow_cursor else Result := polygon_close_cursor end end feature -- Event Handling handle_mouse_button_down_event (event: EM_MOUSEBUTTON_EVENT) is -- Handle mouse button down event. do if scene.focus_on_scene and event.is_left_button then drag_start_coords := scene.coordinates_from_screen_position (x, y) is_pressed := True end end handle_mouse_button_up_event (event: EM_MOUSEBUTTON_EVENT) is -- Handle mouse button up event. local v: EM_VECTOR_2D do if event.is_left_button then is_pressed := False end if event.is_left_button and drag_start_coords /= Void then -- add point if `polygon' is not void, otherwise, create polygon v := scene.coordinates_from_screen_position (x, y) if polygon = Void then -- create new polygon create polygon.make_obj (v, scene) polygon.extend (create {EM_VECTOR_2D}.make_from_other (v)) scene.polygons.force_last (polygon) elseif is_mouse_over_closing_polygon then -- closepolygon if polygon.count > 3 then if polygon.count > 0 then polygon.remove_last end scene.history.execute (create {CREATE_POLYGON}.make, [scene.polygons, polygon]) if scene.history.execute_successful then scene.set_changed end polygon := Void end else -- add point polygon.set_changed polygon.extend (v) end end drag_start_coords := Void end handle_mouse_motion_event (event: EM_MOUSEMOTION_EVENT) is -- Handle mouse motion event. local coords: EM_VECTOR_2D do x := event.screen_x y := event.screen_y - 32 if polygon /= Void and then polygon.count > 0 then -- update the last position of the polygon coords := scene.coordinates_from_screen_position (x, y) polygon.last.make_from_other (coords) end last_x := x last_y := y end handle_key_down_event (event: EM_KEYBOARD_EVENT) is -- Handle key down event. do end handle_key_up_event (event: EM_KEYBOARD_EVENT) is -- Handle key up event. do end is_mouse_over_closing_polygon: BOOLEAN is -- is the mouse about to close the polygon? do if scene.focus_on_scene and then polygon /= Void then polygon.finish polygon.back Result := (polygon.item_for_iteration.distance (scene.coordinates_from_screen_position (x, y)) * scene.scale < distance_to_point_to_close or polygon.first.distance (scene.coordinates_from_screen_position (x, y)) * scene.scale < distance_to_point_to_close) end end feature {NONE} -- Implementation drag_start_coords: EM_VECTOR_2D -- where the mouse started dragging polygon: POLYGON -- current polygon (if not void it is just beeing created upon release) distance_to_point_to_close: INTEGER is 3 -- the distance at which the point is detected as the same, for closing the polygon (in pixels) end