indexing description: "[ Inherit from this class to create clickable objects which can process mouse clicks, mouse hoover events and key press events. The objects have a position and size given by x, y, width and height values. You can attach a descriptive string to a clickable. You get this string back by querying either the 'description' or the 'out' attribute. An arbitrary data object can be attached to the clickable for tracking purposes. This class is part of the XAE Extended Adventure Engine Project. ]" author: "Ralph Wiedemeier, ralphw@student.ethz.ch" date: "$Date$" revision: "$Revision$" class GUI_CLICKABLE inherit ANY redefine out end create make_from_position_and_size, make_from_rect feature -- Initialization make_from_rect (a_rect: EM_RECT) is -- Create new clickable from given rectangle require valid_rect: a_rect /= Void do make_from_position_and_size (a_rect.x,a_rect.y, a_rect.width, a_rect.height) end make_from_position_and_size (an_x: INTEGER; an_y: INTEGER; a_width: INTEGER; a_height: INTEGER) is -- Create new clickable from given position and size require valid_width: a_width > 0 valid_height: a_height > 0 do set_x (an_x) set_y (an_y) set_height (a_height) set_width (a_width) set_enabled end feature -- Status set_enabled is -- Enable current (respond to events) do is_enabled := True end set_disabled is -- Disable current (no response to events) do is_enabled := False end is_enabled: BOOLEAN feature -- Size and position set_x (an_x: INTEGER) is -- Set horizontal position do x := an_x end x: INTEGER set_y (an_y: INTEGER) is -- Set vertical position do y := an_y end y: INTEGER set_position (an_x: INTEGER; an_y: INTEGER) is -- Set position do set_x (an_x) set_y (an_y) end set_width (a_width: INTEGER) is -- Set width require valid_width: a_width > 0 do width := a_width end width: INTEGER set_height (a_height: INTEGER) is -- Set height require valid_height: a_height > 0 do height := a_height end height: INTEGER set_size (a_width: INTEGER; a_height: INTEGER) is -- Set width and height do set_width (a_width) set_height (a_height) end feature -- Event handling publish_event_click (mouse_x: INTEGER; mouse_y: INTEGER) is -- Process mouse click event -- If current is hit, fire the associated action_click event do if is_enabled and then is_target (mouse_x, mouse_y) then execute_event_action_click end end publish_event_hoover (mouse_x: INTEGER; mouse_y: INTEGER) is -- Process mouse hoover event -- If current is hit, fire the associated action_hoover event do if is_enabled then if is_target (mouse_x, mouse_y) then if not inside_item then inside_item := True execute_event_action_hoover_in end else if inside_item then execute_event_action_hoover_out inside_item := False end end end end publish_event_key (a_key: ANY) is -- Process key press event do if is_enabled then execute_event_action_key (a_key) end end set_event_action_click (an_action: PROCEDURE [ANY, TUPLE]) is -- Set action for click event do action_click := an_action end set_event_action_hoover_in (an_action: PROCEDURE [ANY, TUPLE]) is -- Set action for hoover event inside item do action_hoover_in := an_action end set_event_action_hoover_out (an_action: PROCEDURE [ANY, TUPLE]) is -- Set action for hoover event outside item do action_hoover_out := an_action end set_event_action_key (an_action: PROCEDURE [ANY, TUPLE]) is -- Set action for key press event do action_key := an_action end is_target (an_x: INTEGER; an_y: INTEGER): BOOLEAN is -- Test if specified point hits current do Result := an_x > x and then an_x < x + width and then an_y > y and then an_y < y + height end feature -- Context information set_description (a_string: STRING) is -- Set textual description do description := a_string end description: STRING set_data (an_object: ANY) is -- Attach arbitrary data do data := an_object end data: ANY out: STRING is -- Get descriptive string of current do if description /= Void then Result := description else Result := "" end ensure then out_string_not_void: Result /= Void end feature {NONE} -- Implementation execute_event_action_click is -- Execute action for mouse click event do if action_click /= Void then action_click.call ([current]) end end execute_event_action_hoover_in is -- Execute action for mouse hoover event do if action_hoover_in /= Void then action_hoover_in.call ([current]) end end execute_event_action_hoover_out is -- Execute action for mouse hoover event outside do if action_hoover_out /= Void then action_hoover_out.call ([current]) end end execute_event_action_key (a_key: ANY) is -- Execute action for key press event do if action_key /= Void then action_key.call ([current, a_key]) end end action_click: PROCEDURE [ANY, TUPLE] action_hoover_in: PROCEDURE [ANY, TUPLE] action_hoover_out: PROCEDURE [ANY, TUPLE] action_key: PROCEDURE [ANY, TUPLE] inside_item: BOOLEAN end -- class GUI_CLICKABLE