summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/third/chat/t-chat.mkiv
blob: b3e1933549ed057e9c92423a748d22ce2ad12879 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
%D \module
%D   [       file=t-chat,
%D        version=2024.08.10,
%D          title=\CONTEXT\ User Module,
%D       subtitle=Chat,
%D         author=Henning Hraban Ramm,
%D           date=\currentdate,
%D      copyright={fiëé visuëlle, H. Ramm},
%D        license={GPL 3.0+}]

\unprotect

%D First we define a few colors.
%D Don’t rely on these names, use the “Chat” palet instead.
\definecolor[ChatColorI][c=0,m=.03,y=.15,k=0]
\definecolor[ChatColorII][c=.1,m=0,y=0,k=0]
\definecolor[ChatNameColor][c=0,m=0,y=0,k=.1]
\definecolor[ChatColor][ChatColorI]
\definecolor[ChatFrameColor][ChatNameColor]

%D The palet contains all used colors.
%D ATM there are only 2 different colors for recipient and sender,
%D there should be more colors to distinguish senders.
\definepalet[Chat][
  frame=ChatFrameColor,
  name=ChatNameColor,
  1=ChatColorI,
  2=ChatColorII,
]

\installcorenamespace {chat}

\installsimplecommandhandler \??chat {Chat} \??chat

\setupChat[
  recipient=,
  person=,
  \c!distance=.25em,
  \c!style={\ss},
  \c!width=.8\textwidth,
  senderstyle={\ss\bf},
]

% \Chatparameter

\startuniqueMPgraphic{ChatBubble}
% side: 0=right, 1=left
% pos: 0 solo, 1 top, 2 middle, 3 bottom

  numeric side, pos ;
  string bgcol ;

  if "\getvariable{Chat}{Person}" = "\Chatparameter{recipient}" :
      side := 0;
      bgcol := "Chat:1";
  ; else :
      side := 1;
      bgcol := "Chat:2";
  ; fi ;

  if "\getvariable{Chat}{Person}" = "\getvariable{Chat}{PrevPerson}" :
    % same as previous person: omit tail
      pos := 3;
  ; else :
    % since we can’t know what’s next, go with "solo"
      pos := 0;
  ; fi ;

  cradius := .125cm;
  tailoffset := .125cm;
  tailsize := .25cm;
  h := OverlayHeight;
  w := OverlayWidth;

  if h < (2*cradius + 2*tailsize):
    % if the box is too small, reduce radius and tail size
    cradius := h/3;
    tailsize := h/3;
    tailoffset := 0;
  fi;

  pair ll[],lr[],tl[],tr[],s[];

  % corners
  ll1 := (0,cradius);
  ll2 := (cradius,0);
  lr1 := (w,cradius);
  lr2 := (w-cradius,0);
  % tail points
  tl1 := (0,h-cradius);
  tl2 := (cradius,h);
  tr1 := (w,h-cradius);
  tr2 := (w-cradius,h);

  s1 := (w,h-cradius-tailoffset);
  s2 := (w+tailsize,h-cradius-tailoffset);
  s3 := (w,h-cradius-tailoffset-tailsize);

  if pos > 1: % disable tail on middle/bottom
    %s1 := (w,h-cradius-tailoffset);
    s2 := (w,h-cradius-tailoffset);
    %s3 := (w,h-cradius-tailoffset-tailsize);
  fi;
  if (pos = 1) or (pos = 2): % top/middle
    lr1 := (w,cradius/3);
    lr2 := (w-cradius/3,0);
  fi;
  if (pos = 2) or (pos = 3): % middle/bottom
    tr1 := (w,h-cradius/3);
    tr2 := (w-cradius/3,h);
  fi;

  path p ;
  p := (ll1 ... ll2 --- lr2 ... lr1 -- s3 -- s2 -- s1 -- tr1 ... tr2 --- tl2 ... tl1 --- cycle);

  % mirror the bubble for left side
  if side = 1:
    p := p reflectedabout ((w/2,0),(w/2,h));
  fi;

  fill p withcolor bgcol ;
  draw p withpen pencircle scaled 0.75 withcolor "Chat:frame";

  setbounds currentpicture to OverlayBox ;
\stopuniqueMPgraphic

\defineframedtext[ChatBubbleFrame][
  style=\Chatparameter\s!style,
  align=flushleft,
  frame=off,
  width=\Chatparameter\s!width,
  autowidth=force,
  offset=0pt,
  loffset=2pt,
  roffset=3pt,
  before={\vskip\dimexpr-1em+\Chatparameter\c!distance\relax},
  after={\vskip\dimexpr-\Chatparameter\c!distance\relax},
]

%D Receive text (left-facing).
\defineoverlay[ChatGetOverlay][\uniqueMPgraphic{ChatBubble}]
\defineframedtext[ChatGetFrame][ChatBubbleFrame][
  background=ChatGetOverlay,
  location=left,
]

%D Send text (right-facing).
\defineoverlay[ChatSendOverlay][\uniqueMPgraphic{ChatBubble}]
\defineframedtext[ChatSendFrame][ChatBubbleFrame][
  background=ChatSendOverlay,
  location=right,
]

%D This should become an avatar
%D or simply initials in a circle
\defineframed[ChatSenderFramed][
  width=1em,
  offset=overlay,
  frame=off,
  style=\Chatparameter{senderstyle},
  %offset=0pt,
  %loffset=0.25em,
  %roffset=0.25em,
  %corner=rounded,
  %frameradius=.5em,
  %background=color,
  %backgroundcolor=Chat:frame,
]

%D Only show names (avatars) if person is different from last one
% Sender name: #1 x-offset
\define[1]\ChatSender{\offset[x=#1]{%
  \doifnot{\getvariable{Chat}{Person}}{\getvariable{Chat}{PrevPerson}}{
    % TODO: style \ss\tfx if more than 2 chars
  \ChatSenderFramed{\style[style=\Chatparameter{senderstyle}]{\getvariable{Chat}{Person}}}}%
  }%
}

\define[1]\startChat{%
  \setevariable{Chat}{PrevPerson}{\getvariable{Chat}{Person}}
  \setevariable{Chat}{Person}{#1}
  \testpage[1]
  \doifelse{\getvariable{Chat}{Person}}{\Chatparameter{recipient}}{
    % recipient
    \ChatSender{\dimexpr\textwidth-1.25em\relax}
    \startnarrow[left=.5em]\startChatSendFrame
  }{
    % other sender
    \ChatSender{-0.25em}
    \startnarrow[right=.5em]\startChatGetFrame
  }
}

\def\resetChat{%
  \setevariable{Chat}{Recipient}{}%
  \setevariable{Chat}{Person}{}%
  \setevariable{Chat}{PrevPerson}{0}%
}

\def\stopChat{
\doifelse{\getvariable{Chat}{Person}}{\Chatparameter{recipient}}{
  % recipient
  \stopChatSendFrame%
}{
  % other sender
  \stopChatGetFrame%
}
\stopnarrow%
}

\resetChat

\protect

\continueifinputfile{t-chat.mkiv}

\starttext

\setupChat[
  recipient=A,
]

\starttext
\startChat{A} Do you wanna chat?\stopChat
\startChat{B} Maybe. What about?\stopChat
\startChat{A} How about cats?\stopChat
\startChat{A} or squirrels?\stopChat
\stoptext


\stoptext