Skip to content

Commit dd67467

Browse files
authored
Merge pull request #15 from ChocoScaff/context_menu
Context menu
2 parents 4c46079 + 2c943ef commit dd67467

File tree

6 files changed

+130
-21
lines changed

6 files changed

+130
-21
lines changed

caption.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ def __init__(self, sourceSDK) -> None:
1616

1717
self.sdk = sourceSDK
1818

19-
def build_caption(self):
19+
def build_caption(self, file=None):
2020
"""
2121
Compile caption
2222
"""
23-
24-
filenameTXT = filedialog.askopenfile(title="Select .txt file", filetypes=[("TXT files", "closecaption*.txt")], initialdir=self.sdk.selected_folder + "/resource")
23+
if file == None:
24+
filenameTXT = filedialog.askopenfile(title="Select .txt file", filetypes=[("TXT files", "closecaption*.txt")], initialdir=self.sdk.selected_folder + "/resource")
25+
file = filenameTXT.name
26+
2527
captioncompiler = (self.sdk.bin_folder + "/captioncompiler.exe")
26-
command = ('"' + captioncompiler + '"' + " -game " + '"' + self.sdk.selected_folder + '"' + " " + '"' + filenameTXT.name + '"')
28+
command = ('"' + captioncompiler + '"' + " -game " + '"' + self.sdk.selected_folder + '"' + " " + '"' + file + '"')
2729
print(command)
2830
result = subprocess.run(command, shell=True, capture_output=True, text=True)
2931
print(result)

file.py

+59-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from fileListApp import FileListApp # Assuming this is your custom module
55
from open import Open # Assuming this is your custom module
66
from PIL import Image, ImageTk
7+
from model import Model
8+
from texture import Texture
9+
from map import Map
710

811
class File:
912
"""
@@ -112,6 +115,7 @@ def display_files(self):
112115

113116
# Bind double-click event to open the selected file
114117
self.tree.bind("<Double-Button-1>", self.open_file)
118+
self.tree.bind("<Button-3>", self.show_context_menu)
115119

116120
self.fileList = FileListApp(self.sdk, self.main_root)
117121

@@ -189,14 +193,12 @@ def load_thumbnail(self, file_path, parent=""):
189193
".smd": "txt.png",
190194
".cfg": "txt.png",
191195
".sln": "Visual_Studio.png",
192-
".vpk": "fileexplorer.png",
193196
".wav": "audio.png",
194197
".mp3": "audio.png",
195198
".bik": "video.png",
196199
".bat": "terminal.png"
197200
}
198201

199-
#ext = os.path.splitext(file_path)[1]
200202
file_name, file_extension = os.path.splitext(file_path)
201203

202204
if file_extension in file_icons:
@@ -217,3 +219,58 @@ def load_thumbnail(self, file_path, parent=""):
217219
except Exception as e:
218220
print("Error loading thumbnail:", e)
219221
return None
222+
223+
def show_context_menu(self, event):
224+
"""
225+
Show the context menu on right-click.
226+
"""
227+
selected_item = self.tree.identify_row(event.y)
228+
229+
if selected_item:
230+
self.tree.selection_set(selected_item)
231+
232+
filename = self.tree.item(selected_item, 'text')
233+
print(filename)
234+
235+
parent_item = self.tree.parent(selected_item)
236+
237+
file_path_parts = [filename]
238+
239+
while parent_item:
240+
item_text = self.tree.item(parent_item, "text")
241+
file_path_parts.append(item_text)
242+
parent_item = self.tree.parent(parent_item)
243+
244+
file_path_parts.reverse()
245+
file_path = os.path.join(self.sdk.parent_folder, *file_path_parts)
246+
247+
print(file_path)
248+
249+
self.context_menu = tk.Menu(self.tree, tearoff=0)
250+
251+
file_extension = os.path.splitext(filename)[1]
252+
253+
if file_extension == ".qc":
254+
model = Model(self.sdk)
255+
self.context_menu.add_command(label="Compile Model", command=lambda: model.build_model(file_path))
256+
elif file_extension == ".tga":
257+
texture = Texture(self.sdk)
258+
self.context_menu.add_command(label="Compile Texture", command=lambda: texture.build_texture(file_path))
259+
elif file_extension == ".vmf":
260+
map = Map(self.sdk)
261+
self.context_menu.add_command(label="Compile Map", command=lambda: map.build_map(file_path))
262+
263+
self.context_menu.add_command(label="Delete", command=lambda: self.delete_file(file_path, selected_item))
264+
265+
self.context_menu.post(event.x_root, event.y_root)
266+
267+
def delete_file(self, file_path, tree_item):
268+
"""
269+
Delete the specified file and update the Treeview.
270+
"""
271+
try:
272+
os.remove(file_path)
273+
self.tree.delete(tree_item)
274+
print(f"Deleted: {file_path}")
275+
except Exception as e:
276+
print(f"Error deleting file: {e}")

fileListApp.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from PIL import Image, ImageTk
44
import os
55
from open import Open
6+
from model import Model
7+
from texture import Texture
8+
from map import Map
69

710
class FileListApp:
811
def __init__(self, sourceSDK, root):
@@ -94,13 +97,19 @@ def load_files(self, folder):
9497

9598
if os.path.isdir(file_path):
9699
bind_func = lambda e, path=file_path: self.load_files(path)
100+
bind_right = lambda e, path=file_path: self.show_context_menu(e, path)
97101
else:
98102
bind_func = lambda e, path=file_path: self.open_file(path)
103+
bind_right = lambda e, path=file_path: self.show_context_menu(e, path)
99104

100105
frame.bind("<Double-Button-1>", bind_func)
101106
label.bind("<Double-Button-1>", bind_func)
107+
frame.bind("<Button-3>", bind_right)
108+
label.bind("<Button-3>", bind_right)
109+
102110
if thumbnail:
103111
thumbnail_label.bind("<Double-Button-1>", bind_func)
112+
thumbnail_label.bind("<Button-3>", bind_right)
104113

105114
col += 1
106115
if col >= columns:
@@ -188,4 +197,39 @@ def resize(self, event):
188197
if new_width != self.previous_width:
189198
self.previous_width = new_width
190199
# Add the code you want to execute when the width changes
191-
self.load_files(self.current_folder)
200+
self.load_files(self.current_folder)
201+
202+
def show_context_menu(self, event, file_path):
203+
"""
204+
Show the context menu on right-click.
205+
"""
206+
file_name, file_extension = os.path.splitext(file_path)
207+
208+
self.context_menu = tk.Menu(self.root, tearoff=0)
209+
210+
if file_extension == ".qc":
211+
model = Model(self.sdk)
212+
self.context_menu.add_command(label="Compile Model", command=lambda: model.build_model(file_path))
213+
elif file_extension == ".tga":
214+
texture = Texture(self.sdk)
215+
self.context_menu.add_command(label="Compile Texture", command=lambda: texture.build_texture(file_path))
216+
elif file_extension == ".vmf":
217+
map = Map(self.sdk)
218+
self.context_menu.add_command(label="Compile Map", command=lambda: map.build_map(file_path))
219+
220+
self.context_menu.add_command(label="Delete", command=lambda: self.delete_file(file_path))
221+
222+
223+
self.context_menu.post(event.x_root, event.y_root)
224+
225+
def delete_file(self, file_path):
226+
"""
227+
Delete the specified file and update the Treeview.
228+
"""
229+
try:
230+
os.remove(file_path)
231+
print(f"Deleted: {file_path}")
232+
except Exception as e:
233+
print(f"Error deleting file: {e}")
234+
235+
self.load_files(self.current_folder)

map.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ def __init__(self, sourceSDK) -> None:
1919
"""
2020
self.sdk = sourceSDK
2121

22-
def build_map(self):
22+
def build_map(self, file=None):
2323
"""
2424
"""
2525

2626
mapsrc_directory = os.path.join(self.sdk.selected_folder, "mapsrc")
2727
map_directory = os.path.join(self.sdk.selected_folder, "maps")
28+
29+
if file == None:
30+
filenameVMF = filedialog.askopenfile(title="Select .vmf file", filetypes=[("VMF files", "*.vmf")], initialdir=mapsrc_directory)
31+
file = filenameVMF.name
2832

29-
filenameVMF = filedialog.askopenfile(title="Select .vmf file", filetypes=[("VMF files", "*.vmf")], initialdir=mapsrc_directory)
30-
31-
print("file =", filenameVMF.name)
33+
print("file =", file)
3234
# Execute vbsp.exe
3335

34-
fileBSP = filenameVMF.name
36+
fileBSP = file
3537
#file_directory = os.path.dirname(fileBSP)
3638
fileBSP = os.path.splitext(os.path.basename(fileBSP))[0]
3739

@@ -42,7 +44,7 @@ def build_map(self):
4244
print(self.sdk.bin_folder)
4345

4446
vbsp = (self.sdk.bin_folder + "/vbsp.exe")
45-
command = ('"' + vbsp + '"' + " -game " + '"' + self.sdk.selected_folder + '"' + " " + '"' + filenameVMF.name + '"')
47+
command = ('"' + vbsp + '"' + " -game " + '"' + self.sdk.selected_folder + '"' + " " + '"' + file + '"')
4648
print(command)
4749
#Execute the command in cmd
4850
result = subprocess.run(command, shell=True, capture_output=True, text=True)

model.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ def open_hlmv(self):
2424

2525
subprocess.Popen([self.sdk.bin_folder + "/hlmv.exe"])
2626

27-
def build_model(self):
27+
def build_model(self, file=None):
2828
"""
2929
"""
30-
31-
filenameQC = filedialog.askopenfile(title="Select .qc file", filetypes=[("QC files", "*.qc")], initialdir=self.sdk.selected_folder + "/modelsrc")
30+
if file == None:
31+
filenameQC = filedialog.askopenfile(title="Select .qc file", filetypes=[("QC files", "*.qc")], initialdir=self.sdk.selected_folder + "/modelsrc")
32+
file= filenameQC.name
33+
34+
3235
mdl = (self.sdk.bin_folder + "/studiomdl.exe")
33-
command = ('"' + mdl + '"' + " -game " + '"' + self.sdk.selected_folder + '"' + " " + '"' + filenameQC.name + '"')
36+
command = ('"' + mdl + '"' + " -game " + '"' + self.sdk.selected_folder + '"' + " " + '"' + file + '"')
3437
print(command)
3538
result = subprocess.run(command, shell=True, capture_output=True, text=True)
3639
print(result)

texture.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ def build_all_texture(self):
5252
result = subprocess.run(command, shell=True, capture_output=True, text=True)
5353
print(result)
5454

55-
def build_texture(self):
55+
def build_texture(self,file=None):
5656
"""
5757
"""
58-
59-
filenameTGA = filedialog.askopenfile(title="Select .tga file", filetypes=[("TGA files", "*.tga")], initialdir=self.sdk.selected_folder + "/materialsrc")
58+
if file == None:
59+
filenameTGA = filedialog.askopenfile(title="Select .tga file", filetypes=[("TGA files", "*.tga")], initialdir=self.sdk.selected_folder + "/materialsrc")
60+
file = filenameTGA.name
6061
vtex = (self.sdk.bin_folder + "/vtex.exe")
61-
command = ('"' + vtex + '"' + " -game " + '"' + self.sdk.selected_folder + '"' + " -nopause " + '"' + filenameTGA.name + '"' )
62+
command = ('"' + vtex + '"' + " -game " + '"' + self.sdk.selected_folder + '"' + " -nopause " + '"' + file + '"' )
6263
print(command)
6364
result = subprocess.run(command, shell=True, capture_output=True, text=True)
6465
print(result)

0 commit comments

Comments
 (0)