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*/;
27 // ////////////////////////////////////////////////////////////////////////// //
32 PTGlyph detectedGlyph
;
35 void fixNameMaxLen () {
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
);
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
) {
75 if (g
is detectedGlyph
) {
77 col
= rgb2col(255, 255, 255);
78 //bkcol = rgb2col(0, 0, 255);
79 bkcol
= rgb2col(0, 100, 0);
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
) {
98 // ////////////////////////////////////////////////////////////////////////// //
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
) {
118 curPattern
= cast(int)gpos
;
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
;
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;
155 glib
= glib
[0..curPattern
]~glib
[curPattern
+1..$];
156 detectedGlyph
= null;
166 switch (ev
.keysym
.sym
) {
167 case SDLK_ESCAPE
: editingName
= false; break;
169 if (curGlyphName
.length
> 0) curGlyphName
= curGlyphName
[0..$-1];
171 case SDLK_RETURN
: registerGlyph(); editingName
= false; break;
174 if (!editingName
) stopTextInput();
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
);
185 if (ev
.keysym
.sym
== SDLK_F3
) {
186 glib
= gstLibLoad(File("strokes.dat"));
188 writefln("%s strokes loaded", glib
.length
);
189 detectedGlyph
= null;
195 if (ev
.keysym
.sym
== SDLK_DELETE
) {
196 if (curPattern
>= 0) {
197 yesNoMessage
= "Remove '"~glib
[curPattern
].name
~"'?";
205 // ////////////////////////////////////////////////////////////////////////// //
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);
220 void onMouseDownCB (in ref SDL_MouseButtonEvent ev
) {
221 if (yesNoMessage
.length
> 0 || editingName
) return;
223 if (ev
.button
== SDL_BUTTON_LEFT
) {
224 auto ng
= getSelectedGlyph(ev
.x
, ev
.y
);
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
);
242 void onMouseUpCB (in ref SDL_MouseButtonEvent ev
) {
243 if (yesNoMessage
.length
> 0 || editingName
) return;
245 if (drawnGlyph
.valid
) {
247 detectedGlyph
= drawnGlyph
.findMatch(glib
[]);
249 curGlyphName
= (curPattern
>= 0 ? glib
[curPattern
].name
: "");
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;
270 auto ng
= getSelectedGlyph(ev
.x
, ev
.y
);
271 if (ng
!= curGlyph
) {
275 } else if (mdown
!= 0) {
276 drawnGlyph
.addPoint(ev
.x
, ev
.y
);
282 // ////////////////////////////////////////////////////////////////////////// //
283 void main (string
[] args
) {
284 glib
= gstLibLoad(File("strokes.dat"));
286 writefln("%s strokes loaded", glib
.length
);
287 //gstLibSave(File("strokes_new.dat", "w"), glib[]);
293 initVideo("Gestures/SDL");
294 } catch (Throwable e
) {
295 writeln("FATAL: ", e
.msg
);
300 onUpdate
= &updateCB
;
301 onRebuild
= &rebuildCB
;
302 onKeyDown
= &onKeyDownCB
;
303 onTextInput
= &onTextInputCB
;
304 onMouseDown
= &onMouseDownCB
;
305 onMouseUp
= &onMouseUpCB
;
306 onMouseDouble
= &onMouseDoubleCB
;
307 onMouseMotion
= &onMouseMotionCB
;