Re: [Vala] Access Clutter Script Actor in Event handler



Hey Everyone,

It looks like I did not setup my event handler params properly. For this
particular signal I needed three params; Actor (Rectangle), ButtonEvent,
Script. In this way I am able to connect my handler in the normal
"clutterScript" way using "ui.connect_signals (ui);".  Now I can set the
color of my rectangle using the first param as the object and get the stage
object from the ui I pass in as the third param. Here is the code if anyone
is interested ...

using Clutter;
using GLib;


void button_press (Rectangle actor, ButtonEvent event, Script ui) {
   stdout.printf("%s", "button press event");
   Stage stage = (Stage)ui.get_object ("stage");
   actor.set_color(Color.from_string("white"));
   stage.set_color(Color.from_string("red"));
}


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;
}

[ClutterScript UI]

[
  {
    "id" : "stage",
    "type" : "ClutterStage",
    "width" : 300,
    "height" : 300,
    "color" : "#335",

    "signals" : [
      { "name" : "hide", "handler" : "clutter_main_quit" }
    ],

    "children" : [ "rectangle" ]
  },

  {
    "id" : "rectangle",
    "type" : "ClutterRectangle",
    "width" : 200,
    "height" : 200,
    "x" : 50,
    "y" : 50,
    "color" : "#a90",
    "rotation-center-z-gravity" : "center",
    "reactive" : true,

    "signals" : [
      { "name" : "button-press-event", "handler" : "button_press" }
    ],

    "actions" : [
      {
"type" : "ClutterClickAction",
"signals" : [
  { "name" : "clicked", "handler" : "foo_button_clicked_cb" }
]
      }
    ]

  }
]

On Thu, Jan 26, 2012 at 5:06 AM, tomw <tomw ubilix com> wrote:

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;
}




-- 
Duff


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]