This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
303 lines (225 loc) · 10 KB
/
App.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QFileDialog, QPushButton, QListWidgetItem
from PyQt5.QtCore import Qt, QMimeData, QObject, pyqtSignal, pyqtSlot, QThread
from PyQt5.QtGui import QDrag, QDragEnterEvent, QDropEvent, QPixmap, QIcon
import pyautogui
from app.ui import Ui_MainWindow
import sys, os
from lib.commander import Commander
from lib.gamer import Gamer
from lib.jsonHandler import JsonHandler, functions
from PIL import Image
import threading
class App(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
self.setupUi(self)
self.setWindowIcon(QIcon('docs\Bilder\VisualAutomationBot.png'))
self.jHandler = None
self.stop_event = threading.Event()
self.com = Commander(self.stop_event)
self.game_thread = None
self.create_folder_name = None
self.create_img = None
self.windowSize = None
self.screenSize = pyautogui.size()
self.comboBoxselected_item = None
self.newSaveTextBox.hide()
def openDir(self):
# Clear bevor new Folder
self.comboBox.clear()
self.functionWidget.clear()
self.pictureWidget.clear()
options = QFileDialog.Options()
current_dir = QFileDialog.getExistingDirectory(self,"QFileDialog.getExistingDirectory()", "", options=options)
if os.path.exists(current_dir + "/config.json") == True:
folder_name = os.path.basename(current_dir)
self.jHandler = JsonHandler(folder_name)
self.openDirButton.setText(folder_name)
# Drag and drop boxes
pic = self.jHandler.getData('pictures').keys()
self.pictureWidget.addItems(pic)
# self.functionWidget.addItems(functions)
for function, description in functions.items():
item = QListWidgetItem(function)
item.setToolTip(f"{function}: {description}") # Set tooltip for each item
self.functionWidget.addItem(item)
actionset_keys = self.jHandler.getData('actionset').keys()
self.functionWidget.addItems(["actionset : " + key for key in actionset_keys])
# Init ComboBox
dropDown = ["playset"]
dropDown.extend(["actionset : " + key for key in actionset_keys])
dropDown.append("Add new Actionset")
self.comboBox.addItems(dropDown)
self.saveButton.setEnabled(True)
def save(self):
arr = []
temp = []
for i in range(self.textlist.count()):
item_text = self.textlist.item(i).text()
if item_text in functions :
if temp:
arr.append(temp)
temp = []
if item_text.split(" : ")[0] == "actionset":
item_text = item_text.split(" : ")[1]
temp.append(item_text)
if temp:
arr.append(temp)
for sublist in arr:
# Check if the sublist has more than one element
if len(sublist) > 1:
# Add double quotes around each element except the first one
for i in range(1, len(sublist)):
sublist[i] = f"\"{sublist[i]}\""
if self.comboBoxselected_item == "playset":
data = {'playset': arr}
self.jHandler.add(data)
self.jHandler.saveData()
self.saveButton.setText("Saved")
return
if self.comboBoxselected_item == "Add new Actionset":
key = self.newSaveTextBox.text()
if key == "":
self.saveButton.setText("Add Savename")
return
current_items = [self.comboBox.itemText(i) for i in range(self.comboBox.count())]
current_items.insert(-1, "actionset : " + key)
self.comboBox.clear()
self.comboBox.addItems(current_items)
self.newSaveTextBox.clear()
else:
aset, key = self.comboBoxselected_item.split(" : ")
result = {key : arr}
print(result)
self.jHandler.add(result, "actionset",)
self.jHandler.saveData()
# Renew the functionWidget
self.functionWidget.clear()
self.functionWidget.addItems(functions)
actionset_keys = self.jHandler.getData('actionset').keys()
self.functionWidget.addItems(["actionset : " + key for key in actionset_keys])
def handleComboBoxSelection(self, index):
self.comboBoxselected_item = self.comboBox.itemText(index)
self.newSaveTextBox.hide()
self.textlist.clear()
if self.comboBoxselected_item == "playset":
item = sum(self.jHandler.getData('playset'), [])
for i in range(len(item)):
if isinstance(item[i], str):
item[i] = item[i].replace('"', '')
self.textlist.addItems(item)
actionset_keys = self.jHandler.getData('actionset').keys()
actionset_text = ["actionset : " + key for key in actionset_keys]
if self.comboBoxselected_item in actionset_text:
key = self.comboBoxselected_item.split(" : ")[1]
item = sum(self.jHandler.getData('actionset', key ), [])
for i in range(len(item)):
if isinstance(item[i], str):
item[i] = item[i].replace('"', '')
self.textlist.addItems(item)
if self.comboBoxselected_item == "Add new Actionset":
self.newSaveTextBox.show()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Delete:
selected_items = self.textlist.selectedItems()
if selected_items:
for item in selected_items:
self.textlist.takeItem(self.textlist.row(item))
self.textlist.clearSelection()
else:
super().keyPressEvent(event)
def mouseMoveEvent(self, event):
item = self.functionWidget.itemAt(event.pos())
if item:
tooltip = f"Item: {item.text()}"
self.functionWidget.setToolTip(tooltip)
else:
self.functionWidget.setToolTip("")
# Listen on Keypress and add
def keyPress(self):
current_text = self.valueTextBox.text()
if current_text != "":
self.textlist.addItems([current_text])
self.valueTextBox.setText("")
# Play
def kill(self):
pass
def loadGame(self):
options = QFileDialog.Options()
current_dir = QFileDialog.getExistingDirectory(self, "QFileDialog.getExistingDirectory()", "", options=options)
if os.path.exists(current_dir + "/config.json") == True:
folder_name = os.path.basename(current_dir)
self.loadGameButton.setText(folder_name)
self.com.addGame(folder_name)
self.start_stopButton.setEnabled(True)
self.killButton.setEnabled(True)
def start_game_loop(self):
if self.game_thread == None:
self.game_thread = threading.Thread(target=self.com.gameLoop)
self.game_thread.start()
else:
self.stop_event.set()
def stop_game_loop(self):
self.stop_event.set()
def start_stop(self):
if self.start_stopButton.isChecked():
self.start_stopButton.setText("STOP")
self.start_stopButton.setStyleSheet("background-color : red")
self.start_game_loop()
else:
self.start_stopButton.setText("Start")
self.start_stopButton.setStyleSheet("background-color : lightgrey")
self.stop_game_loop()
# Create
def selectGame(self):
options = QFileDialog.Options()
current_dir = QFileDialog.getExistingDirectory(self,"QFileDialog.getExistingDirectory()", "", options=options)
if os.path.exists(current_dir) == True:
self.create_folder_name = os.path.basename(current_dir)
self.selectGameButton.setText(self.create_folder_name)
self.jHandler = JsonHandler(self.create_folder_name)
def selectFrameSize(self):
pos = Gamer.getCommandArea()
self.windowSize = (pos["width"], pos["height"])
self.selectFrameSizeButton.setText(f"{self.windowSize[0]} x {self.windowSize[1]}")
self.takePictureButton.setEnabled(True)
def takePicture(self):
self.createSaveButton.setText("Save")
pos = Gamer.getCommandArea()
region = (pos["x"], pos["y"], pos["width"], pos["height"])
self.create_img = pyautogui.screenshot(region=region)
self.takePictureButton.setText("Take New Picture")
# Needs to be save, QPixmap cant handel raw picture
self.create_img.save("temp.png")
self.pictureWidget_2.setPixmap(QPixmap("temp.png").scaled(580,580,True))
os.remove("temp.png")
self.createSaveButton.setEnabled(True)
def createSave(self):
try:
if self.create_img != None :
text = self.fileNameTextBox.text()
if text.strip() != "":
# Save
path = f"{self.create_folder_name}/{text}"
self.create_img.save(f"{path}.png")
self.jHandler.saveNewPicture(text, self.create_img, path, self.windowSize, self.screenSize)
# Cleanup
self.create_img = None
self.pictureWidget_2.setPixmap(QPixmap())
self.fileNameTextBox.setText("")
# Responds
self.createSaveButton.setText("Successfully Saved")
self.createSaveButton.setEnabled(False)
else:
self.createSaveButton.setText("Name!?!!!")
except Exception as e:
self.createSaveButton.setText("Error While Saving")
print(e)
def main():
app = QApplication(sys.argv)
form = App()
form.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()