~ubuntu-branches/ubuntu/quantal/pyclutter/quantal

« back to all changes in this revision

Viewing changes to examples/script.py

  • Committer: Bazaar Package Importer
  • Author(s): Rob Bradford
  • Date: 2008-10-15 15:25:57 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081015152557-qv1xzqtng4iqgwfd
Tags: 0.8.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
import clutter
 
3
 
 
4
BUFFER = '''
 
5
[
 
6
  { "id" : "move-timeline",  "type" : "ClutterTimeline", "duration" : 2500 },
 
7
  { "id" : "scale-timeline", "type" : "ClutterTimeline", "duration" : 2000 },
 
8
  { "id" : "fade-timeline",  "type" : "ClutterTimeline", "duration" : 1500 },
 
9
  {
 
10
    "id" : "move-behaviour", "type" : "ClutterBehaviourPath",
 
11
    "alpha" : {
 
12
      "timeline" : "move-timeline",
 
13
      "function" : "clutter_sine_inc_func"
 
14
    },
 
15
    "knots" : [
 
16
      [ 100, 100 ],
 
17
      [ 200, 150 ]
 
18
    ]
 
19
  },
 
20
  {
 
21
    "id" : "scale-behaviour", "type" : "ClutterBehaviourScale",
 
22
    "x-scale-start" : 1.0, "x-scale-end" : 0.7,
 
23
    "y-scale-start" : 1.0, "y-scale-end" : 0.7,
 
24
    "alpha" : {
 
25
      "timeline" : "scale-timeline",
 
26
      "function" : "sine-inc"
 
27
    }
 
28
  },
 
29
  {
 
30
    "id" : "fade-behaviour", "type" : "ClutterBehaviourOpacity",
 
31
    "opacity-start" : 255, "opacity-end" : 0,
 
32
    "alpha" : {
 
33
      "timeline" : "fade-timeline",
 
34
      "function" : "sine-inc"
 
35
    }
 
36
  },
 
37
  {
 
38
    "id" : "main-stage",
 
39
    "type" : "ClutterStage",
 
40
    "color" : "#ffffff",
 
41
    "visible" : true,
 
42
    "reactive" : true,
 
43
    "signals" : [
 
44
      { "name" : "key-press-event", "handler" : "do_quit" },
 
45
      { "name" : "destroy", "handler" : "do_quit" }
 
46
    ],
 
47
    "children" : [
 
48
      {
 
49
        "id" : "red-button",
 
50
        "type" : "ClutterRectangle",
 
51
        "visible" : true,
 
52
        "reactive" : true,
 
53
        "color" : "#dd0000",
 
54
        "opacity" : 255,
 
55
        "x" : 100, "y" : 100, "width" : 300, "height" : 300,
 
56
        "rotation" : [
 
57
          { "z-axis" : [ 45.0, [ 200, 200 ] ] }
 
58
        ],
 
59
        "signals" : [
 
60
          { "name" : "button-press-event", "handler" : "do_press" }
 
61
        ],
 
62
        "behaviours" : [
 
63
          "move-behaviour",
 
64
          "scale-behaviour",
 
65
          "fade-behaviour"
 
66
        ]
 
67
      }
 
68
    ]
 
69
  }
 
70
]
 
71
'''
 
72
 
 
73
class TestScript:
 
74
    def __init__ (self, *args, **kwargs):
 
75
        self._score = clutter.Score()
 
76
 
 
77
    def do_quit (self, *args):
 
78
        print "quitting"
 
79
        clutter.main_quit()
 
80
 
 
81
    def do_timeline_start (self, score, timeline):
 
82
        print "timeline started: %s" % (clutter.get_script_id(timeline))
 
83
 
 
84
    def do_press (self, actor, event):
 
85
        print "running the score"
 
86
        self._score.connect('timeline-started', self.do_timeline_start)
 
87
        self._score.connect('completed', self.do_quit)
 
88
        self._score.start()
 
89
 
 
90
        return True
 
91
 
 
92
    def load_script (self):
 
93
        self._script = clutter.Script()
 
94
        try:
 
95
            self._script.load_from_data(BUFFER, -1)
 
96
            self._script.connect_signals(self)
 
97
        except Exception, exc:
 
98
            print "Unable to load buffer: %s" % (exc)
 
99
            sys.exit(1)
 
100
 
 
101
    def run (self):
 
102
        self.load_script()
 
103
 
 
104
        self._timelines = self._script.get_objects('move-timeline',
 
105
                                                   'scale-timeline',
 
106
                                                   'fade-timeline')
 
107
        for timeline in self._timelines:
 
108
            print "Timeline: %s" % (clutter.get_script_id (timeline))
 
109
 
 
110
        self._score.append(timeline=self._timelines[0])
 
111
        self._score.append(timeline=self._timelines[1], parent=self._timelines[0])
 
112
        self._score.append(timeline=self._timelines[2], parent=self._timelines[1])
 
113
        assert(3 == len(self._score.list_timelines()))
 
114
 
 
115
        self._stage = self._script.get_object('main-stage')
 
116
        self._stage.show_all()
 
117
 
 
118
        clutter.main()
 
119
 
 
120
        return 0
 
121
 
 
122
if __name__ == '__main__':
 
123
    test = TestScript()
 
124
    sys.exit(test.run())