home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / onboard / Key.py < prev    next >
Encoding:
Python Source  |  2007-02-20  |  7.7 KB  |  265 lines

  1. import pango
  2. from math import sqrt
  3.  
  4. BASE_PANE_TAB_HEIGHT = 40
  5.  
  6. class Key:
  7.     def __init__(self,pane):
  8.         self.pane = pane
  9.         self.actions = [False,False,False,False,False,False] #Dealt with in keyboard.py, press_key.
  10.         self.on = False
  11.         self.stuckOn = False # On when key is sticky and pressed twice in a row.
  12.         
  13.         self.beingScanned = False 
  14.         
  15.     def set_properties(self, actions, labels, sticky, fontOffsetX, fontOffsetY):
  16.         self.fontOffsetX=fontOffsetX #Mostly for odd shaped keys.
  17.         self.fontOffsetY=fontOffsetY
  18.         self.actions = actions
  19.         self.sticky = sticky
  20.         self.labels = labels
  21.     
  22.     def paintFont(self,fontContext, xScale, yScale, x, y):
  23.         
  24.             if xScale<yScale:
  25.                 fontScale = xScale
  26.             else: 
  27.                 fontScale = yScale # oddly python doesn't do scope in if statements.
  28.             
  29.             fontContext.move_to((x + self.fontOffsetX)*xScale + 4, (y +self.fontOffsetY)*yScale - 0.03*self.pane.fontSize*sqrt(fontScale))
  30.  
  31.         if self.pane.sok.mods[1]:
  32.                 if self.pane.sok.mods[128] and self.labels[4]:
  33.                     self.layout = self.pane.da.create_pango_layout(self.labels[4])
  34.                 elif self.labels[2]:
  35.                 self.layout = self.pane.da.create_pango_layout(self.labels[2])
  36.             elif self.labels[1]:
  37.                 self.layout = self.pane.da.create_pango_layout(self.labels[1])
  38.             else:
  39.                 self.layout = self.pane.da.create_pango_layout(self.labels[0])
  40.         
  41.         elif self.pane.sok.mods[128] and self.labels[4]:
  42.             self.layout = self.pane.da.create_pango_layout(self.labels[3])
  43.         
  44.         elif self.pane.sok.mods[2]:
  45.             if self.labels[1]:
  46.                 self.layout = self.pane.da.create_pango_layout(self.labels[1])
  47.             else:
  48.                 self.layout = self.pane.da.create_pango_layout(self.labels[0])        
  49.         else:
  50.             self.layout = self.pane.da.create_pango_layout(self.labels[0])
  51.         
  52.  
  53.  
  54.             self.layout.set_font_description(pango.FontDescription("Sans Serif %d" %(fontScale*self.pane.fontSize)))
  55.             fontContext.update_layout(self.layout)
  56.             
  57.             fontContext.show_layout(self.layout)
  58.         
  59.         
  60. class TabKey(Key):
  61.    "class for those tabs up the right hand side"   
  62.    def __init__(self,keyboard,width,pane):
  63.     Key.__init__(self,pane)    
  64.         self.width = width
  65.  
  66.         self.keyboard = keyboard
  67.     
  68.     self.modifier = None#what for?
  69.  
  70.     self.sticky = True
  71.  
  72.    def point_within_key(self,mouseX,mouseY):
  73.     if (mouseX > self.keyboard.kbwidth 
  74.         and mouseY > self.height*self.index + BASE_PANE_TAB_HEIGHT 
  75.         and mouseY < self.height*(self.index + 1)+ BASE_PANE_TAB_HEIGHT):
  76.             return True
  77.     return False
  78.  
  79.    
  80.    def paint(self,context):
  81.     self.height = self.keyboard.height/len(self.keyboard.panes) - BASE_PANE_TAB_HEIGHT/len(self.keyboard.panes)
  82.     self.index = self.keyboard.panes.index(self.pane)
  83.     
  84.  
  85.     context.rectangle(self.keyboard.kbwidth,self.height*self.index + BASE_PANE_TAB_HEIGHT,self.width, self.height)
  86.  
  87.     if self.pane == self.keyboard.activePane and self.stuckOn:
  88.             context.set_source_rgba(1, 0, 0,1)
  89.     else:        
  90.         context.set_source_rgba(float(self.pane.rgba[0]), float(self.pane.rgba[1]),float(self.pane.rgba[2]),float(self.pane.rgba[3]))
  91.         
  92.     context.fill()
  93.  
  94.     
  95. class BaseTabKey(Key):
  96.    "class for the tab that brings you to the base pane"   
  97.    def __init__(self,keyboard,width):
  98.     Key.__init__(self,None)    
  99.         self.width = width
  100.  
  101.         self.keyboard = keyboard
  102.     
  103.     self.modifier = None#what for?
  104.  
  105.     self.sticky = False
  106.  
  107.    def point_within_key(self,mouseX,mouseY):
  108.     if (mouseX > self.keyboard.kbwidth 
  109.         and mouseY < BASE_PANE_TAB_HEIGHT):
  110.             return True
  111.     return False
  112.  
  113.    
  114.    def paint(self,context):
  115.     
  116.     return       
  117.  
  118. class LineKey(Key):
  119.     "class for keyboard buttons made of lines"
  120.     def __init__(self, pane, coordList, rgba):
  121.         Key.__init__(self,pane)
  122.         self.coordList = coordList
  123.         self.rgba = rgba
  124.             
  125.         
  126.         
  127.     def set_properties(self, actions, labels, sticky, fontOffsetX, fontOffsetY):
  128.         Key.set_properties(self,actions,labels,sticky,fontOffsetX, fontOffsetY)
  129.  
  130.         
  131.     def point_crosses_edge(self,x,y,xp1,yp1,sMouseX,sMouseY):
  132.         if ((((y<=sMouseY) and (sMouseY<yp1)) or  
  133.             ((yp1<=sMouseY) and (sMouseY<y))) and 
  134.             (sMouseX < (xp1 - x) * (sMouseY - y) / (yp1 - y) + x)):
  135.                             return True
  136.                             
  137.         else:
  138.             return False
  139.         
  140.     
  141.     def point_within_key(self,mouseX,mouseY):
  142.         "Checks whether point is within shape.  Currently does not bother trying to work out curved paths accurately."
  143.         x = self.coordList[0]
  144.         y = self.coordList[1]
  145.         c = 2
  146.         coordLen = len(self.coordList)
  147.         within = False
  148.         
  149.         sMouseX = mouseX/self.pane.xScale
  150.         sMouseY = mouseY/self.pane.yScale
  151.         
  152.         while not c == coordLen:
  153.  
  154.             xp1 = self.coordList[c+1]
  155.             yp1 = self.coordList[c+2]
  156.             try:
  157.                 if self.coordList[c] == "L":
  158.                     within = (self.point_crosses_edge(x,y,xp1,yp1,sMouseX,sMouseY) ^ within) # a xor        
  159.                     c +=3
  160.                     x = xp1
  161.                     y = yp1
  162.                         
  163.                 else:    
  164.                     xp2 = self.coordList[c+3]
  165.                     yp2 = self.coordList[c+4]
  166.                     xp3 = self.coordList[c+5]
  167.                     yp3 = self.coordList[c+6]
  168.                     within = (self.point_crosses_edge(x,y,xp3,yp3,sMouseX,sMouseY) ^ within) # a xor        i
  169.                     x = xp3
  170.                     y = yp3
  171.                     c += 7
  172.  
  173.                 
  174.  
  175.             except ZeroDivisionError, (strerror):
  176.                 print strerror
  177.                 print "x: %f, y: %f, yp1: %f" % (x,y,yp1)
  178.         return within
  179.         
  180.         def paint(self,context,xScale,yScale):
  181.         c = 2
  182.         context.move_to(self.coordList[0]*xScale, self.coordList[1]*yScale)
  183.         while not c == len(self.coordList):
  184.             xp1 = self.coordList[c+1]*xScale
  185.             yp1 = self.coordList[c+2]*yScale
  186.             try:
  187.                 if self.coordList[c] == "L":
  188.                     c +=3
  189.                     context.line_to(xp1,yp1)
  190.                 else:    
  191.                     xp2 = self.coordList[c+3]*xScale
  192.                     yp2 = self.coordList[c+4]*yScale
  193.                     xp3 = self.coordList[c+5]*xScale
  194.                     yp3 = self.coordList[c+6]*yScale
  195.                     context.curve_to(xp1,yp1,xp2,yp2,xp3,yp3)
  196.                     c += 7
  197.                     
  198.             except TypeError, (strerror):
  199.                 print x
  200.                 print y
  201.                 print xp1
  202.                 print yp1
  203.                 print strerror
  204.                 
  205.                 if (self.stuckOn):
  206.                     context.set_source_rgba(1, 0, 0,1)
  207.             elif (self.on):
  208.                         context.set_source_rgba(0.5, 0.5, 0.5,1)
  209.                     elif (self.beingScanned):    
  210.                         context.set_source_rgba(0.45,0.45,0.7,1)
  211.                 else:
  212.                         context.set_source_rgba(self.rgba[0], self.rgba[1],self.rgba[2],self.rgba[3])
  213.     
  214.                 context.fill_preserve()
  215.                 context.set_source_rgb(0, 0, 0)
  216.                 
  217.         def paintFont(self,fontContext, xScale, yScale):
  218.             Key.paintFont(self,fontContext, xScale, yScale, self.coordList[0], self.coordList[1])
  219.             
  220.     
  221.     
  222. class RectKey(Key):
  223.     "Class for rectangular keyboard buttons"
  224.     def __init__(self,pane,x,y,width,height,rgba):
  225.         Key.__init__(self,pane)
  226.     self.x = x
  227.         self.y = y
  228.         self.width = width
  229.         self.height = height
  230.     self.rgba = rgba      
  231.       
  232.     def set_properties(self, actions,labels,sticky,fontOffsetX,fontOffsetY):
  233.     Key.set_properties(self, actions,labels,sticky,fontOffsetX,fontOffsetY)
  234.     
  235.  
  236.     def point_within_key(self,mouseX,mouseY):
  237.         if(mouseX/self.pane.xScale>self.x and mouseX/self.pane.xScale<(self.x+self.width)):
  238.             if(mouseY/self.pane.yScale>self.y and mouseY/self.pane.yScale<(self.y+self.height)):
  239.                 return True
  240.         else:
  241.             return False
  242.          
  243.     
  244.     def paint(self,context,xScale,yScale):
  245.         
  246.     context.rectangle(self.x*xScale,self.y*yScale,self.width*xScale, self.height*yScale)
  247.     
  248.         if (self.stuckOn):
  249.         context.set_source_rgba(1, 0, 0,1)
  250.     elif (self.on):
  251.             context.set_source_rgba(0.5, 0.5, 0.5,1)
  252.         elif (self.beingScanned):    
  253.                         context.set_source_rgba(0.45,0.45,0.7,1)
  254.         else:
  255.             context.set_source_rgba(self.rgba[0], self.rgba[1],self.rgba[2],self.rgba[3])
  256.     
  257.         context.fill_preserve()
  258.         context.set_source_rgb(0, 0, 0)
  259.  
  260.           context.stroke()
  261.         
  262.     def paintFont(self,fontContext, xScale, yScale):
  263.         Key.paintFont(self,fontContext, xScale, yScale, self.x, self.y)
  264.         
  265.