Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # grid menu example
- # Joe, 13/12/2014
- import appuifw as A
- import graphics as G
- class Grid:
- def __init__(self, num=6, pos=(5,5), pad=2, bgcolor=0xbde72f, color=0xfcfcfc, focus_color=0x00af00, txtcolor=0, font='legend', fill=False):
- self.w,self.h=G.sysinfo.display_pixels()
- self.img=G.Image.new((self.w,self.h))
- self.run,self.clicked,self.filled=False,False,fill
- self.cols=self.rows=num
- self.px,self.py=pos
- self.pad=pad
- self.width=(self.w - self.px - (self.pad*self.cols))/self.cols
- self.color=color
- self.bgcolor=bgcolor
- self.focus_color=focus_color
- self.txtcolor,self.font=txtcolor,font
- self.curx,self.cury=0,0
- self.hint=True
- A.app.screen='full'
- self.c=A.Canvas(redraw_callback=self._redraw,event_callback=self._event)
- A.app.body=self.c
- A.app.exit_key_handler=self.exit
- def exit(self):
- A.app.set_exit()
- def _redraw(self,rect):
- if self.img:
- self.c.blit(self.img)
- def _event(self,sc):
- k=sc['scancode']
- if sc['type'] is not A.EEventKey:return
- self.hint=False
- if self.clicked:
- self.clicked=False
- if k==0xf: # kanan
- self.curx+=1
- if self.curx>self.rows-1:
- self.curx=0
- self.cury+=1
- if self.cury>self.cols-1:
- self.cury=0
- self.curx=0
- elif k==0xe: # kiri
- self.curx-=1
- if self.curx<0:
- self.curx=self.rows-1
- self.cury-=1
- if self.cury<0:
- self.cury=self.cols-1
- self.curx=self.rows-1
- elif k==0x10: # atas
- self.cury-=1
- if self.cury<0:
- self.cury=self.cols-1
- elif k==0x11: # bawah
- self.cury+=1
- if self.cury>self.cols-1:
- self.cury=0
- elif k in (0xa7,0x35): # OK / 5
- self.clicked=True
- def _tsize(self,text):
- if not isinstance(text,unicode):
- text=unicode(text)
- m=self.img.measure_text(text,font=self.font)[0]
- w,h=(m[2]-m[0],m[3]-m[1])
- return (w,h)
- def _draw_note(self,msg):
- x,y=self.w/2,self.h/2
- (tw,th)=self._tsize(msg)
- self.img.rectangle((x-(tw/2)-10,y-th,x+(tw/2)+10,y+(th/2)),fill=self.bgcolor)
- self.img.text((x-(tw/2),y),u'%s' %msg,self.txtcolor,self.font)
- def draw(self):
- self.run=True
- while self.run:
- self.img.clear(self.bgcolor)
- for i in range(self.cols):
- for j in range(self.rows):
- dx = self.px+(j*self.width)+(j*self.pad)
- dy = self.py+(i*self.width)+(i*self.pad)
- self.img.rectangle((dx,dy,dx+self.width,dy+self.width),fill=self.color)
- dx = self.px+(self.curx*self.width)+(self.curx*self.pad)
- dy = self.py+(self.cury*self.width)+(self.cury*self.pad)
- 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])
- if self.clicked:
- self._draw_note('Dipilih: x=%d, y=%d' %(self.curx,self.cury))
- if self.hint:
- self._draw_note('Grid Menu Example')
- self._redraw(0)
- A.e32.ao_sleep(1e-04)
- '''
- Argumen keyword:
- num = jumlah grid (int)
- pos = posisi (x,y) (tuple x dan y)
- pad = jarak antar kotak (int)
- bgcolor = warna dasar (kode warna)
- color = warna kotak (kode warna)
- focus_color = warna kursor (kode warna)
- txtcolor = warna teks (kode warna)
- font = font teks (kode warna)
- fill = kursor diisi warna atau hanya border nya saja (True / False)
- '''
- grid=Grid()
- #grid=Grid(fill=True)
- grid.draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement