示例#1
0
        internal static void ModifySlotCount(WidgetLayout layout, int maxSlots)
        {
            var slots            = layout.slots;
            var changedSlotCount = maxSlots - slots.Count;

            if (changedSlotCount == 0)
            {
                return;
            }

            var templateSlot = slots[0];

            // add missing
            int index = slots[0].GetSiblingIndex();

            for (var i = slots.Count; i < maxSlots; i++)
            {
                var newSlot = UnityEngine.Object.Instantiate(templateSlot, layout.layout_slots);
                //newSlot.localPosition = new Vector3(0, -(1 + i * SlotHeight), 0);
                newSlot.SetSiblingIndex(index + i);
                newSlot.name = "slot (" + i + ")";
                slots.Add(newSlot);
            }

            // remove abundant
            while (slots.Count > maxSlots)
            {
                var slot = slots.Last();
                slots.RemoveAt(slots.Count - 1);
                UnityEngine.Object.Destroy(slot.gameObject);
            }
        }
示例#2
0
        public void Layout_SetsDisplayHasFocus()
        {
            var widgetsCreated = new List <IWidget>();

            for (int i = 0; i < 10; i++)
            {
                var widget = CommonMocks.Widget("test" + i).Object;

                widgetsCreated.Add(widget);
                WidgetLayout.Add(widget);
            }

            for (int j = 0; j < 10; j++)
            {
                WidgetLayout.Focus = widgetsCreated[j];

                FocusShouldBe(widgetsCreated[j]);

                for (int i = 0; i < 10; i++)
                {
                    if (i == j)
                    {
                        widgetsCreated[i].Display.HasFocus.Should().BeTrue($"item {i} should be told that it has focus");
                    }
                    else
                    {
                        widgetsCreated[i].Display.HasFocus.Should().BeFalse(
                            $"after setting item {j} to have focus, item {i} should be marked as not having focus");
                    }
                }
            }
        }
示例#3
0
        public static void Postfix(MechLabLocationWidget __instance, int ___maxSlots, LocationLoadoutDef ___loadout)
        {
            try
            {
                // we can't reduce to zero
                if (___maxSlots < 1)
                {
                    return;
                }

                var widgetLayout = new WidgetLayout(__instance);
                if (widgetLayout.layout_slots == null)
                {
                    return;
                }

                if (__instance == (__instance.parentDropTarget as MechLabPanel).centerTorsoWidget)
                {
                    ___maxSlots -= MechLabSlotsFeature.settings.MechLabGeneralSlots;
                }

                ModifySlotCount(widgetLayout, ___maxSlots);
                AddFillersToSlots(widgetLayout, ___loadout.Location);
            }
            catch (Exception e)
            {
                Control.mod.Logger.LogError(e);
            }
        }
示例#4
0
        public void Layout_FirstItemHasFocus()
        {
            WidgetLayout.Clear();

            WidgetLayout.Focus.Should().BeNull();

            var widget = CommonMocks.Widget("widget").Object;

            WidgetLayout.Add(widget);
            WidgetLayout.Focus.Should().Be(widget);
        }
        private static void AddFillersToSlots(WidgetLayout layout)
        {
            var fillers = new List <Filler>();

            foreach (var slot in layout.slots)
            {
                var filler = Filler.CreateFromSlot(slot.gameObject);
                filler.Hide();
                fillers.Add(filler);
            }

            Fillers[layout.location] = fillers;
        }
 public static void Postfix(MechLabLocationWidget __instance, int ___maxSlots)
 {
     try
     {
         var widgetLayout = new WidgetLayout(__instance);
         MechLabSlotsFixer.FixSlots(widgetLayout, ___maxSlots);
         DynamicSlotsFeature.PrepareWidget(widgetLayout);
     }
     catch (Exception e)
     {
         Control.mod.Logger.LogError(e);
     }
 }
示例#7
0
    internal static void FixSlots(WidgetLayout widgetLayout, int ___maxSlots)
    {
        var mechLabPanel = (MechLabPanel)widgetLayout.widget.parentDropTarget;

        // MechPropertiesWidget feature
        if (widgetLayout.widget == mechLabPanel.centerTorsoWidget)
        {
            ___maxSlots = Mathf.Max(0,
                                    ___maxSlots - MechLabSlotsFeature.settings.TopLeftWidget.Slots -
                                    MechLabSlotsFeature.settings.TopRightWidget.Slots);
        }

        ModifyLayoutSlotCount(widgetLayout, ___maxSlots);
    }
示例#8
0
        public static void Postfix(MechLabLocationWidget __instance, int ___maxSlots, ref LocationLoadoutDef loadout)
        {
            try
            {
                var widget = __instance;

                var widgetLayout = new WidgetLayout(widget);
                MechLabSlotsFixer.FixSlots(widgetLayout, ___maxSlots);
                DynamicSlotsFeature.PrepareWidget(widgetLayout);
                AdjustMechLabLocationNaming(widget, loadout.Location);
            }
            catch (Exception e)
            {
                Control.Logger.Error.Log(e);
            }
        }
示例#9
0
        public void Layout_WidgetAddedEvent()
        {
            var widgetsAdded   = new List <IWidget>();
            var widgetsCreated = new List <IWidget>();

            WidgetLayout.WidgetAdded += w => widgetsAdded.Add(w);

            for (int i = 0; i < 10; i++)
            {
                var widget = CommonMocks.Widget("test" + i).Object;

                widgetsCreated.Add(widget);
                WidgetLayout.Add(widget);
            }

            widgetsAdded.Should().BeEquivalentTo(widgetsCreated);
        }
示例#10
0
        private static void AddFillersToSlots(WidgetLayout layout, ChassisLocations location)
        {
            // dispose of all fillers
            if (Fillers.TryGetValue(location, out var existing))
            {
                foreach (var old in existing)
                {
                    old.Dispose();
                }
            }

            var fillers = new List <Filler>();

            foreach (var slot in layout.slots)
            {
                fillers.Add(new Filler(slot));
            }

            Fillers[location] = fillers;
        }
示例#11
0
        private static void AddFillersToSlots(WidgetLayout layout)
        {
            var images = new List <Image>();

            foreach (var slot in layout.slots)
            {
                Image image;

                var go = slot.gameObject.FindFirstChildNamed("Filler");
                if (go == null)
                {
                    go = new GameObject("Filler");

                    var rect = go.AddComponent <RectTransform>();
                    rect.pivot            = new Vector2(0.5f, 0.5f);
                    rect.anchorMin        = new Vector2(0, 0);
                    rect.anchorMax        = new Vector2(1, 1);
                    rect.anchoredPosition = Vector2.zero;
                    rect.sizeDelta        = new Vector2(-6, -6);
                    go.AddComponent <CanvasRenderer>();

                    image = go.AddComponent <Image>();

                    rect.SetParent(slot, false);
                }
                else
                {
                    image = go.GetComponent <Image>();
                }

                go.SetActive(false);
                image.color = Color.red;
                images.Add(image);
            }

            FillerImageCache[layout.location] = images;
        }
示例#12
0
        public static void Postfix(MechLabLocationWidget __instance, int ___maxSlots, LocationLoadoutDef ___loadout)
        {
            try
            {
                // we can't reduce to zero
                if (___maxSlots < 1)
                {
                    return;
                }

                var widgetLayout = new WidgetLayout(__instance, ___loadout.Location);
                if (widgetLayout.layout_slots == null)
                {
                    return;
                }

                ModifySlotCount(widgetLayout, ___maxSlots);
                AddFillersToSlots(widgetLayout);
            }
            catch (Exception e)
            {
                Control.mod.Logger.LogError(e);
            }
        }
示例#13
0
        public void SaveAllSettings()
        {
            try
            {
                if (WindowStyle == WindowStyle.None)
                {
                    ToggleFullScreenMode();
                }

                var layoutFullPath = Path.Combine(SettingsHolder.Instance.Folder, "Settings");
                if (!Directory.Exists(layoutFullPath))
                {
                    Directory.CreateDirectory(layoutFullPath);
                }

                layoutFullPath = Path.Combine(layoutFullPath, "Layout.xml");
                if (!File.Exists(layoutFullPath))
                {
                    File.Delete(layoutFullPath);
                }

                new XmlLayoutSerializer(_dockManager).Serialize(layoutFullPath);
            }
            catch (Exception ex)
            {
                ErrorLogger.Instance.Write(string.Format("Error save layout:{0}\r\n{1}", ex.Message, ex.StackTrace));
            }

            var widgetLayoutFullPath = Path.Combine(SettingsHolder.Instance.Folder, "Settings", "WidgetLayout.xml");

            try
            {
                var widgetLayout = new WidgetLayout {
                    Widgets = new List <WidgetLayoutItem>()
                };
                foreach (var widget in _allWidgets)
                {
                    var widgetDescription = (WidgetDescription)widget.Tag;
                    var widgetLayoutItem  = new WidgetLayoutItem
                    {
                        WidgetName = widgetDescription.Name,
                        Top        = widget.Top,
                        Left       = widget.Left,
                        Height     = widget.Height,
                        Width      = widget.Width,
                        Visible    = widget.Visibility == Visibility.Visible,
                    };
                    widgetLayout.Widgets.Add(widgetLayoutItem);
                }
                using (var stream = File.Open(widgetLayoutFullPath, FileMode.Create))
                {
                    var serializer = new XmlSerializer(typeof(WidgetLayout));
                    serializer.Serialize(stream, widgetLayout);
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.Instance.Write(string.Format("Error saving widget layout: {0}", ex));
            }
            try
            {
                foreach (var outputWindow in _outputWindows)
                {
                    outputWindow.Save();
                }

                SettingsHolder.Instance.Settings.MainWindowTop    = (int)Top;
                SettingsHolder.Instance.Settings.MainWindowLeft   = (int)Left;
                SettingsHolder.Instance.Settings.MainWindowWidth  = (int)Width;
                SettingsHolder.Instance.Settings.MainWindowHeight = (int)Height;
                SettingsHolder.Instance.Settings.MainWindowState  = WindowState;

                SettingsHolder.Instance.SaveAllSettings();
            }
            catch (Exception ex)
            {
                ErrorLogger.Instance.Write(string.Format("Error save settings:{0}\r\n{1}", ex.Message, ex.StackTrace));
            }
        }
示例#14
0
 internal static void PrepareWidget(WidgetLayout widgetLayout)
 {
     AddFillersToSlots(widgetLayout);
 }
示例#15
0
    internal static void SetupWidget(
        string id,
        ref MechLabLocationWidget?topWidget,
        MechLabPanel mechLabPanel,
        MechLabLocationWidget armWidget,
        MechLabSlotsSettings.WidgetSettings settings
        )
    {
        GameObject go;

        if (topWidget == null)
        {
            var template = mechLabPanel.centerTorsoWidget;

            go      = Object.Instantiate(template.gameObject, null);
            go.name = id;
            go.SetActive(settings.Enabled);
            {
                var vlg = go.GetComponent <VerticalLayoutGroup>();
                vlg.padding = new RectOffset(0, 0, 0, 3);
                vlg.spacing = 4;
            }

            go.transform.Find("layout_armor").gameObject.SetActive(false);
            go.transform.Find("layout_hardpoints").gameObject.SetActive(false);
            go.transform.Find("layout_locationText/txt_structure").gameObject.SetActive(false);
            go.transform.Find("layout_locationText/txt_location").GetComponent <TextMeshProUGUI>().text = settings.Label;

            topWidget = go.GetComponent <MechLabLocationWidget>();
        }
        else
        {
            go = topWidget.gameObject;
        }

        var parent = armWidget.transform.parent;

        go.transform.SetParent(parent, false);
        go.transform.SetAsFirstSibling();
        go.GetComponent <LayoutElement>().ignoreLayout = true;
        {
            var rect = go.GetComponent <RectTransform>();
            rect.localPosition    = new Vector3(0, 0);
            rect.pivot            = new Vector2(0, 0);
            rect.anchoredPosition = new Vector2(0, -MechLabSlotsFeature.settings.MechLabArmTopPadding + 20);
        }
        {
            var clg = parent.GetComponent <VerticalLayoutGroup>();
            clg.padding = new RectOffset(0, 0, MechLabSlotsFeature.settings.MechLabArmTopPadding, 0);
        }

        topWidget.Init(mechLabPanel);

        var layout = new WidgetLayout(topWidget);

        MechLabSlotsFixer.ModifyLayoutSlotCount(layout, settings.Slots);
        {
            var mechRectTransform = parent.parent.GetComponent <RectTransform>();
            LayoutRebuilder.ForceRebuildLayoutImmediate(mechRectTransform);
        }
    }