From 01b4c2458b34f75fa45e25b40a8eb8a5f956f40f Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Mon, 1 Apr 2024 10:35:55 -0400 Subject: add first attempt srb_tk and stackrpms_tk_lib --- srb_tk.py | 534 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 534 insertions(+) create mode 100755 srb_tk.py (limited to 'srb_tk.py') diff --git a/srb_tk.py b/srb_tk.py new file mode 100755 index 0000000..75d4f60 --- /dev/null +++ b/srb_tk.py @@ -0,0 +1,534 @@ +#!/usr/bin/env python3 +# File: srb_tk.py +# Location: https://bgstack15.ddns.net/cgit/srb_lib +# Author: bgstack15 +# SPDX-License-Identifier: GPL-3.0-only +# Startdate: 2024-03-28-5 13:43 +# Title: Graphical frontend for savegame editor for Snoopy vs. the Red Baron +# Project: srb_lib +# Purpose: Savegame editor graphical frontend for +# History: +# Usage: +# ./srb_tk.py +# References: +# logout-manager-tcl, lmlib.py +# https://likegeeks.com/python-gui-examples-tkinter-tutorial/#Add_a_Menu_bar +# icon from https://www.iconarchive.com/show/peanuts-icons-by-ed-mundy/Snoopy-icon.html +# https://www.tcl.tk/man/tcl8.6/TkCmd/bind.htm from https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm +# https://www.reddit.com/r/learnpython/comments/mavinn/tkinter_remove_optionmenu_icon/ +# https://stackoverflow.com/questions/69806038/navigate-to-tkinter-optionmenu-with-tab-key/69806039#69806039 +# icon courtesy of https://icons8.com/icon/9336/aircraft +# possible way to have a widget store its variable: https://stackoverflow.com/questions/63871376/tkinter-widget-cgetvariable +# Improve: +# Add levelset, level info. +# Enable the checkbox for profile-in-use +# Dependencies: +# dep-devuan: python3-tkinter, python3-pil.imagetk +# rec-devuan: python3-cairosvg +import tkinter as tk +from pathlib import Path +import tkinter.filedialog, srb_lib, os, time +import stackrpms_tk_lib as stk + +ABOUT_TEXT = """ +srb_tk \"Savegame Editor for Snoopy vs. the Red Baron\" +(C) 2024 bgstack15 +SPDX-License-Identifier: GPL-3.0-only +Icon courtesy of https://icons8.com/icon/9336/aircraft +""" + +class App(tk.Frame): + def __init__(self, master): + super().__init__(master) + self.master.title("Savegame editor for SRB") + self.bdata = None + menu = tk.Menu(self.master) + self.master.config(menu=menu) + menu_file = tk.Menu(menu,tearoff=0) + menu_file.add_command(label="Open...", command=self.func_open, underline=0) + menu_file.add_command(label="Reload current file", command=self.func_reload, underline=0) + menu_file.add_command(label="Save", command=self.func_save, underline=0) + menu_file.add_command(label="Save as...", command=self.func_save_as, underline=5) + menu_file.add_separator() + menu_file.add_command(label="Exit", command=self.func_exit, underline=1) + menu.add_cascade(label="File",menu=menu_file, underline=0) + menu_help = tk.Menu(menu,tearoff=0) + menu_help.add_command(label="About", command=self.func_about, underline=0) + menu.add_cascade(label="Help",menu=menu_help,underline=0) + self.grid() # use this geometry manager instead of pack() + # app icon + #imgicon = stk.get_scaled_icon("hexedit",24,"Numix-Circle", "","apps") + #imgicon = stk.get_scaled_icon(os.path.join(os.getenv("HOME"),"Downloads/snoopy.png"),24,"Numix-Circle", "","apps") + imgicon = stk.get_scaled_icon("./srb.png",24,"default", "","apps") + self.master.tk.call("wm","iconphoto",self.master._w,imgicon) + + # app variables + self.silent = tk.BooleanVar() + self.silent.set(True) + + # data variables + self.current_file = tk.StringVar(value = os.path.join(os.getenv("HOME"),"Documents","Snoopy vs. the Red Baron","Profile 1","Profile 1.sav")) + #print(f"beginning: {self.current_file.get()}") + self.checksum = tk.IntVar() + self.selected_profile = tk.IntVar(value=1) + self.prof1_used = tk.BooleanVar() + # Do not use traces for most things though, because it will catch itself in a loop. Instead, use the bind on the form fields. + self.prof1name = tk.StringVar() + self.prof1tut = tk.BooleanVar() + self.prof2_used = tk.BooleanVar() + self.prof2name = tk.StringVar() + self.prof2tut = tk.BooleanVar() + self.prof3_used = tk.BooleanVar() + self.prof3name = tk.StringVar() + self.prof3tut = tk.BooleanVar() + # current-profile form: + self.money = tk.IntVar() + self.equipped_weapon = tk.StringVar() + self.health = tk.IntVar() + self.stunt = tk.IntVar() + self.gun = tk.IntVar() + self.pur_weap_potato = tk.BooleanVar() + self.pur_weap_stinger = tk.BooleanVar() + self.pur_weap_watball = tk.BooleanVar() + self.pur_weap_snow = tk.BooleanVar() + self.pur_weap_fire = tk.BooleanVar() + self.pur_weap_lightn = tk.BooleanVar() + self.pur_weap_romanc = tk.BooleanVar() + self.pur_weap_pumpkin = tk.BooleanVar() + # these strings will hold the uppercase-when-collected letters, and be used by the checkboxes for the purchasable planes. + self.pur_plane_str_marcie = tk.StringVar(value="marcie") + self.pur_plane_bool_marcie = tk.BooleanVar() + self.pur_plane_str_sally = tk.StringVar(value="sally") + self.pur_plane_bool_sally = tk.BooleanVar() + self.pur_plane_str_rerun = tk.StringVar(value="rerun") + self.pur_plane_bool_rerun = tk.BooleanVar() + self.pur_plane_str_pigpen = tk.StringVar(value="pigpen") + self.pur_plane_bool_pigpen = tk.BooleanVar() + self.pur_plane_str_woodstock = tk.StringVar(value="woodstock") + self.pur_plane_bool_woodstock = tk.BooleanVar() + self.pur_plane_str_baron = tk.StringVar(value="baron") + self.pur_plane_bool_baron = tk.BooleanVar() + # statusbar + self.statustext = tk.StringVar() + + # app label, for vanity + # WORKHERE, vanity tag does not actually fill two columns. + self.lbl_app = tk.Label(self.master,text="Savegame editor for Snoopy vs. the Red Baron") + self.lbl_app.grid(row=0,column=0,columnspan=2) + stk.Tooltip(self.lbl_app,text="Also checkout the cli with ./srb.py") + self.lbl_checksum = tk.Label(self.master,text="Checksum",underline=0) + self.lbl_checksum.grid(row=1,column=0) + self.ent_checksum = tk.Entry(state="readonly",textvariable=self.checksum) + #self.ent_checksum.bind("",self.load_form_into_data) + self.ent_checksum.grid(row=2,column=0) + self.chk_silent = tk.Checkbutton(self.master,text="silent",variable=self.silent) + self.chk_silent.grid(row=2,column=1) + self.frm_profiles = tk.Frame(self.master) #,borderwidth=1,background="green") + self.frm_profiles.grid(row=3,column=0,columnspan=2) + # investigate radio button key-bind, up-down, to increment/decrement choice? + self.rad_prof1 = stk.Radiobutton(self.frm_profiles,value=1,text="Profile 1",variable=self.selected_profile,underline=8) + self.rad_prof1.grid(row=0,column=0) + self.chk_prof1_used = tk.Checkbutton(self.frm_profiles,text="used",state="disabled",variable=self.prof1_used) + self.chk_prof1_used.grid(row=0,column=2) + self.chk_prof1_tut = stk.Checkbutton(self.frm_profiles,text="tutorial",variable=self.prof1tut,func=self.load_form_into_data) + #self.hook_checkbox_enter_keys(self.chk_prof1_tut,self.load_form_into_data) + self.chk_prof1_tut.grid(row=0,column=3) + self.ent_prof1name = stk.Entry(self.frm_profiles,textvariable=self.prof1name,func=self.load_form_into_data) + self.ent_prof1name.bind("",self.load_form_into_data) + self.ent_prof1name.bind("",self.load_form_into_data) + self.ent_prof1name.grid(row=0,column=4) + self.rad_prof2 = stk.Radiobutton(self.frm_profiles,value=2,text="Profile 2",variable=self.selected_profile,underline=8) + self.rad_prof2.grid(row=1,column=0) + self.prof2_used.set(False) + self.chk_prof2_used = tk.Checkbutton(self.frm_profiles,text="used",state="disabled",variable=self.prof2_used) + self.chk_prof2_used.grid(row=1,column=2) + self.chk_prof2_tut = stk.Checkbutton(self.frm_profiles,text="tutorial",variable=self.prof2tut,func=self.load_form_into_data) + #self.hook_checkbox_enter_keys(self.chk_prof2_tut,self.load_form_into_data) + self.chk_prof2_tut.grid(row=1,column=3) + self.ent_prof2name = stk.Entry(self.frm_profiles,textvariable=self.prof2name,func=self.load_form_into_data) + self.ent_prof2name.bind("",self.load_form_into_data) + self.ent_prof2name.grid(row=1,column=4) + self.rad_prof3 = stk.Radiobutton(self.frm_profiles,value=3,text="Profile 3",variable=self.selected_profile,underline=8) + self.rad_prof3.grid(row=2,column=0) + self.prof3_used.set(False) + self.chk_prof3_used = tk.Checkbutton(self.frm_profiles,text="used",state="disabled",variable=self.prof3_used) + self.chk_prof3_used.grid(row=2,column=2) + self.chk_prof3_tut = stk.Checkbutton(self.frm_profiles,text="tutorial",variable=self.prof3tut,func=self.load_form_into_data) + #self.hook_checkbox_enter_keys(self.chk_prof3_tut,self.load_form_into_data) + self.chk_prof3_tut.grid(row=2,column=3) + self.ent_prof3name = stk.Entry(self.frm_profiles,textvariable=self.prof3name,func=self.load_form_into_data) + self.ent_prof3name.bind("",self.load_form_into_data) + self.ent_prof3name.grid(row=2,column=4) + #self.selected_profile.set(1) # purposefully do not set this value. + + # one profile, so we will have to use the value of self.selected_profile + self.frm_curprof = tk.Frame(self.master) + self.frm_curprof.grid(row=4,column=0,rowspan=5) + # most form fields are inside the current profile + self.lbl_money = tk.Label(self.frm_curprof,text="Money") + self.lbl_money.grid(row=0,column=0) + self.ent_money = stk.Entry(self.frm_curprof,textvariable=self.money,func=self.load_form_into_data) + #self.ent_money.bind("",self.load_form_into_data) + self.ent_money.grid(row=0,column=1) + self.lbl_equ_weapon = tk.Label(self.frm_curprof,text="Equipped weapon") + self.lbl_equ_weapon.grid(row=1,column=0) + # list of names + equippable_weapons = [i["name"] for i in srb_lib.WEAPONS if i["e"]] + self.opt_equ_weapon = tk.OptionMenu(self.frm_curprof,self.equipped_weapon,*equippable_weapons) + # the indicatoron,padx,pady and grid sticky are incredibly useful and hard to find in the docs + self.opt_equ_weapon.config(indicatoron=False,padx=1,pady=1,takefocus=1) + self.opt_equ_weapon.grid(row=1,column=1,sticky="ew") + self.lbl_health = tk.Label(self.frm_curprof,text="Health") + self.lbl_health.grid(row=2,column=0) + self.spn_health = stk.Spinbox(self.frm_curprof,from_=1,to=4,textvariable=self.health) + self.spn_health.grid(row=2,column=1) + self.lbl_stunt = tk.Label(self.frm_curprof,text="Stunt") + self.lbl_stunt.grid(row=3,column=0) + self.spn_stunt = stk.Spinbox(self.frm_curprof,from_=1,to=4,textvariable=self.stunt) + self.spn_stunt.grid(row=3,column=1) + self.lbl_gun = tk.Label(self.frm_curprof,text="Gun") + self.lbl_gun.grid(row=4,column=0) + self.spn_gun = stk.Spinbox(self.frm_curprof,from_=1,to=5,textvariable=self.gun) + self.spn_gun.grid(row=4,column=1) + self.lbl_pur = tk.Label(self.frm_curprof,text="Purchased: Weapons") + self.lbl_pur.grid(row=5,column=0) + #self.lbl_pur_weap = tk.Label(self.frm_curprof,text="Weapons") + #self.lbl_pur_weap.grid(row=5,column=0) + self.lbl_pur_weap = tk.Label(self.frm_curprof,text="Planes") + self.lbl_pur_weap.grid(row=5,column=1) + self.chk_pur_weap_potato = stk.Checkbutton(self.frm_curprof,text="Potato Gun",variable=self.pur_weap_potato,func=self.load_form_into_data) + self.chk_pur_weap_potato.grid(row=6,column=0,sticky="W") + self.chk_pur_weap_stinger = stk.Checkbutton(self.frm_curprof,text="Stinger",variable=self.pur_weap_stinger,func=self.load_form_into_data) + self.chk_pur_weap_stinger.grid(row=7,column=0,sticky="W") + self.chk_pur_weap_watball = stk.Checkbutton(self.frm_curprof,text="Water Balloon Cannon",variable=self.pur_weap_watball,func=self.load_form_into_data) + self.chk_pur_weap_watball.grid(row=8,column=0,sticky="W") + self.chk_pur_weap_snow = stk.Checkbutton(self.frm_curprof,text="Snow Blower",variable=self.pur_weap_snow,func=self.load_form_into_data) + self.chk_pur_weap_snow.grid(row=9,column=0,sticky="W") + self.chk_pur_weap_fire = stk.Checkbutton(self.frm_curprof,text="Fire Boomerang",variable=self.pur_weap_fire,func=self.load_form_into_data) + self.chk_pur_weap_fire.grid(row=10,column=0,sticky="W") + self.chk_pur_weap_lightn = stk.Checkbutton(self.frm_curprof,text="Lightning Rod",variable=self.pur_weap_lightn,func=self.load_form_into_data) + self.chk_pur_weap_lightn.grid(row=11,column=0,sticky="W") + self.chk_pur_weap_romanc = stk.Checkbutton(self.frm_curprof,text="Roman Candles",variable=self.pur_weap_romanc,func=self.load_form_into_data) + self.chk_pur_weap_romanc.grid(row=12,column=0,sticky="W") + self.chk_pur_weap_pumpkin = stk.Checkbutton(self.frm_curprof,text="10 Gauge Pumpkin",variable=self.pur_weap_pumpkin,func=self.load_form_into_data) + self.chk_pur_weap_pumpkin.grid(row=13,column=0,sticky="W") + self.chk_pur_plane_marcie = stk.Checkbutton(self.frm_curprof,textvariable=self.pur_plane_str_marcie,variable=self.pur_plane_bool_marcie,func=self.load_form_into_data) + self.chk_pur_plane_marcie.grid(row=6,column=1,sticky="W") + self.chk_pur_plane_sally = stk.Checkbutton(self.frm_curprof,textvariable=self.pur_plane_str_sally,variable=self.pur_plane_bool_sally,func=self.load_form_into_data) + self.chk_pur_plane_sally.grid(row=7,column=1,sticky="W") + self.chk_pur_plane_rerun = stk.Checkbutton(self.frm_curprof,textvariable=self.pur_plane_str_rerun,variable=self.pur_plane_bool_rerun,func=self.load_form_into_data) + self.chk_pur_plane_rerun.grid(row=8,column=1,sticky="W") + self.chk_pur_plane_pigpen = stk.Checkbutton(self.frm_curprof,textvariable=self.pur_plane_str_pigpen,variable=self.pur_plane_bool_pigpen,func=self.load_form_into_data) + self.chk_pur_plane_pigpen.grid(row=9,column=1,sticky="W") + self.chk_pur_plane_woodstock = stk.Checkbutton(self.frm_curprof,textvariable=self.pur_plane_str_woodstock,variable=self.pur_plane_bool_woodstock,func=self.load_form_into_data) + self.chk_pur_plane_woodstock.grid(row=10,column=1,sticky="W") + self.chk_pur_plane_baron = stk.Checkbutton(self.frm_curprof,textvariable=self.pur_plane_str_baron,variable=self.pur_plane_bool_baron,func=self.load_form_into_data) + self.chk_pur_plane_baron.grid(row=11,column=1,sticky="W") + + # status bar + self.status = stk.StatusBar(self.master,var=self.statustext) + #self.grid(expand=True,fill=tk.BOTH) + # and now open the first file + self.func_open(self.current_file.get()) + + def load_form_into_data(self, arg1 = None, arg2 = None, arg3 = None): + """ + Update self.bdata with the values currently displayed/edited on the form. This also updates the checksum. + """ + #print(f"debug (load_form_into_data): {arg1}, {arg2}, {arg3}") + if self.bdata: + bdata = self.bdata + else: + raise Exception("No file loaded.") + try: + bdata, message = srb_lib.set_name(bdata,1,self.prof1name.get()) + if message != "": + self.flash_entry(self.ent_prof1name) + raise Exception("Profile 1 name") + bdata, message = srb_lib.set_name(bdata,2,self.prof2name.get()) + if message != "": + self.flash_entry(self.ent_prof2name) + raise Exception("Profile 2 name") + bdata, message = srb_lib.set_name(bdata,3,self.prof3name.get()) + if message != "": + self.flash_entry(self.ent_prof3name) + raise Exception("Profile 3 name") + #print(f"debug: loading data with value prof1 tutorial: {self.prof1tut.get()}") + bdata, _ = srb_lib.set_tutorial_completed(bdata,1,self.prof1tut.get()) + bdata, _ = srb_lib.set_tutorial_completed(bdata,2,self.prof2tut.get()) + bdata, _ = srb_lib.set_tutorial_completed(bdata,3,self.prof3tut.get()) + + # and now the current profile stuff + profile_id = self.selected_profile.get() + if profile_id >= 1 and profile_id <= 3: + bdata = srb_lib.set_money(bdata,profile_id,self.money.get(),silent=self.silent.get()) + bdata, _ = srb_lib.set_plane_stat(bdata,profile_id,"health",self.health.get()) + bdata, _ = srb_lib.set_plane_stat(bdata,profile_id,"stunt",self.stunt.get()) + bdata, _ = srb_lib.set_plane_stat(bdata,profile_id,"gun",self.gun.get()) + bdata2 = srb_lib.set_weapon(bdata,profile_id,self.equipped_weapon.get(),silent=self.silent.get()) + if bdata2 == -1: + raise Exception("Failed when setting equipped weapon.") + else: + bdata = bdata2 + # purchased weapons + pur_weap_list = [] + if self.pur_weap_potato.get(): + pur_weap_list.append("Potato Gun") + if self.pur_weap_stinger.get(): + pur_weap_list.append("Stinger") + if self.pur_weap_watball.get(): + pur_weap_list.append("Water Balloon Cannon") + if self.pur_weap_snow.get(): + pur_weap_list.append("Snow Blower") + if self.pur_weap_fire.get(): + pur_weap_list.append("Fire Boomerang") + if self.pur_weap_lightn.get(): + pur_weap_list.append("Lightning Rod") + if self.pur_weap_romanc.get(): + pur_weap_list.append("Roman Candles") + if self.pur_weap_pumpkin.get(): + pur_weap_list.append("10 Gauge Pumpkin") + bdata, message = srb_lib.set_purchased_weapons(bdata, profile_id, "remove",["all"], silent=self.silent.get()) + if bdata == -1 or message != "": + raise Exception("Failed when clearing purchased weapons.") + #print(f"debug: will set weapon list {pur_weap_list}") + bdata, message = srb_lib.set_purchased_weapons(bdata, profile_id, "add",pur_weap_list, silent=self.silent.get()) + if bdata == -1 or message != "": + raise Exception("Failed when setting purchased weapons.") + # purchased planes + pur_planes_list = [] + if self.pur_plane_bool_marcie.get(): + pur_planes_list.append("marcie") + if self.pur_plane_bool_sally.get(): + pur_planes_list.append("sally") + if self.pur_plane_bool_rerun.get(): + pur_planes_list.append("rerun") + if self.pur_plane_bool_pigpen.get(): + pur_planes_list.append("pigpen") + if self.pur_plane_bool_woodstock.get(): + pur_planes_list.append("woodstock") + if self.pur_plane_bool_baron.get(): + pur_planes_list.append("baron") + bdata, message = srb_lib.set_purchased_planes(bdata, profile_id, "remove",["all"], silent=self.silent.get()) + if bdata == -1 or message != "": + raise Exception("Failed when clearing purchased planes.") + #print(f"debug: will set plane list {pur_planes_list}") + bdata, message = srb_lib.set_purchased_planes(bdata, profile_id, "add", pur_planes_list, silent=self.silent.get()) + if bdata == -1 or message != "": + raise Exception(f"Failed when setting purchased planes, message {message}.") + except Exception as e: + bdata = -1 + print(f"ERROR! Invalid {e}") + # If everything saved correctly, reload the checksum and then reload the form with the new data. This means that if you type in lower-case letters in a profile name, it will reload the valid upper-case name because the game only stores uppercase letters. + if bdata != -1: + self.bdata = bdata + csum = 0 + try: + csum = srb_lib.calculate_checksum(self.bdata) + except: + pass + if csum != 0: + #print(f"need to do something with new checksum 0x{csum:08x}") + self.bchecksum = csum + self.load_data_into_form() + + def flash_entry(self, this_entry): + """ Flash the offending entry box red for one second. """ + # Improve: move this to stackrpms_tk_lib? + current_color = this_entry.cget("background") + #print(f"DEBUG (flash_entry): this_entry={this_entry}") + #print(f"DEBUG: current_color {current_color}") + if current_color in ["#ffffff","white"]: # name is supposed to work but it does not + next_color = "red" #if current_color == "#ffffff" else "white" + this_entry.config(background=next_color) + self.master.after(1000, lambda te=this_entry: self.flash_entry(te)) + else: + next_color = "white" + this_entry.config(background=next_color) + + # functions + def func_about(self): + """ Display about dialog. """ + tkinter.messagebox.Message(title="About",message=ABOUT_TEXT,icon="info").show() + + def func_exit(self): + # in case we need to manually do stuff + # otherwise command=self.client_exit would have sufficed. + self.master.quit() + + def func_open(self, filen = None): + if filen: + filename = filen + else: + filename = tkinter.filedialog.askopenfilename() + # filename might be a None or something if the dialog was cancelled + if filename: + self.current_file.set(filename) + self.statustext.set(f"File: {self.current_file.get()}") + self.bchecksum, self.bdata = srb_lib.read_file(filename) + if not self.silent.get(): + print(f"File {filename} has checksum 0x{self.bchecksum:08x}, len(data)={len(self.bdata)}") + self.load_data_into_form() + + def func_reload(self): + """ + Convenience function to just run func_open against the current file. The command lambda func_open failed to work because it seems to hardcode to just the first-defined current_file. + """ + self.func_open(self.current_file.get()) + + def mark_traces(self,action): + """ Enable or disable traces for variables. This protects the form from always calling load-form-into-data while we are populating the form initially! """ + if action == "enable": + for j in [ + self.health, + self.stunt, + self.gun, + self.equipped_weapon, + #self.prof1name, # do not trace the profile names, because it prevents spaces ending on the variables + #self.prof2name, + #self.prof3name, + ]: + j.trace("w",self.load_form_into_data) + # different one + self.selected_profile.trace("w",self.load_data_into_form_for_current_profile) + else: # disable + for j in [ + self.health, + self.selected_profile, + self.stunt, + self.gun, + self.equipped_weapon, + #self.prof1name, + #self.prof2name, + #self.prof3name, + ]: + for i in j.trace_info(): + if i[0][0] == "write": + j.trace_remove(i[0][0],i[1]) + + def load_data_into_form(self, arg1 = None, arg2 = None, arg3 = None): + """ Update all fields in dialog with contents of bdata. """ + #print(f"debug (load_data_into_form): {arg1}, {arg2}, {arg3}") + self.mark_traces("disable") + # set all variables that are tied to entry boxes + self.checksum.set(hex(self.bchecksum)[2:]) + self.prof1_used.set(srb_lib.get_profile_in_use(self.bdata,1)) + self.prof1tut.set(srb_lib.get_tutorial_completed(self.bdata,1)) + prof1name = srb_lib.get_name(self.bdata,1).rstrip() + self.prof1name.set(prof1name) + self.prof2_used.set(srb_lib.get_profile_in_use(self.bdata,2)) + self.prof2tut.set(srb_lib.get_tutorial_completed(self.bdata,2)) + prof2name = srb_lib.get_name(self.bdata,2).rstrip() + self.prof2name.set(prof2name) + self.prof3_used.set(srb_lib.get_profile_in_use(self.bdata,3)) + self.prof3tut.set(srb_lib.get_tutorial_completed(self.bdata,3)) + prof3name = srb_lib.get_name(self.bdata,3).rstrip() + self.prof3name.set(prof3name) + self.load_data_into_form_for_current_profile() + # not necessary because for_current_profile runs the enable. + #self.mark_traces("enable") + + def load_data_into_form_for_current_profile(self, arg1 = None, arg2 = None, arg3 = None): + """ Determine the currently-selected radio button for profile and load those values into the form. """ + if not self.bdata: + return "No file loaded." + profile_id = self.selected_profile.get() + #print(f"debug: Using profile_id {profile_id}") + #print(f"debug: arg1 {arg1}") + #print(f"debug: arg2 {arg2}") + #print(f"debug: arg3 {arg3}") + if profile_id >= 1 and profile_id <= 3: + self.mark_traces("disable") + # money + self.money.set(srb_lib.get_money(self.bdata,profile_id)) + # equipped weapon + self.equipped_weapon.set(srb_lib.get_weapon(self.bdata,profile_id)) + # plane stats + stat, _ = srb_lib.get_plane_stat(self.bdata,profile_id,"health") + self.health.set(stat) + stat, _ = srb_lib.get_plane_stat(self.bdata,profile_id,"stunt") + self.stunt.set(stat) + stat, _ = srb_lib.get_plane_stat(self.bdata,profile_id,"gun") + self.gun.set(stat) + # purchased weapons + weapons_list, weapons_mask = srb_lib.get_purchased_weapons(self.bdata,profile_id,silent=self.silent.get()) + if weapons_list == "all": + weapons_list = [i["name"] for i in srb_lib.WEAPONS if i["e"]] + elif weapons_list == "none": + weapons_list = [] + # and now set each weapon + self.pur_weap_potato.set(True if "Potato Gun" in weapons_list else False) + self.pur_weap_stinger.set(True if "Stinger" in weapons_list else False) + self.pur_weap_watball.set(True if "Water Balloon Cannon" in weapons_list else False) + self.pur_weap_snow.set(True if "Snow Blower" in weapons_list else False) + self.pur_weap_fire.set(True if "Fire Boomerang" in weapons_list else False) + self.pur_weap_lightn.set(True if "Lightning Rod" in weapons_list else False) + self.pur_weap_romanc.set(True if "Roman Candles" in weapons_list else False) + self.pur_weap_pumpkin.set(True if "10 Gauge Pumpkin" in weapons_list else False) + # purchased planes + planes_list, planes_mask = srb_lib.get_purchased_planes(self.bdata,profile_id,silent=self.silent.get()) + if planes_list == "all": + planes_list = [i["name"] for i in srb_lib.PLANES] + elif planes_list == "none": + planes_list = [] + self.pur_plane_bool_marcie.set(True if "marcie" in planes_list else False) + self.pur_plane_bool_sally.set(True if "sally" in planes_list else False) + self.pur_plane_bool_rerun.set(True if "rerun" in planes_list else False) + self.pur_plane_bool_pigpen.set(True if "pigpen" in planes_list else False) + self.pur_plane_bool_woodstock.set(True if "woodstock" in planes_list else False) + self.pur_plane_bool_baron.set(True if "baron" in planes_list else False) + x = 0 + for i in srb_lib.LEVELSETS: + _, letters = srb_lib.get_levelset_status(self.bdata,profile_id,x,silent=self.silent.get()) + if 0 == x: + self.pur_plane_str_marcie.set(letters) + elif 1 == x: + self.pur_plane_str_sally.set(letters) + elif 2 == x: + self.pur_plane_str_rerun.set(letters) + elif 3 == x: + self.pur_plane_str_pigpen.set(letters) + elif 4 == x: + self.pur_plane_str_woodstock.set(letters) + elif 5 == x: + self.pur_plane_str_baron.set(letters) + x += 1 + self.mark_traces("enable") + # end if-profile_id is 1,2 or 3. + + else: + print(f"Error! Cannot load that profile_id into the form.") + + def refresh_to_data(self): + """ Update bdata with values from dialog? """ + #srb_lib. + + def func_save(self): + filename = None + try: + filename = self.current_file.get() + except: + msg = "Cannot save because we have not opened anything yet." + print(f"WARNING: {msg}") + tk.messagebox.showerror(title="Error",message=msg) + if filename: + checksum = self.bchecksum + #print(f"how about save to file {filename}, checksum {checksum}") + #print(f"checksum hex 0x{checksum:08x}, int {checksum}") + srb_lib.write_file(filename,checksum, self.bdata) + + def func_save_as(self): + filename = None + try: + filename = tkinter.filedialog.asksaveasfilename(initialfile=self.current_file.get()) + except: + msg = "Cannot save as because did not get a valid file from the dialog." + tk.messagebox.showerror(title="Error",message=msg) + if filename: + checksum = self.bchecksum + srb_lib.write_file(filename,checksum,self.bdata) + +# main +root = tk.Tk() +srb_tk = App(root) +srb_tk.mainloop() -- cgit