Commits

anatoly techtonik committed 234c1be

FilePanel: draw centered panel title

Comments (0)

Files changed (1)

 
 Public domain work by anatoly techtonik <techtonik@gmail.com>
 Use MIT License if public domain doesn't make sense for you.
+
+
+File panel works in text mode - screen is divided into
+characters, coordinates and dimensions are soecified in
+characters too.
 """
 
 import os
 
 
 class FilePanel(object):
+    """
+    FilePanel works in text mode, where all coordinates are specified
+    in symbols.
+    """
     # a list of unique directory entries, one entry is always active
     # [ ] background color (pyglet bug)
     # [x] up/down arrows move cursor
     # [x] scrolling
     # [x] border
+    """
+    Element implementation details:
+        - four phases: init, update, prerender, draw
+    """
     def __init__(self, width, height):
-        # file panel dimensions with the border (in symbols)
-        self.height = height
-        self.width = width
-        
+
+        # --- logical state ---
+
         # every item is (filename, label)
         self.items = []
         self.item_idx = 0
-        # offset of starting visible element from the items list
+        # offset of visible window in items list
         self.offset = 0
-        # [ ] resize - correct offset when window height shrinked
         
-        # precalculate border labels
+        self.title = None
+        
+        self.height = height  # in characters
+        self.width = width
+
+        # --- graphical state ---
+
+        self.border_labels = []
+        
+        self.prerender()
+        # [ ] resize
+        #   [ ] correct offset when window height is shrinked
+
+    def prerender(self):
+        """
+        Pre calculate labels and other stuff that will be copied (blit)
+        during the draw phase.
+        """
+        # layer0: borders
         self.border_labels = []
         label = pyglet.text.Label('╔'+ '═'*(self.width-2) + '╗', anchor_y='top',
                     font_name=font_family, x=0, y=font_height*self.height)
             for position,item in enumerate(items):
                 item[1].x = xstart * font_width
                 item[1].y = (ystart + (yspace - position)) * font_height
-        
+
     def up(self):
         """Move cursor up"""
         if self.item_idx == 0:
             self.offset += 1
             self._refresh_label_positions()
             
-    def draw_border(self):
+    def draw(self):
+        # draw layer0 - borders
+        # [ ] TODO - draw border only when window is created/resized
         for label in self.border_labels:
             label.draw()
-
-    def draw(self):
-        self.draw_border()
+            
+        # draw layer1 - title
+        avail_width = self.width-2-2  # width-border-margin
+        label_title = self.title
+        if len(self.title) > avail_width:
+            # [ ] TODO: crop symbols from the middle
+            label_title = ' ' + self.title[:len(self.width)-5] + '...' + ' '
+        # center title label
+        xpos = 3 + avail_width//2 - len(label_title)//2
+        label = pyglet.text.Label(label_title, anchor_y='top',
+                    font_name=font_family,
+                    x=font_width*xpos, y=font_height*self.height)
+        # [ ] TODO label background
+        label.draw()
+        
         yspace = self.height-2
         for idx, (name, label) in enumerate(self.items):
             if idx >= self.offset and idx < self.offset + yspace:
         self.title = os.path.abspath(directory)
         for name in os.listdir(directory):
             self.add_entry(name)
-        
+                    
 filepanel = FilePanel(80, 25)
 
 # add handler for on_draw event of the window with a decorator