strex: added `detectUrl()`
[iv.d.git] / protractor_test / gestures_old.d
blob7dda7b443ee80c9f6ba8e8d07cd4a8fa9168c66f
1 /* Invisible Vector Library
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module gesturespt_test /*is aliced*/;
18 private:
20 import std.stdio;
22 import iv.alice;
23 import iv.videolib;
24 import iv.geng;
27 // ////////////////////////////////////////////////////////////////////////// //
28 PTGlyph[] glib;
29 int nameMaxLen = 0;
30 int curPattern = -1;
31 PTGlyph drawnGlyph;
32 PTGlyph detectedGlyph;
35 void fixNameMaxLen () {
36 nameMaxLen = 0;
37 foreach (auto g; glib) if (g.name.length > nameMaxLen) nameMaxLen = cast(int)g.name.length;
41 // ////////////////////////////////////////////////////////////////////////// //
42 void drawStroke (const(PTGlyph) stk) {
43 auto col = rgb2col(255, 255, 0);
44 foreach (uint idx; 1..stk.length) {
45 double x0 = stk.x(idx-1), y0 = stk.y(idx-1);
46 double x1 = stk.x(idx), y1 = stk.y(idx);
47 vscrOvl.line(cast(int)x0, cast(int)y0, cast(int)x1, cast(int)y1, col);
52 void drawTemplate (const(PTGlyph) stk) {
53 foreach (uint idx; 1..stk.length) {
54 auto g = cast(ubyte)(255*idx/(stk.length-1));
55 auto b = cast(ubyte)(255-(255*idx/(stk.length-1)));
56 double x0 = stk.x(idx-1), y0 = stk.y(idx-1);
57 double x1 = stk.x(idx), y1 = stk.y(idx);
58 x0 = x0*200+400;
59 y0 = y0*200+300;
60 x1 = x1*200+400;
61 y1 = y1*200+300;
62 vscrOvl.line(cast(int)x0, cast(int)y0, cast(int)x1, cast(int)y1, rgb2col(0, g, b));
67 void drawStrokeList (int curptr) {
68 int wdt = nameMaxLen*6+4;
69 int hgt = cast(int)(glib.length*8+4);
70 vscrOvl.rect(0, 0, wdt, hgt, rgb2col(255, 255, 255));
71 vscrOvl.rect(1, 1, wdt-2, hgt-2, 0);
72 vscrOvl.fillRect(2, 2, wdt-4, hgt-4, rgb2col(0, 0, 127));
73 foreach (auto idx, auto g; glib) {
74 Color col, bkcol;
75 if (g is detectedGlyph) {
76 // highlighted
77 col = rgb2col(255, 255, 255);
78 //bkcol = rgb2col(0, 0, 255);
79 bkcol = rgb2col(0, 100, 0);
80 } else {
81 col = rgb2col(255, 127, 0);
82 bkcol = rgb2col(0, 0, 127);
84 if (curptr == idx) bkcol = rgb2col(0, 127, 0);
85 if (idx == curPattern) col = rgb2col(255, 255, 0);
86 vscrOvl.fillRect(2, idx*8+2, wdt-4, 8, bkcol);
87 vscrOvl.drawStr(2, idx*8+2, g.name, col);
92 // ////////////////////////////////////////////////////////////////////////// //
93 void updateCB (int elapsedTicks) {
94 //frameChanged();
98 // ////////////////////////////////////////////////////////////////////////// //
99 int curGlyph = -1;
100 string curGlyphName;
101 bool editingName;
102 string yesNoMessage;
105 void registerGlyph () {
106 if (drawnGlyph !is null && drawnGlyph.valid && curGlyphName.length > 0) {
107 auto gg = drawnGlyph.clone.normalize;
108 gg.name = curGlyphName;
109 usize gpos = usize.max;
110 foreach (auto idx, auto g; glib) if (g.name == curGlyphName) { gpos = idx; break; }
111 if (gpos != usize.max) {
112 glib[gpos] = gg;
113 } else {
114 gpos = glib.length;
115 glib ~= gg;
117 fixNameMaxLen();
118 curPattern = cast(int)gpos;
123 void rebuildCB () {
124 vscrOvl.clear(0);
125 if (curPattern >= 0 && curPattern < cast(int)glib.length) drawTemplate(glib[curPattern]);
126 drawStrokeList(curGlyph);
127 if (drawnGlyph !is null && drawnGlyph.valid) drawStroke(drawnGlyph);
128 if (yesNoMessage.length > 0) {
129 vscrOvl.fillRect(0, vlHeight-8, vlWidth, 8, rgb2col(128, 0, 0));
130 vscrOvl.drawStr(0, vlHeight-8, yesNoMessage, rgb2col(255, 255, 0));
131 } else if (editingName) {
132 vscrOvl.fillRect(0, vlHeight-8, vlWidth, 8, rgb2col(0, 0, 190));
133 vscrOvl.drawStr(0, vlHeight-8, curGlyphName, rgb2col(255, 127, 0));
134 vscrOvl.fillRect(curGlyphName.length*6, vlHeight-8, 6, 8, rgb2col(255, 255, 0));
139 // ////////////////////////////////////////////////////////////////////////// //
140 void onTextInputCB (dchar ch) {
141 if (ch >= ' ' && ch <= 255) {
142 curGlyphName ~= cast(char)ch;
143 frameChanged();
144 return;
149 void onKeyDownCB (in ref SDL_KeyboardEvent ev) {
150 if (ev.keysym.sym == SDLK_RETURN && (ev.keysym.mod&KMOD_ALT)) { switchFullscreen(); return; }
151 if (yesNoMessage.length > 0) {
152 switch (ev.keysym.sym) {
153 case SDLK_ESCAPE: yesNoMessage = null; break;
154 case SDLK_RETURN:
155 glib = glib[0..curPattern]~glib[curPattern+1..$];
156 detectedGlyph = null;
157 curPattern = -1;
158 yesNoMessage = null;
159 break;
160 default: break;
162 frameChanged();
163 return;
165 if (editingName) {
166 switch (ev.keysym.sym) {
167 case SDLK_ESCAPE: editingName = false; break;
168 case SDLK_BACKSPACE:
169 if (curGlyphName.length > 0) curGlyphName = curGlyphName[0..$-1];
170 break;
171 case SDLK_RETURN: registerGlyph(); editingName = false; break;
172 default: break;
174 if (!editingName) stopTextInput();
175 frameChanged();
176 return;
178 if (ev.keysym.sym == SDLK_ESCAPE) { postQuitMessage(); return; }
179 if (ev.keysym.sym == SDLK_F2) {
180 gstLibSave(File("strokes.dat", "w"), glib[]);
181 writefln("%s strokes saved", glib.length);
182 frameChanged();
183 return;
185 if (ev.keysym.sym == SDLK_F3) {
186 glib = gstLibLoad(File("strokes.dat"));
187 fixNameMaxLen();
188 writefln("%s strokes loaded", glib.length);
189 detectedGlyph = null;
190 curPattern = -1;
191 drawnGlyph = null;
192 frameChanged();
193 return;
195 if (ev.keysym.sym == SDLK_DELETE) {
196 if (curPattern >= 0) {
197 yesNoMessage = "Remove '"~glib[curPattern].name~"'?";
198 frameChanged();
199 return;
205 // ////////////////////////////////////////////////////////////////////////// //
206 int mdown = 0;
209 int getSelectedGlyph (int x, int y) {
210 int wdt = nameMaxLen*6+4;
211 int hgt = cast(int)(glib.length*8+4);
212 if (x >= 2 && y >= 2 && x < wdt-4 && y < hgt-4) {
213 return cast(int)((y-2)/8);
214 } else {
215 return -1;
220 void onMouseDownCB (in ref SDL_MouseButtonEvent ev) {
221 if (yesNoMessage.length > 0 || editingName) return;
222 if (mdown == 0) {
223 if (ev.button == SDL_BUTTON_LEFT) {
224 auto ng = getSelectedGlyph(ev.x, ev.y);
225 if (ng >= 0) {
226 curPattern = ng;
227 frameChanged();
228 return;
231 if (ev.button == SDL_BUTTON_LEFT || ev.button == SDL_BUTTON_RIGHT) {
232 mdown = (ev.button == SDL_BUTTON_LEFT ? 1 : 2);
233 detectedGlyph = null;
234 drawnGlyph = new PTGlyph();
235 drawnGlyph.addPoint(ev.x, ev.y);
236 frameChanged();
242 void onMouseUpCB (in ref SDL_MouseButtonEvent ev) {
243 if (yesNoMessage.length > 0 || editingName) return;
244 if (mdown != 0) {
245 if (drawnGlyph.valid) {
246 if (mdown == 1) {
247 detectedGlyph = drawnGlyph.findMatch(glib[]);
248 } else {
249 curGlyphName = (curPattern >= 0 ? glib[curPattern].name : "");
250 editingName = true;
251 startTextInput();
253 } else {
254 drawnGlyph = null;
256 frameChanged();
258 mdown = 0;
262 void onMouseDoubleCB (in ref SDL_MouseButtonEvent ev) {
263 //writeln("double button", ev.button, " at (", ev.x, ",", ev.y, ")");
267 void onMouseMotionCB (in ref SDL_MouseMotionEvent ev) {
268 if (yesNoMessage.length > 0 || editingName) return;
269 if (mdown == 0) {
270 auto ng = getSelectedGlyph(ev.x, ev.y);
271 if (ng != curGlyph) {
272 curGlyph = ng;
273 frameChanged();
275 } else if (mdown != 0) {
276 drawnGlyph.addPoint(ev.x, ev.y);
277 frameChanged();
282 // ////////////////////////////////////////////////////////////////////////// //
283 void main (string[] args) {
284 glib = gstLibLoad(File("strokes.dat"));
285 fixNameMaxLen();
286 writefln("%s strokes loaded", glib.length);
287 //gstLibSave(File("strokes_new.dat", "w"), glib[]);
288 vlWidth = 800;
289 vlHeight = 600;
290 useMag2x = false;
291 processArgs(args);
292 try {
293 initVideo("Gestures/SDL");
294 } catch (Throwable e) {
295 writeln("FATAL: ", e.msg);
296 return;
299 setFPS(35);
300 onUpdate = &updateCB;
301 onRebuild = &rebuildCB;
302 onKeyDown = &onKeyDownCB;
303 onTextInput = &onTextInputCB;
304 onMouseDown = &onMouseDownCB;
305 onMouseUp = &onMouseUpCB;
306 onMouseDouble = &onMouseDoubleCB;
307 onMouseMotion = &onMouseMotionCB;
308 mainLoop();