def Text(text,pos=None,size=None,**kwargs): theme = UIState.getTheme() pos,size = theme.drawText(text,pos,size,simulate=True,**kwargs) pos,size = UIState.doLayout(pos,size,**kwargs) # theme.drawBox(pos,size,**kwargs) theme.drawText(text,pos,size,**kwargs) UIState.updateLayout(pos,size,**kwargs)
def TextBox(text,pos=None,size=None,**kwargs): theme = UIState.getTheme() pos,size = theme.drawText(text,pos,size,simulate=True,**kwargs) pos,size = UIState.doLayout(pos,size,**kwargs) x,y = pos w,h = size theme.drawBox(pos,size,**kwargs) theme.drawText(text,(x+5,y+5),(w-10,h-10),**kwargs) UIState.updateLayout(pos,size,**kwargs)
def drawIcon(self,icon,pos,size,**kwargs): super(BetterTheme,self).drawIcon(icon,pos,size,**kwargs) draw = UIState.getDraw() color = self._get("iconColor") x,y = pos w,h = size if icon == Icons.Move: draw.drawRect((x,y),(w,h),color) draw.drawCircle((x+w/2,y+h/2),h/4,color) elif icon == Icons.MoveActive: draw.drawRect((x,y),(w,h),color) draw.drawCircle((x+w/2,y+h/2),h/4,color,0) elif icon == Icons.Close: draw.drawRect((x,y),(w,h),color) draw.drawLine((x+2,y+h/2),(x+w-2,y+h/2),color) elif icon == Icons.Open: draw.drawRect((x,y),(w,h),color) draw.drawLine((x+w/2,y+2),(x+w/2,y+h-2),color) elif icon == Icons.Resize: draw.drawRect((x,y),(w,h),color) draw.drawPolygon([(x+w-4,y+4),(x+w-4,y+h-4),(x+4,y+h-4)],color) elif icon == Icons.ResizeActive: draw.drawRect((x,y),(w,h),color) draw.drawPolygon([(x+w-4,y+4),(x+w-4,y+h-4),(x+4,y+h-4)],color,0)
def drawRect(self,pos,size,width,color,**kwargs): super(BetterTheme,self).drawRect(pos,size,width,color,**kwargs) draw = UIState.getDraw() draw.drawRect(pos,size,color,width) return (pos,size)
def _drawButton(self,pos,size,color,borderColor,width,textColor,font,text=None,**kwargs): simulate = "simulate" in kwargs and kwargs['simulate'] if text: tw,th = font.size(text) if not size: size = (tw+20,th+10) if simulate: return (pos,size) x,y = pos w,h = size draw = UIState.getDraw() draw.fillRect(pos,size,color) draw.drawRect(pos,size,borderColor,width) if text: if "upper" in kwargs and kwargs['upper']: text = text.upper() elif "lower" in kwargs and kwargs['lower']: text = text.lower() elif "capitalize" in kwargs and kwargs['capitalize']: text = text.capitalize() fx = size[0]/2-tw/2 fy = size[1]/2-th/2-1 draw.fontRender(font,text,(x+fx,y+fy),textColor) return (pos,size)
def _drawRoundedButton(self,pos,size,color,borderColor,width,textColor,font,text=None,**kwargs): simulate = "simulate" in kwargs and kwargs['simulate'] if text: tw,th = font.size(text) if not size: size = (tw+30,th+10) if simulate: return (pos,size) draw = UIState.getDraw() x,y = pos w,h = size roundedWidth=h/2 draw.drawCircle((x+roundedWidth/2,y+h/2+1),roundedWidth,borderColor,0) draw.drawCircle((x+w-roundedWidth/2,y+h/2+1),roundedWidth,borderColor,0) draw.drawCircle((x+roundedWidth/2,y+h/2+1),roundedWidth-2,color,0) draw.drawCircle((x+w-roundedWidth/2,y+h/2+1),roundedWidth-2,color,0) draw.fillRect((x+roundedWidth/2,y),(w-roundedWidth,h),color) vdrawLine((x+roundedWidth/2,y),(x+w-roundedWidth/2-1,y),borderColor,width) draw.drawLine((x+roundedWidth/2+1,y+h),(x+w-roundedWidth/2-1,y+h),borderColor,width) if text: fx = size[0]/2-tw/2 fy = size[1]/2-th/2 draw.fontRender(font,text,(x+fx,y+fy),textColor) return (pos,size)
def _drawRoundedButton(self,pos,size,color,borderColor,width,textColor,font,text=None,**kwargs): simulate = "simulate" in kwargs and kwargs['simulate'] if text: tw,th = font.size(text) if not size: size = (tw+30,th+10) if simulate: return (pos,size) x,y = pos w,h = size roundedWidth=h/2.0 draw = UIState.getDraw() draw.drawCircle((x+roundedWidth/2.0,y+h/2),roundedWidth,color,0) draw.drawCircle((x+w-roundedWidth/2.0,y+h/2),roundedWidth,color,0) def getArc(pos,startangle,endangle,radius): numdivs = endangle-startangle / 10 sr = math.radians(startangle) er = math.radians(endangle) x,y = pos points=[] for i in range(numdivs): r = sr+(er-sr)/float(numdivs)*i points.append((x+math.cos(r)*radius,y+math.sin(r)*radius)) return points # points = [] # points.extend(getArc((x+roundedWidth/2.0,y+h/2),90,180+90,roundedWidth)) # points.append((x+roundedWidth/2.0,y)) # points.append((x+w-roundedWidth/2,y)) # points.extend(getArc((x+w-roundedWidth/2.0,y+h/2),180+90,180+180+90,roundedWidth)) # points.append((x+roundedWidth/2.0,y+h)) # points.append((x+w-roundedWidth/2,y+h)) draw.drawCircle((x+roundedWidth/2.0,y+h/2),roundedWidth,borderColor,width) draw.drawCircle((x+w-roundedWidth/2.0,y+h/2),roundedWidth,borderColor,width) draw.drawLine((x+roundedWidth/2.0,y),(x+w-roundedWidth/2,y),borderColor,width) draw.drawLine((x+roundedWidth/2.0,y+h),(x+w-roundedWidth/2,y+h),borderColor,width) # draw.drawLines(points,borderColor,False,width) draw.fillRect((x+roundedWidth/2.0,y+1),(w-roundedWidth,h-2),color) if text: fx = size[0]/2-tw/2 fy = size[1]/2-th/2 draw.fontRender(font,text,(x+fx,y+fy),textColor) return (pos,size)
def drawCheckbox(self,value,pos,size,**kwargs): super(BetterTheme,self).drawCheckbox(value,pos,size,**kwargs) draw = UIState.getDraw() self.drawBox(pos,size,None,**kwargs) lcolor = self._get("boxEdgeColor") if value: x,y = pos w,h = size draw.drawLine((x,y),(x+w-1,y+h-2),lcolor,2) draw.drawLine((x+w-2,y),(x,y+h-2),lcolor,2) return (pos,size)
def drawText(self,text,pos,size=None,**kwargs): pos,size = super(BetterTheme,self).drawText(text,pos,size,**kwargs) text = unicode(text) font = self._getFont("textFont") pad = self._get("textBoxPadding") lines = None if size != None: lines = pgui.textUtils.wrapText(text,font,size[0]-pad*2) else: lines = text.splitlines() if not size: fw,fh = pgui.textUtils.getTextExtents(lines,font) size = (fw+pad*2,fh+pad*2) if "simulate" in kwargs and kwargs["simulate"]: return (pos,size) x,y = pos w,h = size color = self._get("textColor") tx = x+pad ty = y+pad doCenter = "center" in kwargs and kwargs["center"] doRight = "right" in kwargs and kwargs["right"] and not doCenter draw = UIState.getDraw() for line in lines: _w,_h = font.size(line) lx = 0 if doCenter: lx = w/2-_w/2 elif doRight: lx = w-pad*2-_w draw.fontRender(font,line,(tx+lx,ty),color) ty += _h return (pos,size)
def drawBox(self,pos,size,color=None,**kwargs): super(BetterTheme,self).drawBox(pos,size,color,**kwargs) active = kwargs.pop("active",False) if not color: if active: color = self._get("boxActiveColor") else: color = self._get("boxColor") edgeColor = self._get("boxEdgeColor") draw = UIState.getDraw() # drawShadow(pos,size) draw.fillRect(pos,size,color) x,y = pos w,h = size width = self._get("boxEdgeWidth",1) draw.drawLines([pos,(x+w-width,y),(x+w-width,y+h-width),(x,y+h-width)],edgeColor,True,width) return (pos,size)
def CheckBox(value,text,pos=None,size=None,**kwargs): theme = UIState.getTheme() xsize = 16 boxsize = (xsize,xsize) if not size and text: pos,textsize = theme.drawText(text,pos,size,simulate=True,**kwargs) boxsize = (textsize[1]+4,textsize[1]+4) size = (textsize[0]+boxsize[0]+4,textsize[1]) else: size = boxsize pos,size = UIState.doLayout(pos,size,**kwargs) disabled = "disabled" in kwargs and kwargs['disabled'] if mouseIsInside(pos,boxsize): if not disabled: UIState.setHot(pos,boxsize) if mousePressed(): if not UIState.hasActive(): UIState.setActive(pos,boxsize) UIState.setKeyboardActive() else: if UIState.isActive(pos,boxsize): UIState.setActive() if value: value = False else: value = True theme.drawCheckbox(value,pos,boxsize,active=UIState.isActive(pos,boxsize)) if text: theme.drawText(text,(pos[0]+4+boxsize[0],pos[1]),textsize,checkbox=True,**kwargs) UIState.updateLayout(pos,size,**kwargs) return value
def ScrollBar(value,minvalue=0,maxvalue=255,pos=None,size=None,**kwargs): # if type(value) != types.ListType: # raise ParameterError("value needs to be list") theme = UIState.getTheme() disabled = "disabled" in kwargs and kwargs['disabled'] pos,size = theme.drawBox(pos,size,simulate=True,**kwargs) pos,size = UIState.doLayout(pos,size,**kwargs) isint = type(value) == types.IntType mx,my = UIState.getMousePos() x,y = pos w,h = size sliderh = h - 4 bh = 10 bw = w-4 v = float(value - minvalue) / float(maxvalue) bx = x+2 by = v * (sliderh-bh) drawFunc = theme.drawButton if mouseIsInside(pos,size): if not disabled: UIState.setHot(pos,size) drawFunc = theme.drawActiveButton if mousePressed(): if not UIState.hasActive(): UIState.setActive(pos,size) UIState.setKeyboardActive() else: if UIState.isActive(pos,size): UIState.setActive() active = UIState.isActive(pos,size) theme.drawBox(pos,size,active=active,**kwargs) if active: my = min(max(y,my),y+h) v = float(my-y)/float(h) drawFunc = theme.drawClickedButton drawFunc((bx,y+by+2),(bw-1,bh),straight=True) UIState.updateLayout(pos,size,**kwargs) retv = minvalue + v * (maxvalue-minvalue) if isint: return int(retv) return retv
def mouseClicked(): return UIState.isClicked(Buttons.Left)
def mousePressed(): return UIState.isPressed(Buttons.Left)
def EditText(text,pos=None,size=None,**kwargs): theme = UIState.getTheme() ret = False disabled = "disabled" in kwargs and kwargs['disabled'] maxlength = kwargs.pop("max_length",255) font = theme.getFont("textFont") if not size: size = (None,font.get_linesize()) # _p,textsize = theme.drawText(text,pos,size,simulate=True,**kwargs) pos,size = UIState.doLayout(pos,size,**kwargs) if mouseIsInside(pos,size): if not disabled: UIState.setHot(pos,size) if mousePressed(): if not UIState.hasKeyboardActive(): UIState.setKeyboardActive(pos,size) UIState.setActive(pos,size) data = { "cursorpos" : len(text), } UIState.setActiveData(data) if UIState.isKeyboardActive(pos,size): data = UIState.getActiveData() # if not UIState.isActive(pos,size): # UIState.setKeyboardActive() # else: breakKeys = [K_RETURN,K_TAB,K_ESCAPE] while UIState.hasKeypresses(): k = UIState.getKeypress() if k.key in breakKeys: # K_RETURN or k.key == K_TAB or k.key == K_ESCAPE: UIState.setKeyboardActive() # UIState.setActive() UIState.setLastKeyboardActive() ret = True break elif k.key == K_BACKSPACE: text = text[:-1] elif k.unicode != '': if len(text) < maxlength: text += k.unicode elif UIState.wasKeyboardActive(pos,size): ret = True UIState.setLastKeyboardActive() if not size: size = (textsize[0]+4,textsize[1]+4) theme.drawBox(pos,size,active=UIState.isKeyboardActive(pos,size),**kwargs) theme.drawText(text,(pos[0]+2,pos[1]+2),size,**kwargs) if UIState.isKeyboardActive(pos,size): if time.clock()*1000 % 500 < 250: x,y = pos if len(text) > 0: w,h = pgui.textUtils.getTextExtents(text,font) else: w = 2 h = size[1]-6 # with SurfaceContext() as screen: draw = UIState.getDraw() draw.drawLine((x+2+w,y+4),(x+2+w,y+h),(255,255,255,255),1) # value[0] = text UIState.updateLayout(pos,size,**kwargs) return text,ret
def CheckedButton(value,text=None,pos=None,size=None,**kwargs): ret = False theme = UIState.getTheme() pos,size = theme.drawButton(pos,size,text,simulate=True,**kwargs) pos,size = UIState.doLayout(pos,size,**kwargs) disabled = "disabled" in kwargs and kwargs['disabled'] disabledFunc = None if disabled: disabledFunc = theme.drawDisabledButton drawFunc = theme.drawButton if mouseIsInside(pos,size): if not disabled: UIState.setHot(pos,size) drawFunc = theme.drawActiveButton if mousePressed(): if not UIState.hasActive(): UIState.setActive(pos,size) UIState.setKeyboardActive() else: if UIState.isActive(pos,size): if value: value = False else: value = True UIState.setActive() else: if UIState.isActive(pos,size): UIState.setActive() if value: drawFunc = theme.drawClickedButton drawFunc = disabledFunc or drawFunc drawFunc(pos,size,text,**kwargs) UIState.updateLayout(pos,size,**kwargs) return value
def mouseIsInside(pos, size, **kwargs): return isInside(UIState.getMousePos(), pos, size, **kwargs)