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