cgi shape; // -*-C-*-ish import CGI; import primitives; import Image; Void PreContent() { content("Shape Thingy"); content("

Shape Thingy

"); } 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("
"); content(imageHandler(draw,d,"[Pretty picture]",d.x,d.y)); content(""); 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")+"
"); idx++; } content("
"); } 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("

Add a shape

"); content(formHandler(OnAddShape,d)); content(""+ ""+ ""+ ""+ ""+ ""+ ""+ ""); content(""+ ""+ ""+ ""+ ""+ ""+ ""+ ""); content(""+ ""+ ""+ ""+ ""+ ""+ ""+ ""); content("
RectangleX"+textBox("rx","",5)+"Y"+textBox("ry","",5)+"Width"+textBox("rw","",5)+"Height"+textBox("rh","",5)+"Colour"+colourBox("rcol")+""+submit("Add Rectangle")+"
EllipseX"+textBox("ex","",5)+"Y"+textBox("ey","",5)+"Width"+textBox("ew","",5)+"Height"+textBox("eh","",5)+"Colour"+colourBox("ecol")+""+submit("Add Ellipse")+"
TriangleX"+textBox("tx","",5)+"Y"+textBox("ty","",5)+"Base"+textBox("tb","",5)+"Height"+textBox("th","",5)+"Colour"+colourBox("tcol")+""+submit("Add Triangle")+"
"); content(closeForm); } Void OnAddShape(Drawing d) { try { if (submitUsed=="Add Rectangle") { x = httpInt("rx"); y = httpInt("ry"); w = httpInt("rw"); h = httpInt("rh"); col = checkInput(incomingValue("rcol",DataPost), validCols, ""); c = mkColour(col); 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"); col = checkInput(incomingValue("ecol",DataPost), validCols, ""); c = mkColour(col); 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"); col = checkInput(incomingValue("tcol",DataPost), validCols, ""); c = mkColour(col); shape = Primitive(Triangle(c,b,h),x,y); } push(d.ps,shape); } catch(e) { content("

Invalid input!

"); } showDrawing(d); addShapeForm(d); } Void PostContent() { content("shape.k
"); content("primitives.k
"); content("Memory usage: "+gcHeapSize); content(""); }