///
<summary>
///
剧情对话板
///
</summary>
public
sealed
class
DramaDialogue : UIBase {
///
<summary>
///
显示时触发
///
</summary>
public
event
EventHandler Showing;
///
<summary>
///
消失时触发
///
</summary>
public
event
EventHandler Hiding;
Rectangle topRect
=
new
Rectangle() { Fill
=
new
ImageBrush() { ImageSource
=
GlobalMethod.GetImage(
"
UI/DramaLine.jpg
"
, UriType.Project), Stretch
=
Stretch.Fill } };
Rectangle middleRect
=
new
Rectangle() { Fill
=
new
ImageBrush() { ImageSource
=
GlobalMethod.GetImage(
"
UI/GrayRect.png
"
, UriType.Project), Stretch
=
Stretch.Fill } };
Rectangle bottomRect
=
new
Rectangle() { Fill
=
new
ImageBrush() { ImageSource
=
GlobalMethod.GetImage(
"
UI/DramaLine.jpg
"
, UriType.Project), Stretch
=
Stretch.Fill } };
Canvas topCanvas
=
new
Canvas();
Canvas bottomCanvas
=
new
Canvas();
EntityObject closer
=
new
EntityObject() { Source
=
GlobalMethod.GetImage(
"
UI/EndDialogue.png
"
, UriType.Project) };
TextBlock tip
=
new
TextBlock() { Text
=
"
【按Esc退出对话】
"
, Foreground
=
new
SolidColorBrush(Colors.Orange), FontSize
=
18
, TextWrapping
=
TextWrapping.Wrap };
TextBlock content
=
new
TextBlock() { Foreground
=
new
SolidColorBrush(Colors.White), FontSize
=
24
, TextWrapping
=
TextWrapping.Wrap };
EntityObject avatar
=
new
EntityObject() { Source
=
GlobalMethod.GetImage(
"
UI/Avatar0.png
"
, UriType.Project) };
DispatcherTimer timer
=
new
DispatcherTimer() { Interval
=
TimeSpan.FromMilliseconds(
30
) };
///
<summary>
///
是否正在显示
///
</summary>
public
bool
IsShowing {
get
;
private
set
; }
///
<summary>
///
获取或设置剧情播放速度
///
</summary>
public
int
DialogSpeed {
get
{
return
timer.Interval.Milliseconds; }
set
{ timer.Interval
=
TimeSpan.FromMilliseconds(value); }
}
public
DramaDialogue() {
this
.Children.Add(topCanvas);
topCanvas.Children.Add(topRect);
topCanvas.Children.Add(closer); Canvas.SetTop(closer,
10
);
this
.Children.Add(middleRect);
this
.Children.Add(bottomCanvas);
bottomCanvas.Children.Add(bottomRect);
bottomCanvas.Children.Add(tip); Canvas.SetLeft(tip,
15
); Canvas.SetTop(tip,
65
);
bottomCanvas.Children.Add(content); Canvas.SetTop(content,
10
);
bottomCanvas.Children.Add(avatar); Canvas.SetLeft(avatar,
10
);
timer.Tick
+=
delegate
{
countText
++
;
if
(countText
>
drama.Content[countDrama].Length) {
if
(timer.IsEnabled) {
timer.Stop();
countDrama
++
;
}
}
else
{
content.Text
=
drama.Content[countDrama].Substring(
, countText);
}
};
this
.MouseLeftButtonDown
+=
(s, e)
=>
{
if
(IsShowing) {
Point p
=
e.GetPosition(closer);
if
(p.X
>=
&&
p.X
<=
closer.RealWidth
&&
p.Y
>=
&&
p.Y
<=
closer.RealHeight) { Hide(); e.Handled
=
true
;
return
; }
if
(timer.IsEnabled) {
content.Text
=
drama.Content[countDrama];
timer.Stop();
countDrama
++
;
}
else
{
if
(countDrama
==
drama.Content.Count) {
Hide();
}
else
{
countText
=
;
avatar.Source
=
GlobalMethod.GetImage(
string
.Format(
"
UI/Avatar{0}.png
"
, drama.Avatar[countDrama]), UriType.Project);
timer.Start();
}
}
}
e.Handled
=
true
;
};
}
Drama drama;
int
countText
=
;
int
countDrama
=
;
public
void
Show(Drama drama) {
IsShowing
=
true
;
if
(Showing
!=
null
) { Showing(
this
,
null
); }
this
.drama
=
drama;
avatar.Source
=
GlobalMethod.GetImage(
string
.Format(
"
UI/Avatar{0}.png
"
, drama.Avatar[countDrama]), UriType.Project);
Storyboard storyboard
=
new
Storyboard();
Duration duration
=
TimeSpan.FromMilliseconds(
500
);
PowerEase powerEase
=
new
PowerEase() { EasingMode
=
EasingMode.EaseOut };
storyboard.Children.Add(GlobalMethod.CreateDoubleAnimation(topCanvas,
"
(Canvas.Top)
"
,
-
topRect.Height,
, duration, powerEase));
storyboard.Children.Add(GlobalMethod.CreateDoubleAnimation(middleRect,
"
Opacity
"
,
,
1
, duration, powerEase));
storyboard.Children.Add(GlobalMethod.CreateDoubleAnimation(bottomCanvas,
"
(Canvas.Top)
"
, Application.Current.Host.Content.ActualHeight, Application.Current.Host.Content.ActualHeight
*
4
/
5
, duration, powerEase));
storyboard.Children.Add(GlobalMethod.CreateDoubleAnimation(avatar,
"
Opacity
"
,
,
1
, duration, powerEase));
EventHandler handler
=
null
;
storyboard.Completed
+=
handler
=
delegate
{
storyboard.Completed
-=
handler;
timer.Start();
};
storyboard.Begin();
}
public
void
Hide() {
IsShowing
=
false
;
Storyboard storyboard
=
new
Storyboard();
Duration duration
=
TimeSpan.FromMilliseconds(
500
);
PowerEase powerEase
=
new
PowerEase() { EasingMode
=
EasingMode.EaseOut };
storyboard.Children.Add(GlobalMethod.CreateDoubleAnimation(topCanvas,
"
(Canvas.Top)
"
,
,
-
topRect.Height, duration, powerEase));
storyboard.Children.Add(GlobalMethod.CreateDoubleAnimation(middleRect,
"
Opacity
"
,
1
,
, duration, powerEase));
storyboard.Children.Add(GlobalMethod.CreateDoubleAnimation(bottomCanvas,
"
(Canvas.Top)
"
, Application.Current.Host.Content.ActualHeight
*
4
/
5
, Application.Current.Host.Content.ActualHeight, duration, powerEase));
storyboard.Children.Add(GlobalMethod.CreateDoubleAnimation(avatar,
"
Opacity
"
,
1
,
, duration, powerEase));
EventHandler handler
=
null
;
storyboard.Completed
+=
handler
=
delegate
{
storyboard.Completed
-=
handler;
countText
=
;
countDrama
=
;
content.Text
=
string
.Empty;
timer.Stop();
if
(Hiding
!=
null
) { Hiding(
this
,
null
); }
};
storyboard.Begin();
}
public
override
void
AdaptiveWindowSize() {
try
{
topRect.Width
=
middleRect.Width
=
bottomRect.Width
=
Application.Current.Host.Content.ActualWidth;
topRect.Height
=
Application.Current.Host.Content.ActualHeight
/
14
;
middleRect.Height
=
Application.Current.Host.Content.ActualHeight;
bottomRect.Height
=
Application.Current.Host.Content.ActualHeight
*
1
/
5
;
tip.Width
=
Application.Current.Host.Content.ActualWidth
*
1
/
5
-
15
;
content.Width
=
Application.Current.Host.Content.ActualWidth
*
4
/
5
-
15
;
Canvas.SetLeft(closer, Application.Current.Host.Content.ActualWidth
-
closer.RealWidth
-
15
);
Canvas.SetLeft(content, Application.Current.Host.Content.ActualWidth
*
1
/
5
);
Canvas.SetTop(avatar,
-
avatar.RealHeight);
Canvas.SetTop(bottomCanvas, Application.Current.Host.Content.ActualHeight
*
4
/
5
);
}
catch
{ }
}
}