library edit;
import 'dart:html';
import 'package:color/color.dart';
import 'package:chi/tools.dart';
import 'dart:convert';
import 'dart:async';
import 'dart:typed_data';
final Color BG_COLOR = new Color.hex("ced6b5");
final Color SHADE_COLOR = new Color.hex("bac1a3");
final Color BLACK = new Color.hex("000000");
final Color GREY50 = new Color.hex("7c806d");
final Color GREY25 = new Color.hex("a5ab91");
const int OUTER_SIDE = INNER_SIDE+2*INNER_MARGIN+2*OUTER_STROKE;
const int INNER_SIDE = 7;
const int INNER_MARGIN = 1;
const int OUTER_MARGIN = 1;
const int OUTER_STROKE = 1;
const DOT = OUTER_SIDE+2*OUTER_MARGIN;
class Point {
int x = 0;
int y = 0;
Color col = BLACK;
Point(this.x, this.y);
String toString() => '{x: $x, y: $y, col: "$col"}';
Map toMap(){
Map ret = new Map();
ret["x"]=x;
ret["y"]=y;
ret["col"]=col.toString();
return ret;
}
Point.fromMap(Map m){
this.x = m["x"];
this.y = m["y"];
this.col = new Color.hex(m["col"]);
}
}
class Port {
CanvasRenderingContext2D ctx;
CanvasRenderingContext2D via;
CanvasRenderingContext2D img;
int width;
int height;
Color lastPen;
Map, Point> data = new Map();
Tuple2 size = new Tuple2(50, 40);
void prepare(int w, int h){
ctx.imageSmoothingEnabled = false;
ctx.translate(0.5, 0.5);
ctx.scale(1, 1);
via.imageSmoothingEnabled = false;
via.translate(0.5, 0.5);
via.scale(1, 1);
img.imageSmoothingEnabled = false;
img.translate(0.5, 0.5);
img.scale(1, 1);
width = w;
height = h;
}
void back(){
RgbColor color;
color = BG_COLOR.toRgbColor();
ctx.lineWidth = OUTER_STROKE;
ctx.setFillColorRgb(color.r, color.g, color.b);
ctx.fillRect(0, 0, width, height);
color = SHADE_COLOR.toRgbColor();
ctx.setFillColorRgb(color.r, color.g, color.b);
ctx.setStrokeColorRgb(color.r, color.g, color.b);
int w = 0;
int h = 0;
int y = 2*OUTER_MARGIN;
while(y(w, h);
Point tp = data[p];
if (tp!=null){
var color = tp.col.toRgbColor();
img.setFillColorRgb(color.r, color.g, color.b);
img.setStrokeColorRgb(color.r, color.g, color.b);
img.strokeRect(x, y, OUTER_SIDE, OUTER_SIDE);
img.fillRect(x+OUTER_STROKE + INNER_MARGIN, y+OUTER_STROKE+INNER_MARGIN, INNER_SIDE, INNER_SIDE);
img.strokeRect(x+OUTER_STROKE + INNER_MARGIN, y+OUTER_STROKE + INNER_MARGIN, INNER_SIDE, INNER_SIDE);
}
w++;
x = x + DOT;
}
h++;
y = y + DOT;
}
}
void limit(int x, int y){
size = new Tuple2(x, y);
}
void move(int x, int y){
// Store the current transformation matrix
via.save();
// Use the identity matrix while clearing the canvas
via.setTransform(1, 0, 0, 1, 0, 0);
via.clearRect(0, 0, width, height);
// Restore the transform
via.restore();
var c = new Color.hex("0000ff").toRgbColor();
via.setStrokeColorRgb(c.r, c.g, c.b);
via.strokeRect(x*DOT+1, y*DOT+1, DOT, DOT);
via.strokeRect(x*DOT+1+OUTER_STROKE+INNER_MARGIN, y*DOT+1+OUTER_STROKE+INNER_MARGIN, INNER_SIDE+2*INNER_MARGIN, INNER_SIDE+2*INNER_MARGIN);
(querySelector("#pos") as SpanElement).text = "$x:$y";
(querySelector("#size") as SpanElement).text = "$size";
}
void leave(){
via.clearRect(0, 0, width, height);
}
void put(int x, int y, [Color col = null]){
var p = new Tuple2(x, y);
Point tp = data[p];
if ((tp == null) && (col != SHADE_COLOR)){
data[p]=new Point(x, y);
if(col != null){
data[p].col = col;
}else{
data[p].col = BLACK;
}
lastPen = data[p].col;
}else if (col!=null){
lastPen = col;
if (col == SHADE_COLOR){
clear(x, y);
}else{
tp.col = col;
}
}else if (col==null){
if(tp.col == BLACK){
tp.col = GREY50;
lastPen = tp.col;
}else if(tp.col == GREY50){
tp.col = GREY25;
lastPen = tp.col;
}else{
clear(x, y);
}
}
}
void clear(int x, int y){
var p = new Tuple2(x, y);
data.remove(p);
lastPen = SHADE_COLOR;
}
void shift(dx, dy){
Map, Point> tmp = new Map();
data.keys.forEach((t){
var p = data[t];
var nt = new Tuple2(t.i1 + dx, t.i2+dy);
var np = new Point(nt.i1, nt.i2);
np.col = p.col;
tmp[nt] = np;
});
data = tmp;
}
String export(){
if (data.values!=null){
Map r = new Map();
List