private void DrawSlotSelection(Rect canvas) { GUI.DrawTexture(canvas, _darkBackground); if (_source.NullOrEmpty()) { return; } Rect viewRect = new Rect(canvas); viewRect.width -= 16f; viewRect.height = _source.Count * _rowHeight; Widgets.BeginScrollView(canvas, ref _availableScrollPosition, viewRect.AtZero()); int startRow = (int)Math.Floor((decimal)(_availableScrollPosition.y / _rowHeight)); startRow = (startRow < 0) ? 0 : startRow; int endRow = startRow + (int)(Math.Ceiling((decimal)(canvas.height / _rowHeight))); endRow = (endRow > _source.Count) ? _source.Count : endRow; for (int i = startRow; i < endRow; i++) { // gray out weapons not in stock Color baseColor = GUI.color; if (Find.VisibleMap.listerThings.AllThings.FindAll(x => x.def == _source[i]).Count <= 0) { GUI.color = Color.gray; } Rect row = new Rect(0f, i * _rowHeight, canvas.width, _rowHeight); Rect labelRect = new Rect(row); TooltipHandler.TipRegion(row, _source[i].GetWeightAndBulkTip()); labelRect.xMin += _margin; if (i % 2 == 0) { GUI.DrawTexture(row, _darkBackground); } Text.Anchor = TextAnchor.MiddleLeft; Widgets.Label(labelRect, _source[i].LabelCap); Text.Anchor = TextAnchor.UpperLeft; Widgets.DrawHighlightIfMouseover(row); if (Widgets.ButtonInvisible(row)) { LoadoutSlot slot = new LoadoutSlot(_source[i], 1); CurrentLoadout.AddSlot(slot); } // revert to original color GUI.color = baseColor; } Widgets.EndScrollView(); }
private void DrawSlotSelection(Rect canvas) { GUI.DrawTexture(canvas, _darkBackground); if (_source.NullOrEmpty()) { return; } Rect viewRect = new Rect(canvas); viewRect.width -= 16f; viewRect.height = _source.Count * _rowHeight; Widgets.BeginScrollView(canvas, ref _availableScrollPosition, viewRect.AtZero()); for (int i = 0; i < _source.Count; i++) { Rect row = new Rect(0f, i * _rowHeight, canvas.width, _rowHeight); Rect labelRect = new Rect(row); TooltipHandler.TipRegion(row, _source[i].GetWeightAndBulkTip()); labelRect.xMin += _margin; if (i % 2 == 0) { GUI.DrawTexture(row, _darkBackground); } Text.Anchor = TextAnchor.MiddleLeft; Widgets.Label(labelRect, _source[i].LabelCap); Text.Anchor = TextAnchor.UpperLeft; Widgets.DrawHighlightIfMouseover(row); if (Widgets.ButtonInvisible(row)) { var slot = new LoadoutSlot(_source[i], 1); CurrentLoadout.AddSlot(slot); } } Widgets.EndScrollView(); }
//TODO: Research if the UI list of items can be cached (That's the Unity UI list)... private void DrawSlotList(Rect canvas) { // set up content canvas Rect viewRect = new Rect(0f, 0f, canvas.width, _rowHeight * CurrentLoadout.SlotCount + 1); // create some extra height if we're dragging if (Dragging != null) { viewRect.height += _rowHeight; } // leave room for scrollbar if necessary if (viewRect.height > canvas.height) { viewRect.width -= 16f; } // darken whole area GUI.DrawTexture(canvas, _darkBackground); Widgets.BeginScrollView(canvas, ref _slotScrollPosition, viewRect); int i = 0; float curY = 0f; for (; i < CurrentLoadout.SlotCount; i++) { // create row rect Rect row = new Rect(0f, curY, viewRect.width, _rowHeight); curY += _rowHeight; // if we're dragging, and currently on this row, and this row is not the row being dragged - draw a ghost of the slot here if (Dragging != null && Mouse.IsOver(row) && Dragging != CurrentLoadout.Slots[i]) { // draw ghost GUI.color = new Color(.7f, .7f, .7f, .5f); DrawSlot(row, Dragging); GUI.color = Color.white; // catch mouseUp if (Input.GetMouseButtonUp(0)) { CurrentLoadout.MoveSlot(Dragging, i); Dragging = null; } // ofset further slots down row.y += _rowHeight; curY += _rowHeight; } // alternate row background if (i % 2 == 0) { GUI.DrawTexture(row, _darkBackground); } // draw the slot - grey out if draggin this, but only when dragged over somewhere else if (Dragging == CurrentLoadout.Slots[i] && !Mouse.IsOver(row)) { GUI.color = new Color(.6f, .6f, .6f, .4f); } DrawSlot(row, CurrentLoadout.Slots[i], CurrentLoadout.SlotCount > 1); GUI.color = Color.white; } // if we're dragging, create an extra invisible row to allow moving stuff to the bottom if (Dragging != null) { Rect row = new Rect(0f, curY, viewRect.width, _rowHeight); if (Mouse.IsOver(row)) { // draw ghost GUI.color = new Color(.7f, .7f, .7f, .5f); DrawSlot(row, Dragging); GUI.color = Color.white; // catch mouseUp if (Input.GetMouseButtonUp(0)) { CurrentLoadout.MoveSlot(Dragging, CurrentLoadout.Slots.Count - 1); Dragging = null; } } } // cancel drag when mouse leaves the area, or on mouseup. if (!Mouse.IsOver(viewRect) || Input.GetMouseButtonUp(0)) { Dragging = null; } Widgets.EndScrollView(); }
private void DrawSlot(Rect row, LoadoutSlot slot, bool slotDraggable = true) { // set up rects // dragging handle (square) | label (fill) | count (50px) | delete (iconSize) Rect draggingHandle = new Rect(row); draggingHandle.width = row.height; Rect labelRect = new Rect(row); if (slotDraggable) { labelRect.xMin = draggingHandle.xMax; } labelRect.xMax = row.xMax - _countFieldSize.x - _iconSize - 2 * _margin; Rect countRect = new Rect( row.xMax - _countFieldSize.x - _iconSize - 2 * _margin, row.yMin + (row.height - _countFieldSize.y) / 2f, _countFieldSize.x, _countFieldSize.y); Rect ammoRect = new Rect( countRect.xMin - _iconSize - _margin, row.yMin + (row.height - _iconSize) / 2f, _iconSize, _iconSize); Rect countModeRect = new Rect( ammoRect.xMin - _iconSize - _margin, row.yMin + (row.height - _iconSize) / 2f, _iconSize, _iconSize); Rect deleteRect = new Rect(countRect.xMax + _margin, row.yMin + (row.height - _iconSize) / 2f, _iconSize, _iconSize); // dragging on dragHandle if (slotDraggable) { TooltipHandler.TipRegion(draggingHandle, "CE_DragToReorder".Translate()); GUI.DrawTexture(draggingHandle, _iconMove); if (Mouse.IsOver(draggingHandle) && Input.GetMouseButtonDown(0)) { Dragging = slot; } } // interactions (main row rect) if (!Mouse.IsOver(deleteRect)) { Widgets.DrawHighlightIfMouseover(row); TooltipHandler.TipRegion(row, slot.genericDef != null ? slot.genericDef.GetWeightAndBulkTip(slot.count) : slot.thingDef.GetWeightAndBulkTip(slot.count)); } // label Text.Anchor = TextAnchor.MiddleLeft; Widgets.Label(labelRect, slot.LabelCap); Text.Anchor = TextAnchor.UpperLeft; // easy ammo adder, ranged weapons only if (slot.thingDef != null && slot.thingDef.IsRangedWeapon) { // make sure there's an ammoset defined AmmoSetDef ammoSet = ((slot.thingDef.GetCompProperties <CompProperties_AmmoUser>() == null) ? null : slot.thingDef.GetCompProperties <CompProperties_AmmoUser>().ammoSet); bool?temp = !((((ammoSet == null) ? null : ammoSet.ammoTypes)).NullOrEmpty()); if (temp ?? false) { if (Widgets.ButtonImage(ammoRect, _iconAmmoAdd)) { List <FloatMenuOption> options = new List <FloatMenuOption>(); int magazineSize = (slot.thingDef.GetCompProperties <CompProperties_AmmoUser>() == null) ? 0 : slot.thingDef.GetCompProperties <CompProperties_AmmoUser>().magazineSize; foreach (AmmoLink link in ((ammoSet == null) ? null : ammoSet.ammoTypes)) { options.Add(new FloatMenuOption(link.ammo.LabelCap, delegate { CurrentLoadout.AddSlot(new LoadoutSlot(link.ammo, (magazineSize <= 1 ? link.ammo.defaultAmmoCount : magazineSize))); })); } // Add in the generic for this gun. LoadoutGenericDef generic = DefDatabase <LoadoutGenericDef> .GetNamed("GenericAmmo-" + slot.thingDef.defName); if (generic != null) { options.Add(new FloatMenuOption(generic.LabelCap, delegate { CurrentLoadout.AddSlot(new LoadoutSlot(generic)); })); } Find.WindowStack.Add(new FloatMenu(options, "CE_AddAmmoFor".Translate(slot.thingDef.LabelCap))); } } } // count DrawCountField(countRect, slot); // toggle count mode if (slot.genericDef != null) { Texture2D curModeIcon = slot.countType == LoadoutCountType.dropExcess ? _iconDropExcess : _iconPickupDrop; string tipString = slot.countType == LoadoutCountType.dropExcess ? "Drop Excess" : "Pickup Missing and Drop Excess"; if (Widgets.ButtonImage(countModeRect, curModeIcon)) { slot.countType = slot.countType == LoadoutCountType.dropExcess ? LoadoutCountType.pickupDrop : LoadoutCountType.dropExcess; } TooltipHandler.TipRegion(countModeRect, tipString); } // delete if (Mouse.IsOver(deleteRect)) { GUI.DrawTexture(row, TexUI.HighlightTex); } if (Widgets.ButtonImage(deleteRect, _iconClear)) { CurrentLoadout.RemoveSlot(slot); } TooltipHandler.TipRegion(deleteRect, "CE_DeleteFilter".Translate()); }
public override void DoWindowContents(Rect canvas) { // fix weird zooming bug Text.Font = GameFont.Small; // SET UP RECTS // top buttons Rect selectRect = new Rect(0f, 0f, canvas.width * .2f, _topAreaHeight); Rect newRect = new Rect(selectRect.xMax + _margin, 0f, canvas.width * .2f, _topAreaHeight); Rect copyRect = new Rect(newRect.xMax + _margin, 0f, canvas.width * .2f, _topAreaHeight); Rect deleteRect = new Rect(copyRect.xMax + _margin, 0f, canvas.width * .2f, _topAreaHeight); // main areas Rect nameRect = new Rect( 0f, _topAreaHeight + _margin * 2, (canvas.width - _margin) / 2f, 24f); Rect slotListRect = new Rect( 0f, nameRect.yMax + _margin, (canvas.width - _margin) / 2f, canvas.height - _topAreaHeight - nameRect.height - _barHeight * 2 - _margin * 5); Rect weightBarRect = new Rect(slotListRect.xMin, slotListRect.yMax + _margin, slotListRect.width, _barHeight); Rect bulkBarRect = new Rect(weightBarRect.xMin, weightBarRect.yMax + _margin, weightBarRect.width, _barHeight); Rect sourceButtonRect = new Rect( slotListRect.xMax + _margin, _topAreaHeight + _margin * 2, (canvas.width - _margin) / 2f, 24f); Rect selectionRect = new Rect( slotListRect.xMax + _margin, sourceButtonRect.yMax + _margin, (canvas.width - _margin) / 2f, canvas.height - 24f - _topAreaHeight - _margin * 3); LoadoutManager.SortLoadouts(); List <Loadout> loadouts = LoadoutManager.Loadouts.Where(l => !l.defaultLoadout).ToList(); // DRAW CONTENTS // buttons // select loadout if (Widgets.ButtonText(selectRect, "CE_SelectLoadout".Translate())) { List <FloatMenuOption> options = new List <FloatMenuOption>(); if (loadouts.Count == 0) { options.Add(new FloatMenuOption("CE_NoLoadouts".Translate(), null)); } else { for (int i = 0; i < loadouts.Count; i++) { int local_i = i; options.Add(new FloatMenuOption(loadouts[i].LabelCap, delegate { CurrentLoadout = loadouts[local_i]; })); } } Find.WindowStack.Add(new FloatMenu(options)); } // create loadout if (Widgets.ButtonText(newRect, "CE_NewLoadout".Translate())) { Loadout loadout = new Loadout(); loadout.AddBasicSlots(); LoadoutManager.AddLoadout(loadout); CurrentLoadout = loadout; } // copy loadout if (CurrentLoadout != null && Widgets.ButtonText(copyRect, "CE_CopyLoadout".Translate())) { CurrentLoadout = CurrentLoadout.Copy(); LoadoutManager.AddLoadout(CurrentLoadout); } // delete loadout if (loadouts.Any(l => l.canBeDeleted) && Widgets.ButtonText(deleteRect, "CE_DeleteLoadout".Translate())) { List <FloatMenuOption> options = new List <FloatMenuOption>(); for (int i = 0; i < loadouts.Count; i++) { int local_i = i; // don't allow deleting the default loadout if (!loadouts[i].canBeDeleted) { continue; } options.Add(new FloatMenuOption(loadouts[i].LabelCap, delegate { if (CurrentLoadout == loadouts[local_i]) { CurrentLoadout = LoadoutManager.DefaultLoadout; } LoadoutManager.RemoveLoadout(loadouts[local_i]); })); } Find.WindowStack.Add(new FloatMenu(options)); } // draw notification if no loadout selected if (CurrentLoadout == null) { Text.Anchor = TextAnchor.MiddleCenter; GUI.color = Color.grey; Widgets.Label(canvas, "CE_NoLoadoutSelected".Translate()); GUI.color = Color.white; Text.Anchor = TextAnchor.UpperLeft; // and stop further drawing return; } // name DrawNameField(nameRect); // source selection DrawSourceSelection(sourceButtonRect); // selection area DrawSlotSelection(selectionRect); // current slots DrawSlotList(slotListRect); // bars if (CurrentLoadout != null) { Utility_Loadouts.DrawBar(weightBarRect, CurrentLoadout.Weight, Utility_Loadouts.medianWeightCapacity, "CE_Weight".Translate(), CurrentLoadout.GetWeightTip()); Utility_Loadouts.DrawBar(bulkBarRect, CurrentLoadout.Bulk, Utility_Loadouts.medianBulkCapacity, "CE_Bulk".Translate(), CurrentLoadout.GetBulkTip()); } // done! }
private void DrawSlot(Rect row, LoadoutSlot slot, bool slotDraggable = true) { // set up rects // dragging handle (square) | label (fill) | count (50px) | delete (iconSize) Rect draggingHandle = new Rect(row); draggingHandle.width = row.height; Rect labelRect = new Rect(row); if (slotDraggable) { labelRect.xMin = draggingHandle.xMax; } labelRect.xMax = row.xMax - _countFieldSize.x - _iconSize - 2 * _margin; Rect countRect = new Rect( row.xMax - _countFieldSize.x - _iconSize - 2 * _margin, row.yMin + (row.height - _countFieldSize.y) / 2f, _countFieldSize.x, _countFieldSize.y); Rect ammoRect = new Rect( countRect.xMin - _iconSize - _margin, row.yMin + (row.height - _iconSize) / 2f, _iconSize, _iconSize); Rect deleteRect = new Rect(countRect.xMax + _margin, row.yMin + (row.height - _iconSize) / 2f, _iconSize, _iconSize); // dragging on dragHandle if (slotDraggable) { TooltipHandler.TipRegion(draggingHandle, "CR.DragToReorder".Translate()); GUI.DrawTexture(draggingHandle, _iconMove); if (Mouse.IsOver(draggingHandle) && Input.GetMouseButtonDown(0)) { Dragging = slot; } } // interactions (main row rect) if (!Mouse.IsOver(deleteRect)) { Widgets.DrawHighlightIfMouseover(row); TooltipHandler.TipRegion(row, slot.Def.GetWeightAndBulkTip(slot.Count)); } // label Text.Anchor = TextAnchor.MiddleLeft; Widgets.Label(labelRect, slot.Def.LabelCap); Text.Anchor = TextAnchor.UpperLeft; // easy ammo adder, ranged weapons only if (slot.Def.IsRangedWeapon) { // make sure there's an ammoset defined AmmoSetDef ammoSet = ((slot.Def.GetCompProperties <CompProperties_AmmoUser>() == null) ? null : slot.Def.GetCompProperties <CompProperties_AmmoUser>().ammoSet); bool?temp = !((((ammoSet == null) ? null : ammoSet.ammoTypes)).NullOrEmpty()); if (temp ?? false) { if (Widgets.ButtonImage(ammoRect, _iconAmmoAdd)) { List <FloatMenuOption> options = new List <FloatMenuOption>(); foreach (var ammo in ((ammoSet == null) ? null : ammoSet.ammoTypes)) { options.Add(new FloatMenuOption(ammo.LabelCap, delegate { CurrentLoadout.AddSlot(new LoadoutSlot(ammo)); })); } Find.WindowStack.Add(new FloatMenu(options, "CR.AddAmmoFor".Translate(slot.Def.LabelCap))); } } } // count DrawCountField(countRect, slot); // delete if (Mouse.IsOver(deleteRect)) { GUI.DrawTexture(row, TexUI.HighlightTex); } if (Widgets.ButtonImage(deleteRect, _iconClear)) { CurrentLoadout.RemoveSlot(slot); } TooltipHandler.TipRegion(deleteRect, "CR.DeleteFilter".Translate()); }
private void DrawSlotSelection(Rect canvas) { int count = _sourceType == SourceSelection.Generic ? _sourceGeneric.Count : _source.Count; GUI.DrawTexture(canvas, _darkBackground); if ((_sourceType != SourceSelection.Generic && _source.NullOrEmpty()) || (_sourceType == SourceSelection.Generic && _sourceGeneric.NullOrEmpty())) { return; } Rect viewRect = new Rect(canvas); viewRect.width -= 16f; viewRect.height = count * _rowHeight; Widgets.BeginScrollView(canvas, ref _availableScrollPosition, viewRect.AtZero()); int startRow = (int)Math.Floor((decimal)(_availableScrollPosition.y / _rowHeight)); startRow = (startRow < 0) ? 0 : startRow; int endRow = startRow + (int)(Math.Ceiling((decimal)(canvas.height / _rowHeight))); endRow = (endRow > count) ? count : endRow; for (int i = startRow; i < endRow; i++) { // gray out weapons not in stock Color baseColor = GUI.color; if (_sourceType == SourceSelection.Generic) { if (GetVisibleGeneric(_sourceGeneric[i])) { GUI.color = Color.gray; } } else { if (_source[i].isGreyedOut) { GUI.color = Color.gray; } } Rect row = new Rect(0f, i * _rowHeight, canvas.width, _rowHeight); Rect labelRect = new Rect(row); if (_sourceType == SourceSelection.Generic) { TooltipHandler.TipRegion(row, _sourceGeneric[i].GetWeightAndBulkTip()); } else { TooltipHandler.TipRegion(row, _source[i].thingDef.GetWeightAndBulkTip()); } labelRect.xMin += _margin; if (i % 2 == 0) { GUI.DrawTexture(row, _darkBackground); } Text.Anchor = TextAnchor.MiddleLeft; Text.WordWrap = false; if (_sourceType == SourceSelection.Generic) { Widgets.Label(labelRect, _sourceGeneric[i].LabelCap); } else { Widgets.Label(labelRect, _source[i].thingDef.LabelCap); } Text.WordWrap = true; Text.Anchor = TextAnchor.UpperLeft; Widgets.DrawHighlightIfMouseover(row); if (Widgets.ButtonInvisible(row)) { LoadoutSlot slot; if (_sourceType == SourceSelection.Generic) { slot = new LoadoutSlot(_sourceGeneric[i]); } else { slot = new LoadoutSlot(_source[i].thingDef); } CurrentLoadout.AddSlot(slot); } // revert to original color GUI.color = baseColor; } Widgets.EndScrollView(); }