This repository was archived by the owner on Feb 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.py
More file actions
152 lines (136 loc) · 5.86 KB
/
CommandLine.py
File metadata and controls
152 lines (136 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from direct.gui.OnscreenImage import OnscreenImage
from direct.gui.DirectGui import *
from direct.gui.OnscreenText import OnscreenText
from panda3d.core import *
import sys,os
MAINDIR = Filename.from_os_specific(os.getcwd())
class Console:
def __init__(self):
return None
def create(self,renderBase,CommandDictionary):
base.a2dBottomLeft.set_bin('background', 123) # avoid drawing order conflict
self.CommandDictionary = {**CommandDictionary,**{"usage":self.helper,"help":self.showCommands}} #copy for further use in other methods
self.hidden = False
self.textscale = 0.04
self.Lines = 43
self.background = OnscreenImage(image = str(MAINDIR)+"/files/bg.png",pos = (0.65,0,1), parent = base.a2dBottomLeft)
self.background.setTransparency(TransparencyAttrib.MAlpha)
self.SavedLines = [OnscreenText(text = '', pos = (0.02, 0.1 + x*1.1*self.textscale), scale = self.textscale, align = TextNode.ALeft, fg = (1,1,1,1), parent = base.a2dBottomLeft) for x in range(self.Lines)]
self.loadConsoleEntry()
self.commands = self.CommandDictionary
#self.entry.reparent_to(App)
base.accept('f1',self.toggle,[base])
self.toggle(base) # initialize as hidden
return None
def loadConsoleEntry(self): #-1.76, 0, -0.97
self.entry = DirectEntry(scale=self.textscale,
frameColor=(0,0,0,1),
text_fg = (1,1,1,1),
pos = (0.025, 0, 0.03),
overflow = 1,
command=self.ConvertToFunction,
initialText="",
numLines = 1,
focus=True,
width = 40,
parent = base.a2dBottomLeft)
return None
def toggle(self,base):
if self.hidden:
for i in self.SavedLines:
i.show()
self.entry.show()
self.background.show()
else:
for i in self.SavedLines:
i.hide()
self.entry.hide()
self.background.hide()
self.hidden = not(self.hidden)
return None
def clearText(self):
self.entry.enterText('')
return None
def ConvertToFunction(self,data):
self.entry.destroy()
self.loadConsoleEntry()
self.ConsoleOutput(" ")
self.ConsoleOutput(str(MAINDIR)+"> "+data)
self.ConsoleOutput(" ")
Buffer = [""]
for x in range(len(data)): # I know the way I did this sucks but I didn't want to think a lot
if data[x] == "(":
Buffer.append("(")
if x != len(data) - 1:
Buffer.append("")
elif data[x] == ")":
Buffer.append(")")
if x != len(data) - 1:
Buffer.append("")
elif data[x] == ",":
if x != len(data) - 1:
Buffer.append("")
else:
Buffer[len(Buffer)-1] += data[x]
try:
ChosenCommand = self.commands[Buffer[0]] # check if the command exists
if len(Buffer)-1 and Buffer[1] == "(" and Buffer[len(Buffer)-1] == ")": # check if the command has some arguments
args = Buffer[2:len(Buffer)-1]
for i in range(len(args)):
try:
args[i] = float(args[i])
except:
args[i] = str(args[i])
try:
ChosenCommand(*args)
except:
self.ConsoleOutput("Wrong arguments provided")
elif len(Buffer) - 1 and Buffer[len(Buffer)-1] != ")":
self.ConsoleOutput('Missing parenthesis ")" in "'+ data + '"')
else:
try:
ChosenCommand()
except:
self.ConsoleOutput('This command requires (at least) one argument')
except:
self.CommandError(Buffer[0])
return None
def SError(self,report):
self.ConsoleOutput("Traceback (most recent call last):")
self.ConsoleOutput("Incorrect use of the "+str(report)+" command")
return None
def CommandError(self,report):
self.ConsoleOutput("Traceback (most recent call last):")
self.ConsoleOutput("SyntaxError: command "+str(report)+" is not defined")
def ConsoleOutput(self,output):
#maxsize = self.entry['width']
maxsize = 73
discretized = [output[i:i+maxsize] for i in range(0,len(output),maxsize)]
for i in discretized:
for x in range(self.Lines-1,0,-1):
self.SavedLines[x].text = self.SavedLines[x-1].text
self.SavedLines[0].text = i
return None
def helper(self,index):
i = self.CommandDictionary[index]
self.ConsoleOutput("Help concerning command '"+str(index)+"':")
self.ConsoleOutput(" associated function name is "+str(i.__name__))
self.ConsoleOutput("Documentation provided: ")
doc = self.TextToLine(str(i.__doc__))
self.ConsoleOutput(" "+doc)
self.ConsoleOutput("Known arguments: ")
self.ConsoleOutput(" "+str(i.__code__.co_varnames))
return None
def showCommands(self):
self.ConsoleOutput("List of available commands: ")
for i in self.CommandDictionary:
self.ConsoleOutput("- "+str(i))
self.ConsoleOutput(" ")
self.ConsoleOutput("Use usage(command) for more details on a specific command")
return None
def TextToLine(self,text):
try:
text = text.replace("\n","")
except:
pass
return text