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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
%% ------------------------------------------------------------
%% A rubberbanding example in Erlang
%% ------------------------------------------------------------
-module(rubber).
-compile([{nowarn_deprecated_function,{gs,button,2}},
{nowarn_deprecated_function,{gs,canvas,2}},
{nowarn_deprecated_function,{gs,config,2}},
{nowarn_deprecated_function,{gs,create,3}},
{nowarn_deprecated_function,{gs,destroy,1}},
{nowarn_deprecated_function,{gs,radiobutton,2}},
{nowarn_deprecated_function,{gs,start,0}},
{nowarn_deprecated_function,{gs,window,2}}]).
-export([start/0, init/0]).
start() ->
spawn(rubber,init,[]).
init() ->
I=gs:start(),
Win = gs:window(I,[{title,"Rubberbanding in Erlang"},
{width,300},{height,200}]),
C= gs:canvas(Win,[{width,300},{height,200},{bg,green}]),
gs:radiobutton(Win,[{width,100},{label,{text,"Oval"}},{align,w},
{data,oval},{y,0},{select,true}]),
gs:radiobutton(Win,[{width,100},{label,{text,"Rectangle"}},{align,w},
{data,rectangle},{y,30}]),
gs:radiobutton(Win,[{width,100},{label,{text,"Line"}},{align,w},
{data,line},{y,60}]),
gs:button(Win,[{label,{text,"Quit"}},{data,quit},{y,90}]),
gs:config(Win,[{motion,true},{buttonpress,true},{buttonrelease,true}]),
gs:config(Win,{map,true}),
loop(C,nil,oval,0,0).
loop(Win,Obj,Objtype,X1,Y1) ->
receive
{gs,_,motion,_,[X2,Y2]} ->
flush(Win,Obj,Objtype,X1,Y1,X2,Y2);
{gs,_,buttonpress,_,[1,X2,Y2]} ->
down(Win,Obj,Objtype,X2,Y2);
{gs,_,buttonrelease,_,[1,X2,Y2]} ->
up(Win,Obj,Objtype,X1,Y1,X2,Y2);
{gs,_,click,quit,_} ->
exit(normal);
{gs,_,click,Newtype,_} ->
loop(Win,Obj,Newtype,X1,Y1);
{gs,_,destroy,_,_} ->
exit(normal);
Other ->
io:format("Other:~w~n",[Other]),
loop(Win,Obj,Objtype,X1,Y1)
end.
down(Win,nil,oval,X1,Y1) ->
Obj = gs:create(oval,Win,[{coords,[{X1,Y1},{X1+1,Y1+1}]},{fill,red}]),
loop(Win,Obj,oval,X1,Y1);
down(Win,nil,line,X1,Y1) ->
Obj = gs:create(line,Win,[{coords,[{X1,Y1},{X1+1,Y1+1}]},{width,3},{fg,black}]),
loop(Win,Obj,line,X1,Y1);
down(Win,nil,Objtype,X1,Y1) ->
Obj = gs:create(Objtype,Win,[{coords,[{X1,Y1},{X1+1,Y1+1}]},{bw,2},{fg,blue}]),
loop(Win,Obj,Objtype,X1,Y1);
down(Win,Obj,Objtype,X1,Y1) ->
gs:destroy(Obj),
down(Win,nil,Objtype,X1,Y1).
up(Win,nil,Objtype,_X1,_Y1,X2,Y2) ->
loop(Win,nil,Objtype,X2,Y2);
up(Win,Obj,Objtype,X1,Y1,X2,Y2) ->
gs:config(Obj,{coords,[{X1,Y1},{X2,Y2}]}),
loop(Win,nil,Objtype,X2,Y2).
move(Win,nil,Objtype,X1,Y1,_X2,_Y2) ->
loop(Win,nil,Objtype,X1,Y1);
move(Win,Obj,Objtype,X1,Y1,X2,Y2) ->
gs:config(Obj,{coords,[{X1,Y1},{X2,Y2}]}),
loop(Win,Obj,Objtype,X1,Y1).
flush(Win,Obj,Objtype,X1,Y1,X2,Y2) ->
receive
{gs,_,motion,_,[XX2,YY2]} ->
flush(Win,Obj,Objtype,X1,Y1,XX2,YY2)
after
0 -> move(Win,Obj,Objtype,X1,Y1,X2,Y2)
end.
%% ------------------------------------------------------------
|