File: shape.k

package info (click to toggle)
kaya 0.4.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,448 kB
  • ctags: 1,694
  • sloc: cpp: 9,536; haskell: 7,461; sh: 3,013; yacc: 910; makefile: 816; perl: 90
file content (128 lines) | stat: -rw-r--r-- 3,504 bytes parent folder | download | duplicates (4)
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
cgi shape; // -*-C-*-ish

import CGI;
import primitives;
import Image;

Void PreContent()
{
    content("<html><head><title>Shape Thingy</title></head><body>");
    content("<h1>Shape Thingy</h1>");
}

Void Default()
{
//    prim = Primitive(Rectangle(DrawColour(255,0,0),50,50),80,80);
    drawing = Drawing(480,320,DrawColour(0,0,0),[]);

    showDrawing(drawing);
    addShapeForm(drawing);
}

Void showDrawing(Drawing d) {
    content("<table><tr><td>");
    content(imageHandler(draw,d,"[Pretty picture]",d.x,d.y));
    content("</td><td>");

    idx = 0;
    for p in d.ps {
	case p.shape of {
	    Rectangle(col,rx,ry) -> shape = "Rectangle";
	  | Ellipse(col,ex,ey) -> shape = "Ellipse";
	  | Triangle(col,tb,th) -> shape = "Triangle";
	}
	content(shape+" at ("+p.x+","+p.y+") ");
	content(linkHandler(deleteShape,(d,idx),"Delete")+"<br>");
	idx++;
    }
    content("</td></table>");
}

Void deleteShape((Drawing,Int) didx) {
    d = didx.fst;
    idx = didx.snd;
    
    removeAt(d.ps,idx);
    showDrawing(d);
    addShapeForm(d);    
}

HTML colourBox(String name) 
   = selBox(name,3,["Red","Green","Yellow","Blue","White","Random"]);

Void addShapeForm(Drawing d) {
    content("<h3>Add a shape</h3>");

    content(formHandler(OnAddShape,d));
    content("<table><tr>"+
	    "<td>Rectangle</td>"+
	    "<td>X</td><td>"+textBox("rx","",5)+"</td>"+
	    "<td>Y</td><td>"+textBox("ry","",5)+"</td>"+
	    "<td>Width</td><td>"+textBox("rw","",5)+"</td>"+
	    "<td>Height</td><td>"+textBox("rh","",5)+"</td>"+
	    "<td>Colour</td><td>"+colourBox("rcol")+"</td>"+
	    "<td>"+submit("Add Rectangle")+"</td></tr>");
    content("<tr>"+
	    "<td>Ellipse</td>"+
	    "<td>X</td><td>"+textBox("ex","",5)+"</td>"+
	    "<td>Y</td><td>"+textBox("ey","",5)+"</td>"+
	    "<td>Width</td><td>"+textBox("ew","",5)+"</td>"+
	    "<td>Height</td><td>"+textBox("eh","",5)+"</td>"+
	    "<td>Colour</td><td>"+colourBox("ecol")+"</td>"+
	    "<td>"+submit("Add Ellipse")+"</td></tr>");
    content("<tr>"+
	    "<td>Triangle</td>"+
	    "<td>X</td><td>"+textBox("tx","",5)+"</td>"+
	    "<td>Y</td><td>"+textBox("ty","",5)+"</td>"+
	    "<td>Base</td><td>"+textBox("tb","",5)+"</td>"+
	    "<td>Height</td><td>"+textBox("th","",5)+"</td>"+
	    "<td>Colour</td><td>"+colourBox("tcol")+"</td>"+
	    "<td>"+submit("Add Triangle")+"</td></tr>");
    content("</table>");
    content(closeForm);
}

Void OnAddShape(Drawing d)
{
    try {
	if (submitUsed=="Add Rectangle") {
	    x = httpInt("rx");
	    y = httpInt("ry");
	    w = httpInt("rw");
	    h = httpInt("rh");
	    c = mkColour(incomingValue("rcol",DataPost));
	    shape = Primitive(Rectangle(c,w,h),x,y);
	}
	else if (submitUsed=="Add Ellipse") {
	    x = httpInt("ex");
	    y = httpInt("ey");
	    w = httpInt("ew");
	    h = httpInt("eh");
	    c = mkColour(incomingValue("ecol",DataPost));
	    shape = Primitive(Ellipse(c,w,h),x,y);
	}
	else if (submitUsed=="Add Triangle") {
	    x = httpInt("tx");
	    y = httpInt("ty");
	    b = httpInt("tb");
	    h = httpInt("th");
	    c = mkColour(incomingValue("tcol",DataPost));
	    shape = Primitive(Triangle(c,b,h),x,y);
	}
	push(d.ps,shape);
    }
    catch(e) {
	content("<p><strong>Invalid input!</strong></p>");
    }

    showDrawing(d);
    addShapeForm(d);
}

Void PostContent()
{
    content("<a href=\"../files/shape.k\">shape.k</a><br>");
    content("<a href=\"../files/primitives.k\">primitives.k</a><br>");
    content("Memory usage: "+gcHeapSize);
    content("</body></html>");
}