Ejemplo n.º 1
0
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