command_prompt: Add cursor and basic cursor functionality
This commit is contained in:
parent
8d483d055b
commit
7ad1a56627
@ -1,5 +1,6 @@
|
||||
from kivy.uix.label import Label
|
||||
from kivy.core.window import Window
|
||||
from kivy.graphics import Color,Rectangle
|
||||
|
||||
class Command_prompt(Label):
|
||||
|
||||
@ -10,21 +11,73 @@ class Command_prompt(Label):
|
||||
# insert mode
|
||||
self.insert=False
|
||||
|
||||
# cursor position
|
||||
self.cursor=0
|
||||
self.cursor_width=9
|
||||
|
||||
# the text, with no color information
|
||||
self.raw_text=""
|
||||
|
||||
# init Label
|
||||
super(Command_prompt,self).__init__(**kwargs)
|
||||
|
||||
self.keyboard = Window.request_keyboard(None,self,"text")
|
||||
self.keyboard.bind(on_textinput=self.on_textinput,on_key_down=self.on_key_down)
|
||||
|
||||
self.draw()
|
||||
|
||||
def draw(self):
|
||||
with self.canvas.before:
|
||||
# background
|
||||
Color(0,0,0)
|
||||
Rectangle(pos=self.pos,size=self.size)
|
||||
# cursor
|
||||
Color(1,1,1)
|
||||
Rectangle(pos=(self.pos[0]+self.cursor*self.cursor_width,self.pos[1]),size=(self.cursor_width,self.height))
|
||||
|
||||
# make text under cursor black
|
||||
if self.cursor<len(self.raw_text):
|
||||
self.text=self.raw_text[:self.cursor]+"[color=000]"+self.raw_text[self.cursor]+"[/color]"+self.raw_text[self.cursor+1:]
|
||||
else:
|
||||
self.text=self.raw_text
|
||||
|
||||
def on_key_down(self, keyboard, keycode, text, modifiers):
|
||||
print(keycode,text,modifiers)
|
||||
if self.insert:
|
||||
if keycode[1]=="enter":
|
||||
self.insert=False
|
||||
self.text=""
|
||||
self.set_text("")
|
||||
elif keycode[1]=="left":
|
||||
self.move_cursor(-1)
|
||||
elif keycode[1]=="right":
|
||||
self.move_cursor(1)
|
||||
|
||||
def on_textinput(self,window,text):
|
||||
print(text)
|
||||
if self.insert:
|
||||
self.text+=text
|
||||
self.append_text_at_cursor(text)
|
||||
elif text==':':
|
||||
self.text+=text
|
||||
self.append_text_at_cursor(text)
|
||||
self.insert=True
|
||||
|
||||
# append to text at position of cursor
|
||||
def append_text_at_cursor(self,text):
|
||||
if self.cursor==len(self.raw_text):
|
||||
self.raw_text+=text
|
||||
else:
|
||||
self.raw_text=self.raw_text[:self.cursor]+text+self.raw_text[self.cursor:]
|
||||
self.cursor+=len(text)
|
||||
self.draw()
|
||||
|
||||
# set text in prompt
|
||||
def set_text(self,text):
|
||||
self.raw_text=text
|
||||
self.cursor=len(text)
|
||||
self.draw()
|
||||
|
||||
# move cursor n steps to right (left if negative)
|
||||
def move_cursor(self,n):
|
||||
if n==0 or (n>0 and self.cursor==len(self.raw_text)) or (n<0 and self.cursor==0):
|
||||
return
|
||||
self.cursor=max(0,min(len(self.raw_text),self.cursor+n))
|
||||
self.draw()
|
||||
|
Loading…
Reference in New Issue
Block a user