Прикреплённый файл «win.py»

Загрузка

   1 #!/usr/bin/python
   2 # coding: utf-8
   3 # vim: expandtab:ts=4:sw=4
   4 '''
   5 Написать «оконную систему», состоящую из классов
   6 
   7         * Экран (контейнер окон, позволяет манипулировать окнами)
   8                     o Список окон, определение окна-получателя события
   9                     o Глубина окон, изменение глубины окон
  10                     o Перемещение и изменение размера окон 
  11         * Окно (позволяет манипулировать своим содержимым)
  12 
  13 win.py: классы
  14 '''
  15 
  16 class descriptor:
  17     '''Window descritor'''
  18     def __init__(self, rect, window, mode=["basic"]):
  19         '''Create a window descriptor object with defaut basic mode'''
  20         self.rect=rect
  21         self.window=window
  22         if type(mode) is str:
  23             self.mode=[mode]
  24         else:
  25             self.mode=mode or ["basic"]
  26 
  27 class screen:
  28     '''Windows container'''
  29     def __init__(self, surface, bg=(0,0,0)):
  30         '''Create a screen using given surface'''
  31         self.surface = surface
  32         self.windows = []
  33         self.bg = bg
  34 
  35     def append(self, rect, mode, creator, *args, **argn):
  36         '''Call creator() with *args, **argn
  37         to produce a window (of mode type) on rect-sized subsurface
  38         and put the window in the top'''
  39         wnd = creator(self.surface.subsurface(rect), *args, **argn)
  40         self.windows.append(descriptor(rect,wnd,mode))
  41         return wnd
  42 
  43     def pop(self, index=-1):
  44         '''Unregister corresponded window and return it'''
  45         if self.windows:
  46             return self.windows.pop(index)
  47         else:
  48             return None
  49 
  50     def clear(self):
  51         '''Draw initial screen's surface without all windows'''
  52         self.surface.fill(self.bg)
  53 
  54     def redraw(self):
  55         '''Redraw all the contents'''
  56         self.clear()
  57         for wnd in self.windows:
  58             wnd.window.redraw()
  59 
  60     def up(self, index):
  61         '''Put indexed window deeper'''
  62         if len(self.windows) > 1 and index < len(self.windows):
  63             self.windows[index:index+2]=self.windows[index+1:index-1:-1]
  64 
  65     def down(self, index):
  66         '''Pull indexed window upper'''
  67         if len(self.windows) > 1 and index > 0:
  68             self.windows[index-1:index+1]=self.windows[index:index-2:-1]
  69 
  70     def top(self, index):
  71         '''Bring indexed window to the top'''
  72         if index < len(self.windows):
  73             self.windows[index],self.windows[-1]=self.windows[-1],self.windows[index]
  74 
  75     def locate(self, pos):
  76         '''Locate a window than owns visible pos coordinate.
  77         If not found return -1'''
  78         for i in xrange(len(self.windows)-1, -1, -1):
  79             if self.windows[i].rect.collidepoint(pos):
  80                 return i
  81         return -1
  82 
  83     def handler(self, event, pos):
  84         '''Handle an event that not correspond any window'''
  85         return False
  86 
  87     def proceed(self, event, pos):
  88         '''Check if the event is to be handled by certain window
  89         and let the window do it. If no appropriete windows found 
  90         or window nas no idea how to handle an event, handle it manually.
  91         Return false if no event handler is provided.'''
  92         ret=False
  93         index=self.locate(pos)
  94         if index>=0:
  95             wpos=(pos[0]-self.windows[index].rect.left,pos[1]-self.windows[index].rect.top)
  96             ret=self.windows[index].window.proceed(event,wpos)
  97         return ret or self.handler(event, pos)
  98 
  99 class basic_window:
 100     '''Basic type window'''
 101     def __init__(self, surface, bg=(0,0,0)):
 102         '''Create a window based on given surface'''
 103         self.surface = surface
 104         self.bg = bg
 105 
 106     def renew(self,surface):
 107         '''Stck window to a new surface'''
 108         self.surface = surface
 109         
 110     def redraw(self):
 111         self.surface.fill(self.bg)
 112 
 113     def proceed(self, event, pos):
 114         return False

Прикреплённые файлы

Для ссылки на прикреплённый файл в тексте страницы напишите attachment:имяфайла, как показано ниже в списке файлов. Не используйте URL из ссылки «[получить]», так как он чисто внутренний и может измениться.

Вам нельзя прикреплять файлы к этой странице.