-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTranslator.gd
199 lines (142 loc) · 4.55 KB
/
Translator.gd
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class_name Translator
extends VBoxContainer
##
## This file contains callbacks for the app
## Developed by Johannes Witt and Hugo Locurcio
## Placed into the Public Domain
##
const GITHUB_URL = "https://github.com/HaSa1002/codetranslator/"
const VERSION = "0.8-dev (Last Build 2021-02-16 02:13)"
export var bug_report_popup : NodePath
export var about_popup : NodePath
export var paste_bug : NodePath
func _ready():
get_node(about_popup).dialog_text = get_node(about_popup).dialog_text % VERSION
$Controls/Paste.visible = OS.has_feature("JavaScript")
### Utility touching Scene ###
## Adds a warning to the warnings TextEdit
func warn(line : int, what : String):
var warns : TextEdit = $HSplitContainer/VSplitContainer/Warnings/Warnings
warns.text += "Line %d: %s\n" % [line, what]
($HSplitContainer/Source/Source as TextEdit).set_line_as_safe(line - 1, true);
## Clears output and warnings
func clear():
$HSplitContainer/VSplitContainer/Output/Output.text = ""
$HSplitContainer/VSplitContainer/Warnings/Warnings.text = ""
var source := $HSplitContainer/Source/Source as TextEdit
for i in range(source.get_line_count()):
source.set_line_as_safe(i, false)
## Translates input based on the wanted output determined through the buttons
func generate_output():
clear()
var output := ""
var source = $HSplitContainer/Source/Source.text
if $Controls/CSharp.pressed:
var generator := CsharpGenerator.new();
var err = generator.connect("warning_generated", self, "warn")
assert(err == OK) # Fix the signal above
output = generator.generate_csharp(source)
if $Controls/Docs.pressed:
output = "[codeblocks]\n[gdscript]\n%s\n[/gdscript]\n[csharp]\n%s\n[/csharp]\n[/codeblocks]" % \
[source, output]
if $Controls/EscapeXML.pressed:
output = escape_xml(output)
var tabs := int($Controls/Indention.value)
if tabs > 0:
var tabbed_output := ""
for line in output.split('\n'):
if line.empty() || line.strip_edges().empty():
tabbed_output += "\n"
continue
tabbed_output += "\t".repeat(tabs) + line + "\n"
output = tabbed_output.left(tabbed_output.length() - 1)
$HSplitContainer/VSplitContainer/Output/Output.text = output
pass
### Generators ###
## Escapes source code
## Replaces Tabs, &, <, >
func escape_xml(source: String) -> String:
return source.replace("\t", " ").replace("&", "&") \
.replace("<", "<").replace(">", ">")
### Callbacks ###
func _on_Source_text_changed():
generate_output()
func _on_Regenerate_pressed():
generate_output()
func _on_Control_Button_toggled(_button_pressed: bool) -> void:
generate_output()
func _on_ReportBug_pressed():
generate_output()
var source = $HSplitContainer/Source/Source.text
var output = $HSplitContainer/VSplitContainer/Output/Output.text
var warnings = $HSplitContainer/VSplitContainer/Warnings/Warnings.text
var body = """**Version:** %s
**Description of the problem:**
**Steps to reproduce:**
**Code:**
<!-- Please fill in the code -->
<details>
<summary>Source Code</summary>
```gdscript
%s
```
</details>
<details>
<summary>Output</summary>
```csharp
%s
```
</details>
<details>
<summary>Expected Output</summary>
```csharp
```
</details>
<details>
<summary>Warnings</summary>
```console
%s
```
</details>"""
var replaces = {
"%": "%25",
"\n":"%0A",
"#": "%23",
";": "%3B",
}
for r in replaces:
warnings = warnings.replace(r, replaces[r])
source = source.replace(r, replaces[r])
output = output.replace(r, replaces[r])
body = (body % [VERSION, source, output, warnings]).replace("\n", "%0A")
if OS.shell_open(GITHUB_URL + "issues/new?body=%s" % body) != OK:
print("There was no browser opend, i guess?")
# Create and open popup with instructions
var brp : AcceptDialog = get_node(bug_report_popup)
brp.get_close_button().hide()
var t = brp.dialog_text
brp.dialog_text = brp.dialog_text % [GITHUB_URL + "issues", VERSION]
var btn_copy = brp.add_button("Copy Link", false, "copy")
var btn_ok = brp.add_button("Browser opend", false, "ok")
brp.get_ok().hide()
brp.popup_centered()
# Wait for an answer in the popup
var action = yield(brp, "custom_action")
if action == "copy":
OS.clipboard = GITHUB_URL + "issues/new?body=%s" % body
btn_copy.queue_free()
btn_ok.queue_free()
brp.get_ok().show()
brp.hide()
brp.dialog_text = t
func _brp_custom_action(action, link):
printt(action, link)
if action == "Copy Link":
print("copied")
OS.clipboard = link
func _on_Paste_pressed():
get_node(paste_bug).popup_centered()
func _on_About_pressed():
get_node(about_popup).popup_centered()
func _on_Indention_value_changed(_value):
generate_output()