//returns a new instance of KerbalXUploadData for the given craft or returns an existing instance if one has already been created. public static KerbalXUploadData prepare_for(CraftData for_craft) { KerbalXUploadData data; if (upload_data_store.ContainsKey(for_craft.path)) { CraftManager.log("loading upload data for: " + for_craft.name); data = upload_data_store[for_craft.path]; } else { CraftManager.log("creating upload data for: " + for_craft.name); data = new KerbalXUploadData(for_craft); upload_data_store.Add(for_craft.path, data); } return(data); }
protected void show_update_kerbalx_craft_dialog() { string resp = ""; CraftData craft = CraftData.selected_craft; if (craft.upload_data == null) //create new instance of upload data if it's not already been set { craft.upload_data = KerbalXUploadData.prepare_for(craft); } craft.upload_data.update_to_id = craft.matching_remote_ids.Count == 0 ? 0 : craft.matching_remote_ids[0]; string menu_label = ""; string kx_url = ""; bool show_craft_select_menu = craft.matching_remote_ids.Count != 1; Rect d_offset = new Rect(); Dictionary <string, string> upload_select_data_matching = new Dictionary <string, string>(); Dictionary <string, string> upload_select_data_all = new Dictionary <string, string>(); foreach (int id in craft.matching_remote_ids) { upload_select_data_matching.Add(id.ToString(), KerbalX.api.user_craft[id]["url"].Replace(("/" + KerbalX.api.logged_in_as + "/"), "")); } foreach (KeyValuePair <int, Dictionary <string, string> > c in KerbalX.api.user_craft) { upload_select_data_all.Add(c.Key.ToString(), c.Value["url"].Replace(("/" + KerbalX.api.logged_in_as + "/"), "")); } DropdownMenuData upload_select_menu = new DropdownMenuData(); DropdownMenuData upload_select_menu_2 = new DropdownMenuData(); if (craft.matching_remote_ids.Count == 0) { upload_select_menu.set_data(upload_select_data_all); } else { upload_select_menu.set_data(upload_select_data_matching); } upload_select_menu_2.set_data(upload_select_data_all); show_dialog("Update KerbalX Craft", "", d => { d_offset.x = -d.window_pos.x; d_offset.y = -d.window_pos.y; if (craft.upload_data.update_to_id != 0) { menu_label = KerbalX.api.user_craft[craft.upload_data.update_to_id]["url"].Replace(("/" + KerbalX.api.logged_in_as + "/"), ""); kx_url = KerbalX.api.url_to(KerbalX.api.user_craft[craft.upload_data.update_to_id]["url"]); } else { menu_label = "Select a craft"; } if (show_craft_select_menu) { if (craft.matching_remote_ids.Count > 1) { label("There are " + craft.matching_remote_ids.Count + " craft in your account with the same name as this craft"); } label("Select which craft you want to update"); dropdown(menu_label, StyleSheet.assets["caret-down"], "upload_select_menu", upload_select_menu, d, d_offset, d.window_pos.width * 0.8f, (sel) => { craft.upload_data.update_to_id = int.Parse(sel); }); if (upload_select_menu.items != upload_select_data_all) { section(() => { fspace(); button("show all your craft in menu", "hyperlink", () => { upload_select_menu.set_data(upload_select_data_all); open_dropdown(upload_select_menu); }); }); } } if (!String.IsNullOrEmpty(kx_url)) { label("This will update the KerbalX craft:"); section((w) => { button(kx_url, "hyperlink.update_url", 500f, 800f, () => { Application.OpenURL(kx_url); }); if (!show_craft_select_menu) { dropdown("edit", StyleSheet.assets["caret-down"], "upload_select_menu_2", upload_select_menu_2, d, d_offset, d.window_pos.width * 0.8f, (sel) => { craft.upload_data.update_to_id = int.Parse(sel); }); } }); section(() => { fspace(); label("make sure this is the craft you want to update!", "small.compact"); }); } foreach (string error in craft.upload_data.errors) { label(error, "error.bold"); } GUILayout.Space(10f); gui_state(craft.upload_data.update_to_id > 0, () => { button("Confirm Update", "button.large", () => { craft.upload_data.put(); }); }); GUILayout.Space(4f); section(() => { button("Upload as a new Craft", "button.large", open_upload_interface); button("Cancel", "button.large", close_dialog); }); return(resp); }); }