public void UpdateRatios(int forceUnchanged = -1) { updating = true; int quantityFilledSlots = 0; float totalChance = 0; for (int i = 0; i < 10; i++) { ItemSlot slot = inv.GetSlot(i); quantityFilledSlots += (slot.Itemstack != null) ? 1 : 0; GuiElementNumberInput inp = SingleComposer.GetNumberInput("chance" + (i + 1)); totalChance += inp.GetValue(); } float chanceForMissingOnes = 100 / Math.Max(1, quantityFilledSlots); float scaleValue = 100f / totalChance; int totalNew = 0; for (int i = 0; i < 10; i++) { GuiElementNumberInput inp = SingleComposer.GetNumberInput("chance" + (i + 1)); ItemSlot slot = inv.GetSlot(i); if (slot.Itemstack == null) { inp.SetValue(""); continue; } int newVal = (int)(inp.GetValue() * scaleValue); if (inp.GetText().Length != 0) { if ((i != forceUnchanged || (int)(inp.GetValue()) > 100) && totalChance > 100) { inp.SetValue("" + newVal); totalNew += newVal; } else { totalNew += (int)inp.GetValue(); } } } updating = false; SingleComposer.GetDynamicText("totalchance").SetNewText("Total chance: " + (int)totalNew + "%"); }
public GuiDialogItemLootRandomizer(ItemStack[] stacks, float[] chances, ICoreClientAPI capi) : base("Item Loot Randomizer", capi) { double pad = GuiElementItemSlotGrid.unscaledSlotPadding; ElementBounds slotBounds = ElementStdBounds.SlotGrid(EnumDialogArea.None, pad, 45 + pad, 10, 1).FixedGrow(2 * pad, 2 * pad); ElementBounds chanceInputBounds = ElementBounds.Fixed(3, 0, 48, 30).FixedUnder(slotBounds, -4); ElementBounds leftButton = ElementBounds.Fixed(EnumDialogArea.LeftFixed, 0, 0, 0, 0).WithFixedPadding(10, 1); ElementBounds chanceTextBounds = ElementBounds.Fixed(EnumDialogArea.CenterFixed, 0, 0, 150, 30).WithFixedPadding(10, 1); ElementBounds rightButton = ElementBounds.Fixed(EnumDialogArea.RightFixed, 0, 0, 0, 0).WithFixedPadding(10, 1); ElementBounds bgBounds = ElementBounds.Fill.WithFixedPadding(ElementGeometrics.ElementToDialogPadding); bgBounds.BothSizing = ElementSizing.FitToChildren; inv = new InventoryGeneric(10, "lootrandomizer-1", capi, null); for (int i = 0; i < 10; i++) { inv.GetSlot(i).Itemstack = stacks[i]; } ElementBounds dialogBounds = ElementStdBounds .AutosizedMainDialog.WithAlignment(EnumDialogArea.CenterMiddle) .WithFixedAlignmentOffset(ElementGeometrics.DialogToScreenPadding, 0); float totalChance = chances.Sum(); string text = "Total chance: " + (int)totalChance + "%"; SingleComposer = capi.Gui .CreateCompo("itemlootrandomizer", dialogBounds, false) .AddDialogBG(bgBounds, true) .AddDialogTitleBar("Item Loot Randomizer", OnTitleBarClose) .BeginChildElements(bgBounds) .AddItemSlotGrid(inv, SendInvPacket, 10, slotBounds, "slots") .AddNumberInput(chanceInputBounds = chanceInputBounds.FlatCopy(), (t) => OnTextChanced(0), CairoFont.WhiteDetailText(), "chance1") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(1), CairoFont.WhiteDetailText(), "chance2") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(2), CairoFont.WhiteDetailText(), "chance3") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(3), CairoFont.WhiteDetailText(), "chance4") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(4), CairoFont.WhiteDetailText(), "chance5") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(5), CairoFont.WhiteDetailText(), "chance6") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(6), CairoFont.WhiteDetailText(), "chance7") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(7), CairoFont.WhiteDetailText(), "chance8") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(8), CairoFont.WhiteDetailText(), "chance9") .AddNumberInput(chanceInputBounds = chanceInputBounds.RightCopy(3), (t) => OnTextChanced(9), CairoFont.WhiteDetailText(), "chance10") .AddButton("Close", OnCloseClicked, leftButton.FixedUnder(chanceInputBounds, 25)) .AddDynamicText(text, CairoFont.WhiteDetailText(), EnumTextOrientation.Left, chanceTextBounds.FixedUnder(chanceInputBounds, 25), 1, "totalchance") .AddButton("Save", OnSaveClicked, rightButton.FixedUnder(chanceInputBounds, 25)) .EndChildElements() .Compose() ; for (int i = 0; i < 10; i++) { GuiElementNumberInput inp = SingleComposer.GetNumberInput("chance" + (i + 1)); inp.SetValue("" + chances[i]); } SingleComposer.GetSlotGrid("slots").CanClickSlot = OnCanClickSlot; }