[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2312] trunk/py/scripts/addons/ space_view3d_screencast_keys.py: Updates for space_view3d_screencast_keys. py

Brendon Murphy meta.androcto1 at gmail.com
Wed Sep 7 11:26:52 CEST 2011


Revision: 2312
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2312
Author:   meta-androcto
Date:     2011-09-07 09:26:52 +0000 (Wed, 07 Sep 2011)
Log Message:
-----------
Updates for space_view3d_screencast_keys.py
adds a Mouse&text grouped modus (with a visual boundingBox), mouse and font be sized separately,
settings will be stored along with the blend file now.

Modified Paths:
--------------
    trunk/py/scripts/addons/space_view3d_screencast_keys.py

Modified: trunk/py/scripts/addons/space_view3d_screencast_keys.py
===================================================================
--- trunk/py/scripts/addons/space_view3d_screencast_keys.py	2011-09-07 00:37:05 UTC (rev 2311)
+++ trunk/py/scripts/addons/space_view3d_screencast_keys.py	2011-09-07 09:26:52 UTC (rev 2312)
@@ -22,9 +22,9 @@
 bl_info = {
     'name': 'Screencast Keys',
     'author': 'Paulo Gomes, Bart Crouch, John E. Herrenyo',
-    'version': (1, 2),
+    'version': (1, 3),
     'blender': (2, 5, 9),
-    'api': 39576,
+    'api': 39933,
     'location': 'View3D > Properties panel > Screencast Keys',
     'warning': '',
     'description': 'Display keys pressed in the 3d-view, '\
@@ -41,24 +41,63 @@
 import bpy
 import time
 
+MOUSE_RATIO = 0.535
 
+def getDisplayLocation(context):
+    sc   = context.scene
+    mouse_size = sc.screencast_keys_mouse_size
+    pos_x = int( (context.region.width  - mouse_size*MOUSE_RATIO) * sc.screencast_keys_pos_x / 100)
+    pos_y = int( (context.region.height - mouse_size) * sc.screencast_keys_pos_y / 100)
+    return pos_x, pos_y
+
+def getBoundingBox(current_width, current_height, new_text):
+    w,h = blf.dimensions(0,new_text)
+    if w > current_width:
+        current_width = w
+    current_height += h
+    return current_width, current_height
+
 def draw_callback_px(self, context):
     wm = context.window_manager
-    if not wm.display_keys:
+    sc = context.scene
+    if not wm.screencast_keys_keys:
         return
-    
+   
+    font_size  = sc.screencast_keys_font_size
+    mouse_size = sc.screencast_keys_mouse_size
+    link       = sc.screencast_keys_link
+    pos_x, pos_y = getDisplayLocation(context)
+
+
     # draw text in the 3d-view
-    blf.size(0, wm.display_font_size, 72)
-    r, g, b = wm.display_color
+    # ========================
+    blf.size(0, sc.screencast_keys_font_size, 72)
+    r, g, b = sc.screencast_keys_color
     final = 0
+    row_count = len(self.key)
+
+    keypos_x = pos_x
+    if link==True:
+        keypos_x += mouse_size * MOUSE_RATIO * 1.3
+    shift = 0
+    if mouse_size > font_size*row_count:
+        shift = (mouse_size - font_size*row_count) / 2
+
+    text_width, text_height = 0,0
+    row_count = 0
+    alpha = 1.0
     for i in range(len(self.key)):
         label_time = time.time() - self.time[i]
         if label_time < 2: # only display key-presses of last 2 seconds
-            blf.position(0, wm.display_pos_x,
-                wm.display_pos_y + wm.display_font_size*i, 0)
+
+            keypos_y = pos_y + shift + font_size*(i+0.1) 
+
+            blf.position(0, keypos_x, keypos_y , 0)
             alpha = min(1.0, max(0.0, 2 * (2 - label_time)))
             bgl.glColor4f(r, g, b, alpha)
             blf.draw(0, self.key[i])
+            text_width, text_height = getBoundingBox(text_width, text_height, self.key[i])
+            row_count += 1
             final = i
         else:
             break
@@ -66,12 +105,15 @@
     # get rid of status texts that aren't displayed anymore
     self.key = self.key[:final+1]
     self.time = self.time[:final+1]
-    
+  
+ 
     # draw graphical representation of the mouse
-    if wm.display_mouse == 'graphic':
+    # ==========================================
+    if sc.screencast_keys_mouse == 'icon':
         for shape in ["mouse", "left_button", "middle_button", "right_button"]:
             draw_mouse(context, shape, "outline", 0.5)
         final = 0
+
         for i in range(len(self.mouse)):
             click_time = time.time() - self.mouse_time[i]
             if click_time < 2:
@@ -82,22 +124,71 @@
                 final = i
             else:
                 break
+
     
     # get rid of mouse clicks that aren't displayed anymore
     self.mouse = self.mouse[:final+1]
     self.mouse_time = self.mouse_time[:final+1]
 
 
+    # Draw border (if enabled)
+    # ========================
+    if link == True and row_count > 0:
+        bgl.glEnable(bgl.GL_BLEND)
+        bgl.glBegin(bgl.GL_QUADS)
+        bgl.glLineWidth(2)
+        bgl.glColor4f(r, g, b, 0.2)
+        drawRectangle(pos_x , pos_y , text_width+mouse_size*MOUSE_RATIO*1.3 , max(mouse_size, font_size*row_count), 4 )
+        bgl.glEnd()
+        bgl.glBegin(bgl.GL_LINES)
+        bgl.glColor4f(r, g, b, alpha )
+        drawRectangle(pos_x , pos_y , text_width+mouse_size*MOUSE_RATIO*1.3 , max(mouse_size, font_size*row_count), 4 )
+        bgl.glEnd()
+
+
+# Draw a line. currently not used.
+def drawLinef(from_x, from_y, to_x, to_y):
+    bgl.glVertex2f(from_x, from_y)
+    bgl.glVertex2f(to_x, to_y)
+
+
+# Draw a rectangle. Currently not used
+def drawRectangle (ox, oy, ow, oh, padding=0):
+
+    x = ox - 2*padding
+    y = oy - padding
+    w = ow + 4 * padding
+    h = oh + 2 * padding
+
+    drawLinef(x,   y,   x+w, y)
+    drawLinef(x+w, y,   x+w, y+h)
+    drawLinef(x+w, y+h, x  , y+h)
+    drawLinef(x  , y+h, x  ,   y)
+
+
 def draw_mouse(context, shape, style, alpha):
     # shape and position
-    wm = context.window_manager
-    size = wm.display_font_size * 3
-    offset_x = context.region.width - wm.display_pos_x - (size*0.535)
-    offset_y = wm.display_pos_y
+    sc   = context.scene
+    mouse_size = sc.screencast_keys_mouse_size
+    font_size  = sc.screencast_keys_font_size
+    link = sc.screencast_keys_link
+
+    pos_x, pos_y = getDisplayLocation(context)
+    if link==True:
+        offset_x = pos_x
+    else:
+        offset_x = context.region.width - pos_x - (mouse_size * MOUSE_RATIO)
+
+    offset_y = pos_y
+    if font_size > mouse_size:
+        offset_y += (font_size - mouse_size) / 2
+ 
     shape_data = get_shape_data(shape)
+
     bgl.glTranslatef(offset_x, offset_y, 0)
+
     # color
-    r, g, b = wm.display_color
+    r, g, b = sc.screencast_keys_color
     bgl.glEnable(bgl.GL_BLEND)
     #bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
     #bgl.glColor4f(r, g, b, alpha)
@@ -111,10 +202,10 @@
     # outer shape
     for i in shape_data:
         shape_segment = i
-        shape_segment[0] = [size * k for k in shape_segment[0]]
-        shape_segment[1] = [size * k for k in shape_segment[1]]
-        shape_segment[2] = [size * k for k in shape_segment[2]]
-        shape_segment[3] = [size * k for k in shape_segment[3]]
+        shape_segment[0] = [mouse_size * k for k in shape_segment[0]]
+        shape_segment[1] = [mouse_size * k for k in shape_segment[1]]
+        shape_segment[2] = [mouse_size * k for k in shape_segment[2]]
+        shape_segment[3] = [mouse_size * k for k in shape_segment[3]]
         
         # create the buffer
         shape_buffer = bgl.Buffer(bgl.GL_FLOAT, [4, 3], shape_segment)
@@ -123,6 +214,7 @@
         bgl.glMap1f(bgl.GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, shape_buffer)
         bgl.glEnable(bgl.GL_MAP1_VERTEX_3)
         bgl.glColor4f(r, g, b, alpha)
+
         
         if style == "outline":
             bgl.glBegin(bgl.GL_LINE_STRIP)
@@ -142,7 +234,7 @@
         bgl.glColor4f(r, g, b, alpha)
         bgl.glBegin(bgl.GL_TRIANGLE_FAN)
         for i in inner_shape:
-            j = [size * k for k in i]
+            j = [mouse_size * k for k in i]
             x, y, z = j
             bgl.glVertex3f(x, y, z)
         bgl.glEnd()
@@ -275,7 +367,6 @@
         shape = "middle_button"
     elif event == 'RIGHTMOUSE':
         shape = "right_button"
-    
     return(shape)
 
 
@@ -288,12 +379,13 @@
         if context.area:
             context.area.tag_redraw()
         wm = context.window_manager
+        sc = context.scene
         # keys that shouldn't show up in the 3d-view
         mouse_keys = ['MOUSEMOVE','MIDDLEMOUSE','LEFTMOUSE',
          'RIGHTMOUSE', 'WHEELDOWNMOUSE','WHEELUPMOUSE']
         ignore_keys = ['LEFT_SHIFT', 'RIGHT_SHIFT', 'LEFT_ALT',
          'RIGHT_ALT', 'LEFT_CTRL', 'RIGHT_CTRL', 'TIMER']
-        if wm.display_mouse != 'text':
+        if sc.screencast_keys_mouse != 'text':
             ignore_keys.extend(mouse_keys)
 
         if event.value == 'PRESS':
@@ -320,35 +412,36 @@
                             sc_amount = " x2"
                         del self.key[0]
                         del self.time[0]
-           
+         
             if event.type not in ignore_keys:
                 sc_keys.append(event.type)
                 self.key.insert(0, "+ ".join(sc_keys) + sc_amount)
                 self.time.insert(0, time.time())
-            
-            elif event.type in mouse_keys and wm.display_mouse == 'graphic':
+             
+            elif event.type in mouse_keys and sc.screencast_keys_mouse == 'icon':
                 self.mouse.insert(0, event.type)
-                self.mouse_time.insert(0, time.time())
-        
-        if not context.window_manager.display_keys:
+                self.mouse_time.insert(0, time.time()) 
+
+        if not context.window_manager.screencast_keys_keys:
             # stop script
             context.region.callback_remove(self._handle)
             return {'CANCELLED'}
 
         return {'PASS_THROUGH'}
 
+
     def cancel(self, context):
-        if context.window_manager.display_keys:
+        if context.window_manager.screencast_keys_keys:
             context.region.callback_remove(self._handle)
-            context.window_manager.display_keys = False
-
+            context.window_manager.screencast_keys_keys = False
         return {'CANCELLED'}
 
+
     def invoke(self, context, event):
         if context.area.type == 'VIEW_3D':
-            if context.window_manager.display_keys == False:
+            if context.window_manager.screencast_keys_keys == False:
                 # operator is called for the first time, start everything
-                context.window_manager.display_keys = True
+                context.window_manager.screencast_keys_keys = True
                 context.window_manager.modal_handler_add(self)
                 self.key = []
                 self.time = []
@@ -359,7 +452,7 @@
                 return {'RUNNING_MODAL'}
             else:
                 # operator is called again, stop displaying
-                context.window_manager.display_keys = False
+                context.window_manager.screencast_keys_keys = False
                 self.key = []
                 self.time = []
                 self.mouse = []
@@ -372,42 +465,70 @@
 
 # properties used by the script
 def init_properties():
-    bpy.types.WindowManager.display_keys = bpy.props.BoolProperty(
-        default=False)

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list