Clutter Learning (8): Actor events

Source: Internet
Author: User
Tags call back

In the previous example, the g_signal_connect () function is used for time triggering. These triggers are either based on gtk or ClutterStage. Clutter can also provide event triggers based on actor, which are button-press-event,
Button-release-event,
Motion-event, enter-event, and leave-event. They mean literally.

In clutter, the event trigger function of actor is disabled by default, and clutter_actor_set_reactive. Since the motion-event is triggered by the movement of the mouse, the trigger frequency is very high, which affects the performance. We can disable it through clutter_set_motion_events_enabled. The following is an example.

# Include <stdlib. h>
# Include
<Clutter/clutter. h>

/*
This is one of our experiments. We can use stage events that are opened by default instead of Actor event detection. However, we need to determine which actor occurs based on the coordinates, in this way, the program will be cumbersome. In addition, if we enable the actor event detection function, for an event stage and the actor call back function at the same time, only the actor is triggered, not the stage. */

Static gboolean on_stage_button_press (ClutterStage * stage, ClutterEvent
* Event, gpointer data ){
Gfloat x = 0.0, y = 0.0;

Clutter_event_get_coords (event, & x, & y );

ClutterActor * rect =Clutter_stage_get_actor_at_pos

(Stage, CLUTTER_PICK_ALL, x, y );

If (! Rect ){

Printf ("on_stage_button_press trigger -- not on our
Stage./n ");
Return FALSE;
}
If (rect =
Data)
Printf ("on_stage_button_press trigger. -- on our
Rectangle/N ");
Else
Printf ("on_stage_button_press
Trigger. -- other position/N ");

Return true;
}

Static gboolean on_actor_button_press (clutterrectangle *
Rect, clutterevent * event, gpointer data ){
Cluttercolor actor_color
= {0xff, 0xff, 0xff, 0xe0 };

Clutter_rectangle_set_color (rect, & actor_color );

Printf ("on_actor_button_press trigger./n ");
Return TRUE;
}

Static gboolean on_actor_button_release (ClutterRectangle *
Rect, ClutterEvent * event, gpointer data ){
ClutterColor actor_color
= {0xff, 0xff, 0xff, 0xA0 };

Clutter_rectangle_set_color (rect, & actor_color );

Printf ("on_actor_button_release trigger./n ");
Return TRUE;
}

Static gboolean on_actor_motion_event (ClutterRectangle *
Rect, ClutterEvent * event, gpointer data ){

Printf ("on_actor_motion_event trigger./n ");
Return true;
}

Static gboolean on_actor_enter_event (clutteractor * rect,
Clutterevent * event, gpointer data ){

Clutter_actor_set_scale (rect, 1.2, 1.2 );
Printf ("on_actor_enter_event
Trigger./N ");
Return true;
}

Static gboolean on_actor_leave_event (clutteractor * rect,
Clutterevent * event, gpointer data ){

Clutter_actor_set_scale (rect, 1.0, 1.0 );
Printf ("on_actor_leave_event
Trigger./N ");
Return true;
}

Int main (int argc, char * argv []) {
ClutterColor
Stage_color = {0x00,0x00,0x00, 0xff };
ClutterColor actor_color
= {0xff, 0xff, 0xff, 0xA0 };

Clutter_init (& argc, & argv );

ClutterActor * stage =
Clutter_stage_get_default ();

Clutter_actor_set_size (stage, 200,200 );

Clutter_stage_set_color (CLUTTER_STAGE (stage), & stage_color );

ClutterActor * rect =
Clutter_rectangle_new_with_color (& actor_color );

Clutter_actor_set_size (rect, 100,100 );

Clutter_actor_set_position (rect, 50, 50 );

Clutter_container_add_actor (CLUTTER_CONTAINER (stage), rect );


G_signal_connect (stage, "button-press-event", G_CALLBACK (on_stage_button_press), NULL );

/* Allow the actor to emit
Events. By default only the stage does this.
*
Actor event triggering is disabled by default. clutter_actor_set_reactive must be called. To improve performance, you can disable triggering by moving the mouse */


Clutter_actor_set_reactive
(Rect, TRUE );
//Clutter_set_motion_events_enabled
(FALSE );/*
The experiment results show that when the mouse enters the rect, the Enter-event is not triggered effectively, and the button is required to be triggered. However, when the mouse leaves the rect, you also need to press the button to trigger it, however, it can be triggered when the mouse leaves the stage. */


G_signal_connect (rect, "button-press-Event", g_callback
(On_actor_button_press), null );
G_signal_connect (rect,
"Button-release-Event", g_callback (on_actor_button_release), null );

G_signal_connect (rect, "motion-Event", g_callback (on_actor_motion_event ),
Null );
G_signal_connect (rect, "Enter-Event", g_callback
(On_actor_enter_event), null );
G_signal_connect (rect, "Leave-Event ",
G_callback (on_actor_leave_event), null );

Clutter_actor_show_all (stage );

Clutter_main ();
Return EXIT_SUCCESS;
}

Learning materials:
Http://www.openismus.com/documents/clutter_tutorial/0.9/docs/tutorial/html/sec-actors-events.html

 

Related links:



My Clutter blog

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.