Advertisement
phjoe

Grid Menu

Dec 12th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # grid menu example
  2. # Joe, 13/12/2014
  3.  
  4. import appuifw as A
  5. import graphics as G
  6.  
  7. class Grid:
  8.  def __init__(self, num=6, pos=(5,5), pad=2, bgcolor=0xbde72f, color=0xfcfcfc, focus_color=0x00af00, txtcolor=0, font='legend', fill=False):
  9.   self.w,self.h=G.sysinfo.display_pixels()
  10.   self.img=G.Image.new((self.w,self.h))
  11.   self.run,self.clicked,self.filled=False,False,fill
  12.   self.cols=self.rows=num
  13.   self.px,self.py=pos
  14.   self.pad=pad
  15.   self.width=(self.w - self.px - (self.pad*self.cols))/self.cols
  16.   self.color=color
  17.   self.bgcolor=bgcolor
  18.   self.focus_color=focus_color
  19.   self.txtcolor,self.font=txtcolor,font
  20.   self.curx,self.cury=0,0
  21.   self.hint=True
  22.   A.app.screen='full'
  23.   self.c=A.Canvas(redraw_callback=self._redraw,event_callback=self._event)
  24.   A.app.body=self.c
  25.   A.app.exit_key_handler=self.exit
  26.  
  27.  def exit(self):
  28.   A.app.set_exit()
  29.  
  30.  def _redraw(self,rect):
  31.   if self.img:
  32.    self.c.blit(self.img)
  33.  
  34.  def _event(self,sc):
  35.   k=sc['scancode']
  36.   if sc['type'] is not A.EEventKey:return
  37.   self.hint=False
  38.   if self.clicked:
  39.    self.clicked=False
  40.   if k==0xf: # kanan
  41.    self.curx+=1
  42.    if self.curx>self.rows-1:
  43.     self.curx=0
  44.     self.cury+=1
  45.     if self.cury>self.cols-1:
  46.      self.cury=0
  47.      self.curx=0
  48.   elif k==0xe: # kiri
  49.    self.curx-=1
  50.    if self.curx<0:
  51.     self.curx=self.rows-1
  52.     self.cury-=1
  53.     if self.cury<0:
  54.      self.cury=self.cols-1
  55.      self.curx=self.rows-1
  56.   elif k==0x10: # atas
  57.    self.cury-=1
  58.    if self.cury<0:
  59.     self.cury=self.cols-1
  60.   elif k==0x11: # bawah
  61.    self.cury+=1
  62.    if self.cury>self.cols-1:
  63.     self.cury=0
  64.   elif k in (0xa7,0x35): # OK / 5
  65.    self.clicked=True
  66.  
  67.  def _tsize(self,text):
  68.   if not isinstance(text,unicode):
  69.    text=unicode(text)
  70.   m=self.img.measure_text(text,font=self.font)[0]
  71.   w,h=(m[2]-m[0],m[3]-m[1])
  72.   return (w,h)
  73.  
  74.  def _draw_note(self,msg):
  75.   x,y=self.w/2,self.h/2
  76.   (tw,th)=self._tsize(msg)
  77.   self.img.rectangle((x-(tw/2)-10,y-th,x+(tw/2)+10,y+(th/2)),fill=self.bgcolor)
  78.   self.img.text((x-(tw/2),y),u'%s' %msg,self.txtcolor,self.font)
  79.  
  80.  def draw(self):
  81.   self.run=True
  82.   while self.run:
  83.    self.img.clear(self.bgcolor)
  84.    for i in range(self.cols):
  85.     for j in range(self.rows):
  86.      dx = self.px+(j*self.width)+(j*self.pad)
  87.      dy = self.py+(i*self.width)+(i*self.pad)
  88.      self.img.rectangle((dx,dy,dx+self.width,dy+self.width),fill=self.color)
  89.    dx = self.px+(self.curx*self.width)+(self.curx*self.pad)
  90.    dy = self.py+(self.cury*self.width)+(self.cury*self.pad)
  91.    self.img.rectangle((dx,dy,dx+self.width,dy+self.width),self.focus_color,fill=(None,self.focus_color)[self.filled],width=(self.pad,0)[self.filled])
  92.    if self.clicked:
  93.     self._draw_note('Dipilih: x=%d, y=%d' %(self.curx,self.cury))
  94.    if self.hint:
  95.     self._draw_note('Grid Menu Example')
  96.    self._redraw(0)
  97.    A.e32.ao_sleep(1e-04)
  98.  
  99.  
  100. '''
  101. Argumen keyword:
  102. num = jumlah grid (int)
  103. pos = posisi (x,y) (tuple x dan y)
  104. pad = jarak antar kotak (int)
  105. bgcolor = warna dasar (kode warna)
  106. color = warna kotak (kode warna)
  107. focus_color = warna kursor (kode warna)
  108. txtcolor = warna teks (kode warna)
  109. font = font teks (kode warna)
  110. fill = kursor diisi warna atau hanya border nya saja (True / False)
  111. '''
  112.  
  113. grid=Grid()
  114. #grid=Grid(fill=True)
  115. grid.draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement