author | unc0rr |
Thu, 30 Apr 2009 20:16:58 +0000 | |
changeset 2019 | b3f1eda8865f |
parent 2017 | 7845c77c8d31 |
child 2111 | 9be70514954c |
permissions | -rw-r--r-- |
942 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
942 | 3 |
* Copyright (c) 2008 Andrey Korotaev <unC0Rr@gmail.com> |
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
19 |
unit uChat; |
|
20 |
||
21 |
interface |
|
22 |
||
23 |
procedure AddChatString(s: shortstring); |
|
24 |
procedure DrawChat; |
|
946 | 25 |
procedure KeyPressChat(Key: Longword); |
942 | 26 |
|
949 | 27 |
var UserNick: shortstring = ''; |
993 | 28 |
showAll: boolean = false; |
949 | 29 |
|
942 | 30 |
implementation |
1035 | 31 |
uses uMisc, uStore, uConsts, SDLh, uConsole, uKeys, uTeams; |
942 | 32 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
33 |
const MaxStrIndex = 27; |
942 | 34 |
|
946 | 35 |
type TChatLine = record |
1431 | 36 |
Tex: PTexture; |
942 | 37 |
Time: Longword; |
1431 | 38 |
Width: LongInt; |
39 |
s: shortstring; |
|
942 | 40 |
end; |
41 |
||
946 | 42 |
var Strs: array[0 .. MaxStrIndex] of TChatLine; |
942 | 43 |
lastStr: Longword = 0; |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
44 |
visibleCount: Longword = 0; |
946 | 45 |
|
46 |
InputStr: TChatLine; |
|
47 |
InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char |
|
942 | 48 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
49 |
procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean); |
1118 | 50 |
var strSurface, resSurface: PSDL_Surface; |
1431 | 51 |
w, h: LongInt; |
942 | 52 |
begin |
1118 | 53 |
if cl.Tex <> nil then |
54 |
FreeTexture(cl.Tex); |
|
942 | 55 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
56 |
cl.s:= str; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
57 |
|
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
58 |
if isInput then str:= UserNick + '> ' + str + '_'; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
59 |
|
1118 | 60 |
TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(str), w, h); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
61 |
|
1118 | 62 |
resSurface:= SDL_CreateRGBSurface(0, |
1431 | 63 |
toPowerOf2(w), |
64 |
toPowerOf2(h), |
|
1118 | 65 |
32, |
66 |
RMask, GMask, BMask, AMask); |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
67 |
|
1118 | 68 |
strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF); |
1431 | 69 |
cl.Width:= w + 4; |
1118 | 70 |
SDL_UpperBlit(strSurface, nil, resSurface, nil); |
71 |
SDL_FreeSurface(strSurface); |
|
72 |
||
73 |
cl.Time:= RealTicks + 12500; |
|
74 |
cl.Tex:= Surface2Tex(resSurface); |
|
1431 | 75 |
|
1118 | 76 |
SDL_FreeSurface(resSurface) |
946 | 77 |
end; |
78 |
||
79 |
procedure AddChatString(s: shortstring); |
|
80 |
begin |
|
81 |
lastStr:= (lastStr + 1) mod (MaxStrIndex + 1); |
|
82 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
83 |
SetLine(Strs[lastStr], s, false); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
84 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
85 |
inc(visibleCount) |
942 | 86 |
end; |
87 |
||
88 |
procedure DrawChat; |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
89 |
var i, t, cnt: Longword; |
1431 | 90 |
r: TSDL_Rect; |
942 | 91 |
begin |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
92 |
cnt:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
93 |
t:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
94 |
i:= lastStr; |
1431 | 95 |
|
96 |
r.x:= 6; |
|
97 |
r.y:= (visibleCount - t) * 16 + 10; |
|
98 |
r.h:= 16; |
|
99 |
||
100 |
if (GameState = gsChat) |
|
101 |
and (InputStr.Tex <> nil) then |
|
102 |
begin |
|
103 |
r.w:= InputStr.Width; |
|
104 |
DrawFillRect(r); |
|
105 |
DrawTexture(8, visibleCount * 16 + 10, InputStr.Tex); |
|
106 |
end; |
|
107 |
||
108 |
dec(r.y, 16); |
|
109 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
110 |
while |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
111 |
( |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
112 |
((t < 7) and (Strs[i].Time > RealTicks)) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
113 |
or |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
114 |
((t < MaxStrIndex) and showAll) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
115 |
) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
116 |
and |
1118 | 117 |
(Strs[i].Tex <> nil) do |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
118 |
begin |
1431 | 119 |
r.w:= Strs[i].Width; |
120 |
DrawFillRect(r); |
|
1118 | 121 |
DrawTexture(8, (visibleCount - t) * 16 - 6, Strs[i].Tex); |
1431 | 122 |
dec(r.y, 16); |
123 |
||
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
124 |
if i = 0 then i:= MaxStrIndex else dec(i); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
125 |
inc(cnt); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
126 |
inc(t) |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
127 |
end; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
128 |
|
947 | 129 |
visibleCount:= cnt; |
942 | 130 |
end; |
131 |
||
1033 | 132 |
procedure AcceptChatString(s: shortstring); |
1035 | 133 |
var i: TWave; |
1033 | 134 |
begin |
2017 | 135 |
// "Make hedgehog say something" |
136 |
if (s[1] = '"') and (s[Length(s)] = '"') then |
|
137 |
begin |
|
138 |
ParseCommand('/hogsay '+#1+copy(s, 2, Length(s)-2), true); |
|
139 |
exit |
|
140 |
end; |
|
141 |
// 'Make hedgehog think something' |
|
142 |
if (s[1] = '''') and (s[Length(s)] = '''') then |
|
143 |
begin |
|
144 |
ParseCommand('/hogsay '+#2+copy(s, 2, Length(s)-2), true); |
|
145 |
exit |
|
146 |
end; |
|
147 |
// -Make hedgehog yell something- |
|
148 |
if (s[1] = '-') and (s[Length(s)] = '-') then |
|
149 |
begin |
|
150 |
ParseCommand('/hogsay '+#3+copy(s, 2, Length(s)-2), true); |
|
151 |
exit |
|
152 |
end; |
|
153 |
// These 3 are same as above, only are to make the hedgehog say it on next attack |
|
154 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hsa ') then |
|
155 |
begin |
|
156 |
ParseCommand('/hogsay '+#4+copy(s, 6, Length(s)-5), true); |
|
157 |
exit |
|
158 |
end; |
|
159 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hta ') then |
|
160 |
begin |
|
161 |
ParseCommand('/hogsay '+#5+copy(s, 6, Length(s)-5), true); |
|
162 |
exit |
|
163 |
end; |
|
164 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hya ') then |
|
165 |
begin |
|
166 |
ParseCommand('/hogsay '+#6+copy(s, 6, Length(s)-5), true); |
|
167 |
exit |
|
168 |
end; |
|
1378 | 169 |
if (s[1] = '/') and (copy(s, 1, 4) <> '/me ') then |
1035 | 170 |
begin |
171 |
if CurrentTeam^.ExtDriven then exit; |
|
172 |
||
173 |
for i:= Low(TWave) to High(TWave) do |
|
174 |
if (s = Wavez[i].cmd) then |
|
175 |
begin |
|
176 |
ParseCommand('/taunt ' + char(i), true); |
|
177 |
exit |
|
178 |
end; |
|
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
179 |
if (s = '/newgrave') then |
2017 | 180 |
begin |
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
181 |
ParseCommand('/newgrave', true); |
2017 | 182 |
exit |
183 |
end; |
|
184 |
end |
|
1035 | 185 |
else |
186 |
ParseCommand('/say ' + s, true); |
|
1033 | 187 |
end; |
188 |
||
946 | 189 |
procedure KeyPressChat(Key: Longword); |
190 |
const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0); |
|
191 |
var i, btw: integer; |
|
1001 | 192 |
utf8: shortstring; |
946 | 193 |
begin |
1819 | 194 |
|
946 | 195 |
if Key <> 0 then |
196 |
case Key of |
|
1819 | 197 |
{Backspace} |
198 |
8, 127: if Length(InputStr.s) > 0 then |
|
946 | 199 |
begin |
200 |
InputStr.s[0]:= InputStrL[byte(InputStr.s[0])]; |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
201 |
SetLine(InputStr, InputStr.s, true) |
946 | 202 |
end; |
1819 | 203 |
{Esc} |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
204 |
27: SetLine(InputStr, '', true); |
1819 | 205 |
{Return} |
2003 | 206 |
3, 13, 271: begin |
947 | 207 |
if Length(InputStr.s) > 0 then |
208 |
begin |
|
1033 | 209 |
AcceptChatString(InputStr.s); |
1118 | 210 |
SetLine(InputStr, '', false) |
947 | 211 |
end; |
948 | 212 |
FreezeEnterKey; |
946 | 213 |
GameState:= gsGame |
1819 | 214 |
end; |
215 |
{missing Mac keys} |
|
216 |
{* |
|
217 |
63272 is canc |
|
218 |
63232 is UP |
|
219 |
63234 is LEFT |
|
220 |
63235 is RIGHT |
|
221 |
63233 is DOWN |
|
222 |
*} |
|
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
223 |
63272, 63232, 36233, 36234, 36235: Key:= 28; |
946 | 224 |
else |
225 |
if (Key < $80) then btw:= 1 |
|
226 |
else if (Key < $800) then btw:= 2 |
|
227 |
else if (Key < $10000) then btw:= 3 |
|
228 |
else btw:= 4; |
|
229 |
||
230 |
utf8:= ''; |
|
231 |
||
232 |
for i:= btw downto 2 do |
|
233 |
begin |
|
234 |
utf8:= char((Key or $80) and $BF) + utf8; |
|
235 |
Key:= Key shr 6 |
|
236 |
end; |
|
237 |
||
238 |
utf8:= char(Key or firstByteMark[btw]) + utf8; |
|
239 |
||
1485
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
240 |
if byte(InputStr.s[0]) + btw > 240 then exit; |
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
241 |
|
946 | 242 |
InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0]; |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
243 |
SetLine(InputStr, InputStr.s + utf8, true) |
946 | 244 |
end |
245 |
end; |
|
246 |
||
247 |
||
942 | 248 |
end. |