Skip to content

Commit

Permalink
Dialogboxfix clean (#12)
Browse files Browse the repository at this point in the history
*  #553 Fix issue with text box height
GameObject needs to be new for Unity to provide a proper height.
Apply to width too just in case.
  • Loading branch information
BenRQ committed Oct 7, 2018
1 parent bc1fe68 commit b92d0a7
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 42 deletions.
2 changes: 1 addition & 1 deletion unity/Assets/Scripts/Content/CommonStringKeys.cs
Expand Up @@ -23,7 +23,7 @@ public class CommonStringKeys
public static readonly StringKey PLUS = new StringKey(null,"+", false);
public static readonly StringKey MINUS = new StringKey(null,"-", false);
public static readonly StringKey HASH = new StringKey(null,"#", false);
public static readonly StringKey TAB = new StringKey(null,"->",false);
public static readonly StringKey TAB = new StringKey(null, "", false);
public static readonly StringKey EVENT = new StringKey(VAL, "EVENT");
public static readonly StringKey OK = new StringKey(VAL, "OK");
public static readonly StringKey CANCEL = new StringKey(VAL, "CANCEL");
Expand Down
12 changes: 5 additions & 7 deletions unity/Assets/Scripts/Quest/DialogWindow.cs
Expand Up @@ -64,13 +64,12 @@ public DialogWindow(EventManager.Event e)
public void CreateWindow()
{
// Draw text
float offset = UIElement.GetStringHeight(text, 28);
UIElement ui = new UIElement();
float offset = ui.GetStringHeight(text, 28);
if (offset < 4)
{
offset = 4;
}

UIElement ui = new UIElement();
ui.SetLocation(UIScaler.GetHCenter(-14f), 0.5f, 28, offset);
ui.SetText(text);
new UIElementBorder(ui);
Expand All @@ -85,7 +84,7 @@ public void CreateWindow()
List<DialogWindow.EventButton> buttons = eventData.GetButtons();
foreach (EventButton eb in buttons)
{
float length = UIElement.GetStringWidth(eb.GetLabel().Translate(), UIScaler.GetMediumFont());
float length = ui.GetStringWidth(eb.GetLabel().Translate(), UIScaler.GetMediumFont());
if (length > buttonWidth)
{
buttonWidth = length;
Expand Down Expand Up @@ -123,13 +122,12 @@ public void CreateWindow()
public void CreateQuotaWindow()
{
// Draw text
float offset = UIElement.GetStringHeight(text, 28);
UIElement ui = new UIElement();
float offset = ui.GetStringHeight(text, 28);
if (offset < 4)
{
offset = 4;
}

UIElement ui = new UIElement();
ui.SetLocation(UIScaler.GetHCenter(-14f), 0.5f, 28, offset);
ui.SetText(text);
new UIElementBorder(ui);
Expand Down
2 changes: 1 addition & 1 deletion unity/Assets/Scripts/Quest/LogWindow.cs
Expand Up @@ -51,7 +51,7 @@ public void Update(bool toggle = false)
string entry = e.GetEntry(developerToggle).Trim('\n');
if (entry.Length == 0) continue;
ui = new UIElement(scrollArea.GetScrollTransform());
float height = UIElement.GetStringHeight(entry, textWidth);
float height = ui.GetStringHeight(entry, textWidth);
ui.SetLocation(0, offset, textWidth, height);
ui.SetText(entry, Color.black);
ui.SetBGColor(Color.white);
Expand Down
55 changes: 38 additions & 17 deletions unity/Assets/Scripts/Quest/NextStageButton.cs
Expand Up @@ -21,6 +21,9 @@ public NextStageButton()

public void Update()
{
// First tile has not been displayed, button bar is not required yet
if (!Game.Get().quest.firstTileDisplayed) return;

// Clean up everything marked as 'uiphase'
foreach (GameObject go in GameObject.FindGameObjectsWithTag(Game.UIPHASE))
Object.Destroy(go);
Expand All @@ -45,48 +48,66 @@ public void Update()
bgColor = new Color(0, 0.05f, 0, 0.9f);
}

float string_width=0f, offset=0.5f;
StringKey text;

// Inventory button
UIElement ui = new UIElement(Game.UIPHASE);
ui.SetLocation(UIScaler.GetHCenter(12f), UIScaler.GetBottom(-2.5f), 4, 2);
ui.SetText(CommonStringKeys.TAB);
text = new StringKey("val", "ITEMS_SMALL");
ui.SetText(text);
string_width = ui.GetStringWidth(text, UIScaler.GetMediumFont(), Game.Get().gameType.GetHeaderFont()) + 0.5f;
ui.SetLocation(offset, UIScaler.GetBottom(-2.5f), string_width, 2);
ui.SetFont(Game.Get().gameType.GetHeaderFont());
ui.SetFontSize(UIScaler.GetMediumFont());
ui.SetButton(Next);
ui.SetButton(Items);
ui.SetBGColor(bgColor);
new UIElementBorder(ui);
offset += string_width;

// Action button
ui = new UIElement(Game.UIPHASE);
ui.SetLocation(UIScaler.GetHCenter(16f), UIScaler.GetBottom(-2.5f), 4, 2);
ui.SetText(new StringKey("val", "ITEMS_SMALL"));
text = CommonStringKeys.SET;
ui.SetText(text);
string_width = ui.GetStringWidth(text, UIScaler.GetMediumFont(), Game.Get().gameType.GetHeaderFont()) + 0.5f;
ui.SetLocation(offset, UIScaler.GetBottom(-2.5f), string_width, 2);
ui.SetFont(Game.Get().gameType.GetHeaderFont());
ui.SetFontSize(UIScaler.GetMediumFont());
ui.SetButton(Items);
ui.SetButton(Set);
ui.SetBGColor(bgColor);
new UIElementBorder(ui);
offset += string_width;

// Log button (text from previous event)
ui = new UIElement(Game.UIPHASE);
ui.SetLocation(UIScaler.GetHCenter(-12f), UIScaler.GetBottom(-2.5f), 4, 2);
ui.SetText(CommonStringKeys.LOG);
text = CommonStringKeys.LOG;
ui.SetText(text);
string_width = ui.GetStringWidth(text, UIScaler.GetMediumFont(), Game.Get().gameType.GetHeaderFont()) + 0.5f;
ui.SetLocation(offset, UIScaler.GetBottom(-2.5f), string_width, 2);
ui.SetFont(Game.Get().gameType.GetHeaderFont());
ui.SetFontSize(UIScaler.GetMediumFont());
ui.SetButton(Log);
ui.SetBGColor(bgColor);
new UIElementBorder(ui);
offset += string_width;

// Text description for current phase
ui = new UIElement(Game.UIPHASE);
ui.SetLocation(UIScaler.GetHCenter(-8f), UIScaler.GetBottom(-2.5f), 4, 2);
ui.SetText(CommonStringKeys.SET);
ui.SetText(phase);
string_width = ui.GetStringWidth(phase, UIScaler.GetMediumFont(), Game.Get().gameType.GetHeaderFont()) + 0.5f;
ui.SetLocation(UIScaler.GetRight(-4.5f - string_width), UIScaler.GetBottom(-2.5f), string_width, 2);
ui.SetBGColor(bgColor);
ui.SetFont(Game.Get().gameType.GetHeaderFont());
ui.SetFontSize(UIScaler.GetMediumFont());
ui.SetButton(Set);
ui.SetBGColor(bgColor);
new UIElementBorder(ui);
ui.SetFontStyle(FontStyle.Italic);

// Next phase button
ui = new UIElement(Game.UIPHASE);
ui.SetLocation(UIScaler.GetHCenter(-4f), UIScaler.GetBottom(-2.5f), 16, 2);
ui.SetText(phase);
ui.SetBGColor(bgColor);
ui.SetLocation(UIScaler.GetRight(-4f), UIScaler.GetBottom(-2.5f), 3, 2);
ui.SetText(CommonStringKeys.TAB);
ui.SetFont(Game.Get().gameType.GetHeaderFont());
ui.SetFontSize(UIScaler.GetMediumFont());
ui.SetFontSize(UIScaler.GetLargeFont());
ui.SetButton(Next);
ui.SetBGColor(bgColor);
new UIElementBorder(ui);
}

Expand Down
22 changes: 19 additions & 3 deletions unity/Assets/Scripts/Quest/Quest.cs
Expand Up @@ -80,6 +80,9 @@ public class Quest
// This is true once heros are selected and the quest is started
public bool heroesSelected = false;

// This is true once the first tile has been displayed
public bool firstTileDisplayed = false;

// A list of music if custom music has been selected - used for save games
public List<string> music = new List<string>();

Expand Down Expand Up @@ -579,7 +582,8 @@ public void ChangeQuest(string path)
h.activated = false;
h.defeated = false;
h.selected = false;
if (h.heroData != null) {
if (h.heroData != null)
{
heroCount++;
// Create variable to value 1 for each selected Hero
game.quest.vars.SetValue("#" + h.heroData.sectionName, 1);
Expand Down Expand Up @@ -885,9 +889,9 @@ public void Undo()
public void AdjustMorale(int m, bool delay = false)
{
Game game = Game.Get();

float morale = vars.GetValue("$%morale") + m;
vars.SetValue("$%morale", morale);
vars.SetValue("$%morale", morale);

// Test for no morale ending
if (morale < 0)
Expand Down Expand Up @@ -1334,6 +1338,18 @@ public Tile(QuestData.Tile questTile, Game gameObject) : base(gameObject)
// Move tile into target location (Space.World is needed because tile has been rotated)
unityObject.transform.Translate(new Vector3(qTile.location.x, qTile.location.y, 0), Space.World);
image.color = new Color(1, 1, 1, 0);

if (!Game.Get().quest.firstTileDisplayed)
{
Game.Get().quest.firstTileDisplayed = true;

// We wait for the first tile displayed on MoM to display the 'NextStage' button bar
// Don't do anything if quest is being loaded and stageUI does not exist yet
if (game.gameType.TypeName() == "MoM" && game.stageUI != null)
{
game.stageUI.Update();
}
}
}

// Remove this tile
Expand Down
2 changes: 1 addition & 1 deletion unity/Assets/Scripts/UI/MenuButton.cs
Expand Up @@ -19,7 +19,7 @@ public MenuButton()
}
else
{
ui.SetLocation(0.5f, UIScaler.GetBottom(-2.5f),5, 2);
ui.SetLocation(UIScaler.GetRight(-5.5f), 0.5f ,5, 2);
}
ui.SetText(MENU);
ui.SetFont(Game.Get().gameType.GetHeaderFont());
Expand Down
8 changes: 4 additions & 4 deletions unity/Assets/Scripts/UI/Screens/QuestDetailsScreen.cs
Expand Up @@ -32,17 +32,17 @@ public QuestDetailsScreen(QuestData.Quest q)
}

// Draw Description
float height = UIElement.GetStringHeight(q.description, 30);
if (height > 25) height = 25;
ui = new UIElement();
float height = ui.GetStringHeight(q.description, 30);
if (height > 25) height = 25;
ui.SetLocation(UIScaler.GetHCenter(-7), 15 - (height / 2), 30, height);
ui.SetText(q.description);
new UIElementBorder(ui);

// Draw authors
height = UIElement.GetStringHeight(q.authors, 14);
if (height > 25) height = 25;
ui = new UIElement();
height = ui.GetStringHeight(q.authors, 14);
if (height > 25) height = 25;
ui.SetLocation(UIScaler.GetHCenter(-23), 18.5f - (height / 2), 14, height);
ui.SetText(q.authors);
new UIElementBorder(ui);
Expand Down
61 changes: 53 additions & 8 deletions unity/Assets/Scripts/UI/UIElement.cs
Expand Up @@ -21,8 +21,8 @@ public class UIElement
protected float textPadding = textPaddingDefault;

// Used for calculating text size
protected static GameObject textWidthObj;
protected static GameObject textHeightObj;
protected GameObject textWidthObj;
protected GameObject textHeightObj;

protected UnityEngine.Events.UnityAction buttonCall;

Expand Down Expand Up @@ -256,6 +256,16 @@ public virtual void SetFontSize(int size)
text.GetComponent<UnityEngine.UI.Text>().fontSize = size;
}

/// <summary>
/// Set font style.</summary>
/// <param name="style">Style of text (bold, italic...) </param>
/// <remarks>
/// Must be called after SetText.</remarks>
public virtual void SetFontStyle(FontStyle style)
{
text.GetComponent<UnityEngine.UI.Text>().fontStyle = style;
}

/// <summary>
/// Set font.</summary>
/// <param name="font">Font to use.</param>
Expand Down Expand Up @@ -301,17 +311,40 @@ public virtual bool Empty()
/// <param name="content">Text to translate.</param>
/// <returns>
/// The size of the text in UIScaler units.</returns>
public static float GetStringWidth(StringKey content)
public float GetStringWidth(StringKey content)
{
return GetStringWidth(content.Translate());
}

/// <summary>
/// Get the length of a text string at given size with given font.</summary>
/// <param name="content">Text to translate.</param>
/// <param name="fontSize">Size of font.</param>
/// <returns>
/// The size of the text in UIScaler units.</returns>
public float GetStringWidth(StringKey content, int fontSize)
{
return GetStringWidth(content.Translate(), fontSize);
}

/// <summary>
/// Get the length of a text string at small size with given font.</summary>
/// <param name="content">Text to translate.</param>
/// <param name="fontSize">Size of font.</param>
/// <param name="fontName">Name of font.</param>
/// <returns>
/// The size of the text in UIScaler units.</returns>
public float GetStringWidth(StringKey content, int fontSize, Font fontName)
{
return GetStringWidth(content.Translate(), fontSize, fontName);
}

/// <summary>
/// Get the length of a text string at small size with standard font.</summary>
/// <param name="content">Text to measure.</param>
/// <returns>
/// The size of the text in UIScaler units.</returns>
public static float GetStringWidth(string content)
public float GetStringWidth(string content)
{
return GetStringWidth(content, UIScaler.GetSmallFont());
}
Expand All @@ -322,15 +355,27 @@ public static float GetStringWidth(string content)
/// <param name="fontSize">Size of font.</param>
/// <returns>
/// The size of the text in UIScaler units.</returns>
public static float GetStringWidth(string content, int fontSize)
public float GetStringWidth(string content, int fontSize)
{
return GetStringWidth(content, fontSize, Game.Get().gameType.GetFont());
}

/// <summary>
/// Get the length of a text string at small size with standard font.</summary>
/// <param name="content">Text to measure.</param>
/// <param name="fontSize">Size of font.</param>
/// <param name="fontName">Name of font.</param>
/// <returns>
/// The size of the text in UIScaler units.</returns>
public float GetStringWidth(string content, int fontSize, Font fontName)
{
if (textWidthObj == null)
{
textWidthObj = new GameObject("TextSizing");
textWidthObj.AddComponent<UnityEngine.UI.Text>();
RectTransform transform = textWidthObj.GetComponent<RectTransform>();
transform.offsetMax = new Vector2(20000, 20000);
textWidthObj.GetComponent<UnityEngine.UI.Text>().font = Game.Get().gameType.GetFont();
textWidthObj.GetComponent<UnityEngine.UI.Text>().font = fontName;
textWidthObj.GetComponent<UnityEngine.UI.Text>().fontSize = fontSize;
}
textWidthObj.GetComponent<UnityEngine.UI.Text>().text = content;
Expand All @@ -344,7 +389,7 @@ public static float GetStringWidth(string content, int fontSize)
/// <param name="width">Width of the text box in UIScaler units.</param>
/// <returns>
/// The required text box height in UIScaler units.</returns>
public static float GetStringHeight(StringKey content, float width)
public float GetStringHeight(StringKey content, float width)
{
return GetStringHeight(content.Translate(), width);
}
Expand All @@ -355,7 +400,7 @@ public static float GetStringHeight(StringKey content, float width)
/// <param name="width">Width of the text box in UIScaler units.</param>
/// <returns>
/// The required text box height in UIScaler units.</returns>
public static float GetStringHeight(string content, float width)
public float GetStringHeight(string content, float width)
{
if (textHeightObj == null)
{
Expand Down

0 comments on commit b92d0a7

Please sign in to comment.