-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagrammer.pl
248 lines (230 loc) · 6.17 KB
/
diagrammer.pl
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
:- module(diagrammer , [
chatroom_loop/1,
diagrammer//0
]).
:- use_module(library(http/json)).
:- use_module(library(http/hub)).
:- use_module(library(http/http_path)).
:- use_module(library(http/html_head)).
:- use_module(library(http/js_write)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(dcg/basics)).
:- dynamic
node/1, % drawing element
visitor/1, % joined visitors
arc/2.
:- html_resource(js('diagrammer.js'),[requires(js('jquery-2.0.3.min.js'))]).
%% chatroom_loop(+Room)
%
% Realise the chatroom main loop: listen for an event, update the
% state and possibly broadcast status updates.
chatroom_loop(Room) :-
thread_get_message(Room.queues.event, Message),
handle_message(Message, Room),
chatroom_loop(Room).
%% handle_message(+Message, +Room) is det
%
% Handed a dict with a hub message, and a
% dict that defines the Room, handles the message.
%
% add an item (eventually do other things that can be incrementally
% updated
handle_message(Message, Room) :-
websocket{opcode:text, data:String} :< Message,
read_term_from_atom(String, Term, []),
phrase(broadcast_update(Term), CodesToBrowser),
atom_codes(AtomToBrowser, CodesToBrowser),
debug(diagrammer, 'AtomToBrowser ~w, ~w', [String, AtomToBrowser]),
hub_broadcast(Room.name, Message.put(data, AtomToBrowser)).
% someone joined
% we are sending all in one hub_send here, but it's not clear that
% we have to. I did this while looking for a thread starve issue, and
% it's better architecture, so I'm leaving it.
handle_message(Message, _Room) :-
hub{joined:Id} :< Message, !,
phrase(joined_update, CodesUpdate),
atom_codes(AtomUpdate, CodesUpdate),
assertz(visitor(Id)),
hub_send(Id, text(AtomUpdate)).
handle_message(Message, _Room) :-
hub{left:Id} :< Message, !,
retractall(visitor(Id)).
handle_message(Message, _Room) :-
debug(chat, 'Ignoring message ~p', [Message]).
% arc draw case
broadcast_update(commit(_, DownX, DownY, X, Y, 2)) -->
{
number(DownX),
number(DownY),
node_hit(DownX, DownY, Node),
node_hit(X, Y, EndNode),
Node =.. [_, ID, _, _],
EndNode =.. [_, EndID, _, _],
assertz(arc(ID, EndID))
},
rebuild_from_scratch.
% move case
broadcast_update(commit(_, DownX, DownY, X, Y, 0)) -->
{
number(DownX),
number(DownY),
node_hit(DownX, DownY, Node),
Node =.. [Functor, ID, OldX, OldY],
NewX is DownX - OldX + X,
NewY is DownY - OldY + Y,
NNode =.. [Functor, ID, NewX, NewY],
retractall(node(Node)),
assertz(node(NNode))
},
rebuild_from_scratch.
% new case
broadcast_update(commit(rect, _DownX, _DownY, X, Y, 0)) -->
{
number(X),
number(Y),
gensym(node, ID),
assertz(node(rect(ID,X,Y)))
},
"ddd.addRect(",
number(X),
", ",
number(Y),
");".
broadcast_update(commit(oval, _DownX, _DownY, X, Y, 0)) -->
{
number(X),
number(Y),
gensym(node, ID),
assertz(node(oval(ID,X,Y)))
},
"ddd.addOval(",
number(X),
", ",
number(Y),
");".
broadcast_update(commit(diamond, _DownX, _DownY, X, Y, 0)) -->
{
number(X),
number(Y),
gensym(node, ID),
assertz(node(diamond(ID,X,Y)))
},
"ddd.addDiamond(",
number(X),
", ",
number(Y),
");".
joined_update --> rebuild_from_scratch.
rebuild_from_scratch -->
{
findall(X, node(X), Bag),
findall(arc(A,B), arc(A,B), ArcBag)
},
"ddd.clear();",
arcs(ArcBag),
joined_update_adds(Bag).
arcs([]) --> [],!.
arcs(List) -->
"ddd.ctx().setTransform(1,0,0,1,0,0);",
"ddd.ctx().beginPath();",
arc_list(List),
"ddd.ctx().closePath(); ddd.strokeOnly();".
arc_list([]) --> [].
arc_list([arc(A,B) | T]) -->
{
node(S),
S =.. [_, A, AX, AY],
node(E),
E =.. [_, B, BX, BY]
},
"ddd.ctx().moveTo(",
number(AX),
",",
number(AY),
"); ddd.ctx().lineTo(",
number(BX),
",",
number(BY),
");",
arc_list(T).
joined_update_adds([rect(_ID, X, Y) | T]) -->
"ddd.addRect(",
number(X),
",",
number(Y),
");",
joined_update_adds(T).
joined_update_adds([oval(_ID, X, Y) | T]) -->
"ddd.addOval(",
number(X),
",",
number(Y),
");",
joined_update_adds(T).
joined_update_adds([diamond(_ID, X, Y) | T]) -->
"ddd.addDiamond(",
number(X),
",",
number(Y),
");",
joined_update_adds(T).
joined_update_adds([]) --> [].
%% diagrammer(?A, ?B) is det
%
% DCG diagram widget
%
diagrammer -->
{
http_absolute_location(img('rect.png'), RectLoc, []),
http_absolute_location(img('oval.png'), OvalLoc, []),
http_absolute_location(img('diamond.png'), DiamondLoc, []),
http_absolute_location(img('text.png'), TextLoc, [])
},
html_requires(css('diagrammer.css')),
html_requires(js('jquery-2.0.3.min.js')),
html([
div(id(diagrammer), [
div([class(componentbar)], [
img([id(rect_tool), src(RectLoc)]),
img([class(selected), id(oval_tool), src(OvalLoc)]),
img([id(diamond_tool), src(DiamondLoc)]),
img([id(text_tool), src(TextLoc)])
]),
div([class('canvas-container')], [
canvas([class(drawarea),
width('1000'), height('612')], []),
input([type(text), id(dia_textentry)], []) ])
% http://stackoverflow.com/questions/17034795/html-canvas-scale
]),
p(id(msg), 'message area'),
p(id(output), 'output area')
]),
script.
%% script//
%
% Generate the JavaScript that establishes the websocket and
% handles events on the websocket.
script -->
html_requires(js('diagrammer.js')),
{ http_link_to_id(chat_websocket, [], WebSocketURL)
},
js_script({|javascript(WebSocketURL)||
$(document).ready(function() {
ws_initialize(WebSocketURL);
});
|}).
node_hit(X, Y, rect(ID, RX, RY)) :-
node(rect(ID, RX, RY)),
X >= RX - 50,
X =< RX + 50,
Y >= RY - 37.5,
Y =< RY + 37.5.
node_hit(X, Y, diamond(ID, RX, RY)) :-
node(diamond(ID, RX, RY)),
DX is abs(X - RX),
DY is abs(Y - RY),
DY < 37.5 - DX * 37.5 / 50.0.
node_hit(X, Y, oval(ID, RX, RY)) :-
node(oval(ID, RX, RY)),
37.5 * 37.5 > (X - RX)*(X - RX) + (Y - RY)*(Y - RY).