Exemple #1
0
        /// <summary>
        /// Updates a school building's tooltip (in the education tool panel).
        /// </summary>
        /// <param name="prefab">School prefab to update</param>
        private void UpdateSchoolTooltip(BuildingInfo prefab)
        {
            // Find education panel game object.
            GameObject educationPanelObject = GameObject.Find("EducationDefaultPanel");

            if (educationPanelObject == null)
            {
                Logging.Message("couldn't find education panel object (tooltip won't be updated)");
            }
            else
            {
                // Find education panel scrollable panel.
                UIScrollablePanel edScrollPanel = educationPanelObject.GetComponentInChildren <UIScrollablePanel>();
                if (edScrollPanel == null)
                {
                    Logging.Message("couldn't find education panel scrollable panel (tooltip won't be updated)");
                }
                else
                {
                    // Find buttons in panel.
                    UIButton[] schoolButtons = edScrollPanel.GetComponentsInChildren <UIButton>();

                    if (schoolButtons == null)
                    {
                        Logging.Message("couldn't find school buttons (tooltip won't be updated)");
                    }
                    else
                    {
                        // Iterate through list of buttons, looking for a match for our prefab.
                        foreach (UIButton schoolButton in schoolButtons)
                        {
                            if (schoolButton.name.Equals(prefab.name))
                            {
                                // Match!  Update tooltip.
                                schoolButton.tooltip = prefab.GetLocalizedTooltip();
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        private void GetServiceBuildings(BuildingManager buildingManager, DistrictManager districtManager, ItemClass.Service service, string filter)
        {
            var m_size = buildingManager.GetServiceBuildings(service);

            for (ushort i = 0; i < m_size.m_size; i++)
            {
                Building building = buildingManager.m_buildings.m_buffer[m_size[i]];
                if (EnumExtensions.IsFlagSet(building.m_flags, Building.Flags.Created))
                {
                    BuildingInfo info = building.Info;
                    string       name = EnumExtensions.IsFlagSet(building.m_flags, Building.Flags.CustomName)
                        ? buildingManager.GetBuildingName(m_size[i], info.m_instanceID)
                        : info.GetLocalizedTitle();

                    if (!String.IsNullOrEmpty(filter) && name.ToLower().Contains(filter.ToLower()))
                    {
                        continue;
                    }

                    InstanceID instanceID = InstanceID.Empty;
                    instanceID.Building = m_size[i];
                    byte districtID = districtManager.GetDistrict(building.m_position);
                    byte parkID     = districtManager.GetPark(building.m_position);
                    buildings.Add(new XMLBuilding
                    {
                        instanceID = instanceID,
                        name       = name,
                        district   = parkID != 0 ? districtManager.GetParkName(parkID) : districtID == 0
                            ? Singleton <SimulationManager> .instance.m_metaData.m_CityName
                            : districtManager.GetDistrictName(districtID),
                        position = building.m_position,
                        service  = service,
                        stats    = info.m_buildingAI.GetLocalizedStats(m_size[i], ref building).Replace(Environment.NewLine, "; "),
                        status   = info.m_buildingAI.GetLocalizedStatus(m_size[i], ref building),
                        tooltip  = info.GetLocalizedTooltip(),
                        upkeep   = info.m_buildingAI.GetResourceRate(m_size[i], ref building, EconomyManager.Resource.Maintenance)
                    });
                    buildingMap.Add(m_size[i]);
                }
            }
        }
        private static bool DrawBuildingButtonOnPanel(BuildingInfo prefab, UIScrollablePanel panel)
        {
            int _UIBaseHeight = 109;

            var BuildingButton = panel.AddUIComponent <UIButton>();

            BuildingButton.size  = new Vector2(_UIBaseHeight, 100);  //apply settings to building buttons.
            BuildingButton.atlas = prefab.m_Atlas;

            BuildingButton.normalFgSprite   = prefab.m_Thumbnail;
            BuildingButton.focusedFgSprite  = prefab.m_Thumbnail + "Focused";
            BuildingButton.hoveredFgSprite  = prefab.m_Thumbnail + "Hovered";
            BuildingButton.pressedFgSprite  = prefab.m_Thumbnail + "Pressed";
            BuildingButton.disabledFgSprite = prefab.m_Thumbnail + "Disabled";

            if (prefab.m_Thumbnail == null || prefab.m_Thumbnail == "")
            {
                BuildingButton.normalFgSprite = "ToolbarIconProps";
            }

            BuildingButton.objectUserData      = prefab;
            BuildingButton.horizontalAlignment = UIHorizontalAlignment.Center;
            BuildingButton.verticalAlignment   = UIVerticalAlignment.Middle;
            BuildingButton.pivot = UIPivotPoint.TopCenter;

            //if ( Category == "education" )
            //    BuildingButton.verticalAlignment = UIVerticalAlignment.Bottom;

            string      localizedTooltip = prefab.GetLocalizedTooltip();
            int         hashCode         = TooltipHelper.GetHashCode(localizedTooltip);
            UIComponent tooltipBox       = GeneratedPanel.GetTooltipBox(hashCode);

            BuildingButton.tooltipAnchor = UITooltipAnchor.Anchored;
            BuildingButton.isEnabled     = true;
            BuildingButton.tooltip       = localizedTooltip;
            BuildingButton.tooltipBox    = tooltipBox;
            BuildingButton.eventClick   += UI.PrefabButtonClicked;
            return(true);
        }
Exemple #4
0
        void DrawBuildingButton(BuildingInfo BuildingPrefab, string type)
        {
            try
            {
                BuildingButton = new UIButton(); //draw button on appropriate panel.
                if (type == "reslow")
                {
                    BuildingButton = BuildingPanels[0].AddUIComponent <UIButton>();
                }
                if (type == "reshigh")
                {
                    BuildingButton = BuildingPanels[1].AddUIComponent <UIButton>();
                }
                if (type == "comlow")
                {
                    BuildingButton = BuildingPanels[2].AddUIComponent <UIButton>();
                }
                if (type == "comhigh")
                {
                    BuildingButton = BuildingPanels[3].AddUIComponent <UIButton>();
                }
                if (type == "office")
                {
                    BuildingButton = BuildingPanels[4].AddUIComponent <UIButton>();
                }
                if (type == "industrial")
                {
                    BuildingButton = BuildingPanels[5].AddUIComponent <UIButton>();
                }
                if (type == "farming")
                {
                    BuildingButton = BuildingPanels[6].AddUIComponent <UIButton>();
                }
                if (type == "oil")
                {
                    BuildingButton = BuildingPanels[8].AddUIComponent <UIButton>();
                }
                if (type == "forest")
                {
                    BuildingButton = BuildingPanels[7].AddUIComponent <UIButton>();
                }
                if (type == "ore")
                {
                    BuildingButton = BuildingPanels[9].AddUIComponent <UIButton>();
                }
                if (type == "leisure")
                {
                    if (Util.isADinstalled())
                    {
                        BuildingButton = BuildingPanels[10].AddUIComponent <UIButton>();
                    }
                    else
                    {
                        BuildingButton = BuildingPanels[3].AddUIComponent <UIButton>();
                    }
                }
                if (type == "tourist")
                {
                    if (Util.isADinstalled())
                    {
                        BuildingButton = BuildingPanels[11].AddUIComponent <UIButton>();
                    }
                    else
                    {
                        BuildingButton = BuildingPanels[3].AddUIComponent <UIButton>();
                    }
                }

                BuildingButton.size  = new Vector2(109, 100); //apply settings to building buttons.
                BuildingButton.atlas = BuildingPrefab.m_Atlas;



                if (BuildingPrefab.m_Thumbnail == null || BuildingPrefab.m_Thumbnail == "")
                {
                    BuildingButton.normalFgSprite = "ToolbarIconProps";
                }
                else
                {
                    BuildingButton.normalFgSprite   = BuildingPrefab.m_Thumbnail;
                    BuildingButton.focusedFgSprite  = BuildingPrefab.m_Thumbnail + "Focused";
                    BuildingButton.hoveredFgSprite  = BuildingPrefab.m_Thumbnail + "Hovered";
                    BuildingButton.pressedFgSprite  = BuildingPrefab.m_Thumbnail + "Pressed";
                    BuildingButton.disabledFgSprite = BuildingPrefab.m_Thumbnail + "Disabled";
                }

                BuildingButton.objectUserData      = BuildingPrefab;
                BuildingButton.horizontalAlignment = UIHorizontalAlignment.Center;
                BuildingButton.verticalAlignment   = UIVerticalAlignment.Middle;
                BuildingButton.pivot = UIPivotPoint.TopCenter;

                string      localizedTooltip = BuildingPrefab.GetLocalizedTooltip();
                int         hashCode         = TooltipHelper.GetHashCode(localizedTooltip);
                UIComponent tooltipBox       = GeneratedPanel.GetTooltipBox(hashCode);

                BuildingButton.tooltipAnchor    = UITooltipAnchor.Anchored;
                BuildingButton.isEnabled        = enabled;
                BuildingButton.tooltip          = localizedTooltip;
                BuildingButton.tooltipBox       = tooltipBox;
                BuildingButton.eventClick      += (sender, e) => BuildingBClicked(sender, e, BuildingPrefab);
                BuildingButton.eventMouseHover += (sender, e) => BuildingBHovered(sender, e, BuildingPrefab);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
        }
Exemple #5
0
        public override void OnLevelLoaded(LoadMode mode)
        {
            Debug.Log("IconModifier: Patching icons");
            int patchCount = 0;

            // Load the selection filter LUT texture
            var stream = (UnmanagedMemoryStream)Assembly.GetAssembly(typeof(IconModifier)).GetManifestResourceStream("AssetIcons.Assets.SelectFilter.png");
            var data   = new BinaryReader(stream).ReadBytes((int)stream.Length);

            focusedFilterTexture = new Texture2D(1024, 32);
            focusedFilterTexture.LoadImage(data);

            // Build references to the assets for thumbnails and tooltips.
            var thumbnails = new Dictionary <string, Package.Asset>();
            var tooltips   = new Dictionary <string, Package.Asset>();

            foreach (Package package in PackageManager.allPackages)
            {
                foreach (Package.Asset asset in package)
                {
                    if (asset.type == UserAssetType.CustomAssetMetaData)
                    {
                        CustomAssetMetaData md = asset.Instantiate <CustomAssetMetaData>();
                        if (md.imageRef != null && !thumbnails.ContainsKey(package.packageName))
                        {
                            md.imageRef.Cache();
                            thumbnails.Add(package.packageName, md.imageRef);
                        }
                        if (md.steamPreviewRef != null && !tooltips.ContainsKey(package.packageName))
                        {
                            md.steamPreviewRef.Cache();
                            tooltips.Add(package.packageName, md.steamPreviewRef);
                        }
                    }
                }
            }

            // Now go through all BuildingInfos and patch their icons.
            int infoCount = PrefabCollection <BuildingInfo> .LoadedCount();

            for (uint infoIndex = 0; infoIndex < infoCount; ++infoIndex)
            {
                BuildingInfo info = PrefabCollection <BuildingInfo> .GetLoaded(infoIndex);

                Texture2D thumbnailTexture = null;
                Texture2D tooltipTexture   = null;
                string    packageName      = info.name.Split('.')[0];

                // TODO: Item has a thumnail, apply color correction. Should the color corrected thumbnail be cached?
                UIButton   uiButton   = null;
                GameObject gameObject = GameObject.Find(info.name);
                if (gameObject != null)
                {
                    uiButton = gameObject.GetComponent <UIButton>();
                }
                if (!string.IsNullOrEmpty(info.m_Thumbnail))
                {
                    // This item has a thumbnail, check if it's a generated one.
                    if (uiButton != null && uiButton.atlas != null)
                    {
                        uiButton = gameObject.GetComponent <UIButton>();

                        // TODO: should this be optimized as well?
                        if (uiButton != null && uiButton.atlas != null)
                        {
                            var focusedSpriteInfo = uiButton.atlas[uiButton.focusedFgSprite];
                            if (focusedSpriteInfo != null && focusedSpriteInfo.texture != null)
                            {
                                long nonBlueCount = 0;
                                try
                                {
                                    Color32[] focusedPixels = focusedSpriteInfo.texture.GetPixels32();
                                    // The default atlas generator creates ugly dark blue focused icons
                                    // by removing everything from the red and green channel.  Detect these
                                    // by adding up the amount of red and green in the image.
                                    foreach (Color32 pixel in focusedPixels)
                                    {
                                        if (pixel.a > 32)
                                        {
                                            nonBlueCount += pixel.r + pixel.g;
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    Debug.Log(String.Format("Failed to patch texture for {0}, skipping.", info.name));
                                    continue;
                                }

                                if (nonBlueCount < 10000)
                                {
                                    // This is a generated atlas.  Replace the focused icon by generating
                                    // a new atlas.  Include the tooltip if there is one.
                                    var thumbnailSpriteInfo = uiButton.atlas[uiButton.normalFgSprite];
                                    if (thumbnailSpriteInfo != null && thumbnailSpriteInfo.texture != null)
                                    {
                                        thumbnailTexture      = thumbnailSpriteInfo.texture;
                                        thumbnailTexture.name = info.name + "Icon";
                                    }
                                    var tooltipSpriteInfo = uiButton.atlas["tooltip"];
                                    if (tooltipSpriteInfo != null && thumbnailSpriteInfo.texture != null)
                                    {
                                        tooltipTexture      = tooltipSpriteInfo.texture;
                                        tooltipTexture.name = info.name;
                                    }
                                }
                            }
                        }
                    }
                }

                // TODO: check if thumbnail is cached, if it is assign it to info.info_mThumbnail
                // Process steam workshop image if thumbnail not cached.
                // Create a thumbnail based on the steam workshop image.
                if (string.IsNullOrEmpty(info.m_Thumbnail) && thumbnails.ContainsKey(packageName))
                {
                    if (!LoadTexture(packageName, ref thumbnailTexture))
                    {
                        // TODO: what does Instantiate do?
                        thumbnailTexture = thumbnails[packageName].Instantiate <Texture2D>();
                        if (thumbnailTexture != null)
                        {
                            thumbnailTexture.wrapMode = TextureWrapMode.Clamp;
                            thumbnailTexture.name     = info.name + "Icon";
                            if (thumbnailTexture.width > thumbnailTexture.height)
                            {
                                ScaleTexture(thumbnailTexture, THUMBNAIL_SIZE, (THUMBNAIL_SIZE * thumbnailTexture.height) / thumbnailTexture.width);
                            }
                            else
                            {
                                ScaleTexture(thumbnailTexture, (THUMBNAIL_SIZE * thumbnailTexture.width) / thumbnailTexture.height, THUMBNAIL_SIZE);
                            }
                        }
                    }
                }

                // TODO: check if tooltip thumbnail is cached, if it is assign it to info.m_InfoTooltipThumbnail
                // Process steam workshop image if tooltip thumbnail not cached.
                // Create a tooltip image based on the steam workshop image.
                if (string.IsNullOrEmpty(info.m_InfoTooltipThumbnail) && tooltips.ContainsKey(packageName))
                {
                    // Note: clamp and wrap mode not set for tooltip.
                    if (!LoadTexture(packageName, ref tooltipTexture))
                    {
                        tooltipTexture = tooltips[packageName].Instantiate <Texture2D>();
                        if (tooltipTexture != null)
                        {
                            // The tooltip texture name must match the info name, as that's the key value that's used
                            // (stored on UIButton.m_Tooltip, not by us).
                            tooltipTexture.name = info.name;

                            // Crop and scale the tooltip to TOOLTIP_WIDTH x TOOLTIP_HEIGHT
                            if (((float)tooltipTexture.width / (float)tooltipTexture.height) > (float)TOOLTIP_WIDTH / (float)TOOLTIP_HEIGHT)
                            {
                                // Picture is too wide, scale to TOOLTIP_HEIGHT pixels tall and then crop out the middle
                                ScaleTexture(tooltipTexture, (TOOLTIP_HEIGHT * tooltipTexture.width) / tooltipTexture.height, TOOLTIP_HEIGHT);
                                CropTexture(tooltipTexture, (tooltipTexture.width - TOOLTIP_WIDTH) / 2, 0, TOOLTIP_WIDTH, TOOLTIP_HEIGHT);
                            }
                            else if (((float)tooltipTexture.width / (float)tooltipTexture.height) < (float)TOOLTIP_WIDTH / (float)TOOLTIP_HEIGHT)
                            {
                                // Picture is too tall, scale to TOOLTIP_WIDTH pixels wide and then crop out the middle
                                ScaleTexture(tooltipTexture, TOOLTIP_WIDTH, (TOOLTIP_WIDTH * tooltipTexture.height) / tooltipTexture.width);
                                CropTexture(tooltipTexture, 0, (tooltipTexture.height - TOOLTIP_HEIGHT) / 2, TOOLTIP_WIDTH, TOOLTIP_HEIGHT);
                            }
                            else
                            {
                                // Picture is the right aspect ratio, just scale it
                                ScaleTexture(tooltipTexture, TOOLTIP_WIDTH, TOOLTIP_HEIGHT);
                            }
                        }
                    }
                }

                // thumbnailTexture and toolTipTexture should have been fetched from cache or generated at this point.
                // No modifications needed in this method below this line.
                // Build these textures into an atlas.
                Texture2D[] textures;
                if (thumbnailTexture != null)
                {
                    if (tooltipTexture != null)
                    {
                        textures = new Texture2D[] { thumbnailTexture, null, null, null, null, tooltipTexture };
                    }
                    else
                    {
                        textures = new Texture2D[] { thumbnailTexture, null, null, null, null };
                    }
                    GenerateMissingThumbnailVariants(ref textures);
                }
                else if (tooltipTexture != null)
                {
                    textures = new Texture2D[] { tooltipTexture };
                }
                else
                {
                    // Hmm, we've failed to make any textures.  Move on to the next BuildingInfo.
                    continue;
                }


                // Following code assigns the textures to the asset. Should have them from cache at this point.

                UITextureAtlas atlas = AssetImporterThumbnails.CreateThumbnailAtlas(textures, info.name + "Atlas");

                if (thumbnailTexture != null)
                {
                    // Store the thumbnail atlas and icon names on the uiButton for this building.
                    if (uiButton != null)
                    {
                        uiButton.atlas = atlas;

                        string baseIconName = info.name + "Icon";
                        uiButton.normalFgSprite   = baseIconName;
                        uiButton.focusedFgSprite  = baseIconName + "Focused";
                        uiButton.hoveredFgSprite  = baseIconName + "Hovered";
                        uiButton.pressedFgSprite  = baseIconName + "Pressed";
                        uiButton.disabledFgSprite = baseIconName + "Disabled";
                    }

                    // Store the thumbnail atlas for this building.
                    info.m_Atlas = atlas;

                    // Store the name of the thumbnail.
                    info.m_Thumbnail = info.name + "Icon";
                }

                if (tooltipTexture != null)
                {
                    // Store the tooltip atlas for this building.
                    info.m_InfoTooltipAtlas = atlas;

                    // This is actually the transition thumbnail, we set it to the default to blank
                    // out the tooltip during transitions, which matches the behaviour of the
                    // existing icons.
                    info.m_InfoTooltipThumbnail = "";

                    if (uiButton != null)
                    {
                        uiButton.tooltip = info.GetLocalizedTooltip();
                    }
                }

                ++patchCount;
            }

            Debug.Log(String.Format("IconModifier: Complete.  Patched {0} icons.", patchCount));
        }