-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
228 lines (214 loc) · 9.04 KB
/
gui.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
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
import FactorioCalc
import IconExtractor
import io
from zipfile import ZipFile
import re
import json
def setup(modfp):
global micons
global bicons
global modlist
micons = IconExtractor.GetmodIcons(modfp)
bicons = IconExtractor.GetBaseIcons(basefp)
modlistjson = AssMachines = json.load(open(fr"{modfp}\mod-list.json"))
modlist = []
for i in modlistjson["mods"]:
if i["enabled"] == True:
modlist.append(i["name"])
def show_image(tar):
imagefile = findicon(tar)
image = ImageTk.PhotoImage(data=io.BytesIO(imagefile).read())
parent.config(image=image)
parent.image = image
def findicon(tar):
if "space-exploration" in modlist:
if tar == "se-rocket-science-pack":
tar = "orange"
if tar == "se-iron-ingot-to-plate":
tar = "iron-plate"
if tar.startswith("se-"):
tar = tar.replace("se-","")
if "aai-industry" in modlist:
if tar == "motor":
tar = "single-cylinder-engine"
if tar == "electric-motor":
tar = "small-electric-motor"
if tar == "electronic-circuit-stone":
tar = "electronic-circuit"
if tar == "engine-unit":
tar = "multi-cylinder-engine"
print(f"Getting icon: {tar}")
for item in bicons:
if item == (f"{tar}.png"):
with open(f"{bicons[item]}", "rb") as image:
img = Image.open(f"{bicons[item]}")
image = img.crop((0,0,64,64))
image.save("test.png")
with open("test.png", "rb") as image:
f = image.read()
print(f"icon found:{bicons[item]}")
b = bytearray(f)
return b
for mod in micons:
for item in micons[mod]:
if item.filename.endswith(f"/{tar}.png"):
with ZipFile(mod) as myzip:
with myzip.open(item) as myfile:
print(f"icon found:{item}")
image = myfile.read()
return image
print(f"Icon not found: {tar}")
with open(f"placeholder.png", "rb") as image:
img = Image.open(f"placeholder.png")
image = img.crop((0,0,64,64))
image.save("test.png")
with open("test.png", "rb") as image:
f = image.read()
b = bytearray(f)
return b
def TreePop(item):
global ids
ipms = ipm.get()
tree.delete(*tree.get_children())
ids = []
global storedimgs
recipes = FactorioCalc.GetRecipes(filepath)
for name in recipes:
if item in name:
if name.startswith("se-"):
item = name
break
if item == name:
item = name
break
print(recipes[item]['ingredients'])
jsn = FactorioCalc.HumanFriendly(item)
imagefile = findicon(item)
image =ImageTk.PhotoImage(data=io.BytesIO(imagefile).read())
storedimgs.append(image)
ratio = float(ipms)/float(recipes[item]['products'][0]['amount'])
if recipes[item]["category"] == "smelting":
tree.insert('', tk.END,jsn, open=False,image= image, values=(f"{jsn}",ipms,0,FactorioCalc.NumOfSmel(item,filepath,ipms,FactorioCalc.JsonFriendly(furnaceDD.get()))))
else:
tree.insert('', tk.END,jsn, open=False,image= image, values=(f"{jsn}",ipms,FactorioCalc.NumOfAss(item,filepath,ipms,FactorioCalc.JsonFriendly(AssemblersDD.get()))))
items = FactorioCalc.GetSubIngredient(item,recipes)
print(f"ITEMS: {items}")
for ingr in items:
imagefile = findicon(ingr['name'])
image =ImageTk.PhotoImage(data=io.BytesIO(imagefile).read())
storedimgs.append(image)
idn = f"{ingr['name']}0"
ids.append(idn)
print(ingr)
tree.insert(f'{jsn}', tk.END,idn, open=False,image= image, values=(f"{ingr['name']}",ingr['amount']*ratio,FactorioCalc.NumOfAss(ingr['name'],filepath,ingr['amount']*ratio,FactorioCalc.JsonFriendly(AssemblersDD.get()))))
if "space-exploration" in modlist:
if ingr['name'] == "electronic-circuit":
subitems = FactorioCalc.GetSubIngredient('electronic-circuit-stone',recipes)
else:
subitems = FactorioCalc.GetSubIngredient(ingr['name'],recipes)
else:
subitems = FactorioCalc.GetSubIngredient(ingr['name'],recipes)
print(f"SUBITEM: {subitems}")
subingri(subitems,idn,ratio)
def subingri(subitems,parent,ipm):
recipes = FactorioCalc.GetRecipes(filepath)
ipms = ipm
for subingr in subitems:
imagefile = findicon(subingr['name'])
image =ImageTk.PhotoImage(data=io.BytesIO(imagefile).read())
storedimgs.append(image)
high = 0
for idno in ids:
x = re.search(f"{subingr['name']}",idno)
if x:
if int(idno[-1]) > high:
high = int(idno[-1])
idn = f"{subingr['name']}{high+1}"
ids.append(idn)
if subingr['name'].endswith("ore") or subingr['name'] == "stone" or subingr['name'] == "coal":
tree.insert(parent, tk.END,idn, open=False,image= image, values=(f"{subingr['name']}",subingr['amount']*ipm,FactorioCalc.NumOfAss(subingr['name'],filepath,ipms,FactorioCalc.JsonFriendly(AssemblersDD.get()))))
else:
if recipes[subingr['name']]["category"] == "smelting" or recipes[subingr['name']]["category"] == "kiln":
tree.insert(parent, tk.END,idn, open=False,image= image, values=(f"{subingr['name']}",subingr['amount']*ipm,0,FactorioCalc.NumOfSmel(subingr['name'],filepath,ipm,FactorioCalc.JsonFriendly(furnaceDD.get()))))
else:
tree.insert(parent, tk.END,idn, open=False,image= image, values=(f"{subingr['name']}",subingr['amount']*ipm,FactorioCalc.NumOfAss(subingr['name'],filepath,subingr['amount']*ipm,FactorioCalc.JsonFriendly(AssemblersDD.get())),0))
sub = FactorioCalc.GetSubIngredient(subingr['name'],recipes)
if sub != None:
if "space-exploration" in modlist:
if subingr['name'] == "electronic-circuit":
sub = FactorioCalc.GetSubIngredient('electronic-circuit-stone',recipes)
else:
sub = FactorioCalc.GetSubIngredient(subingr['name'],recipes)
else:
sub = FactorioCalc.GetSubIngredient(subingr['name'],recipes)
subingri(sub,idn,ipm)
filepath = r"C:\Users\rudyb\AppData\Roaming\Factorio\script-output\recipe-lister"
modfp= r"C:\Users\rudyb\AppData\Roaming\Factorio\mods"
basefp = r"C:\Program Files (x86)\Steam\steamapps\common\Factorio\data\base\graphics\icons"
setup(modfp)
root = tk.Tk()
root.title("Demo")
root.geometry("1000x900")
frame = tk.Frame(root)
frame.pack()
frame3 = tk.Frame(root)
frame3.pack()
button = tk.Button(frame, text="QUIT", fg="red", command=quit)
button.pack(side=tk.LEFT)
#dropdown menu
recipes = FactorioCalc.GetRecipes(filepath)
temp = []
storedimgs = []
for item in recipes:
temp.append(FactorioCalc.HumanFriendly(item))
recipes = temp
recipesDD = ttk.Combobox(frame3,values=recipes, state='normal')
Assemblers = FactorioCalc.GetAssMachines(filepath)
temp = []
for item in Assemblers:
temp.append(FactorioCalc.HumanFriendly(item))
Assemblers = temp
AssemblersDD = ttk.Combobox(frame3,values=Assemblers, state='normal')
AssemblersDD.set("assembling machine 1")
furnace = FactorioCalc.GetFurnace(filepath)
temp = []
for item in furnace:
temp.append(FactorioCalc.HumanFriendly(item))
furnace = temp
furnaceDD = ttk.Combobox(frame3, values=furnace, state='normal')
furnaceDD.set("stone furnace")
recipesDD.pack(side=tk.LEFT)
AssemblersDD.pack(side=tk.LEFT)
furnaceDD.pack(side=tk.LEFT)
#Buttons
image = tk.Button(frame, text="image", command=lambda: show_image(FactorioCalc.JsonFriendly(recipesDD.get())))
image.pack(side=tk.LEFT)
calculater = tk.Button(frame, text="calculate", command=lambda: show_image(FactorioCalc.JsonFriendly(recipesDD.get())))
calculater.pack(side=tk.LEFT)
treeB = tk.Button(frame,text="TreeB", command= lambda:TreePop(FactorioCalc.JsonFriendly(recipesDD.get())))
treeB.pack(side=tk.LEFT)
#IPM input
entry_float = tk.DoubleVar()
entry_float.set(60.0)
ipm = ttk.Entry(frame3,textvariable=entry_float)
ipm.pack(side=tk.LEFT)
# label to show the image
frame2 = tk.Frame(root)
frame2.pack()
parent = tk.Label(frame2)
parent.pack()
#treeview
style = ttk.Style()
style.configure('Treeview',rowheight=64)
coloumns = ('#0','#1','#2','#3')
tree = ttk.Treeview(frame2,columns=coloumns, height=900)
tree.heading('#0', text='icon', anchor=tk.W)
tree.heading('#1', text='Item', anchor=tk.W)
tree.heading('#2', text='Ratios', anchor=tk.W)
tree.heading('#3', text='Assemblers', anchor=tk.W)
tree.heading('#4', text='Smelters', anchor=tk.W)
tree.pack()
root.mainloop()