author | unc0rr |
Mon, 13 Apr 2009 09:27:21 +0000 | |
changeset 1978 | 8ab9949c8775 |
parent 1821 | 6b6cf3389f92 |
child 2003 | 41b3d00949ca |
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 |
1378 | 135 |
if (s[1] = '/') and (copy(s, 1, 4) <> '/me ') then |
1035 | 136 |
begin |
137 |
if CurrentTeam^.ExtDriven then exit; |
|
138 |
||
139 |
for i:= Low(TWave) to High(TWave) do |
|
140 |
if (s = Wavez[i].cmd) then |
|
141 |
begin |
|
142 |
ParseCommand('/taunt ' + char(i), true); |
|
143 |
exit |
|
144 |
end; |
|
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
145 |
if (s = '/newgrave') then |
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
146 |
ParseCommand('/newgrave', true); |
1035 | 147 |
end |
148 |
else |
|
149 |
ParseCommand('/say ' + s, true); |
|
1033 | 150 |
end; |
151 |
||
946 | 152 |
procedure KeyPressChat(Key: Longword); |
153 |
const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0); |
|
154 |
var i, btw: integer; |
|
1001 | 155 |
utf8: shortstring; |
946 | 156 |
begin |
1819 | 157 |
|
946 | 158 |
if Key <> 0 then |
159 |
case Key of |
|
1819 | 160 |
{Backspace} |
161 |
8, 127: if Length(InputStr.s) > 0 then |
|
946 | 162 |
begin |
163 |
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
|
164 |
SetLine(InputStr, InputStr.s, true) |
946 | 165 |
end; |
1819 | 166 |
{Esc} |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
167 |
27: SetLine(InputStr, '', true); |
1819 | 168 |
{Return} |
948 | 169 |
13, 271: begin |
947 | 170 |
if Length(InputStr.s) > 0 then |
171 |
begin |
|
1033 | 172 |
AcceptChatString(InputStr.s); |
1118 | 173 |
SetLine(InputStr, '', false) |
947 | 174 |
end; |
948 | 175 |
FreezeEnterKey; |
946 | 176 |
GameState:= gsGame |
1819 | 177 |
end; |
178 |
{missing Mac keys} |
|
179 |
{* |
|
180 |
63272 is canc |
|
181 |
63232 is UP |
|
182 |
63234 is LEFT |
|
183 |
63235 is RIGHT |
|
184 |
63233 is DOWN |
|
185 |
*} |
|
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
186 |
63272, 63232, 36233, 36234, 36235: Key:= 28; |
946 | 187 |
else |
188 |
if (Key < $80) then btw:= 1 |
|
189 |
else if (Key < $800) then btw:= 2 |
|
190 |
else if (Key < $10000) then btw:= 3 |
|
191 |
else btw:= 4; |
|
192 |
||
193 |
utf8:= ''; |
|
194 |
||
195 |
for i:= btw downto 2 do |
|
196 |
begin |
|
197 |
utf8:= char((Key or $80) and $BF) + utf8; |
|
198 |
Key:= Key shr 6 |
|
199 |
end; |
|
200 |
||
201 |
utf8:= char(Key or firstByteMark[btw]) + utf8; |
|
202 |
||
1485
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
203 |
if byte(InputStr.s[0]) + btw > 240 then exit; |
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
204 |
|
946 | 205 |
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
|
206 |
SetLine(InputStr, InputStr.s + utf8, true) |
946 | 207 |
end |
208 |
end; |
|
209 |
||
210 |
||
942 | 211 |
end. |