MCPcopy Create free account
hub / github.com/brain-workshop/brainworkshop / TextInputScreen

Class TextInputScreen

brainworkshop.py:1837–1893  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1835 anchor_x = 'center', anchor_y = 'center')
1836
1837class TextInputScreen:
1838 titlesize = calc_fontsize(18)
1839 textsize = calc_fontsize(16)
1840 instance = None
1841
1842 def __init__(self, title='', text='', callback=None, catch=''):
1843 self.titletext = title
1844 self.text = text
1845 self.starttext = text
1846 self.bgcolor = (255 * int(not cfg.BLACK_BACKGROUND), )*3
1847 self.textcolor = (255 * int(cfg.BLACK_BACKGROUND), )*3 + (255, )
1848 self.batch = pyglet.graphics.Batch()
1849 self.title = pyglet.text.Label(title, font_size=self.titlesize,
1850 bold=True, color=self.textcolor, batch=self.batch,
1851 x=width_center(), y=(window.height*9)/10,
1852 anchor_x='center', anchor_y='center')
1853 self.document = pyglet.text.document.UnformattedDocument()
1854 self.document.set_style(0, len(self.document.text), {'color': self.textcolor})
1855 self.layout = pyglet.text.layout.IncrementalTextLayout(self.document,
1856 (from_width_center(-20) - len(title) * calc_fontsize(6)), (window.height*10)/11, batch=self.batch, dpi=calc_dpi())
1857 self.layout.x = from_width_center(15) + len(title) * calc_fontsize(6)
1858 if not callback: callback = lambda x: x
1859 self.callback = callback
1860 self.caret = pyglet.text.caret.Caret(self.layout)
1861 window.push_handlers(self.caret)
1862 window.push_handlers(self.on_key_press, self.on_draw)
1863 self.document.text = text
1864 # workaround for a bug: the keypress that spawns TextInputScreen doesn't
1865 # get handled until after the caret handler has been pushed, which seems
1866 # to result in the keypress being interpreted as a text input, so we
1867 # catch that later
1868 self.catch = catch
1869 self.instance = self
1870
1871
1872 def on_draw(self):
1873 # the bugfix hack, which currently does not work
1874 if self.catch and self.document.text == self.catch + self.starttext:
1875 self.document.text = self.starttext
1876 self.catch = ''
1877 self.caret.select_paragraph(600,0)
1878
1879 window.clear()
1880 self.batch.draw()
1881 return pyglet.event.EVENT_HANDLED
1882
1883
1884 def on_key_press(self, k, mod):
1885 if k in (key.ESCAPE, key.RETURN, key.ENTER):
1886 if k is key.ESCAPE:
1887 self.text = self.starttext
1888 else:
1889 self.text = self.document.text
1890 window.pop_handlers()
1891 window.pop_handlers()
1892 self.callback(self.text.strip())
1893 return pyglet.event.EVENT_HANDLED
1894

Callers 1

chooseMethod · 0.85

Calls 1

calc_fontsizeFunction · 0.85

Tested by

no test coverage detected