private void LoadAssignedWeapons() { SelectablePawns p = this.selectablePawns[this.pawnIndex]; if (!WorldComp.TryGetAssignedWeapons(p.Pawn, out this.assignedWeapons)) { this.assignedWeapons = new AssignedWeaponContainer { Pawn = p.Pawn }; if (p.Pawn.equipment.Primary != null) { this.assignedWeapons.Add(p.Pawn.equipment.Primary); } WorldComp.AddAssignedWeapons(p.Pawn, this.assignedWeapons); } this.RebuildPossibleWeapons(); }
public override void DoWindowContents(Rect inRect) { #if TRACE ++i; #endif GUI.color = Color.white; Text.Font = GameFont.Small; try { float x = 0, y = 0; string label = ""; if (Settings.EnableAssignWeapons) { #region Assign To Widgets.Label(new Rect(x, y + 4, 100, 30), "WeaponStorage.AssignTo".Translate()); x += 80; if (this.selectablePawns.Count > 0 && GUI.Button(new Rect(x, y, 30, 30), previousTexture)) { --this.pawnIndex; if (this.pawnIndex < 0 || this.assignedWeapons == null) { this.pawnIndex = this.selectablePawns.Count - 1; } this.LoadAssignedWeapons(); } x += 30; label = (this.assignedWeapons != null) ? SelectablePawns.GetLabelAndStatsFor(this.assignedWeapons.Pawn) : ""; if (Widgets.ButtonText(new Rect(x, y, 400, 30), label)) { List <FloatMenuOption> options = new List <FloatMenuOption>(); foreach (SelectablePawns p in this.selectablePawns) { options.Add(new FloatMenuOption(p.LabelAndStats, delegate { this.UpdatePawnIndex(p.Pawn); }, MenuOptionPriority.Default, null, null, 0f, null, null)); } Find.WindowStack.Add(new FloatMenu(options)); } x += 400; if (this.selectablePawns.Count > 0 && GUI.Button(new Rect(x, y, 30, 30), nextTexture)) { ++this.pawnIndex; if (this.pawnIndex >= this.selectablePawns.Count || this.assignedWeapons == null) { this.pawnIndex = 0; } this.LoadAssignedWeapons(); } x += 40; #endregion y += 40; } #region Weapon Storage x = 0; Widgets.Label(new Rect(x, y - 4, 100, 60), "WeaponStorage".Translate()); x += 80; if (WorldComp.HasStorages() && GUI.Button(new Rect(x, y, 30, 30), previousTexture)) { this.NextWeaponStorage(-1); } x += 30; label = (this.weaponStorage != null) ? this.weaponStorage.Label : ""; if (Widgets.ButtonText(new Rect(x, y, 400, 30), label)) { List <FloatMenuOption> options = new List <FloatMenuOption>(); foreach (var ws in WorldComp.GetWeaponStorages()) { options.Add(new FloatMenuOption(ws.Label, delegate { this.weaponStorage = ws; this.RebuildPossibleWeapons(); }, MenuOptionPriority.Default, null, null, 0f, null, null)); } Find.WindowStack.Add(new FloatMenu(options)); } x += 400; if (WorldComp.HasStorages() && GUI.Button(new Rect(x, y, 30, 30), nextTexture)) { this.NextWeaponStorage(1); } x += 40; #endregion y += 40; Widgets.Label(new Rect(0, y + 4, 70, 30), "WeaponStorage.Search".Translate()); this.textBuffer = Widgets.TextField(new Rect(80, y, 200, 30), this.textBuffer); y += 40; const int HEIGHT = 30; const int BUFFER = 2; float width = inRect.width - 100; scrollPosition = GUI.BeginScrollView(new Rect(40, y, width, inRect.height - y - 121), scrollPosition, new Rect(0, 0, width - 16, this.PreviousY)); x = y = 0; if (this.PossibleWeapons != null) { ThingWithComps weapon; for (int i = 0; i < this.PossibleWeapons.Count; ++i) { x = 0; weapon = this.PossibleWeapons[i]; bool isChecked = false; if (this.assignedWeapons != null && this.weaponStorage != null) { isChecked = this.IsAssignedWeapon(i); if (!isChecked && !this.IncludeWeapon(weapon)) { continue; } bool backup = isChecked; Widgets.Checkbox(x, y, ref isChecked, 20); x += 20 + BUFFER; if (isChecked != backup) { if (this.IsAssignedWeapon(i)) { if (this.assignedWeapons.Pawn.equipment.Primary == weapon) { this.assignedWeapons.Pawn.equipment.Remove(weapon); if (this.assignedWeapons.Pawn.jobs.curJob.def == JobDefOf.Hunt) { this.assignedWeapons.Pawn.jobs.StopAll(); } } if (this.assignedWeapons.Remove(weapon)) { if (this.weaponStorage == null || (!this.weaponStorage.AddWeapon(weapon) && !WorldComp.Add(weapon))) { BuildingUtil.DropSingleThing(weapon, this.assignedWeapons.Pawn.Position, this.assignedWeapons.Pawn.Map); } } else { Log.Error("Unable to remove weapon " + weapon); } } else { if (this.weaponStorage != null && this.weaponStorage.RemoveNoDrop(weapon)) { this.assignedWeapons.Add(weapon); } else { Log.Error("Unable to remove weapon " + weapon); } } this.RebuildPossibleWeapons(); break; } } if (!isChecked && !this.IncludeWeapon(weapon)) { continue; } Widgets.ThingIcon(new Rect(x, y, HEIGHT, HEIGHT), weapon); x += HEIGHT + BUFFER; if (Widgets.InfoCardButton(x, y, weapon)) { Find.WindowStack.Add(new Dialog_InfoCard(weapon)); } x += HEIGHT + BUFFER; Widgets.Label(new Rect(x, y, 250, HEIGHT), weapon.Label); x += 250 + BUFFER; if (this.weaponStorage != null && Widgets.ButtonImage(new Rect(width - 16 - HEIGHT, y, 20, 20), DropTexture)) { if (this.IsAssignedWeapon(i)) { if (!this.assignedWeapons.Remove(weapon)) { Log.Error("Unable to drop assigned weapon"); } } else { if (this.weaponStorage == null || !this.weaponStorage.Remove(weapon)) { Log.Error("Unable to remove weapon " + weapon); } } this.RebuildPossibleWeapons(); break; } var biocodableComp = weapon.GetComp <CompBiocodable>(); if (biocodableComp?.CodedPawn != null) { y += HEIGHT - 4; Widgets.Label(new Rect(x - 250 - BUFFER, y, 250, 20), biocodableComp.CompInspectStringExtra()); y += 4; } this.PossibleWeapons[i] = weapon; y += HEIGHT + BUFFER; } } else { #if TRACE if (i > 600) { Log.Warning("WeaponStorage DoWindowContents: Display non-checkbox weapons. Count: " + this.weaponStorage.Count); } #endif if (this.weaponStorage != null) { foreach (ThingWithComps t in this.weaponStorage.GetWeapons(false)) { #if TRACE if (i > 600) { Log.Warning("-" + t.Label); } #endif //if (!IncludeWeapon(t)) // continue; if (!this.IncludeWeapon(t)) { continue; } x = 34; Widgets.ThingIcon(new Rect(x, y, HEIGHT, HEIGHT), t); x += HEIGHT + BUFFER; if (Widgets.InfoCardButton(x, y, t)) { Find.WindowStack.Add(new Dialog_InfoCard(t)); } x += HEIGHT + BUFFER; Widgets.Label(new Rect(x, y, 250, HEIGHT), t.Label); x += 250 + BUFFER; if (Widgets.ButtonImage(new Rect(x + 100, y, 20, 20), DropTexture)) { this.weaponStorage.Remove(t); break; } y += HEIGHT + BUFFER; } foreach (ThingWithComps t in this.weaponStorage.GetBioEncodedWeapons()) { #if TRACE if (i > 600) { Log.Warning("-" + t.Label); } #endif //if (!IncludeWeapon(t)) // continue; x = 34; Widgets.ThingIcon(new Rect(x, y, HEIGHT, HEIGHT), t); x += HEIGHT + BUFFER; if (Widgets.InfoCardButton(x, y, t)) { Find.WindowStack.Add(new Dialog_InfoCard(t)); } x += HEIGHT + BUFFER; Widgets.Label(new Rect(x, y, 250, HEIGHT), t.Label); x += 250 + BUFFER; if (Widgets.ButtonImage(new Rect(x + 100, y, 20, 20), DropTexture)) { this.weaponStorage.Remove(t); break; } var biocodableComp = t.GetComp <CompBiocodable>(); if (biocodableComp?.CodedPawn != null) { y += HEIGHT - 4; Widgets.Label(new Rect(x - 250 - BUFFER, y, 250, 20), biocodableComp.CompInspectStringExtra()); y += 4; } y += HEIGHT + BUFFER; } } } GUI.EndScrollView(); this.PreviousY = y; } catch (Exception e) { String msg = this.GetType().Name + " closed due to: " + e.GetType().Name + " " + e.Message; Log.Error(msg); Messages.Message(msg, MessageTypeDefOf.NegativeEvent); base.Close(); } finally { Text.Anchor = TextAnchor.UpperLeft; GUI.color = Color.white; } }