Browse Source

Fixed font scale calculations.

Fonts now scale when resizing window.
Added a short delay before quick continue.
Centered title text (in style prefabs)
master
chrisgregan 11 years ago
parent
commit
2e11f6a182
  1. BIN
      Assets/Fungus/Prefabs/PageStyle(NoBox).prefab
  2. BIN
      Assets/Fungus/Prefabs/PageStyle.prefab
  3. 63
      Assets/Fungus/Scripts/Page.cs
  4. 39
      Assets/Fungus/Scripts/PageStyle.cs
  5. BIN
      Assets/FungusExample/Scenes/Example.unity

BIN
Assets/Fungus/Prefabs/PageStyle(NoBox).prefab

Binary file not shown.

BIN
Assets/Fungus/Prefabs/PageStyle.prefab

Binary file not shown.

63
Assets/Fungus/Scripts/Page.cs

@ -47,6 +47,17 @@ namespace Fungus
List<Option> options = new List<Option>();
float quickContinueTimer;
public virtual void Update()
{
if (quickContinueTimer > 0)
{
quickContinueTimer -= Time.deltaTime;
quickContinueTimer = Mathf.Max(quickContinueTimer, 0f);
}
}
public void SetTitle(string _titleText)
{
titleText = _titleText;
@ -83,6 +94,11 @@ namespace Fungus
return;
}
GUIStyle sayStyle = pageStyle.GetScaledSayStyle();
// Disable quick continue for a short period to prevent accidental taps
quickContinueTimer = 0.8f;
// Add continue option
if (writeAction != null)
{
@ -99,7 +115,7 @@ namespace Fungus
else
{
float textWidth = CalcInnerRect(GetScreenRect()).width;
originalStoryText = InsertLineBreaks(storyText, pageStyle.sayStyle, textWidth);
originalStoryText = InsertLineBreaks(storyText, sayStyle, textWidth);
displayedStoryText = "";
// Use a coroutine to write the story text out over time
@ -156,6 +172,11 @@ namespace Fungus
return;
}
GUIStyle boxStyle = pageStyle.boxStyle;
GUIStyle titleStyle = pageStyle.GetScaledTitleStyle();
GUIStyle sayStyle = pageStyle.GetScaledSayStyle();
GUIStyle optionStyle = pageStyle.GetScaledOptionStyle();
Rect pageRect = GetScreenRect();
Rect outerRect = pageRect;
Rect innerRect = CalcInnerRect(outerRect);
@ -167,25 +188,25 @@ namespace Fungus
float contentHeight = titleHeight + storyHeight + optionsHeight;
// Adjust inner and outer rect to center around original page middle
outerRect.height = contentHeight + (pageStyle.boxStyle.padding.top + pageStyle.boxStyle.padding.bottom);
outerRect.height = contentHeight + (boxStyle.padding.top + boxStyle.padding.bottom);
outerRect.y = pageRect.center.y - outerRect.height / 2;
innerRect = CalcInnerRect(outerRect);
// Draw box
Rect boxRect = outerRect;
boxRect.height = contentHeight + (pageStyle.boxStyle.padding.top + pageStyle.boxStyle.padding.bottom);
GUI.Box(boxRect, "", pageStyle.boxStyle);
boxRect.height = contentHeight + (boxStyle.padding.top + boxStyle.padding.bottom);
GUI.Box(boxRect, "", boxStyle);
// Draw title label
Rect titleRect = innerRect;
titleRect.height = titleHeight;
GUI.Label(titleRect, titleText, pageStyle.titleStyle);
GUI.Label(titleRect, titleText, titleStyle);
// Draw say label
Rect storyRect = innerRect;
storyRect.y += titleHeight;
storyRect.height = storyHeight;
GUI.Label(storyRect, displayedStoryText, pageStyle.sayStyle);
GUI.Label(storyRect, displayedStoryText, sayStyle);
// Draw option buttons
bool finishedWriting = (displayedStoryText.Length == originalStoryText.Length);
@ -193,17 +214,19 @@ namespace Fungus
if (finishedWriting)
{
// Player can continue through single options by clicking / tapping anywhere
bool quickContinue = (options.Count == 1 && (Input.GetMouseButtonUp(0) || Input.anyKeyDown));
bool quickContinue = (quickContinueTimer == 0 &&
options.Count == 1 &&
(Input.GetMouseButtonUp(0) || Input.anyKeyDown));
Rect buttonRect = innerRect;
buttonRect.y += titleHeight + storyHeight;
foreach (Option option in options)
{
GUIContent buttonContent = new GUIContent(option.optionText);
buttonRect.height = pageStyle.optionStyle.CalcHeight(buttonContent, innerRect.width);
buttonRect.height = optionStyle.CalcHeight(buttonContent, innerRect.width);
if (quickContinue ||
GUI.Button(buttonRect, buttonContent, pageStyle.optionStyle))
GUI.Button(buttonRect, buttonContent, optionStyle))
{
if (option.optionAction != null)
{
@ -262,8 +285,10 @@ namespace Fungus
return 0;
}
GUIStyle titleStyle = pageStyle.GetScaledTitleStyle();
GUIContent titleContent = new GUIContent(titleText);
return pageStyle.titleStyle.CalcHeight(titleContent, boxWidth);
return titleStyle.CalcHeight(titleContent, boxWidth);
}
float CalcStoryHeight(float boxWidth)
@ -277,8 +302,10 @@ namespace Fungus
return 0;
}
GUIStyle sayStyle = pageStyle.GetScaledSayStyle();
GUIContent storyContent = new GUIContent(originalStoryText + "\n");
return pageStyle.sayStyle.CalcHeight(storyContent, boxWidth);
return sayStyle.CalcHeight(storyContent, boxWidth);
}
float CalcOptionsHeight(float boxWidth)
@ -292,11 +319,13 @@ namespace Fungus
return 0;
}
GUIStyle optionStyle = pageStyle.GetScaledOptionStyle();
float totalHeight = 0;
foreach (Option option in options)
{
GUIContent optionContent = new GUIContent(option.optionText);
float optionHeight = pageStyle.optionStyle.CalcHeight(optionContent, boxWidth);
float optionHeight = optionStyle.CalcHeight(optionContent, boxWidth);
totalHeight += optionHeight;
}
@ -313,10 +342,12 @@ namespace Fungus
return new Rect();
}
return new Rect(outerRect.x + pageStyle.boxStyle.padding.left,
outerRect.y + pageStyle.boxStyle.padding.top,
outerRect.width - (pageStyle.boxStyle.padding.left + pageStyle.boxStyle.padding.right),
outerRect.height - (pageStyle.boxStyle.padding.top + pageStyle.boxStyle.padding.bottom));
GUIStyle boxStyle = pageStyle.boxStyle;
return new Rect(outerRect.x + boxStyle.padding.left,
outerRect.y + boxStyle.padding.top,
outerRect.width - (boxStyle.padding.left + boxStyle.padding.right),
outerRect.height - (boxStyle.padding.top + boxStyle.padding.bottom));
}
// Returns the page rect in screen space coords

39
Assets/Fungus/Scripts/PageStyle.cs

@ -36,13 +36,40 @@ namespace Fungus
/// Style for text box
public GUIStyle boxStyle;
void Update()
/**
* Returns the style for Title text.
* Override the font size to compensate for varying device resolution.
* Font size is calculated as a fraction of the current screen height.
*/
public GUIStyle GetScaledTitleStyle()
{
GUIStyle style = new GUIStyle(titleStyle);
style.fontSize = Mathf.RoundToInt((float)Screen.height * titleFontScale);
return style;
}
/**
* Returns the style for Say text.
* Override the font size to compensate for varying device resolution.
* Font size is calculated as a fraction of the current screen height.
*/
public GUIStyle GetScaledSayStyle()
{
GUIStyle style = new GUIStyle(sayStyle);
style.fontSize = Mathf.RoundToInt((float)Screen.height * sayFontScale);
return style;
}
/**
* Returns the style for Option text.
* Override the font size to compensate for varying device resolution.
* Font size is calculated as a fraction of the current screen height.
*/
public GUIStyle GetScaledOptionStyle()
{
// Override the font size to compensate for varying device resolution
// Font size is calculated as a fraction of the current screen height
titleStyle.fontSize = Mathf.RoundToInt((float)Screen.height * titleFontScale);
sayStyle.fontSize = Mathf.RoundToInt((float)Screen.height * sayFontScale);
optionStyle.fontSize = Mathf.RoundToInt((float)Screen.height * optionFontScale);
GUIStyle style = new GUIStyle(optionStyle);
style.fontSize = Mathf.RoundToInt((float)Screen.height * optionFontScale);
return style;
}
}
}

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.
Loading…
Cancel
Save