Clutter学习(八):Actor的事件

  在以前的例子中,使用g_signal_connect()函数来进行时间触发,这些触发要么基于gtk,要么基于ClutterStage。Clutter基于actor也可以提供事件触发,分别是button-press-event, button-release-event, motion-event,enter-event,leave-event。他们的意思就是字面意思。

  在clutter中actor的事件触发功能缺省是不打开,clutter_actor_set_reactive。由于motion-event由鼠标的移动所触发,所以触发的频率很大,会影响性能,我们可以通过clutter_set_motion_events_enabled来关闭。下面我们学习一个例子。

#include <stdlib.h>
#include <clutter/clutter.h>

/* 这是我们的一个实验,我们可以不使用Actor的事件侦测,而使用缺省打开的stage的事件,但是我们需要判断根据坐标位置来判断到底发生在哪个actor上,这样程序会比较繁琐。另外如果我们打开了actor的事件检测,对于某一个事件stage和actor同时又callback函数,则只触发actor的,不触发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的事件触发缺省是不开启的,必须调用clutter_actor_set_reactive,为了提高性能可关闭移动鼠标的触发*/

        clutter_actor_set_reactive (rect,TRUE);
        //clutter_set_motion_events_enabled (FALSE); /* 实验结果表明:当鼠标进入rect的时候,并没有有效触发enter-event,要按键才能触发,但是鼠标离开rect的时候,也需要按键才能并且触发,但是若鼠标离开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;
}

  学习资料: http://www.openismus.com/documents/clutter_tutorial/0.9/docs/tutorial/html/sec-actors-events.html

 

相关链接:
我的Clutter相关博客

 

你可能感兴趣的:(null,callback,actor,button,Signal,events)