gnomemm r1918 - cluttermm_tutorial/trunk/examples/custom_actor



Author: daniel
Date: Wed Dec 24 15:06:28 2008
New Revision: 1918
URL: http://svn.gnome.org/viewvc/gnomemm?rev=1918&view=rev

Log:
Add custom_actor example.


Added:
   cluttermm_tutorial/trunk/examples/custom_actor/
   cluttermm_tutorial/trunk/examples/custom_actor/main.cc
   cluttermm_tutorial/trunk/examples/custom_actor/triangle_actor.cc
   cluttermm_tutorial/trunk/examples/custom_actor/triangle_actor.h

Added: cluttermm_tutorial/trunk/examples/custom_actor/main.cc
==============================================================================
--- (empty file)
+++ cluttermm_tutorial/trunk/examples/custom_actor/main.cc	Wed Dec 24 15:06:28 2008
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2008 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "triangle_actor.h"
+#include <cluttermm.h>
+
+int main(int argc, char** argv)
+{
+  Clutter::init(&argc, &argv);
+
+  // Get the stage and set its size and color
+  Glib::RefPtr<Clutter::Stage> stage = Clutter::Stage::get_default();
+  stage->set_size(200, 200);
+  stage->set_color(Clutter::Color(0x00, 0x00, 0x00, 0xFF)); // black
+
+  // Add our custom actor to the stage
+  Glib::RefPtr<Tutorial::Triangle> actor =
+      Tutorial::Triangle::create(Clutter::Color(0xFF, 0xFF, 0xFF, 0x99));
+  actor->set_size(100, 100);
+  actor->set_position(20, 20);
+  stage->add_actor(actor);
+  actor->show();
+
+  // Show the stage
+  stage->show();
+
+  // Start the main loop, so we can respond to events
+  Clutter::main();
+
+  return 0;
+}

Added: cluttermm_tutorial/trunk/examples/custom_actor/triangle_actor.cc
==============================================================================
--- (empty file)
+++ cluttermm_tutorial/trunk/examples/custom_actor/triangle_actor.cc	Wed Dec 24 15:06:28 2008
@@ -0,0 +1,124 @@
+/* Copyright 2007 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "triangle_actor.h"
+#include <cogl/cogl.h>
+
+namespace Tutorial
+{
+
+Glib::RefPtr<Triangle> Triangle::create()
+{
+  return Glib::RefPtr<Triangle>(new Triangle());
+}
+
+Glib::RefPtr<Triangle> Triangle::create(const Clutter::Color& color)
+{
+  return Glib::RefPtr<Triangle>(new Triangle(color));
+}
+
+Triangle::Triangle()
+:
+  color_ (0xFF, 0xFF, 0xFF, 0xFF)
+{}
+
+Triangle::Triangle(const Clutter::Color& color)
+:
+  color_ (color)
+{}
+
+Triangle::~Triangle()
+{}
+
+void Triangle::do_triangle_paint(const CoglColor* color)
+{
+  const Clutter::Geometry geom = get_geometry();
+
+  cogl_push_matrix();
+  cogl_set_source_color(color);
+
+  ClutterFixed coords[6];
+
+  // Paint a triangle.  The parent paint call will have translated us into
+  // position so paint from 0, 0.
+  coords[0] = CLUTTER_INT_TO_FIXED(0);
+  coords[1] = CLUTTER_INT_TO_FIXED(0);
+
+  coords[2] = CLUTTER_INT_TO_FIXED(0);
+  coords[3] = CLUTTER_INT_TO_FIXED(geom.get_height());
+
+  coords[4] = CLUTTER_INT_TO_FIXED(geom.get_width());
+  coords[5] = coords[3];
+
+  cogl_path_polygon(coords, G_N_ELEMENTS(coords) / 2);
+  cogl_path_fill();
+
+  cogl_pop_matrix();
+}
+
+void Triangle::on_paint()
+{
+  CoglColor coglcolor;
+
+  // Paint the triangle with the actor's color
+  cogl_color_set_from_4ub(&coglcolor,
+                          color_.get_red(),
+                          color_.get_green(),
+                          color_.get_blue(),
+                          get_opacity());
+  do_triangle_paint(&coglcolor);
+}
+
+void Triangle::pick_vfunc(const Clutter::Color& color)
+{
+  CoglColor coglcolor;
+  // Paint the triangle with the pick color, offscreen.
+  // This is used by Clutter to detect the actor under the cursor 
+  // by identifying the unique color under the cursor.
+  cogl_color_set_from_4ub(&coglcolor,
+                          color.get_red(),
+                          color.get_green(),
+                          color.get_blue(),
+                          color.get_alpha());
+  do_triangle_paint(&coglcolor);
+}
+
+/**
+ * Tutorial::Triangle::get_color:
+ *
+ * @returns the color of the triangle.
+ */
+Clutter::Color Triangle::get_color() const
+{
+  return color_;
+}
+
+/**
+ * Tutorial::Triangle::set_color:
+ * @color: a Clutter::Color
+ *
+ * Sets the color of the triangle.
+ */
+void Triangle::set_color(const Clutter::Color& color)
+{
+  color_ = color;
+  set_opacity(color_.get_alpha());
+
+  if (is_visible())
+    queue_redraw();
+}
+
+} // namespace Tutorial

Added: cluttermm_tutorial/trunk/examples/custom_actor/triangle_actor.h
==============================================================================
--- (empty file)
+++ cluttermm_tutorial/trunk/examples/custom_actor/triangle_actor.h	Wed Dec 24 15:06:28 2008
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2008 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef CLUTTER_TUTORIAL_TRIANGLE_ACTOR_H
+#define CLUTTER_TUTORIAL_TRIANGLE_ACTOR_H
+
+#include <cluttermm.h>
+
+namespace Tutorial
+{
+
+class Triangle : public Clutter::Actor
+{
+public:
+  static Glib::RefPtr<Triangle> create();
+  static Glib::RefPtr<Triangle> create(const Clutter::Color& color);
+
+  virtual ~Triangle();
+
+  void set_color(const Clutter::Color& color);
+  Clutter::Color get_color() const;
+
+protected:
+  Triangle();
+  explicit Triangle(const Clutter::Color& color);
+
+  virtual void on_paint();
+  virtual void pick_vfunc(const Clutter::Color& color);
+
+private:
+  Clutter::Color color_;
+
+  void do_triangle_paint(const CoglColor* color);
+};
+
+} // namespace Tutorial
+
+#endif /* !CLUTTER_TUTORIAL_TRIANGLE_ACTOR */



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