Re: Help with Clutter::Scriptable



On Fri, Feb 25, 2011 at 4:21 PM, Brian Gregg <biscuitalmighty gmail com> wrote:
> Am I right to assume that the constructor call to
> Glib::ObjectBase("TypeName") needs to be called to create the type before I
> load the object from script?  I would need to instantiate a TypeName object
> before the script would understand the type exists correct?
> Brian
>

In case you haven't figured it out, here is some code that seems to work.

#include <cluttermm.h>

// This is the JSON node. Notice that all types are C-types.
// One can not currenty use C++-types.
static const Glib::ustring jsonNode("[                   \
  {                                                      \
    \"id\" : \"stage\",                                  \
    \"type\" : \"ClutterStage\",                         \
    \"width\" : 200,                                     \
    \"height\" : 200,                                    \
    \"color\" : \"#20456f\",                             \
    \"children\" : [ \"text-box\", \"myrect\", \"rect2\"]\
  },                                                     \
  {                                                      \
    \"id\" : \"myrect\",                                 \
    \"type\" : \"gtkmm__CustomObject_myrect\",           \
    \"width\" :  100,                                    \
    \"height\" : 100,                                    \
    \"x\" : 10,                                          \
    \"y\" : 10,                                          \
    \"color\" : \"#cc99ee\",                             \
    \"opacity\" : 127                                    \
  },                                                     \
  {                                                      \
    \"id\" : \"rect2\",                                  \
    \"type\" : \"ClutterRectangle\",                     \
    \"width\" :  100,                                    \
    \"height\" : 100,                                    \
    \"x\" : 50,                                          \
    \"y\" : 50,                                          \
    \"color\" : \"#cc99ee\"                              \
  },                                                     \
  {                                                      \
    \"id\" : \"text-box\",                               \
    \"type\" : \"ClutterText\",                          \
    \"width\" : 100,                                     \
    \"height\" : 60,                                     \
    \"x\" : 20,                                          \
    \"y\" : 20,                                          \
    \"text\" : \"This is text!\",                        \
    \"color\" : \"#ddeeee\"                              \
  }                                                      \
]");

class MyRect : public Clutter::Rectangle
{
public:
  MyRect();
  virtual ~MyRect();

  static Glib::RefPtr<MyRect> create();
};

MyRect::MyRect() :
  // This creates a c-type named gtkmm__CustomObject_myrect.
  // that can be used in the JSON node.
  Glib::ObjectBase("myrect")
{};

MyRect::~MyRect() {}

Glib::RefPtr<MyRect> MyRect::create()
{
  return Glib::RefPtr<MyRect>( new MyRect() );
}


int main(int argc, char** argv)
{
  Clutter::init(&argc, &argv);

  // This must be called first to initialize type.
  Glib::RefPtr<MyRect> myRect = MyRect::create();

  // Now that the custom type is initialized, we can load the JSON node.
  Glib::RefPtr<Clutter::Script> script = Clutter::Script::create();
  script->load_from_data(jsonNode);

  // Get the stage by name, casting it to the correct C++-type.
  const Glib::RefPtr<Clutter::Stage> stage =
    Glib::RefPtr<Clutter::Stage>::cast_static(script->get_object("stage"));

  // Show the stage:
  stage->show();

  // Start the main loop, so we can respond to events:
  Clutter::main();

  return 0;
}

// Chris


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