Browse Source

Added PageStyle class to simplify control of Page rendering styles.

Can now set the Game.activePageStyle property to control the currently
active Page rendering style.
Added a SetPageStyle() command to set the style in a command sequence.
Added 2 PageStyle prefabs assets.
Renamed Room classes to be consistent.
master
chrisgregan 11 years ago
parent
commit
d35ff84ea3
  1. BIN
      Assets/Fungus/Prefabs/Game.prefab
  2. BIN
      Assets/Fungus/Prefabs/PageStyle(NoBox).prefab
  3. 4
      Assets/Fungus/Prefabs/PageStyle(NoBox).prefab.meta
  4. BIN
      Assets/Fungus/Prefabs/PageStyle.prefab
  5. 4
      Assets/Fungus/Prefabs/PageStyle.prefab.meta
  6. 22
      Assets/Fungus/Scripts/Commands.cs
  7. 12
      Assets/Fungus/Scripts/Game.cs
  8. 34
      Assets/Fungus/Scripts/GameController.cs
  9. 85
      Assets/Fungus/Scripts/Page.cs
  10. 48
      Assets/Fungus/Scripts/PageStyle.cs
  11. 8
      Assets/Fungus/Scripts/PageStyle.cs.meta
  12. BIN
      Assets/FungusExample/Scenes/Example.unity
  13. 14
      Assets/FungusExample/Scripts/MenuRoom.cs
  14. 0
      Assets/FungusExample/Scripts/MenuRoom.cs.meta
  15. 13
      Assets/FungusExample/Scripts/PageRoom.cs
  16. 0
      Assets/FungusExample/Scripts/PageRoom.cs.meta
  17. 3
      Assets/FungusExample/Scripts/SpriteRoom.cs
  18. 0
      Assets/FungusExample/Scripts/SpriteRoom.cs.meta
  19. 2
      Assets/FungusExample/Scripts/ViewRoom.cs
  20. 0
      Assets/FungusExample/Scripts/ViewRoom.cs.meta

BIN
Assets/Fungus/Prefabs/Game.prefab

Binary file not shown.

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

Binary file not shown.

4
Assets/Fungus/Prefabs/PageStyle(NoBox).prefab.meta

@ -0,0 +1,4 @@
fileFormatVersion: 2
guid: 25812898d7e74407b94d0216027d5876
NativeFormatImporter:
userData:

BIN
Assets/Fungus/Prefabs/PageStyle.prefab

Binary file not shown.

4
Assets/Fungus/Prefabs/PageStyle.prefab.meta

@ -0,0 +1,4 @@
fileFormatVersion: 2
guid: cdd7a3dba1d864bb78106f6d917a70ae
NativeFormatImporter:
userData:

22
Assets/Fungus/Scripts/Commands.cs

@ -124,6 +124,28 @@ namespace Fungus
} }
} }
/**
* Sets the currently active Page Style for rendering Pages.
*/
public class SetPageStyleCommand : CommandQueue.Command
{
PageStyle pageStyle;
public SetPageStyleCommand(PageStyle _pageStyle)
{
pageStyle = _pageStyle;
}
public override void Execute(CommandQueue commandQueue, Action onComplete)
{
Game.GetInstance().activePageStyle = pageStyle;
if (onComplete != null)
{
onComplete();
}
}
}
/** /**
* Sets the title text displayed at the top of the active page. * Sets the title text displayed at the top of the active page.
*/ */

12
Assets/Fungus/Scripts/Game.cs

@ -27,6 +27,11 @@ namespace Fungus
*/ */
public Room activeRoom; public Room activeRoom;
/**
* The style to apply when displaying Pages.
*/
public PageStyle activePageStyle;
/** /**
* Automatically display links between connected Rooms. * Automatically display links between connected Rooms.
*/ */
@ -96,6 +101,13 @@ namespace Fungus
commandQueue = gameObject.AddComponent<CommandQueue>(); commandQueue = gameObject.AddComponent<CommandQueue>();
cameraController = gameObject.AddComponent<CameraController>(); cameraController = gameObject.AddComponent<CameraController>();
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
audioSource.playOnAwake = false;
audioSource.loop = true;
// Create a default page style if none exists
if (activeRoom == null) if (activeRoom == null)
{ {
// Pick first room found if none is specified // Pick first room found if none is specified

34
Assets/Fungus/Scripts/GameController.cs

@ -106,28 +106,40 @@ namespace Fungus
#region Page Methods #region Page Methods
/** /**
* Sets the title text displayed at the top of the active Page. * Sets the currently active Page for story text display.
* The title text is only displayed when there is some story text or options to be shown. * Once this command executes, all story text added using Say(), AddOption(), Choose(), etc. will be displayed on this Page.
* When a Room contains multiple Page objects, this method can be used to control which Page object is active at a given time.
* This method returns immediately but it queues an asynchronous command for later execution. * This method returns immediately but it queues an asynchronous command for later execution.
* @param titleText The text to display as the title of the Page. * @param page The Page object to make active
*/ */
public void Title(string titleText) public void SetPage(Page page)
{ {
CommandQueue commandQueue = Game.GetInstance().commandQueue; CommandQueue commandQueue = Game.GetInstance().commandQueue;
commandQueue.AddCommand(new TitleCommand(titleText)); commandQueue.AddCommand(new SetPageCommand(page));
} }
/** /**
* Sets the currently active Page for story text display. * Sets the currently active style for displaying Pages.
* Once this command executes, all story text added using Say(), AddOption(), Choose(), etc. will be displayed on this Page. * Once this command executes, all Pages will display using the new style.
* When a Room contains multiple Page objects, this method can be used to control which Page object is active at a given time.
* This method returns immediately but it queues an asynchronous command for later execution. * This method returns immediately but it queues an asynchronous command for later execution.
* @param page The Page object to make active * @param pageStyle The style object to make active
*/ */
public void SetPage(Page page) public void SetPageStyle(PageStyle pageStyle)
{ {
CommandQueue commandQueue = Game.GetInstance().commandQueue; CommandQueue commandQueue = Game.GetInstance().commandQueue;
commandQueue.AddCommand(new SetPageCommand(page)); commandQueue.AddCommand(new SetPageStyleCommand(pageStyle));
}
/**
* Sets the title text displayed at the top of the active Page.
* The title text is only displayed when there is some story text or options to be shown.
* This method returns immediately but it queues an asynchronous command for later execution.
* @param titleText The text to display as the title of the Page.
*/
public void Title(string titleText)
{
CommandQueue commandQueue = Game.GetInstance().commandQueue;
commandQueue.AddCommand(new TitleCommand(titleText));
} }
/** /**

85
Assets/Fungus/Scripts/Page.cs

@ -14,19 +14,9 @@ namespace Fungus
[ExecuteInEditMode] [ExecuteInEditMode]
public class Page : MonoBehaviour public class Page : MonoBehaviour
{ {
/// Rectangular bounds used to display page text
public Bounds pageBounds = new Bounds(Vector3.zero, new Vector3(0.25f, 0.25f, 0f)); public Bounds pageBounds = new Bounds(Vector3.zero, new Vector3(0.25f, 0.25f, 0f));
// The font size for title, say and option text is calculated by multiplying the screen height
// by the corresponding font scale. Text appears the same size across all device resolutions.
public float titleFontScale = 1f / 20f;
public float sayFontScale = 1f / 25f;
public float optionFontScale = 1f / 25f;
public GUIStyle titleStyle;
public GUIStyle sayStyle;
public GUIStyle optionStyle;
public GUIStyle boxStyle;
string titleText = ""; string titleText = "";
string originalStoryText = ""; string originalStoryText = "";
@ -57,15 +47,6 @@ namespace Fungus
List<Option> options = new List<Option>(); List<Option> options = new List<Option>();
void Start()
{
// 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);
}
public void SetTitle(string _titleText) public void SetTitle(string _titleText)
{ {
titleText = _titleText; titleText = _titleText;
@ -96,6 +77,12 @@ namespace Fungus
void WriteStory(string storyText, Action writeAction) void WriteStory(string storyText, Action writeAction)
{ {
PageStyle pageStyle = Game.GetInstance().activePageStyle;
if (pageStyle == null)
{
return;
}
// Add continue option // Add continue option
if (writeAction != null) if (writeAction != null)
{ {
@ -112,7 +99,7 @@ namespace Fungus
else else
{ {
float textWidth = CalcInnerRect(GetScreenRect()).width; float textWidth = CalcInnerRect(GetScreenRect()).width;
originalStoryText = InsertLineBreaks(storyText, sayStyle, textWidth); originalStoryText = InsertLineBreaks(storyText, pageStyle.sayStyle, textWidth);
displayedStoryText = ""; displayedStoryText = "";
// Use a coroutine to write the story text out over time // Use a coroutine to write the story text out over time
@ -163,6 +150,12 @@ namespace Fungus
return; return;
} }
PageStyle pageStyle = Game.GetInstance().activePageStyle;
if (pageStyle == null)
{
return;
}
Rect pageRect = GetScreenRect(); Rect pageRect = GetScreenRect();
Rect outerRect = pageRect; Rect outerRect = pageRect;
Rect innerRect = CalcInnerRect(outerRect); Rect innerRect = CalcInnerRect(outerRect);
@ -174,25 +167,25 @@ namespace Fungus
float contentHeight = titleHeight + storyHeight + optionsHeight; float contentHeight = titleHeight + storyHeight + optionsHeight;
// Adjust inner and outer rect to center around original page middle // Adjust inner and outer rect to center around original page middle
outerRect.height = contentHeight + (boxStyle.padding.top + boxStyle.padding.bottom); outerRect.height = contentHeight + (pageStyle.boxStyle.padding.top + pageStyle.boxStyle.padding.bottom);
outerRect.y = pageRect.center.y - outerRect.height / 2; outerRect.y = pageRect.center.y - outerRect.height / 2;
innerRect = CalcInnerRect(outerRect); innerRect = CalcInnerRect(outerRect);
// Draw box // Draw box
Rect boxRect = outerRect; Rect boxRect = outerRect;
boxRect.height = contentHeight + (boxStyle.padding.top + boxStyle.padding.bottom); boxRect.height = contentHeight + (pageStyle.boxStyle.padding.top + pageStyle.boxStyle.padding.bottom);
GUI.Box(boxRect, "", boxStyle); GUI.Box(boxRect, "", pageStyle.boxStyle);
// Draw title label // Draw title label
Rect titleRect = innerRect; Rect titleRect = innerRect;
titleRect.height = titleHeight; titleRect.height = titleHeight;
GUI.Label(titleRect, titleText, titleStyle); GUI.Label(titleRect, titleText, pageStyle.titleStyle);
// Draw say label // Draw say label
Rect storyRect = innerRect; Rect storyRect = innerRect;
storyRect.y += titleHeight; storyRect.y += titleHeight;
storyRect.height = storyHeight; storyRect.height = storyHeight;
GUI.Label(storyRect, displayedStoryText, sayStyle); GUI.Label(storyRect, displayedStoryText, pageStyle.sayStyle);
// Draw option buttons // Draw option buttons
bool finishedWriting = (displayedStoryText.Length == originalStoryText.Length); bool finishedWriting = (displayedStoryText.Length == originalStoryText.Length);
@ -207,10 +200,10 @@ namespace Fungus
foreach (Option option in options) foreach (Option option in options)
{ {
GUIContent buttonContent = new GUIContent(option.optionText); GUIContent buttonContent = new GUIContent(option.optionText);
buttonRect.height = optionStyle.CalcHeight(buttonContent, innerRect.width); buttonRect.height = pageStyle.optionStyle.CalcHeight(buttonContent, innerRect.width);
if (quickContinue || if (quickContinue ||
GUI.Button(buttonRect, buttonContent, optionStyle)) GUI.Button(buttonRect, buttonContent, pageStyle.optionStyle))
{ {
if (option.optionAction != null) if (option.optionAction != null)
{ {
@ -260,31 +253,40 @@ namespace Fungus
float CalcTitleHeight(float boxWidth) float CalcTitleHeight(float boxWidth)
{ {
if (mode == Mode.Idle || PageStyle pageStyle = Game.GetInstance().activePageStyle;
if (pageStyle == null ||
mode == Mode.Idle ||
titleText.Length == 0) titleText.Length == 0)
{ {
return 0; return 0;
} }
GUIContent titleContent = new GUIContent(titleText); GUIContent titleContent = new GUIContent(titleText);
return titleStyle.CalcHeight(titleContent, boxWidth); return pageStyle.titleStyle.CalcHeight(titleContent, boxWidth);
} }
float CalcStoryHeight(float boxWidth) float CalcStoryHeight(float boxWidth)
{ {
if (mode == Mode.Idle || PageStyle pageStyle = Game.GetInstance().activePageStyle;
if (pageStyle == null ||
mode == Mode.Idle ||
originalStoryText.Length == 0) originalStoryText.Length == 0)
{ {
return 0; return 0;
} }
GUIContent storyContent = new GUIContent(originalStoryText + "\n"); GUIContent storyContent = new GUIContent(originalStoryText + "\n");
return sayStyle.CalcHeight(storyContent, boxWidth); return pageStyle.sayStyle.CalcHeight(storyContent, boxWidth);
} }
float CalcOptionsHeight(float boxWidth) float CalcOptionsHeight(float boxWidth)
{ {
if (mode == Mode.Idle || PageStyle pageStyle = Game.GetInstance().activePageStyle;
if (pageStyle == null ||
mode == Mode.Idle ||
options.Count == 0) options.Count == 0)
{ {
return 0; return 0;
@ -294,7 +296,7 @@ namespace Fungus
foreach (Option option in options) foreach (Option option in options)
{ {
GUIContent optionContent = new GUIContent(option.optionText); GUIContent optionContent = new GUIContent(option.optionText);
float optionHeight = optionStyle.CalcHeight(optionContent, boxWidth); float optionHeight = pageStyle.optionStyle.CalcHeight(optionContent, boxWidth);
totalHeight += optionHeight; totalHeight += optionHeight;
} }
@ -304,10 +306,17 @@ namespace Fungus
// Returns smaller internal box rect with padding style applied // Returns smaller internal box rect with padding style applied
Rect CalcInnerRect(Rect outerRect) Rect CalcInnerRect(Rect outerRect)
{ {
return new Rect(outerRect.x + boxStyle.padding.left, PageStyle pageStyle = Game.GetInstance().activePageStyle;
outerRect.y + boxStyle.padding.top,
outerRect.width - (boxStyle.padding.left + boxStyle.padding.right), if (pageStyle == null)
outerRect.height - (boxStyle.padding.top + boxStyle.padding.bottom)); {
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));
} }
// Returns the page rect in screen space coords // Returns the page rect in screen space coords

48
Assets/Fungus/Scripts/PageStyle.cs

@ -0,0 +1,48 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
/**
* Defines visual appearance of a Page.
* Usage:
* 1. Add this component to an empty game object
* 2. Customize the style properties
* 3. Assign the style object to the pageStyle property of the Page you wish to style
*/
public class PageStyle : MonoBehaviour
{
// The font size for title, say and option text is calculated by multiplying the screen height
// by the corresponding font scale. Text appears the same size across all device resolutions.
/// Title font size as a fraction of screen height.
public float titleFontScale = 1f / 20f;
/// Say font size as a fraction of screen height.
public float sayFontScale = 1f / 25f;
/// Option font size as a fraction of screen height.
public float optionFontScale = 1f / 25f;
/// Style for title text
public GUIStyle titleStyle;
/// Style for say text
public GUIStyle sayStyle;
/// Style for option text
public GUIStyle optionStyle;
/// Style for text box
public GUIStyle boxStyle;
void Update()
{
// 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);
}
}
}

8
Assets/Fungus/Scripts/PageStyle.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1f1a1fd7fb09d46438885139f2364a93
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.

14
Assets/FungusExample/Scripts/MenusRoom.cs → Assets/FungusExample/Scripts/MenuRoom.cs

@ -2,11 +2,11 @@ using UnityEngine;
using System.Collections; using System.Collections;
using Fungus; using Fungus;
public class MenusRoom : Room public class MenuRoom : Room
{ {
public Room pagesRoom; public Room pageRoom;
public Room viewsRoom; public Room viewRoom;
public Room spritesRoom; public Room spriteRoom;
public Room audioRoom; public Room audioRoom;
void OnEnter() void OnEnter()
@ -20,17 +20,17 @@ public class MenusRoom : Room
void MoveToWriting() void MoveToWriting()
{ {
MoveToRoom(pagesRoom); MoveToRoom(pageRoom);
} }
void MoveToViews() void MoveToViews()
{ {
MoveToRoom(viewsRoom); MoveToRoom(viewRoom);
} }
void MoveToAnimations() void MoveToAnimations()
{ {
MoveToRoom(spritesRoom); MoveToRoom(spriteRoom);
} }
void MoveToAudio() void MoveToAudio()

0
Assets/FungusExample/Scripts/MenusRoom.cs.meta → Assets/FungusExample/Scripts/MenuRoom.cs.meta

13
Assets/FungusExample/Scripts/PagesRoom.cs → Assets/FungusExample/Scripts/PageRoom.cs

@ -2,11 +2,16 @@
using System.Collections; using System.Collections;
using Fungus; using Fungus;
public class PagesRoom : Room public class PageRoom : Room
{ {
// This is a reference to the menu room so we can transition back to the menu using MoveToRoom() // This is a reference to the menu room so we can transition back to the menu using MoveToRoom()
public Room menuRoom; public Room menuRoom;
// References to PageStyle prefab assets
// Use these with SetPageStyle() to change the Page rendering style
public PageStyle defaultStyle;
public PageStyle noBoxStyle;
// The OnEnter() method is called whenever the player enters the room // The OnEnter() method is called whenever the player enters the room
// You can also use the OnLeave() method to handle when the player leaves the room. // You can also use the OnLeave() method to handle when the player leaves the room.
void OnEnter() void OnEnter()
@ -63,9 +68,15 @@ public class PagesRoom : Room
void ProduceSpores() void ProduceSpores()
{ {
// Set a PageStyle with no background box texture
SetPageStyle(noBoxStyle);
Say("Yeah! I feel like doing some sporing!"); Say("Yeah! I feel like doing some sporing!");
Say("Wow - look at all these spores! COOL!"); Say("Wow - look at all these spores! COOL!");
// Set the default style with background box texture
SetPageStyle(defaultStyle);
// Sets a game flag which we check above in GoToSleep // Sets a game flag which we check above in GoToSleep
SetFlag("spawned", true); SetFlag("spawned", true);

0
Assets/FungusExample/Scripts/PagesRoom.cs.meta → Assets/FungusExample/Scripts/PageRoom.cs.meta

3
Assets/FungusExample/Scripts/SpritesRoom.cs → Assets/FungusExample/Scripts/SpriteRoom.cs

@ -2,9 +2,10 @@
using System.Collections; using System.Collections;
using Fungus; using Fungus;
public class SpritesRoom : Room public class SpriteRoom : Room
{ {
public Room menuRoom; public Room menuRoom;
public Animator blueAlienAnim; public Animator blueAlienAnim;
public SpriteRenderer blueAlienSprite; public SpriteRenderer blueAlienSprite;
public SpriteRenderer redMushroomSprite; public SpriteRenderer redMushroomSprite;

0
Assets/FungusExample/Scripts/SpritesRoom.cs.meta → Assets/FungusExample/Scripts/SpriteRoom.cs.meta

2
Assets/FungusExample/Scripts/ViewsRoom.cs → Assets/FungusExample/Scripts/ViewRoom.cs

@ -2,7 +2,7 @@
using System.Collections; using System.Collections;
using Fungus; using Fungus;
public class ViewsRoom : Room public class ViewRoom : Room
{ {
public Room menuRoom; public Room menuRoom;

0
Assets/FungusExample/Scripts/ViewsRoom.cs.meta → Assets/FungusExample/Scripts/ViewRoom.cs.meta

Loading…
Cancel
Save