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

在上次的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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/21255398/viewspace-594847/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值