ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 使用Moblin开发应用程序 - Clutter之GTK与Clutter

使用Moblin开发应用程序 - Clutter之GTK与Clutter

原创 Linux操作系统 作者:Nikey001 时间:2009-05-12 15:10:41 0 删除 编辑
在上次的Demo中我们实现了鼠标拖拽的效果。理论上说,现在该是做一些具体功能的时候了。写程序时却发现,在Clutter中要做个菜单、工具栏啥的太麻烦了。为了偷懒,我就尝试着将上次的Clutter程序先搬到熟悉的GTK上。

Clutter的设计目标来看,Clutter是支持与GTK进行互操作的,也就是说,无论是在GTK的程序框架中嵌入Clutter控件还是在Clutter程序框架中嵌入GTK控件都是支持的。理论归理论,目前Clutter仅支持在GTK框架中嵌入Clutter代码。

要在GTK程序中使用Clutter,首要任务是搭台,也就是在GTK中为Clutter找到一个Stage。这个任务就交给控件GtkClutterEmbed来完成了。GtkClutterEmbed顾名思义就是嵌入在GTK窗口中的Clutter(Stage),它的角色是窗口中的默认Stage。有了这个Stage后,Clutter相关代码就与原生Clutter代码别无二致了。用GtkClutterEmbed这个控件之前,要

#include 或者 import cluttergtk

在写程序时不能忘记此时我们是生活在GTK下滴,所以要用gtk main loop, gtk_main_quit(), gtk_...

直接看代码吧。

#!/usr/bin/python

 

import sys

import os

import random

 

import cluttergtk

import clutter

import gtk

 

STAGE_WIDTH=1024

STAGE_HEIGHT=768

Dragging = False

DraggingPhoto = None

 

class Photo:

       '''Photo class'''

       border_width = 10

 

       def __init__(self, path, stage):

              self.stage = stage

              self.path = path

              self.x = 0

              self.y = 0

              self.degree = 0

              self.drag_start_x = 0

              self.drag_start_y = 0

 

              self.pic = clutter.Texture()

              self.pic.set_from_file(path)

              self.width = self.pic.get_width()+2*Photo.border_width

              self.height = self.pic.get_height()+2*Photo.border_width            

             

              self.frame. = clutter.Rectangle()

              self.frame.set_color(clutter.Color(0xff, 0xff, 0xff, 0xff))

              self.frame.set_position(self.x, self.y)

              self.frame.set_size(self.width, self.height)

 

              self.group = clutter.Group()

              self.group.add(self.frame)

              self.group.add(self.pic)

              self.pic.set_position(Photo.border_width, Photo.border_width)

             

              self.stage.add(self.group)    

 

              self.group.set_reactive(True)

              self.group.connect("button-press-event", self.on_button_press)

              self.group.connect("button-release-event", self.on_button_release)

              self.group.connect("motion-event", self.on_motion)

             

 

       def set_random_position(self):

              stage_width = self.stage.get_width()

              stage_height = self.stage.get_height()

              left = random.randint(0, stage_width)

              top = random.randint(0, stage_height)

              degree = random.randint(0, 360)

 

              while left+self.width > stage_width:

                     left = random.randint(0, stage_width)

 

              while top+self.height > stage_height:

                     top = random.randint(0, stage_height)

 

              self.set_position(left, top, degree)

                    

 

       def set_position(self, x, y, degree):

              self.x = x

              self.y = y

              self.degree = degree

              self.group.set_position(x, y)

              self.group.set_rotation(clutter.Z_AXIS, degree, (x+self.width)/2, (y+self.width)/2, 0)

 

 

       def on_button_press(self, actor, event):

              global Dragging, DraggingPhoto       

              if event.button == 1 and Dragging == False:

                     Dragging = True

                     DraggingPhoto = self

                     self.drag_start_x = event.x

                     self.drag_start_y = event.y

                     self.group.raise_top()

              return True   

 

       def on_motion(self, actor, event):

              global Dragging, DraggingPhoto

              if event.modifier_state & clutter.BUTTON1_MASK and Dragging == True and DraggingPhoto == self:

                     dist_x = event.x - self.drag_start_x

                     dist_y = event.y - self.drag_start_y

                     self.group.move_by(dist_x, dist_y)

                     self.drag_start_x = event.x

                     self.drag_start_y = event.y

              return True

 

 

       def on_button_release(self, actor, event):

              global Dragging, DraggingPhoto

              if event.button == 1:

                     Dragging = False

                     DraggingPhoto = None

              return True

 

      

 

def main(args):    

       if len(args) < 2:

              print "The number of arguments is less than 2!"

              return -1

 

       path=args[1]

       if not os.path.exists(path):

              print path, "doesn't exist!"

              return -1

 

       window = gtk.Window()

       window.connect("destroy-event", gtk.main_quit)

       window.set_title("Clutter Photo Viewer")

 

       vbox = gtk.VBox(False, 0)

       window.add(vbox)

       toolbar = gtk.Toolbar()

       tbutton = gtk.ToolButton()

       tbutton.set_label("Flimstrip")

       toolbar.insert(tbutton, -1)   

 

       embed = cluttergtk.Embed()

       vbox.pack_start(toolbar, False, False, 0)

       vbox.pack_start(embed, True, True, 0)

       embed.set_size_request(STAGE_WIDTH, STAGE_HEIGHT)

      

       embed.realize()

 

       stage = embed.get_stage()

       stage.set_size(STAGE_WIDTH, STAGE_HEIGHT)

       stage.set_color(clutter.Color(0x00, 0x00, 0x00, 0x00))   

 

       if not path.endswith(os.sep):

              path+=os.sep

 

       filelist = os.listdir(path)

      

       for item in filelist:

              pic=Photo(path+item, stage)

              pic.set_random_position()

             

       window.show_all()

      

       gtk.main()    

       return 0

 

 

if __name__ == '__main__':

       main(sys.argv)


作者: 方亮 (Intel)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21255398/viewspace-594847/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-05-12

  • 博文量
    37
  • 访问量
    34429