%donald
#change size of lights

#independently
green/rad = 25

#all together
int radall
yellow/rad = radall
green/rad = radall
red/rad = radall
radall = 25

#inappropriately
radall = 100

#restore original state
radall = 50

%scout
#change background colour for lights

string backcol;
backcol="black";
window donwin = {
    type: DONALD
    box: [{50, 50}, {250, 550}]
    pict: "traflight"
    bgcolor: backcol
        border:1
        xmin: 400
        xmax: 600
        ymin: 300
        ymax: 800
};

backcol="blue";
backcol="red";
backcol="black";

%eden
/* introduce a switching mechanism for lights */

g_on = 1;
r_on = 1;
y_on = 1;
l_off = "color=white";
attr_glight is (g_on)?"color=green":l_off;
attr_rlight is (r_on)?"color=red":l_off;
attr_ylight is (y_on)?"color=yellow":l_off;

A_green_light is "fill=solid,"//attr_glight;
A_red_light is "fill=solid,"//attr_rlight;
A_yellow_light is "fill=solid,"//attr_ylight;

%scout
#make each light a button

window donwin = {
    type: DONALD
    box: [{50, 50}, {250, 550}]
    pict: "traflight"
    bgcolor: backcol
        border:1
        xmin: 400
        xmax: 600
        ymin: 300
        ymax: 800
	sensitive: ON
};

%eden
func incircle {
	para x,y, cx, cy, rad;
	auto result;
	result = ((x-cx)*(x-cx) + (y-cy)*(y-cy) < rad * rad) ? 1:0;
	return result;
};

/* make the red light into a button */
proc redpress: donwin_mouse {
	auto x,y,cx,cy,rad;
	x = donwin_mouse[4];
	y = donwin_mouse[5];
	cx = _red_centre[2];
	cy = _red_centre[3];
	rad = _red_rad;
	if ((donwin_mouse[2]==4)&&(incircle(x,y,cx,cy,rad)))
		r_on = 1 - r_on; 
};

/* make the yellow light into a button */
proc yellowpress: donwin_mouse {
	auto x,y,cx,cy,rad;
	x = donwin_mouse[4];
	y = donwin_mouse[5];
	cx = _yellow_centre[2];
	cy = _yellow_centre[3];
	rad = _yellow_rad;
	if ((donwin_mouse[2]==4)&&(incircle(x,y,cx,cy,rad)))
		y_on = 1 - y_on; 
};

/* make the green light into a button */
proc greenpress: donwin_mouse {
	auto x,y,cx,cy,rad;
	x = donwin_mouse[4];
	y = donwin_mouse[5];
	cx = _green_centre[2];
	cy = _green_centre[3];
	rad = _green_rad;
	if ((donwin_mouse[2]==4)&&(incircle(x,y,cx,cy,rad)))
		g_on = 1 - g_on; 
};

/* introduce time-based behaviour
	tim represents a virtual time, del a virtual delay */

tim = 0;
delay = 100;
proc tick : tim {
	if (tim%delay==0) r_on = 1-r_on;
	todo("tim = tim+1;");
};

/* disable the tick procedure */
proc tick {
};

/* introduce phases of the light cycle as observables */ 

phase = 1;
cyclelight = [[1,0,0], [1,1,0], [0,0,1], [0,1,0]];
r_on is cyclelight[phase][1];
y_on is cyclelight[phase][2];
g_on is cyclelight[phase][3];


proc tick : tim {
        if (tim%delay==0) phase = phase%4+1;
        todo("tim = tim+1;");
};

/* failure of lights */

r_ok = 1;
y_ok=1;
g_ok = 1;
GREEN is (g_ok)?"green":"white";
RED is (r_ok)?"red":"white";
YELLOW is (y_ok)?"yellow":"white";

/* red bulb fails */
r_ok = 0;

attr_glight is (g_on)?"color="//GREEN:gl_off;
attr_rlight is (r_on)?"color="//RED:rl_off;
attr_ylight is (y_on)?"color="//YELLOW:yl_off;

gl_off = l_off;
rl_off = l_off;
yl_off = l_off;

/* green light sticks on */
gl_off = "color=green";

%scout
integer width, height;
width = 200;
height = 5*width/2;
window donwin = {
    type: DONALD
    box: [{50, 50}, {width+50, height+50}]
    pict: "traflight"
    xmin: 400
    ymin: 300
    xmax: 600
    ymax: 800
    bgcolor: backcol
    border: 1
};



proc tick : tim {
        if (tim%delay==0) {
		phase = phase%4+1;
		width = width - 1;
	};
        todo("tim = tim+1;");
};
