Commits

anatoly techtonik  committed d0c6fae

Further prototype extension. Document pyglet bugs, load monospace
font, implement cursor browser.

  • Participants
  • Parent commits 30c2635

Comments (0)

Files changed (1)

 #   [ ] research other options (style, character sets to draw lines, unicode)
 # [x] get font size to initialize window
 
+# [ ] fix font_family as a list under Linux
+#   [ ] get real font name for loaded font and store requested name/family
+#   [ ] fix have_font under Linux
+
+# Abstract. Font management mechanizm for pyglet. User stories:
+# - load font from specific family (monospace)
+# - load font by name
+# - inspect font is available by family
+# - inspect font is available by name
+# - get real font name and request real name
+# - load system-independent font (bundled with application)
+    
 font_family = ['Terminus', 'Terminal', 'Courier', 'Monospace']
+font_family = 'Monospace'
 render = pyglet.text.Label('A', font_name=font_family)
 font_width, font_height = render.content_width, render.content_height
 
 # [ ] pyglet: load font, query size and use it for labels explicitly instead
 #             of rendering text to get the metrics
+# [ ] bonus: detect how much video memory is available
+#   [ ] how much memory is used by the font
 
 # ---[ window ]--------------------------------------------------------------
 # [x] set window size to 80x25
 #   [ ] try to get coordinate 0 from top left corner instead of bottom left
 
 window = pyglet.window.Window(font_width*80, font_height*25)
+"""
 label = pyglet.text.Label(__doc__, font_name=font_family)
 
 @window.event
 def on_draw():
     window.clear()
     label.draw()
+pyglet.app.run()
+"""
 
 # [ ] read filenames
 # [ ] print filenames
 # [ ] redraw only when there is something to redraw (or F5 pressed)
 #   [ ] detect when window is redrawn
 
+from pyglet.window.key import MOTION_UP, MOTION_DOWN, MOTION_LEFT, MOTION_RIGHT
+from pyglet.window.key import MOTION_BEGINNING_OF_LINE, MOTION_END_OF_LINE
+from pyglet.window.key import MOTION_PREVIOUS_PAGE, MOTION_NEXT_PAGE
+# [ ] other motions
+
+# just a convenient container, no instances are meant to be created
+class Palette(object):
+    foreground = (255, 255, 255, 255)
+    background = (0, 0, 0, 0)
+    active = (255, 0, 255, 255)
+    background_active = (255, 0, 255, 255)
+
+class DNPalette(Palette):
+    '''DOS Navigator Palette'''
+    background = (85, 85, 85, 255)
+    active = (22, 0, 85, 255)
+
+palette = DNPalette
+
+
+class FilePanel(object):
+    # a list of unique directory entries, one entry is always active
+    # [ ] background color
+    def __init__(self):
+        self.height = 25        
+        
+        # every item is (filename, label)
+        self.items = []
+        self.current_index = 0
+        
+    def add_entry(self, name):
+        label = pyglet.text.Label(name, font_name=font_family, anchor_y='top')
+        label.y = (self.height - len(self.items)) * font_height
+        if len(self.items) == self.current_index:
+            label.color = palette.active
+            label.background_color = palette.background_active
+        self.items.append((name, label))
+        
+    def _color_entry(self, idx, foreground, background=None):
+        self.items[idx][1].color = foreground
+        self.items[idx][1].background_color = background
+        
+    def up(self):
+        """Move cursor up"""
+        if self.current_index != 0:
+            self._color_entry(self.current_index, palette.foreground,
+                                                  palette.background)
+            self.current_index -= 1
+            self._color_entry(self.current_index, palette.active,
+                                                  palette.background_active)
+
+    def down(self):
+        """Move cursor down"""
+        if self.current_index < len(filepanel.items)-1:
+            self._color_entry(self.current_index, palette.foreground,
+                                                  palette.background)
+            self.current_index += 1
+            self._color_entry(self.current_index, palette.active,
+                                                  palette.background_active)
+
+filepanel = FilePanel()
+
+# add handler for on_draw event of the window with a decorator
+@window.event
+def on_draw():
+    # clear to the default black background color
+    window.clear()
+    #label.draw()
+    for idx, (name, label) in enumerate(filepanel.items):
+        '''
+        if idx == filepanel.current_item:
+            label.color = (255, 255, 0, 255)
+        else:
+            label.color = (255, 255, 255, 255)
+        '''
+        label.draw()
+
+@window.event
+def on_text_motion(motion):
+    current = filepanel.current_index
+    if motion == MOTION_UP:
+        filepanel.up()
+    if motion == MOTION_DOWN:
+        filepanel.down()
+
+import os
+for name in os.listdir('.'):
+    filepanel.add_entry(name)
 pyglet.app.run()
+
+"""
+
+# terminal font by Simon Tatham
+# http://www.chiark.greenend.org.uk/~sgtatham/fonts/
+"""