/// <summary> /// Calculates text height by ignoring bold style of the font. This will not work with inline bold tags. /// NOTE: This will not fix occasional new line when text ends right before end. /// </summary> /// <param name="content">Text content.</param> /// <param name="style">Text style.</param> /// <param name="width">Fixed width of text container.</param> /// <returns>Returns calculated height of the text.</returns> public static float CalculateHeight(GUIContent content, GUIStyle style, float width) { float height; // Bold fonts have much higher chance of having one new line to many than normal font. // There were no issues with missing new lines even with couple of extreme cases. (but extra new lines can occur) if (style.fontStyle == FontStyle.Bold) { style.fontStyle = FontStyle.Normal; style.wordWrap = true; style.fixedWidth = width; style.fixedHeight = 0; Texture2D t = new Texture2D(1,1); content.image = t; style.imagePosition = ImagePosition.ImageLeft; float min, max; style.CalcMinMaxWidth(content, out min, out max); style.clipping = TextClipping.Overflow; height = style.CalcHeight(content, min); style.fontStyle = FontStyle.Bold; } else { height = style.CalcHeight(content, width); } return height; }
public void CalcGUIContentSize(GUIContent content, GUIStyle style, out float width, out float height) { float minWidth; float maxWidth; //GetPadding(); style.CalcMinMaxWidth(content, out minWidth, out maxWidth); float threshold = 250; if (maxWidth < threshold) { style.wordWrap = false; Vector2 size = style.CalcSize(content); style.wordWrap = true; maxWidth = size.x; } width = Mathf.Clamp(maxWidth, 0, threshold); height = Mathf.Clamp(style.CalcHeight(content, width), 21, 150); //Debug.LogWarning(string.Format("min: {0}, max: {1} => w: {2}, isHeightDependentonwidht: {3}", minWidth, maxWidth, width, style)); //SetPadding(l, t, r, b); }
/// <summary> /// Creates an instance of ExtendedNotification /// </summary> /// <param name="text">The text to display on the notification</param> /// <param name="color">The color of the notification</param> /// <param name="duration">The duration of the notification</param> /// <param name="style">The style of the notification</param> public ExtendedNotification( string text, Color color, float duration, GUIStyle style ) { Text = new GUIContent( text ); Color = color; Duration = duration; style.CalcMinMaxWidth( Text, out Size.y, out Size.x ); Size.y = style.CalcHeight( Text, Size.x ); }
static public int CalcMinMaxWidth(IntPtr l) { try { UnityEngine.GUIStyle self = (UnityEngine.GUIStyle)checkSelf(l); UnityEngine.GUIContent a1; checkType(l, 2, out a1); System.Single a2; System.Single a3; self.CalcMinMaxWidth(a1, out a2, out a3); pushValue(l, true); pushValue(l, a2); pushValue(l, a3); return(3); } catch (Exception e) { return(error(l, e)); } }
static int CalcMinMaxWidth(IntPtr L) { try { ToLua.CheckArgsCount(L, 4); UnityEngine.GUIStyle obj = (UnityEngine.GUIStyle)ToLua.CheckObject(L, 1, typeof(UnityEngine.GUIStyle)); UnityEngine.GUIContent arg0 = (UnityEngine.GUIContent)ToLua.CheckObject(L, 2, typeof(UnityEngine.GUIContent)); float arg1; float arg2; obj.CalcMinMaxWidth(arg0, out arg1, out arg2); LuaDLL.lua_pushnumber(L, arg1); LuaDLL.lua_pushnumber(L, arg2); return(2); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
void DrawHoverInfo(){ UnitTB selectedUnit=UnitControl.selectedUnit; #if hoverHP if(tileHovered.unit != null){ //draw hp and AP for unit UnitTB hUnit=tileHovered.unit; Camera camA=CameraControl.GetActiveCamera(); Vector3 screenPosA = camA.WorldToScreenPoint(hUnit.thisT.position); screenPosA.y=Screen.height-screenPosA.y; int startPosX=(int)(screenPosA.x-40/2+7); int startPosY=(int)screenPosA.y+5; styleA.fontSize=20; styleA.normal.textColor=UI.colorH; styleA.alignment=TextAnchor.UpperCenter; if(hUnit.factionID == 1){ GUI.Box(new Rect(startPosX-x_offset-25,startPosY+y_offset-20,140,180),""); GUI.Label(new Rect(startPosX-x_offset-25,startPosY+y_offset-20,140,140),"\n" + hUnit.unitName+ "\nHP:"+hUnit.HP+"/" +hUnit.GetFullHP()+"\n AP:" + hUnit.AP + "/"+hUnit.GetFullAP() + "\n Moves:"+hUnit.moveRemain + "\n Attacks:"+hUnit.attackRemain,styleA); } else{ GUI.Box(new Rect(startPosX-x_offset-25,startPosY+y_offset,140,140),""); GUI.Label(new Rect(startPosX-x_offset-25,startPosY+y_offset,140,140),"\n HP:"+hUnit.HP+"/" +hUnit.GetFullHP()+"\n AP:" + hUnit.AP + "/"+hUnit.GetFullAP() + "\n Moves:"+hUnit.moveRemain + "\n Attacks:"+hUnit.attackRemain,styleA); } } #endif if(tileHovered.attackableToSelected){ UnitTB unit=tileHovered.unit; int dmgMin=0, dmgMax=0; string hit="", crit=""; int dist=GridManager.Distance(tileHovered, selectedUnit.occupiedTile); int armorType=unit.armorType; int damageType=selectedUnit.damageType; float dmgModifier=DamageTable.GetModifier(armorType, damageType); if(dist==1 && selectedUnit.attackMode!=_AttackMode.Range){ dmgMin=(int)((float)selectedUnit.GetMeleeDamageMin()*dmgModifier); dmgMax=(int)((float)selectedUnit.GetMeleeDamageMax()*dmgModifier); hit=(selectedUnit.GetTotalHitChanceMelee(unit)*100).ToString("f0")+"%"; crit=(selectedUnit.GetTotalCritChanceMelee(unit)*100).ToString("f0")+"%"; } else{ dmgMin=(int)((float)selectedUnit.GetRangeDamageMin()*dmgModifier); dmgMax=(int)((float)selectedUnit.GetRangeDamageMax()*dmgModifier); hit=(selectedUnit.GetTotalHitChanceRange(unit)*100).ToString("f0")+"%"; crit=(selectedUnit.GetTotalCritChanceRange(unit)*100).ToString("f0")+"%"; } string text=""; text+=hit+"chance to hit\n"; text+="Damage: "+dmgMin+"-"+dmgMax+"\n\n"; if(crit!="0%") text+="Critical Chance: "+crit; bool counter=false; if(GameControlTB.IsCounterAttackEnabled()){ if(dist>=unit.GetAttackRangeMin() && dist<=unit.GetAttackRangeMax() && unit.counterAttackRemain>0){ counter=true; } } int cost=0; if(GameControlTB.AttackAPCostRule()==_AttackAPCostRule.PerAttack){ cost=selectedUnit.APCostAttack; } GUIStyle style=new GUIStyle(); style.fontStyle=FontStyle.Bold; #if mousePos int width=500; int w_offset =50; int height=160; int h_offset = 20; //get pos X and Y once every second to prevent flicker getMousePos(); /*for(int i=0; i<3; i++) GUI.Box(new Rect(posX-(width+w_offset)/2, posY-230, width+w_offset, height), ""); style.fontSize=20; style.normal.textColor=UI.colorH; style.alignment=TextAnchor.UpperCenter; GUI.Label(new Rect(posX-width/2, posY-240, width, height), ability.name, style); style.fontSize=16; style.normal.textColor=UI.colorH; style.alignment=TextAnchor.UpperRight; GUI.Label(new Rect(posX-width/2-5, posY-240, width, height), ability.cost+"AP", style); style.fontSize=16; style.normal.textColor=UI.colorN; style.alignment=TextAnchor.UpperCenter; style.wordWrap=true; GUI.Label(new Rect(posX-width/2, posY-190, width, height), ability.desp, style); GUI.color=Color.white;*/ //for(int i=0; i<3; i++) GUI.Box(new Rect(posX-(width +w_offset)/2, posY, width+w_offset, height), ""); GUI.Box(new Rect(posX-(width/2)/2, Screen.height-posY+40, width/2, height-20), ""); // to remove flicker comment out this line style.fontSize=20; style.normal.textColor=UI.colorH; style.alignment=TextAnchor.UpperCenter; GUI.Label(new Rect(posX-(width)/2, Screen.height-posY+40+h_offset, width, height), "Attack", style); if(cost>0){ style.fontSize=16; style.normal.textColor=UI.colorH; style.alignment=TextAnchor.UpperRight; GUI.Label(new Rect(posX-width/2-5, Screen.height-posY+50+h_offset, width, height), cost+"AP", style); } //reposition to be at location of mouse style.fontSize=16; style.normal.textColor=UI.colorN; style.alignment=TextAnchor.UpperCenter; style.wordWrap=true; GUI.Label(new Rect(posX-width/2, Screen.height-posY+60+h_offset, width, height), text, style); if(counter){ style.fontSize=14; style.normal.textColor=UI.colorH; style.wordWrap=false; GUI.Label(new Rect(posX-width/2, Screen.height-posY+40+h_offset, width, height), "Target will counter attack", style); } #else int width=500; int height=160; for(int i=0; i<3; i++) GUI.Box(new Rect(Screen.width/2-width/2, Screen.height-230, width, height), ""); style.fontSize=20; style.normal.textColor=UI.colorH; style.alignment=TextAnchor.UpperCenter; GUI.Label(new Rect(Screen.width/2-width/2, Screen.height-240, width, height), "Attack", style); if(cost>0){ style.fontSize=16; style.normal.textColor=UI.colorH; style.alignment=TextAnchor.UpperRight; GUI.Label(new Rect(Screen.width/2-width/2-5, Screen.height-220, width, height), cost+"AP", style); } //reposition to be at location of mouse style.fontSize=16; style.normal.textColor=UI.colorN; style.alignment=TextAnchor.UpperCenter; style.wordWrap=true; GUI.Label(new Rect(Screen.width/2-width/2, Screen.height-190, width, height), text, style); if(counter){ style.fontSize=14; style.normal.textColor=UI.colorH; style.wordWrap=false; GUI.Label(new Rect(Screen.width/2-width/2, Screen.height-120, width, height), "Target will counter attack", style); } #endif GUI.color=Color.white; } else{ if(tileHovered.walkableToSelected && selectedUnit!=null){ //Vector3 screenPos = cam.WorldToScreenPoint(tileHovered.pos); //hoverObject.transform.localPosition=screenPos+new Vector3(-40, 40, 0); List<Vector3> list=AStar.SearchWalkablePos(tileHovered, selectedUnit.occupiedTile); int dist=list.Count; string text="Move: "+(dist*selectedUnit.APCostMove)+"AP"; //string text="Move: "+Random.Range(0, 9999)+"AP"; GUIContent cont=new GUIContent(text); GUI.color=Color.white; GUIStyle style=new GUIStyle(); style.fontStyle=FontStyle.Bold; style.fontSize=16; style.normal.textColor=UI.colorN; style.alignment=TextAnchor.LowerRight; Camera cam=CameraControl.GetActiveCamera(); Vector3 screenPos = cam.WorldToScreenPoint(tileHovered.pos); screenPos.y=Screen.height-screenPos.y; float widthMin=0; float widthMax=0; style.CalcMinMaxWidth(cont, out widthMin, out widthMax); #if ibox #else GUI.Box(new Rect(screenPos.x-widthMax-50, screenPos.y-50, widthMax+25, 22), ""); GUI.Label(new Rect(screenPos.x-widthMax-50, screenPos.y-50, widthMax+25-4, 20), text, style); #endif } } }
private Boolean DrawAlarmLine(KACAlarm tmpAlarm) { Boolean blnReturn = false; GUILayout.BeginHorizontal(); switch (tmpAlarm.TypeOfAlarm) { case KACAlarm.AlarmType.Raw: GUILayout.Label(KACResources.iconRaw, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.Maneuver: case KACAlarm.AlarmType.ManeuverAuto: GUILayout.Label(KACResources.iconMNode, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.SOIChange: case KACAlarm.AlarmType.SOIChangeAuto: GUILayout.Label(KACResources.iconSOI, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.Transfer: case KACAlarm.AlarmType.TransferModelled: GUILayout.Label(KACResources.iconXFer, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.Apoapsis: GUILayout.Label(KACResources.iconAp, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.Periapsis: GUILayout.Label(KACResources.iconPe, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.AscendingNode: GUILayout.Label(KACResources.iconAN, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.DescendingNode: GUILayout.Label(KACResources.iconDN, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.LaunchRendevous: GUILayout.Label(KACResources.iconLaunchRendezvous, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.Closest: GUILayout.Label(KACResources.iconClosest, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.Distance: //TODO - SOMETHING HERE MAYBE GUILayout.Label(KACResources.iconClosest, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.Crew: GUILayout.Label(KACResources.iconCrew, KACResources.styleAlarmIcon); break; case KACAlarm.AlarmType.EarthTime: GUILayout.Label(KACResources.iconEarth, KACResources.styleAlarmIcon); break; default: GUILayout.Label(KACResources.iconNone, KACResources.styleAlarmIcon); break; } //Set the Content up //int intMaxWidth = intTestheight; //String strTimeToAlarm = String.Format(" ({0})",KerbalTime.PrintInterval(tmpAlarm.Remaining, Settings.TimeFormat)); //float fTimeToAlarmWidth; //KACResources.styleAlarmText.CalcMinMaxWidth(new GUIContent(strTimeToAlarm),out fOutMin1,out fOutMax1); //fTimeToAlarmWidth = fOutMax1; //String strTextToDisplay = tmpAlarm.Name; //KACResources.styleAlarmText.CalcMinMaxWidth(new GUIContent(strTextToDisplay), out fOutMin, out fOutMax); //while (strTextToDisplay.Length>10 &&( fOutMax+fTimeToAlarmWidth>intMaxWidth) ) //{ // strTextToDisplay = strTextToDisplay.Remove(strTextToDisplay.Length - 2); // KACResources.styleAlarmText.CalcMinMaxWidth(new GUIContent(strTextToDisplay), out fOutMin, out fOutMax); //} ////String strLabelText = strTextToDisplay + strTimeToAlarm; //String strTimeText = String.Format("({0})", KerbalTime.PrintInterval(tmpAlarm.Remaining, Settings.TimeFormat)); //String strLabelText = tmpAlarm.Name; //GUIStyle styleLabel = new GUIStyle(KACResources.styleAlarmText); //if ((!tmpAlarm.Enabled || tmpAlarm.Actioned)) // styleLabel.normal.textColor=Color.gray; //GUIStyle styleTime = new GUIStyle(styleLabel); //styleTime.stretchWidth = true; //GUIContent contAlarmLabel = new GUIContent(strLabelText, tmpAlarm.Notes); //GUIContent contAlarmTime = new GUIContent(strTimeText, tmpAlarm.Notes); ////calc correct width for first part //KACResources.styleAlarmText.CalcMinMaxWidth(contAlarmTime, out fOutMin, out fOutMax); //styleLabel.CalcMinMaxWidth(contAlarmLabel, out fOutMin1, out fOutMax1); //int intMaxWidth = intTestheight; //if (fOutMax1 + fOutMax > intMaxWidth) // fOutMax1 = intMaxWidth - fOutMax; //if ((alarmEdit == tmpAlarm) && _ShowEditPane) //{ // intMaxWidth -= 20; //} //float width1 = fOutMin1; String strLabelText = ""; strLabelText = String.Format("{0} ({1})", tmpAlarm.Name, KACTime.PrintInterval(tmpAlarm.Remaining, Settings.TimeFormat)); GUIStyle styleLabel = new GUIStyle( KACResources.styleAlarmText); if ((!tmpAlarm.Enabled || tmpAlarm.Actioned)) styleLabel.normal.textColor=Color.gray; GUIContent contAlarmLabel = new GUIContent(strLabelText, tmpAlarm.Notes); //Calc the line height //////////////////////////////////////////////////// //Currently the max width doesnt work out when an alarm is in edit mode correctly in edit mode //Also need to test max list length and when the scrollbar kicks in /////////////////////////////////////////////////// Single sOutMin1,sOutMax1; styleLabel.CalcMinMaxWidth(contAlarmLabel,out sOutMin1, out sOutMax1); tmpAlarm.AlarmLineWidth = Convert.ToInt32(sOutMax1); Int32 intMaxwidth = 220;// 228; if (_ShowEditPane && (alarmEdit == tmpAlarm)) intMaxwidth = 198;// 216; tmpAlarm.AlarmLineHeight = Convert.ToInt32(styleLabel.CalcHeight(contAlarmLabel, intMaxwidth)); //Draw a button that looks like a label. if (GUILayout.Button(contAlarmLabel, styleLabel, GUILayout.MaxWidth(218))) { if (!_ShowSettings) { if (alarmEdit == tmpAlarm) { //If there was an alarm open, then save em again if (_ShowEditPane) Settings.Save(); _ShowEditPane = !_ShowEditPane; } else { //If there was an alarm open, then save em again if (_ShowEditPane) Settings.Save(); alarmEdit = tmpAlarm; _ShowEditPane = true; _ShowSettings = false; _ShowAddPane = false; } } } if ((alarmEdit == tmpAlarm) && _ShowEditPane) { GUILayout.Label(new GUIContent(KACResources.iconEdit, "Editing..."), KACResources.styleLabelWarp); } if (tmpAlarm.PauseGame) { GUILayout.Label(new GUIContent(KACResources.GetPauseListIcon(tmpAlarm.WarpInfluence),"Pause"), KACResources.styleLabelWarp); } else if (tmpAlarm.HaltWarp) { GUILayout.Label(new GUIContent(KACResources.GetWarpListIcon(tmpAlarm.WarpInfluence), "Kill Warp"), KACResources.styleLabelWarp); } else { GUILayout.Label(new GUIContent(KACResources.iconNone), KACResources.styleLabelWarp); } if (GUILayout.Button(new GUIContent(KACResources.btnRedCross,"Delete Alarm"), GUI.skin.button, GUILayout.MaxWidth(20), GUILayout.MaxHeight(20))) blnReturn = true; GUILayout.EndHorizontal(); return blnReturn; }
private void ShowScore(GameObject obj, Transform offsetX, GUIStyle style, string score) { Vector3 rawPosition = obj.transform.position; rawPosition.x = offsetX.position.x; Vector3 start = Camera.main.WorldToScreenPoint(rawPosition); start.y = Screen.height - start.y; float minWidth, maxWidth; string text = TextFormatting.AddSpacingBetweenCharacters(score); GUIContent content = new GUIContent(text); style.CalcMinMaxWidth(content, out minWidth, out maxWidth); float height = style.CalcHeight(content, maxWidth); height /= 2.0f; float halfWidth = maxWidth / 2.0f; GUI.Label(new Rect(start.x - halfWidth - 3, start.y + 2 - height, maxWidth, 120), FontColorUtils.Shadow(text), style); GUI.Label(new Rect(start.x - halfWidth, start.y - height, maxWidth, 120), FontColorUtils.MainGameScore(text), style); }
public static bool List(Rect position, ref bool showList, ref int listEntry, GUIContent buttonContent, GUIContent[] listContent, GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle) { int controlID = GUIUtility.GetControlID(popupListHash, FocusType.Passive); bool done = false; switch (Event.current.GetTypeForControl(controlID)) { case EventType.mouseDown: if (position.Contains(Event.current.mousePosition)) { GUIUtility.hotControl = controlID; showList = true; } break; case EventType.mouseUp: if (showList) { done = true; } break; } GUI.Label(position, buttonContent, buttonStyle); if (showList) { float width = position.width, minWidth = 0f, maxWidth = 0f; foreach (var v in listContent) { try { listStyle.CalcMinMaxWidth(v, out minWidth, out maxWidth); if (width < maxWidth) { width = maxWidth; } } catch {} } Rect listRect = new Rect(position.x, position.y, width, listStyle.CalcHeight(listContent[0], 1.0f)*listContent.Length); GUI.Box(listRect, "", boxStyle); listEntry = GUI.SelectionGrid(listRect, listEntry, listContent, 1, listStyle); } if (done) { showList = false; } return done; }
public static void Hyperlink(string Label, string Url, GUIStyle gsBase) { if (GUILayout.Button(Label, gsBase)) Application.OpenURL(Url); Rect r = GUILayoutUtility.GetLastRect(); float mw, Mw; float h = gsBase.CalcHeight(new GUIContent(Label), r.width) - gsBase.padding.top - gsBase.padding.bottom; gsBase.CalcMinMaxWidth(new GUIContent(Label), out mw, out Mw); //Vector2 v = gsBase.CalcSize(new GUIContent(Label)); Rect nr = new Rect( r.x + gsBase.padding.left, r.y + gsBase.padding.top + h, Mw - gsBase.padding.left - gsBase.padding.right, 1); Color oldC = GUI.color; GUI.color = gsBase.normal.textColor; GUI.DrawTexture(nr, wHiteTex); GUI.color = oldC; #if UNITY_EDITOR UnityEditor.EditorGUIUtility.AddCursorRect(r, UnityEditor.MouseCursor.Link); #endif }