Debian Codegraph @

godot/addons/gut/strutils.gd

raw
1
2 var _utils = load('res://addons/gut/utils.gd').get_instance()
3 # Hash containing all the built in types in Godot. This provides an English
4 # name for the types that corosponds with the type constants defined in the
5 # engine.
6 var types = {}
7
8 func _init_types_dictionary():
9 types[TYPE_NIL] = 'TYPE_NIL'
10 types[TYPE_BOOL] = 'Bool'
11 types[TYPE_INT] = 'Int'
12 types[TYPE_REAL] = 'Float/Real'
13 types[TYPE_STRING] = 'String'
14 types[TYPE_VECTOR2] = 'Vector2'
15 types[TYPE_RECT2] = 'Rect2'
16 types[TYPE_VECTOR3] = 'Vector3'
17 #types[8] = 'Matrix32'
18 types[TYPE_PLANE] = 'Plane'
19 types[TYPE_QUAT] = 'QUAT'
20 types[TYPE_AABB] = 'AABB'
21 #types[12] = 'Matrix3'
22 types[TYPE_TRANSFORM] = 'Transform'
23 types[TYPE_COLOR] = 'Color'
24 #types[15] = 'Image'
25 types[TYPE_NODE_PATH] = 'Node Path'
26 types[TYPE_RID] = 'RID'
27 types[TYPE_OBJECT] = 'TYPE_OBJECT'
28 #types[19] = 'TYPE_INPUT_EVENT'
29 types[TYPE_DICTIONARY] = 'Dictionary'
30 types[TYPE_ARRAY] = 'Array'
31 types[TYPE_RAW_ARRAY] = 'TYPE_RAW_ARRAY'
32 types[TYPE_INT_ARRAY] = 'TYPE_INT_ARRAY'
33 types[TYPE_REAL_ARRAY] = 'TYPE_REAL_ARRAY'
34 types[TYPE_STRING_ARRAY] = 'TYPE_STRING_ARRAY'
35 types[TYPE_VECTOR2_ARRAY] = 'TYPE_VECTOR2_ARRAY'
36 types[TYPE_VECTOR3_ARRAY] = 'TYPE_VECTOR3_ARRAY'
37 types[TYPE_COLOR_ARRAY] = 'TYPE_COLOR_ARRAY'
38 types[TYPE_MAX] = 'TYPE_MAX'
39
40 # Types to not be formatted when using _str
41 var _str_ignore_types = [
42 TYPE_INT, TYPE_REAL, TYPE_STRING,
43 TYPE_NIL, TYPE_BOOL
44 ]
45
46 func _init():
47 _init_types_dictionary()
48
49 # ------------------------------------------------------------------------------
50 # ------------------------------------------------------------------------------
51 func _get_filename(path):
52 return path.split('/')[-1]
53
54 # ------------------------------------------------------------------------------
55 # Gets the filename of an object passed in. This does not return the
56 # full path to the object, just the filename.
57 # ------------------------------------------------------------------------------
58 func _get_obj_filename(thing):
59 var filename = null
60
61 if(thing == null or
62 !is_instance_valid(thing) or
63 str(thing) == '[Object:null]' or
64 typeof(thing) != TYPE_OBJECT or
65 thing.has_method('__gut_instance_from_id')):
66 return
67
68 if(thing.get_script() == null):
69 if(thing is PackedScene):
70 filename = _get_filename(thing.resource_path)
71 else:
72 # If it isn't a packed scene and it doesn't have a script then
73 # we do nothing. This just read better.
74 pass
75 elif(thing.get_script() is NativeScript):
76 # Work with GDNative scripts:
77 # inst2dict fails with "Not a script with an instance" on GDNative script instances
78 filename = _get_filename(thing.get_script().resource_path)
79 elif(!_utils.is_native_class(thing)):
80 var dict = inst2dict(thing)
81 filename = _get_filename(dict['@path'])
82 if(dict['@subpath'] != ''):
83 filename += str('/', dict['@subpath'])
84
85 return filename
86
87 # ------------------------------------------------------------------------------
88 # Better object/thing to string conversion. Includes extra details about
89 # whatever is passed in when it can/should.
90 # ------------------------------------------------------------------------------
91 func type2str(thing):
92 var oc = _utils.OrphanCounter.new()
93 var filename = _get_obj_filename(thing)
94 var str_thing = str(thing)
95
96 if(thing == null):
97 # According to str there is a difference between null and an Object
98 # that is somehow null. To avoid getting '[Object:null]' as output
99 # always set it to str(null) instead of str(thing). A null object
100 # will pass typeof(thing) == TYPE_OBJECT check so this has to be
101 # before that.
102 str_thing = str(null)
103 elif(typeof(thing) == TYPE_REAL):
104 if(!'.' in str_thing):
105 str_thing += '.0'
106 elif(typeof(thing) == TYPE_STRING):
107 str_thing = str('"', thing, '"')
108 elif(typeof(thing) in _str_ignore_types):
109 # do nothing b/c we already have str(thing) in
110 # to_return. I think this just reads a little
111 # better this way.
112 pass
113 elif(typeof(thing) == TYPE_OBJECT):
114 if(_utils.is_native_class(thing)):
115 str_thing = _utils.get_native_class_name(thing)
116 elif(_utils.is_double(thing)):
117 var double_path = _get_filename(thing.__gut_metadata_.path)
118 if(thing.__gut_metadata_.subpath != ''):
119 double_path += str('/', thing.__gut_metadata_.subpath)
120
121 str_thing += '(double of ' + double_path + ')'
122 filename = null
123 elif(types.has(typeof(thing))):
124 if(!str_thing.begins_with('(')):
125 str_thing = '(' + str_thing + ')'
126 str_thing = str(types[typeof(thing)], str_thing)
127
128 if(filename != null):
129 str_thing += str('(', filename, ')')
130 return str_thing
131
132 # ------------------------------------------------------------------------------
133 # Returns the string truncated with an '...' in it. Shows the start and last
134 # 10 chars. If the string is smaller than max_size the entire string is
135 # returned. If max_size is -1 then truncation is skipped.
136 # ------------------------------------------------------------------------------
137 func truncate_string(src, max_size):
138 var to_return = src
139 if(src.length() > max_size - 10 and max_size != -1):
140 to_return = str(src.substr(0, max_size - 10), '...', src.substr(src.length() - 10, src.length()))
141 return to_return
142
143
144 func _get_indent_text(times, pad):
145 var to_return = ''
146 for i in range(times):
147 to_return += pad
148
149 return to_return
150
151 func indent_text(text, times, pad):
152 if(times == 0):
153 return text
154
155 var to_return = text
156 var ending_newline = ''
157
158 if(text.ends_with("\n")):
159 ending_newline = "\n"
160 to_return = to_return.left(to_return.length() -1)
161
162 var padding = _get_indent_text(times, pad)
163 to_return = to_return.replace("\n", "\n" + padding)
164 to_return += ending_newline
165
166 return padding + to_return