Fix red in bootloaders
[maemo-rb.git] / apps / plugins / rockpaint.c
blob20bebdd11fd84eb7e47d5ab919835b8438fedb6b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Antoine Cellerier <dionoea -at- videolan -dot- org>
11 * Based on parts of rockpaint 0.45, Copyright (C) 2005 Eli Sherer
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 /**
24 * TODO:
25 * - implement 2 layers with alpha colors
26 * - take brush width into account when drawing shapes
27 * - handle bigger than screen bitmaps
28 * - cache fonts when building the font preview (else it only works well on simulators because they have "fast" disk read)
31 #include "plugin.h"
32 #include "lib/pluginlib_bmp.h"
33 #include "lib/rgb_hsv.h"
35 PLUGIN_HEADER
37 /***********************************************************************
38 * Buttons
39 ***********************************************************************/
41 #if CONFIG_KEYPAD == IRIVER_H300_PAD
42 #define ROCKPAINT_QUIT BUTTON_OFF
43 #define ROCKPAINT_DRAW BUTTON_SELECT
44 #define ROCKPAINT_MENU BUTTON_ON
45 #define ROCKPAINT_TOOLBAR BUTTON_REC
46 #define ROCKPAINT_TOOLBAR2 BUTTON_MODE
47 #define ROCKPAINT_UP BUTTON_UP
48 #define ROCKPAINT_DOWN BUTTON_DOWN
49 #define ROCKPAINT_LEFT BUTTON_LEFT
50 #define ROCKPAINT_RIGHT BUTTON_RIGHT
52 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
53 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
54 #define ROCKPAINT_QUIT ( ~BUTTON_MAIN )
55 #define ROCKPAINT_DRAW BUTTON_SELECT
56 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_MENU )
57 #define ROCKPAINT_TOOLBAR ( BUTTON_MENU | BUTTON_LEFT )
58 #define ROCKPAINT_TOOLBAR2 ( BUTTON_MENU | BUTTON_RIGHT )
59 #define ROCKPAINT_UP BUTTON_MENU
60 #define ROCKPAINT_DOWN BUTTON_PLAY
61 #define ROCKPAINT_LEFT BUTTON_LEFT
62 #define ROCKPAINT_RIGHT BUTTON_RIGHT
64 #elif ( CONFIG_KEYPAD == IAUDIO_X5M5_PAD )
65 #define ROCKPAINT_QUIT BUTTON_POWER
66 #define ROCKPAINT_DRAW BUTTON_SELECT
67 #define ROCKPAINT_MENU BUTTON_PLAY
68 #define ROCKPAINT_TOOLBAR BUTTON_REC
69 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
70 #define ROCKPAINT_UP BUTTON_UP
71 #define ROCKPAINT_DOWN BUTTON_DOWN
72 #define ROCKPAINT_LEFT BUTTON_LEFT
73 #define ROCKPAINT_RIGHT BUTTON_RIGHT
75 #elif CONFIG_KEYPAD == GIGABEAT_PAD
76 #define ROCKPAINT_QUIT BUTTON_POWER
77 #define ROCKPAINT_DRAW BUTTON_SELECT
78 #define ROCKPAINT_MENU BUTTON_MENU
79 #define ROCKPAINT_TOOLBAR BUTTON_A
80 #define ROCKPAINT_TOOLBAR2 ( BUTTON_A | BUTTON_LEFT )
81 #define ROCKPAINT_UP BUTTON_UP
82 #define ROCKPAINT_DOWN BUTTON_DOWN
83 #define ROCKPAINT_LEFT BUTTON_LEFT
84 #define ROCKPAINT_RIGHT BUTTON_RIGHT
86 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
87 (CONFIG_KEYPAD == SANSA_C200_PAD)
88 #define ROCKPAINT_QUIT BUTTON_POWER
89 #define ROCKPAINT_DRAW BUTTON_SELECT
90 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_POWER )
91 #define ROCKPAINT_TOOLBAR BUTTON_REC
92 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
93 #define ROCKPAINT_UP BUTTON_UP
94 #define ROCKPAINT_DOWN BUTTON_DOWN
95 #define ROCKPAINT_LEFT BUTTON_LEFT
96 #define ROCKPAINT_RIGHT BUTTON_RIGHT
98 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
99 #define ROCKPAINT_QUIT (BUTTON_HOME|BUTTON_REPEAT)
100 #define ROCKPAINT_DRAW BUTTON_SELECT
101 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_DOWN )
102 #define ROCKPAINT_TOOLBAR ( BUTTON_SELECT | BUTTON_LEFT )
103 #define ROCKPAINT_TOOLBAR2 ( BUTTON_SELECT | BUTTON_RIGHT )
104 #define ROCKPAINT_UP BUTTON_UP
105 #define ROCKPAINT_DOWN BUTTON_DOWN
106 #define ROCKPAINT_LEFT BUTTON_LEFT
107 #define ROCKPAINT_RIGHT BUTTON_RIGHT
109 #elif ( CONFIG_KEYPAD == IRIVER_H10_PAD )
110 #define ROCKPAINT_QUIT BUTTON_POWER
111 #define ROCKPAINT_DRAW BUTTON_FF
112 #define ROCKPAINT_MENU BUTTON_PLAY
113 #define ROCKPAINT_TOOLBAR BUTTON_REW
114 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
115 #define ROCKPAINT_UP BUTTON_SCROLL_UP
116 #define ROCKPAINT_DOWN BUTTON_SCROLL_DOWN
117 #define ROCKPAINT_LEFT BUTTON_LEFT
118 #define ROCKPAINT_RIGHT BUTTON_RIGHT
120 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
121 #define ROCKPAINT_QUIT BUTTON_BACK
122 #define ROCKPAINT_DRAW BUTTON_SELECT
123 #define ROCKPAINT_MENU BUTTON_MENU
124 #define ROCKPAINT_TOOLBAR BUTTON_PLAY
125 #define ROCKPAINT_TOOLBAR2 ( BUTTON_PLAY | BUTTON_LEFT )
126 #define ROCKPAINT_UP BUTTON_UP
127 #define ROCKPAINT_DOWN BUTTON_DOWN
128 #define ROCKPAINT_LEFT BUTTON_LEFT
129 #define ROCKPAINT_RIGHT BUTTON_RIGHT
131 #elif ( CONFIG_KEYPAD == COWOND2_PAD )
132 #define ROCKPAINT_QUIT BUTTON_POWER
133 #define ROCKPAINT_MENU BUTTON_MENU
135 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
136 #define ROCKPAINT_QUIT BUTTON_BACK
137 #define ROCKPAINT_DRAW BUTTON_SELECT
138 #define ROCKPAINT_MENU BUTTON_MENU
139 #define ROCKPAINT_TOOLBAR BUTTON_PLAY
140 #define ROCKPAINT_TOOLBAR2 ( BUTTON_PLAY | BUTTON_LEFT )
141 #define ROCKPAINT_UP BUTTON_UP
142 #define ROCKPAINT_DOWN BUTTON_DOWN
143 #define ROCKPAINT_LEFT BUTTON_LEFT
144 #define ROCKPAINT_RIGHT BUTTON_RIGHT
146 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
147 #define ROCKPAINT_QUIT BUTTON_POWER
148 #define ROCKPAINT_DRAW BUTTON_SELECT
149 #define ROCKPAINT_MENU BUTTON_MENU
150 #define ROCKPAINT_TOOLBAR BUTTON_VIEW
151 #define ROCKPAINT_TOOLBAR2 BUTTON_PLAYLIST
152 #define ROCKPAINT_UP BUTTON_UP
153 #define ROCKPAINT_DOWN BUTTON_DOWN
154 #define ROCKPAINT_LEFT BUTTON_LEFT
155 #define ROCKPAINT_RIGHT BUTTON_RIGHT
157 #elif ( CONFIG_KEYPAD == ONDAVX747_PAD )
158 #define ROCKPAINT_QUIT BUTTON_POWER
159 #define ROCKPAINT_MENU BUTTON_MENU
161 #elif CONFIG_KEYPAD == MROBE500_PAD
162 #define ROCKPAINT_QUIT BUTTON_POWER
164 #else
165 #error "Please define keys for this keypad"
166 #endif
168 #ifdef HAVE_TOUCHSCREEN
169 #ifndef ROCKPAINT_QUIT
170 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
171 #endif
172 #ifndef ROCKPAINT_DRAW
173 #define ROCKPAINT_DRAW BUTTON_CENTER
174 #endif
175 #ifndef ROCKPAINT_MENU
176 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
177 #endif
178 #ifndef ROCKPAINT_TOOLBAR
179 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
180 #endif
181 #ifndef ROCKPAINT_TOOLBAR2
182 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
183 #endif
184 #ifndef ROCKPAINT_UP
185 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
186 #endif
187 #ifndef ROCKPAINT_DOWN
188 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
189 #endif
190 #ifndef ROCKPAINT_LEFT
191 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
192 #endif
193 #ifndef ROCKPAINT_RIGHT
194 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
195 #endif
196 #endif
198 /***********************************************************************
199 * Palette Default Colors
200 ***********************************************************************/
201 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
202 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
203 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
204 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
205 #define COLOR_RED LCD_RGBPACK(128,0,0)
206 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
207 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
208 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
209 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
210 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
211 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
212 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
213 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
214 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
215 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
216 #define COLOR_PINK LCD_RGBPACK(255,0,255)
217 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
218 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
220 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
221 #define ROCKPAINT_TITLE_FONT 2
223 /***********************************************************************
224 * Program Colors
225 ***********************************************************************/
226 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
227 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
229 #define ROWS LCD_HEIGHT
230 #define COLS LCD_WIDTH
233 * Toolbar positioning stuff ... don't read this unless you really need to
235 * TB Toolbar
236 * SP Separator
237 * SC Selected Color
238 * PL Palette
239 * TL Tools
242 /* Separator sizes */
243 #define TB_SP_MARGIN 3
244 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
246 /* Selected color sizes */
247 #define TB_SC_SIZE 12
249 /* Palette sizes */
250 #define TB_PL_COLOR_SIZE 7
251 #define TB_PL_COLOR_SPACING 2
252 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
253 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
255 /* Tools sizes */
256 #define TB_TL_SIZE 8
257 #define TB_TL_SPACING 2
258 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
259 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
261 /* Menu button size ... gruik */
262 #define TB_MENU_MIN_WIDTH 30
264 /* Selected colors position */
265 #define TB_SC_FG_TOP 2
266 #define TB_SC_FG_LEFT 2
267 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
268 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
270 /* Palette position */
271 #define TB_PL_TOP TB_SC_FG_TOP
272 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
274 /* Tools position */
275 #define TB_TL_TOP TB_SC_FG_TOP
276 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
278 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
279 #undef TB_TL_TOP
280 #undef TB_TL_LEFT
281 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
282 #define TB_TL_LEFT TB_SC_FG_LEFT
283 #endif
285 /* Menu button position */
286 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
287 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
289 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
292 static void draw_pixel(int x,int y);
293 static void draw_line( int x1, int y1, int x2, int y2 );
294 static void draw_rect( int x1, int y1, int x2, int y2 );
295 static void draw_toolbars(bool update);
296 static void inv_cursor(bool update);
297 static void restore_screen(void);
298 static void clear_drawing(void);
299 static void goto_menu(void);
300 static int load_bitmap( const char *filename );
301 static int save_bitmap( char *filename );
302 static void draw_rect_full( int x1, int y1, int x2, int y2 );
303 extern int errno;
305 /***********************************************************************
306 * Global variables
307 ***********************************************************************/
309 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
310 int errno;
311 #endif
313 static int drawcolor=0; /* Current color (in palette) */
314 static int bgdrawcolor=9; /* Current background color (in palette) */
315 bool isbg = false; /* gruik ugly hack alert */
317 static int preview=false; /* Is preview mode on ? */
319 /* TODO: clean this up */
320 static int x=0, y=0; /* cursor position */
321 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
322 static int prev_x2=-1, prev_y2=-1;
323 static int prev_x3=-1, prev_y3=-1;
324 static int tool_mode=-1;
327 static int bsize=1; /* brush size */
328 static int bspeed=1; /* brush speed */
330 enum Tools { Brush = 0, /* Regular brush */
331 Fill = 1, /* Fill a shape with current color */
332 SelectRectangle = 2,
333 ColorPicker = 3, /* Pick a color */
334 Line = 4, /* Draw a line between two points */
335 Unused = 5, /* THIS IS UNUSED ... */
336 Curve = 6,
337 Text = 7,
338 Rectangle = 8, /* Draw a rectangle */
339 RectangleFull = 9,
340 Oval = 10, /* Draw an oval */
341 OvalFull = 11,
342 LinearGradient = 12,
343 RadialGradient = 13
346 enum Tools tool = Brush;
348 static bool quit=false;
349 static int gridsize=0;
351 static fb_data rp_colors[18] =
353 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
354 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
355 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
356 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
357 COLOR_LIGHTBROWN
360 static fb_data save_buffer[ ROWS*COLS ];
362 extern fb_data rockpaint[];
363 extern fb_data rockpaint_hsvrgb[];
365 /* Maximum string size allowed for the text tool */
366 #define MAX_TEXT 255
368 static union
370 /* Used by fill and gradient algorithms */
371 struct
373 short x;
374 short y;
375 } coord[ ROWS*COLS ];
377 /* Used by bezier curve algorithms */
378 struct
380 short x1, y1;
381 short x2, y2;
382 short x3, y3;
383 short x4, y4;
384 short depth;
385 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
386 * than coord ... so we divide to take
387 * less memory. */
389 /* Used to cut/copy/paste data */
390 fb_data clipboard[ ROWS*COLS ];
392 /* Used for text mode */
393 struct
395 char text[MAX_TEXT+1];
396 char font[MAX_PATH+1];
397 char old_font[MAX_PATH+1];
398 int fh_buf[30];
399 int fw_buf[30];
400 char fontname_buf[30][MAX_PATH];
401 } text;
402 } buffer;
404 /* Current filename */
405 static char filename[MAX_PATH+1];
407 /* Font preview buffer */
408 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
409 //#define FONT_PREVIEW_HEIGHT 1000
410 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
412 /***********************************************************************
413 * Offscreen buffer/Text/Fonts handling
415 * Parts of code taken from firmware/drivers/lcd-16bit.c
416 ***********************************************************************/
417 static void buffer_mono_bitmap_part(
418 fb_data *buf, int buf_width, int buf_height,
419 const unsigned char *src, int src_x, int src_y,
420 int stride, int x, int y, int width, int height )
421 /* this function only draws the foreground part of the bitmap */
423 const unsigned char *src_end;
424 fb_data *dst, *dst_end;
425 unsigned fgcolor = rb->lcd_get_foreground();
427 /* nothing to draw? */
428 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
429 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
430 return;
432 /* clipping */
433 if( x < 0 )
435 width += x;
436 src_x -= x;
437 x = 0;
439 if( y < 0 )
441 height += y;
442 src_y -= y;
443 y = 0;
445 if( x + width > buf_width )
446 width = buf_width - x;
447 if( y + height > buf_height )
448 height = buf_height - y;
450 src += stride * (src_y >> 3) + src_x; /* move starting point */
451 src_y &= 7;
452 src_end = src + width;
454 dst = buf + y*buf_width + x;
458 const unsigned char *src_col = src++;
459 unsigned data = *src_col >> src_y;
460 fb_data *dst_col = dst++;
461 int numbits = 8 - src_y;
463 dst_end = dst_col + height * buf_width;
466 if( data & 0x01 )
467 *dst_col = fgcolor; /* FIXME ? */
469 dst_col += buf_width;
471 data >>= 1;
472 if( --numbits == 0 )
474 src_col += stride;
475 data = *src_col;
476 numbits = 8;
478 } while( dst_col < dst_end );
479 } while( src < src_end );
482 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
483 int x, int y, int ofs, const unsigned char *str )
485 unsigned short ch;
486 unsigned short *ucs;
488 struct font *pf = rb->font_get( FONT_UI );
489 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
491 ucs = rb->bidi_l2v( str, 1 );
493 while( (ch = *ucs++) != 0 && x < buf_width )
495 int width;
496 const unsigned char *bits;
498 /* get proportional width and glyph bits */
499 width = rb->font_get_width( pf, ch );
501 if( ofs > width )
503 ofs -= width;
504 continue;
507 bits = rb->font_get_bits( pf, ch );
509 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0, width, x, y, width - ofs, pf->height);
511 x += width - ofs;
512 ofs = 0;
516 /***********************************************************************
517 * Menu handling
518 ***********************************************************************/
519 enum {
520 /* Main menu */
521 MAIN_MENU_RESUME,
522 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
523 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
524 MAIN_MENU_GRID_SIZE,
525 MAIN_MENU_EXIT,
527 enum {
528 /* Select action menu */
529 SELECT_MENU_CUT, SELECT_MENU_COPY, SELECT_MENU_INVERT,
530 SELECT_MENU_HFLIP, SELECT_MENU_VFLIP, SELECT_MENU_ROTATE90,
531 SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
532 SELECT_MENU_CANCEL,
534 enum {
535 /* Text menu */
536 TEXT_MENU_TEXT, TEXT_MENU_FONT,
537 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
540 MENUITEM_STRINGLIST(main_menu, "RockPaint", NULL,
541 "Resume", "New", "Load", "Save",
542 "Brush Size", "Brush Speed",
543 "Choose Color", "Grid Size", "Exit");
544 MENUITEM_STRINGLIST(size_menu, "Choose Size", NULL,
545 "1x", "2x","4x", "8x");
546 MENUITEM_STRINGLIST(speed_menu, "Choose Speed", NULL,
547 "1x", "2x","4x");
548 MENUITEM_STRINGLIST(gridsize_menu, "Grid Size", NULL,
549 "No grid", "5px", "10px", "20px");
550 MENUITEM_STRINGLIST(select_menu, "Select...", NULL,
551 "Cut", "Copy", "Invert", "Horizontal Flip" ,
552 "Vertical Flip", "Rotate 90°",
553 "Rotate 180°", "Rotate 270°", "Cancel");
554 MENUITEM_STRINGLIST(text_menu, "Text", NULL,
555 "Set Text", "Change Font",
556 "Preview", "Apply", "Cancel");
557 static const int times_list[] = { 1, 2, 4, 8 };
558 static const int gridsize_list[] = { 0, 5, 10, 20 };
560 static int draw_window( int height, int width,
561 int *top, int *left,
562 const char *title )
564 int fh;
565 rb->lcd_getstringsize( title, NULL, &fh );
566 fh++;
568 const int _top = ( LCD_HEIGHT - height ) / 2;
569 const int _left = ( LCD_WIDTH - width ) / 2;
570 if( top ) *top = _top;
571 if( left ) *left = _left;
572 rb->lcd_set_background(COLOR_BLUE);
573 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
574 rb->lcd_fillrect( _left, _top, width, height );
575 rb->lcd_set_foreground(COLOR_BLUE);
576 rb->lcd_fillrect( _left, _top, width, fh+4 );
577 rb->lcd_set_foreground(COLOR_WHITE);
578 rb->lcd_putsxy( _left+2, _top+2, title );
579 rb->lcd_set_foreground(COLOR_BLACK);
580 rb->lcd_drawrect( _left, _top, width, height );
581 return _top+fh+4;
584 /***********************************************************************
585 * File browser
586 ***********************************************************************/
588 char bbuf[MAX_PATH+1]; /* used by file and font browsers */
589 char bbuf_s[MAX_PATH+1]; /* used by file and font browsers */
590 struct tree_context *tree = NULL;
592 static char * browse_get_name_cb( int selected_item, void *data,
593 char *buffer, size_t buffer_len )
595 int *indexes = (int *) data;
596 struct entry* dc = tree->dircache;
597 struct entry* e = &dc[indexes[selected_item]];
598 (void) buffer;
599 (void) buffer_len;
601 return (e->name);
604 static bool browse( char *dst, int dst_size, const char *start )
606 struct gui_synclist browse_list;
607 int item_count = 0, selected, button;
608 struct tree_context backup;
609 struct entry *dc;
610 bool reload = true;
611 int dirfilter = SHOW_ALL;
612 int *indexes = (int *) buffer.clipboard;
614 char *a;
616 rb->strcpy( bbuf, start );
617 a = bbuf+rb->strlen(bbuf)-1;
618 if( *a != '/' )
620 a[1] = '/';
621 a[2] = '\0';
623 bbuf_s[0] = '\0';
625 rb->gui_synclist_init( &browse_list, browse_get_name_cb,
626 (void*) indexes, false, 1, NULL );
628 tree = rb->tree_get_context();
629 backup = *tree;
630 dc = tree->dircache;
631 a = backup.currdir+rb->strlen(backup.currdir)-1;
632 if( *a != '/' )
634 *++a = '/';
635 *++a = '\0';
637 rb->strcpy( a, dc[tree->selected_item].name );
638 tree->dirfilter = &dirfilter;
639 while( 1 )
641 if( reload )
643 int i;
644 rb->set_current_file(bbuf);
645 item_count = 0;
646 selected = 0;
647 for( i = 0; i < tree->filesindir ; i++)
649 /* only displayes directories and .bmp files */
650 if( ((dc[i].attr & ATTR_DIRECTORY ) &&
651 rb->strcmp( dc[i].name, "." ) &&
652 rb->strcmp( dc[i].name, ".." )) ||
653 ( !(dc[i].attr & ATTR_DIRECTORY) &&
654 (a = rb->strrchr( dc[i].name,'.' )) &&
655 !rb->strcmp( a, ".bmp" ) ))
657 if( !rb->strcmp( dc[i].name, bbuf_s ) )
658 selected = item_count;
659 indexes[item_count++] = i;
663 rb->gui_synclist_set_nb_items(&browse_list,item_count);
664 rb->gui_synclist_select_item(&browse_list, selected);
665 rb->gui_synclist_set_title(&browse_list, bbuf, NOICON);
666 rb->gui_synclist_draw(&browse_list);
667 reload = false;
669 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
670 if (rb->gui_synclist_do_button(&browse_list,&button,LIST_WRAP_UNLESS_HELD))
671 continue;
672 switch( button )
674 case ACTION_STD_CANCEL:
675 if( !rb->strcmp( bbuf, "/" ) )
677 *tree = backup;
678 rb->set_current_file( backup.currdir );
679 return false;
681 rb->strcpy( bbuf_s, ".." );
682 case ACTION_STD_OK:
683 if( button == ACTION_STD_OK )
685 selected = rb->gui_synclist_get_sel_pos( &browse_list );
686 if( selected < 0 || selected >= item_count )
687 break;
688 struct entry* e = &dc[indexes[selected]];
689 rb->strncpy( bbuf_s, e->name, sizeof( bbuf_s ) );
690 if( !( e->attr & ATTR_DIRECTORY ) )
692 *tree = backup;
693 rb->set_current_file( backup.currdir );
694 rb->snprintf( dst, dst_size, "%s%s", bbuf, bbuf_s );
695 return true;
698 if( !rb->strcmp( bbuf_s, "." ) ) break;
699 a = bbuf+rb->strlen(bbuf);
700 if( !rb->strcmp( bbuf_s, ".." ) )
702 a--;
703 if( a == bbuf ) break;
704 if( *a == '/' ) a--;
705 while( *a != '/' ) a--;
706 rb->strcpy( bbuf_s, ++a );
707 /* select parent directory */
708 bbuf_s[rb->strlen(bbuf_s)-1] = '\0';
709 *a = '\0';
710 reload = true;
711 break;
713 rb->snprintf( a, bbuf+sizeof(bbuf)-a, "%s/", bbuf_s );
714 reload = true;
715 break;
717 case ACTION_STD_MENU:
718 *tree = backup;
719 rb->set_current_file( backup.currdir );
720 return false;
725 /***********************************************************************
726 * Font browser
728 * FIXME: This still needs some work ... it currently only works fine
729 * on the simulators, disk spins too much on real targets -> rendered
730 * font buffer needed.
731 ***********************************************************************/
732 static bool browse_fonts( char *dst, int dst_size )
734 #define WIDTH ( LCD_WIDTH - 20 )
735 #define HEIGHT ( LCD_HEIGHT - 20 )
736 #define LINE_SPACE 2
737 int top, top_inside = 0, left;
739 DIR *d;
740 struct dirent *de;
741 int fvi = 0; /* first visible item */
742 int lvi = 0; /* last visible item */
743 int si = 0; /* selected item */
744 int osi = 0; /* old selected item */
745 int li = 0; /* last item */
746 int nvih = 0; /* next visible item height */
747 int i;
748 int b_need_redraw = 1; /* Do we need to redraw ? */
750 int cp = 0; /* current position */
751 int fh; /* font height */
753 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
754 #define fw_buf buffer.text.fw_buf
755 int fw;
756 #define fontname_buf buffer.text.fontname_buf
758 rb->snprintf( buffer.text.old_font, MAX_PATH,
759 FONT_DIR "/%s.fnt",
760 rb->global_settings->font_file );
762 while( 1 )
764 if( !b_need_redraw )
766 /* we don't need to redraw ... but we need to unselect
767 * the previously selected item */
768 cp = top_inside + LINE_SPACE;
769 for( i = 0; i+fvi < osi; i++ )
771 cp += fh_buf[i] + LINE_SPACE;
773 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
774 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
775 rb->lcd_set_drawmode(DRMODE_SOLID);
778 if( b_need_redraw )
780 b_need_redraw = 0;
782 d = rb->opendir( FONT_DIR "/" );
783 if( !d )
785 return false;
787 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
788 i = 0;
789 li = -1;
790 while( i < fvi )
792 rb->readdir( d );
793 i++;
795 cp = top_inside+LINE_SPACE;
797 rb->lcd_set_foreground(COLOR_BLACK);
798 rb->lcd_set_background(COLOR_LIGHTGRAY);
800 while( cp < top+HEIGHT )
802 de = rb->readdir( d );
803 if( !de )
805 li = i-1;
806 break;
808 if( rb->strlen( de->d_name ) < 4
809 || rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
810 ".fnt" ) )
811 continue;
812 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
813 de->d_name );
814 rb->font_load( bbuf );
815 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
816 if( nvih > 0 )
818 nvih -= fh;
819 fvi++;
820 if( nvih < 0 ) nvih = 0;
821 i++;
822 continue;
824 if( cp + fh >= top+HEIGHT )
826 nvih = fh;
827 break;
829 rb->lcd_putsxy( left+10, cp, de->d_name );
830 fh_buf[i-fvi] = fh;
831 fw_buf[i-fvi] = fw;
832 cp += fh + LINE_SPACE;
833 rb->strcpy( fontname_buf[i-fvi], bbuf );
834 i++;
836 lvi = i-1;
837 if( li == -1 )
839 if( !(de = rb->readdir( d ) ) )
841 li = lvi;
843 else if( !nvih && !rb->strlen( de->d_name ) < 4
844 && !rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
845 ".fnt" ) )
847 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
848 de->d_name );
849 rb->font_load( bbuf );
850 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
851 nvih = fh;
854 rb->font_load( buffer.text.old_font );
855 rb->closedir( d );
858 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
859 cp = top_inside + LINE_SPACE;
860 for( i = 0; i+fvi < si; i++ )
862 cp += fh_buf[i] + LINE_SPACE;
864 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
865 rb->lcd_set_drawmode(DRMODE_SOLID);
867 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
869 osi = si;
870 i = fvi;
871 switch( rb->button_get(true) )
873 case ROCKPAINT_UP:
874 case ROCKPAINT_UP|BUTTON_REPEAT:
875 if( si > 0 )
877 si--;
878 if( si<fvi )
880 fvi = si;
883 break;
885 case ROCKPAINT_DOWN:
886 case ROCKPAINT_DOWN|BUTTON_REPEAT:
887 if( li == -1 || si < li )
889 si++;
891 break;
893 case ROCKPAINT_LEFT:
894 return false;
896 case ROCKPAINT_RIGHT:
897 case ROCKPAINT_DRAW:
898 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
899 return true;
902 if( i != fvi || si > lvi )
904 b_need_redraw = 1;
907 if( si<=lvi )
909 nvih = 0;
912 #undef fh_buf
913 #undef fw_buf
914 #undef fontname_buf
915 #undef WIDTH
916 #undef HEIGHT
917 #undef LINE_SPACE
920 /***********************************************************************
921 * HSVRGB Color chooser
922 ***********************************************************************/
923 static unsigned int color_chooser( unsigned int color )
925 int red = RGB_UNPACK_RED( color );
926 int green = RGB_UNPACK_GREEN( color );
927 int blue = RGB_UNPACK_BLUE( color );
928 int hue, saturation, value;
929 int r, g, b; /* temp variables */
930 int i, top, left;
932 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
933 Red = 3, Green = 4, Blue = 5 };
934 enum BaseColor current = Red;
935 bool has_changed;
937 char str[6] = "";
939 restore_screen();
941 rgb2hsv( red, green, blue, &hue, &saturation, &value );
943 while( 1 )
945 has_changed = false;
946 color = LCD_RGBPACK( red, green, blue );
948 #define HEIGHT ( 100 )
949 #define WIDTH ( 150 )
951 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
952 top -= 15;
954 for( i=0; i<100; i++ )
956 hsv2rgb( i*36, saturation, value, &r, &g, &b );
957 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
958 rb->lcd_vline( left+15+i, top+20, top+27 );
959 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
960 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
961 rb->lcd_vline( left+15+i, top+30, top+37 );
962 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
963 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
964 rb->lcd_vline( left+15+i, top+40, top+47 );
965 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
966 rb->lcd_vline( left+15+i, top+50, top+57 );
967 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
968 rb->lcd_vline( left+15+i, top+60, top+67 );
969 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
970 rb->lcd_vline( left+15+i, top+70, top+77 );
973 rb->lcd_set_foreground(COLOR_BLACK);
974 #define POSITION( a, i ) \
975 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
976 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
977 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
978 rb->lcd_drawpixel( left+16+i, top + 28 + a );
979 POSITION( 0, hue/36 );
980 POSITION( 10, saturation*99/255 );
981 POSITION( 20, value*99/255 );
982 POSITION( 30, red*99/255 );
983 POSITION( 40, green*99/255 );
984 POSITION( 50, blue*99/255 );
985 #undef POSITION
986 rb->lcd_set_background(COLOR_LIGHTGRAY);
987 rb->lcd_setfont( FONT_SYSFIXED );
988 rb->snprintf( str, 6, "%d", hue/10 );
989 rb->lcd_putsxy( left + 117, top + 20, str );
990 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
991 rb->lcd_putsxy( left + 117, top + 30, str );
992 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
993 rb->lcd_putsxy( left + 117, top + 40, str );
994 rb->snprintf( str, 6, "%d", red );
995 rb->lcd_putsxy( left + 117, top + 50, str );
996 rb->snprintf( str, 6, "%d", green );
997 rb->lcd_putsxy( left + 117, top + 60, str );
998 rb->snprintf( str, 6, "%d", blue );
999 rb->lcd_putsxy( left + 117, top + 70, str );
1000 rb->lcd_setfont( FONT_UI );
1002 #define CURSOR( l ) \
1003 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1004 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1005 CURSOR( 5 );
1006 #undef CURSOR
1008 rb->lcd_set_foreground( color );
1009 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1011 rb->lcd_update();
1013 switch( rb->button_get(true) )
1015 case ROCKPAINT_UP:
1016 current = ( current + 5 )%6;
1017 break;
1019 case ROCKPAINT_DOWN:
1020 current = (current + 1 )%6;
1021 break;
1023 case ROCKPAINT_LEFT:
1024 has_changed = true;
1025 switch( current )
1027 case Hue:
1028 hue = ( hue + 3600 - 10 )%3600;
1029 break;
1030 case Saturation:
1031 if( saturation ) saturation--;
1032 break;
1033 case Value:
1034 if( value ) value--;
1035 break;
1036 case Red:
1037 if( red ) red--;
1038 break;
1039 case Green:
1040 if( green ) green--;
1041 break;
1042 case Blue:
1043 if( blue ) blue--;
1044 break;
1046 break;
1048 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1049 has_changed = true;
1050 switch( current )
1052 case Hue:
1053 hue = ( hue + 3600 - 100 )%3600;
1054 break;
1055 case Saturation:
1056 if( saturation >= 8 ) saturation-=8;
1057 else saturation = 0;
1058 break;
1059 case Value:
1060 if( value >= 8 ) value-=8;
1061 else value = 0;
1062 break;
1063 case Red:
1064 if( red >= 8 ) red-=8;
1065 else red = 0;
1066 break;
1067 case Green:
1068 if( green >= 8 ) green-=8;
1069 else green = 0;
1070 break;
1071 case Blue:
1072 if( blue >= 8 ) blue-=8;
1073 else blue = 0;
1074 break;
1076 break;
1078 case ROCKPAINT_RIGHT:
1079 has_changed = true;
1080 switch( current )
1082 case Hue:
1083 hue = ( hue + 10 )%3600;
1084 break;
1085 case Saturation:
1086 if( saturation < 0xff ) saturation++;
1087 break;
1088 case Value:
1089 if( value < 0xff ) value++;
1090 break;
1091 case Red:
1092 if( red < 0xff ) red++;
1093 break;
1094 case Green:
1095 if( green < 0xff ) green++;
1096 break;
1097 case Blue:
1098 if( blue < 0xff ) blue++;
1099 break;
1101 break;
1103 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1104 has_changed = true;
1105 switch( current )
1107 case Hue:
1108 hue = ( hue + 100 )%3600;
1109 break;
1110 case Saturation:
1111 if( saturation < 0xff - 8 ) saturation+=8;
1112 else saturation = 0xff;
1113 break;
1114 case Value:
1115 if( value < 0xff - 8 ) value+=8;
1116 else value = 0xff;
1117 break;
1118 case Red:
1119 if( red < 0xff - 8 ) red+=8;
1120 else red = 0xff;
1121 break;
1122 case Green:
1123 if( green < 0xff - 8 ) green+=8;
1124 else green = 0xff;
1125 break;
1126 case Blue:
1127 if( blue < 0xff - 8 ) blue+=8;
1128 else blue = 0xff;
1129 break;
1131 break;
1133 case ROCKPAINT_DRAW:
1134 return color;
1136 if( has_changed )
1138 switch( current )
1140 case Hue:
1141 case Saturation:
1142 case Value:
1143 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1144 break;
1146 case Red:
1147 case Green:
1148 case Blue:
1149 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1150 break;
1153 #undef HEIGHT
1154 #undef WIDTH
1158 /***********************************************************************
1159 * Misc routines
1160 ***********************************************************************/
1161 static void init_buffer(void)
1163 int i;
1164 fb_data color = rp_colors[ bgdrawcolor ];
1165 for( i = 0; i < ROWS*COLS; i++ )
1167 save_buffer[i] = color;
1171 static void draw_pixel(int x,int y)
1173 if( !preview )
1175 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1176 if( isbg )
1178 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1180 else
1182 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1185 rb->lcd_drawpixel(x,y);
1188 static void color_picker( int x, int y )
1190 if( preview )
1192 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1193 #define PSIZE 12
1194 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1195 rb->lcd_drawrect( x<COLS-PSIZE ? x + 2 : x - PSIZE, y<ROWS-PSIZE ? y + 2: y - PSIZE, PSIZE - 2, PSIZE - 2 );
1196 rb->lcd_set_drawmode(DRMODE_SOLID);
1197 rb->lcd_fillrect( x<COLS-PSIZE ? x+3 : x - PSIZE+1, y<ROWS-PSIZE ? y +3: y - PSIZE+1, PSIZE-4, PSIZE-4 );
1198 #undef PSIZE
1199 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1201 else
1203 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1207 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1208 /* This is a preview mode only function */
1210 int i,a;
1211 if( x1 > x2 )
1213 i = x1;
1214 x1 = x2;
1215 x2 = i;
1217 if( y1 > y2 )
1219 i = y1;
1220 y1 = y2;
1221 y2 = i;
1223 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1224 i = 0;
1225 for( a = x1; a < x2; a++, i++ )
1226 if( i%2 )
1227 rb->lcd_drawpixel( a, y1 );
1228 for( a = y1; a < y2; a++, i++ )
1229 if( i%2 )
1230 rb->lcd_drawpixel( x2, a );
1231 if( y2 != y1 )
1232 for( a = x2; a > x1; a--, i++ )
1233 if( i%2 )
1234 rb->lcd_drawpixel( a, y2 );
1235 if( x2 != x1 )
1236 for( a = y2; a > y1; a--, i++ )
1237 if( i%2 )
1238 rb->lcd_drawpixel( x1, a );
1239 rb->lcd_set_drawmode(DRMODE_SOLID);
1242 static void copy_to_clipboard( void )
1244 /* This needs to be optimised ... but i'm lazy ATM */
1245 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1248 /* no preview mode handling atm ... do we need it ? (one if) */
1249 static void draw_invert( int x1, int y1, int x2, int y2 )
1251 int i;
1252 if( x1 > x2 )
1254 i = x1;
1255 x1 = x2;
1256 x2 = i;
1258 if( y1 > y2 )
1260 i = y1;
1261 y1 = y2;
1262 y2 = i;
1265 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1266 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1267 rb->lcd_set_drawmode(DRMODE_SOLID);
1269 for( ; y1<=y2; y1++ )
1271 for( i = x1; i<=x2; i++ )
1273 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1276 /*if( update )*/ rb->lcd_update();
1279 static void draw_hflip( int x1, int y1, int x2, int y2 )
1281 int i;
1282 if( x1 > x2 )
1284 i = x1;
1285 x1 = x2;
1286 x2 = i;
1288 if( y1 > y2 )
1290 i = y1;
1291 y1 = y2;
1292 y2 = i;
1295 copy_to_clipboard();
1297 for( i = 0; i <= y2 - y1; i++ )
1299 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1300 buffer.clipboard+(y2-i)*COLS+x1,
1301 (x2-x1+1)*sizeof( fb_data ) );
1303 restore_screen();
1304 rb->lcd_update();
1307 static void draw_vflip( int x1, int y1, int x2, int y2 )
1309 int i;
1310 if( x1 > x2 )
1312 i = x1;
1313 x1 = x2;
1314 x2 = i;
1316 if( y1 > y2 )
1318 i = y1;
1319 y1 = y2;
1320 y2 = i;
1323 copy_to_clipboard();
1325 for( ; y1 <= y2; y1++ )
1327 for( i = 0; i <= x2 - x1; i++ )
1329 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1332 restore_screen();
1333 rb->lcd_update();
1336 /* direction: -1 = left, 1 = right */
1337 static void draw_rot_90_deg( int x1, int y1, int x2, int y2, int direction )
1339 int i, j;
1340 if( x1 > x2 )
1342 i = x1;
1343 x1 = x2;
1344 x2 = i;
1346 if( y1 > y2 )
1348 i = y1;
1349 y1 = y2;
1350 y2 = i;
1353 copy_to_clipboard();
1355 fb_data color = rp_colors[ bgdrawcolor ];
1356 const int width = x2 - x1, height = y2 - y1;
1357 const int sub_half = width/2-height/2, add_half = (width+height)/2;
1358 if( width > height )
1360 for( i = 0; i <= height; i++ )
1362 for( j = 0; j < sub_half; j++ )
1363 save_buffer[(y1+i)*COLS+x1+j] = color;
1364 for( j = add_half+1; j <= width; j++ )
1365 save_buffer[(y1+i)*COLS+x1+j] = color;
1368 else if( width < height )
1370 for( j = 0; j <= width; j++ )
1372 for( i = 0; i < -sub_half; i++ )
1373 save_buffer[(y1+i)*COLS+x1+j] = color;
1374 for( i = add_half+1; i <= height; i++ )
1375 save_buffer[(y1+i)*COLS+x1+j] = color;
1378 int x3 = x1 + sub_half, y3 = y1 - sub_half;
1379 int is = x3<0?-x3:0, ie = COLS-x3-1, js = y3<0?-y3:0, je = ROWS-y3-1;
1380 if( ie > height ) ie = height;
1381 if( je > width ) je = width;
1382 for( i = is; i <= ie; i++ )
1384 for( j = js; j <= je; j++ )
1386 int x, y;
1387 if(direction > 0)
1389 x = x1+j;
1390 y = y1+height-i;
1392 else
1394 x = x1+width-j;
1395 y = y1+i;
1397 save_buffer[(y3+j)*COLS+x3+i] = buffer.clipboard[y*COLS+x];
1400 restore_screen();
1401 rb->lcd_update();
1404 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1405 int src_y2, int x1, int y1, int mode )
1407 int i;
1408 if( mode == SELECT_MENU_CUT )
1410 i = drawcolor;
1411 drawcolor = bgdrawcolor;
1412 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1413 drawcolor = i;
1415 if( src_x1 > src_x2 )
1417 i = src_x1;
1418 src_x1 = src_x2;
1419 src_x2 = i;
1421 if( src_y1 > src_y2 )
1423 i = src_y1;
1424 src_y1 = src_y2;
1425 src_y2 = i;
1427 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1428 x1, y1, src_x2-src_x1+1, src_y2-src_y1+1 );
1429 if( !preview )
1431 for( i = 0; i <= src_y2 - src_y1; i++ )
1433 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1434 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1435 (src_x2 - src_x1 + 1)*sizeof( fb_data ) );
1440 static void show_grid( bool update )
1442 int i;
1443 if( gridsize > 0 )
1445 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1446 for( i = gridsize; i < COLS; i+= gridsize )
1448 rb->lcd_vline( i, 0, ROWS-1 );
1450 for( i = gridsize; i < ROWS; i+= gridsize )
1452 rb->lcd_hline( 0, COLS-1, i );
1454 rb->lcd_set_drawmode(DRMODE_SOLID);
1455 if( update ) rb->lcd_update();
1459 static void draw_text( int x, int y )
1461 int selected = 0;
1462 buffer.text.text[0] = '\0';
1463 rb->snprintf( buffer.text.old_font, MAX_PATH,
1464 FONT_DIR "/%s.fnt",
1465 rb->global_settings->font_file );
1466 while( 1 )
1468 switch( rb->do_menu( &text_menu, &selected, NULL, NULL ) )
1470 case TEXT_MENU_TEXT:
1471 rb->lcd_set_foreground(COLOR_BLACK);
1472 rb->kbd_input( buffer.text.text, MAX_TEXT );
1473 break;
1475 case TEXT_MENU_FONT:
1476 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1478 rb->font_load( buffer.text.font );
1480 break;
1482 case TEXT_MENU_PREVIEW:
1483 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1484 while( 1 )
1486 int button;
1487 restore_screen();
1488 rb->lcd_putsxy( x, y, buffer.text.text );
1489 rb->lcd_update();
1490 switch( button = rb->button_get( true ) )
1492 case ROCKPAINT_LEFT:
1493 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1494 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1495 if (x<0) x=COLS-1;
1496 break;
1498 case ROCKPAINT_RIGHT:
1499 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1500 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1501 if (x>=COLS) x=0;
1502 break;
1504 case ROCKPAINT_UP:
1505 case ROCKPAINT_UP | BUTTON_REPEAT:
1506 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1507 if (y<0) y=ROWS-1;
1508 break;
1510 case ROCKPAINT_DOWN:
1511 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1512 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1513 if (y>=ROWS-1) y=0;
1514 break;
1516 case ROCKPAINT_DRAW:
1517 break;
1518 default:
1519 if(rb->default_event_handler(button)
1520 == SYS_USB_CONNECTED)
1521 button = ROCKPAINT_DRAW;
1522 break;
1524 if( button == ROCKPAINT_DRAW ) break;
1526 break;
1528 case TEXT_MENU_APPLY:
1529 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1530 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1531 buffer.text.text );
1532 case TEXT_MENU_CANCEL:
1533 default:
1534 restore_screen();
1535 rb->font_load( buffer.text.old_font );
1536 return;
1541 static void draw_brush( int x, int y )
1543 int i,j;
1544 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1546 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1548 draw_pixel( x+i, y+j );
1553 /* This is an implementation of Bresenham's line algorithm.
1554 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1556 static void draw_line( int x1, int y1, int x2, int y2 )
1558 int x = x1;
1559 int y = y1;
1560 int deltax = x2 - x1;
1561 int deltay = y2 - y1;
1562 int i;
1564 int xerr = abs(deltax);
1565 int yerr = abs(deltay);
1566 int xstep = deltax > 0 ? 1 : -1;
1567 int ystep = deltay > 0 ? 1 : -1;
1568 int err;
1570 if (yerr > xerr)
1572 /* more vertical */
1573 err = yerr;
1574 xerr <<= 1;
1575 yerr <<= 1;
1577 /* to leave off the last pixel of the line, leave off the "+ 1" */
1578 for (i = abs(deltay) + 1; i; --i)
1580 draw_pixel(x, y);
1581 y += ystep;
1582 err -= xerr;
1583 if (err < 0) {
1584 x += xstep;
1585 err += yerr;
1589 else
1591 /* more horizontal */
1592 err = xerr;
1593 xerr <<= 1;
1594 yerr <<= 1;
1596 for (i = abs(deltax) + 1; i; --i)
1598 draw_pixel(x, y);
1599 x += xstep;
1600 err -= yerr;
1601 if (err < 0) {
1602 y += ystep;
1603 err += xerr;
1609 static void draw_curve( int x1, int y1, int x2, int y2,
1610 int xa, int ya, int xb, int yb )
1612 int i = 0;
1613 short xl1, yl1;
1614 short xl2, yl2;
1615 short xl3, yl3;
1616 short xl4, yl4;
1617 short xr1, yr1;
1618 short xr2, yr2;
1619 short xr3, yr3;
1620 short xr4, yr4;
1621 short depth;
1622 short xh, yh;
1624 if( x1 == x2 && y1 == y2 )
1626 draw_pixel( x1, y1 );
1627 return;
1630 // if( preview )
1632 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1633 if( xa == -1 || ya == -1 )
1635 rb->lcd_drawline( x1, y1, xb, yb );
1636 rb->lcd_drawline( x2, y2, xb, yb );
1638 else
1640 rb->lcd_drawline( x1, y1, xa, ya );
1641 rb->lcd_drawline( x2, y2, xb, yb );
1643 rb->lcd_set_drawmode(DRMODE_SOLID);
1646 if( xa == -1 || ya == -1 )
1647 /* We only have 3 of the points
1648 * This will currently only be used in preview mode */
1650 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1651 buffer.bezier[i].x1 = a1; \
1652 buffer.bezier[i].y1 = b1; \
1653 buffer.bezier[i].x2 = a2; \
1654 buffer.bezier[i].y2 = b2; \
1655 buffer.bezier[i].x3 = a3; \
1656 buffer.bezier[i].y3 = b3; \
1657 buffer.bezier[i].depth = d; \
1658 i++;
1659 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1660 i--; \
1661 a1 = buffer.bezier[i].x1; \
1662 b1 = buffer.bezier[i].y1; \
1663 a2 = buffer.bezier[i].x2; \
1664 b2 = buffer.bezier[i].y2; \
1665 a3 = buffer.bezier[i].x3; \
1666 b3 = buffer.bezier[i].y3; \
1667 d = buffer.bezier[i].depth;
1668 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1669 while( i )
1671 /* de Casteljau's algorithm (see wikipedia) */
1672 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1673 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1675 xl2 = ( xl1 + xb )>>1;
1676 yl2 = ( yl1 + yb )>>1;
1677 xr2 = ( xb + xr3 )>>1;
1678 yr2 = ( yb + yr3 )>>1;
1679 xr1 = ( xl2 + xr2 )>>1;
1680 yr1 = ( yl2 + yr2 )>>1;
1681 xl3 = xr1;
1682 yl3 = yr1;
1683 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1684 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1686 else
1688 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1689 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1692 #undef PUSH
1693 #undef POP
1695 else /* We have the 4 points */
1697 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1698 buffer.bezier[i].x1 = a1; \
1699 buffer.bezier[i].y1 = b1; \
1700 buffer.bezier[i].x2 = a2; \
1701 buffer.bezier[i].y2 = b2; \
1702 buffer.bezier[i].x3 = a3; \
1703 buffer.bezier[i].y3 = b3; \
1704 buffer.bezier[i].x4 = a4; \
1705 buffer.bezier[i].y4 = b4; \
1706 buffer.bezier[i].depth = d; \
1707 i++;
1708 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1709 i--; \
1710 a1 = buffer.bezier[i].x1; \
1711 b1 = buffer.bezier[i].y1; \
1712 a2 = buffer.bezier[i].x2; \
1713 b2 = buffer.bezier[i].y2; \
1714 a3 = buffer.bezier[i].x3; \
1715 b3 = buffer.bezier[i].y3; \
1716 a4 = buffer.bezier[i].x4; \
1717 b4 = buffer.bezier[i].y4; \
1718 d = buffer.bezier[i].depth;
1720 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1721 while( i )
1723 /* de Casteljau's algorithm (see wikipedia) */
1724 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1725 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1727 xl2 = ( xl1 + xa )>>1;
1728 yl2 = ( yl1 + ya )>>1;
1729 xh = ( xa + xb )>>1;
1730 yh = ( ya + yb )>>1;
1731 xr3 = ( xb + xr4 )>>1;
1732 yr3 = ( yb + yr4 )>>1;
1733 xl3 = ( xl2 + xh )>>1;
1734 yl3 = ( yl2 + yh )>>1;
1735 xr2 = ( xr3 + xh )>>1;
1736 yr2 = ( yr3 + yh )>>1;
1737 xl4 = ( xl3 + xr2 )>>1;
1738 yl4 = ( yl3 + yr2 )>>1;
1739 xr1 = xl4;
1740 yr1 = yl4;
1741 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1742 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1744 else
1746 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1747 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1750 #undef PUSH
1751 #undef POP
1755 static void draw_rect( int x1, int y1, int x2, int y2 )
1757 draw_line( x1, y1, x1, y2 );
1758 draw_line( x1, y1, x2, y1 );
1759 draw_line( x1, y2, x2, y2 );
1760 draw_line( x2, y1, x2, y2 );
1763 static void togglebg( void )
1765 if( isbg )
1767 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1769 else
1771 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1773 isbg = !isbg;
1776 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1778 /* GRUIK */
1779 int x = x1;
1780 togglebg();
1781 if( x < x2 )
1783 do {
1784 draw_line( x, y1, x, y2 );
1785 } while( ++x <= x2 );
1787 else
1789 do {
1790 draw_line( x, y1, x, y2 );
1791 } while( --x >= x2 );
1793 togglebg();
1794 draw_rect( x1, y1, x2, y2 );
1797 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1799 /* TODO: simplify :) */
1800 int cx = (x1+x2)>>1;
1801 int cy = (y1+y2)>>1;
1803 int rx = (x1-x2)>>1;
1804 int ry = (y1-y2)>>1;
1805 if( rx < 0 ) rx *= -1;
1806 if( ry < 0 ) ry *= -1;
1808 if( rx == 0 || ry == 0 )
1810 draw_line( x1, y1, x2, y2 );
1811 return;
1814 int x,y;
1815 int dst, old_dst;
1817 for( x = 0; x < rx; x++ )
1819 y = 0;
1820 dst = -0xfff;
1821 do {
1822 old_dst = dst;
1823 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1824 y++;
1825 } while( dst < 0 );
1826 if( -old_dst < dst ) y--;
1827 if( full )
1829 draw_line( cx+x, cy, cx+x, cy+y );
1830 draw_line( cx+x, cy, cx+x, cy-y );
1831 draw_line( cx-x, cy, cx-x, cy+y );
1832 draw_line( cx-x, cy, cx-x, cy-y );
1834 else
1836 draw_pixel( cx+x, cy+y );
1837 draw_pixel( cx+x, cy-y );
1838 draw_pixel( cx-x, cy+y );
1839 draw_pixel( cx-x, cy-y );
1842 for( y = 0; y < ry; y++ )
1844 x = 0;
1845 dst = -0xfff;
1846 do {
1847 old_dst = dst;
1848 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1849 x++;
1850 } while( dst < 0 );
1851 if( -old_dst < dst ) x--;
1852 if( full )
1854 draw_line( cx+x, cy, cx+x, cy+y );
1855 draw_line( cx+x, cy, cx+x, cy-y );
1856 draw_line( cx-x, cy, cx-x, cy+y );
1857 draw_line( cx-x, cy, cx-x, cy-y );
1859 else
1861 draw_pixel( cx+x, cy+y );
1862 draw_pixel( cx+x, cy-y );
1863 draw_pixel( cx-x, cy+y );
1864 draw_pixel( cx-x, cy-y );
1869 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1871 draw_oval( x1, y1, x2, y2, false );
1874 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1876 togglebg();
1877 draw_oval( x1, y1, x2, y2, true );
1878 togglebg();
1879 draw_oval( x1, y1, x2, y2, false );
1882 static void draw_fill( int x0, int y0 )
1884 #define PUSH( a, b ) \
1885 draw_pixel( (int)a, (int)b ); \
1886 buffer.coord[i].x = a; \
1887 buffer.coord[i].y = b; \
1888 i++;
1889 #define POP( a, b ) \
1890 i--; \
1891 a = buffer.coord[i].x; \
1892 b = buffer.coord[i].y;
1894 unsigned int i=0;
1895 short x = x0;
1896 short y = y0;
1897 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1899 if( prev_color == rp_colors[ drawcolor ] ) return;
1901 PUSH( x, y );
1903 while( i != 0 )
1905 POP( x, y );
1906 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1908 PUSH( x-1, y );
1910 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1912 PUSH( x+1, y );
1914 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1916 PUSH( x, y-1 );
1918 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1920 PUSH( x, y+1 );
1923 #undef PUSH
1924 #undef POP
1928 /* For preview purposes only */
1929 static void line_gradient( int x1, int y1, int x2, int y2 )
1931 int r1, g1, b1;
1932 int r2, g2, b2;
1933 int h1, s1, v1, h2, s2, v2, r, g, b;
1934 int w, h, x, y;
1936 bool a = false;
1938 x1 <<= 1;
1939 y1 <<= 1;
1940 x2 <<= 1;
1941 y2 <<= 1;
1943 w = x1 - x2;
1944 h = y1 - y2;
1946 if( w == 0 && h == 0 )
1948 draw_pixel( x1>>1, y1>>1 );
1949 return;
1952 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
1953 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
1954 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
1955 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
1956 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
1957 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
1959 if( w < 0 )
1961 w *= -1;
1962 a = true;
1964 if( h < 0 )
1966 h *= -1;
1967 a = !a;
1969 if( a )
1971 r = r1;
1972 r1 = r2;
1973 r2 = r;
1974 g = g1;
1975 g1 = g2;
1976 g2 = g;
1977 b = b1;
1978 b1 = b2;
1979 b2 = b;
1982 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
1983 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
1985 if( w > h )
1987 if( x1 > x2 )
1989 x = x2;
1990 y = y2;
1991 x2 = x1;
1992 y2 = y1;
1993 x1 = x;
1994 y1 = y;
1996 w = x1 - x2;
1997 h = y1 - y2;
1998 while( x1 <= x2 )
2000 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2001 s1+((s2-s1)*(x1-x2))/w,
2002 v1+((v2-v1)*(x1-x2))/w,
2003 &r, &g, &b );
2004 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2005 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2006 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2007 x1+=2;
2008 y1 = y2 - ( x2 - x1 ) * h / w;
2011 else /* h > w */
2013 if( y1 > y2 )
2015 x = x2;
2016 y = y2;
2017 x2 = x1;
2018 y2 = y1;
2019 x1 = x;
2020 y1 = y;
2022 w = x1 - x2;
2023 h = y1 - y2;
2024 while( y1 <= y2 )
2026 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2027 s1+((s2-s1)*(y1-y2))/h,
2028 v1+((v2-v1)*(y1-y2))/h,
2029 &r, &g, &b );
2030 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2031 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2032 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2033 y1+=2;
2034 x1 = x2 - ( y2 - y1 ) * w / h;
2037 if( a )
2039 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2041 else
2043 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2047 static void linear_gradient( int x1, int y1, int x2, int y2 )
2049 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2050 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2051 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2052 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2053 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2054 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2056 int h1, s1, v1, h2, s2, v2, r, g, b;
2058 /* radius^2 */
2059 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2060 int dist2, i=0;
2062 /* We only propagate the gradient to neighboring pixels with the same
2063 * color as ( x1, y1 ) */
2064 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2066 int x = x1;
2067 int y = y1;
2069 if( radius2 == 0 ) return;
2070 if( preview )
2072 line_gradient( x1, y1, x2, y2 );
2075 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2076 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2078 #define PUSH( x0, y0 ) \
2079 buffer.coord[i].x = (short)(x0); \
2080 buffer.coord[i].y = (short)(y0); \
2081 i++;
2082 #define POP( a, b ) \
2083 i--; \
2084 a = (int)buffer.coord[i].x; \
2085 b = (int)buffer.coord[i].y;
2087 PUSH( x, y );
2089 while( i != 0 )
2091 POP( x, y );
2093 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2094 if( dist2 <= 0 )
2096 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2098 else if( dist2 < radius2 )
2100 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2101 s1+((s2-s1)*dist2)/radius2,
2102 v1+((v2-v1)*dist2)/radius2,
2103 &r, &g, &b );
2104 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2106 else
2108 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2110 if( rp_colors[ drawcolor ] == prev_color )
2112 if( rp_colors[ drawcolor ])
2113 rp_colors[ drawcolor ]--; /* GRUIK */
2114 else
2115 rp_colors[ drawcolor ]++; /* GRUIK */
2117 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2118 draw_pixel( x, y );
2120 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2122 PUSH( x-1, y );
2124 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2126 PUSH( x+1, y );
2128 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2130 PUSH( x, y-1 );
2132 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2134 PUSH( x, y+1 );
2137 #undef PUSH
2138 #undef POP
2140 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2143 static void radial_gradient( int x1, int y1, int x2, int y2 )
2145 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2146 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2147 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2148 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2149 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2150 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2152 int h1, s1, v1, h2, s2, v2, r, g, b;
2154 /* radius^2 */
2155 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2156 int dist2, i=0;
2158 /* We only propagate the gradient to neighboring pixels with the same
2159 * color as ( x1, y1 ) */
2160 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2162 int x = x1;
2163 int y = y1;
2165 if( radius2 == 0 ) return;
2166 if( preview )
2168 line_gradient( x1, y1, x2, y2 );
2171 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2172 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2174 #define PUSH( x0, y0 ) \
2175 buffer.coord[i].x = (short)(x0); \
2176 buffer.coord[i].y = (short)(y0); \
2177 i++;
2178 #define POP( a, b ) \
2179 i--; \
2180 a = (int)buffer.coord[i].x; \
2181 b = (int)buffer.coord[i].y;
2183 PUSH( x, y );
2185 while( i != 0 )
2187 POP( x, y );
2189 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2191 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2192 s1+((s2-s1)*dist2)/radius2,
2193 v1+((v2-v1)*dist2)/radius2,
2194 &r, &g, &b );
2195 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2197 else
2199 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2201 if( rp_colors[ drawcolor ] == prev_color )
2203 if( rp_colors[ drawcolor ])
2204 rp_colors[ drawcolor ]--; /* GRUIK */
2205 else
2206 rp_colors[ drawcolor ]++; /* GRUIK */
2208 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2209 draw_pixel( x, y );
2211 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2213 PUSH( x-1, y );
2215 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2217 PUSH( x+1, y );
2219 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2221 PUSH( x, y-1 );
2223 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2225 PUSH( x, y+1 );
2228 #undef PUSH
2229 #undef POP
2231 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2234 static void draw_toolbars(bool update)
2236 int i;
2237 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2238 rb->lcd_set_background( COLOR_LIGHTGRAY );
2239 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2240 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2241 rb->lcd_set_foreground( COLOR_BLACK );
2242 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2244 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2245 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2246 TB_SC_SIZE, TB_SC_SIZE );
2247 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2248 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2249 TB_SC_SIZE, TB_SC_SIZE );
2250 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2251 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2252 TB_SC_SIZE, TB_SC_SIZE );
2253 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2254 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2255 TB_SC_SIZE, TB_SC_SIZE );
2257 for( i=0; i<18; i++ )
2259 rb->lcd_set_foreground( rp_colors[i] );
2260 rb->lcd_fillrect(
2261 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2262 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2263 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2264 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2265 rb->lcd_drawrect(
2266 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2267 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2268 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2271 #define SEPARATOR( x, y ) \
2272 rb->lcd_set_foreground( COLOR_WHITE ); \
2273 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2274 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2275 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2276 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2278 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2279 TB_TL_WIDTH, TB_TL_HEIGHT );
2280 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2281 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2282 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2283 TB_TL_SIZE, TB_TL_SIZE );
2285 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2287 rb->lcd_setfont( FONT_SYSFIXED );
2288 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2289 rb->lcd_setfont( FONT_UI );
2290 #undef TOP
2292 if( update ) rb->lcd_update();
2295 static void toolbar( void )
2297 int button, i, j;
2298 restore_screen();
2299 draw_toolbars( false );
2300 y = LCD_HEIGHT-TB_HEIGHT/2;
2301 inv_cursor( true );
2302 while( 1 )
2304 switch( button = rb->button_get( true ) )
2306 case ROCKPAINT_DRAW:
2307 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2308 if( y >= TOP + TB_SC_FG_TOP
2309 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2310 && x >= TB_SC_FG_LEFT
2311 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2313 /* click on the foreground color */
2314 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2316 else if( y >= TOP + TB_SC_BG_TOP
2317 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2318 && x >= TB_SC_BG_LEFT
2319 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2321 /* click on the background color */
2322 i = drawcolor;
2323 drawcolor = bgdrawcolor;
2324 bgdrawcolor = i;
2326 else if( y >= TOP + TB_PL_TOP
2327 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2328 && x >= TB_PL_LEFT
2329 && x < TB_PL_LEFT + TB_PL_WIDTH )
2331 /* click on the palette */
2332 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2333 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2334 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2335 break;
2336 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2337 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2338 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2340 else if( y >= TOP+TB_TL_TOP
2341 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2342 && x >= TB_TL_LEFT
2343 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2345 /* click on the tools */
2346 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2347 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2348 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2349 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2350 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2351 tool = i*2+j;
2352 prev_x = -1;
2353 prev_y = -1;
2354 prev_x2 = -1;
2355 prev_y2 = -1;
2356 prev_x3 = -1;
2357 prev_y3 = -1;
2358 preview = false;
2360 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2362 /* menu button */
2363 goto_menu();
2365 #undef TOP
2366 restore_screen();
2367 draw_toolbars( false );
2368 inv_cursor( true );
2369 break;
2371 case ROCKPAINT_LEFT:
2372 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2373 inv_cursor(false);
2374 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2375 if (x<0) x=COLS-1;
2376 inv_cursor(true);
2377 break;
2379 case ROCKPAINT_RIGHT:
2380 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2381 inv_cursor(false);
2382 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2383 if (x>=COLS) x=0;
2384 inv_cursor(true);
2385 break;
2387 case ROCKPAINT_UP:
2388 case ROCKPAINT_UP | BUTTON_REPEAT:
2389 inv_cursor(false);
2390 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2391 if (y<LCD_HEIGHT-TB_HEIGHT)
2393 return;
2395 inv_cursor(true);
2396 break;
2398 case ROCKPAINT_DOWN:
2399 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2400 inv_cursor(false);
2401 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2402 if (y>=LCD_HEIGHT)
2404 y = 0;
2405 return;
2407 inv_cursor(true);
2408 break;
2410 case ROCKPAINT_TOOLBAR:
2411 case ROCKPAINT_TOOLBAR2:
2412 return;
2414 if( quit ) return;
2418 static void inv_cursor(bool update)
2420 rb->lcd_set_foreground(COLOR_BLACK);
2421 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2422 /* cross painting */
2423 rb->lcd_hline(x-4,x-1,y);
2424 rb->lcd_hline(x+1,x+4,y);
2425 rb->lcd_vline(x,y-4,y-1);
2426 rb->lcd_vline(x,y+1,y+4);
2427 rb->lcd_set_foreground(rp_colors[drawcolor]);
2428 rb->lcd_set_drawmode(DRMODE_SOLID);
2430 if( update ) rb->lcd_update();
2433 static void restore_screen(void)
2435 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2438 static void clear_drawing(void)
2440 init_buffer();
2441 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2442 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2443 rb->lcd_update();
2446 static void goto_menu(void)
2448 int multi;
2449 int selected = 0;
2451 while( 1 )
2453 switch( rb->do_menu( &main_menu, &selected, NULL, false ) )
2455 case MAIN_MENU_NEW:
2456 clear_drawing();
2457 return;
2459 case MAIN_MENU_LOAD:
2460 if( browse( filename, MAX_PATH, "/" ) )
2462 if( load_bitmap( filename ) <= 0 )
2464 rb->splashf( 1*HZ, "Error while loading %s",
2465 filename );
2467 else
2469 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2470 restore_screen();
2471 inv_cursor(true);
2472 return;
2475 break;
2477 case MAIN_MENU_SAVE:
2478 rb->lcd_set_foreground(COLOR_BLACK);
2479 if (!filename[0])
2480 rb->strcpy(filename,"/");
2481 if( !rb->kbd_input( filename, MAX_PATH ) )
2483 if(rb->strlen(filename) <= 4 ||
2484 rb->strcasecmp(&filename[rb->strlen(filename)-4], ".bmp"))
2485 rb->strcat(filename, ".bmp");
2486 save_bitmap( filename );
2487 rb->splashf( 1*HZ, "File saved (%s)", filename );
2489 break;
2491 case MAIN_MENU_BRUSH_SIZE:
2492 for(multi = 0; multi<4; multi++)
2493 if(bsize == times_list[multi]) break;
2494 rb->do_menu( &size_menu, &multi, NULL, false );
2495 if( multi >= 0 )
2496 bsize = times_list[multi];
2497 break;
2499 case MAIN_MENU_BRUSH_SPEED:
2500 for(multi = 0; multi<3; multi++)
2501 if(bspeed == times_list[multi]) break;
2502 rb->do_menu( &speed_menu, &multi, NULL, false );
2503 if( multi >= 0 )
2504 bspeed = times_list[multi];
2505 break;
2507 case MAIN_MENU_COLOR:
2508 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2509 break;
2511 case MAIN_MENU_GRID_SIZE:
2512 for(multi = 0; multi<4; multi++)
2513 if(gridsize == gridsize_list[multi]) break;
2514 rb->do_menu( &gridsize_menu, &multi, NULL, false );
2515 if( multi >= 0 )
2516 gridsize = gridsize_list[multi];
2517 break;
2519 case MAIN_MENU_EXIT:
2520 restore_screen();
2521 quit=true;
2522 return;
2524 case MAIN_MENU_RESUME:
2525 default:
2526 restore_screen();
2527 return;
2528 }/* end switch */
2529 }/* end while */
2532 static void reset_tool( void )
2534 prev_x = -1;
2535 prev_y = -1;
2536 prev_x2 = -1;
2537 prev_y2 = -1;
2538 prev_x3 = -1;
2539 prev_y3 = -1;
2540 tool_mode = -1;
2541 preview = false;
2544 static bool rockpaint_loop( void )
2546 int button=0,i,j;
2547 int accelaration;
2549 toolbar();
2550 restore_screen();
2551 inv_cursor(true);
2553 while (!quit) {
2554 button = rb->button_get(true);
2556 if( tool == Brush && prev_x != -1 )
2558 accelaration = 1;
2560 else if( button & BUTTON_REPEAT )
2562 accelaration = 4;
2564 else
2566 accelaration = 1;
2569 switch(button)
2571 case ROCKPAINT_QUIT:
2572 rb->lcd_set_drawmode(DRMODE_SOLID);
2573 return PLUGIN_OK;
2575 case ROCKPAINT_MENU:
2576 inv_cursor(false);
2577 goto_menu();
2578 restore_screen();
2579 inv_cursor(true);
2580 break;
2582 case ROCKPAINT_DRAW:
2583 inv_cursor(false);
2584 switch( tool )
2586 case Brush:
2587 if( prev_x == -1 ) prev_x = 1;
2588 else prev_x = -1;
2589 break;
2591 case SelectRectangle:
2592 case Line:
2593 case Curve:
2594 case Rectangle:
2595 case RectangleFull:
2596 case Oval:
2597 case OvalFull:
2598 case LinearGradient:
2599 case RadialGradient:
2600 /* Curve uses 4 points, others use 2 */
2601 if( prev_x == -1 || prev_y == -1 )
2603 prev_x = x;
2604 prev_y = y;
2605 preview = true;
2607 else if( tool == Curve
2608 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2610 prev_x2 = x;
2611 prev_y2 = y;
2613 else if( tool == SelectRectangle
2614 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2616 tool_mode = rb->do_menu( &select_menu,
2617 NULL, NULL, false );
2618 switch( tool_mode )
2620 case SELECT_MENU_CUT:
2621 case SELECT_MENU_COPY:
2622 prev_x2 = x;
2623 prev_y2 = y;
2624 copy_to_clipboard();
2625 if( prev_x < x ) x = prev_x;
2626 if( prev_y < y ) y = prev_y;
2627 break;
2629 case SELECT_MENU_INVERT:
2630 draw_invert( prev_x, prev_y, x, y );
2631 reset_tool();
2632 break;
2634 case SELECT_MENU_HFLIP:
2635 draw_hflip( prev_x, prev_y, x, y );
2636 reset_tool();
2637 break;
2639 case SELECT_MENU_VFLIP:
2640 draw_vflip( prev_x, prev_y, x, y );
2641 reset_tool();
2642 break;
2644 case SELECT_MENU_ROTATE90:
2645 draw_rot_90_deg( prev_x, prev_y, x, y, 1 );
2646 reset_tool();
2647 break;
2649 case SELECT_MENU_ROTATE180:
2650 draw_hflip( prev_x, prev_y, x, y );
2651 draw_vflip( prev_x, prev_y, x, y );
2652 reset_tool();
2653 break;
2655 case SELECT_MENU_ROTATE270:
2656 draw_rot_90_deg( prev_x, prev_y, x, y, -1 );
2657 reset_tool();
2658 break;
2660 case SELECT_MENU_CANCEL:
2661 reset_tool();
2662 break;
2664 default:
2665 break;
2667 restore_screen();
2669 else if( tool == Curve
2670 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2672 prev_x3 = x;
2673 prev_y3 = y;
2675 else
2677 preview = false;
2678 switch( tool )
2680 case SelectRectangle:
2681 draw_paste_rectangle( prev_x, prev_y,
2682 prev_x2, prev_y2,
2683 x, y, tool_mode );
2684 break;
2685 case Line:
2686 draw_line( prev_x, prev_y, x, y );
2687 break;
2688 case Curve:
2689 draw_curve( prev_x, prev_y,
2690 prev_x2, prev_y2,
2691 prev_x3, prev_y3,
2692 x, y );
2693 break;
2694 case Rectangle:
2695 draw_rect( prev_x, prev_y, x, y );
2696 break;
2697 case RectangleFull:
2698 draw_rect_full( prev_x, prev_y, x, y );
2699 break;
2700 case Oval:
2701 draw_oval_empty( prev_x, prev_y, x, y );
2702 break;
2703 case OvalFull:
2704 draw_oval_full( prev_x, prev_y, x, y );
2705 break;
2706 case LinearGradient:
2707 linear_gradient( prev_x, prev_y, x, y );
2708 break;
2709 case RadialGradient:
2710 radial_gradient( prev_x, prev_y, x, y );
2711 break;
2712 default:
2713 break;
2715 reset_tool();
2717 break;
2719 case Fill:
2720 draw_fill( x, y );
2721 break;
2723 case ColorPicker:
2724 color_picker( x, y );
2725 break;
2727 case Text:
2728 draw_text( x, y );
2729 break;
2731 default:
2732 break;
2734 inv_cursor(true);
2735 break;
2737 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2738 if( tool == Curve )
2740 /* 3 point bezier curve */
2741 preview = false;
2742 draw_curve( prev_x, prev_y,
2743 prev_x2, prev_y2,
2744 -1, -1,
2745 x, y );
2746 reset_tool();
2747 restore_screen();
2748 inv_cursor( true );
2750 break;
2752 case ROCKPAINT_TOOLBAR:
2753 i = x; j = y;
2754 x = 10;
2755 toolbar();
2756 x = i; y = j;
2757 restore_screen();
2758 inv_cursor(true);
2759 break;
2761 case ROCKPAINT_TOOLBAR2:
2762 i = x; j = y;
2763 x = 110;
2764 toolbar();
2765 x = i; y = j;
2766 restore_screen();
2767 inv_cursor(true);
2768 break;
2770 case ROCKPAINT_LEFT:
2771 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2772 inv_cursor(false);
2773 x-=bspeed * accelaration;
2774 if (x<0) x=COLS-1;
2775 inv_cursor(true);
2776 break;
2778 case ROCKPAINT_RIGHT:
2779 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2780 inv_cursor(false);
2781 x+=bspeed * accelaration;
2782 if (x>=COLS) x=0;
2783 inv_cursor(true);
2784 break;
2786 case ROCKPAINT_UP:
2787 case ROCKPAINT_UP | BUTTON_REPEAT:
2788 inv_cursor(false);
2789 y-=bspeed * accelaration;
2790 if (y<0) y=ROWS-1;
2791 inv_cursor(true);
2792 break;
2794 case ROCKPAINT_DOWN:
2795 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2796 inv_cursor(false);
2797 y+=bspeed * accelaration;
2798 if (y>=ROWS)
2800 toolbar();
2801 restore_screen();
2803 inv_cursor(true);
2804 break;
2806 default:
2807 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2808 return PLUGIN_USB_CONNECTED;
2809 break;
2811 if( tool == Brush && prev_x == 1 )
2813 inv_cursor(false);
2814 draw_brush( x, y );
2815 inv_cursor(true);
2817 if( preview || tool == ColorPicker )
2818 /* always preview color picker */
2820 restore_screen();
2821 switch( tool )
2823 case SelectRectangle:
2824 if( prev_x2 == -1 || prev_y2 == -1 )
2826 /* we are defining the selection */
2827 draw_select_rectangle( prev_x, prev_y, x, y );
2829 else
2831 /* we are pasting the selected data */
2832 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2833 prev_y2, x, y, tool_mode );
2834 prev_x3 = prev_x2-prev_x;
2835 if( prev_x3 < 0 ) prev_x3 *= -1;
2836 prev_y3 = prev_y2-prev_y;
2837 if( prev_y3 < 0 ) prev_y3 *= -1;
2838 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2839 prev_x3 = -1;
2840 prev_y3 = -1;
2842 break;
2844 case Brush:
2845 break;
2847 case Line:
2848 draw_line( prev_x, prev_y, x, y );
2849 break;
2851 case Curve:
2852 if( prev_x2 == -1 || prev_y2 == -1 )
2854 draw_line( prev_x, prev_y, x, y );
2856 else
2858 draw_curve( prev_x, prev_y,
2859 prev_x2, prev_y2,
2860 prev_x3, prev_y3,
2861 x, y );
2863 break;
2865 case Rectangle:
2866 draw_rect( prev_x, prev_y, x, y );
2867 break;
2869 case RectangleFull:
2870 draw_rect_full( prev_x, prev_y, x, y );
2871 break;
2873 case Oval:
2874 draw_oval_empty( prev_x, prev_y, x, y );
2875 break;
2877 case OvalFull:
2878 draw_oval_full( prev_x, prev_y, x, y );
2879 break;
2881 case Fill:
2882 break;
2884 case ColorPicker:
2885 preview = true;
2886 color_picker( x, y );
2887 preview = false;
2888 break;
2890 case LinearGradient:
2891 line_gradient( prev_x, prev_y, x, y );
2892 break;
2894 case RadialGradient:
2895 line_gradient( prev_x, prev_y, x, y );
2896 break;
2898 case Text:
2899 default:
2900 break;
2902 inv_cursor( true );
2904 if( gridsize > 0 )
2906 show_grid( true );
2907 show_grid( false );
2911 return PLUGIN_OK;
2914 static int load_bitmap( const char *file )
2916 struct bitmap bm;
2917 bool ret;
2918 int l;
2920 bm.data = (char*)save_buffer;
2921 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2922 FORMAT_NATIVE, NULL );
2924 if((bm.width > COLS ) || ( bm.height > ROWS ))
2925 return -1;
2927 for( l = bm.height-1; l > 0; l-- )
2929 rb->memmove( save_buffer+l*COLS, save_buffer+l*bm.width,
2930 sizeof( fb_data )*bm.width );
2932 for( l = 0; l < bm.height; l++ )
2934 rb->memset( save_buffer+l*COLS+bm.width, rp_colors[ bgdrawcolor ],
2935 sizeof( fb_data )*(COLS-bm.width) );
2937 rb->memset( save_buffer+COLS*bm.height, rp_colors[ bgdrawcolor ],
2938 sizeof( fb_data )*COLS*(ROWS-bm.height) );
2940 return ret;
2943 static int save_bitmap( char *file )
2945 struct bitmap bm;
2946 bm.data = (char*)save_buffer;
2947 bm.height = ROWS;
2948 bm.width = COLS;
2949 bm.format = FORMAT_NATIVE;
2950 return save_bmp_file( file, &bm );
2953 enum plugin_status plugin_start(const void* parameter)
2955 rb->lcd_set_foreground(COLOR_WHITE);
2956 rb->lcd_set_backdrop(NULL);
2957 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
2958 rb->splash( HZ/2, "Rock Paint");
2960 rb->lcd_clear_display();
2962 filename[0] = '\0';
2964 if( parameter )
2966 if( load_bitmap( parameter ) <= 0 )
2968 rb->splash( 1*HZ, "File Open Error");
2969 clear_drawing();
2971 else
2973 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
2974 restore_screen();
2975 rb->strcpy( filename, parameter );
2978 else
2980 clear_drawing();
2982 inv_cursor(true);
2984 return rockpaint_loop();