@@ -10,35 +10,13 @@ enum FileType {
10
10
SETGET
11
11
}
12
12
13
- class GenPreFab :
14
- extends Resource
15
- var file_type :FileType
16
- ## The file being saved to
17
- var file_name :String
18
- ## The name of the variable
19
- var var_name :String
20
- ## The type of the variable, eg. "int", "Node3D", etc.
21
- var var_type :String
22
- ## The templates to be formatted and written to a file
23
- var text_arr :PackedStringArray
24
-
25
- var valuedict : Dictionary = {
26
- "variable" : "" ,
27
- "class" : "" ,
28
- "defvar" : "" ,
29
- "var_enum" : ""
30
- }
31
- var arguments :Dictionary = {
32
-
33
- }
34
-
35
13
@onready var text_box :TextEdit = $ values
36
14
37
15
var root :AppRoot
38
16
39
17
var default_var :String
40
18
41
- var variable_array :Array [GenPreFab ] = []
19
+ var variable_array :Array [VariableInfo ] = []
42
20
43
21
func _ready () -> void :
44
22
root = get_tree ().current_scene
@@ -56,87 +34,80 @@ func _on_visibility_changed() -> void:
56
34
pass
57
35
58
36
func generate ():
59
- variable_array .clear ()
60
- if default_var .is_empty ():
61
- default_var = "Variant"
62
- if root .current_class_name .is_empty ():
63
- OS .alert ("You haven't set a class for your variables!" , "No class set" )
64
- return
65
37
parse_values ()
66
- for fabs in variable_array :
38
+
39
+ root .cpp_header .text += root .class_declaration .format ({"class_name" : root .current_class_name , "inherits" : root .class_inherits })
40
+ for fabs :VariableInfo in variable_array :
41
+ var value_dict : Dictionary = {
42
+ "variable" : fabs .var_name ,
43
+ "class" : root .current_class_name ,
44
+ "inherits" : root .class_inherits ,
45
+ "defvar" : fabs .var_type ,
46
+ "var_enum" : fabs .type_enum_string ,
47
+ "init_value" : fabs .init_value
48
+ }
49
+
50
+ var templates :PackedStringArray
51
+
67
52
# Bindings
68
- fabs .file_type = FileType .BINDING
69
- fabs .text_arr .append (" ClassDB::bind_method(D_METHOD(\" get_{variable} \" ), &{class} ::get_{variable} );\n " )
70
- fabs .text_arr .append (" ClassDB::bind_method(D_METHOD(\" set_{variable} \" , \" {variable} \" ), &{class} ::set_{variable} );\n\n " )
71
- fabs .text_arr .append (" ADD_PROPERTY(PropertyInfo(Variant::{var_enum} , \" {variable} \" ), \" set_{variable} \" , \" get_{variable} \" );\n\n " )
72
- write (fabs )
53
+ templates .append (" ClassDB::bind_method(D_METHOD(\" get_{variable} \" ), &{class} ::get_{variable} );\n " )
54
+ templates .append (" ClassDB::bind_method(D_METHOD(\" set_{variable} \" , \" {variable} \" ), &{class} ::set_{variable} );\n\n " )
55
+ templates .append (" ADD_PROPERTY(PropertyInfo(Variant::{var_enum} , \" {variable} \" ), \" set_{variable} \" , \" get_{variable} \" );\n\n " )
56
+ for each_template :String in templates :
57
+ root .cpp_binding .text += each_template .format (value_dict )
58
+ templates .clear ()
59
+
73
60
# set_gets
74
- fabs .text_arr .clear ()
75
- fabs .file_type = FileType .SETGET
76
- fabs .text_arr .append ("{defvar} {class} ::get_{variable} (){\n return {variable} ;\n }\n\n " )
77
- fabs .text_arr .append ("void {class} ::set_{variable} ({defvar} new_{variable} ){\n {variable} = new_{variable} ;\n }\n\n " )
78
- write (fabs )
61
+ templates .append ("{defvar} {class} ::get_{variable} (){\n return {variable} ;\n }\n\n " )
62
+ templates .append ("void {class} ::set_{variable} ({defvar} new_{variable} ){\n {variable} = new_{variable} ;\n }\n\n " )
63
+ for each_template :String in templates :
64
+ root .cpp_setgets .text += each_template .format (value_dict )
65
+ templates .clear ()
66
+
79
67
# function class definition
80
- fabs .text_arr .clear ()
81
- fabs .file_type = FileType .HEADER
82
- fabs .text_arr .append (" {defvar} {variable} ;\n " )
83
- fabs .text_arr .append (" {defvar} get_{variable} ();\n " )
84
- fabs .text_arr .append (" void set_{variable} ({defvar} new_{variable} ); \n\n " )
85
- write (fabs )
86
-
87
-
88
-
68
+ if fabs .init_value :
69
+ templates .append (" {defvar} {variable} = {init_value} ;\n " )
70
+ else :
71
+ templates .append (" {defvar} {variable} ;\n " )
72
+ templates .append (" {defvar} get_{variable} ();\n " )
73
+ templates .append (" void set_{variable} ({defvar} new_{variable} ); \n\n " )
74
+
75
+ for each_template :String in templates :
76
+ root .cpp_header .text += each_template .format (value_dict )
77
+ templates .clear ()
78
+ root .cpp_header .text += "}\n\n "
89
79
90
80
func parse_values ():
81
+ variable_array .clear ()
82
+
91
83
var unspaced_values :String = text_box .text .replacen (" " , "" )
92
84
93
- # cheap way to detect if any functions are being bound
94
- if unspaced_values .contains ("(" ):
95
- pass
96
- else : # variables only, use old parser
97
- var var_array :PackedStringArray = unspaced_values .split ("," , false )
98
-
99
- for variables :String in var_array :
100
- var fab :GenPreFab = GenPreFab .new ()
101
- var fab_2 :VariableInfo = VariableInfo .new ()
102
- var split :PackedStringArray = variables .split (":" , false )
103
- if split .size () > 1 : # Variable is typed, and ":" being present was not a false positive
104
- fab .var_name = split [0 ]
105
- fab_2 .var_name = split [0 ]
106
- fab .var_type = split [1 ]
107
- fab_2 .var_type = split [1 ]
108
- else : # Variable is assumed to be untyped; use default var
109
- fab .var_name = variables
110
- fab_2 .var_name = variables
111
- fab .var_type = default_var
112
- fab_2 .var_type = default_var
113
- variable_array .append (fab )
114
-
115
- func write (prefab :GenPreFab ):
116
- var code_box :CodeEdit
117
- match prefab .file_type :
118
- FileType .HEADER :
119
- code_box = root .cpp_header
120
- FileType .BINDING :
121
- code_box = root .cpp_binding
122
- FileType .SETGET :
123
- code_box = root .cpp_setgets
124
- _ :
125
- push_error ("Huh." )
85
+ if default_var .is_empty ():
86
+ default_var = "Variant"
87
+ if root .current_class_name .is_empty ():
88
+ OS .alert ("You haven't set a class for your variables!" , "No class set" )
89
+ return
126
90
127
- var valuedict : Dictionary = {
128
- "variable" : "" ,
129
- "class" : root .current_class_name ,
130
- "defvar" : default_var ,
131
- "var_enum" : root .format_variant_enum (default_var )
132
- }
133
- for entries in variable_array :
134
- if entries .var_type != default_var :
135
- # Also done, just in case, because Dictionaries are weird
136
- valuedict ["defvar" ] = entries .var_type
137
- valuedict ["var_enum" ] = root .format_variant_enum (entries .var_type )
91
+ var var_array :PackedStringArray = unspaced_values .split ("," , false )
92
+
93
+ for variables :String in var_array :
94
+ var var_info :VariableInfo = VariableInfo .new ()
95
+
96
+ # static type detection
97
+ var split :PackedStringArray = variables .split (":" , false )
98
+ if split .size () > 1 : # Variable is typed
99
+ var_info .var_name = split [0 ]
100
+
101
+ if split [1 ].contains ("=" ): # init value detected
102
+ var subsplit :PackedStringArray = split [1 ].split ("=" )
103
+ var_info .var_type = subsplit [0 ]
104
+ var_info .init_value = subsplit [1 ]
105
+ else :
106
+ var_info .var_type = split [1 ]
107
+ else : # Variable is assumed to be untyped; use default var
108
+ var_info .var_name = variables
109
+ var_info .var_type = default_var
110
+
111
+ var_info .type_enum_string = root .format_variant_enum (var_info .var_type )
138
112
139
- valuedict ["class" ] = root .current_class_name
140
- valuedict ["variable" ] = entries .var_name
141
- for string_templates in prefab .text_arr :
142
- code_box .text += string_templates .format (valuedict )
113
+ variable_array .append (var_info )
0 commit comments