-
Notifications
You must be signed in to change notification settings - Fork 13
/
gui1.erl
114 lines (94 loc) · 3.02 KB
/
gui1.erl
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
%% Copyright (c) 2011 Joe Armstrong
%% See MIT-LICENSE for licensing information.
%% Time-stamp: <2011-02-14 14:54:12 joe>
-module(gui1).
%% First attempt at a middle man
%% This demonstrates dynamically sending GUI objects to a window
%% and pushing text and colored things to the browser
-compile(export_all).
start(Pid) ->
S = self(),
Logic = spawn_link(fun() -> logic(S) end),
mm(Pid, Logic).
%%----------------------------------------------------------------------
logic(Pid) ->
Pid ! clear_window,
Pid ! {bg, "orange"},
Pid ! {add_button, "Click me", "zapit"},
Pid ! {add_div,"a"},
Pid ! {add_canvas,"c"},
Pid ! {add_random_rectangle,"c"},
random_seed(),
spawn(fun() -> ticker(Pid, 0) end),
logic_loop(Pid).
ticker(Pid, N) ->
receive
after 250 ->
true
end,
Pid ! {add_txt_to_div, "a", "Message number " ++ i2s(N)},
Pid ! {add_random_rectangle, "c"},
ticker(Pid, N+1).
logic_loop(Pid) ->
receive
Any ->
io:format("event loop Msg => ~p~n",[Any]),
logic_loop(Pid)
end.
%%----------------------------------------------------------------------
mm(JS, Erlang) ->
receive
{add_canvas, Name} ->
JS ! {eval, add_canvas(Name)};
{add_random_rectangle, Name} ->
JS ! {eval, add_random_rectangle(Name)};
{add_div, Name} ->
JS ! {eval, add_div(Name)};
{add_txt_to_div, Name, Txt} ->
JS ! {eval, add_txt_to_div(Name, Txt)};
{add_button, Txt, Msg} ->
JS ! {eval,button(Txt,Msg)};
clear_window ->
JS ! {eval,"document.body.innerHTML=''"};
{bg, C} ->
JS ! {eval, ["document.body.style.backgroundColor='", C, "';"]};
{add_text, S} ->
JS ! {eval, ["document.body.innerHTML +='", S, "';"]};
X ->
io:format("Dropping:~p~n",[X])
end,
mm(JS, Erlang).
add_div(X) ->
["document.body.innerHTML+=\"<p><div style='border:1px solid black' id='",X,"'></div>\";"].
add_canvas(X) ->
["document.body.innerHTML+=\"<p><canvas width=500 height=200 id='",X,"'></canvas>\";",
X,"= document.getElementById('",X,"').getContext('2d');"].
add_txt_to_div(X, Y) ->
["document.getElementById('",X,"').innerHTML = '",Y,"';"].
button(Txt, Msg) ->
["document.body.innerHTML+=",
"\"<button onclick='send(\\\"",Msg,"\\\")'>",Txt,"</button>\";"].
i2s(X) ->
integer_to_list(X).
rgb(R,G,B) ->
["'rgb(",i2s(R),",",i2s(G),",",i2s(B),")';"].
fillRect(X,Y,W,H) ->
["fillRect(",i2s(X),",",i2s(Y),",",i2s(W),",",i2s(H),")"].
add_random_rectangle(Name) ->
%% "c.fillStyle = 'rgb(255,0,0)';c.fillRect(30, 30, 50, 50);".
R=random:uniform(255),
G=random:uniform(255),
B=random:uniform(255),
X=100+random:uniform(100),
Y=100+random:uniform(100),
W=random:uniform(150),
H=random:uniform(150),
%% "c.fillStyle = 'rgb(255,0,0)';c.fillRect(30, 30, 50, 50);".
[Name,".fillStyle=",rgb(R,G,B),Name,".",fillRect(X,Y,W,H),";"].
random_seed() ->
{_,_,X} = erlang:now(),
{H,M,S} = time(),
H1 = H * X rem 32767,
M1 = M * X rem 32767,
S1 = S * X rem 32767,
put(random_seed, {H1,M1,S1}).