def __init__(self, batch, groups, on_click, on_enter, on_motion, on_search): # create document self.document = FormattedDocument(' ') self.document.set_style( 0, 1, dict(font_name=FONT_NAME, font_size=FONT_SIZE, color=FONT_COLOR)) # calculate font height and margin font = self.document.get_font(0) self.font_height = font.ascent - font.descent self.margin = self.font_height * TEXT_INPUT_MARGIN # create text input self.input = IncrementalTextLayout(self.document, 100, self.font_height, batch=batch, group=groups[1]) self.input.x = 100 self.input.y = 100 # creating a caret and push it to window handlers self.caret = Caret(self.input, FONT_COLOR[:3], on_click, on_enter, on_motion, on_search) self.clear_text() # create background rectangle self.rect = Rectangle(0, 0, 100, self.font_height + 2 * self.margin, color=TEXT_INPUT_COLOR, batch=batch, group=groups[0])
def __init__(self, text, color, x, y, width, pad=3): win_width, win_height = get_size() self.batch = Batch() self._doc = pyglet.text.document.UnformattedDocument(text) self._doc.set_style(0, len(self._doc.text), dict(color=(255, 255, 255, 255))) font = self._doc.get_font() self.text_height = font.ascent - font.descent self.pad = pad self._outline = BorderedRectangle(x - self.pad, win_height - y - self.pad, width + self.pad, self.text_height + self.pad, color=color[:3], border_color=(100, 100, 100)) self._outline.opacity = color[-1] self._layout = IncrementalTextLayout(width, self.text_height, multiline=False, batch=self.batch) self._caret = Caret(self._layout, color=(255, 255, 255)) self._caret.visible = False self._layout.x = x self._layout.y = win_height - y self._focus = False self._press = False super().__init__(x, win_height - y, width, height)
def __init__(self): win_width, win_height = get_size() self.batch = Batch() self._doc = pyglet.text.document.UnformattedDocument('') self._doc.set_style(0, len(self._doc.text), dict(color=(255, 255, 255, 255))) font = self._doc.get_font() self.text_height = font.ascent - font.descent self.pad = 2 self._outline = Rectangle(5, 5 + self.pad, get_size()[0] - self.pad - 10, self.text_height + self.pad, color=(0, 0, 0)) self._outline.opacity = 150 self._layout = IncrementalTextLayout(self._doc, get_size()[0] - 14, self.text_height, multiline=False, batch=self.batch) self._caret = Caret(self._layout, color=(255, 255, 255)) self._caret.visible = False self._layout.x = 5 self._layout.y = 5 + self.pad self._focus = False self._press = False self.last_press = [0, 0] super().__init__(5, 5 + self.pad, get_size()[0] - self.pad - 10, self.text_height + self.pad)
def on_activate(self): super().on_activate() if not self.document: self.document = FormattedDocument(text=self.license_text) self.document.set_style( 0, len(self.document.text), { 'font_name': 'Arial', 'font_size': get_bottom_bar_height(self.screen_resolution) // 5, 'bold': False, 'italic': False, 'color': (*WHITE_RGB, self.opacity), 'align': 'center' }) if not self.license_layout: self.license_layout = IncrementalTextLayout( document=self.document, width=self.viewport.x2 - self.viewport.x1, height=self.viewport.y2 - self.viewport.y1, multiline=True, batch=BATCHES['ui_batch'], group=GROUPS['button_text']) self.license_layout.x, self.license_layout.y = self.viewport.x1, self.viewport.y1
def __init__(self, text, color, x, y, width): win_width, win_height = get_size() self.batch = Batch() self._doc = pyglet.text.document.UnformattedDocument(text) self._doc.set_style( 0, len(self._doc.text), dict(color=(255, 255, 255, 255), font_name='minecraftia')) font = self._doc.get_font() height = font.ascent - font.descent pad = 2 self._outline = Rectangle(x - pad, y - pad, width + pad, height + pad, color=color[:3]) self._outline.opacity = color[-1] self._layout = IncrementalTextLayout(self._doc, width, height, multiline=False, batch=self.batch) self._caret = Caret(self._layout, color=(255, 255, 255)) self._caret.visible = False self._layout.x = x self._layout.y = y self._focus = False self._press = False super().__init__(x, y, width, height) self.last_char = ''
def __init__(self, value="", x=0, y=0, width=125, height=30, padding=(0,0), wrap=True, id=None, **kwargs): """ An editable text box. When clicked, it has the focus and can receive keyboard events. With wrap=True, several lines of text will wrap around the width. Optional parameters can include fill, font, fontsize, fontweight. """ txt = Text(value or " ", **{ "fill" : _popdefault(kwargs, "fill", Color(0,0.9)), "font" : _popdefault(kwargs, "font", theme["fontname"]), "fontsize" : _popdefault(kwargs, "fontsize", theme["fontsize"]), "fontweight" : _popdefault(kwargs, "fontweight", theme["fontweight"]), "lineheight" : _popdefault(kwargs, "lineheight", 1), "align" : LEFT }) kwargs["width"] = width kwargs["height"] = height Control.__init__(self, x=x, y=y, id=id, **kwargs) self.reserved = kwargs.get("reserved", [ENTER, TAB]) self._padding = padding self._i = 0 # Index of character on which the mouse is pressed. self._empty = value == "" and True or False self._editor = IncrementalTextLayout(txt._label.document, width, height, multiline=wrap) self._editor.content_valign = wrap and "top" or "center" self._editor.selection_background_color = (170, 200, 230, 255) self._editor.selection_color = txt._label.color self._editor.caret = Caret(self._editor) self._editor.caret.visible = False self._editing = False # When True, cursor is blinking and text can be edited. Editable._pack(self) # On init, call Editable._pack(), not the derived Field._pack().
def __init__(self, width: int, height: int = 0, multiline: bool = False, dpi: object = None, batch: Batch = None, group: Group = None, wrap_lines: bool = True, x: int = 0, y: int = 0, underlined: bool = True, caret_color: tuple = (0, 0, 0), numbers_only: bool = False, font_name=None, font_size=None, font_color=(255, 255, 255, 2555), max_chars=0) -> None: self.document = FormattedDocument() self.layout = IncrementalTextLayout(self.document, width, height, multiline, dpi, batch, group, wrap_lines) self.numbers_only = numbers_only self.style['color'] = font_color self.max_chars = max_chars if font_name: self.style['font_name'] = font_name if font_size: self.style['font_size'] = font_size self.reset_style() if not height: # If the dev didn't specify a height, make the height equal to the height of the font font = pyglet.font.load( font_name or self.document.get_style('font'), font_size or self.document.get_style('font_size')) self.height = font.ascent - font.descent self._hover_cursor = self.get_window().CURSOR_TEXT super().__init__(self.layout, color=caret_color) # TODO: Allow the dev to specify how x and y are treated self.x = x - self.width // 2 self.y = y - self.height // 2 if underlined: self.underline = Rectangle( x=self.x, y=self.y, width=self.width, height=1, )
def __init__(self, *args, **kwargs): super(TestWindow, self).__init__(*args, **kwargs) self.batch = pyglet.graphics.Batch() self.document = pyglet.text.decode_attributed(doctext) for i in range(0, len(doctext), 300): self.document.insert_element(i, TestElement(60, -10, 70)) self.margin = 2 self.layout = IncrementalTextLayout(self.document, self.width - self.margin * 2, self.height - self.margin * 2, multiline=True, batch=self.batch) self.caret = caret.Caret(self.layout) self.push_handlers(self.caret) self.set_mouse_cursor(self.get_system_mouse_cursor('text'))
def __init__(self, text, x, y, width, color=(255, 255, 255, 255), batch=None, group=None): self._doc = pyglet.text.document.UnformattedDocument(text) self._doc.set_style(0, len(self._doc.text), dict(color=(0, 0, 0, 255))) font = self._doc.get_font() height = font.ascent - font.descent self._user_group = group bg_group = OrderedGroup(0, parent=group) fg_group = OrderedGroup(1, parent=group) # Rectangular outline: pad = 2 x1 = x - pad y1 = y - pad x2 = x + width + pad y2 = y + height + pad self._outline = batch.add(4, pyglet.gl.GL_QUADS, bg_group, ('v2i', [x1, y1, x2, y1, x2, y2, x1, y2]), ('c4B', color * 4)) # Text and Caret: self._layout = IncrementalTextLayout(self._doc, width, height, multiline=False, batch=batch, group=fg_group) self._caret = Caret(self._layout) self._caret.visible = False self._layout.x = x self._layout.y = y self._focus = False super().__init__(x, y, width, height)
def __init__(self, text, x, y, width, color=(255, 255, 255, 255), batch=None, group=None): self._doc = pyglet.text.document.UnformattedDocument(text) self._doc.set_style(0, len(self._doc.text), dict(color=(0, 0, 0, 255))) font = self._doc.get_font() height = font.ascent - font.descent self._user_group = group bg_group = Group(order=0, parent=group) fg_group = Group(order=1, parent=group) # Rectangular outline with 2-pixel pad: p = 2 self._outline = pyglet.shapes.Rectangle(x - p, y - p, width + p + p, height + p + p, color[:3], batch, bg_group) self._outline.opacity = color[3] # Text and Caret: self._layout = IncrementalTextLayout(self._doc, width, height, multiline=False, batch=batch, group=fg_group) self._layout.x = x self._layout.y = y self._caret = Caret(self._layout) self._caret.visible = False self._focus = False super().__init__(x, y, width, height)
def __init__(self, text, x, y, width, color=(255, 255, 255, 255), text_color=(0, 0, 0, 255), caret_color=(0, 0, 0), batch=None, group=None): """Create a text entry widget. :Parameters: `text` : str Initial text to display. `x` : int X coordinate of the text entry widget. `y` : int Y coordinate of the text entry widget. `width` : int The width of the text entry widget. `color` : (int, int, int, int) The color of the outline box in RGBA format. `text_color` : (int, int, int, int) The color of the text in RGBA format. `text_color` : (int, int, int) The color of the caret in RGB format. `batch` : `~pyglet.graphics.Batch` Optional batch to add the text entry widget to. `group` : `~pyglet.graphics.Group` Optional parent group of text entry widget. """ self._doc = pyglet.text.document.UnformattedDocument(text) self._doc.set_style(0, len(self._doc.text), dict(color=text_color)) font = self._doc.get_font() height = font.ascent - font.descent self._user_group = group bg_group = Group(order=0, parent=group) fg_group = Group(order=1, parent=group) # Rectangular outline with 2-pixel pad: self._pad = p = 2 self._outline = pyglet.shapes.Rectangle(x - p, y - p, width + p + p, height + p + p, color[:3], batch, bg_group) self._outline.opacity = color[3] # Text and Caret: self._layout = IncrementalTextLayout(self._doc, width, height, multiline=False, batch=batch, group=fg_group) self._layout.x = x self._layout.y = y self._caret = Caret(self._layout, color=caret_color) self._caret.visible = False self._focus = False super().__init__(x, y, width, height)
def __init__(self, *, highscores, ui_batch, cb_start_game=None, cb_add_highscore=None, add_score=None): super().__init__() # Make sure we can handle events self.event_handlers = [self] # Store callbacks, the ui batch and our optional score3 self.cb_start_game = cb_start_game self.cb_add_highscore = cb_add_highscore self.ui_batch = ui_batch self.add_score = add_score # We need to keep track of our children so we can remove them in self.delete() self.children = [] # Show title self.children.append( Label('WELCOME TO', font_name='m5x7', font_size=128, x=WIDTH / 2, y=HEIGHT - 16, anchor_x='center', anchor_y='top', batch=self.ui_batch)) self.children.append( Label('HELL', font_name='m5x7', font_size=512, x=WIDTH / 2 + 50, y=HEIGHT, anchor_x='center', anchor_y='top', batch=self.ui_batch)) # If we need to add a score if add_score is not None: # Make sure that self.continue_label exists so we don't crash in self.tick() self.continue_label = None # Show some text self.children.append( Label('Your score was:', font_name='m5x7', font_size=48, x=WIDTH / 2, y=400, align='center', anchor_x='center', anchor_y='bottom', batch=self.ui_batch)) self.children.append( Label(f'{add_score}', font_name='m5x7', font_size=128, x=WIDTH / 2, y=280, align='center', anchor_x='center', anchor_y='bottom', batch=self.ui_batch)) self.children.append( Label('Enter name for highscore:', font_name='m5x7', font_size=32, x=WIDTH / 2, y=200, align='center', anchor_x='center', anchor_y='bottom', batch=self.ui_batch)) # Prepare a document with styling self.document = UnformattedDocument() self.document.set_style( 0, 1, { 'font_name': 'm5x7', 'font_size': 64, 'color': (255, 255, 255, 255), 'align': 'center' }) # Find the height of the font font = self.document.get_font() height = font.ascent - font.descent # Make a TextLayout that handles dynamically adding text # Make it multiline even though we don't want multiple lines because otherwise align=center doesn't work self.layout = IncrementalTextLayout(self.document, WIDTH / 3, height, multiline=True, batch=self.ui_batch) self.layout.anchor_y = 'top' self.layout.anchor_x = 'center' self.layout.x = WIDTH / 2 self.layout.y = 200 # Add a carat (cursor) self.caret = Caret(self.layout, batch=self.ui_batch, color=(255, 255, 255)) # Type q and then backspace # This ensures that the carat is visible as it only shows up after something has been typed self.caret.on_text('q') self.caret.on_text_motion(key.MOTION_BACKSPACE) # Make sure we can delete layout and carat in self.delete() self.children.append(self.layout) self.children.append(self.caret) else: # If we have some highscores to show if highscores: # Show a title self.children.append( Label('Highscores:', font_name='m5x7', font_size=48, x=WIDTH / 2, y=420, align='center', anchor_x='center', anchor_y='bottom', batch=self.ui_batch)) # ... followed by each highscore for i, highscore in enumerate(highscores): self.children.append( Label(f'{highscore.name} - {highscore.score}', font_name='m5x7', font_size=32, x=WIDTH / 2, y=380 - i * 32, align='center', anchor_x='center', anchor_y='bottom', batch=self.ui_batch)) # Show continue label, and make sure to have it below highscores if any self.continue_label = Label('Press SPACE to start game...', font_name='m5x7', font_size=48, x=WIDTH / 2, y=32 if highscores else HEIGHT / 4, align='center', anchor_x='center', anchor_y='bottom', batch=self.ui_batch) # Timer for blinking self.continue_timer = 0 self.children.append(self.continue_label)