Re: [Vala] Access Clutter Script Actor in Event handler
- From: tomw <tomw ubilix com>
- To: Brian Duffy <brduffy gmail com>
- Cc: clutter-app-devel-list clutter-project org, vala-list <vala-list gnome org>
- Subject: Re: [Vala] Access Clutter Script Actor in Event handler
- Date: Thu, 26 Jan 2012 11:06:33 +0100
Hi,
if you want to pass user data to a signal handler you should connect the
signal in the code rather than by the script. The vala code may look
like this:
using Clutter;
using GLib;
public bool button_press (Actor actor, Script ui) {
stdout.printf("button press event");
Rectangle rect = (Rectangle)ui.get_object ("rectangle");
rect.set_color(Color.from_string("white"));
return true;
}
public bool key_pressed (KeyEvent event) {
stdout.printf ("Key pressed : %d \n", event.hardware_keycode);
Clutter.main_quit ();
return true;
}
int main (string[] args) {
//Stage stage = Stage.get_default();
string filename = "gui_test.json";
if (Clutter.init(ref args) != Clutter.InitError.SUCCESS) {
stdout.printf("Error Initializing Clutter");
return 1;
}
Script ui = new Script();
uint merge_id = ui.load_from_file (filename);
if (merge_id == 0)
{
stdout.printf("%s", "Error Loading ClutterScript");
return 1;
}
//ui.connect_signals (null);
Stage stage = (Stage)ui.get_object ("stage");
stage.button_press_event.connect (() => {button_press (stage, ui);
return true;});
stage.key_press_event.connect (key_pressed);
stage.show_all();
Clutter.main();
return 0;
}
and the json-file (slightly simpler) like this:
[
{
"id" : "stage",
"type" : "ClutterStage",
"width" : 300,
"height" : 300,
"color" : "#335",
"signals" : [
{ "name" : "hide", "handler" : "clutter_main_quit" }
],
"children" : [
{
"id" : "rectangle",
"type" : "ClutterRectangle",
"width" : 200,
"height" : 200,
"x" : 50,
"y" : 50,
"color" : "#a90",
"rotation-center-z-gravity" : "center",
"reactive" : true
}
]
}
]
On Mi, 2012-01-25 at 16:55 -0500, Brian Duffy wrote:
using Clutter;
using GLib;
bool button_press (ButtonEvent event, Script ui) { // wrong
stdout.printf("%s", "button press event");
Rectangle rect = (Rectangle)ui.get_object ("rectangle"); // this
segfaults
//rect.set_color(Color.from_string("white"));
return true;
}
int main (string[] args) {
//Stage stage = Stage.get_default();
string filename = "gui_test.json";
if (Clutter.init(ref args) != Clutter.InitError.SUCCESS) {
stdout.printf("%s", "Error Initializing Clutter");
return 1;
}
Script ui = new Script();
uint merge_id = ui.load_from_file (filename);
if (merge_id == 0)
{
stdout.printf("%s", "Error Loading ClutterScript");
return 1;
}
ui.connect_signals (ui);
Stage stage = (Stage)ui.get_object ("stage");
/* make the objects in the script available to all signals
* by passing the script as the second argument
* to clutter_script_connect_signals()
*/
stage.show_all();
Clutter.main();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]