Colour targets: Revert an optimisation from almost 18 months ago that actually turned...
[Rockbox.git] / apps / plugins / rockpaint.c
blob82fb2ea29b4f4d5bec262b313c70791fbdf0336b
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 "errno.h"
33 #include "lib/bmp.h"
34 #include "lib/rgb_hsv.h"
36 PLUGIN_HEADER
38 /***********************************************************************
39 * Buttons
40 ***********************************************************************/
42 #if CONFIG_KEYPAD == IRIVER_H300_PAD
43 #define ROCKPAINT_QUIT BUTTON_OFF
44 #define ROCKPAINT_DRAW BUTTON_SELECT
45 #define ROCKPAINT_MENU BUTTON_ON
46 #define ROCKPAINT_TOOLBAR BUTTON_REC
47 #define ROCKPAINT_TOOLBAR2 BUTTON_MODE
48 #define ROCKPAINT_UP BUTTON_UP
49 #define ROCKPAINT_DOWN BUTTON_DOWN
50 #define ROCKPAINT_LEFT BUTTON_LEFT
51 #define ROCKPAINT_RIGHT BUTTON_RIGHT
53 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
54 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
55 #define ROCKPAINT_QUIT ( ~BUTTON_MAIN )
56 #define ROCKPAINT_DRAW BUTTON_SELECT
57 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_MENU )
58 #define ROCKPAINT_TOOLBAR ( BUTTON_MENU | BUTTON_LEFT )
59 #define ROCKPAINT_TOOLBAR2 ( BUTTON_MENU | BUTTON_RIGHT )
60 #define ROCKPAINT_UP BUTTON_MENU
61 #define ROCKPAINT_DOWN BUTTON_PLAY
62 #define ROCKPAINT_LEFT BUTTON_LEFT
63 #define ROCKPAINT_RIGHT BUTTON_RIGHT
65 #elif ( CONFIG_KEYPAD == IAUDIO_X5M5_PAD )
66 #define ROCKPAINT_QUIT BUTTON_POWER
67 #define ROCKPAINT_DRAW BUTTON_SELECT
68 #define ROCKPAINT_MENU BUTTON_PLAY
69 #define ROCKPAINT_TOOLBAR BUTTON_REC
70 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
71 #define ROCKPAINT_UP BUTTON_UP
72 #define ROCKPAINT_DOWN BUTTON_DOWN
73 #define ROCKPAINT_LEFT BUTTON_LEFT
74 #define ROCKPAINT_RIGHT BUTTON_RIGHT
76 #elif CONFIG_KEYPAD == GIGABEAT_PAD
77 #define ROCKPAINT_QUIT BUTTON_POWER
78 #define ROCKPAINT_DRAW BUTTON_SELECT
79 #define ROCKPAINT_MENU BUTTON_MENU
80 #define ROCKPAINT_TOOLBAR BUTTON_A
81 #define ROCKPAINT_TOOLBAR2 ( BUTTON_A | BUTTON_LEFT )
82 #define ROCKPAINT_UP BUTTON_UP
83 #define ROCKPAINT_DOWN BUTTON_DOWN
84 #define ROCKPAINT_LEFT BUTTON_LEFT
85 #define ROCKPAINT_RIGHT BUTTON_RIGHT
87 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
88 (CONFIG_KEYPAD == SANSA_C200_PAD)
89 #define ROCKPAINT_QUIT BUTTON_POWER
90 #define ROCKPAINT_DRAW BUTTON_SELECT
91 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_POWER )
92 #define ROCKPAINT_TOOLBAR BUTTON_REC
93 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
94 #define ROCKPAINT_UP BUTTON_UP
95 #define ROCKPAINT_DOWN BUTTON_DOWN
96 #define ROCKPAINT_LEFT BUTTON_LEFT
97 #define ROCKPAINT_RIGHT BUTTON_RIGHT
99 #elif ( CONFIG_KEYPAD == IRIVER_H10_PAD )
100 #define ROCKPAINT_QUIT BUTTON_POWER
101 #define ROCKPAINT_DRAW BUTTON_FF
102 #define ROCKPAINT_MENU BUTTON_PLAY
103 #define ROCKPAINT_TOOLBAR BUTTON_REW
104 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
105 #define ROCKPAINT_UP BUTTON_SCROLL_UP
106 #define ROCKPAINT_DOWN BUTTON_SCROLL_DOWN
107 #define ROCKPAINT_LEFT BUTTON_LEFT
108 #define ROCKPAINT_RIGHT BUTTON_RIGHT
110 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
111 #define ROCKPAINT_QUIT BUTTON_BACK
112 #define ROCKPAINT_DRAW BUTTON_SELECT
113 #define ROCKPAINT_MENU BUTTON_MENU
114 #define ROCKPAINT_TOOLBAR BUTTON_PLAY
115 #define ROCKPAINT_TOOLBAR2 ( BUTTON_PLAY | BUTTON_LEFT )
116 #define ROCKPAINT_UP BUTTON_UP
117 #define ROCKPAINT_DOWN BUTTON_DOWN
118 #define ROCKPAINT_LEFT BUTTON_LEFT
119 #define ROCKPAINT_RIGHT BUTTON_RIGHT
121 #elif ( CONFIG_KEYPAD == COWOND2_PAD )
122 #define ROCKPAINT_QUIT BUTTON_POWER
123 #define ROCKPAINT_MENU BUTTON_MENU
125 #else
126 #error "Please define keys for this keypad"
127 #endif
129 #ifdef HAVE_TOUCHPAD
130 #ifndef ROCKPAINT_QUIT
131 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
132 #endif
133 #ifndef ROCKPAINT_DRAW
134 #define ROCKPAINT_DRAW BUTTON_CENTER
135 #endif
136 #ifndef ROCKPAINT_MENU
137 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
138 #endif
139 #ifndef ROCKPAINT_TOOLBAR
140 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
141 #endif
142 #ifndef ROCKPAINT_TOOLBAR2
143 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
144 #endif
145 #ifndef ROCKPAINT_UP
146 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
147 #endif
148 #ifndef ROCKPAINT_DOWN
149 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
150 #endif
151 #ifndef ROCKPAINT_LEFT
152 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
153 #endif
154 #ifndef ROCKPAINT_RIGHT
155 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
156 #endif
157 #endif
159 /***********************************************************************
160 * Palette Default Colors
161 ***********************************************************************/
162 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
163 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
164 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
165 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
166 #define COLOR_RED LCD_RGBPACK(128,0,0)
167 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
168 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
169 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
170 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
171 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
172 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
173 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
174 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
175 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
176 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
177 #define COLOR_PINK LCD_RGBPACK(255,0,255)
178 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
179 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
181 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
182 #define ROCKPAINT_TITLE_FONT 2
184 /***********************************************************************
185 * Program Colors
186 ***********************************************************************/
187 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
188 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
190 #define ROWS LCD_HEIGHT
191 #define COLS LCD_WIDTH
194 * Toolbar positioning stuff ... don't read this unless you really need to
196 * TB Toolbar
197 * SP Separator
198 * SC Selected Color
199 * PL Palette
200 * TL Tools
203 /* Separator sizes */
204 #define TB_SP_MARGIN 3
205 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
207 /* Selected color sizes */
208 #define TB_SC_SIZE 12
210 /* Palette sizes */
211 #define TB_PL_COLOR_SIZE 7
212 #define TB_PL_COLOR_SPACING 2
213 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
214 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
216 /* Tools sizes */
217 #define TB_TL_SIZE 8
218 #define TB_TL_SPACING 2
219 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
220 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
222 /* Menu button size ... gruik */
223 #define TB_MENU_MIN_WIDTH 30
225 /* Selected colors position */
226 #define TB_SC_FG_TOP 2
227 #define TB_SC_FG_LEFT 2
228 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
229 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
231 /* Palette position */
232 #define TB_PL_TOP TB_SC_FG_TOP
233 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
235 /* Tools position */
236 #define TB_TL_TOP TB_SC_FG_TOP
237 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
239 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
240 #undef TB_TL_TOP
241 #undef TB_TL_LEFT
242 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
243 #define TB_TL_LEFT TB_SC_FG_LEFT
244 #endif
246 /* Menu button position */
247 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
248 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
250 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
253 static void draw_pixel(int x,int y);
254 static void draw_line( int x1, int y1, int x2, int y2 );
255 static void draw_rect( int x1, int y1, int x2, int y2 );
256 static void draw_toolbars(bool update);
257 static void inv_cursor(bool update);
258 static void restore_screen(void);
259 static void clear_drawing(void);
260 static void goto_menu(void);
261 static int load_bitmap( const char *filename );
262 static int save_bitmap( char *filename );
263 static void draw_rect_full( int x1, int y1, int x2, int y2 );
264 extern int errno;
266 /***********************************************************************
267 * Global variables
268 ***********************************************************************/
270 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
271 int errno;
272 #endif
274 static const struct plugin_api* rb;
276 MEM_FUNCTION_WRAPPERS(rb);
278 static int drawcolor=0; /* Current color (in palette) */
279 static int bgdrawcolor=9; /* Current background color (in palette) */
280 bool isbg = false; /* gruik ugly hack alert */
282 static int preview=false; /* Is preview mode on ? */
284 /* TODO: clean this up */
285 static int x=0, y=0; /* cursor position */
286 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
287 static int prev_x2=-1, prev_y2=-1;
288 static int prev_x3=-1, prev_y3=-1;
289 static int tool_mode=-1;
292 static int bsize=1; /* brush size */
293 static int bspeed=1; /* brush speed */
295 enum Tools { Brush = 0, /* Regular brush */
296 Fill = 1, /* Fill a shape with current color */
297 SelectRectangle = 2,
298 ColorPicker = 3, /* Pick a color */
299 Line = 4, /* Draw a line between two points */
300 Unused = 5, /* THIS IS UNUSED ... */
301 Curve = 6,
302 Text = 7,
303 Rectangle = 8, /* Draw a rectangle */
304 RectangleFull = 9,
305 Oval = 10, /* Draw an oval */
306 OvalFull = 11,
307 LinearGradient = 12,
308 RadialGradient = 13
311 enum Tools tool = Brush;
313 static bool quit=false;
314 static int gridsize=0;
316 static fb_data rp_colors[18] =
318 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
319 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
320 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
321 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
322 COLOR_LIGHTBROWN
325 static fb_data save_buffer[ ROWS*COLS ];
327 extern fb_data rockpaint[];
328 extern fb_data rockpaint_hsvrgb[];
330 /* Maximum string size allowed for the text tool */
331 #define MAX_TEXT 255
333 static union
335 /* Used by fill and gradient algorithms */
336 struct
338 short x;
339 short y;
340 } coord[ ROWS*COLS ];
342 /* Used by bezier curve algorithms */
343 struct
345 short x1, y1;
346 short x2, y2;
347 short x3, y3;
348 short x4, y4;
349 short depth;
350 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
351 * than coord ... so we divide to take
352 * less memory. */
354 /* Used to cut/copy/paste data */
355 fb_data clipboard[ ROWS*COLS ];
357 /* Used for text mode */
358 struct
360 char text[MAX_TEXT+1];
361 char font[MAX_PATH+1];
362 char old_font[MAX_PATH+1];
363 int fh_buf[30];
364 int fw_buf[30];
365 char fontname_buf[30][MAX_PATH];
366 } text;
367 } buffer;
369 /* Current filename */
370 static char filename[MAX_PATH+1];
372 /* Font preview buffer */
373 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
374 //#define FONT_PREVIEW_HEIGHT 1000
375 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
377 /***********************************************************************
378 * Offscreen buffer/Text/Fonts handling
380 * Parts of code taken from firmware/drivers/lcd-16bit.c
381 ***********************************************************************/
382 static void buffer_mono_bitmap_part(
383 fb_data *buf, int buf_width, int buf_height,
384 const unsigned char *src, int src_x, int src_y,
385 int stride, int x, int y, int width, int height )
386 /* this function only draws the foreground part of the bitmap */
388 const unsigned char *src_end;
389 fb_data *dst, *dst_end;
390 unsigned fgcolor = rb->lcd_get_foreground();
392 /* nothing to draw? */
393 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
394 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
395 return;
397 /* clipping */
398 if( x < 0 )
400 width += x;
401 src_x -= x;
402 x = 0;
404 if( y < 0 )
406 height += y;
407 src_y -= y;
408 y = 0;
410 if( x + width > buf_width )
411 width = buf_width - x;
412 if( y + height > buf_height )
413 height = buf_height - y;
415 src += stride * (src_y >> 3) + src_x; /* move starting point */
416 src_y &= 7;
417 src_end = src + width;
419 dst = buf + y*buf_width + x;
423 const unsigned char *src_col = src++;
424 unsigned data = *src_col >> src_y;
425 fb_data *dst_col = dst++;
426 int numbits = 8 - src_y;
428 dst_end = dst_col + height * buf_width;
431 if( data & 0x01 )
432 *dst_col = fgcolor; /* FIXME ? */
434 dst_col += buf_width;
436 data >>= 1;
437 if( --numbits == 0 )
439 src_col += stride;
440 data = *src_col;
441 numbits = 8;
443 } while( dst_col < dst_end );
444 } while( src < src_end );
447 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
448 int x, int y, int ofs, const unsigned char *str )
450 unsigned short ch;
451 unsigned short *ucs;
453 struct font *pf = rb->font_get( FONT_UI );
454 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
456 ucs = rb->bidi_l2v( str, 1 );
458 while( (ch = *ucs++) != 0 && x < buf_width )
460 int width;
461 const unsigned char *bits;
463 /* get proportional width and glyph bits */
464 width = rb->font_get_width( pf, ch );
466 if( ofs > width )
468 ofs -= width;
469 continue;
472 bits = rb->font_get_bits( pf, ch );
474 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0, width, x, y, width - ofs, pf->height);
476 x += width - ofs;
477 ofs = 0;
481 /***********************************************************************
482 * Menu handling
483 ***********************************************************************/
484 struct menu_items
486 int value;
487 char label[16]; /* GRUIK ? */
490 #define MENU_ESC -1242
491 enum {
492 /* Generic menu items */
493 MENU_END = -42, MENU_TITLE = -12,
494 /* Main menu */
495 MAIN_MENU_RESUME,
496 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
497 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
498 MAIN_MENU_GRID_SIZE,
499 MAIN_MENU_EXIT,
500 /* Select action menu */
501 SELECT_MENU_CUT, SELECT_MENU_COPY, SELECT_MENU_INVERT,
502 SELECT_MENU_HFLIP, SELECT_MENU_VFLIP, SELECT_MENU_ROTATE90,
503 SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
504 SELECT_MENU_CANCEL,
505 /* Text menu */
506 TEXT_MENU_TEXT, TEXT_MENU_FONT,
507 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
510 static struct menu_items main_menu[]=
511 { { MENU_TITLE, "RockPaint" },
512 { MAIN_MENU_RESUME, "Resume" },
513 { MAIN_MENU_NEW, "New" },
514 { MAIN_MENU_LOAD, "Load" },
515 { MAIN_MENU_SAVE, "Save" },
516 { MAIN_MENU_BRUSH_SIZE, "Brush Size" },
517 { MAIN_MENU_BRUSH_SPEED, "Brush Speed" },
518 { MAIN_MENU_COLOR, "Choose Color" },
519 { MAIN_MENU_GRID_SIZE, "Grid Size" },
520 { MAIN_MENU_EXIT, "Exit" },
521 { MENU_END, "" } };
523 static struct menu_items size_menu[] =
524 { { MENU_TITLE, "Choose Size" },
525 { 1, "1x" },
526 { 2, "2x" },
527 { 4, "4x" },
528 { 8, "8x" },
529 { MENU_END, "" } };
531 static struct menu_items speed_menu[] =
532 { { MENU_TITLE, "Choose Speed" },
533 { 1, "1x" },
534 { 2, "2x" },
535 { 4, "4x" },
536 { MENU_END, "" } };
538 static struct menu_items gridsize_menu[] =
539 { { MENU_TITLE, "Grid Size" },
540 { 0, "No grid" },
541 { 5, "5px" },
542 { 10, "10px" },
543 { 20, "20px" },
544 { MENU_END, "" } };
546 static struct menu_items select_menu[] =
547 { { MENU_TITLE, "Select..." },
548 { SELECT_MENU_CUT, "Cut" },
549 { SELECT_MENU_COPY, "Copy" },
550 { SELECT_MENU_INVERT, "Invert" },
551 { SELECT_MENU_HFLIP, "Horizontal flip" },
552 { SELECT_MENU_VFLIP, "Vertical flip" },
553 // { SELECT_MENU_ROTATE90, "Rotate 90°" },
554 { SELECT_MENU_ROTATE180, "Rotate 180°" },
555 // { SELECT_MENU_ROTATE270, "Rotate 270°" },
556 { SELECT_MENU_CANCEL, "Cancel" },
557 { MENU_END, "" } };
559 static struct menu_items text_menu[] =
560 { { MENU_TITLE, "Text" },
561 { TEXT_MENU_TEXT, "Set text" },
562 { TEXT_MENU_FONT, "Change font" },
563 { TEXT_MENU_PREVIEW, "Preview" },
564 { TEXT_MENU_APPLY, "Apply" },
565 { TEXT_MENU_CANCEL, "Cancel" },
566 { MENU_END, "" } };
568 static int draw_window( int height, int width,
569 int *top, int *left,
570 const char *title )
572 int fh;
573 rb->lcd_getstringsize( title, NULL, &fh );
574 fh++;
576 const int _top = ( LCD_HEIGHT - height ) / 2;
577 const int _left = ( LCD_WIDTH - width ) / 2;
578 if( top ) *top = _top;
579 if( left ) *left = _left;
580 rb->lcd_set_background(COLOR_BLUE);
581 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
582 rb->lcd_fillrect( _left, _top, width, height );
583 rb->lcd_set_foreground(COLOR_BLUE);
584 rb->lcd_fillrect( _left, _top, width, fh+4 );
585 rb->lcd_set_foreground(COLOR_WHITE);
586 rb->lcd_putsxy( _left+2, _top+2, title );
587 rb->lcd_set_foreground(COLOR_BLACK);
588 rb->lcd_drawrect( _left, _top, width, height );
589 return _top+fh+4;
592 static int menu_display( struct menu_items menu[], int prev_value )
594 int i,
595 fh, /* font height */
596 width, height,
597 a, b,
598 selection=1,
599 menu_length,
600 top, left;
601 int scroll = 0, onscreen = 0;
603 rb->lcd_getstringsize( menu[0].label, &width, &fh );
604 for( i=1; menu[i].value != MENU_END; i++ )
606 if( prev_value == menu[i].value )
608 selection = i;
610 rb->lcd_getstringsize( menu[i].label, &a, &b );
611 if( a > width ) width = a;
612 if( b > fh ) fh = b;
614 menu_length = i;
615 fh++;
616 width += 10;
618 height = menu_length * fh + 4 + 2 + 2;
619 if( height >= LCD_HEIGHT )
621 scroll = 1;
622 onscreen = ( LCD_HEIGHT - 4 - 2 - 2 )/fh;
623 height = onscreen * fh + 4 + 2 + 2;
624 width += 5;
626 else
628 onscreen = menu_length;
631 draw_window( height, width, &top, &left, menu[0].label );
633 while( 1 )
635 for( i = (scroll == 0 ? 1 : scroll);
636 i < (scroll ? scroll + onscreen - 1:onscreen);
637 i++ )
639 if( i == selection )
641 rb->lcd_set_foreground( COLOR_WHITE );
642 rb->lcd_set_background( COLOR_BLUE );
644 else
646 rb->lcd_set_foreground( COLOR_BLACK );
647 rb->lcd_set_background( COLOR_LIGHTGRAY );
649 rb->lcd_putsxy( left+2,
650 top+6+fh*(i-(scroll == 0 ? 0 : scroll-1 )),
651 menu[i].label );
653 if( scroll )
655 int scroll_height = ((height-fh-4-2)*onscreen)/menu_length;
656 rb->lcd_set_foreground( COLOR_BLACK );
657 rb->lcd_vline( left+width-5, top+fh+4, top+height-2 );
658 rb->lcd_fillrect( left+width-4,
659 top+fh+4+((height-4-2-fh-scroll_height)*(scroll-1))/(menu_length-onscreen),
660 3, scroll_height );
662 rb->lcd_update();
664 switch( rb->button_get(true) )
666 case ROCKPAINT_UP:
667 case ROCKPAINT_UP|BUTTON_REPEAT:
668 selection = (selection + menu_length-1)%menu_length;
669 if( !selection ) selection = menu_length-1;
670 break;
672 case ROCKPAINT_DOWN:
673 case ROCKPAINT_DOWN|BUTTON_REPEAT:
674 selection = (selection + 1)%menu_length;
675 if( !selection ) selection++;
676 break;
678 case ROCKPAINT_LEFT:
679 restore_screen();
680 return MENU_ESC;
682 case ROCKPAINT_RIGHT:
683 case ROCKPAINT_DRAW:
684 restore_screen();
685 return menu[selection].value;
687 if( scroll )
689 if( selection < scroll )
691 scroll = selection;
692 draw_window( height, width, NULL, NULL, menu[0].label );
694 if( selection >= scroll + onscreen - 1 )
696 scroll++;
697 draw_window( height, width, NULL, NULL, menu[0].label );
703 /***********************************************************************
704 * File browser
705 ***********************************************************************/
707 char bbuf[MAX_PATH+1]; /* used by file and font browsers */
708 char bbuf_s[MAX_PATH+1]; /* used by file and font browsers */
710 static bool browse( char *dst, int dst_size, const char *start )
712 #define WIDTH ( LCD_WIDTH - 20 )
713 #define HEIGHT ( LCD_HEIGHT - 20 )
714 #define LINE_SPACE 2
715 int top, top_inside, left;
717 DIR *d;
718 struct dirent *de;
719 int fvi = 0; /* first visible item */
720 int lvi = 0; /* last visible item */
721 int si = 0; /* selected item */
722 int li = 0; /* last item */
723 int i;
725 int fh;
726 char *a;
728 rb->lcd_getstringsize( "Ap", NULL, &fh );
730 rb->strcpy( bbuf, start );
731 a = bbuf+rb->strlen(bbuf)-1;
732 if( *a != '/' )
734 a[1] = '/';
735 a[2] = '\0';
738 while( 1 )
740 d = rb->opendir( bbuf );
741 if( !d )
744 if( errno == ENOTDIR )
746 /* this is a file */
747 bbuf[rb->strlen(bbuf)-1] = '\0';
748 rb->strncpy( dst, bbuf, dst_size );
749 return true;
751 else if( errno == EACCES || errno == ENOENT )
753 bbuf[0] = '/'; bbuf[1] = '\0';
754 d = rb->opendir( "/" );
756 else
758 return false;
761 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, bbuf );
762 i = 0;
763 li = -1;
764 while( i < fvi )
766 rb->readdir( d );
767 i++;
769 while( top_inside+(i-fvi)*(fh+LINE_SPACE) < HEIGHT )
771 de = rb->readdir( d );
772 if( !de )
774 li = i-1;
775 break;
777 rb->lcd_set_foreground((si==i?COLOR_WHITE:COLOR_BLACK));
778 rb->lcd_set_background((si==i?COLOR_BLUE:COLOR_LIGHTGRAY));
779 rb->lcd_putsxy( left+10,
780 top_inside+(i-fvi)*(fh+LINE_SPACE),
781 de->d_name );
782 if( si == i )
783 rb->strcpy( bbuf_s, de->d_name );
784 i++;
786 lvi = i-1;
787 if( li == -1 )
789 if( !rb->readdir( d ) )
791 li = lvi;
794 rb->closedir( d );
796 rb->lcd_update();
798 switch( rb->button_get(true) )
800 case ROCKPAINT_UP:
801 case ROCKPAINT_UP|BUTTON_REPEAT:
802 if( si > 0 )
804 si--;
805 if( si<fvi )
807 fvi--;
810 break;
812 case ROCKPAINT_DOWN:
813 case ROCKPAINT_DOWN|BUTTON_REPEAT:
814 if( li == -1 || si < li )
816 si++;
817 if( si>lvi )
819 fvi++;
822 break;
824 case ROCKPAINT_LEFT:
825 if( bbuf[0] == '/' && !bbuf[1] ) return false;
826 bbuf_s[0] = '.';
827 bbuf_s[1] = '.';
828 bbuf_s[2] = '\0';
829 case ROCKPAINT_RIGHT:
830 case ROCKPAINT_DRAW:
831 if( *bbuf_s == '.' && !bbuf_s[1] ) break;
832 a = bbuf;
833 while( *a ) a++;
834 if( *bbuf_s == '.' && bbuf_s[1] == '.' && !bbuf_s[2] )
836 a--;
837 if( a == bbuf ) break;
838 if( *a == '/' ) a--;
839 while( *a != '/' ) a--;
840 *++a = '\0';
841 break;
843 rb->strcpy( a, bbuf_s );
844 while( *a ) a++;
845 *a++ = '/';
846 *a = '\0';
847 fvi = si = 0;
848 break;
852 #undef WIDTH
853 #undef HEIGHT
854 #undef LINE_SPACE
857 /***********************************************************************
858 * Font browser
860 * FIXME: This still needs some work ... it currently only works fine
861 * on the simulators, disk spins too much on real targets -> rendered
862 * font buffer needed.
863 ***********************************************************************/
864 static bool browse_fonts( char *dst, int dst_size )
866 char old_font[MAX_PATH];
867 #define WIDTH ( LCD_WIDTH - 20 )
868 #define HEIGHT ( LCD_HEIGHT - 20 )
869 #define LINE_SPACE 2
870 int top, top_inside = 0, left;
872 DIR *d;
873 struct dirent *de;
874 int fvi = 0; /* first visible item */
875 int lvi = 0; /* last visible item */
876 int si = 0; /* selected item */
877 int osi = 0; /* old selected item */
878 int li = 0; /* last item */
879 int nvih = 0; /* next visible item height */
880 int i;
881 int b_need_redraw = 1; /* Do we need to redraw ? */
883 int cp = 0; /* current position */
884 int fh; /* font height */
886 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
887 #define fw_buf buffer.text.fw_buf
888 int fw;
889 #define fontname_buf buffer.text.fontname_buf
891 rb->snprintf( old_font, MAX_PATH,
892 FONT_DIR "/%s.fnt",
893 rb->global_settings->font_file );
895 while( 1 )
897 if( !b_need_redraw )
899 /* we don't need to redraw ... but we need to unselect
900 * the previously selected item */
901 cp = top_inside + LINE_SPACE;
902 for( i = 0; i+fvi < osi; i++ )
904 cp += fh_buf[i] + LINE_SPACE;
906 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
907 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
908 rb->lcd_set_drawmode(DRMODE_SOLID);
911 if( b_need_redraw )
913 b_need_redraw = 0;
915 d = rb->opendir( FONT_DIR "/" );
916 if( !d )
918 return false;
920 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
921 i = 0;
922 li = -1;
923 while( i < fvi )
925 rb->readdir( d );
926 i++;
928 cp = top_inside+LINE_SPACE;
930 rb->lcd_set_foreground(COLOR_BLACK);
931 rb->lcd_set_background(COLOR_LIGHTGRAY);
933 while( cp < top+HEIGHT )
935 de = rb->readdir( d );
936 if( !de )
938 li = i-1;
939 break;
941 if( rb->strlen( de->d_name ) < 4
942 || rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
943 ".fnt" ) )
944 continue;
945 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
946 de->d_name );
947 rb->font_load( bbuf );
948 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
949 if( nvih > 0 )
951 nvih -= fh;
952 fvi++;
953 if( nvih < 0 ) nvih = 0;
954 i++;
955 continue;
957 if( cp + fh >= top+HEIGHT )
959 nvih = fh;
960 break;
962 rb->lcd_putsxy( left+10, cp, de->d_name );
963 fh_buf[i-fvi] = fh;
964 fw_buf[i-fvi] = fw;
965 cp += fh + LINE_SPACE;
966 rb->strcpy( fontname_buf[i-fvi], bbuf );
967 i++;
969 lvi = i-1;
970 if( li == -1 )
972 if( !(de = rb->readdir( d ) ) )
974 li = lvi;
976 else if( !nvih && !rb->strlen( de->d_name ) < 4
977 && !rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
978 ".fnt" ) )
980 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
981 de->d_name );
982 rb->font_load( bbuf );
983 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
984 nvih = fh;
987 rb->font_load( old_font );
988 rb->closedir( d );
991 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
992 cp = top_inside + LINE_SPACE;
993 for( i = 0; i+fvi < si; i++ )
995 cp += fh_buf[i] + LINE_SPACE;
997 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
998 rb->lcd_set_drawmode(DRMODE_SOLID);
1000 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
1002 osi = si;
1003 i = fvi;
1004 switch( rb->button_get(true) )
1006 case ROCKPAINT_UP:
1007 case ROCKPAINT_UP|BUTTON_REPEAT:
1008 if( si > 0 )
1010 si--;
1011 if( si<fvi )
1013 fvi = si;
1016 break;
1018 case ROCKPAINT_DOWN:
1019 case ROCKPAINT_DOWN|BUTTON_REPEAT:
1020 if( li == -1 || si < li )
1022 si++;
1024 break;
1026 case ROCKPAINT_LEFT:
1027 return false;
1029 case ROCKPAINT_RIGHT:
1030 case ROCKPAINT_DRAW:
1031 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
1032 return true;
1035 if( i != fvi || si > lvi )
1037 b_need_redraw = 1;
1040 if( si<=lvi )
1042 nvih = 0;
1045 #undef fh_buf
1046 #undef fw_buf
1047 #undef fontname_buf
1048 #undef WIDTH
1049 #undef HEIGHT
1050 #undef LINE_SPACE
1053 /***********************************************************************
1054 * HSVRGB Color chooser
1055 ***********************************************************************/
1056 static unsigned int color_chooser( unsigned int color )
1058 int red = RGB_UNPACK_RED( color );
1059 int green = RGB_UNPACK_GREEN( color );
1060 int blue = RGB_UNPACK_BLUE( color );
1061 int hue, saturation, value;
1062 int r, g, b; /* temp variables */
1063 int i, top, left;
1065 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
1066 Red = 3, Green = 4, Blue = 5 };
1067 enum BaseColor current = Red;
1068 bool has_changed;
1070 char str[6] = "";
1072 restore_screen();
1074 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1076 while( 1 )
1078 has_changed = false;
1079 color = LCD_RGBPACK( red, green, blue );
1081 #define HEIGHT ( 100 )
1082 #define WIDTH ( 150 )
1084 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
1085 top -= 15;
1087 for( i=0; i<100; i++ )
1089 hsv2rgb( i*36, saturation, value, &r, &g, &b );
1090 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1091 rb->lcd_vline( left+15+i, top+20, top+27 );
1092 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
1093 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1094 rb->lcd_vline( left+15+i, top+30, top+37 );
1095 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
1096 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1097 rb->lcd_vline( left+15+i, top+40, top+47 );
1098 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
1099 rb->lcd_vline( left+15+i, top+50, top+57 );
1100 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
1101 rb->lcd_vline( left+15+i, top+60, top+67 );
1102 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
1103 rb->lcd_vline( left+15+i, top+70, top+77 );
1106 rb->lcd_set_foreground(COLOR_BLACK);
1107 #define POSITION( a, i ) \
1108 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
1109 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
1110 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
1111 rb->lcd_drawpixel( left+16+i, top + 28 + a );
1112 POSITION( 0, hue/36 );
1113 POSITION( 10, saturation*99/255 );
1114 POSITION( 20, value*99/255 );
1115 POSITION( 30, red*99/255 );
1116 POSITION( 40, green*99/255 );
1117 POSITION( 50, blue*99/255 );
1118 #undef POSITION
1119 rb->lcd_set_background(COLOR_LIGHTGRAY);
1120 rb->lcd_setfont( FONT_SYSFIXED );
1121 rb->snprintf( str, 6, "%d", hue/10 );
1122 rb->lcd_putsxy( left + 117, top + 20, str );
1123 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1124 rb->lcd_putsxy( left + 117, top + 30, str );
1125 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1126 rb->lcd_putsxy( left + 117, top + 40, str );
1127 rb->snprintf( str, 6, "%d", red );
1128 rb->lcd_putsxy( left + 117, top + 50, str );
1129 rb->snprintf( str, 6, "%d", green );
1130 rb->lcd_putsxy( left + 117, top + 60, str );
1131 rb->snprintf( str, 6, "%d", blue );
1132 rb->lcd_putsxy( left + 117, top + 70, str );
1133 rb->lcd_setfont( FONT_UI );
1135 #define CURSOR( l ) \
1136 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1137 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1138 CURSOR( 5 );
1139 #undef CURSOR
1141 rb->lcd_set_foreground( color );
1142 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1144 rb->lcd_update();
1146 switch( rb->button_get(true) )
1148 case ROCKPAINT_UP:
1149 current = ( current + 5 )%6;
1150 break;
1152 case ROCKPAINT_DOWN:
1153 current = (current + 1 )%6;
1154 break;
1156 case ROCKPAINT_LEFT:
1157 has_changed = true;
1158 switch( current )
1160 case Hue:
1161 hue = ( hue + 3600 - 10 )%3600;
1162 break;
1163 case Saturation:
1164 if( saturation ) saturation--;
1165 break;
1166 case Value:
1167 if( value ) value--;
1168 break;
1169 case Red:
1170 if( red ) red--;
1171 break;
1172 case Green:
1173 if( green ) green--;
1174 break;
1175 case Blue:
1176 if( blue ) blue--;
1177 break;
1179 break;
1181 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1182 has_changed = true;
1183 switch( current )
1185 case Hue:
1186 hue = ( hue + 3600 - 100 )%3600;
1187 break;
1188 case Saturation:
1189 if( saturation >= 8 ) saturation-=8;
1190 else saturation = 0;
1191 break;
1192 case Value:
1193 if( value >= 8 ) value-=8;
1194 else value = 0;
1195 break;
1196 case Red:
1197 if( red >= 8 ) red-=8;
1198 else red = 0;
1199 break;
1200 case Green:
1201 if( green >= 8 ) green-=8;
1202 else green = 0;
1203 break;
1204 case Blue:
1205 if( blue >= 8 ) blue-=8;
1206 else blue = 0;
1207 break;
1209 break;
1211 case ROCKPAINT_RIGHT:
1212 has_changed = true;
1213 switch( current )
1215 case Hue:
1216 hue = ( hue + 10 )%3600;
1217 break;
1218 case Saturation:
1219 if( saturation < 0xff ) saturation++;
1220 break;
1221 case Value:
1222 if( value < 0xff ) value++;
1223 break;
1224 case Red:
1225 if( red < 0xff ) red++;
1226 break;
1227 case Green:
1228 if( green < 0xff ) green++;
1229 break;
1230 case Blue:
1231 if( blue < 0xff ) blue++;
1232 break;
1234 break;
1236 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1237 has_changed = true;
1238 switch( current )
1240 case Hue:
1241 hue = ( hue + 100 )%3600;
1242 break;
1243 case Saturation:
1244 if( saturation < 0xff - 8 ) saturation+=8;
1245 else saturation = 0xff;
1246 break;
1247 case Value:
1248 if( value < 0xff - 8 ) value+=8;
1249 else value = 0xff;
1250 break;
1251 case Red:
1252 if( red < 0xff - 8 ) red+=8;
1253 else red = 0xff;
1254 break;
1255 case Green:
1256 if( green < 0xff - 8 ) green+=8;
1257 else green = 0xff;
1258 break;
1259 case Blue:
1260 if( blue < 0xff - 8 ) blue+=8;
1261 else blue = 0xff;
1262 break;
1264 break;
1266 case ROCKPAINT_DRAW:
1267 return color;
1269 if( has_changed )
1271 switch( current )
1273 case Hue:
1274 case Saturation:
1275 case Value:
1276 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1277 break;
1279 case Red:
1280 case Green:
1281 case Blue:
1282 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1283 break;
1286 #undef HEIGHT
1287 #undef WIDTH
1291 /***********************************************************************
1292 * Misc routines
1293 ***********************************************************************/
1294 static void init_buffer(void)
1296 int i;
1297 fb_data color = rp_colors[ bgdrawcolor ];
1298 for( i = 0; i < ROWS*COLS; i++ )
1300 save_buffer[i] = color;
1304 static void draw_pixel(int x,int y)
1306 if( !preview )
1308 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1309 if( isbg )
1311 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1313 else
1315 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1318 rb->lcd_drawpixel(x,y);
1321 static void color_picker( int x, int y )
1323 if( preview )
1325 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1326 #define PSIZE 12
1327 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1328 rb->lcd_drawrect( x<COLS-PSIZE ? x + 2 : x - PSIZE, y<ROWS-PSIZE ? y + 2: y - PSIZE, PSIZE - 2, PSIZE - 2 );
1329 rb->lcd_set_drawmode(DRMODE_SOLID);
1330 rb->lcd_fillrect( x<COLS-PSIZE ? x+3 : x - PSIZE+1, y<ROWS-PSIZE ? y +3: y - PSIZE+1, PSIZE-4, PSIZE-4 );
1331 #undef PSIZE
1332 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1334 else
1336 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1340 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1341 /* This is a preview mode only function */
1343 int i,a;
1344 if( x1 > x2 )
1346 i = x1;
1347 x1 = x2;
1348 x2 = i;
1350 if( y1 > y2 )
1352 i = y1;
1353 y1 = y2;
1354 y2 = i;
1356 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1357 i = 0;
1358 for( a = x1; a < x2; a++, i++ )
1359 if( i%2 )
1360 rb->lcd_drawpixel( a, y1 );
1361 for( a = y1; a < y2; a++, i++ )
1362 if( i%2 )
1363 rb->lcd_drawpixel( x2, a );
1364 if( y2 != y1 )
1365 for( a = x2; a > x1; a--, i++ )
1366 if( i%2 )
1367 rb->lcd_drawpixel( a, y2 );
1368 if( x2 != x1 )
1369 for( a = y2; a > y1; a--, i++ )
1370 if( i%2 )
1371 rb->lcd_drawpixel( x1, a );
1372 rb->lcd_set_drawmode(DRMODE_SOLID);
1375 static void copy_to_clipboard( void )
1377 /* This needs to be optimised ... but i'm lazy ATM */
1378 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1381 /* no preview mode handling atm ... do we need it ? (one if) */
1382 static void draw_invert( int x1, int y1, int x2, int y2 )
1384 int i;
1385 if( x1 > x2 )
1387 i = x1;
1388 x1 = x2;
1389 x2 = i;
1391 if( y1 > y2 )
1393 i = y1;
1394 y1 = y2;
1395 y2 = i;
1398 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1399 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1400 rb->lcd_set_drawmode(DRMODE_SOLID);
1402 for( ; y1<=y2; y1++ )
1404 for( i = x1; i<=x2; i++ )
1406 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1409 /*if( update )*/ rb->lcd_update();
1412 static void draw_hflip( int x1, int y1, int x2, int y2 )
1414 int i;
1415 if( x1 > x2 )
1417 i = x1;
1418 x1 = x2;
1419 x2 = i;
1421 if( y1 > y2 )
1423 i = y1;
1424 y1 = y2;
1425 y2 = i;
1428 copy_to_clipboard();
1430 for( i = 0; i <= y2 - y1; i++ )
1432 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1433 buffer.clipboard+(y2-i)*COLS+x1,
1434 (x2-x1+1)*sizeof( fb_data ) );
1436 restore_screen();
1437 rb->lcd_update();
1440 static void draw_vflip( int x1, int y1, int x2, int y2 )
1442 int i;
1443 if( x1 > x2 )
1445 i = x1;
1446 x1 = x2;
1447 x2 = i;
1449 if( y1 > y2 )
1451 i = y1;
1452 y1 = y2;
1453 y2 = i;
1456 copy_to_clipboard();
1458 for( ; y1 <= y2; y1++ )
1460 for( i = 0; i <= x2 - x1; i++ )
1462 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1465 restore_screen();
1466 rb->lcd_update();
1469 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1470 int src_y2, int x1, int y1, int mode )
1472 int i;
1473 if( mode == SELECT_MENU_CUT )
1475 i = drawcolor;
1476 drawcolor = bgdrawcolor;
1477 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1478 drawcolor = i;
1480 if( src_x1 > src_x2 )
1482 i = src_x1;
1483 src_x1 = src_x2;
1484 src_x2 = i;
1486 if( src_y1 > src_y2 )
1488 i = src_y1;
1489 src_y1 = src_y2;
1490 src_y2 = i;
1492 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1493 x1, y1, src_x2-src_x1+1, src_y2-src_y1+1 );
1494 if( !preview )
1496 for( i = 0; i <= src_y2 - src_y1; i++ )
1498 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1499 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1500 (src_x2 - src_x1 + 1)*sizeof( fb_data ) );
1505 static void show_grid( bool update )
1507 int i;
1508 if( gridsize > 0 )
1510 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1511 for( i = gridsize; i < COLS; i+= gridsize )
1513 rb->lcd_vline( i, 0, ROWS-1 );
1515 for( i = gridsize; i < ROWS; i+= gridsize )
1517 rb->lcd_hline( 0, COLS-1, i );
1519 rb->lcd_set_drawmode(DRMODE_SOLID);
1520 if( update ) rb->lcd_update();
1524 static void draw_text( int x, int y )
1526 buffer.text.text[0] = '\0';
1527 rb->snprintf( buffer.text.old_font, MAX_PATH,
1528 FONT_DIR "/%s.fnt",
1529 rb->global_settings->font_file );
1530 while( 1 )
1532 int m = TEXT_MENU_TEXT;
1533 switch( m = menu_display( text_menu, m ) )
1535 case TEXT_MENU_TEXT:
1536 rb->kbd_input( buffer.text.text, MAX_TEXT );
1537 restore_screen();
1538 break;
1540 case TEXT_MENU_FONT:
1541 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1543 rb->font_load( buffer.text.font );
1545 break;
1547 case TEXT_MENU_PREVIEW:
1548 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1549 while( 1 )
1551 unsigned int button;
1552 restore_screen();
1553 rb->lcd_putsxy( x, y, buffer.text.text );
1554 rb->lcd_update();
1555 switch( button = rb->button_get( true ) )
1557 case ROCKPAINT_LEFT:
1558 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1559 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1560 if (x<0) x=COLS-1;
1561 break;
1563 case ROCKPAINT_RIGHT:
1564 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1565 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1566 if (x>=COLS) x=0;
1567 break;
1569 case ROCKPAINT_UP:
1570 case ROCKPAINT_UP | BUTTON_REPEAT:
1571 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1572 if (y<0) y=ROWS-1;
1573 break;
1575 case ROCKPAINT_DOWN:
1576 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1577 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1578 if (y>=ROWS-1) y=0;
1579 break;
1581 case ROCKPAINT_DRAW:
1582 button = 1242;
1583 break;
1585 if( button == 1242 ) break;
1587 break;
1589 case TEXT_MENU_APPLY:
1590 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1591 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1592 buffer.text.text );
1593 case TEXT_MENU_CANCEL:
1594 restore_screen();
1595 rb->font_load( buffer.text.old_font );
1596 return;
1601 static void draw_brush( int x, int y )
1603 int i,j;
1604 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1606 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1608 draw_pixel( x+i, y+j );
1613 static void draw_line( int x1, int y1, int x2, int y2 )
1615 x1 = x1<<1;
1616 y1 = y1<<1;
1617 x2 = x2<<1;
1618 y2 = y2<<1;
1619 int w = x1 - x2;
1620 int h = y1 - y2;
1622 int x, y;
1624 if( w == 0 && h == 0 )
1626 draw_pixel( x1>>1, y1>>1 );
1627 return;
1630 if( w < 0 ) w *= -1;
1631 if( h < 0 ) h *= -1;
1633 if( w > h )
1635 if( x1 > x2 )
1637 x = x2;
1638 y = y2;
1639 x2 = x1;
1640 y2 = y1;
1641 x1 = x;
1642 y1 = y;
1644 w = x1 - x2;
1645 h = y1 - y2;
1646 while( x1 <= x2 )
1648 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
1649 x1+=2;
1650 y1 = y2 - ( x2 - x1 ) * h / w;
1653 else /* h > w */
1655 if( y1 > y2 )
1657 x = x2;
1658 y = y2;
1659 x2 = x1;
1660 y2 = y1;
1661 x1 = x;
1662 y1 = y;
1664 w = x1 - x2;
1665 h = y1 - y2;
1666 while( y1 <= y2 )
1668 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
1669 y1+=2;
1670 x1 = x2 - ( y2 - y1 ) * w / h;
1675 static void draw_curve( int x1, int y1, int x2, int y2,
1676 int xa, int ya, int xb, int yb )
1678 int i = 0;
1679 short xl1, yl1;
1680 short xl2, yl2;
1681 short xl3, yl3;
1682 short xl4, yl4;
1683 short xr1, yr1;
1684 short xr2, yr2;
1685 short xr3, yr3;
1686 short xr4, yr4;
1687 short depth;
1688 short xh, yh;
1690 if( x1 == x2 && y1 == y2 )
1692 draw_pixel( x1, y1 );
1693 return;
1696 // if( preview )
1698 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1699 if( xa == -1 || ya == -1 )
1701 rb->lcd_drawline( x1, y1, xb, yb );
1702 rb->lcd_drawline( x2, y2, xb, yb );
1704 else
1706 rb->lcd_drawline( x1, y1, xa, ya );
1707 rb->lcd_drawline( x2, y2, xb, yb );
1709 rb->lcd_set_drawmode(DRMODE_SOLID);
1712 if( xa == -1 || ya == -1 )
1713 /* We only have 3 of the points
1714 * This will currently only be used in preview mode */
1716 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1717 buffer.bezier[i].x1 = a1; \
1718 buffer.bezier[i].y1 = b1; \
1719 buffer.bezier[i].x2 = a2; \
1720 buffer.bezier[i].y2 = b2; \
1721 buffer.bezier[i].x3 = a3; \
1722 buffer.bezier[i].y3 = b3; \
1723 buffer.bezier[i].depth = d; \
1724 i++;
1725 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1726 i--; \
1727 a1 = buffer.bezier[i].x1; \
1728 b1 = buffer.bezier[i].y1; \
1729 a2 = buffer.bezier[i].x2; \
1730 b2 = buffer.bezier[i].y2; \
1731 a3 = buffer.bezier[i].x3; \
1732 b3 = buffer.bezier[i].y3; \
1733 d = buffer.bezier[i].depth;
1734 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1735 while( i )
1737 /* de Casteljau's algorithm (see wikipedia) */
1738 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1739 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1741 xl2 = ( xl1 + xb )>>1;
1742 yl2 = ( yl1 + yb )>>1;
1743 xr2 = ( xb + xr3 )>>1;
1744 yr2 = ( yb + yr3 )>>1;
1745 xr1 = ( xl2 + xr2 )>>1;
1746 yr1 = ( yl2 + yr2 )>>1;
1747 xl3 = xr1;
1748 yl3 = yr1;
1749 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1750 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1752 else
1754 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1755 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1758 #undef PUSH
1759 #undef POP
1761 else /* We have the 4 points */
1763 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1764 buffer.bezier[i].x1 = a1; \
1765 buffer.bezier[i].y1 = b1; \
1766 buffer.bezier[i].x2 = a2; \
1767 buffer.bezier[i].y2 = b2; \
1768 buffer.bezier[i].x3 = a3; \
1769 buffer.bezier[i].y3 = b3; \
1770 buffer.bezier[i].x4 = a4; \
1771 buffer.bezier[i].y4 = b4; \
1772 buffer.bezier[i].depth = d; \
1773 i++;
1774 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1775 i--; \
1776 a1 = buffer.bezier[i].x1; \
1777 b1 = buffer.bezier[i].y1; \
1778 a2 = buffer.bezier[i].x2; \
1779 b2 = buffer.bezier[i].y2; \
1780 a3 = buffer.bezier[i].x3; \
1781 b3 = buffer.bezier[i].y3; \
1782 a4 = buffer.bezier[i].x4; \
1783 b4 = buffer.bezier[i].y4; \
1784 d = buffer.bezier[i].depth;
1786 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1787 while( i )
1789 /* de Casteljau's algorithm (see wikipedia) */
1790 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1791 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1793 xl2 = ( xl1 + xa )>>1;
1794 yl2 = ( yl1 + ya )>>1;
1795 xh = ( xa + xb )>>1;
1796 yh = ( ya + yb )>>1;
1797 xr3 = ( xb + xr4 )>>1;
1798 yr3 = ( yb + yr4 )>>1;
1799 xl3 = ( xl2 + xh )>>1;
1800 yl3 = ( yl2 + yh )>>1;
1801 xr2 = ( xr3 + xh )>>1;
1802 yr2 = ( yr3 + yh )>>1;
1803 xl4 = ( xl3 + xr2 )>>1;
1804 yl4 = ( yl3 + yr2 )>>1;
1805 xr1 = xl4;
1806 yr1 = yl4;
1807 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1808 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1810 else
1812 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1813 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1816 #undef PUSH
1817 #undef POP
1821 static void draw_rect( int x1, int y1, int x2, int y2 )
1823 draw_line( x1, y1, x1, y2 );
1824 draw_line( x1, y1, x2, y1 );
1825 draw_line( x1, y2, x2, y2 );
1826 draw_line( x2, y1, x2, y2 );
1829 static void togglebg( void )
1831 if( isbg )
1833 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1835 else
1837 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1839 isbg = !isbg;
1842 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1844 /* GRUIK */
1845 int x = x1;
1846 togglebg();
1847 if( x < x2 )
1849 do {
1850 draw_line( x, y1, x, y2 );
1851 } while( ++x <= x2 );
1853 else
1855 do {
1856 draw_line( x, y1, x, y2 );
1857 } while( --x >= x2 );
1859 togglebg();
1860 draw_rect( x1, y1, x2, y2 );
1863 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1865 /* TODO: simplify :) */
1866 int cx = (x1+x2)>>1;
1867 int cy = (y1+y2)>>1;
1869 int rx = (x1-x2)>>1;
1870 int ry = (y1-y2)>>1;
1871 if( rx < 0 ) rx *= -1;
1872 if( ry < 0 ) ry *= -1;
1874 if( rx == 0 || ry == 0 )
1876 draw_line( x1, y1, x2, y2 );
1877 return;
1880 int x,y;
1881 int dst, old_dst;
1883 for( x = 0; x < rx; x++ )
1885 y = 0;
1886 dst = -0xfff;
1887 do {
1888 old_dst = dst;
1889 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1890 y++;
1891 } while( dst < 0 );
1892 if( -old_dst < dst ) y--;
1893 if( full )
1895 draw_line( cx+x, cy, cx+x, cy+y );
1896 draw_line( cx+x, cy, cx+x, cy-y );
1897 draw_line( cx-x, cy, cx-x, cy+y );
1898 draw_line( cx-x, cy, cx-x, cy-y );
1900 else
1902 draw_pixel( cx+x, cy+y );
1903 draw_pixel( cx+x, cy-y );
1904 draw_pixel( cx-x, cy+y );
1905 draw_pixel( cx-x, cy-y );
1908 for( y = 0; y < ry; y++ )
1910 x = 0;
1911 dst = -0xfff;
1912 do {
1913 old_dst = dst;
1914 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1915 x++;
1916 } while( dst < 0 );
1917 if( -old_dst < dst ) x--;
1918 if( full )
1920 draw_line( cx+x, cy, cx+x, cy+y );
1921 draw_line( cx+x, cy, cx+x, cy-y );
1922 draw_line( cx-x, cy, cx-x, cy+y );
1923 draw_line( cx-x, cy, cx-x, cy-y );
1925 else
1927 draw_pixel( cx+x, cy+y );
1928 draw_pixel( cx+x, cy-y );
1929 draw_pixel( cx-x, cy+y );
1930 draw_pixel( cx-x, cy-y );
1935 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1937 draw_oval( x1, y1, x2, y2, false );
1940 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1942 togglebg();
1943 draw_oval( x1, y1, x2, y2, true );
1944 togglebg();
1945 draw_oval( x1, y1, x2, y2, false );
1948 static void draw_fill( int x0, int y0 )
1950 #define PUSH( a, b ) \
1951 draw_pixel( (int)a, (int)b ); \
1952 buffer.coord[i].x = a; \
1953 buffer.coord[i].y = b; \
1954 i++;
1955 #define POP( a, b ) \
1956 i--; \
1957 a = buffer.coord[i].x; \
1958 b = buffer.coord[i].y;
1960 unsigned int i=0;
1961 short x = x0;
1962 short y = y0;
1963 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1965 if( prev_color == rp_colors[ drawcolor ] ) return;
1967 PUSH( x, y );
1969 while( i != 0 )
1971 POP( x, y );
1972 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1974 PUSH( x-1, y );
1976 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1978 PUSH( x+1, y );
1980 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1982 PUSH( x, y-1 );
1984 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1986 PUSH( x, y+1 );
1989 #undef PUSH
1990 #undef POP
1994 /* For preview purposes only */
1995 static void line_gradient( int x1, int y1, int x2, int y2 )
1997 int r1, g1, b1;
1998 int r2, g2, b2;
1999 int h1, s1, v1, h2, s2, v2, r, g, b;
2000 int w, h, x, y;
2002 bool a = false;
2004 x1 <<= 1;
2005 y1 <<= 1;
2006 x2 <<= 1;
2007 y2 <<= 1;
2009 w = x1 - x2;
2010 h = y1 - y2;
2012 if( w == 0 && h == 0 )
2014 draw_pixel( x1>>1, y1>>1 );
2015 return;
2018 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2019 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2020 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2021 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2022 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2023 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2025 if( w < 0 )
2027 w *= -1;
2028 a = true;
2030 if( h < 0 )
2032 h *= -1;
2033 a = !a;
2035 if( a )
2037 r = r1;
2038 r1 = r2;
2039 r2 = r;
2040 g = g1;
2041 g1 = g2;
2042 g2 = g;
2043 b = b1;
2044 b1 = b2;
2045 b2 = b;
2048 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2049 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2051 if( w > h )
2053 if( x1 > x2 )
2055 x = x2;
2056 y = y2;
2057 x2 = x1;
2058 y2 = y1;
2059 x1 = x;
2060 y1 = y;
2062 w = x1 - x2;
2063 h = y1 - y2;
2064 while( x1 <= x2 )
2066 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2067 s1+((s2-s1)*(x1-x2))/w,
2068 v1+((v2-v1)*(x1-x2))/w,
2069 &r, &g, &b );
2070 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2071 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2072 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2073 x1+=2;
2074 y1 = y2 - ( x2 - x1 ) * h / w;
2077 else /* h > w */
2079 if( y1 > y2 )
2081 x = x2;
2082 y = y2;
2083 x2 = x1;
2084 y2 = y1;
2085 x1 = x;
2086 y1 = y;
2088 w = x1 - x2;
2089 h = y1 - y2;
2090 while( y1 <= y2 )
2092 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2093 s1+((s2-s1)*(y1-y2))/h,
2094 v1+((v2-v1)*(y1-y2))/h,
2095 &r, &g, &b );
2096 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2097 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2098 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2099 y1+=2;
2100 x1 = x2 - ( y2 - y1 ) * w / h;
2103 if( a )
2105 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2107 else
2109 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2113 static void linear_gradient( int x1, int y1, int x2, int y2 )
2115 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2116 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2117 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2118 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2119 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2120 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2122 int h1, s1, v1, h2, s2, v2, r, g, b;
2124 /* radius^2 */
2125 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2126 int dist2, i=0;
2128 /* We only propagate the gradient to neighboring pixels with the same
2129 * color as ( x1, y1 ) */
2130 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2132 int x = x1;
2133 int y = y1;
2135 if( radius2 == 0 ) return;
2136 if( preview )
2138 line_gradient( x1, y1, x2, y2 );
2141 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2142 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2144 #define PUSH( x0, y0 ) \
2145 buffer.coord[i].x = (short)(x0); \
2146 buffer.coord[i].y = (short)(y0); \
2147 i++;
2148 #define POP( a, b ) \
2149 i--; \
2150 a = (int)buffer.coord[i].x; \
2151 b = (int)buffer.coord[i].y;
2153 PUSH( x, y );
2155 while( i != 0 )
2157 POP( x, y );
2159 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2160 if( dist2 <= 0 )
2162 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2164 else if( dist2 < radius2 )
2166 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2167 s1+((s2-s1)*dist2)/radius2,
2168 v1+((v2-v1)*dist2)/radius2,
2169 &r, &g, &b );
2170 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2172 else
2174 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2176 if( rp_colors[ drawcolor ] == prev_color )
2178 if( rp_colors[ drawcolor ])
2179 rp_colors[ drawcolor ]--; /* GRUIK */
2180 else
2181 rp_colors[ drawcolor ]++; /* GRUIK */
2183 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2184 draw_pixel( x, y );
2186 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2188 PUSH( x-1, y );
2190 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2192 PUSH( x+1, y );
2194 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2196 PUSH( x, y-1 );
2198 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2200 PUSH( x, y+1 );
2203 #undef PUSH
2204 #undef POP
2206 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2209 static void radial_gradient( int x1, int y1, int x2, int y2 )
2211 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2212 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2213 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2214 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2215 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2216 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2218 int h1, s1, v1, h2, s2, v2, r, g, b;
2220 /* radius^2 */
2221 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2222 int dist2, i=0;
2224 /* We only propagate the gradient to neighboring pixels with the same
2225 * color as ( x1, y1 ) */
2226 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2228 int x = x1;
2229 int y = y1;
2231 if( radius2 == 0 ) return;
2232 if( preview )
2234 line_gradient( x1, y1, x2, y2 );
2237 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2238 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2240 #define PUSH( x0, y0 ) \
2241 buffer.coord[i].x = (short)(x0); \
2242 buffer.coord[i].y = (short)(y0); \
2243 i++;
2244 #define POP( a, b ) \
2245 i--; \
2246 a = (int)buffer.coord[i].x; \
2247 b = (int)buffer.coord[i].y;
2249 PUSH( x, y );
2251 while( i != 0 )
2253 POP( x, y );
2255 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2257 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2258 s1+((s2-s1)*dist2)/radius2,
2259 v1+((v2-v1)*dist2)/radius2,
2260 &r, &g, &b );
2261 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2263 else
2265 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2267 if( rp_colors[ drawcolor ] == prev_color )
2269 if( rp_colors[ drawcolor ])
2270 rp_colors[ drawcolor ]--; /* GRUIK */
2271 else
2272 rp_colors[ drawcolor ]++; /* GRUIK */
2274 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2275 draw_pixel( x, y );
2277 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2279 PUSH( x-1, y );
2281 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2283 PUSH( x+1, y );
2285 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2287 PUSH( x, y-1 );
2289 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2291 PUSH( x, y+1 );
2294 #undef PUSH
2295 #undef POP
2297 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2300 static void draw_toolbars(bool update)
2302 int i;
2303 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2304 rb->lcd_set_background( COLOR_LIGHTGRAY );
2305 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2306 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2307 rb->lcd_set_foreground( COLOR_BLACK );
2308 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2310 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2311 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2312 TB_SC_SIZE, TB_SC_SIZE );
2313 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2314 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2315 TB_SC_SIZE, TB_SC_SIZE );
2316 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2317 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2318 TB_SC_SIZE, TB_SC_SIZE );
2319 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2320 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2321 TB_SC_SIZE, TB_SC_SIZE );
2323 for( i=0; i<18; i++ )
2325 rb->lcd_set_foreground( rp_colors[i] );
2326 rb->lcd_fillrect(
2327 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2328 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2329 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2330 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2331 rb->lcd_drawrect(
2332 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2333 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2334 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2337 #define SEPARATOR( x, y ) \
2338 rb->lcd_set_foreground( COLOR_WHITE ); \
2339 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2340 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2341 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2342 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2344 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2345 TB_TL_WIDTH, TB_TL_HEIGHT );
2346 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2347 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2348 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2349 TB_TL_SIZE, TB_TL_SIZE );
2351 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2353 rb->lcd_setfont( FONT_SYSFIXED );
2354 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2355 rb->lcd_setfont( FONT_UI );
2356 #undef TOP
2358 if( update ) rb->lcd_update();
2361 static void toolbar( void )
2363 int button, i, j;
2364 restore_screen();
2365 draw_toolbars( false );
2366 y = LCD_HEIGHT-TB_HEIGHT/2;
2367 inv_cursor( true );
2368 while( 1 )
2370 switch( button = rb->button_get( true ) )
2372 case ROCKPAINT_DRAW:
2373 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2374 if( y >= TOP + TB_SC_FG_TOP
2375 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2376 && x >= TB_SC_FG_LEFT
2377 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2379 /* click on the foreground color */
2380 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2382 else if( y >= TOP + TB_SC_BG_TOP
2383 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2384 && x >= TB_SC_BG_LEFT
2385 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2387 /* click on the background color */
2388 i = drawcolor;
2389 drawcolor = bgdrawcolor;
2390 bgdrawcolor = i;
2392 else if( y >= TOP + TB_PL_TOP
2393 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2394 && x >= TB_PL_LEFT
2395 && x < TB_PL_LEFT + TB_PL_WIDTH )
2397 /* click on the palette */
2398 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2399 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2400 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2401 break;
2402 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2403 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2404 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2406 else if( y >= TOP+TB_TL_TOP
2407 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2408 && x >= TB_TL_LEFT
2409 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2411 /* click on the tools */
2412 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2413 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2414 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2415 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2416 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2417 tool = i*2+j;
2418 prev_x = -1;
2419 prev_y = -1;
2420 prev_x2 = -1;
2421 prev_y2 = -1;
2422 prev_x3 = -1;
2423 prev_y3 = -1;
2424 preview = false;
2426 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2428 /* menu button */
2429 goto_menu();
2431 #undef TOP
2432 restore_screen();
2433 draw_toolbars( false );
2434 inv_cursor( true );
2435 break;
2437 case ROCKPAINT_LEFT:
2438 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2439 inv_cursor(false);
2440 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2441 if (x<0) x=COLS-1;
2442 inv_cursor(true);
2443 break;
2445 case ROCKPAINT_RIGHT:
2446 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2447 inv_cursor(false);
2448 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2449 if (x>=COLS) x=0;
2450 inv_cursor(true);
2451 break;
2453 case ROCKPAINT_UP:
2454 case ROCKPAINT_UP | BUTTON_REPEAT:
2455 inv_cursor(false);
2456 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2457 if (y<LCD_HEIGHT-TB_HEIGHT)
2459 return;
2461 inv_cursor(true);
2462 break;
2464 case ROCKPAINT_DOWN:
2465 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2466 inv_cursor(false);
2467 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2468 if (y>=LCD_HEIGHT)
2470 y = 0;
2471 return;
2473 inv_cursor(true);
2474 break;
2476 case ROCKPAINT_TOOLBAR:
2477 case ROCKPAINT_TOOLBAR2:
2478 return;
2480 if( quit ) return;
2484 static void inv_cursor(bool update)
2486 rb->lcd_set_foreground(COLOR_BLACK);
2487 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2488 /* cross painting */
2489 rb->lcd_hline(x-4,x-1,y);
2490 rb->lcd_hline(x+1,x+4,y);
2491 rb->lcd_vline(x,y-4,y-1);
2492 rb->lcd_vline(x,y+1,y+4);
2493 rb->lcd_set_foreground(rp_colors[drawcolor]);
2494 rb->lcd_set_drawmode(DRMODE_SOLID);
2496 if( update ) rb->lcd_update();
2499 static void restore_screen(void)
2501 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2504 static void clear_drawing(void)
2506 init_buffer();
2507 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2508 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2509 rb->lcd_update();
2512 static void goto_menu(void)
2514 int multi;
2516 while( 1 )
2518 switch( menu_display( main_menu, 1 ) )
2520 case MAIN_MENU_NEW:
2521 clear_drawing();
2522 return;
2524 case MAIN_MENU_LOAD:
2525 if( browse( filename, MAX_PATH, "/" ) )
2527 if( load_bitmap( filename ) <= 0 )
2529 rb->splash( 1*HZ, "Error while loading %s",
2530 filename );
2532 else
2534 rb->splash( 1*HZ, "Image loaded (%s)", filename );
2535 restore_screen();
2536 inv_cursor(true);
2537 return;
2540 break;
2542 case MAIN_MENU_SAVE:
2543 if (!filename[0])
2544 rb->strcpy(filename,"/");
2545 if( !rb->kbd_input( filename, MAX_PATH ) )
2547 if(rb->strlen(filename) <= 4 ||
2548 rb->strcasecmp(&filename[rb->strlen(filename)-4], ".bmp"))
2549 rb->strcat(filename, ".bmp");
2550 save_bitmap( filename );
2551 rb->splash( 1*HZ, "File saved (%s)", filename );
2553 break;
2555 case MAIN_MENU_BRUSH_SIZE:
2556 multi = menu_display( size_menu, bsize );
2557 if( multi != - 1 )
2558 bsize = multi;
2559 break;
2561 case MAIN_MENU_BRUSH_SPEED:
2562 multi = menu_display( speed_menu, bspeed );
2563 if( multi != -1 )
2564 bspeed = multi;
2565 break;
2567 case MAIN_MENU_COLOR:
2568 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2569 break;
2571 case MAIN_MENU_GRID_SIZE:
2572 multi = menu_display( gridsize_menu, gridsize );
2573 if( multi != - 1 )
2574 gridsize = multi;
2575 break;
2577 case MAIN_MENU_EXIT:
2578 quit=true;
2579 return;
2581 case MAIN_MENU_RESUME:
2582 case MENU_ESC:
2583 return;
2584 }/* end switch */
2585 }/* end while */
2588 static void reset_tool( void )
2590 prev_x = -1;
2591 prev_y = -1;
2592 prev_x2 = -1;
2593 prev_y2 = -1;
2594 prev_x3 = -1;
2595 prev_y3 = -1;
2596 tool_mode = -1;
2597 preview = false;
2600 static bool rockpaint_loop( void )
2602 int button=0,i,j;
2603 int accelaration;
2605 while (!quit) {
2606 button = rb->button_get(true);
2608 if( tool == Brush && prev_x != -1 )
2610 accelaration = 1;
2612 else if( button & BUTTON_REPEAT )
2614 accelaration = 4;
2616 else
2618 accelaration = 1;
2621 switch(button)
2623 case ROCKPAINT_QUIT:
2624 rb->lcd_set_drawmode(DRMODE_SOLID);
2625 return PLUGIN_OK;
2627 case ROCKPAINT_MENU:
2628 inv_cursor(false);
2629 goto_menu();
2630 inv_cursor(true);
2631 break;
2633 case ROCKPAINT_DRAW:
2634 inv_cursor(false);
2635 switch( tool )
2637 case Brush:
2638 if( prev_x == -1 ) prev_x = 1;
2639 else prev_x = -1;
2640 break;
2642 case SelectRectangle:
2643 case Line:
2644 case Curve:
2645 case Rectangle:
2646 case RectangleFull:
2647 case Oval:
2648 case OvalFull:
2649 case LinearGradient:
2650 case RadialGradient:
2651 /* Curve uses 4 points, others use 2 */
2652 if( prev_x == -1 || prev_y == -1 )
2654 prev_x = x;
2655 prev_y = y;
2656 preview = true;
2658 else if( tool == Curve
2659 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2661 prev_x2 = x;
2662 prev_y2 = y;
2664 else if( tool == SelectRectangle
2665 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2667 tool_mode = menu_display( select_menu,
2668 SELECT_MENU_CUT );
2669 switch( tool_mode )
2671 case SELECT_MENU_CUT:
2672 case SELECT_MENU_COPY:
2673 prev_x2 = x;
2674 prev_y2 = y;
2675 copy_to_clipboard();
2676 if( prev_x < x ) x = prev_x;
2677 if( prev_y < y ) y = prev_y;
2678 break;
2680 case SELECT_MENU_INVERT:
2681 draw_invert( prev_x, prev_y, x, y );
2682 reset_tool();
2683 break;
2685 case SELECT_MENU_HFLIP:
2686 draw_hflip( prev_x, prev_y, x, y );
2687 reset_tool();
2688 break;
2690 case SELECT_MENU_VFLIP:
2691 draw_vflip( prev_x, prev_y, x, y );
2692 reset_tool();
2693 break;
2695 case SELECT_MENU_ROTATE90:
2696 break;
2697 case SELECT_MENU_ROTATE180:
2698 draw_hflip( prev_x, prev_y, x, y );
2699 draw_vflip( prev_x, prev_y, x, y );
2700 reset_tool();
2701 break;
2702 case SELECT_MENU_ROTATE270:
2703 break;
2705 case SELECT_MENU_CANCEL:
2706 reset_tool();
2707 break;
2709 case MENU_ESC:
2710 break;
2713 else if( tool == Curve
2714 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2716 prev_x3 = x;
2717 prev_y3 = y;
2719 else
2721 preview = false;
2722 switch( tool )
2724 case SelectRectangle:
2725 draw_paste_rectangle( prev_x, prev_y,
2726 prev_x2, prev_y2,
2727 x, y, tool_mode );
2728 break;
2729 case Line:
2730 draw_line( prev_x, prev_y, x, y );
2731 break;
2732 case Curve:
2733 draw_curve( prev_x, prev_y,
2734 prev_x2, prev_y2,
2735 prev_x3, prev_y3,
2736 x, y );
2737 break;
2738 case Rectangle:
2739 draw_rect( prev_x, prev_y, x, y );
2740 break;
2741 case RectangleFull:
2742 draw_rect_full( prev_x, prev_y, x, y );
2743 break;
2744 case Oval:
2745 draw_oval_empty( prev_x, prev_y, x, y );
2746 break;
2747 case OvalFull:
2748 draw_oval_full( prev_x, prev_y, x, y );
2749 break;
2750 case LinearGradient:
2751 linear_gradient( prev_x, prev_y, x, y );
2752 break;
2753 case RadialGradient:
2754 radial_gradient( prev_x, prev_y, x, y );
2755 break;
2756 default:
2757 break;
2759 reset_tool();
2761 break;
2763 case Fill:
2764 draw_fill( x, y );
2765 break;
2767 case ColorPicker:
2768 color_picker( x, y );
2769 break;
2771 case Text:
2772 draw_text( x, y );
2773 break;
2775 default:
2776 break;
2778 inv_cursor(true);
2779 break;
2781 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2782 if( tool == Curve )
2784 /* 3 point bezier curve */
2785 preview = false;
2786 draw_curve( prev_x, prev_y,
2787 prev_x2, prev_y2,
2788 -1, -1,
2789 x, y );
2790 reset_tool();
2791 restore_screen();
2792 inv_cursor( true );
2794 break;
2796 case ROCKPAINT_TOOLBAR:
2797 i = x; j = y;
2798 x = 10;
2799 toolbar();
2800 x = i; y = j;
2801 restore_screen();
2802 inv_cursor(true);
2803 break;
2805 case ROCKPAINT_TOOLBAR2:
2806 i = x; j = y;
2807 x = 110;
2808 toolbar();
2809 x = i; y = j;
2810 restore_screen();
2811 inv_cursor(true);
2812 break;
2814 case ROCKPAINT_LEFT:
2815 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2816 inv_cursor(false);
2817 x-=bspeed * accelaration;
2818 if (x<0) x=COLS-1;
2819 inv_cursor(true);
2820 break;
2822 case ROCKPAINT_RIGHT:
2823 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2824 inv_cursor(false);
2825 x+=bspeed * accelaration;
2826 if (x>=COLS) x=0;
2827 inv_cursor(true);
2828 break;
2830 case ROCKPAINT_UP:
2831 case ROCKPAINT_UP | BUTTON_REPEAT:
2832 inv_cursor(false);
2833 y-=bspeed * accelaration;
2834 if (y<0) y=ROWS-1;
2835 inv_cursor(true);
2836 break;
2838 case ROCKPAINT_DOWN:
2839 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2840 inv_cursor(false);
2841 y+=bspeed * accelaration;
2842 if (y>=ROWS)
2844 toolbar();
2845 restore_screen();
2847 inv_cursor(true);
2848 break;
2850 default:
2851 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2852 return PLUGIN_USB_CONNECTED;
2853 break;
2855 if( tool == Brush && prev_x == 1 )
2857 inv_cursor(false);
2858 draw_brush( x, y );
2859 inv_cursor(true);
2861 if( preview || tool == ColorPicker )
2862 /* always preview color picker */
2864 restore_screen();
2865 switch( tool )
2867 case SelectRectangle:
2868 if( prev_x2 == -1 || prev_y2 == -1 )
2870 /* we are defining the selection */
2871 draw_select_rectangle( prev_x, prev_y, x, y );
2873 else
2875 /* we are pasting the selected data */
2876 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2877 prev_y2, x, y, tool_mode );
2878 prev_x3 = prev_x2-prev_x;
2879 if( prev_x3 < 0 ) prev_x3 *= -1;
2880 prev_y3 = prev_y2-prev_y;
2881 if( prev_y3 < 0 ) prev_y3 *= -1;
2882 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2883 prev_x3 = -1;
2884 prev_y3 = -1;
2886 break;
2888 case Brush:
2889 break;
2891 case Line:
2892 draw_line( prev_x, prev_y, x, y );
2893 break;
2895 case Curve:
2896 if( prev_x2 == -1 || prev_y2 == -1 )
2898 draw_line( prev_x, prev_y, x, y );
2900 else
2902 draw_curve( prev_x, prev_y,
2903 prev_x2, prev_y2,
2904 prev_x3, prev_y3,
2905 x, y );
2907 break;
2909 case Rectangle:
2910 draw_rect( prev_x, prev_y, x, y );
2911 break;
2913 case RectangleFull:
2914 draw_rect_full( prev_x, prev_y, x, y );
2915 break;
2917 case Oval:
2918 draw_oval_empty( prev_x, prev_y, x, y );
2919 break;
2921 case OvalFull:
2922 draw_oval_full( prev_x, prev_y, x, y );
2923 break;
2925 case Fill:
2926 break;
2928 case ColorPicker:
2929 preview = true;
2930 color_picker( x, y );
2931 preview = false;
2932 break;
2934 case LinearGradient:
2935 line_gradient( prev_x, prev_y, x, y );
2936 break;
2938 case RadialGradient:
2939 line_gradient( prev_x, prev_y, x, y );
2940 break;
2942 case Text:
2943 default:
2944 break;
2946 inv_cursor( true );
2948 if( gridsize > 0 )
2950 show_grid( true );
2951 show_grid( false );
2955 return PLUGIN_OK;
2958 static int load_bitmap( const char *file )
2960 struct bitmap bm;
2961 bool ret;
2962 int l;
2964 bm.data = (char*)save_buffer;
2965 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2966 FORMAT_NATIVE );
2968 if((bm.width > COLS ) || ( bm.height > ROWS ))
2969 return -1;
2971 for( l = bm.height-1; l > 0; l-- )
2973 rb->memmove( save_buffer+l*COLS, save_buffer+l*bm.width,
2974 sizeof( fb_data )*bm.width );
2976 for( l = 0; l < bm.height; l++ )
2978 rb->memset( save_buffer+l*COLS+bm.width, rp_colors[ bgdrawcolor ],
2979 sizeof( fb_data )*(COLS-bm.width) );
2981 rb->memset( save_buffer+COLS*bm.height, rp_colors[ bgdrawcolor ],
2982 sizeof( fb_data )*COLS*(ROWS-bm.height) );
2984 return ret;
2987 static int save_bitmap( char *file )
2989 struct bitmap bm;
2990 bm.data = (char*)save_buffer;
2991 bm.height = ROWS;
2992 bm.width = COLS;
2993 bm.format = FORMAT_NATIVE;
2994 return save_bmp_file( file, &bm, rb );
2997 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
2999 /** must have stuff **/
3000 rb = api;
3002 rb->lcd_set_foreground(COLOR_WHITE);
3003 rb->lcd_set_backdrop(NULL);
3004 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
3005 rb->splash( HZ/2, "Rock Paint");
3007 rb->lcd_clear_display();
3009 filename[0] = '\0';
3011 if( parameter )
3013 if( load_bitmap( parameter ) <= 0 )
3015 rb->splash( 1*HZ, "File Open Error");
3016 clear_drawing();
3018 else
3020 rb->splash( 1*HZ, "Image loaded (%s)", (char *)parameter );
3021 restore_screen();
3022 rb->strcpy( filename, parameter );
3025 else
3027 clear_drawing();
3029 inv_cursor(true);
3031 return rockpaint_loop();