Skip to content

Commit 307626d

Browse files
oxmon-2500xantares
authored andcommitted
Menu arrow and new user problem #52
1 parent 1c3ffc3 commit 307626d

File tree

1 file changed

+99
-2
lines changed

1 file changed

+99
-2
lines changed

brainworkshop.py

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,91 @@ def on_key_press(self, k, mod):
18351835
self.callback(self.text.strip())
18361836
return pyglet.event.EVENT_HANDLED
18371837

1838+
class TextInputScreen3():
1839+
titlesize = calc_fontsize(18)
1840+
textsize = calc_fontsize(16)
1841+
instance = None
1842+
1843+
def __init__(self, title='', text='', callback=None, catch=''):
1844+
self.titletext = title
1845+
self.input_text = [] # Using list for easier manipulation
1846+
self.starttext = text
1847+
self.bgcolor = (255 * int(not cfg.BLACK_BACKGROUND), )*3
1848+
self.textcolor = (255 * int(cfg.BLACK_BACKGROUND), )*3 + (255, )
1849+
self.batch = pyglet.graphics.Batch()
1850+
self.title = pyglet.text.Label(title, font_size=10, #self.titlesize,
1851+
weight='normal', color=self.textcolor, batch=self.batch,
1852+
x=int(window.width/2), y=(window.height*8)/10+24,
1853+
anchor_x='center', anchor_y='center')
1854+
window.push_handlers(self.on_key_press, self.on_draw, self.on_text)
1855+
self.callback = callback
1856+
self.cursor_pos = 0
1857+
self.cursor_visible = True
1858+
self.cursor_time = 0
1859+
self.xInp = 250
1860+
self.yInp = (window.height*8)/10-18
1861+
pyglet.clock.schedule_interval(self.update_cursor, 0.5)
1862+
self.update_displ()
1863+
1864+
def on_text(self, text):
1865+
if text.isprintable() and len(text) == 1:
1866+
self.input_text.insert(self.cursor_pos, text)
1867+
self.cursor_pos += 1
1868+
self.update_displ()
1869+
1870+
def on_key_press(self, k, modifiers):
1871+
if k == key.BACKSPACE:
1872+
if self.cursor_pos > 0:
1873+
del self.input_text[self.cursor_pos - 1]
1874+
self.cursor_pos -= 1
1875+
elif k == key.LEFT:
1876+
self.cursor_pos = max(0, self.cursor_pos - 1)
1877+
elif k == key.RIGHT:
1878+
self.cursor_pos = min(len(self.input_text), self.cursor_pos + 1)
1879+
elif k in (key.ESCAPE, key.RETURN, key.ENTER):
1880+
if k !=key.ESCAPE:
1881+
self.callback(''.join(self.input_text))
1882+
self.cursor_pos = 0
1883+
self.input_text = []
1884+
window.pop_handlers()
1885+
self.update_displ()
1886+
return pyglet.event.EVENT_HANDLED
1887+
1888+
def update_cursor(self, dt):
1889+
self.cursor_visible = not self.cursor_visible
1890+
self.update_displ()
1891+
1892+
def on_draw(self):
1893+
#window.clear()
1894+
self.batch.draw()
1895+
return pyglet.event.EVENT_HANDLED
1896+
1897+
def update_displ(self):
1898+
1899+
# Draw input field background
1900+
bg = pyglet.shapes.Rectangle(
1901+
self.xInp, self.yInp, 400, 28,
1902+
color=(200, 200, 200)
1903+
)
1904+
bg.draw()
1905+
1906+
# Create display text with cursor
1907+
cursor = "_" if self.cursor_visible else " "
1908+
display_text = (
1909+
''.join(self.input_text[:self.cursor_pos]) +
1910+
cursor +
1911+
''.join(self.input_text[self.cursor_pos:])
1912+
)
1913+
1914+
# Create and draw label
1915+
label = pyglet.text.Label(
1916+
display_text,
1917+
font_size=12,
1918+
x=self.xInp+4,
1919+
y=self.yInp+14,
1920+
anchor_y='center'
1921+
)
1922+
label.draw()
18381923

18391924
class Cycler:
18401925
def __init__(self, values, default=0):
@@ -1967,7 +2052,16 @@ def update_labels(self):
19672052
if ending:
19682053
self.labels[i].text = '...'
19692054
w, h, cs = window.width, window.height, self.choicesize
1970-
self.marker.vertices = [w//10, int((h*8)/10 - markerpos*(cs*3/2) + cs/2),
2055+
if have_shapes:
2056+
self.marker = pyglet.shapes.Polygon(
2057+
(w//10, int((h*8)/10 - markerpos*(cs*3/2) + cs/2)),
2058+
(w//9, int((h*8)/10 - markerpos*(cs*3/2))),
2059+
(w//10, int((h*8)/10 - markerpos*(cs*3/2) - cs/2)),
2060+
color=(1,1,1), #(255, 55, 0),
2061+
batch=self.batch
2062+
)
2063+
else:
2064+
self.marker.vertices = [w//10, int((h*8)/10 - markerpos*(cs*3/2) + cs/2),
19712065
w//9, int((h*8)/10 - markerpos*(cs*3/2)),
19722066
w//10, int((h*8)/10 - markerpos*(cs*3/2) - cs/2)]
19732067

@@ -2076,7 +2170,10 @@ def choose(self, k, i):
20762170
newuser = self.users[i]
20772171
if newuser == _("New user"):
20782172
# TODO Don't allow the user to create a username that's an empty string
2079-
textInput = TextInputScreen(_("Enter new user name:"), USER, callback=set_user, catch=' ')
2173+
if sys.version_info[0] == 2: # python 2
2174+
textInput = TextInputScreen(_("Enter new user name:"), USER, callback=set_user, catch=' ')
2175+
else:
2176+
textInput = TextInputScreen3(_("Enter new user name:"), USER, callback=set_user, catch=' ')
20802177
else:
20812178
set_user(newuser)
20822179

0 commit comments

Comments
 (0)