@ -5,145 +5,216 @@ using System;
using System.Collections ;
using System.Collections.Generic ;
public class DialogController : MonoBehaviour
namespace Fungus.Script
{
public Sprite testCharacter ;
public Canvas dialogCanvas ;
public List < Button > optionButtons = new List < Button > ( ) ;
public Text nameText ;
public Text storyText ;
public Image continueImage ;
public Image leftImage ;
public Image rightImage ;
public enum ImageSide
{
Left ,
Right
} ;
public void SetCharacterImage ( Sprite image , ImageSide side )
public class DialogController : MonoBehaviour
{
if ( leftImage ! = null )
public enum DialogSide
{
Left ,
Right
} ;
public class Option
{
public string text ;
public Action onSelect ;
}
public Canvas dialogCanvas ;
public List < UnityEngine . UI . Button > optionButtons = new List < UnityEngine . UI . Button > ( ) ;
public Text nameText ;
public Text storyText ;
public Image continueImage ;
public Image leftImage ;
public Image rightImage ;
List < Action > optionActions = new List < Action > ( ) ;
public void SetCharacter ( Character character )
{
if ( character = = null )
{
return ;
}
SetCharacterImage ( character . characterImage , character . dialogSide ) ;
SetCharacterName ( character . name , character . characterColor ) ;
}
public void SetCharacterImage ( Sprite image , DialogSide side )
{
if ( image ! = null & &
side = = ImageSide . Left )
if ( leftImage ! = null )
{
leftImage . sprite = image ;
leftImage . enabled = true ;
if ( image ! = null & &
side = = DialogSide . Left )
{
leftImage . sprite = image ;
leftImage . enabled = true ;
}
else
{
leftImage . enabled = false ;
}
}
else
if ( rightImage ! = null )
{
leftImage . enabled = false ;
rightImage . sprite = null ;
if ( image ! = null & &
side = = DialogSide . Right )
{
rightImage . sprite = image ;
rightImage . enabled = true ;
}
else
{
rightImage . sprite = null ;
rightImage . enabled = false ;
}
}
}
if ( rightImage ! = null )
public void SetCharacterName ( string name , Color color )
{
rightImage . sprite = null ;
if ( image ! = null & &
side = = ImageSide . Right )
if ( nameText ! = null )
{
rightImage . sprite = image ;
rightImage . enabled = true ;
nameText . text = name ;
nameText . color = color ;
}
else
}
public void Say ( string text , Action onComplete )
{
Clear ( ) ;
if ( storyText ! = null )
{
rightImage . sprite = null ;
rightImage . enabled = false ;
storyText . text = text ;
}
// TODO: Wait for text to finish writing
ShowContinueIcon ( true ) ;
StartCoroutine ( WaitForInput ( onComplete ) ) ;
}
}
public void SetCharacterName ( string name , Color color )
{
if ( nameText ! = null )
public void Say ( string text , List < Option > options )
{
nameText . text = name ;
nameText . color = color ;
Clear ( ) ;
if ( storyText ! = null )
{
storyText . text = text ;
}
ShowContinueIcon ( false ) ;
foreach ( Option option in options )
{
AddOption ( option . text , option . onSelect ) ;
}
}
}
public void SetStoryText ( string text )
{
if ( storyText ! = null )
IEnumerator WaitForInput ( Action onComplete )
{
storyText . text = text ;
// TODO: Handle touch input
while ( ! Input . GetMouseButtonDown ( 0 ) )
{
yield return null ;
}
Clear ( ) ;
if ( onComplete ! = null )
{
onComplete ( ) ;
}
}
}
public void ShowContinueIcon ( bool visible )
{
if ( continueImage ! = null )
void ShowContinueIcon ( bool visible )
{
continueImage . enabled = visible ;
if ( continueImage ! = null )
{
continueImage . enabled = visible ;
}
}
}
public void ClearOptions ( )
{
if ( optionButtons = = null )
void Clear ( )
{
return ;
ClearStoryText ( ) ;
ClearOptions ( ) ;
}
foreach ( Button button in optionButtons )
void ClearStoryText ( )
{
button . gameObject . SetActive ( false ) ;
if ( storyText ! = null )
{
storyText . text = "" ;
}
}
}
public void AddOption ( string text , Action action )
{
if ( optionButtons = = null )
void ClearOptions ( )
{
return ;
if ( optionButtons = = null )
{
return ;
}
optionActions . Clear ( ) ;
foreach ( UnityEngine . UI . Button button in optionButtons )
{
if ( button ! = null )
{
button . gameObject . SetActive ( false ) ;
}
}
}
foreach ( Button button in optionButtons )
bool AddOption ( string text , Action action )
{
if ( ! button . gameObject . activeSelf )
if ( optionButtons = = null )
{
button . gameObject . SetActive ( true ) ;
return false ;
}
Text textComponent = button . GetComponentInChildren < Text > ( ) ;
if ( textComponent ! = null )
bool addedOption = false ;
foreach ( UnityEngine . UI . Button button in optionButtons )
{
if ( ! button . gameObject . activeSelf )
{
textComponent . text = text ;
}
button . gameObject . SetActive ( true ) ;
Text textComponent = button . GetComponentInChildren < Text > ( ) ;
if ( textComponent ! = null )
{
textComponent . text = text ;
}
// TODO: Connect action
optionActions . Add ( action ) ;
break ;
addedOption = true ;
break ;
}
}
}
}
public void Start ( )
{
SetCharacterImage ( testCharacter , ImageSide . Left ) ;
SetCharacterName ( "Podrick" , Color . red ) ;
SetStoryText ( "Simple story text" ) ;
ShowContinueIcon ( false ) ;
ClearOptions ( ) ;
AddOption ( "Something 1" , Callback ) ;
AddOption ( "Something 2" , Callback ) ;
}
return addedOption ;
}
void Callback ( )
{
Debug . Log ( "Callback" ) ;
public void SelectOption ( int index )
{
if ( index < optionActions . Count )
{
Action optionAction = optionActions [ index ] ;
if ( optionAction ! = null )
{
Clear ( ) ;
optionAction ( ) ;
}
}
}
}
//public UnityEvent testEvent;
// Write story text over time
// Show character image (with side, fade in?)
// Hide / Show canvas
// Show continue image
// Show one button
// Show button grid
}