summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Cellerier <dionoea@videolan.org>2006-07-19 20:11:02 +0000
committerAntoine Cellerier <dionoea@videolan.org>2006-07-19 20:11:02 +0000
commit928c33c255e47c39c5f549b3e1aa40fbb3aaa09a (patch)
tree00b456ea97dea45fcbe13f5c428e427a0a710e02
parent36daf39e57d8712e0165aae4c11c08adf8f8c694 (diff)
downloadrockbox-928c33c255e47c39c5f549b3e1aa40fbb3aaa09a.tar.gz
rockbox-928c33c255e47c39c5f549b3e1aa40fbb3aaa09a.zip
rockpaint.c: Paint plugin for color LCD targets. Features include: different tools (brush, bucket, cut/copy/paste, line, bezier curve, text, rectangles, ovals, linear and radial gradients), 18 color palette, RGBHSV color picker, BMP file loading and saving. This still needs some work (and maybe some, if not a lot, of cleaning up of the source code) but it works fine (tested on h3x0 and ipod5g sims and ipod5g real target).
viewers.config: add rockpaint to the list of available viewers for .bmp files git-svn-id: svn://svn.rockbox.org/rockbox/trunk@10258 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/SOURCES4
-rw-r--r--apps/plugins/rockpaint.c2799
-rw-r--r--apps/plugins/viewers.config1
3 files changed, 2804 insertions, 0 deletions
diff --git a/apps/plugins/SOURCES b/apps/plugins/SOURCES
index 526a127c7a..0e0a78bc9d 100644
--- a/apps/plugins/SOURCES
+++ b/apps/plugins/SOURCES
@@ -85,6 +85,10 @@ xobox.c
85 85
86spacerocks.c 86spacerocks.c
87 87
88#if LCD_DEPTH >= 16
89rockpaint.c
90#endif
91
88#endif /* HAVE_LCD_BITMAP */ 92#endif /* HAVE_LCD_BITMAP */
89 93
90#ifdef HAVE_LCD_CHARCELLS /* Player model only */ 94#ifdef HAVE_LCD_CHARCELLS /* Player model only */
diff --git a/apps/plugins/rockpaint.c b/apps/plugins/rockpaint.c
new file mode 100644
index 0000000000..7ca4cb50f8
--- /dev/null
+++ b/apps/plugins/rockpaint.c
@@ -0,0 +1,2799 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 Antoine Cellerier <dionoea -at- videolan -dot- org>
11 * Based on parts of rockpaint 0.45, Copyright (C) 2005 Eli Sherer
12 *
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21/**
22 * TODO:
23 * - implement 2 layers with alpha colors
24 * - take brush width into account when drawing shapes
25 * - handle bigger than screen bitmaps
26 * - cache fonts when building the font preview (else it only works well on simulators because they have "fast" disk read)
27 */
28
29#include "plugin.h"
30#include "errno.h"
31#include "lib/bmp.h"
32#include "lib/rgb_hsv.h"
33
34PLUGIN_HEADER
35
36/***********************************************************************
37 * Buttons
38 ***********************************************************************/
39
40#if CONFIG_KEYPAD == IRIVER_H300_PAD
41#define ROCKPAINT_QUIT BUTTON_OFF
42#define ROCKPAINT_DRAW BUTTON_SELECT
43#define ROCKPAINT_MENU BUTTON_ON
44#define ROCKPAINT_TOOLBAR BUTTON_REC
45#define ROCKPAINT_TOOLBAR2 BUTTON_MODE
46#define ROCKPAINT_UP BUTTON_UP
47#define ROCKPAINT_DOWN BUTTON_DOWN
48#define ROCKPAINT_LEFT BUTTON_LEFT
49#define ROCKPAINT_RIGHT BUTTON_RIGHT
50
51#elif ( CONFIG_KEYPAD == IPOD_4G_PAD ) || ( CONFIG_KEYPAD == IPOD_3G_PAD )
52#define ROCKPAINT_QUIT ( ~BUTTON_MAIN )
53#define ROCKPAINT_DRAW BUTTON_SELECT
54#define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_MENU )
55#define ROCKPAINT_TOOLBAR ( BUTTON_MENU | BUTTON_LEFT )
56#define ROCKPAINT_TOOLBAR2 ( BUTTON_MENU | BUTTON_RIGHT )
57#define ROCKPAINT_UP BUTTON_MENU
58#define ROCKPAINT_DOWN BUTTON_PLAY
59#define ROCKPAINT_LEFT BUTTON_LEFT
60#define ROCKPAINT_RIGHT BUTTON_RIGHT
61
62#elif ( CONFIG_KEYPAD == IAUDIO_X5_PAD )
63#define ROCKPAINT_QUIT BUTTON_POWER
64#define ROCKPAINT_DRAW BUTTON_SELECT
65#define ROCKPAINT_MENU BUTTON_PLAY
66#define ROCKPAINT_TOOLBAR BUTTON_REC
67#define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
68#define ROCKPAINT_UP BUTTON_UP
69#define ROCKPAINT_DOWN BUTTON_DOWN
70#define ROCKPAINT_LEFT BUTTON_LEFT
71#define ROCKPAINT_RIGHT BUTTON_RIGHT
72
73#else
74#error "Please define keys for this keypad"
75#endif
76
77/***********************************************************************
78 * Palette Default Colors
79 ***********************************************************************/
80#define COLOR_BLACK LCD_RGBPACK(0,0,0)
81#define COLOR_WHITE LCD_RGBPACK(255,255,255)
82#define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
83#define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
84#define COLOR_RED LCD_RGBPACK(128,0,0)
85#define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
86#define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
87#define COLOR_YELLOW LCD_RGBPACK(255,255,0)
88#define COLOR_GREEN LCD_RGBPACK(0,128,0)
89#define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
90#define COLOR_CYAN LCD_RGBPACK(0,128,128)
91#define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
92#define COLOR_BLUE LCD_RGBPACK(0,0,128)
93#define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
94#define COLOR_PURPLE LCD_RGBPACK(128,0,128)
95#define COLOR_PINK LCD_RGBPACK(255,0,255)
96#define COLOR_BROWN LCD_RGBPACK(128,64,0)
97#define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
98
99#define SPLASH_SCREEN PLUGIN_DIR "/rockpaint/splash.bmp"
100#define ROCKPAINT_TITLE_FONT 2
101
102/***********************************************************************
103 * Program Colors
104 ***********************************************************************/
105#define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
106#define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
107
108#define ROWS LCD_HEIGHT
109#define COLS LCD_WIDTH
110
111/**
112 * Toolbar positioning stuff ... don't read this unless you really need to
113 *
114 * TB Toolbar
115 * SP Separator
116 * SC Selected Color
117 * PL Palette
118 * TL Tools
119 */
120
121/* Separator sizes */
122#define TB_SP_MARGIN 3
123#define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
124
125/* Selected color sizes */
126#define TB_SC_SIZE 12
127
128/* Palette sizes */
129#define TB_PL_COLOR_SIZE 7
130#define TB_PL_COLOR_SPACING 2
131#define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
132#define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
133
134/* Tools sizes */
135#define TB_TL_SIZE 8
136#define TB_TL_SPACING 2
137#define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
138#define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
139
140/* Menu button size ... gruik */
141#define TB_MENU_MIN_WIDTH 30
142
143/* Selected colors position */
144#define TB_SC_FG_TOP 2
145#define TB_SC_FG_LEFT 2
146#define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
147#define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
148
149/* Palette position */
150#define TB_PL_TOP TB_SC_FG_TOP
151#define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
152
153/* Tools position */
154#define TB_TL_TOP TB_SC_FG_TOP
155#define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
156
157#if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
158#undef TB_TL_TOP
159#undef TB_TL_LEFT
160#define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
161#define TB_TL_LEFT TB_SC_FG_LEFT
162#endif
163
164/* Menu button position */
165#define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
166#define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
167
168#define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
169
170
171static void draw_pixel(int x,int y);
172static void draw_line( int x1, int y1, int x2, int y2 );
173static void draw_rect( int x1, int y1, int x2, int y2 );
174static void draw_toolbars(bool update);
175static void inv_cursor(bool update);
176static void restore_screen(void);
177static void clear_drawing(void);
178static void goto_menu(void);
179static int load_bitmap( char *filename );
180static int save_bitmap( char *filename );
181static void draw_rect_full( int x1, int y1, int x2, int y2 );
182extern int errno;
183
184/***********************************************************************
185 * Global variables
186 ***********************************************************************/
187
188#if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
189int errno;
190#endif
191
192static struct plugin_api* rb;
193
194static int drawcolor=0; /* Current color (in palette) */
195static int bgdrawcolor=9; /* Current background color (in palette) */
196bool isbg = false; /* gruik ugly hack alert */
197
198static int preview=false; /* Is preview mode on ? */
199
200/* TODO: clean this up */
201static int x=0, y=0; /* cursor position */
202static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
203static int prev_x2=-1, prev_y2=-1;
204static int prev_x3=-1, prev_y3=-1;
205static int tool_mode=-1;
206
207
208static int bsize=1; /* brush size */
209static int bspeed=1; /* brush speed */
210
211enum Tools { Brush = 0, /* Regular brush */
212 Fill = 1, /* Fill a shape with current color */
213 SelectRectangle = 2,
214 ColorPicker = 3, /* Pick a color */
215 Line = 4, /* Draw a line between two points */
216 Unused = 5, /* THIS IS UNUSED ... */
217 Curve = 6,
218 Text = 7,
219 Rectangle = 8, /* Draw a rectangle */
220 RectangleFull = 9,
221 Oval = 10, /* Draw an oval */
222 OvalFull = 11,
223 LinearGradient = 12,
224 RadialGradient = 13
225 };
226
227enum Tools tool = Brush;
228
229static bool quit=false;
230static int gridsize=0;
231
232static fb_data rp_colors[18] =
233{
234 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
235 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
236 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
237 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
238 COLOR_LIGHTBROWN
239};
240
241static fb_data save_buffer[ ROWS*COLS ];
242
243extern fb_data rockpaint[];
244extern fb_data rockpaint_hsvrgb[];
245
246/* Maximum string size allowed for the text tool */
247#define MAX_TEXT 255
248
249static union
250{
251 /* Used by fill and gradient algorithms */
252 struct
253 {
254 short x;
255 short y;
256 } coord[ ROWS*COLS ];
257
258 /* Used by bezier curve algorithms */
259 struct
260 {
261 short x1, y1;
262 short x2, y2;
263 short x3, y3;
264 short x4, y4;
265 short depth;
266 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
267 * than coord ... so we divide to take
268 * less memory. */
269
270 /* Used to cut/copy/paste data */
271 fb_data clipboard[ ROWS*COLS ];
272
273 /* Used for text mode */
274 struct
275 {
276 char text[MAX_TEXT+1];
277 char font[MAX_PATH+1];
278 char old_font[MAX_PATH+1];
279 } text;
280} buffer;
281
282/* Current filename */
283static char filename[MAX_PATH+1];
284
285/* Font preview buffer */
286//#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
287//#define FONT_PREVIEW_HEIGHT 1000
288//static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
289
290/***********************************************************************
291 * Offscreen buffer/Text/Fonts handling
292 *
293 * Parts of code taken from firmware/drivers/lcd-16bit.c
294 ***********************************************************************/
295static void buffer_mono_bitmap_part(
296 fb_data *buf, int buf_width, int buf_height,
297 const unsigned char *src, int src_x, int src_y,
298 int stride, int x, int y, int width, int height )
299/* this function only draws the foreground part of the bitmap */
300{
301 const unsigned char *src_end;
302 fb_data *dst, *dst_end;
303 unsigned fgcolor = rb->lcd_get_foreground();
304
305 /* nothing to draw? */
306 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
307 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
308 return;
309
310 /* clipping */
311 if( x < 0 )
312 {
313 width += x;
314 src_x -= x;
315 x = 0;
316 }
317 if( y < 0 )
318 {
319 height += y;
320 src_y -= y;
321 y = 0;
322 }
323 if( x + width > buf_width )
324 width = buf_width - x;
325 if( y + height > buf_height )
326 height = buf_height - y;
327
328 src += stride * (src_y >> 3) + src_x; /* move starting point */
329 src_y &= 7;
330 src_end = src + width;
331
332 dst = buf + y*buf_width + x;
333
334 do
335 {
336 const unsigned char *src_col = src++;
337 unsigned data = *src_col >> src_y;
338 fb_data *dst_col = dst++;
339 int numbits = 8 - src_y;
340
341 dst_end = dst_col + height * buf_width;
342 do
343 {
344 if( data & 0x01 )
345 *dst_col = fgcolor; /* FIXME ? */
346
347 dst_col += buf_width;
348
349 data >>= 1;
350 if( --numbits == 0 )
351 {
352 src_col += stride;
353 data = *src_col;
354 numbits = 8;
355 }
356 } while( dst_col < dst_end );
357 } while( src < src_end );
358}
359
360static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
361 int x, int y, int ofs, const unsigned char *str )
362{
363 unsigned short ch;
364 unsigned short *ucs;
365
366 struct font *pf = rb->font_get( FONT_UI );
367 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
368
369 ucs = rb->bidi_l2v( str, 1 );
370
371 while( (ch = *ucs++) != 0 && x < buf_width )
372 {
373 int width;
374 const unsigned char *bits;
375
376 /* get proportional width and glyph bits */
377 width = rb->font_get_width( pf, ch );
378
379 if( ofs > width )
380 {
381 ofs -= width;
382 continue;
383 }
384
385 bits = rb->font_get_bits( pf, ch );
386
387 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0, width, x, y, width - ofs, pf->height);
388
389 x += width - ofs;
390 ofs = 0;
391 }
392}
393
394/***********************************************************************
395 * Menu handling
396 ***********************************************************************/
397struct menu_items
398{
399 int value;
400 char label[13]; /* GRUIK ? */
401};
402
403#define MENU_ESC -1242
404enum {
405 /* Generic menu items */
406 MENU_END = -42, MENU_TITLE = -12,
407 /* Main menu */
408 MAIN_MENU_RESUME,
409 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
410 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
411 MAIN_MENU_GRID_SIZE,
412 MAIN_MENU_EXIT,
413 /* Select action menu */
414 SELECT_MENU_CUT, SELECT_MENU_COPY, SELECT_MENU_INVERT,
415 SELECT_MENU_CANCEL,
416 /* Text menu */
417 TEXT_MENU_TEXT, TEXT_MENU_FONT,
418 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
419 };
420
421static struct menu_items main_menu[]=
422 { { MENU_TITLE, "RockPaint" },
423 { MAIN_MENU_RESUME, "Resume" },
424 { MAIN_MENU_NEW, "New" },
425 { MAIN_MENU_LOAD, "Load" },
426 { MAIN_MENU_SAVE, "Save" },
427 { MAIN_MENU_BRUSH_SIZE, "Brush Size" },
428 { MAIN_MENU_BRUSH_SPEED, "Brush Speed" },
429 { MAIN_MENU_COLOR, "Choose Color" },
430 { MAIN_MENU_GRID_SIZE, "Grid Size" },
431 { MAIN_MENU_EXIT, "Exit" },
432 { MENU_END, "" } };
433
434static struct menu_items size_menu[] =
435 { { MENU_TITLE, "Choose Size" },
436 { 1, "1x" },
437 { 2, "2x" },
438 { 4, "4x" },
439 { 8, "8x" },
440 { MENU_END, "" } };
441
442static struct menu_items speed_menu[] =
443 { { MENU_TITLE, "Choose Speed" },
444 { 1, "1x" },
445 { 2, "2x" },
446 { 4, "4x" },
447 { MENU_END, "" } };
448
449static struct menu_items gridsize_menu[] =
450 { { MENU_TITLE, "Grid Size" },
451 { 0, "No grid" },
452 { 5, "5px" },
453 { 10, "10px" },
454 { 20, "20px" },
455 { MENU_END, "" } };
456
457static struct menu_items select_menu[] =
458 { { MENU_TITLE, "Select..." },
459 { SELECT_MENU_CUT, "Cut" },
460 { SELECT_MENU_COPY, "Copy" },
461 { SELECT_MENU_INVERT, "Invert" },
462 { SELECT_MENU_CANCEL, "Cancel" },
463 { MENU_END, "" } };
464
465static struct menu_items text_menu[] =
466 { { MENU_TITLE, "Text" },
467 { TEXT_MENU_TEXT, "Set text" },
468 { TEXT_MENU_FONT, "Change font" },
469 { TEXT_MENU_PREVIEW, "Preview" },
470 { TEXT_MENU_APPLY, "Apply" },
471 { TEXT_MENU_CANCEL, "Cancel" },
472 { MENU_END, "" } };
473
474static int draw_window( int height, int width,
475 int *top, int *left,
476 const char *title )
477{
478 int fh;
479 rb->lcd_getstringsize( title, NULL, &fh );
480 fh++;
481
482 const int _top = ( LCD_HEIGHT - height ) / 2;
483 const int _left = ( LCD_WIDTH - width ) / 2;
484 if( top ) *top = _top;
485 if( left ) *left = _left;
486 rb->lcd_set_background(COLOR_BLUE);
487 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
488 rb->lcd_fillrect( _left, _top, width, height );
489 rb->lcd_set_foreground(COLOR_BLUE);
490 rb->lcd_fillrect( _left, _top, width, fh+4 );
491 rb->lcd_set_foreground(COLOR_WHITE);
492 rb->lcd_putsxy( _left+2, _top+2, title );
493 rb->lcd_set_foreground(COLOR_BLACK);
494 rb->lcd_drawrect( _left, _top, width, height );
495 return _top+fh+4;
496}
497
498static int menu_display( struct menu_items menu[], int prev_value )
499{
500 int i,
501 fh, /* font height */
502 width, height,
503 a, b,
504 selection=1,
505 menu_length,
506 top, left;
507 int scroll = 0, onscreen = 0;
508
509 rb->lcd_getstringsize( menu[0].label, &width, &fh );
510 for( i=1; menu[i].value != MENU_END; i++ )
511 {
512 if( prev_value == menu[i].value )
513 {
514 selection = i;
515 }
516 rb->lcd_getstringsize( menu[i].label, &a, &b );
517 if( a > width ) width = a;
518 if( b > fh ) fh = b;
519 }
520 menu_length = i;
521 fh++;
522 width += 10;
523
524 height = menu_length * fh + 4 + 2 + 2;
525 if( height >= LCD_HEIGHT )
526 {
527 scroll = 1;
528 onscreen = ( LCD_HEIGHT - 4 - 2 - 2 )/fh;
529 height = onscreen * fh + 4 + 2 + 2;
530 width += 5;
531 }
532 else
533 {
534 onscreen = menu_length;
535 }
536
537 draw_window( height, width, &top, &left, menu[0].label );
538
539 while( 1 )
540 {
541 for( i = (scroll == 0 ? 1 : scroll);
542 i < (scroll ? scroll + onscreen - 1:onscreen);
543 i++ )
544 {
545 if( i == selection )
546 {
547 rb->lcd_set_foreground( COLOR_WHITE );
548 rb->lcd_set_background( COLOR_BLUE );
549 }
550 else
551 {
552 rb->lcd_set_foreground( COLOR_BLACK );
553 rb->lcd_set_background( COLOR_LIGHTGRAY );
554 }
555 rb->lcd_putsxy( left+2,
556 top+6+fh*(i-(scroll == 0 ? 0 : scroll-1 )),
557 menu[i].label );
558 }
559 if( scroll )
560 {
561 int scroll_height = ((height-fh-4-2)*onscreen)/menu_length;
562 rb->lcd_set_foreground( COLOR_BLACK );
563 rb->lcd_vline( left+width-5, top+fh+4, top+height-2 );
564 rb->lcd_fillrect( left+width-4,
565 top+fh+4+((height-4-2-fh-scroll_height)*(scroll-1))/(menu_length-onscreen),
566 3, scroll_height );
567 }
568 rb->lcd_update();
569
570 switch( rb->button_get(true) )
571 {
572 case ROCKPAINT_UP:
573 case ROCKPAINT_UP|BUTTON_REPEAT:
574 selection = (selection + menu_length-1)%menu_length;
575 if( !selection ) selection = menu_length-1;
576 break;
577
578 case ROCKPAINT_DOWN:
579 case ROCKPAINT_DOWN|BUTTON_REPEAT:
580 selection = (selection + 1)%menu_length;
581 if( !selection ) selection++;
582 break;
583
584 case ROCKPAINT_LEFT:
585 restore_screen();
586 return MENU_ESC;
587
588 case ROCKPAINT_RIGHT:
589 case ROCKPAINT_DRAW:
590 restore_screen();
591 return menu[selection].value;
592 }
593 if( scroll )
594 {
595 if( selection < scroll )
596 {
597 scroll = selection;
598 draw_window( height, width, NULL, NULL, menu[0].label );
599 }
600 if( selection >= scroll + onscreen - 1 )
601 {
602 scroll++;
603 draw_window( height, width, NULL, NULL, menu[0].label );
604 }
605 }
606 }
607}
608
609/***********************************************************************
610 * File browser
611 ***********************************************************************/
612
613char bbuf[MAX_PATH+1]; /* used by file and font browsers */
614char bbuf_s[MAX_PATH+1]; /* used by file and font browsers */
615
616static bool browse( char *dst, int dst_size, const char *start )
617{
618#define WIDTH ( LCD_WIDTH - 20 )
619#define HEIGHT ( LCD_HEIGHT - 20 )
620#define LINE_SPACE 2
621 int top, top_inside, left;
622
623 DIR *d;
624 struct dirent *de;
625 int fvi = 0; /* first visible item */
626 int lvi = 0; /* last visible item */
627 int si = 0; /* selected item */
628 int li = 0; /* last item */
629 int i;
630
631 int fh;
632 char *a;
633
634 rb->lcd_getstringsize( "Ap", NULL, &fh );
635
636 rb->strcpy( bbuf, start );
637 a = bbuf+rb->strlen(bbuf)-1;
638 if( *a != '/' )
639 {
640 a[1] = '/';
641 a[2] = '\0';
642 }
643
644 while( 1 )
645 {
646 d = rb->PREFIX(opendir)( bbuf );
647 if( !d )
648 {
649 if( errno == ENOTDIR )
650 {
651 /* this is a file */
652 bbuf[rb->strlen(bbuf)-1] = '\0';
653 rb->strncpy( dst, bbuf, dst_size );
654 return true;
655 }
656 else if( errno == EACCES || errno == ENOENT )
657 {
658 bbuf[0] = '/'; bbuf[1] = '\0';
659 d = rb->PREFIX(opendir)( "/" );
660 }
661 else
662 {
663 return false;
664 }
665 }
666 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, bbuf );
667 i = 0;
668 li = -1;
669 while( i < fvi )
670 {
671 rb->PREFIX(readdir)( d );
672 i++;
673 }
674 while( top_inside+(i-fvi)*(fh+LINE_SPACE) < HEIGHT )
675 {
676 de = rb->PREFIX(readdir)( d );
677 if( !de )
678 {
679 li = i-1;
680 break;
681 }
682 rb->lcd_set_foreground((si==i?COLOR_WHITE:COLOR_BLACK));
683 rb->lcd_set_background((si==i?COLOR_BLUE:COLOR_LIGHTGRAY));
684 rb->lcd_putsxy( left+10,
685 top_inside+(i-fvi)*(fh+LINE_SPACE),
686 de->d_name );
687 if( si == i )
688 rb->strcpy( bbuf_s, de->d_name );
689 i++;
690 }
691 lvi = i-1;
692 if( li == -1 )
693 {
694 if( !rb->PREFIX(readdir)( d ) )
695 {
696 li = lvi;
697 }
698 }
699 rb->PREFIX(closedir)( d );
700
701 rb->lcd_update();
702
703 switch( rb->button_get(true) )
704 {
705 case ROCKPAINT_UP:
706 case ROCKPAINT_UP|BUTTON_REPEAT:
707 if( si > 0 )
708 {
709 si--;
710 if( si<fvi )
711 {
712 fvi--;
713 }
714 }
715 break;
716
717 case ROCKPAINT_DOWN:
718 case ROCKPAINT_DOWN|BUTTON_REPEAT:
719 if( li == -1 || si < li )
720 {
721 si++;
722 if( si>lvi )
723 {
724 fvi++;
725 }
726 }
727 break;
728
729 case ROCKPAINT_LEFT:
730 if( bbuf[0] == '/' && !bbuf[1] ) return false;
731 bbuf_s[0] = '.';
732 bbuf_s[1] = '.';
733 bbuf_s[2] = '\0';
734 case ROCKPAINT_RIGHT:
735 case ROCKPAINT_DRAW:
736 if( *bbuf_s == '.' && !bbuf_s[1] ) break;
737 a = bbuf;
738 while( *a ) a++;
739 if( *bbuf_s == '.' && bbuf_s[1] == '.' && !bbuf_s[2] )
740 {
741 a--;
742 if( a == bbuf ) break;
743 if( *a == '/' ) a--;
744 while( *a != '/' ) a--;
745 *++a = '\0';
746 break;
747 }
748 rb->strcpy( a, bbuf_s );
749 while( *a ) a++;
750 *a++ = '/';
751 *a = '\0';
752 fvi = si = 0;
753 break;
754 }
755 }
756
757#undef WIDTH
758#undef HEIGHT
759#undef LINE_SPACE
760}
761
762/***********************************************************************
763 * Font browser
764 *
765 * FIXME: This still needs some work ... it currently only works fine
766 * on the simulators, disk spins too much on real targets -> rendered
767 * font buffer needed.
768 ***********************************************************************/
769static bool browse_fonts( char *dst, int dst_size )
770{
771 char old_font[MAX_PATH];
772#define WIDTH ( LCD_WIDTH - 20 )
773#define HEIGHT ( LCD_HEIGHT - 20 )
774#define LINE_SPACE 2
775 int top, top_inside, left;
776
777 DIR *d;
778 struct dirent *de;
779 int fvi = 0; /* first visible item */
780 int lvi = 0; /* last visible item */
781 int si = 0; /* selected item */
782 int li = 0; /* last item */
783 int nvih = 0; /* next visible item height */
784 int i;
785
786 int cp = 0; /* current position */
787 int fh; /* font height */
788
789 rb->snprintf( old_font, MAX_PATH,
790 ROCKBOX_DIR FONT_DIR "/%s.fnt",
791 rb->global_settings->font_file );
792
793 while( 1 )
794 {
795 d = rb->PREFIX(opendir)( ROCKBOX_DIR FONT_DIR "/" );
796 if( !d )
797 {
798 return false;
799 }
800 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
801 i = 0;
802 li = -1;
803 while( i < fvi )
804 {
805 rb->PREFIX(readdir)( d );
806 i++;
807 }
808 cp = top_inside+LINE_SPACE;
809 while( cp < top+HEIGHT )
810 {
811 de = rb->PREFIX(readdir)( d );
812 if( !de )
813 {
814 li = i-1;
815 break;
816 }
817 if( rb->strlen( de->d_name ) < 4
818 || rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
819 ".fnt" ) )
820 continue;
821 rb->snprintf( bbuf, MAX_PATH, ROCKBOX_DIR FONT_DIR "/%s",
822 de->d_name );
823 rb->font_load( bbuf );
824 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
825 if( nvih > 0 )
826 {
827 nvih -= fh;
828 fvi++;
829 if( nvih < 0 ) nvih = 0;
830 i++;
831 continue;
832 }
833 if( cp + fh >= top+HEIGHT )
834 {
835 nvih = fh;
836 break;
837 }
838 rb->lcd_set_foreground((si==i?COLOR_WHITE:COLOR_BLACK));
839 rb->lcd_set_background((si==i?COLOR_BLUE:COLOR_LIGHTGRAY));
840 rb->lcd_putsxy( left+10, cp, de->d_name );
841 cp += fh + LINE_SPACE;
842 if( si == i )
843 rb->strcpy( bbuf_s, bbuf );
844 i++;
845 }
846 lvi = i-1;
847 if( li == -1 )
848 {
849 if( !(de = rb->PREFIX(readdir)( d ) ) )
850 {
851 li = lvi;
852 }
853 else if( !nvih && !rb->strlen( de->d_name ) < 4
854 && !rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
855 ".fnt" ) )
856 {
857 rb->snprintf( bbuf, MAX_PATH, ROCKBOX_DIR FONT_DIR "/%s",
858 de->d_name );
859 rb->font_load( bbuf );
860 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
861 nvih = fh;
862 }
863 }
864 rb->font_load( old_font );
865 rb->PREFIX(closedir)( d );
866
867 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
868
869 switch( rb->button_get(true) )
870 {
871 case ROCKPAINT_UP:
872 case ROCKPAINT_UP|BUTTON_REPEAT:
873 if( si > 0 )
874 {
875 si--;
876 if( si<fvi )
877 {
878 fvi = si;
879 }
880 }
881 break;
882
883 case ROCKPAINT_DOWN:
884 case ROCKPAINT_DOWN|BUTTON_REPEAT:
885 if( li == -1 || si < li )
886 {
887 si++;
888 }
889 break;
890
891 case ROCKPAINT_LEFT:
892 return false;
893
894 case ROCKPAINT_RIGHT:
895 case ROCKPAINT_DRAW:
896 rb->snprintf( dst, dst_size, "%s", bbuf_s );
897 return true;
898 }
899 if( si<=lvi )
900 {
901 nvih = 0;
902 }
903 }
904#undef WIDTH
905#undef HEIGHT
906#undef LINE_SPACE
907}
908
909/***********************************************************************
910 * HSVRGB Color chooser
911 ***********************************************************************/
912static unsigned int color_chooser( unsigned int color )
913{
914 int red = RGB_UNPACK_RED( color );
915 int green = RGB_UNPACK_GREEN( color );
916 int blue = RGB_UNPACK_BLUE( color );
917 int hue, saturation, value;
918 int r, g, b; /* temp variables */
919 int i, top, left;
920
921 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
922 Red = 3, Green = 4, Blue = 5 };
923 enum BaseColor current = Red;
924 bool has_changed;
925
926 char str[6] = "";
927
928 restore_screen();
929
930 rgb2hsv( red, green, blue, &hue, &saturation, &value );
931
932 while( 1 )
933 {
934 has_changed = false;
935 color = LCD_RGBPACK( red, green, blue );
936
937#define HEIGHT ( 100 )
938#define WIDTH ( 150 )
939
940 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
941 top -= 15;
942
943 for( i=0; i<100; i++ )
944 {
945 hsv2rgb( i*36, saturation, value, &r, &g, &b );
946 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
947 rb->lcd_vline( left+15+i, top+20, top+27 );
948 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
949 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
950 rb->lcd_vline( left+15+i, top+30, top+37 );
951 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
952 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
953 rb->lcd_vline( left+15+i, top+40, top+47 );
954 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
955 rb->lcd_vline( left+15+i, top+50, top+57 );
956 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
957 rb->lcd_vline( left+15+i, top+60, top+67 );
958 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
959 rb->lcd_vline( left+15+i, top+70, top+77 );
960 }
961
962 rb->lcd_set_foreground(COLOR_BLACK);
963#define POSITION( a, i ) \
964 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
965 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
966 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
967 rb->lcd_drawpixel( left+16+i, top + 28 + a );
968 POSITION( 0, hue/36 );
969 POSITION( 10, saturation*99/255 );
970 POSITION( 20, value*99/255 );
971 POSITION( 30, red*99/255 );
972 POSITION( 40, green*99/255 );
973 POSITION( 50, blue*99/255 );
974#undef POSITION
975 rb->lcd_set_background(COLOR_LIGHTGRAY);
976 rb->lcd_setfont( FONT_SYSFIXED );
977 rb->snprintf( str, 6, "%d", hue/10 );
978 rb->lcd_putsxy( left + 117, top + 20, str );
979 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
980 rb->lcd_putsxy( left + 117, top + 30, str );
981 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
982 rb->lcd_putsxy( left + 117, top + 40, str );
983 rb->snprintf( str, 6, "%d", red );
984 rb->lcd_putsxy( left + 117, top + 50, str );
985 rb->snprintf( str, 6, "%d", green );
986 rb->lcd_putsxy( left + 117, top + 60, str );
987 rb->snprintf( str, 6, "%d", blue );
988 rb->lcd_putsxy( left + 117, top + 70, str );
989 rb->lcd_setfont( FONT_UI );
990
991#define CURSOR( l ) \
992 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
993 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
994 CURSOR( 5 );
995#undef CURSOR
996
997 rb->lcd_set_foreground( color );
998 rb->lcd_fillrect( left+15, top+85, 100, 8 );
999
1000 rb->lcd_update();
1001
1002 switch( rb->button_get(true) )
1003 {
1004 case ROCKPAINT_UP:
1005 current = ( current + 5 )%6;
1006 break;
1007
1008 case ROCKPAINT_DOWN:
1009 current = (current + 1 )%6;
1010 break;
1011
1012 case ROCKPAINT_LEFT:
1013 has_changed = true;
1014 switch( current )
1015 {
1016 case Hue:
1017 hue = ( hue + 3600 - 10 )%3600;
1018 break;
1019 case Saturation:
1020 if( saturation ) saturation--;
1021 break;
1022 case Value:
1023 if( value ) value--;
1024 break;
1025 case Red:
1026 if( red ) red--;
1027 break;
1028 case Green:
1029 if( green ) green--;
1030 break;
1031 case Blue:
1032 if( blue ) blue--;
1033 break;
1034 }
1035 break;
1036
1037 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1038 has_changed = true;
1039 switch( current )
1040 {
1041 case Hue:
1042 hue = ( hue + 3600 - 100 )%3600;
1043 break;
1044 case Saturation:
1045 if( saturation >= 8 ) saturation-=8;
1046 else saturation = 0;
1047 break;
1048 case Value:
1049 if( value >= 8 ) value-=8;
1050 else value = 0;
1051 break;
1052 case Red:
1053 if( red >= 8 ) red-=8;
1054 else red = 0;
1055 break;
1056 case Green:
1057 if( green >= 8 ) green-=8;
1058 else green = 0;
1059 break;
1060 case Blue:
1061 if( blue >= 8 ) blue-=8;
1062 else blue = 0;
1063 break;
1064 }
1065 break;
1066
1067 case ROCKPAINT_RIGHT:
1068 has_changed = true;
1069 switch( current )
1070 {
1071 case Hue:
1072 hue = ( hue + 10 )%3600;
1073 break;
1074 case Saturation:
1075 if( saturation < 0xff ) saturation++;
1076 break;
1077 case Value:
1078 if( value < 0xff ) value++;
1079 break;
1080 case Red:
1081 if( red < 0xff ) red++;
1082 break;
1083 case Green:
1084 if( green < 0xff ) green++;
1085 break;
1086 case Blue:
1087 if( blue < 0xff ) blue++;
1088 break;
1089 }
1090 break;
1091
1092 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1093 has_changed = true;
1094 switch( current )
1095 {
1096 case Hue:
1097 hue = ( hue + 100 )%3600;
1098 break;
1099 case Saturation:
1100 if( saturation < 0xff - 8 ) saturation+=8;
1101 else saturation = 0xff;
1102 break;
1103 case Value:
1104 if( value < 0xff - 8 ) value+=8;
1105 else value = 0xff;
1106 break;
1107 case Red:
1108 if( red < 0xff - 8 ) red+=8;
1109 else red = 0xff;
1110 break;
1111 case Green:
1112 if( green < 0xff - 8 ) green+=8;
1113 else green = 0xff;
1114 break;
1115 case Blue:
1116 if( blue < 0xff - 8 ) blue+=8;
1117 else blue = 0xff;
1118 break;
1119 }
1120 break;
1121
1122 case ROCKPAINT_DRAW:
1123 return color;
1124 }
1125 if( has_changed )
1126 {
1127 switch( current )
1128 {
1129 case Hue:
1130 case Saturation:
1131 case Value:
1132 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1133 break;
1134
1135 case Red:
1136 case Green:
1137 case Blue:
1138 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1139 break;
1140 }
1141 }
1142#undef HEIGHT
1143#undef WIDTH
1144 }
1145}
1146
1147/***********************************************************************
1148 * Misc routines
1149 ***********************************************************************/
1150static void init_buffer(void)
1151{
1152 int i;
1153 fb_data color = rp_colors[ bgdrawcolor ];
1154 for( i = 0; i < ROWS*COLS; i++ )
1155 {
1156 save_buffer[i] = color;
1157 }
1158}
1159
1160static void draw_pixel(int x,int y)
1161{
1162 if( !preview )
1163 {
1164 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1165 if( isbg )
1166 {
1167 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1168 }
1169 else
1170 {
1171 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1172 }
1173 }
1174 rb->lcd_drawpixel(x,y);
1175}
1176
1177static void color_picker( int x, int y )
1178{
1179 if( preview )
1180 {
1181 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1182#define PSIZE 12
1183 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1184 rb->lcd_drawrect( x<COLS-PSIZE ? x + 2 : x - PSIZE, y<ROWS-PSIZE ? y + 2: y - PSIZE, PSIZE - 2, PSIZE - 2 );
1185 rb->lcd_set_drawmode(DRMODE_SOLID);
1186 rb->lcd_fillrect( x<COLS-PSIZE ? x+3 : x - PSIZE+1, y<ROWS-PSIZE ? y +3: y - PSIZE+1, PSIZE-4, PSIZE-4 );
1187#undef PSIZE
1188 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1189 }
1190 else
1191 {
1192 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1193 }
1194}
1195
1196static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1197/* This is a preview mode only function */
1198{
1199 int i,a;
1200 if( x1 > x2 )
1201 {
1202 i = x1;
1203 x1 = x2;
1204 x2 = i;
1205 }
1206 if( y1 > y2 )
1207 {
1208 i = y1;
1209 y1 = y2;
1210 y2 = i;
1211 }
1212 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1213 i = 0;
1214 for( a = x1; a < x2; a++, i++ )
1215 if( i%2 )
1216 rb->lcd_drawpixel( a, y1 );
1217 for( a = y1; a < y2; a++, i++ )
1218 if( i%2 )
1219 rb->lcd_drawpixel( x2, a );
1220 if( y2 != y1 )
1221 for( a = x2; a > x1; a--, i++ )
1222 if( i%2 )
1223 rb->lcd_drawpixel( a, y2 );
1224 if( x2 != x1 )
1225 for( a = y2; a > y1; a--, i++ )
1226 if( i%2 )
1227 rb->lcd_drawpixel( x1, a );
1228 rb->lcd_set_drawmode(DRMODE_SOLID);
1229}
1230
1231static void copy_to_clipboard( void )
1232{
1233 /* This needs to be optimised ... but i'm lazy ATM */
1234 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1235}
1236
1237/* no preview mode handling atm ... do we need it ? (one if) */
1238static void draw_invert( int x1, int y1, int x2, int y2 )
1239{
1240 int i;
1241 if( x1 > x2 )
1242 {
1243 i = x1;
1244 x1 = x2;
1245 x2 = i;
1246 }
1247 if( y1 > y2 )
1248 {
1249 i = y1;
1250 y1 = y2;
1251 y2 = i;
1252 }
1253
1254 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1255 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1256 rb->lcd_set_drawmode(DRMODE_SOLID);
1257
1258 for( ; y1<=y2; y1++ )
1259 {
1260 for( i = x1; i<=x2; i++ )
1261 {
1262 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1263 }
1264 }
1265 /*if( update )*/ rb->lcd_update();
1266}
1267
1268static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1269 int src_y2, int x1, int y1, int mode )
1270{
1271 int i;
1272 if( mode == SELECT_MENU_CUT )
1273 {
1274 i = drawcolor;
1275 drawcolor = bgdrawcolor;
1276 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1277 drawcolor = i;
1278 }
1279 if( src_x1 > src_x2 )
1280 {
1281 i = src_x1;
1282 src_x1 = src_x2;
1283 src_x2 = i;
1284 }
1285 if( src_y1 > src_y2 )
1286 {
1287 i = src_y1;
1288 src_y1 = src_y2;
1289 src_y2 = i;
1290 }
1291 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1292 x1, y1, src_x2-src_x1+1, src_y2-src_y1+1 );
1293 if( !preview )
1294 {
1295 for( i = 0; i <= src_y2 - src_y1; i++ )
1296 {
1297 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1298 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1299 (src_x2 - src_x1 + 1)*sizeof( fb_data ) );
1300 }
1301 }
1302}
1303
1304static void show_grid( bool update )
1305{
1306 int i;
1307 if( gridsize > 0 )
1308 {
1309 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1310 for( i = gridsize; i < COLS; i+= gridsize )
1311 {
1312 rb->lcd_vline( i, 0, ROWS-1 );
1313 }
1314 for( i = gridsize; i < ROWS; i+= gridsize )
1315 {
1316 rb->lcd_hline( 0, COLS-1, i );
1317 }
1318 rb->lcd_set_drawmode(DRMODE_SOLID);
1319 if( update ) rb->lcd_update();
1320 }
1321}
1322
1323static void draw_text( int x, int y )
1324{
1325 buffer.text.text[0] = '\0';
1326 rb->snprintf( buffer.text.old_font, MAX_PATH,
1327 ROCKBOX_DIR FONT_DIR "/%s.fnt",
1328 rb->global_settings->font_file );
1329 DEBUGF( "old font: %s\n", buffer.text.old_font );
1330 while( 1 )
1331 {
1332 int m = TEXT_MENU_TEXT;
1333 switch( m = menu_display( text_menu, m ) )
1334 {
1335 case TEXT_MENU_TEXT:
1336 rb->kbd_input( buffer.text.text, MAX_TEXT );
1337 restore_screen();
1338 break;
1339
1340 case TEXT_MENU_FONT:
1341 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1342 {
1343 rb->font_load( buffer.text.font );
1344 }
1345 break;
1346
1347 case TEXT_MENU_PREVIEW:
1348 while( 1 )
1349 {
1350 unsigned int button;
1351 restore_screen();
1352 rb->lcd_putsxy( x, y, buffer.text.text );
1353 rb->lcd_update();
1354 switch( button = rb->button_get( true ) )
1355 {
1356 case ROCKPAINT_LEFT:
1357 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1358 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1359 if (x<0) x=COLS-1;
1360 break;
1361
1362 case ROCKPAINT_RIGHT:
1363 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1364 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1365 if (x>=COLS) x=0;
1366 break;
1367
1368 case ROCKPAINT_UP:
1369 case ROCKPAINT_UP | BUTTON_REPEAT:
1370 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1371 if (y<0) y=ROWS-1;
1372 break;
1373
1374 case ROCKPAINT_DOWN:
1375 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1376 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1377 if (y>=ROWS-1) y=0;
1378 break;
1379
1380 case ROCKPAINT_DRAW:
1381 button = 1242;
1382 break;
1383 }
1384 if( button == 1242 ) break;
1385 }
1386 break;
1387
1388 case TEXT_MENU_APPLY:
1389 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1390 buffer.text.text );
1391 case TEXT_MENU_CANCEL:
1392 restore_screen();
1393 rb->font_load( buffer.text.old_font );
1394 return;
1395 }
1396 }
1397}
1398
1399static void draw_brush( int x, int y )
1400{
1401 int i,j;
1402 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1403 {
1404 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1405 {
1406 draw_pixel( x+i, y+j );
1407 }
1408 }
1409}
1410
1411static void draw_line( int x1, int y1, int x2, int y2 )
1412{
1413 x1 = x1<<1;
1414 y1 = y1<<1;
1415 x2 = x2<<1;
1416 y2 = y2<<1;
1417 int w = x1 - x2;
1418 int h = y1 - y2;
1419
1420 int x, y;
1421
1422 if( w == 0 && h == 0 )
1423 {
1424 draw_pixel( x1>>1, y1>>1 );
1425 return;
1426 }
1427
1428 if( w < 0 ) w *= -1;
1429 if( h < 0 ) h *= -1;
1430
1431 if( w > h )
1432 {
1433 if( x1 > x2 )
1434 {
1435 x = x2;
1436 y = y2;
1437 x2 = x1;
1438 y2 = y1;
1439 x1 = x;
1440 y1 = y;
1441 }
1442 w = x1 - x2;
1443 h = y1 - y2;
1444 while( x1 <= x2 )
1445 {
1446 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
1447 x1+=2;
1448 y1 = y2 - ( x2 - x1 ) * h / w;
1449 }
1450 }
1451 else /* h > w */
1452 {
1453 if( y1 > y2 )
1454 {
1455 x = x2;
1456 y = y2;
1457 x2 = x1;
1458 y2 = y1;
1459 x1 = x;
1460 y1 = y;
1461 }
1462 w = x1 - x2;
1463 h = y1 - y2;
1464 while( y1 <= y2 )
1465 {
1466 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
1467 y1+=2;
1468 x1 = x2 - ( y2 - y1 ) * w / h;
1469 }
1470 }
1471}
1472
1473static void draw_curve( int x1, int y1, int x2, int y2,
1474 int xa, int ya, int xb, int yb )
1475{
1476 int i = 0;
1477 short xl1, yl1;
1478 short xl2, yl2;
1479 short xl3, yl3;
1480 short xl4, yl4;
1481 short xr1, yr1;
1482 short xr2, yr2;
1483 short xr3, yr3;
1484 short xr4, yr4;
1485 short depth;
1486 short xh, yh;
1487
1488 if( x1 == x2 && y1 == y2 )
1489 {
1490 draw_pixel( x1, y1 );
1491 return;
1492 }
1493
1494// if( preview )
1495 {
1496 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1497 if( xa == -1 || ya == -1 )
1498 {
1499 rb->lcd_drawline( x1, y1, xb, yb );
1500 rb->lcd_drawline( x2, y2, xb, yb );
1501 }
1502 else
1503 {
1504 rb->lcd_drawline( x1, y1, xa, ya );
1505 rb->lcd_drawline( x2, y2, xb, yb );
1506 }
1507 rb->lcd_set_drawmode(DRMODE_SOLID);
1508 }
1509
1510 if( xa == -1 || ya == -1 )
1511 /* We only have 3 of the points
1512 * This will currently only be used in preview mode */
1513 {
1514#define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1515 buffer.bezier[i].x1 = a1; \
1516 buffer.bezier[i].y1 = b1; \
1517 buffer.bezier[i].x2 = a2; \
1518 buffer.bezier[i].y2 = b2; \
1519 buffer.bezier[i].x3 = a3; \
1520 buffer.bezier[i].y3 = b3; \
1521 buffer.bezier[i].depth = d; \
1522 i++;
1523#define POP( a1, b1, a2, b2, a3, b3, d ) \
1524 i--; \
1525 a1 = buffer.bezier[i].x1; \
1526 b1 = buffer.bezier[i].y1; \
1527 a2 = buffer.bezier[i].x2; \
1528 b2 = buffer.bezier[i].y2; \
1529 a3 = buffer.bezier[i].x3; \
1530 b3 = buffer.bezier[i].y3; \
1531 d = buffer.bezier[i].depth;
1532 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1533 while( i )
1534 {
1535 /* de Casteljau's algorithm (see wikipedia) */
1536 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1537 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1538 {
1539 xl2 = ( xl1 + xb )>>1;
1540 yl2 = ( yl1 + yb )>>1;
1541 xr2 = ( xb + xr3 )>>1;
1542 yr2 = ( yb + yr3 )>>1;
1543 xr1 = ( xl2 + xr2 )>>1;
1544 yr1 = ( yl2 + yr2 )>>1;
1545 xl3 = xr1;
1546 yl3 = yr1;
1547 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1548 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1549 }
1550 else
1551 {
1552 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1553 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1554 }
1555 }
1556#undef PUSH
1557#undef POP
1558 }
1559 else /* We have the 4 points */
1560 {
1561#define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1562 buffer.bezier[i].x1 = a1; \
1563 buffer.bezier[i].y1 = b1; \
1564 buffer.bezier[i].x2 = a2; \
1565 buffer.bezier[i].y2 = b2; \
1566 buffer.bezier[i].x3 = a3; \
1567 buffer.bezier[i].y3 = b3; \
1568 buffer.bezier[i].x4 = a4; \
1569 buffer.bezier[i].y4 = b4; \
1570 buffer.bezier[i].depth = d; \
1571 i++;
1572#define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1573 i--; \
1574 a1 = buffer.bezier[i].x1; \
1575 b1 = buffer.bezier[i].y1; \
1576 a2 = buffer.bezier[i].x2; \
1577 b2 = buffer.bezier[i].y2; \
1578 a3 = buffer.bezier[i].x3; \
1579 b3 = buffer.bezier[i].y3; \
1580 a4 = buffer.bezier[i].x4; \
1581 b4 = buffer.bezier[i].y4; \
1582 d = buffer.bezier[i].depth;
1583
1584 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1585 while( i )
1586 {
1587 /* de Casteljau's algorithm (see wikipedia) */
1588 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1589 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1590 {
1591 xl2 = ( xl1 + xa )>>1;
1592 yl2 = ( yl1 + ya )>>1;
1593 xh = ( xa + xb )>>1;
1594 yh = ( ya + yb )>>1;
1595 xr3 = ( xb + xr4 )>>1;
1596 yr3 = ( yb + yr4 )>>1;
1597 xl3 = ( xl2 + xh )>>1;
1598 yl3 = ( yl2 + yh )>>1;
1599 xr2 = ( xr3 + xh )>>1;
1600 yr2 = ( yr3 + yh )>>1;
1601 xl4 = ( xl3 + xr2 )>>1;
1602 yl4 = ( yl3 + yr2 )>>1;
1603 xr1 = xl4;
1604 yr1 = yl4;
1605 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1606 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1607 }
1608 else
1609 {
1610 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1611 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1612 }
1613 }
1614#undef PUSH
1615#undef POP
1616 }
1617}
1618
1619static void draw_rect( int x1, int y1, int x2, int y2 )
1620{
1621 draw_line( x1, y1, x1, y2 );
1622 draw_line( x1, y1, x2, y1 );
1623 draw_line( x1, y2, x2, y2 );
1624 draw_line( x2, y1, x2, y2 );
1625}
1626
1627static void togglebg( void )
1628{
1629 if( isbg )
1630 {
1631 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1632 }
1633 else
1634 {
1635 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1636 }
1637 isbg = !isbg;
1638}
1639
1640static void draw_rect_full( int x1, int y1, int x2, int y2 )
1641{
1642 /* GRUIK */
1643 int x = x1;
1644 togglebg();
1645 if( x < x2 )
1646 {
1647 do {
1648 draw_line( x, y1, x, y2 );
1649 } while( ++x <= x2 );
1650 }
1651 else
1652 {
1653 do {
1654 draw_line( x, y1, x, y2 );
1655 } while( --x >= x2 );
1656 }
1657 togglebg();
1658 draw_rect( x1, y1, x2, y2 );
1659}
1660
1661static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1662{
1663 /* TODO: simplify :) */
1664 int cx = (x1+x2)>>1;
1665 int cy = (y1+y2)>>1;
1666
1667 int rx = (x1-x2)>>1;
1668 int ry = (y1-y2)>>1;
1669 if( rx < 0 ) rx *= -1;
1670 if( ry < 0 ) ry *= -1;
1671
1672 if( rx == 0 || ry == 0 )
1673 {
1674 draw_line( x1, y1, x2, y2 );
1675 return;
1676 }
1677
1678 int x,y;
1679 int dst, old_dst;
1680
1681 for( x = 0; x < rx; x++ )
1682 {
1683 y = 0;
1684 dst = -0xfff;
1685 do {
1686 old_dst = dst;
1687 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1688 y++;
1689 } while( dst < 0 );
1690 if( -old_dst < dst ) y--;
1691 if( full )
1692 {
1693 draw_line( cx+x, cy, cx+x, cy+y );
1694 draw_line( cx+x, cy, cx+x, cy-y );
1695 draw_line( cx-x, cy, cx-x, cy+y );
1696 draw_line( cx-x, cy, cx-x, cy-y );
1697 }
1698 else
1699 {
1700 draw_pixel( cx+x, cy+y );
1701 draw_pixel( cx+x, cy-y );
1702 draw_pixel( cx-x, cy+y );
1703 draw_pixel( cx-x, cy-y );
1704 }
1705 }
1706 for( y = 0; y < ry; y++ )
1707 {
1708 x = 0;
1709 dst = -0xfff;
1710 do {
1711 old_dst = dst;
1712 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1713 x++;
1714 } while( dst < 0 );
1715 if( -old_dst < dst ) x--;
1716 if( full )
1717 {
1718 draw_line( cx+x, cy, cx+x, cy+y );
1719 draw_line( cx+x, cy, cx+x, cy-y );
1720 draw_line( cx-x, cy, cx-x, cy+y );
1721 draw_line( cx-x, cy, cx-x, cy-y );
1722 }
1723 else
1724 {
1725 draw_pixel( cx+x, cy+y );
1726 draw_pixel( cx+x, cy-y );
1727 draw_pixel( cx-x, cy+y );
1728 draw_pixel( cx-x, cy-y );
1729 }
1730 }
1731}
1732
1733static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1734{
1735 draw_oval( x1, y1, x2, y2, false );
1736}
1737
1738static void draw_oval_full( int x1, int y1, int x2, int y2 )
1739{
1740 togglebg();
1741 draw_oval( x1, y1, x2, y2, true );
1742 togglebg();
1743 draw_oval( x1, y1, x2, y2, false );
1744}
1745
1746static void draw_fill( int x0, int y0 )
1747{
1748#define PUSH( a, b ) \
1749 draw_pixel( (int)a, (int)b ); \
1750 buffer.coord[i].x = a; \
1751 buffer.coord[i].y = b; \
1752 i++;
1753#define POP( a, b ) \
1754 i--; \
1755 a = buffer.coord[i].x; \
1756 b = buffer.coord[i].y;
1757
1758 unsigned int i=0;
1759 short x = x0;
1760 short y = y0;
1761 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1762
1763 if( prev_color == rp_colors[ drawcolor ] ) return;
1764
1765 PUSH( x, y );
1766
1767 while( i != 0 )
1768 {
1769 POP( x, y );
1770 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1771 {
1772 PUSH( x-1, y );
1773 }
1774 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1775 {
1776 PUSH( x+1, y );
1777 }
1778 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1779 {
1780 PUSH( x, y-1 );
1781 }
1782 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1783 {
1784 PUSH( x, y+1 );
1785 }
1786 }
1787#undef PUSH
1788#undef POP
1789
1790}
1791
1792/* For preview purposes only */
1793static void line_gradient( int x1, int y1, int x2, int y2 )
1794{
1795 int r1, g1, b1;
1796 int r2, g2, b2;
1797 int h1, s1, v1, h2, s2, v2, r, g, b;
1798 int w, h, x, y;
1799
1800 bool a = false;
1801
1802 x1 <<= 1;
1803 y1 <<= 1;
1804 x2 <<= 1;
1805 y2 <<= 1;
1806
1807 w = x1 - x2;
1808 h = y1 - y2;
1809
1810 if( w == 0 && h == 0 )
1811 {
1812 draw_pixel( x1>>1, y1>>1 );
1813 return;
1814 }
1815
1816 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
1817 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
1818 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
1819 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
1820 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
1821 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
1822
1823 if( w < 0 )
1824 {
1825 w *= -1;
1826 a = true;
1827 }
1828 if( h < 0 )
1829 {
1830 h *= -1;
1831 a = !a;
1832 }
1833 if( a )
1834 {
1835 r = r1;
1836 r1 = r2;
1837 r2 = r;
1838 g = g1;
1839 g1 = g2;
1840 g2 = g;
1841 b = b1;
1842 b1 = b2;
1843 b2 = b;
1844 }
1845
1846 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
1847 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
1848
1849 if( w > h )
1850 {
1851 if( x1 > x2 )
1852 {
1853 x = x2;
1854 y = y2;
1855 x2 = x1;
1856 y2 = y1;
1857 x1 = x;
1858 y1 = y;
1859 }
1860 w = x1 - x2;
1861 h = y1 - y2;
1862 while( x1 <= x2 )
1863 {
1864 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
1865 s1+((s2-s1)*(x1-x2))/w,
1866 v1+((v2-v1)*(x1-x2))/w,
1867 &r, &g, &b );
1868 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
1869 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1870 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
1871 x1+=2;
1872 y1 = y2 - ( x2 - x1 ) * h / w;
1873 }
1874 }
1875 else /* h > w */
1876 {
1877 if( y1 > y2 )
1878 {
1879 x = x2;
1880 y = y2;
1881 x2 = x1;
1882 y2 = y1;
1883 x1 = x;
1884 y1 = y;
1885 }
1886 w = x1 - x2;
1887 h = y1 - y2;
1888 while( y1 <= y2 )
1889 {
1890 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
1891 s1+((s2-s1)*(y1-y2))/h,
1892 v1+((v2-v1)*(y1-y2))/h,
1893 &r, &g, &b );
1894 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
1895 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1896 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
1897 y1+=2;
1898 x1 = x2 - ( y2 - y1 ) * w / h;
1899 }
1900 }
1901 if( a )
1902 {
1903 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
1904 }
1905 else
1906 {
1907 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
1908 }
1909}
1910
1911static void linear_gradient( int x1, int y1, int x2, int y2 )
1912{
1913 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
1914 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
1915 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
1916 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
1917 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
1918 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
1919
1920 int h1, s1, v1, h2, s2, v2, r, g, b;
1921
1922 /* radius^2 */
1923 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
1924 int dist2, i=0;
1925
1926 /* We only propagate the gradient to neighboring pixels with the same
1927 * color as ( x1, y1 ) */
1928 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
1929
1930 int x = x1;
1931 int y = y1;
1932
1933 if( radius2 == 0 ) return;
1934 if( preview )
1935 {
1936 line_gradient( x1, y1, x2, y2 );
1937 }
1938
1939 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
1940 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
1941
1942#define PUSH( x0, y0 ) \
1943 buffer.coord[i].x = (short)(x0); \
1944 buffer.coord[i].y = (short)(y0); \
1945 i++;
1946#define POP( a, b ) \
1947 i--; \
1948 a = (int)buffer.coord[i].x; \
1949 b = (int)buffer.coord[i].y;
1950
1951 PUSH( x, y );
1952
1953 while( i != 0 )
1954 {
1955 POP( x, y );
1956
1957 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
1958 if( dist2 <= 0 )
1959 {
1960 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
1961 }
1962 else if( dist2 < radius2 )
1963 {
1964 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
1965 s1+((s2-s1)*dist2)/radius2,
1966 v1+((v2-v1)*dist2)/radius2,
1967 &r, &g, &b );
1968 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
1969 }
1970 else
1971 {
1972 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
1973 }
1974 if( rp_colors[ drawcolor ] == prev_color )
1975 {
1976 if( rp_colors[ drawcolor ])
1977 rp_colors[ drawcolor ]--; /* GRUIK */
1978 else
1979 rp_colors[ drawcolor ]++; /* GRUIK */
1980 }
1981 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1982 draw_pixel( x, y );
1983
1984 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1985 {
1986 PUSH( x-1, y );
1987 }
1988 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1989 {
1990 PUSH( x+1, y );
1991 }
1992 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1993 {
1994 PUSH( x, y-1 );
1995 }
1996 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1997 {
1998 PUSH( x, y+1 );
1999 }
2000 }
2001#undef PUSH
2002#undef POP
2003
2004 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2005}
2006
2007static void radial_gradient( int x1, int y1, int x2, int y2 )
2008{
2009 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2010 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2011 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2012 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2013 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2014 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2015
2016 int h1, s1, v1, h2, s2, v2, r, g, b;
2017
2018 /* radius^2 */
2019 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2020 int dist2, i=0;
2021
2022 /* We only propagate the gradient to neighboring pixels with the same
2023 * color as ( x1, y1 ) */
2024 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2025
2026 int x = x1;
2027 int y = y1;
2028
2029 if( radius2 == 0 ) return;
2030 if( preview )
2031 {
2032 line_gradient( x1, y1, x2, y2 );
2033 }
2034
2035 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2036 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2037
2038#define PUSH( x0, y0 ) \
2039 buffer.coord[i].x = (short)(x0); \
2040 buffer.coord[i].y = (short)(y0); \
2041 i++;
2042#define POP( a, b ) \
2043 i--; \
2044 a = (int)buffer.coord[i].x; \
2045 b = (int)buffer.coord[i].y;
2046
2047 PUSH( x, y );
2048
2049 while( i != 0 )
2050 {
2051 POP( x, y );
2052
2053 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2054 {
2055 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2056 s1+((s2-s1)*dist2)/radius2,
2057 v1+((v2-v1)*dist2)/radius2,
2058 &r, &g, &b );
2059 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2060 }
2061 else
2062 {
2063 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2064 }
2065 if( rp_colors[ drawcolor ] == prev_color )
2066 {
2067 if( rp_colors[ drawcolor ])
2068 rp_colors[ drawcolor ]--; /* GRUIK */
2069 else
2070 rp_colors[ drawcolor ]++; /* GRUIK */
2071 }
2072 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2073 draw_pixel( x, y );
2074
2075 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2076 {
2077 PUSH( x-1, y );
2078 }
2079 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2080 {
2081 PUSH( x+1, y );
2082 }
2083 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2084 {
2085 PUSH( x, y-1 );
2086 }
2087 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2088 {
2089 PUSH( x, y+1 );
2090 }
2091 }
2092#undef PUSH
2093#undef POP
2094
2095 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2096}
2097
2098static void draw_toolbars(bool update)
2099{
2100 int i;
2101#define TOP (LCD_HEIGHT-TB_HEIGHT)
2102 rb->lcd_set_background( COLOR_LIGHTGRAY );
2103 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2104 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2105 rb->lcd_set_foreground( COLOR_BLACK );
2106 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2107
2108 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2109 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2110 TB_SC_SIZE, TB_SC_SIZE );
2111 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2112 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2113 TB_SC_SIZE, TB_SC_SIZE );
2114 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2115 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2116 TB_SC_SIZE, TB_SC_SIZE );
2117 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2118 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2119 TB_SC_SIZE, TB_SC_SIZE );
2120
2121 for( i=0; i<18; i++ )
2122 {
2123 rb->lcd_set_foreground( rp_colors[i] );
2124 rb->lcd_fillrect(
2125 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2126 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2127 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2128 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2129 rb->lcd_drawrect(
2130 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2131 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2132 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2133 }
2134
2135#define SEPARATOR( x, y ) \
2136 rb->lcd_set_foreground( COLOR_WHITE ); \
2137 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2138 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2139 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2140 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2141
2142 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2143 TB_TL_WIDTH, TB_TL_HEIGHT );
2144 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2145 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2146 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2147 TB_TL_SIZE, TB_TL_SIZE );
2148
2149 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2150
2151 rb->lcd_setfont( FONT_SYSFIXED );
2152 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2153 rb->lcd_setfont( FONT_UI );
2154#undef TOP
2155
2156 if( update ) rb->lcd_update();
2157}
2158
2159static void toolbar( void )
2160{
2161 unsigned int button, i, j;
2162 restore_screen();
2163 draw_toolbars( false );
2164 y = LCD_HEIGHT-TB_HEIGHT/2;
2165 inv_cursor( true );
2166 while( 1 )
2167 {
2168 switch( button = rb->button_get( true ) )
2169 {
2170 case ROCKPAINT_DRAW:
2171#define TOP ( LCD_HEIGHT - TB_HEIGHT )
2172 if( y >= TOP + TB_SC_FG_TOP
2173 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2174 && x >= TB_SC_FG_LEFT
2175 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2176 {
2177 /* click on the foreground color */
2178 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2179 }
2180 else if( y >= TOP + TB_SC_BG_TOP
2181 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2182 && x >= TB_SC_BG_LEFT
2183 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2184 {
2185 /* click on the background color */
2186 i = drawcolor;
2187 drawcolor = bgdrawcolor;
2188 bgdrawcolor = i;
2189 }
2190 else if( y >= TOP + TB_PL_TOP
2191 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2192 && x >= TB_PL_LEFT
2193 && x < TB_PL_LEFT + TB_PL_WIDTH )
2194 {
2195 /* click on the palette */
2196 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2197 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2198 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2199 break;
2200 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2201 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2202 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2203 }
2204 else if( y >= TOP+TB_TL_TOP
2205 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2206 && x >= TB_TL_LEFT
2207 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2208 {
2209 /* click on the tools */
2210 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2211 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2212 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2213 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2214 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2215 tool = i*2+j;
2216 prev_x = -1;
2217 prev_y = -1;
2218 prev_x2 = -1;
2219 prev_y2 = -1;
2220 prev_x3 = -1;
2221 prev_y3 = -1;
2222 preview = false;
2223 }
2224 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2225 {
2226 /* menu button */
2227 goto_menu();
2228 }
2229#undef TOP
2230 restore_screen();
2231 draw_toolbars( false );
2232 inv_cursor( true );
2233 break;
2234
2235 case ROCKPAINT_LEFT:
2236 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2237 inv_cursor(false);
2238 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2239 if (x<0) x=COLS-1;
2240 inv_cursor(true);
2241 break;
2242
2243 case ROCKPAINT_RIGHT:
2244 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2245 inv_cursor(false);
2246 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2247 if (x>=COLS) x=0;
2248 inv_cursor(true);
2249 break;
2250
2251 case ROCKPAINT_UP:
2252 case ROCKPAINT_UP | BUTTON_REPEAT:
2253 inv_cursor(false);
2254 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2255 if (y<LCD_HEIGHT-TB_HEIGHT)
2256 {
2257 return;
2258 }
2259 inv_cursor(true);
2260 break;
2261
2262 case ROCKPAINT_DOWN:
2263 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2264 inv_cursor(false);
2265 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2266 if (y>=LCD_HEIGHT)
2267 {
2268 y = 0;
2269 return;
2270 }
2271 inv_cursor(true);
2272 break;
2273
2274 case ROCKPAINT_TOOLBAR:
2275 case ROCKPAINT_TOOLBAR2:
2276 return;
2277 }
2278 if( quit ) return;
2279 }
2280}
2281
2282static void inv_cursor(bool update)
2283{
2284 rb->lcd_set_foreground(COLOR_BLACK);
2285 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2286 /* cross painting */
2287 rb->lcd_drawline(x-4,y,x-1,y);
2288 rb->lcd_drawline(x+1,y,x+4,y);
2289 rb->lcd_drawline(x,y-4,x,y-1);
2290 rb->lcd_drawline(x,y+1,x,y+4);
2291 rb->lcd_set_foreground(rp_colors[drawcolor]);
2292 rb->lcd_set_drawmode(DRMODE_SOLID);
2293
2294 if( update ) rb->lcd_update();
2295}
2296
2297static void restore_screen(void)
2298{
2299 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2300}
2301
2302static void clear_drawing(void)
2303{
2304 init_buffer();
2305 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2306 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2307 rb->lcd_update();
2308}
2309
2310static void goto_menu(void)
2311{
2312 int multi;
2313
2314 while( 1 )
2315 {
2316 switch( menu_display( main_menu, 1 ) )
2317 {
2318 case MAIN_MENU_NEW:
2319 clear_drawing();
2320 return;
2321
2322 case MAIN_MENU_LOAD:
2323 if( browse( filename, MAX_PATH, "/" ) )
2324 {
2325 if( load_bitmap( filename ) <= 0 )
2326 {
2327 rb->splash( 1*HZ, true, "Error while loading %s",
2328 filename );
2329 }
2330 else
2331 {
2332 rb->splash( 1*HZ, true, "Image loaded (%s)", filename );
2333 restore_screen();
2334 inv_cursor(true);
2335 return;
2336 }
2337 }
2338 break;
2339
2340 case MAIN_MENU_SAVE:
2341 if( !rb->kbd_input( filename, MAX_PATH ) )
2342 {
2343 save_bitmap( filename );
2344 rb->splash( 1*HZ, true, "File saved (%s)", filename );
2345 }
2346 break;
2347
2348 case MAIN_MENU_BRUSH_SIZE:
2349 multi = menu_display( size_menu, bsize );
2350 if( multi != - 1 )
2351 bsize = multi;
2352 break;
2353
2354 case MAIN_MENU_BRUSH_SPEED:
2355 multi = menu_display( speed_menu, bspeed );
2356 if( multi != -1 )
2357 bspeed = multi;
2358 break;
2359
2360 case MAIN_MENU_COLOR:
2361 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2362 break;
2363
2364 case MAIN_MENU_GRID_SIZE:
2365 multi = menu_display( gridsize_menu, gridsize );
2366 if( multi != - 1 )
2367 gridsize = multi;
2368 break;
2369
2370 case MAIN_MENU_EXIT:
2371 quit=true;
2372 return;
2373
2374 case MAIN_MENU_RESUME:
2375 case MENU_ESC:
2376 return;
2377 }/* end switch */
2378 }/* end while */
2379}
2380
2381static void reset_tool( void )
2382{
2383 prev_x = -1;
2384 prev_y = -1;
2385 prev_x2 = -1;
2386 prev_y2 = -1;
2387 prev_x3 = -1;
2388 prev_y3 = -1;
2389 tool_mode = -1;
2390 preview = false;
2391}
2392
2393static bool rockpaint_loop( void )
2394{
2395 int button=0,i,j;
2396 int accelaration;
2397
2398 while (!quit) {
2399 button = rb->button_get(true);
2400
2401 if( tool == Brush && prev_x != -1 )
2402 {
2403 accelaration = 1;
2404 }
2405 else if( button & BUTTON_REPEAT )
2406 {
2407 accelaration = 4;
2408 }
2409 else
2410 {
2411 accelaration = 1;
2412 }
2413
2414 switch(button)
2415 {
2416 case ROCKPAINT_QUIT:
2417 rb->lcd_set_drawmode(DRMODE_SOLID);
2418 return PLUGIN_OK;
2419
2420 case ROCKPAINT_MENU:
2421 inv_cursor(false);
2422 goto_menu();
2423 inv_cursor(true);
2424 break;
2425
2426 case ROCKPAINT_DRAW:
2427 inv_cursor(false);
2428 switch( tool )
2429 {
2430 case Brush:
2431 if( prev_x == -1 ) prev_x = 1;
2432 else prev_x = -1;
2433 break;
2434
2435 case SelectRectangle:
2436 case Line:
2437 case Curve:
2438 case Rectangle:
2439 case RectangleFull:
2440 case Oval:
2441 case OvalFull:
2442 case LinearGradient:
2443 case RadialGradient:
2444 /* Curve uses 4 points, others use 2 */
2445 if( prev_x == -1 || prev_y == -1 )
2446 {
2447 prev_x = x;
2448 prev_y = y;
2449 preview = true;
2450 }
2451 else if( tool == Curve
2452 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2453 {
2454 prev_x2 = x;
2455 prev_y2 = y;
2456 }
2457 else if( tool == SelectRectangle
2458 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2459 {
2460 tool_mode = menu_display( select_menu,
2461 SELECT_MENU_CUT );
2462 switch( tool_mode )
2463 {
2464 case SELECT_MENU_CUT:
2465 case SELECT_MENU_COPY:
2466 prev_x2 = x;
2467 prev_y2 = y;
2468 copy_to_clipboard();
2469 break;
2470
2471 case SELECT_MENU_INVERT:
2472 draw_invert( prev_x, prev_y, x, y );
2473 reset_tool();
2474 break;
2475
2476 case SELECT_MENU_CANCEL:
2477 reset_tool();
2478 break;
2479
2480 case MENU_ESC:
2481 break;
2482 }
2483 }
2484 else if( tool == Curve
2485 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2486 {
2487 prev_x3 = x;
2488 prev_y3 = y;
2489 }
2490 else
2491 {
2492 preview = false;
2493 switch( tool )
2494 {
2495 case SelectRectangle:
2496 draw_paste_rectangle( prev_x, prev_y,
2497 prev_x2, prev_y2,
2498 x, y, tool_mode );
2499 break;
2500 case Line:
2501 draw_line( prev_x, prev_y, x, y );
2502 break;
2503 case Curve:
2504 draw_curve( prev_x, prev_y,
2505 prev_x2, prev_y2,
2506 prev_x3, prev_y3,
2507 x, y );
2508 break;
2509 case Rectangle:
2510 draw_rect( prev_x, prev_y, x, y );
2511 break;
2512 case RectangleFull:
2513 draw_rect_full( prev_x, prev_y, x, y );
2514 break;
2515 case Oval:
2516 draw_oval_empty( prev_x, prev_y, x, y );
2517 break;
2518 case OvalFull:
2519 draw_oval_full( prev_x, prev_y, x, y );
2520 break;
2521 case LinearGradient:
2522 linear_gradient( prev_x, prev_y, x, y );
2523 break;
2524 case RadialGradient:
2525 radial_gradient( prev_x, prev_y, x, y );
2526 break;
2527 default:
2528 break;
2529 }
2530 reset_tool();
2531 }
2532 break;
2533
2534 case Fill:
2535 draw_fill( x, y );
2536 break;
2537
2538 case ColorPicker:
2539 color_picker( x, y );
2540 break;
2541
2542 case Text:
2543 draw_text( x, y );
2544 break;
2545
2546 default:
2547 break;
2548 }
2549 inv_cursor(true);
2550 break;
2551
2552 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2553 if( tool == Curve )
2554 {
2555 /* 3 point bezier curve */
2556 preview = false;
2557 draw_curve( prev_x, prev_y,
2558 prev_x2, prev_y2,
2559 -1, -1,
2560 x, y );
2561 reset_tool();
2562 restore_screen();
2563 inv_cursor( true );
2564 }
2565 break;
2566
2567 case ROCKPAINT_TOOLBAR:
2568 i = x; j = y;
2569 x = 10;
2570 toolbar();
2571 x = i; y = j;
2572 restore_screen();
2573 inv_cursor(true);
2574 break;
2575
2576 case ROCKPAINT_TOOLBAR2:
2577 i = x; j = y;
2578 x = 110;
2579 toolbar();
2580 x = i; y = j;
2581 restore_screen();
2582 inv_cursor(true);
2583 break;
2584
2585 case ROCKPAINT_LEFT:
2586 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2587 inv_cursor(false);
2588 x-=bspeed * accelaration;
2589 if (x<0) x=COLS-1;
2590 inv_cursor(true);
2591 break;
2592
2593 case ROCKPAINT_RIGHT:
2594 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2595 inv_cursor(false);
2596 x+=bspeed * accelaration;
2597 if (x>=COLS) x=0;
2598 inv_cursor(true);
2599 break;
2600
2601 case ROCKPAINT_UP:
2602 case ROCKPAINT_UP | BUTTON_REPEAT:
2603 inv_cursor(false);
2604 y-=bspeed * accelaration;
2605 if (y<0) y=ROWS-1;
2606 inv_cursor(true);
2607 break;
2608
2609 case ROCKPAINT_DOWN:
2610 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2611 inv_cursor(false);
2612 y+=bspeed * accelaration;
2613 if (y>=ROWS)
2614 {
2615 toolbar();
2616 restore_screen();
2617 }
2618 inv_cursor(true);
2619 break;
2620
2621 default:
2622 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2623 return PLUGIN_USB_CONNECTED;
2624 break;
2625 }
2626 if( tool == Brush && prev_x == 1 )
2627 {
2628 inv_cursor(false);
2629 draw_brush( x, y );
2630 inv_cursor(true);
2631 }
2632 if( preview || tool == ColorPicker )
2633 /* always preview color picker */
2634 {
2635 restore_screen();
2636 switch( tool )
2637 {
2638 case SelectRectangle:
2639 if( prev_x2 == -1 || prev_y2 == -1 )
2640 {
2641 /* we are defining the selection */
2642 draw_select_rectangle( prev_x, prev_y, x, y );
2643 }
2644 else
2645 {
2646 /* we are pasting the selected data */
2647 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2648 prev_y2, x, y, tool_mode );
2649 prev_x3 = prev_x2-prev_x;
2650 if( prev_x3 < 0 ) prev_x3 *= -1;
2651 prev_y3 = prev_y2-prev_y;
2652 if( prev_y3 < 0 ) prev_y3 *= -1;
2653 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2654 prev_x3 = -1;
2655 prev_y3 = -1;
2656 }
2657 break;
2658
2659 case Brush:
2660 break;
2661
2662 case Line:
2663 draw_line( prev_x, prev_y, x, y );
2664 break;
2665
2666 case Curve:
2667 if( prev_x2 == -1 || prev_y2 == -1 )
2668 {
2669 draw_line( prev_x, prev_y, x, y );
2670 }
2671 else
2672 {
2673 draw_curve( prev_x, prev_y,
2674 prev_x2, prev_y2,
2675 prev_x3, prev_y3,
2676 x, y );
2677 }
2678 break;
2679
2680 case Rectangle:
2681 draw_rect( prev_x, prev_y, x, y );
2682 break;
2683
2684 case RectangleFull:
2685 draw_rect_full( prev_x, prev_y, x, y );
2686 break;
2687
2688 case Oval:
2689 draw_oval_empty( prev_x, prev_y, x, y );
2690 break;
2691
2692 case OvalFull:
2693 draw_oval_full( prev_x, prev_y, x, y );
2694 break;
2695
2696 case Fill:
2697 break;
2698
2699 case ColorPicker:
2700 preview = true;
2701 color_picker( x, y );
2702 preview = false;
2703 break;
2704
2705 case LinearGradient:
2706 line_gradient( prev_x, prev_y, x, y );
2707 break;
2708
2709 case RadialGradient:
2710 line_gradient( prev_x, prev_y, x, y );
2711 break;
2712
2713 case Text:
2714 default:
2715 break;
2716 }
2717 inv_cursor( true );
2718 }
2719 if( gridsize > 0 )
2720 {
2721 show_grid( true );
2722 show_grid( false );
2723 }
2724 }
2725
2726 return PLUGIN_OK;
2727}
2728
2729static int load_bitmap( char *file )
2730{
2731 struct bitmap bm;
2732 bool ret;
2733 bm.data = (char*)save_buffer;
2734 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2735 FORMAT_NATIVE );
2736 if( bm.width < COLS )
2737 {
2738 int l;
2739 for( l = bm.height-1; l > 0; l-- )
2740 {
2741 rb->memmove( save_buffer+l*COLS, save_buffer+l*bm.width,
2742 sizeof( fb_data )*bm.width );
2743 }
2744 for( l = 0; l < bm.height; l++ )
2745 {
2746 rb->memset( save_buffer+l*COLS+bm.width, rp_colors[ bgdrawcolor ],
2747 sizeof( fb_data )*(COLS-bm.width) );
2748 }
2749 rb->memset( save_buffer+COLS*bm.height, rp_colors[ bgdrawcolor ],
2750 sizeof( fb_data )*COLS*(ROWS-bm.height) );
2751 }
2752 return ret;
2753}
2754
2755static int save_bitmap( char *file )
2756{
2757 struct bitmap bm;
2758 bm.data = (char*)save_buffer;
2759 bm.height = ROWS;
2760 bm.width = COLS;
2761 bm.format = FORMAT_NATIVE;
2762 return save_bmp_file( file, &bm, rb );
2763}
2764
2765enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
2766{
2767 /** must have stuff **/
2768 rb = api;
2769
2770 rb->lcd_set_foreground(COLOR_WHITE);
2771 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
2772 rb->splash( HZ/2, true, "Rock Paint");
2773
2774 rb->lcd_clear_display();
2775
2776 filename[0] = '\0';
2777
2778 if( parameter )
2779 {
2780 if( load_bitmap( parameter ) <= 0 )
2781 {
2782 rb->splash( 1*HZ, true, "Error");
2783 clear_drawing();
2784 }
2785 else
2786 {
2787 rb->splash( 1*HZ, true, "Image loaded (%s)", parameter );
2788 restore_screen();
2789 rb->strcpy( filename, parameter );
2790 }
2791 }
2792 else
2793 {
2794 clear_drawing();
2795 }
2796 inv_cursor(true);
2797
2798 return rockpaint_loop();
2799}
diff --git a/apps/plugins/viewers.config b/apps/plugins/viewers.config
index ea95ed184b..7b937a90a9 100644
--- a/apps/plugins/viewers.config
+++ b/apps/plugins/viewers.config
@@ -20,4 +20,5 @@ ss,rocks/sudoku, 55 55 55 55 55 55
20wav,viewers/wav2wv, 00 00 00 00 00 00 20wav,viewers/wav2wv, 00 00 00 00 00 00
21wav,viewers/mp3_encoder, 00 00 00 00 00 00 21wav,viewers/mp3_encoder, 00 00 00 00 00 00
22wav,viewers/wavplay,60 7F 05 35 3F 00 22wav,viewers/wavplay,60 7F 05 35 3F 00
23bmp,rocks/rockpaint, 01 10 01 10 01 10
23 24