indexing description: "[ Representation of a rectangle ]" date: "$Date$" revision: "$Revision$" class RECTANGLE inherit OBJECT undefine copy, is_equal, out, default_create redefine set_true_x_y end EM_RECTANGLE rename move_by as move_by_coords end create make_obj feature -- Access make_obj (v1, v2: EM_VECTOR_2D; a_scene: SCENE) is -- Make rectangle with top left and bottom right corner require v1_not_void: v1 /= Void v2_not_void: v2 /= Void a_scene_not_void: a_scene /= Void do make (v1, v2) true_x := x true_y := y create true_point_a.make_from_other (v1) create true_point_b.make_from_other (v2) create collidable.make (v1, v2) scene := a_scene scene.collision_detector.add (current) collidable.set_holder (current) -- set vertexes create top_left.make (v1, current) create top_right.make (v2, current) create bottom_left.make (v1, current) create bottom_right.make (v2, current) set_changed ensure scene_set: scene = a_scene end make_obj_notcreate (v1, v2: EM_VECTOR_2D) is -- Make rectangle with top left and bottom right corner require v1_not_void: v1 /= Void v2_not_void: v2 /= Void do set_new_true_size (v1, v2) create collidable.make (v1, v2) scene.collision_detector.add (current) collidable.set_holder (current) -- set vertexes create top_left.make (v1, current) create top_right.make (v2, current) create bottom_left.make (v1, current) create bottom_right.make (v2, current) set_changed end collidable: EM_RECTANGLE_COLLIDABLE -- returns a collidable rectangle top_left, top_right, bottom_left, bottom_right: RECTANGLE_VERTEX -- corner vertexes true_point_a, true_point_b: EM_VECTOR_2D -- true corners vertex_list: DS_LINKED_LIST [VERTEX] is -- a list of vertexes that the object has do Create Result.make Result.force_last (top_left) Result.force_last (top_right) Result.force_last (bottom_left) Result.force_last (bottom_right) end feature -- Element Change move_by (an_x, a_y: DOUBLE) is -- sets `x' and `y'. local v: EM_VECTOR_2D do create v.make (an_x / scene.scale, a_y / scene.scale) move_by_coords (v) end set_true_x_y (an_x, a_y: DOUBLE) is -- sets `true_x' and `true_y' local v: EM_VECTOR_2D do true_x := an_x true_y := a_y create v.make (true_x - x, true_y - y) point_a := point_a + v point_b := point_b + v process_changes x := true_x.rounded y := true_y.rounded true_point_a.set_x (true_x) true_point_a.set_y (true_y) true_point_b.set_x (true_x + (point_a.x - point_b.x).abs) true_point_b.set_y (true_y + (point_a.y - point_b.y).abs) set_changed end set_new_true_size (v1, v2: EM_VECTOR_2D) is -- Set new true size. do point_a := v1 point_b := v2 process_changes true_x := point_a.x true_y := point_a.y x := true_x.rounded y := true_y.rounded true_point_a.set_x (true_x) true_point_a.set_y (true_y) true_point_b.set_x (true_x + (point_a.x - point_b.x).abs) true_point_b.set_y (true_y + (point_a.y - point_b.y).abs) set_changed end set_new_size (v1, v2: EM_VECTOR_2D) is -- Set new size. do point_a := v1 point_b := v2 process_changes set_changed end feature -- Computation set_changed is -- update vertexes local l, r, t, b: DOUBLE do -- update vertices l := point_a.x r := point_b.x t := point_a.y b := point_b.y top_left.set_vector (scene.screen_position_from_coordinates (l, t)) top_right.set_vector (scene.screen_position_from_coordinates (r, t)) bottom_left.set_vector (scene.screen_position_from_coordinates (l, b)) bottom_right.set_vector (scene.screen_position_from_coordinates (r, b)) end set_vertexes_updated is -- update state from vertexes do if top_left.is_selected then -- top left vertex is selected point_a := scene.coordinates_from_screen_position (top_left.vector.x, top_left.vector.y) point_b := collidable.corner_bottom_right elseif bottom_right.is_selected then -- bottom right vertex is selected point_a := collidable.corner_top_left point_b := scene.coordinates_from_screen_position (bottom_right.vector.x, bottom_right.vector.y) elseif top_right.is_selected then -- top right vertex is selected point_a := scene.coordinates_from_screen_position (top_left.vector.x, top_right.vector.y) point_b := scene.coordinates_from_screen_position (top_right.vector.x, bottom_right.vector.y) elseif bottom_left.is_selected then -- bottom left vertex is selected point_a := scene.coordinates_from_screen_position (bottom_left.vector.x, top_left.vector.y) point_b := scene.coordinates_from_screen_position (bottom_right.vector.x, bottom_left.vector.y) end process_changes -- set_changed set_changed end recreate_collidable is -- creacreate `collidable'. do collidable.set_center (center) collidable.set_width ((point_a.x - point_b.x).abs) collidable.set_height ((point_a.y - point_b.y).abs) end end