private UIElement SetUpMenu() { if (CircleMarker == null) { CircleMarker = PyDraw.getCircle(363, Color.White); } if (menuFont1 != "" && menuFont1 != null) { UIFontRenderer.LoadFont(Helper, Path.Combine("fonts", menuFont1 + ".fnt"), menuFont1); } if (menuFont2 != "" && menuFont2 != null && menuFont2 != menuFont1) { UIFontRenderer.LoadFont(Helper, Path.Combine("fonts", menuFont2 + ".fnt"), menuFont2); } UIElement menu = UIElement.GetContainer("WarpMenu", 0, UIHelper.GetCentered(0, 0, menuWidth, menuHeight), 1f); UIElement back = UIElement.GetImage(UIHelper.DarkTheme, Color.White * 0.9f, "Back", 1, -1).AsTiledBox(16, true); menu.Add(back); AddLeftMenu(menu); AddInfoMenu(menu); return(menu); }
public static bool Truncate(UILabel label, string text, string suffix = "…") { bool flag = false; try { using (UIFontRenderer renderer = label.ObtainRenderer()) { float units = label.GetUIView().PixelsToUnits(); float[] characterWidths = renderer.GetCharacterWidths(text); float num1 = 0.0f; float num2 = (float)((double)label.width - (double)label.padding.horizontal - 2.0); for (int index = 0; index < characterWidths.Length; ++index) { num1 += characterWidths[index] / units; if ((double)num1 > (double)num2) { flag = true; text = text.Substring(0, index - 3) + suffix; break; } } } label.text = text; } catch { flag = false; } return(flag); }
public static void ApplyText(UICheckboxDropDown dd, string text) { UIButton uibutton = (UIButton)dd.triggerButton; var padding = uibutton.textPadding; padding.left = 5; padding.right = 21; uibutton.text = text; // must set text to mearure text once and only once. using (UIFontRenderer uifontRenderer = ObtainTextRenderer(uibutton)) { float p2uRatio = uibutton.GetUIView().PixelsToUnits(); var widths = uifontRenderer.GetCharacterWidths(text); float x = widths.Sum() / p2uRatio; //Log.Debug($"{uifontRenderer}.GetCharacterWidths(\"{text}\")->{widths.ToSTR()}"); //if (x > uibutton.width - 42) // uibutton.textHorizontalAlignment = UIHorizontalAlignment.Left; //else // uibutton.textHorizontalAlignment = UIHorizontalAlignment.Center; if (x > uibutton.width - uibutton.textPadding.horizontal) { for (int n = 4; n < text.Length; ++n) { float x2 = widths.Take(n).Sum() / p2uRatio + 15; // 15 = width of ... if (x2 > uibutton.width - 21) { text = text.Substring(0, n - 1) + "..."; break; } } } } uibutton.text = text; }
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)); } }
bool GenerateMesh() { UIFont uifont = LoadingScreen.instance.uifont; if (uifont == null) { return(false); } long t = Profiling.Ticks; UIFontRenderer uiFontRenderer = uifont.ObtainRenderer(); UIRenderData uiRenderData = UIRenderData.Obtain(); try { mesh.Clear(); uiFontRenderer.defaultColor = Color.white; uiFontRenderer.textScale = 1f; uiFontRenderer.pixelRatio = 1f; uiFontRenderer.processMarkup = true; uiFontRenderer.multiLine = true; uiFontRenderer.maxSize = new Vector2(LoadingScreen.instance.meshWidth, LoadingScreen.instance.meshHeight); uiFontRenderer.shadow = false; uiFontRenderer.Render(text, uiRenderData); mesh.vertices = uiRenderData.vertices.ToArray(); mesh.colors32 = uiRenderData.colors.ToArray(); mesh.uv = uiRenderData.uvs.ToArray(); mesh.triangles = uiRenderData.triangles.ToArray(); } catch (Exception e) { Util.DebugPrint("Cannot generate font mesh"); UnityEngine.Debug.LogException(e); return(false); } finally { uiFontRenderer.Dispose(); uiRenderData.Dispose(); } return(true); }
public static void FitString(this UILabel label) { using (UIFontRenderer fontRenderer = label.ObtainRenderer()) { float p2u = label.GetUIView().PixelsToUnits(); var characterWidths = fontRenderer.GetCharacterWidths(label.text); float totalSize = label.padding.left + label.padding.right; for (var i = 0; i < characterWidths.Length; i++) { totalSize += characterWidths[i] / p2u; if (!(totalSize > label.width)) { continue; } label.tooltip = label.text; label.text = string.Concat(label.text.Substring(0, i - 1), "..."); break; } } }
private static void Postfix( UIGraph __instance, List <CurveSettings> ___m_Curves, Rect ___m_GraphRect, PoolList <UIRenderData> ___m_RenderData, PoolList <Vector3> vertices, PoolList <int> indices, PoolList <Vector2> uvs, PoolList <Color32> colors) { // Note: this method is extracted from the original game and is slightly modified if (___m_Curves.Count == 0) { return; } float units = pixelsToUnits(__instance); var maxSize = new Vector2(__instance.size.x, __instance.size.y); Vector3 center = __instance.pivot.TransformToCenter(__instance.size, __instance.arbitraryPivotOffset) * units; Vector3 size = units * __instance.size; float aspectRatio = __instance.size.x / __instance.size.y; using (UIFontRenderer uiFontRenderer = __instance.font.ObtainRenderer()) { float xmin = units * __instance.width * (-0.5f + ___m_GraphRect.xMin); float xmax = units * __instance.width * (-0.5f + ___m_GraphRect.xMax); float minInterval = GridIntervalX * units; float startTicks = __instance.StartTime.Ticks; DateTime visibleEndTime = GetVisibleEndTime(___m_Curves, __instance.StartTime, __instance.EndTime); float endTicks = visibleEndTime.Ticks; float minRangeTicks = __instance.StartTime.AddDays(MinRangeInDays).Ticks - __instance.StartTime.Ticks; float rangeTicks = __instance.EndTime.Ticks - __instance.StartTime.Ticks; float timeScale = minRangeTicks / rangeTicks; float scaledX = Mathf.Lerp(xmin, xmax, timeScale) - xmin; TimeSpan step = scaledX <= 0.0001f ? TimeSpan.FromDays(10000) : TimeSpan.FromDays(minInterval / scaledX); UIRenderData textRenderData = ___m_RenderData.Count > 1 ? ___m_RenderData[1] : null; for (DateTime current = __instance.StartTime.AddDays(1); current <= visibleEndTime; current += step) { float currentTicks = current.Ticks; float x = Mathf.Lerp(xmin, xmax, (currentTicks - startTicks) / (endTicks - startTicks)); string text = current.ToString("M", currentCulture); uiFontRenderer.textScale = 1f; uiFontRenderer.vectorOffset = new Vector3(x, __instance.height * units * -0.95f, 0f); uiFontRenderer.pixelRatio = units; uiFontRenderer.maxSize = maxSize; uiFontRenderer.textAlign = UIHorizontalAlignment.Center; uiFontRenderer.defaultColor = __instance.TextColor; uiFontRenderer.Render(text, textRenderData); float val = Mathf.Lerp(-0.5f + ___m_GraphRect.xMin, -0.5f + ___m_GraphRect.xMax, (currentTicks - startTicks) / (endTicks - startTicks)); var corner1 = new Vector2(val - (units * __instance.HelpAxesWidth * aspectRatio), -0.5f + ___m_GraphRect.yMin); var corner2 = new Vector2(val + (units * __instance.HelpAxesWidth * aspectRatio), corner1.y + ___m_GraphRect.height); addSolidQuad( __instance, Vector3.Scale(corner1, size) + center, Vector3.Scale(corner2, size) + center, __instance.HelpAxesColor, vertices, indices, uvs, colors); } } }
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 static Color32 ApplyOpacity(UIFontRenderer renderer, Color32 color) { color.a = (byte)(renderer.opacity * 255f); return(color); }
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); }
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(); } }