indexing description: "[ Main scene of this example ]" date: "$Date$" revision: "$Revision$" class JOYSTICK_SCENE inherit EM_WIDGET_SCENE redefine handle_joystick_button_down_event, handle_joystick_button_up_event end EM_SHARED_BITMAP_FACTORY export {NONE} all end EM_SHARED_STANDARD_FONTS export {NONE} all end create make feature {NONE} -- Initialisation make is -- Initialise scene. local bitmap_background: EM_BITMAP_BACKGROUND img_panel: EM_IMAGEPANEL label: EM_LABEL panel: EM_PANEL i, j, pos_x: INTEGER do -- init scene load_eclipse_theme make_widget_scene set_frame_counter_visibility (false) -- create background Bitmap_factory.create_bitmap_from_image ("./image/background.png") create bitmap_background.make_from_bitmap (Bitmap_factory.last_bitmap) set_background (bitmap_background) -- select your joystick device create label.make_from_text ("Select a Joystick device:") label.align_right label.set_position (265 - label.optimal_width, 10) add_widget (label) create combobox.make_from_list (joystick_subsystem.name_list) combobox.set_position (270, 10) combobox.set_dimension (300, combobox.optimal_height) combobox.selection_changed_event.subscribe (agent on_device_changed(?)) add_widget (combobox) -- create and setup axis panel create axis_panel.make (225, 170) axis_panel.set_position (5, 50) axis_panel.set_border (create {EM_NAMED_BORDER}.make_from_text ("Axes")) add_widget (axis_panel) -- create and setup hat panel create hat_panel.make (100, 170) hat_panel.set_position (235, 50) hat_panel.set_border (create {EM_NAMED_BORDER}.make_from_text ("Hat Positions")) add_widget (hat_panel) -- create and setup panel for balls create ball_panel.make (305, 170) ball_panel.set_position (340, 50) ball_panel.set_border (create {EM_NAMED_BORDER}.make_from_text ("Ball Movements")) add_widget (ball_panel) -- create and setup panel for buttons create button_panel.make_from_dimension (435, 150) button_panel.set_position (5, 225) button_panel.set_border (create {EM_NAMED_BORDER}.make_from_text ("Buttons")) button_panel.set_background_color (create {EM_COLOR}.make_white) add_widget (button_panel) -- create button labels and place them in the button_panel create buttons.make (0, 17) from i := 0 until i = 3 loop j := i * 6 pos_x := 5 + (140 * i) create label.make_from_text ("Button #" + j.out + ": Up") label.set_position (pos_x, 15) label.set_dimension (label.optimal_width + 300, label.optimal_height) button_panel.add_widget (label) buttons.put (label, j) create label.make_from_text ("Button #" + (j+1).out + ": Up") label.set_position (pos_x, 15 + (label.optimal_height + 5)) label.set_dimension (label.optimal_width + 300, label.optimal_height) button_panel.add_widget (label) buttons.put (label, j+1) create label.make_from_text ("Button #" + (j+2).out + ": Up") label.set_position (pos_x, 15 + 2 * (label.optimal_height + 5)) label.set_dimension (label.optimal_width + 300, label.optimal_height) button_panel.add_widget (label) buttons.put (label, j+2) create label.make_from_text ("Button #" + (j+3).out + ": Up") label.set_position (pos_x, 15 + 3 * (label.optimal_height + 5)) label.set_dimension (label.optimal_width + 300, label.optimal_height) button_panel.add_widget (label) buttons.put (label, j+3) create label.make_from_text ("Button #" + (j+4).out + ": Up") label.set_position (pos_x, 15 + 4 * (label.optimal_height + 5)) label.set_dimension (label.optimal_width + 300, label.optimal_height) button_panel.add_widget (label) buttons.put (label, j+4) create label.make_from_text ("Button #" + (j+5).out + ": Up") label.set_position (pos_x, 15 + 5 * (label.optimal_height + 5)) label.set_dimension (label.optimal_width + 300, label.optimal_height) button_panel.add_widget (label) buttons.put (label, j+5) i := i + 1 end -- create and setup panel with standard information create panel.make_from_dimension (200, 150) panel.set_position (445, 225) panel.set_border (create {EM_NAMED_BORDER}.make_from_text ("Joystick Information")) panel.set_background_color (create {EM_COLOR}.make_white) add_widget (panel) create info_txt.make_from_text ("Please select a device to show more informations.") info_txt.set_dimension (190, 180) info_txt.set_position (5, 15) info_txt.align_top info_txt.set_multilined (true) panel.add_widget (info_txt) -- and finally a little image ;) create img_panel.make_from_file ("./image/joystick.png") img_panel.set_transparent img_panel.set_position (5, 0) add_widget (img_panel) -- check if there is any cdrom drives and set the standard drive to true if joystick_subsystem.count > 0 then combobox.set_selected_index (1) -- set standard joystick else combobox.put (no_device_string) combobox.set_selected_index (1) combobox.disable combobox.set_height (combobox.optimal_height) end end feature -- Status text update handle_joystick_button_down_event (event: EM_JOYSTICK_BUTTON_EVENT) is -- Handle joystick button down event do buttons.item (event.is_button).set_text ("Button #" + event.is_button.out + ": Down") end handle_joystick_button_up_event (event: EM_JOYSTICK_BUTTON_EVENT) is -- Handle joystick button up event do buttons.item (event.is_button).set_text ("Button #" + event.is_button.out + ": Up") end feature -- Access on_device_changed (selected: STRING) is -- combobox selection has changed -- update detailed information in the listbox do if selected.is_equal (no_device_string) then -- setup chosen joystick device update_info_txt reset_button_state -- setup the axes/hats panel axis_panel.set_axis_count (0) hat_panel.set_hats_count (0) ball_panel.set_balls_count (0) else -- setup chosen joystick device create_joystick_device_handler update_info_txt reset_button_state -- setup the axes/hats panel axis_panel.set_axis_count (joystick.count_axes) hat_panel.set_hats_count (joystick.count_hats) ball_panel.set_balls_count (joystick.count_balls) end end feature {NONE} -- Implementation create_joystick_device_handler is -- create a device handler with the selected -- device in the combobox do -- first close the existing handler if any if joystick /= void then joystick.close end -- if the combobox has a valid selection it will be between 1 and cdrom_subsystem.count -- but device index are from 0 to cdrom_subsystem.count -1. So we need `-1' create joystick.make_for_device (combobox.selected_index -1) end update_info_txt is -- update the information text with all important -- information about the joystick device do if joystick = void and joystick_subsystem.count = 0 then info_txt.set_text ("There is no joystick attached to your system. Please attache at least one joystick and restart this application.%NNumber of devices: " + joystick_subsystem.count.out) elseif joystick = void and joystick_subsystem.count /= 0 then info_txt.set_text ("Please select a joystick device to show more informations.%NNumber of devices: " + joystick_subsystem.count.out) else info_txt.set_text ("Attached Joysticks: " + joystick_subsystem.count.out + "%NNumber of Axes: " + joystick.count_axes.out + "%NNumber of Balls: " + joystick.count_balls.out + "%NNumber of Buttons: " + joystick.count_buttons.out + "%NNumber of Hats: " + joystick.count_hats.out + "%N%NNote: Only 18 buttons will be shown here.") end end reset_button_state is -- show only the relevant button states local i: INTEGER do -- if there is no joystick attached to the system -- hide everything. if joystick_subsystem.count = 0 then -- hide all labels from i := 0 until i >= 18 loop buttons.item (i).set_text ("") i := i + 1 end else -- setup used buttons from i := 0 until i = joystick.count_buttons or i = 18 loop buttons.item (i).set_text ("Button #" + i.out + ": Up") i := i + 1 end -- hide unused labels from i := joystick.count_buttons until i >= 18 loop buttons.item (i).set_text ("") i := i + 1 end end end joystick: EM_JOYSTICK -- container for the selected cdrom device axis_panel: JOYSTICK_AXIS_PANEL -- here we draw the position of all joystick axes hat_panel: JOYSTICK_HAT_PANEL -- here we draw the postion of all joystick hats ball_panel: JOYSTICK_BALL_PANEL -- here we draw the relative position change of balls button_panel: EM_PANEL -- here we draw the state of all balls info_txt: EM_LABEL -- here we draw some basic information about the selected joystick combobox: EM_COMBOBOX [STRING] -- Combobox to select a Joystick device buttons: ARRAY [EM_LABEL] -- container containing all labels each representing a button on the joystick no_device_string: STRING is "No device avaiable" -- string that describes there is no device end