public float GetPopupWidth() { if (ListWidth == 0f) { var autoWidth = 0f; var pixelRatio = PixelsToUnits(); for (int i = 0; i < Popup.items.Length; i++) { using UIFontRenderer uIFontRenderer = Popup.font.ObtainRenderer(); uIFontRenderer.wordWrap = false; uIFontRenderer.pixelRatio = pixelRatio; uIFontRenderer.textScale = Popup.textScale; uIFontRenderer.characterSpacing = Popup.characterSpacing; uIFontRenderer.multiLine = false; uIFontRenderer.textAlign = UIHorizontalAlignment.Left; uIFontRenderer.processMarkup = Popup.processMarkup; uIFontRenderer.colorizeSprites = Popup.colorizeSprites; uIFontRenderer.overrideMarkupColors = false; var itemWidth = uIFontRenderer.MeasureString(Popup.items[i]); if (itemWidth.x > autoWidth) { autoWidth = itemWidth.x; } } autoWidth += Popup.listPadding.horizontal + Popup.itemPadding.horizontal; return(Mathf.Max(autoWidth, MinListWidth)); } else { return(Mathf.Max(ListWidth, MinListWidth)); } }
public UIButton AddAButton(ModRegistrationInfo mri) { UIButton retval = ScrollPanel.AddUIComponent <UIButton>(); string uniquename = string.Format("{0}_{1}", mri.ModName, mri.ButtonText); retval.name = uniquename; retval.cachedName = uniquename; retval.autoSize = false; retval.height = 50; retval.width = 50; if (mri.SpriteInfo.NormalFgSpritename == null) // no images, use a button with text in it { retval.text = mri.ButtonText; retval.textPadding = new RectOffset(2, 2, 2, 2); retval.textHorizontalAlignment = UIHorizontalAlignment.Center; retval.textScaleMode = UITextScaleMode.None; retval.textScale = 2f; retval.wordWrap = true; UIFontRenderer uifr = retval.font.ObtainRenderer(); float original_uifr_scale = 2f;// uifr.textScale; // always starts at 1 float lastuifrscale = uifr.textScale; if (uifr != null) { while (uifr != null) { Vector2 svec = uifr.MeasureString(retval.text); //Log.Message("svec: " + svec.ToString()); if (Math.Max(svec.x, svec.y) > retval.height) { lastuifrscale -= 0.05f; } else { break; } if (lastuifrscale <= 0.2f) { break; // sanity } uifr.Release(); retval.UpdateFontInfo(); uifr = retval.font.ObtainRenderer(); uifr.textScale = lastuifrscale; } uifr.textScale = original_uifr_scale; uifr.Release(); retval.UpdateFontInfo(); retval.textScale = Math.Max(0.65f, retval.textScale * (lastuifrscale / original_uifr_scale)); // .65 min is at limit of readability retval.Invalidate(); Log.Message("Resizing text scale based on string length. textScale = " + retval.textScale.ToString()); } retval.normalBgSprite = "ButtonMenu"; retval.hoveredBgSprite = "ButtonMenuHovered"; retval.pressedBgSprite = "ButtonMenuPressed"; } else { if (mri.SpriteInfo.NormalFgTexture != null) // this triggers use of all textures { string[] spritenames = { mri.SpriteInfo.NormalFgSpritename, mri.SpriteInfo.NormalBgSpritename, mri.SpriteInfo.HoveredFgSpritename, mri.SpriteInfo.HoveredBgSpritename, mri.SpriteInfo.PressedFgSpritename, mri.SpriteInfo.PressedBgSpritename }; Texture2D[] textures = { mri.SpriteInfo.NormalFgTexture, mri.SpriteInfo.NormalBgTexture, mri.SpriteInfo.HoveredFgTexture, mri.SpriteInfo.HoveredBgTexture, mri.SpriteInfo.PressedFgTexture, mri.SpriteInfo.PressedBgTexture }; retval.atlas = CreateAtlas(uniquename, spritenames, textures); } else { // built-in sprite names // - try to synthesize hover/focus/etc sprite names // - hover = name + "hovered" // - pressed = name + "pressed" string basename = mri.SpriteInfo.NormalFgSpritename; string hoversprite = basename + "Hovered"; string pressedsprite = basename + "Pressed"; if (string.IsNullOrEmpty(mri.SpriteInfo.HoveredFgSpritename)) { mri.SpriteInfo.HoveredFgSpritename = hoversprite; } if (string.IsNullOrEmpty(mri.SpriteInfo.PressedFgSpritename)) { mri.SpriteInfo.PressedFgSpritename = pressedsprite; } } if (string.IsNullOrEmpty(mri.SpriteInfo.HoveredBgSpritename)) { mri.SpriteInfo.HoveredBgSpritename = "OptionBaseHovered"; // so that everybody has some hover feedback, even if they don't specify it } retval.foregroundSpriteMode = UIForegroundSpriteMode.Scale; retval.normalFgSprite = mri.SpriteInfo.NormalFgSpritename; retval.normalBgSprite = mri.SpriteInfo.NormalBgSpritename; retval.hoveredFgSprite = mri.SpriteInfo.HoveredFgSpritename; retval.hoveredBgSprite = mri.SpriteInfo.HoveredBgSpritename; retval.pressedFgSprite = mri.SpriteInfo.PressedFgSpritename; retval.pressedBgSprite = mri.SpriteInfo.PressedBgSpritename; retval.spritePadding = new RectOffset(2, 2, 2, 2); } retval.tooltip = mri.HoverText; retval.tooltipAnchor = UITooltipAnchor.Floating; retval.eventTooltipShow += (component, param) => { param.tooltip.relativePosition = new Vector3(param.tooltip.relativePosition.x + 25, param.tooltip.relativePosition.y, param.tooltip.relativePosition.z); }; retval.canFocus = false; retval.enabled = true; retval.isInteractive = true; retval.isVisible = true; retval.eventClick += (component, param) => { try { if (mri.ClickCallback != null) { mri.ClickCallback(component.name); } } catch (Exception ex) { Log.Error(string.Format("Exception in callback to Mod: {0}. Exception: {1}", component.name, ex.Message)); } ModCorral.mcButton.SimulateClick(); }; return(retval); }
public void CreateBannerLabel(string chirpStr, uint senderID) { if (Shutdown || string.IsNullOrEmpty(chirpStr)) { return; } UILabel newLabel = (UILabel)this.AddUIComponent <UILabel>(); if (newLabel != null) { //newLabel.autoSize = true; //newLabel.autoHeight = true; newLabel.verticalAlignment = UIVerticalAlignment.Middle; newLabel.textAlignment = UIHorizontalAlignment.Left; newLabel.height = this.height; newLabel.padding = new RectOffset(0, 0, 5, 5); newLabel.relativePosition = new Vector3(this.width, 0); newLabel.textScaleMode = UITextScaleMode.ScreenResolution; newLabel.textScale = (float)ChirpyBanner.CurrentConfig.TextSize / 20f; newLabel.opacity = 1.0f; newLabel.processMarkup = true; newLabel.text = chirpStr; newLabel.objectUserData = (object)new InstanceID() { Citizen = senderID }; newLabel.eventClick += (UIComponent comp, UIMouseEventParameter p) => { if (!((UnityEngine.Object)p.source != (UnityEngine.Object)null) || !((UnityEngine.Object)ToolsModifierControl.cameraController != (UnityEngine.Object)null)) { return; } InstanceID id = (InstanceID)p.source.objectUserData; if (!InstanceManager.IsValid(id)) { return; } ToolsModifierControl.cameraController.SetTarget(id, ToolsModifierControl.cameraController.transform.position, true); }; if (OurLabelFont == null) { OurLabelFont = newLabel.font; } if (OurLabelFont != null) { //OurLabelFont.size = ChirpyBanner.CurrentConfig.TextSize; UIFontRenderer fr = OurLabelFont.ObtainRenderer(); fr.textScale = newLabel.textScale; if (fr != null) { Vector2 ms = fr.MeasureString(chirpStr); newLabel.width = ms.x; newLabel.height = ms.y; if (this.height != (newLabel.height + 0)) { this.height = newLabel.height + 0; } } } BannerLabelStruct bls = new BannerLabelStruct(); bls.RelativePosition = new Vector3(this.width, label_y_inset); // starting position is off screen, at max extent of parent panel bls.Label = newLabel; bls.IsCurrentlyVisible = false; // add to queue BannerLabelsQ.Enqueue(bls); } }
private void UpdateNames() { UIFontManager.Invalidate(this.m_properties.m_areaNameFont); namesModifiedField.SetValue(this, false); UIRenderData destination = UIRenderData.Obtain(); UIRenderData uiRenderData = UIRenderData.Obtain(); try { destination.Clear(); uiRenderData.Clear(); PoolList <Vector3> vertices1 = uiRenderData.vertices; PoolList <Vector3> normals1 = uiRenderData.normals; PoolList <Color32> colors1 = uiRenderData.colors; PoolList <Vector2> uvs1 = uiRenderData.uvs; PoolList <int> triangles1 = uiRenderData.triangles; PoolList <Vector3> vertices2 = destination.vertices; PoolList <Vector3> normals2 = destination.normals; PoolList <Color32> colors2 = destination.colors; PoolList <Vector2> uvs2 = destination.uvs; PoolList <int> triangles2 = destination.triangles; for (int district = 1; district < 128; ++district) { if (this.m_districts.m_buffer[district].m_flags != District.Flags.None) { string text = this.GetDistrictName(district) + "\n"; PositionData <DistrictPolicies.Policies>[] orderedEnumData = Utils.GetOrderedEnumData <DistrictPolicies.Policies>(); for (int index = 0; index < orderedEnumData.Length; ++index) { if (this.IsDistrictPolicySet(orderedEnumData[index].enumValue, (byte)district)) { string str = "IconPolicy" + orderedEnumData[index].enumName; text = text + "<sprite " + str + "> "; } } if (text != null) { int count1 = normals2.Count; int count2 = normals1.Count; using (UIFontRenderer renderer = this.m_properties.m_areaNameFont.ObtainRenderer()) { UIDynamicFont.DynamicFontRenderer dynamicFontRenderer = renderer as UIDynamicFont.DynamicFontRenderer; if (dynamicFontRenderer != null) { dynamicFontRenderer.spriteAtlas = this.m_properties.m_areaIconAtlas; dynamicFontRenderer.spriteBuffer = uiRenderData; } float x1 = 450f; renderer.defaultColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)64); renderer.textScale = 2f; renderer.pixelRatio = 1f; renderer.processMarkup = true; renderer.multiLine = true; renderer.wordWrap = true; renderer.textAlign = UIHorizontalAlignment.Center; renderer.maxSize = new Vector2(x1, 900f); renderer.shadow = false; renderer.shadowColor = (Color32)Color.black; renderer.shadowOffset = Vector2.one; Vector2 vector2 = renderer.MeasureString(text); float x2 = vector2.x; if ((double)vector2.x > (double)x1) { x2 = x1 + (float)(((double)vector2.x - (double)x1) * 0.5); x1 = vector2.x; renderer.maxSize = new Vector2(x1, 900f); vector2 = renderer.MeasureString(text); } this.m_districts.m_buffer[district].m_nameSize = vector2; vertices2.Add(new Vector3(-x2, -vector2.y, 1f)); vertices2.Add(new Vector3(-x2, vector2.y, 1f)); vertices2.Add(new Vector3(x2, vector2.y, 1f)); vertices2.Add(new Vector3(x2, -vector2.y, 1f)); colors2.Add(new Color32((byte)0, (byte)0, (byte)0, byte.MaxValue)); colors2.Add(new Color32((byte)0, (byte)0, (byte)0, byte.MaxValue)); colors2.Add(new Color32((byte)0, (byte)0, (byte)0, byte.MaxValue)); colors2.Add(new Color32((byte)0, (byte)0, (byte)0, byte.MaxValue)); uvs2.Add(new Vector2(-1f, -1f)); uvs2.Add(new Vector2(-1f, 1f)); uvs2.Add(new Vector2(1f, 1f)); uvs2.Add(new Vector2(1f, -1f)); triangles2.Add(vertices2.Count - 4); triangles2.Add(vertices2.Count - 3); triangles2.Add(vertices2.Count - 1); triangles2.Add(vertices2.Count - 1); triangles2.Add(vertices2.Count - 3); triangles2.Add(vertices2.Count - 2); renderer.vectorOffset = new Vector3(x1 * -0.5f, vector2.y * 0.5f, 0.0f); renderer.Render(text, destination); } int count3 = vertices2.Count; int count4 = normals2.Count; Vector3 vector3 = this.m_districts.m_buffer[district].m_nameLocation; for (int index = count1; index < count4; ++index) { normals2[index] = vector3; } for (int index = count4; index < count3; ++index) { normals2.Add(vector3); } int count5 = vertices1.Count; int count6 = normals1.Count; for (int index = count2; index < count6; ++index) { normals1[index] = vector3; } for (int index = count6; index < count5; ++index) { normals1.Add(vector3); } } } } if ((Mesh)nameMeshField.GetValue(this) == null) { nameMeshField.SetValue(this, new Mesh()); } var nameMesh = (Mesh)nameMeshField.GetValue(this); nameMesh.Clear(); nameMesh.vertices = vertices2.ToArray(); nameMesh.normals = normals2.ToArray(); nameMesh.colors32 = colors2.ToArray(); nameMesh.uv = uvs2.ToArray(); nameMesh.triangles = triangles2.ToArray(); //begin mod nameMesh.bounds = new Bounds(Vector3.zero, new Vector3(GRID * 19.2f, 1024f, GRID * 19.2f)); //end mod if ((Mesh)iconMeshField.GetValue(this) == null) { iconMeshField.SetValue(this, new Mesh()); } var iconMesh = (Mesh)iconMeshField.GetValue(this); iconMesh.Clear(); iconMesh.vertices = vertices1.ToArray(); iconMesh.normals = normals1.ToArray(); iconMesh.colors32 = colors1.ToArray(); iconMesh.uv = uvs1.ToArray(); iconMesh.triangles = triangles1.ToArray(); //begin mod iconMesh.bounds = new Bounds(Vector3.zero, new Vector3(GRID * 19.2f, 1024f, GRID * 19.2f)); //end mod } finally { destination.Release(); uiRenderData.Release(); } }