indexing description: "[ Scene used to demonstrate mouse events over drawable objects, figures and zoomable container. ]" date: "$Date$" revision: "$Revision$" class FIRST_SCENE inherit EM_DRAWABLE_SCENE redefine initialize_scene end EM_SHARED_STANDARD_FONTS export {NONE} all end EM_SHARED_COLORS export {NONE} all end EM_SHARED_BITMAP_FACTORY export {NONE} all end EM_SHARED_ERROR_HANDLER export {NONE} all end create make_scene feature -- Initialization initialize_scene is -- Initialize the scene. local font: EM_FONT animation: EM_ANIMATION string: EM_STRING string2: EM_STRING image: EM_BITMAP sprite: EM_SPRITE point, first_edge: EM_VECTOR_2D rectangle: EM_RECTANGLE circle: EM_CIRCLE polygon: EM_POLYGON polyline: EM_POLYLINE do set_frame_counter_visibility (true) -- Create background color (dark blue). create background_color.make_with_rgb (0, 0, 100) -- Create black rectangle as background for zoomable widget. create rectangle.make_from_coordinates (x, y, x + width, y + height) rectangle.set_fill_color (black) main_container.extend (rectangle) -- Create zoomable widget and add to `main_container'. create zoomable_widget.make (width, height) zoomable_widget.set_x_y (x, y) main_container.extend (zoomable_widget) -- Add a picture to zoomable widget. Bitmap_factory.create_bitmap_from_image ("./pics/world.gif") image := bitmap_factory.last_bitmap image.set_x_y (200, 80) zoomable_widget.extend (image) -- Add some simple sprite containing an animation and animate it. create animation.make_from_file ("./pics/em_logo.anim") create sprite.make_from_animation (animation, 80, 100) sprite.set_frame_rate (25) sprite.set_do_loop (True) sprite.start zoomable_widget.extend (sprite) -- Add some simple text. font := standard_bmp_fonts.medium_vera_font create string.make ("Hello World!", font) string.set_x_y (50, 300) zoomable_widget.extend (string) -- Add a circle. create point.make (100, 500) create circle.make (point, 50) circle.set_fill_color (blue) zoomable_widget.extend (circle) -- Add a green rectangle with rounded corners and a yellow border. create rectangle.make_from_coordinates (200, 450, 250, 550) rectangle.set_fill_color (green) rectangle.set_line_color (yellow) rectangle.set_line_width (5) zoomable_widget.extend (rectangle) -- Add a regular polygon. create point.make (350, 450) create first_edge.make (20, 0) create polygon.make_regular (8, point, first_edge) polygon.set_fill_color (red) zoomable_widget.extend (polygon) -- Add another, not-visible, text create string2.make ("Moo!", font) string2.set_x_y (300, 460) string2.set_visible (False) zoomable_widget.extend (string2) -- Add a little polygon line. create polyline.make_empty create point.make (500, 300) polyline.extend (point) create point.make (600, 400) polyline.extend (point) create point.make (550, 300) polyline.extend (point) create point.make (700, 400) polyline.extend (point) create point.make (700, 300) polyline.extend (point) polyline.set_line_color (blue) polyline.set_line_width (10) zoomable_widget.extend (polyline) -- Add instruction text below zoomable widget. font := standard_bmp_fonts.small_vera_font create string.make ("Mouse buttons: left = drag, middle = zoom, right = scroll", font) string.set_x_y (x - 40, y + height + 20) main_container.extend (string) create string.make ("rightclick = toggle visibility", font) string.set_x_y (x + 200, y + height + 40) main_container.extend (string) -- Subscribe for mouse events to make items inside `zoomable_widget' dragable. zoomable_widget.mouse_button_down_on_item_event.subscribe (agent handle_mouse_button_down_on_item) zoomable_widget.mouse_motion_event.subscribe (agent handle_mouse_motion) end feature {NONE} -- Implementation handle_mouse_button_down_on_item (an_item: EM_DRAWABLE; a_mouse_button_event: EM_MOUSEBUTTON_EVENT) is -- Activate `an_item' that has been clicked inside `zoomable_widget'. do if a_mouse_button_event.is_left_button then active_item := an_item end if a_mouse_button_event.is_right_button then an_item.set_visible (not an_item.is_visible) end end handle_mouse_motion (a_motion_event: EM_MOUSEMOTION_EVENT) is -- Move `active_item' to the position of the cursor, -- when mouse is moved inside `zoomable_widget'. local new_x, new_y: INTEGER do if active_item /= Void then if a_motion_event.button_state_left then -- Move center of `active_item' to mouse position. new_x := a_motion_event.proportional_position.x.floor new_x := new_x - active_item.width // 2 new_y := a_motion_event.proportional_position.y.floor new_y := new_y - active_item.height // 2 active_item.set_x_y (new_x, new_y) -- Recalculate object area inside `zoomable_widget'. zoomable_widget.calculate_object_area else -- Remove active item if left mouse button is up. active_item := Void end end end zoomable_widget: EM_ZOOMABLE_WIDGET -- Container that can zoom and scroll its content. active_item: EM_DRAWABLE -- Drawable object that is currently moved. -- (the one that was clicked recently) x: INTEGER is 100 -- X-position of `zoomable_widget' y: INTEGER is 100 -- Y-position of `zoomable_widget' width: INTEGER is 800 -- Width of `zoomable_widget' height: INTEGER is 600 -- Height of `zoomable_widget' end