hi again,
I am setting up an basic “chat” script that has input and output
I have some problems detecting new lines by
in the output()
every time an line has to be wrapped this line:
wrapped_text = textwrap.wrap(self.text[:-1], 30)
will do this but this line:
new_line_count = (self.children[0].text.count("
"))+1
after it doesn’t detect this adding lines.
complete script:
from bge import types, logic, events
import textwrap
def keyHit(key_code):
kevts = logic.keyboard.events
return kevts[key_code] == logic.KX_INPUT_JUST_ACTIVATED
class Chat(types.KX_FontObject):
def __init__(self, own):
#self.scene = logic.getCurrentScene()
self.resolution = 8.0
self.text = ''
self.visible = False
self.chatoutput = []
self.main = self.state_hidden
self.children[0].resolution = 8.0
self.children[0].text = ''
def output(self, text):
offset = 0.01
current_line_count = (self.children[0].text.count('
'))
wrapped_text = textwrap.wrap(self.text[:-1], 30)
self.children[0].text = self.children[0].text +"
" + "
".join([a for a in wrapped_text])
new_line_count = (self.children[0].text.count("
"))+1
add_line_count = (new_line_count - current_line_count) + current_line_count
print(add_line_count, new_line_count, current_line_count)
#self.children[0].localPosition.z = self.children[0].localPosition.z + (add_line_count*offset)
self.children[0].localPosition.z + (add_line_count*offset)
#self.chatoutput = "
".join([a for a in reversed(wrapped_text)])
#for t, l in enumerate(reversed(wrapped_text)):
# self.chatoutput[-(1+t)] = l
# self.chatoutput.pop(0)
# print(self.chatoutput)
# #print([t for a, t in enumerate(wrapped_text)])
#self.children[0].text = "
".join(self.chatoutput)
#"
".join(wrapped_text)
#self.chatoutput.append(wrapped_text)
def inputStream(self):
self.text = self["input"] + "_"
#textwrap.fill(self["input"], 30)
#self.chatoutput[-1]
#self.lines[-1] = self.text
#self.lines[-1] = self["input"].strip()
#self.text = "
".join(self.lines)
def toggleVisible(self):
self.visible = not self.visible
self["log"] = not self["log"]
print(self.visible)
if self.main == self.state_visible:
self.main = self.state_hidden
else:
self.main = self.state_visible
#states
def state_visible(self):
if keyHit(events.ENTERKEY): #accept input
#self.processCommand(self["input"].strip())
self.output("")
self["input"] = ""
self.toggleVisible()
elif keyHit(events.YKEY): #cancel input
self["input"] = self["input"][:-1]
self.toggleVisible()
self.inputStream()
def state_hidden(self):
if keyHit(events.TKEY):
#if keyHit(events.SLASHKEY): consolekey
self["input"] = self["input"][:-1]
self.toggleVisible()
#main loop
def main(self):
text = self.inputStream()
if keyHit(events.ENTERKEY):
self.output()
self["input"] = ""
#getClipboard()
#setClipboard(text)
def main(cont):
own = cont.owner
if not "init" in own:
own["init"] = True
own = Chat(own)
own.main()
hope for help