-
Notifications
You must be signed in to change notification settings - Fork 16
/
ChatBubble.cs
181 lines (151 loc) · 6.34 KB
/
ChatBubble.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using EndlessClient.GameExecution;
using EOLib;
using EOLib.Config;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Optional;
using XNAControls;
namespace EndlessClient.Rendering.Chat
{
//todo: clear message when IHaveChatBubble dies
public class ChatBubble : DrawableGameComponent, IChatBubble
{
private readonly IMapActor _parent;
private readonly IChatBubbleTextureProvider _chatBubbleTextureProvider;
private readonly IConfigurationProvider _configurationProvider;
private readonly SpriteBatch _spriteBatch;
private readonly XNALabel _textLabel;
private bool _isGroupChat;
private Vector2 _drawLocation;
private Option<DateTime> _startTime;
public ChatBubble(IMapActor referenceRenderer,
IChatBubbleTextureProvider chatBubbleTextureProvider,
IEndlessGameProvider gameProvider,
IConfigurationProvider configurationProvider)
: base((Game)gameProvider.Game)
{
_parent = referenceRenderer;
_chatBubbleTextureProvider = chatBubbleTextureProvider;
_configurationProvider = configurationProvider;
_spriteBatch = new SpriteBatch(((Game)gameProvider.Game).GraphicsDevice);
_textLabel = new XNALabel(Constants.FontSize08pt5)
{
Visible = false,
TextWidth = 150,
ForeColor = Color.Black,
AutoSize = true,
Text = string.Empty,
DrawOrder = 30,
KeepInClientWindowBounds = false,
};
_drawLocation = Vector2.Zero;
_startTime = Option.None<DateTime>();
DrawOrder = 29;
Visible = false;
}
public override void Initialize()
{
_textLabel.Initialize();
if (!_textLabel.Game.Components.Contains(_textLabel))
_textLabel.Game.Components.Add(_textLabel);
base.Initialize();
}
public void SetMessage(string message, bool isGroupChat)
{
if (!_configurationProvider.ShowChatBubbles)
return;
_isGroupChat = isGroupChat;
_textLabel.Text = message;
Visible = true;
_textLabel.Visible = true;
_startTime = Option.Some(DateTime.Now);
}
public override void Update(GameTime gameTime)
{
SetLabelDrawPosition();
_drawLocation = _textLabel.DrawPosition - new Vector2(
_chatBubbleTextureProvider.ChatBubbleTextures[ChatBubbleTexture.TopLeft].Width,
_chatBubbleTextureProvider.ChatBubbleTextures[ChatBubbleTexture.TopLeft].Height);
_startTime.MatchSome(st =>
{
if ((DateTime.Now - st).TotalMilliseconds > Constants.ChatBubbleTimeout)
{
_textLabel.Visible = false;
Visible = false;
_startTime = Option.None<DateTime>();
}
});
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
var TL = GetTexture(ChatBubbleTexture.TopLeft);
var TM = GetTexture(ChatBubbleTexture.TopMiddle);
var TR = GetTexture(ChatBubbleTexture.TopRight);
var ML = GetTexture(ChatBubbleTexture.MiddleLeft);
var MM = GetTexture(ChatBubbleTexture.MiddleMiddle);
var MR = GetTexture(ChatBubbleTexture.MiddleRight);
var BL = GetTexture(ChatBubbleTexture.BottomLeft);
var BM = GetTexture(ChatBubbleTexture.BottomMiddle);
var BR = GetTexture(ChatBubbleTexture.BottomRight);
var NUB = GetTexture(ChatBubbleTexture.Nubbin);
var xCov = TL.Width;
var yCov = TL.Height;
var color = _isGroupChat ? Color.Tan : Color.FromNonPremultiplied(255, 255, 255, 232);
_spriteBatch.Begin();
//top row
_spriteBatch.Draw(TL, _drawLocation, color);
int xCur;
for (xCur = xCov; xCur < _textLabel.ActualWidth + 6; xCur += TM.Width)
{
_spriteBatch.Draw(TM, _drawLocation + new Vector2(xCur, 0), color);
}
_spriteBatch.Draw(TR, _drawLocation + new Vector2(xCur, 0), color);
//middle area
int y;
for (y = yCov; y < _textLabel.ActualHeight; y += ML.Height)
{
_spriteBatch.Draw(ML, _drawLocation + new Vector2(0, y), color);
int x;
for (x = xCov; x < xCur; x += MM.Width)
{
_spriteBatch.Draw(MM, _drawLocation + new Vector2(x, y), color);
}
_spriteBatch.Draw(MR, _drawLocation + new Vector2(xCur, y), color);
}
//bottom row
_spriteBatch.Draw(BL, _drawLocation + new Vector2(0, y), color);
int x2;
for (x2 = xCov; x2 < xCur; x2 += BM.Width)
{
_spriteBatch.Draw(BM, _drawLocation + new Vector2(x2, y), color);
}
_spriteBatch.Draw(BR, _drawLocation + new Vector2(x2, y), color);
y += BM.Height;
_spriteBatch.Draw(NUB, _drawLocation + new Vector2((x2 + BR.Width - NUB.Width)/2f, y - 1), color);
_spriteBatch.End();
}
private void SetLabelDrawPosition()
{
_textLabel.DrawPosition = new Vector2(
_parent.DrawArea.X + _parent.DrawArea.Width / 2.0f - _textLabel.ActualWidth / 2.0f,
_parent.TopPixelWithOffset - _textLabel.ActualHeight - (GetTexture(ChatBubbleTexture.TopMiddle).Height * 5));
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (Game.Components != null && Game.Components.Contains(this))
Game.Components.Remove(this);
_textLabel.Dispose();
}
}
private Texture2D GetTexture(ChatBubbleTexture whichTexture) =>
_chatBubbleTextureProvider.ChatBubbleTextures[whichTexture];
}
public interface IChatBubble : IDisposable
{
void SetMessage(string message, bool isGroupChat);
}
}