annotate toolboxes/graph_visualisation/lib/lefty/box.lefty @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 load ('def.lefty');
wolffd@0 2 definit ();
wolffd@0 3 #
wolffd@0 4 # initialize window data
wolffd@0 5 #
wolffd@0 6 canvas = defcanvas;
wolffd@0 7 wrect = [0 = ['x' = 0; 'y' = 0;]; 1 = ['x' = 400; 'y' = 500;];];
wolffd@0 8 setwidgetattr (canvas, ['window' = wrect;]);
wolffd@0 9 #
wolffd@0 10 # data structures
wolffd@0 11 #
wolffd@0 12 objarray = [];
wolffd@0 13 objnum = 0;
wolffd@0 14 #
wolffd@0 15 # misc functions
wolffd@0 16 #
wolffd@0 17 min = function (a, b) {
wolffd@0 18 if (a <= b)
wolffd@0 19 return a;
wolffd@0 20 return b;
wolffd@0 21 };
wolffd@0 22 max = function (a, b) {
wolffd@0 23 if (b <= a)
wolffd@0 24 return a;
wolffd@0 25 return b;
wolffd@0 26 };
wolffd@0 27 rectof = function (p1, p2) {
wolffd@0 28 return [
wolffd@0 29 0 = ['x' = min (p1.x, p2.x); 'y' = min (p1.y, p2.y);];
wolffd@0 30 1 = ['x' = max (p1.x, p2.x); 'y' = max (p1.y, p2.y);];
wolffd@0 31 ];
wolffd@0 32 };
wolffd@0 33 pointadd = function (p1, p2) {
wolffd@0 34 return ['x' = p2.x + p1.x; 'y' = p2.y + p1.y;];
wolffd@0 35 };
wolffd@0 36 pointsub = function (p1, p2) {
wolffd@0 37 return ['x' = p2.x - p1.x; 'y' = p2.y - p1.y;];
wolffd@0 38 };
wolffd@0 39 #
wolffd@0 40 # rendering functions
wolffd@0 41 #
wolffd@0 42 drawbox = function (obj, color) {
wolffd@0 43 box (canvas, obj, obj.rect, ['color' = color;]);
wolffd@0 44 };
wolffd@0 45 redrawboxes = function () {
wolffd@0 46 local i;
wolffd@0 47 clear (canvas);
wolffd@0 48 for (i = 0; i < objnum; i = i + 1)
wolffd@0 49 drawbox (objarray[i], 1);
wolffd@0 50 };
wolffd@0 51 redraw = function (canvas) {
wolffd@0 52 redrawboxes ();
wolffd@0 53 };
wolffd@0 54 #
wolffd@0 55 # editing functions
wolffd@0 56 #
wolffd@0 57 new = function (rect) {
wolffd@0 58 objarray[objnum] = [
wolffd@0 59 'rect' = rect;
wolffd@0 60 'id' = objnum;
wolffd@0 61 ];
wolffd@0 62 objnum = objnum + 1;
wolffd@0 63 return objarray[objnum - 1];
wolffd@0 64 };
wolffd@0 65 reshape = function (obj, rect) {
wolffd@0 66 obj.rect = rect;
wolffd@0 67 return obj;
wolffd@0 68 };
wolffd@0 69 move = function (obj, p) {
wolffd@0 70 obj.rect[0] = pointadd (obj.rect[0], p);
wolffd@0 71 obj.rect[1] = pointadd (obj.rect[1], p);
wolffd@0 72 return obj;
wolffd@0 73 };
wolffd@0 74 delete = function (obj) {
wolffd@0 75 if (obj.id ~= objnum - 1) {
wolffd@0 76 objarray[obj.id] = objarray[objnum - 1];
wolffd@0 77 objarray[obj.id].id = obj.id;
wolffd@0 78 }
wolffd@0 79 remove (objnum - 1, objarray);
wolffd@0 80 objnum = objnum - 1;
wolffd@0 81 };
wolffd@0 82 #
wolffd@0 83 # user interface functions
wolffd@0 84 #
wolffd@0 85 # left mouse button creates new box
wolffd@0 86 # middle button moves a box
wolffd@0 87 # right button deletes a box
wolffd@0 88 #
wolffd@0 89 leftdown = function (data) {
wolffd@0 90 if (data.obj ~= null)
wolffd@0 91 return;
wolffd@0 92 leftbox = new (rectof (data.pos, data.pos));
wolffd@0 93 drawbox (leftbox, 1);
wolffd@0 94 setgfxattr (canvas, ['mode' = 'xor';]);
wolffd@0 95 };
wolffd@0 96 leftmove = function (data) {
wolffd@0 97 if (~leftbox)
wolffd@0 98 return;
wolffd@0 99 drawbox (leftbox, 1);
wolffd@0 100 clearpick (canvas, leftbox);
wolffd@0 101 reshape (leftbox, rectof (data.ppos, data.pos));
wolffd@0 102 drawbox (leftbox, 1);
wolffd@0 103 };
wolffd@0 104 leftup = function (data) {
wolffd@0 105 if (~leftbox)
wolffd@0 106 return;
wolffd@0 107 drawbox (leftbox, 1);
wolffd@0 108 clearpick (canvas, leftbox);
wolffd@0 109 reshape (leftbox, rectof (data.ppos, data.pos));
wolffd@0 110 setgfxattr (canvas, ['mode' = 'src';]);
wolffd@0 111 drawbox (leftbox, 1);
wolffd@0 112 remove ('leftbox');
wolffd@0 113 };
wolffd@0 114 middledown = function (data) {
wolffd@0 115 if (data.obj == null)
wolffd@0 116 return;
wolffd@0 117 middlebox = data.obj;
wolffd@0 118 middlepos = data.pos;
wolffd@0 119 setgfxattr (canvas, ['mode' = 'xor';]);
wolffd@0 120 };
wolffd@0 121 middlemove = function (data) {
wolffd@0 122 if (~middlebox)
wolffd@0 123 return;
wolffd@0 124 drawbox (middlebox, 1);
wolffd@0 125 clearpick (canvas, middlebox);
wolffd@0 126 move (middlebox, pointsub (middlepos, data.pos));
wolffd@0 127 middlepos = data.pos;
wolffd@0 128 drawbox (middlebox, 1);
wolffd@0 129 };
wolffd@0 130 middleup = function (data) {
wolffd@0 131 if (~middlebox)
wolffd@0 132 return;
wolffd@0 133 drawbox (middlebox, 1);
wolffd@0 134 clearpick (canvas, middlebox);
wolffd@0 135 move (middlebox, pointsub (middlepos, data.pos));
wolffd@0 136 setgfxattr (canvas, ['mode' = 'src';]);
wolffd@0 137 drawbox (middlebox, 1);
wolffd@0 138 remove ('middlepos');
wolffd@0 139 remove ('middlebox');
wolffd@0 140 };
wolffd@0 141 rightup = function (data) {
wolffd@0 142 if (data.pobj == null)
wolffd@0 143 return;
wolffd@0 144 drawbox (data.obj, 0);
wolffd@0 145 clearpick (canvas, data.obj);
wolffd@0 146 delete (data.obj);
wolffd@0 147 };
wolffd@0 148 dops = function () {
wolffd@0 149 local s;
wolffd@0 150
wolffd@0 151 s = ['x' = 8 * 300; 'y' = 10.5 * 300;];
wolffd@0 152 canvas = createwidget (-1, ['type' = 'ps'; 'size' = s;]);
wolffd@0 153 setwidgetattr (canvas, ['window' = wrect;]);
wolffd@0 154 redraw (canvas);
wolffd@0 155 destroywidget (canvas);
wolffd@0 156 canvas=defcanvas;
wolffd@0 157 };