<commit_before>﻿#-------------------------------------------------------------------------------
# Name:        widgets
# Purpose:	   pygame simple visual controls library
#
# Author:      VoRoN
#
# Created:     20.09.2017
# Copyright:   (c) VoRoN 2017
# Licence:     MIT
#-------------------------------------------------------------------------------
import pygame
from pygame_vkeyboard import *
from os.path import join, dirname

class Screen:
	def __init__(self, config, font_cache, image_cache):
		self.name = config['name']
		self.fontCache = font_cache
		self.imageCache = image_cache
		self.background = tuple(config['background'])
		self.controls = []
		for label in (config['labels'] if config.has_key('labels') else []):
			font = self.fontCache.getFont(label['font'], label['font_size'])
			if font == None:
				font = pygame.font.Font(label['font'], label['font_size'])
				self.fontCache.addFont(label['font'], label['font_size'], font)
			h = False
			v = False
			if label.has_key('hvaligment'):
				h = label['hvaligment'][0]
				v = label['hvaligment'][1]
			self.controls.append(Label(font.render(label['text'], True,
						tuple(label['color'])), tuple(label['position']), h, v))
		
		for picture in (config['pictures'] if config.has_key('pictures') else []):
			image = None
			size = [0, 0]
			name = ''
			if picture.has_key('file'):
				image = self.imageCache.getImage(picture['file'])
			if picture.has_key('size'):
				size = picture['size']
			if picture.has_key('name'):
				name = picture['name']
			self.controls.append(Picture(image, picture['position'], size, name))
			
		for button in (config['buttons'] if config.has_key('buttons') else []):
			image = self.imageCache.getImage(button['image'])
			self.controls.append(Button(image, button['position'], button['event']))
			
		for textedit in (config['textedits'] if config.has_key('textedits') else []):
			self.controls = [TextEdit(textedit['position'], textedit['size'], self.fontCache)] \
							+ self.controls

	def render(self, screen):
		screen.fill(self.background)
		for control in self.controls:
		    control.render(screen)
	
	def onevent(self, event):
		for control in self.controls:
			if isinstance(control, Button):
				event = control.onevent(event)
			if isinstance(control, TextEdit):
				event = control.onevent(event)
			if event == None:
				break

	def getControlByName(self, name):
		for control in self.controls:
			control_name = getattr(control, 'name', '')
			if name == control_name:
				return control
		return None

	def getControlsByType(self, controlType):
		controls = []
		for control in self.controls:
			if isinstance(control, controlType):
				controls.append(control)
		return controls

class Label:
	def __init__(self, text, position, hcenter = False, vcenter = False):
		self.text = text
		self.position = position
		self.hcenter = hcenter
		self.vcenter = vcenter

	def render(self, screen):
		position = (screen.get_width() / 2 - self.text.get_width() / 2 if
											 self.hcenter else self.position[0],
					screen.get_height() / 2 - self.text.get_height() / 2 if
											self.vcenter else self.position[1])
		screen.blit(self.text, position)

class MyKeyboardRenderer(VKeyboardRenderer):
	
    def draw_background(self, surface, position, size):
        pygame.draw.rect(surface, (255, 255, 255, 255),
			(position[0] + 25, position[1]) + (size[0] - 50, size[1]))

MyKeyboardRenderer.DEFAULT = MyKeyboardRenderer(
    pygame.font.Font('fonts\\DejaVuSans.ttf', 25),
    (255, 255, 255),
    ((255, 255, 255), (0, 0, 0)),
    ((0, 0, 0), (255, 255, 255)),
    ((180, 180, 180), (0, 0, 0)),
)

class TextEdit:
	def __init__(self, position, size, fontCache, font = "fonts/arial.ttf",
							font_size = 20, color = (0, 0, 0),
							hcenter = False, vcenter = False):
		self.position = position
		self.size = size
		self.rect = pygame.Rect(self.position[0], self.position[1], self.size[0],
					self.size[1])
		self.showKeyboard = False
		self.keyboard = None
		self.label = None
		self.hcenter = hcenter
		self.vcenter = vcenter
		self.font = font
		self.color = color
		self.font_size = font_size
		self.fontCache = fontCache
		
	def textConsumer(self, text):
		print text
		font = self.fontCache.getFont(self.font, self.font_size)<commit_msg>* fixed path to font<commit_after>﻿#-------------------------------------------------------------------------------
# Name:        widgets
# Purpose:	   pygame simple visual controls library
#
# Author:      VoRoN
#
# Created:     20.09.2017
# Copyright:   (c) VoRoN 2017
# Licence:     MIT
#-------------------------------------------------------------------------------
import pygame
from pygame_vkeyboard import *
from os.path import join, dirname

class Screen:
	def __init__(self, config, font_cache, image_cache):
		self.name = config['name']
		self.fontCache = font_cache
		self.imageCache = image_cache
		self.background = tuple(config['background'])
		self.controls = []
		for label in (config['labels'] if config.has_key('labels') else []):
			font = self.fontCache.getFont(label['font'], label['font_size'])
			if font == None:
				font = pygame.font.Font(label['font'], label['font_size'])
				self.fontCache.addFont(label['font'], label['font_size'], font)
			h = False
			v = False
			if label.has_key('hvaligment'):
				h = label['hvaligment'][0]
				v = label['hvaligment'][1]
			self.controls.append(Label(font.render(label['text'], True,
						tuple(label['color'])), tuple(label['position']), h, v))
		
		for picture in (config['pictures'] if config.has_key('pictures') else []):
			image = None
			size = [0, 0]
			name = ''
			if picture.has_key('file'):
				image = self.imageCache.getImage(picture['file'])
			if picture.has_key('size'):
				size = picture['size']
			if picture.has_key('name'):
				name = picture['name']
			self.controls.append(Picture(image, picture['position'], size, name))
			
		for button in (config['buttons'] if config.has_key('buttons') else []):
			image = self.imageCache.getImage(button['image'])
			self.controls.append(Button(image, button['position'], button['event']))
			
		for textedit in (config['textedits'] if config.has_key('textedits') else []):
			self.controls = [TextEdit(textedit['position'], textedit['size'], self.fontCache)] \
							+ self.controls

	def render(self, screen):
		screen.fill(self.background)
		for control in self.controls:
		    control.render(screen)
	
	def onevent(self, event):
		for control in self.controls:
			if isinstance(control, Button):
				event = control.onevent(event)
			if isinstance(control, TextEdit):
				event = control.onevent(event)
			if event == None:
				break

	def getControlByName(self, name):
		for control in self.controls:
			control_name = getattr(control, 'name', '')
			if name == control_name:
				return control
		return None

	def getControlsByType(self, controlType):
		controls = []
		for control in self.controls:
			if isinstance(control, controlType):
				controls.append(control)
		return controls

class Label:
	def __init__(self, text, position, hcenter = False, vcenter = False):
		self.text = text
		self.position = position
		self.hcenter = hcenter
		self.vcenter = vcenter

	def render(self, screen):
		position = (screen.get_width() / 2 - self.text.get_width() / 2 if
											 self.hcenter else self.position[0],
					screen.get_height() / 2 - self.text.get_height() / 2 if
											self.vcenter else self.position[1])
		screen.blit(self.text, position)

class MyKeyboardRenderer(VKeyboardRenderer):
	
    def draw_background(self, surface, position, size):
        pygame.draw.rect(surface, (255, 255, 255, 255),
			(position[0] + 25, position[1]) + (size[0] - 50, size[1]))

MyKeyboardRenderer.DEFAULT = MyKeyboardRenderer(
    pygame.font.Font('fonts/DejaVuSans.ttf', 25),
    (255, 255, 255),
    ((255, 255, 255), (0, 0, 0)),
    ((0, 0, 0), (255, 255, 255)),
    ((180, 180, 180), (0, 0, 0)),
)

class TextEdit:
	def __init__(self, position, size, fontCache, font = "fonts/arial.ttf",
							font_size = 20, color = (0, 0, 0),
							hcenter = False, vcenter = False):
		self.position = position
		self.size = size
		self.rect = pygame.Rect(self.position[0], self.position[1], self.size[0],
					self.size[1])
		self.showKeyboard = False
		self.keyboard = None
		self.label = None
		self.hcenter = hcenter
		self.vcenter = vcenter
		self.font = font
		self.color = color
		self.font_size = font_size
		self.fontCache = fontCache
		
	def textConsumer(self, text):
		print text
		font = self.fontCache.getFont(self.font, self.font_size)
{'fraction_non_alphanumeric': 0.18056528848399905, 'fraction_numerical': 0.022891847699135714, 'mean_word_length': 3.1852394916911044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4897256', 'n_tokens_mistral': 3251, 'n_tokens_neox': 2723, 'n_words': 665}