Innovative Interfaces with Clutter
Listing 1. “Hello World” Using Clutter
import clutter class HelloWorld: def __init__ (self): # Create stage and set its properties. self.stage = clutter.Stage() self.stage.set_color(clutter.color_parse('Black')) self.stage.set_size(500, 400) self.stage.set_title('Clutter Hello World') # Create label and set its properties. color = clutter.Color(0xff, 0xcc, 0xcc, 0xdd) hello = clutter.Label() hello.set_font_name('Mono 32') hello.set_text("Hello There!") hello.set_color(color) hello.set_position(100, 200) # Add label to stage. self.stage.add(hello) # Start main clutter loop. self.stage.show_all() clutter.main() # Run program. main = HelloWorld()
Now, let's take a look at a more useful Clutter program. Our program will use the Clutter GStreamer library to display and control a video file on the stage. Connect GStreamer's video output to a video texture, which is then displayed on the stage and can be manipulated as an actor.
There are three main actors on the stage: the play button, the pause button and the GStreamer video. When the pause button is pressed, the video pauses, and it will continue playing when the play button is pressed. The program is shown in Listing 2, and the window is shown in Figure 3.
After the initial setup, define a signal for responding to mouse clicks. Clutter uses signals to respond to events in the same way GTK+ does. The signal dictates that when any button on the mouse is clicked, the specified function is called.
Next, create the buttons by creating rectangle actors for the button shape and text actors for the button text. Remember that Clutter is not widget-based, and there are no default button widgets as part of the API.
To create the video, you need a video texture. A video texture is a physical plane on which GStreamer can display the video. The video texture can be manipulated on the stage like an ordinary actor.
You also need two other GStreamer elements to play the video: a playbin and a pipeline. Clutter-Gstreamer will play any file type that GStreamer can play. After creating the playbin, set the location of the video to play and add the playbin to the pipeline.
Then, set the position of the video texture, add it to the stage and tell GStreamer to start playing the video.
Finally, create the mouseClick function. This function is called when the mouse is clicked anywhere on the stage. Check to see if the left mouse button was clicked inside one of the buttons and if it was, change the size and color of the buttons to give visual feedback of the click. Tell GStreamer to start or stop the video depending on which button was pressed.
Listing 2. Clutter-Based Video Player
import clutter import gst from clutter import cluttergst class HelloWorld: def __init__ (self): # Create stage and set its properties. self.stage = clutter.Stage() self.stage.set_color(clutter.color_parse('Black')) self.stage.set_size(500, 400) self.stage.set_title('Clutter Basic Video Player') # Create signal for handling mouse clicks. self.stage.connect('button-press-event', self.mouseClick) # Create play button shape. self.playBtn = clutter.Rectangle() self.playBtn.set_color(clutter.Color(66, 99, 150, 0x99)) self.playBtn.set_size(50, 30) self.playBtn.set_position(118, 34) self.stage.add(self.playBtn) # Create play button text # and overlay the rectangle. playTxt = clutter.Label() playTxt.set_text("Play") playTxt.set_color(clutter.color_parse('Black')) playTxt.set_position(130, 40) self.stage.add(playTxt) # Same for stop button. self.stopBtn = clutter.Rectangle() self.stopBtn.set_color(clutter.Color(66, 99, 150, 0x99)) self.stopBtn.set_size(50, 30) self.stopBtn.set_position(218, 34) self.stage.add(self.stopBtn) StopTxt = clutter.Label() StopTxt.set_text("Pause") StopTxt.set_color(clutter.color_parse('Black')) StopTxt.set_position(225, 40) self.stage.add(StopTxt) # Create video texture. video_tex = cluttergst.VideoTexture() self.pipeline = gst.Pipeline("mypipe") playbin = video_tex.get_playbin() # Specify video file to play. movfile = "file:///home/user/Videos/Video.mov" playbin.set_property('uri', movfile) # Add to playbin to the pipeline. self.pipeline.add(playbin) # Set position and start playing the video. video_tex.set_position(90,100) self.stage.add(video_tex) self.pipeline.set_state(gst.STATE_PLAYING) self.stage.show_all() clutter.main() def mouseClick (self, stage, event): # Mouse click function, called when the moused # is clicked *anywhere* on the stage, we check # the mouse coordinates manually to see if the # click occurred inside a button. # Check for left mouse button. if event.button == 1: # Check to see if stop button was pressed. if event.x > 218 and event.x < 268 and \ event.y > 34 and event.y < 64: self.stopBtn.set_color(clutter.Color(33,50,150,0x89)) self.playBtn.set_color(clutter.Color(66,99,150,0x99)) self.stopBtn.set_size(49, 29) self.playBtn.set_size(50, 30) self.pipeline.set_state(gst.STATE_PAUSED) # Check to see if the play button was pressed. if event.x > 118 and event.x < 168 and \ event.y > 34 and event.y < 64: self.playBtn.set_color(clutter.Color(33,50,150,0x89)) self.stopBtn.set_color(clutter.Color(66,99,150,0x99)) self.playBtn.set_size(49, 29) self.stopBtn.set_size(50, 30) self.pipeline.set_state(gst.STATE_PLAYING) # Run program. main = HelloWorld()
An important feature of Clutter is its animation API. Animating any actors on the stage is easy with Clutter. Using the animation API, you can add smooth animations and effects to your Clutter application.
In the next example, let's take the GStreamer video texture and manipulate it in three dimensions. The GStreamer texture is rotating on the y-axis constantly.
The program is shown in Listing 3, and the window is shown in Figure 4.
After the initial setup, create a timeline. Timelines are used in Clutter to control animation and time events. The example timeline lasts for 100 frames at ten frames per second, and the timeline is set to loop forever.
Next create an alpha (see The Alpha Functions sidebar) for the animation, assign it to your timeline and give it a smooth step decreasing function. In simplest terms, the alpha is used to control the speed of the animation. The smooth step function causes the animation to speed up, then slow down, come to a halt, and then start up again. Clutter has several functions built in that you can use with the alpha, including sine, exponential and ramp functions.
Next, define the Rotation behavior the animation uses. Clutter uses behavior effects to describe animations. The Opacity behavior, for example, can change the visual alpha of an actor, making it transparent or opaque. Other behaviors include Scale, Path, Depth, B-Spline and Ellipse.
In this example, we tell our Rotation behavior to rotate over the x-axis, rotate clockwise, start rotating at an angle of zero, end at 360, and finally, tell it to use the alpha created earlier, respectively. After that, the rotational center, or the point the actor rotates around, is set to the approximate center of the GStreamer texture and the Rotation behaviour is applied to the video texture.
In addition to the normal startup steps, the timeline must be started.
|PostgreSQL, the NoSQL Database||Jan 29, 2015|
|HPC Cluster Grant Accepting Applications!||Jan 28, 2015|
|Sharing Admin Privileges for Many Hosts Securely||Jan 28, 2015|
|Red Hat Enterprise Linux 7.1 beta available on IBM Power Platform||Jan 23, 2015|
|Designing with Linux||Jan 22, 2015|
|Wondershaper—QOS in a Pinch||Jan 21, 2015|
- PostgreSQL, the NoSQL Database
- Sharing Admin Privileges for Many Hosts Securely
- HPC Cluster Grant Accepting Applications!
- Internet of Things Blows Away CES, and it May Be Hunting for YOU Next
- Designing with Linux
- Wondershaper—QOS in a Pinch
- Ideal Backups with zbackup
- Red Hat Enterprise Linux 7.1 beta available on IBM Power Platform
- Slow System? iotop Is Your Friend
- January 2015 Issue of Linux Journal: Security
Editorial Advisory Panel
Thank you to our 2014 Editorial Advisors!
- Jeff Parent
- Brad Baillio
- Nick Baronian
- Steve Case
- Chadalavada Kalyana
- Caleb Cullen
- Keir Davis
- Michael Eager
- Nick Faltys
- Dennis Frey
- Philip Jacob
- Jay Kruizenga
- Steve Marquez
- Dave McAllister
- Craig Oda
- Mike Roberts
- Chris Stark
- Patrick Swartz
- David Lynch
- Alicia Gibb
- Thomas Quinlan
- Carson McDonald
- Kristen Shoemaker
- Charnell Luchich
- James Walker
- Victor Gregorio
- Hari Boukis
- Brian Conner
- David Lane