使用Moblin进行应用程序开发--Clutter之Event

上次的Demo太简单了,仅仅在窗口中铺了一堆图片,用户只能看,却不能动。这次我们要给之前的Demo增加一个功能:支持鼠标拖动。

这次我们会接触到clutter的事件机制。Actor可以接受输入的事件,比如键盘按键、鼠标移动等事件,并且可以通过回调函数对事件进行处理。由于这一套机制是通过glibsignal来实现的,因此熟悉glibgtk编程的用户对此肯定是相当的熟悉。但是与gtk相比,clutter还是有一点不太相同的地方,就是除了Stage之外,所有的Actor默认不接受事件。如果需要让某个Actor接受事件,程序员需要通过clutter_actor_set_reactive()函数手动的将Actor设为reactive,也就是说能够接受事件。我猜想这样的设计是为了减少事件处理时寻找事件源所花费的时间。

    下图是从clutter参考手册中截下来的,每一个有“?”标记的地方用户都可以插入有关的事件处理的代码。

好了,下面是这次demo的源代码。

#!/usr/bin/python

 

import sys

import os

import random

 

import clutter

 

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

 

       stage = clutter.Stage()

       stage.set_size(STAGE_WIDTH, STAGE_HEIGHT)

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

       stage.connect("destroy", clutter.main_quit)

      

 

       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()

             

       stage.show_all()

       clutter.main()      

 

 

if __name__ == '__main__':

       main(sys.argv)

 

 

基本思路:当鼠标点击某张照片时将照片移至顶部。使用全局变量Dragging来控制进入与退出拖拽模式。DraggingPhoto这个变量的引入有些不得已,可能是我的鼠标扫描速率太低,拖拽时常有指针飞出Photo区域的现象,于是多加了这个变量进行控制。

 

 

Catégories:
Étiquettes:
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.