FS#9281 Rename of splash functions.
[kugel-rb.git] / apps / plugins / rockpaint.c
blob6436d9baa5df92f77c5dbf2b4f53a70dc8d352b7
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 /* This is an implementation of Bresenham's line algorithm.
1614 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1616 static void draw_line( int x1, int y1, int x2, int y2 )
1618 int x = x1;
1619 int y = y1;
1620 int deltax = x2 - x1;
1621 int deltay = y2 - y1;
1622 int i;
1624 int xerr = abs(deltax);
1625 int yerr = abs(deltay);
1626 int xstep = deltax > 0 ? 1 : -1;
1627 int ystep = deltay > 0 ? 1 : -1;
1628 int err;
1630 if (yerr > xerr)
1632 /* more vertical */
1633 err = yerr;
1634 xerr <<= 1;
1635 yerr <<= 1;
1637 /* to leave off the last pixel of the line, leave off the "+ 1" */
1638 for (i = abs(deltay) + 1; i; --i)
1640 draw_pixel(x, y);
1641 y += ystep;
1642 err -= xerr;
1643 if (err < 0) {
1644 x += xstep;
1645 err += yerr;
1649 else
1651 /* more horizontal */
1652 err = xerr;
1653 xerr <<= 1;
1654 yerr <<= 1;
1656 for (i = abs(deltax) + 1; i; --i)
1658 draw_pixel(x, y);
1659 x += xstep;
1660 err -= yerr;
1661 if (err < 0) {
1662 y += ystep;
1663 err += xerr;
1669 static void draw_curve( int x1, int y1, int x2, int y2,
1670 int xa, int ya, int xb, int yb )
1672 int i = 0;
1673 short xl1, yl1;
1674 short xl2, yl2;
1675 short xl3, yl3;
1676 short xl4, yl4;
1677 short xr1, yr1;
1678 short xr2, yr2;
1679 short xr3, yr3;
1680 short xr4, yr4;
1681 short depth;
1682 short xh, yh;
1684 if( x1 == x2 && y1 == y2 )
1686 draw_pixel( x1, y1 );
1687 return;
1690 // if( preview )
1692 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1693 if( xa == -1 || ya == -1 )
1695 rb->lcd_drawline( x1, y1, xb, yb );
1696 rb->lcd_drawline( x2, y2, xb, yb );
1698 else
1700 rb->lcd_drawline( x1, y1, xa, ya );
1701 rb->lcd_drawline( x2, y2, xb, yb );
1703 rb->lcd_set_drawmode(DRMODE_SOLID);
1706 if( xa == -1 || ya == -1 )
1707 /* We only have 3 of the points
1708 * This will currently only be used in preview mode */
1710 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1711 buffer.bezier[i].x1 = a1; \
1712 buffer.bezier[i].y1 = b1; \
1713 buffer.bezier[i].x2 = a2; \
1714 buffer.bezier[i].y2 = b2; \
1715 buffer.bezier[i].x3 = a3; \
1716 buffer.bezier[i].y3 = b3; \
1717 buffer.bezier[i].depth = d; \
1718 i++;
1719 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1720 i--; \
1721 a1 = buffer.bezier[i].x1; \
1722 b1 = buffer.bezier[i].y1; \
1723 a2 = buffer.bezier[i].x2; \
1724 b2 = buffer.bezier[i].y2; \
1725 a3 = buffer.bezier[i].x3; \
1726 b3 = buffer.bezier[i].y3; \
1727 d = buffer.bezier[i].depth;
1728 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1729 while( i )
1731 /* de Casteljau's algorithm (see wikipedia) */
1732 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1733 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1735 xl2 = ( xl1 + xb )>>1;
1736 yl2 = ( yl1 + yb )>>1;
1737 xr2 = ( xb + xr3 )>>1;
1738 yr2 = ( yb + yr3 )>>1;
1739 xr1 = ( xl2 + xr2 )>>1;
1740 yr1 = ( yl2 + yr2 )>>1;
1741 xl3 = xr1;
1742 yl3 = yr1;
1743 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1744 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1746 else
1748 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1749 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1752 #undef PUSH
1753 #undef POP
1755 else /* We have the 4 points */
1757 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1758 buffer.bezier[i].x1 = a1; \
1759 buffer.bezier[i].y1 = b1; \
1760 buffer.bezier[i].x2 = a2; \
1761 buffer.bezier[i].y2 = b2; \
1762 buffer.bezier[i].x3 = a3; \
1763 buffer.bezier[i].y3 = b3; \
1764 buffer.bezier[i].x4 = a4; \
1765 buffer.bezier[i].y4 = b4; \
1766 buffer.bezier[i].depth = d; \
1767 i++;
1768 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1769 i--; \
1770 a1 = buffer.bezier[i].x1; \
1771 b1 = buffer.bezier[i].y1; \
1772 a2 = buffer.bezier[i].x2; \
1773 b2 = buffer.bezier[i].y2; \
1774 a3 = buffer.bezier[i].x3; \
1775 b3 = buffer.bezier[i].y3; \
1776 a4 = buffer.bezier[i].x4; \
1777 b4 = buffer.bezier[i].y4; \
1778 d = buffer.bezier[i].depth;
1780 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1781 while( i )
1783 /* de Casteljau's algorithm (see wikipedia) */
1784 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1785 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1787 xl2 = ( xl1 + xa )>>1;
1788 yl2 = ( yl1 + ya )>>1;
1789 xh = ( xa + xb )>>1;
1790 yh = ( ya + yb )>>1;
1791 xr3 = ( xb + xr4 )>>1;
1792 yr3 = ( yb + yr4 )>>1;
1793 xl3 = ( xl2 + xh )>>1;
1794 yl3 = ( yl2 + yh )>>1;
1795 xr2 = ( xr3 + xh )>>1;
1796 yr2 = ( yr3 + yh )>>1;
1797 xl4 = ( xl3 + xr2 )>>1;
1798 yl4 = ( yl3 + yr2 )>>1;
1799 xr1 = xl4;
1800 yr1 = yl4;
1801 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1802 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1804 else
1806 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1807 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1810 #undef PUSH
1811 #undef POP
1815 static void draw_rect( int x1, int y1, int x2, int y2 )
1817 draw_line( x1, y1, x1, y2 );
1818 draw_line( x1, y1, x2, y1 );
1819 draw_line( x1, y2, x2, y2 );
1820 draw_line( x2, y1, x2, y2 );
1823 static void togglebg( void )
1825 if( isbg )
1827 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1829 else
1831 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1833 isbg = !isbg;
1836 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1838 /* GRUIK */
1839 int x = x1;
1840 togglebg();
1841 if( x < x2 )
1843 do {
1844 draw_line( x, y1, x, y2 );
1845 } while( ++x <= x2 );
1847 else
1849 do {
1850 draw_line( x, y1, x, y2 );
1851 } while( --x >= x2 );
1853 togglebg();
1854 draw_rect( x1, y1, x2, y2 );
1857 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1859 /* TODO: simplify :) */
1860 int cx = (x1+x2)>>1;
1861 int cy = (y1+y2)>>1;
1863 int rx = (x1-x2)>>1;
1864 int ry = (y1-y2)>>1;
1865 if( rx < 0 ) rx *= -1;
1866 if( ry < 0 ) ry *= -1;
1868 if( rx == 0 || ry == 0 )
1870 draw_line( x1, y1, x2, y2 );
1871 return;
1874 int x,y;
1875 int dst, old_dst;
1877 for( x = 0; x < rx; x++ )
1879 y = 0;
1880 dst = -0xfff;
1881 do {
1882 old_dst = dst;
1883 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1884 y++;
1885 } while( dst < 0 );
1886 if( -old_dst < dst ) y--;
1887 if( full )
1889 draw_line( cx+x, cy, cx+x, cy+y );
1890 draw_line( cx+x, cy, cx+x, cy-y );
1891 draw_line( cx-x, cy, cx-x, cy+y );
1892 draw_line( cx-x, cy, cx-x, cy-y );
1894 else
1896 draw_pixel( cx+x, cy+y );
1897 draw_pixel( cx+x, cy-y );
1898 draw_pixel( cx-x, cy+y );
1899 draw_pixel( cx-x, cy-y );
1902 for( y = 0; y < ry; y++ )
1904 x = 0;
1905 dst = -0xfff;
1906 do {
1907 old_dst = dst;
1908 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1909 x++;
1910 } while( dst < 0 );
1911 if( -old_dst < dst ) x--;
1912 if( full )
1914 draw_line( cx+x, cy, cx+x, cy+y );
1915 draw_line( cx+x, cy, cx+x, cy-y );
1916 draw_line( cx-x, cy, cx-x, cy+y );
1917 draw_line( cx-x, cy, cx-x, cy-y );
1919 else
1921 draw_pixel( cx+x, cy+y );
1922 draw_pixel( cx+x, cy-y );
1923 draw_pixel( cx-x, cy+y );
1924 draw_pixel( cx-x, cy-y );
1929 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1931 draw_oval( x1, y1, x2, y2, false );
1934 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1936 togglebg();
1937 draw_oval( x1, y1, x2, y2, true );
1938 togglebg();
1939 draw_oval( x1, y1, x2, y2, false );
1942 static void draw_fill( int x0, int y0 )
1944 #define PUSH( a, b ) \
1945 draw_pixel( (int)a, (int)b ); \
1946 buffer.coord[i].x = a; \
1947 buffer.coord[i].y = b; \
1948 i++;
1949 #define POP( a, b ) \
1950 i--; \
1951 a = buffer.coord[i].x; \
1952 b = buffer.coord[i].y;
1954 unsigned int i=0;
1955 short x = x0;
1956 short y = y0;
1957 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1959 if( prev_color == rp_colors[ drawcolor ] ) return;
1961 PUSH( x, y );
1963 while( i != 0 )
1965 POP( x, y );
1966 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1968 PUSH( x-1, y );
1970 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1972 PUSH( x+1, y );
1974 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1976 PUSH( x, y-1 );
1978 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1980 PUSH( x, y+1 );
1983 #undef PUSH
1984 #undef POP
1988 /* For preview purposes only */
1989 static void line_gradient( int x1, int y1, int x2, int y2 )
1991 int r1, g1, b1;
1992 int r2, g2, b2;
1993 int h1, s1, v1, h2, s2, v2, r, g, b;
1994 int w, h, x, y;
1996 bool a = false;
1998 x1 <<= 1;
1999 y1 <<= 1;
2000 x2 <<= 1;
2001 y2 <<= 1;
2003 w = x1 - x2;
2004 h = y1 - y2;
2006 if( w == 0 && h == 0 )
2008 draw_pixel( x1>>1, y1>>1 );
2009 return;
2012 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2013 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2014 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2015 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2016 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2017 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2019 if( w < 0 )
2021 w *= -1;
2022 a = true;
2024 if( h < 0 )
2026 h *= -1;
2027 a = !a;
2029 if( a )
2031 r = r1;
2032 r1 = r2;
2033 r2 = r;
2034 g = g1;
2035 g1 = g2;
2036 g2 = g;
2037 b = b1;
2038 b1 = b2;
2039 b2 = b;
2042 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2043 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2045 if( w > h )
2047 if( x1 > x2 )
2049 x = x2;
2050 y = y2;
2051 x2 = x1;
2052 y2 = y1;
2053 x1 = x;
2054 y1 = y;
2056 w = x1 - x2;
2057 h = y1 - y2;
2058 while( x1 <= x2 )
2060 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2061 s1+((s2-s1)*(x1-x2))/w,
2062 v1+((v2-v1)*(x1-x2))/w,
2063 &r, &g, &b );
2064 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2065 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2066 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2067 x1+=2;
2068 y1 = y2 - ( x2 - x1 ) * h / w;
2071 else /* h > w */
2073 if( y1 > y2 )
2075 x = x2;
2076 y = y2;
2077 x2 = x1;
2078 y2 = y1;
2079 x1 = x;
2080 y1 = y;
2082 w = x1 - x2;
2083 h = y1 - y2;
2084 while( y1 <= y2 )
2086 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2087 s1+((s2-s1)*(y1-y2))/h,
2088 v1+((v2-v1)*(y1-y2))/h,
2089 &r, &g, &b );
2090 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2091 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2092 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2093 y1+=2;
2094 x1 = x2 - ( y2 - y1 ) * w / h;
2097 if( a )
2099 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2101 else
2103 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2107 static void linear_gradient( int x1, int y1, int x2, int y2 )
2109 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2110 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2111 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2112 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2113 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2114 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2116 int h1, s1, v1, h2, s2, v2, r, g, b;
2118 /* radius^2 */
2119 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2120 int dist2, i=0;
2122 /* We only propagate the gradient to neighboring pixels with the same
2123 * color as ( x1, y1 ) */
2124 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2126 int x = x1;
2127 int y = y1;
2129 if( radius2 == 0 ) return;
2130 if( preview )
2132 line_gradient( x1, y1, x2, y2 );
2135 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2136 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2138 #define PUSH( x0, y0 ) \
2139 buffer.coord[i].x = (short)(x0); \
2140 buffer.coord[i].y = (short)(y0); \
2141 i++;
2142 #define POP( a, b ) \
2143 i--; \
2144 a = (int)buffer.coord[i].x; \
2145 b = (int)buffer.coord[i].y;
2147 PUSH( x, y );
2149 while( i != 0 )
2151 POP( x, y );
2153 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2154 if( dist2 <= 0 )
2156 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2158 else if( dist2 < radius2 )
2160 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2161 s1+((s2-s1)*dist2)/radius2,
2162 v1+((v2-v1)*dist2)/radius2,
2163 &r, &g, &b );
2164 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2166 else
2168 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2170 if( rp_colors[ drawcolor ] == prev_color )
2172 if( rp_colors[ drawcolor ])
2173 rp_colors[ drawcolor ]--; /* GRUIK */
2174 else
2175 rp_colors[ drawcolor ]++; /* GRUIK */
2177 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2178 draw_pixel( x, y );
2180 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2182 PUSH( x-1, y );
2184 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2186 PUSH( x+1, y );
2188 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2190 PUSH( x, y-1 );
2192 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2194 PUSH( x, y+1 );
2197 #undef PUSH
2198 #undef POP
2200 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2203 static void radial_gradient( int x1, int y1, int x2, int y2 )
2205 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2206 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2207 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2208 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2209 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2210 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2212 int h1, s1, v1, h2, s2, v2, r, g, b;
2214 /* radius^2 */
2215 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2216 int dist2, i=0;
2218 /* We only propagate the gradient to neighboring pixels with the same
2219 * color as ( x1, y1 ) */
2220 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2222 int x = x1;
2223 int y = y1;
2225 if( radius2 == 0 ) return;
2226 if( preview )
2228 line_gradient( x1, y1, x2, y2 );
2231 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2232 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2234 #define PUSH( x0, y0 ) \
2235 buffer.coord[i].x = (short)(x0); \
2236 buffer.coord[i].y = (short)(y0); \
2237 i++;
2238 #define POP( a, b ) \
2239 i--; \
2240 a = (int)buffer.coord[i].x; \
2241 b = (int)buffer.coord[i].y;
2243 PUSH( x, y );
2245 while( i != 0 )
2247 POP( x, y );
2249 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2251 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2252 s1+((s2-s1)*dist2)/radius2,
2253 v1+((v2-v1)*dist2)/radius2,
2254 &r, &g, &b );
2255 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2257 else
2259 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2261 if( rp_colors[ drawcolor ] == prev_color )
2263 if( rp_colors[ drawcolor ])
2264 rp_colors[ drawcolor ]--; /* GRUIK */
2265 else
2266 rp_colors[ drawcolor ]++; /* GRUIK */
2268 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2269 draw_pixel( x, y );
2271 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2273 PUSH( x-1, y );
2275 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2277 PUSH( x+1, y );
2279 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2281 PUSH( x, y-1 );
2283 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2285 PUSH( x, y+1 );
2288 #undef PUSH
2289 #undef POP
2291 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2294 static void draw_toolbars(bool update)
2296 int i;
2297 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2298 rb->lcd_set_background( COLOR_LIGHTGRAY );
2299 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2300 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2301 rb->lcd_set_foreground( COLOR_BLACK );
2302 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2304 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2305 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2306 TB_SC_SIZE, TB_SC_SIZE );
2307 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2308 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2309 TB_SC_SIZE, TB_SC_SIZE );
2310 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2311 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2312 TB_SC_SIZE, TB_SC_SIZE );
2313 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2314 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2315 TB_SC_SIZE, TB_SC_SIZE );
2317 for( i=0; i<18; i++ )
2319 rb->lcd_set_foreground( rp_colors[i] );
2320 rb->lcd_fillrect(
2321 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2322 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2323 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2324 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2325 rb->lcd_drawrect(
2326 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2327 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2328 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2331 #define SEPARATOR( x, y ) \
2332 rb->lcd_set_foreground( COLOR_WHITE ); \
2333 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2334 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2335 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2336 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2338 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2339 TB_TL_WIDTH, TB_TL_HEIGHT );
2340 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2341 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2342 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2343 TB_TL_SIZE, TB_TL_SIZE );
2345 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2347 rb->lcd_setfont( FONT_SYSFIXED );
2348 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2349 rb->lcd_setfont( FONT_UI );
2350 #undef TOP
2352 if( update ) rb->lcd_update();
2355 static void toolbar( void )
2357 int button, i, j;
2358 restore_screen();
2359 draw_toolbars( false );
2360 y = LCD_HEIGHT-TB_HEIGHT/2;
2361 inv_cursor( true );
2362 while( 1 )
2364 switch( button = rb->button_get( true ) )
2366 case ROCKPAINT_DRAW:
2367 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2368 if( y >= TOP + TB_SC_FG_TOP
2369 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2370 && x >= TB_SC_FG_LEFT
2371 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2373 /* click on the foreground color */
2374 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2376 else if( y >= TOP + TB_SC_BG_TOP
2377 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2378 && x >= TB_SC_BG_LEFT
2379 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2381 /* click on the background color */
2382 i = drawcolor;
2383 drawcolor = bgdrawcolor;
2384 bgdrawcolor = i;
2386 else if( y >= TOP + TB_PL_TOP
2387 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2388 && x >= TB_PL_LEFT
2389 && x < TB_PL_LEFT + TB_PL_WIDTH )
2391 /* click on the palette */
2392 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2393 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2394 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2395 break;
2396 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2397 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2398 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2400 else if( y >= TOP+TB_TL_TOP
2401 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2402 && x >= TB_TL_LEFT
2403 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2405 /* click on the tools */
2406 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2407 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2408 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2409 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2410 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2411 tool = i*2+j;
2412 prev_x = -1;
2413 prev_y = -1;
2414 prev_x2 = -1;
2415 prev_y2 = -1;
2416 prev_x3 = -1;
2417 prev_y3 = -1;
2418 preview = false;
2420 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2422 /* menu button */
2423 goto_menu();
2425 #undef TOP
2426 restore_screen();
2427 draw_toolbars( false );
2428 inv_cursor( true );
2429 break;
2431 case ROCKPAINT_LEFT:
2432 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2433 inv_cursor(false);
2434 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2435 if (x<0) x=COLS-1;
2436 inv_cursor(true);
2437 break;
2439 case ROCKPAINT_RIGHT:
2440 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2441 inv_cursor(false);
2442 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2443 if (x>=COLS) x=0;
2444 inv_cursor(true);
2445 break;
2447 case ROCKPAINT_UP:
2448 case ROCKPAINT_UP | BUTTON_REPEAT:
2449 inv_cursor(false);
2450 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2451 if (y<LCD_HEIGHT-TB_HEIGHT)
2453 return;
2455 inv_cursor(true);
2456 break;
2458 case ROCKPAINT_DOWN:
2459 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2460 inv_cursor(false);
2461 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2462 if (y>=LCD_HEIGHT)
2464 y = 0;
2465 return;
2467 inv_cursor(true);
2468 break;
2470 case ROCKPAINT_TOOLBAR:
2471 case ROCKPAINT_TOOLBAR2:
2472 return;
2474 if( quit ) return;
2478 static void inv_cursor(bool update)
2480 rb->lcd_set_foreground(COLOR_BLACK);
2481 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2482 /* cross painting */
2483 rb->lcd_hline(x-4,x-1,y);
2484 rb->lcd_hline(x+1,x+4,y);
2485 rb->lcd_vline(x,y-4,y-1);
2486 rb->lcd_vline(x,y+1,y+4);
2487 rb->lcd_set_foreground(rp_colors[drawcolor]);
2488 rb->lcd_set_drawmode(DRMODE_SOLID);
2490 if( update ) rb->lcd_update();
2493 static void restore_screen(void)
2495 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2498 static void clear_drawing(void)
2500 init_buffer();
2501 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2502 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2503 rb->lcd_update();
2506 static void goto_menu(void)
2508 int multi;
2510 while( 1 )
2512 switch( menu_display( main_menu, 1 ) )
2514 case MAIN_MENU_NEW:
2515 clear_drawing();
2516 return;
2518 case MAIN_MENU_LOAD:
2519 if( browse( filename, MAX_PATH, "/" ) )
2521 if( load_bitmap( filename ) <= 0 )
2523 rb->splashf( 1*HZ, "Error while loading %s",
2524 filename );
2526 else
2528 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2529 restore_screen();
2530 inv_cursor(true);
2531 return;
2534 break;
2536 case MAIN_MENU_SAVE:
2537 if (!filename[0])
2538 rb->strcpy(filename,"/");
2539 if( !rb->kbd_input( filename, MAX_PATH ) )
2541 if(rb->strlen(filename) <= 4 ||
2542 rb->strcasecmp(&filename[rb->strlen(filename)-4], ".bmp"))
2543 rb->strcat(filename, ".bmp");
2544 save_bitmap( filename );
2545 rb->splashf( 1*HZ, "File saved (%s)", filename );
2547 break;
2549 case MAIN_MENU_BRUSH_SIZE:
2550 multi = menu_display( size_menu, bsize );
2551 if( multi != - 1 )
2552 bsize = multi;
2553 break;
2555 case MAIN_MENU_BRUSH_SPEED:
2556 multi = menu_display( speed_menu, bspeed );
2557 if( multi != -1 )
2558 bspeed = multi;
2559 break;
2561 case MAIN_MENU_COLOR:
2562 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2563 break;
2565 case MAIN_MENU_GRID_SIZE:
2566 multi = menu_display( gridsize_menu, gridsize );
2567 if( multi != - 1 )
2568 gridsize = multi;
2569 break;
2571 case MAIN_MENU_EXIT:
2572 quit=true;
2573 return;
2575 case MAIN_MENU_RESUME:
2576 case MENU_ESC:
2577 return;
2578 }/* end switch */
2579 }/* end while */
2582 static void reset_tool( void )
2584 prev_x = -1;
2585 prev_y = -1;
2586 prev_x2 = -1;
2587 prev_y2 = -1;
2588 prev_x3 = -1;
2589 prev_y3 = -1;
2590 tool_mode = -1;
2591 preview = false;
2594 static bool rockpaint_loop( void )
2596 int button=0,i,j;
2597 int accelaration;
2599 while (!quit) {
2600 button = rb->button_get(true);
2602 if( tool == Brush && prev_x != -1 )
2604 accelaration = 1;
2606 else if( button & BUTTON_REPEAT )
2608 accelaration = 4;
2610 else
2612 accelaration = 1;
2615 switch(button)
2617 case ROCKPAINT_QUIT:
2618 rb->lcd_set_drawmode(DRMODE_SOLID);
2619 return PLUGIN_OK;
2621 case ROCKPAINT_MENU:
2622 inv_cursor(false);
2623 goto_menu();
2624 inv_cursor(true);
2625 break;
2627 case ROCKPAINT_DRAW:
2628 inv_cursor(false);
2629 switch( tool )
2631 case Brush:
2632 if( prev_x == -1 ) prev_x = 1;
2633 else prev_x = -1;
2634 break;
2636 case SelectRectangle:
2637 case Line:
2638 case Curve:
2639 case Rectangle:
2640 case RectangleFull:
2641 case Oval:
2642 case OvalFull:
2643 case LinearGradient:
2644 case RadialGradient:
2645 /* Curve uses 4 points, others use 2 */
2646 if( prev_x == -1 || prev_y == -1 )
2648 prev_x = x;
2649 prev_y = y;
2650 preview = true;
2652 else if( tool == Curve
2653 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2655 prev_x2 = x;
2656 prev_y2 = y;
2658 else if( tool == SelectRectangle
2659 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2661 tool_mode = menu_display( select_menu,
2662 SELECT_MENU_CUT );
2663 switch( tool_mode )
2665 case SELECT_MENU_CUT:
2666 case SELECT_MENU_COPY:
2667 prev_x2 = x;
2668 prev_y2 = y;
2669 copy_to_clipboard();
2670 if( prev_x < x ) x = prev_x;
2671 if( prev_y < y ) y = prev_y;
2672 break;
2674 case SELECT_MENU_INVERT:
2675 draw_invert( prev_x, prev_y, x, y );
2676 reset_tool();
2677 break;
2679 case SELECT_MENU_HFLIP:
2680 draw_hflip( prev_x, prev_y, x, y );
2681 reset_tool();
2682 break;
2684 case SELECT_MENU_VFLIP:
2685 draw_vflip( prev_x, prev_y, x, y );
2686 reset_tool();
2687 break;
2689 case SELECT_MENU_ROTATE90:
2690 break;
2691 case SELECT_MENU_ROTATE180:
2692 draw_hflip( prev_x, prev_y, x, y );
2693 draw_vflip( prev_x, prev_y, x, y );
2694 reset_tool();
2695 break;
2696 case SELECT_MENU_ROTATE270:
2697 break;
2699 case SELECT_MENU_CANCEL:
2700 reset_tool();
2701 break;
2703 case MENU_ESC:
2704 break;
2707 else if( tool == Curve
2708 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2710 prev_x3 = x;
2711 prev_y3 = y;
2713 else
2715 preview = false;
2716 switch( tool )
2718 case SelectRectangle:
2719 draw_paste_rectangle( prev_x, prev_y,
2720 prev_x2, prev_y2,
2721 x, y, tool_mode );
2722 break;
2723 case Line:
2724 draw_line( prev_x, prev_y, x, y );
2725 break;
2726 case Curve:
2727 draw_curve( prev_x, prev_y,
2728 prev_x2, prev_y2,
2729 prev_x3, prev_y3,
2730 x, y );
2731 break;
2732 case Rectangle:
2733 draw_rect( prev_x, prev_y, x, y );
2734 break;
2735 case RectangleFull:
2736 draw_rect_full( prev_x, prev_y, x, y );
2737 break;
2738 case Oval:
2739 draw_oval_empty( prev_x, prev_y, x, y );
2740 break;
2741 case OvalFull:
2742 draw_oval_full( prev_x, prev_y, x, y );
2743 break;
2744 case LinearGradient:
2745 linear_gradient( prev_x, prev_y, x, y );
2746 break;
2747 case RadialGradient:
2748 radial_gradient( prev_x, prev_y, x, y );
2749 break;
2750 default:
2751 break;
2753 reset_tool();
2755 break;
2757 case Fill:
2758 draw_fill( x, y );
2759 break;
2761 case ColorPicker:
2762 color_picker( x, y );
2763 break;
2765 case Text:
2766 draw_text( x, y );
2767 break;
2769 default:
2770 break;
2772 inv_cursor(true);
2773 break;
2775 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2776 if( tool == Curve )
2778 /* 3 point bezier curve */
2779 preview = false;
2780 draw_curve( prev_x, prev_y,
2781 prev_x2, prev_y2,
2782 -1, -1,
2783 x, y );
2784 reset_tool();
2785 restore_screen();
2786 inv_cursor( true );
2788 break;
2790 case ROCKPAINT_TOOLBAR:
2791 i = x; j = y;
2792 x = 10;
2793 toolbar();
2794 x = i; y = j;
2795 restore_screen();
2796 inv_cursor(true);
2797 break;
2799 case ROCKPAINT_TOOLBAR2:
2800 i = x; j = y;
2801 x = 110;
2802 toolbar();
2803 x = i; y = j;
2804 restore_screen();
2805 inv_cursor(true);
2806 break;
2808 case ROCKPAINT_LEFT:
2809 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2810 inv_cursor(false);
2811 x-=bspeed * accelaration;
2812 if (x<0) x=COLS-1;
2813 inv_cursor(true);
2814 break;
2816 case ROCKPAINT_RIGHT:
2817 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2818 inv_cursor(false);
2819 x+=bspeed * accelaration;
2820 if (x>=COLS) x=0;
2821 inv_cursor(true);
2822 break;
2824 case ROCKPAINT_UP:
2825 case ROCKPAINT_UP | BUTTON_REPEAT:
2826 inv_cursor(false);
2827 y-=bspeed * accelaration;
2828 if (y<0) y=ROWS-1;
2829 inv_cursor(true);
2830 break;
2832 case ROCKPAINT_DOWN:
2833 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2834 inv_cursor(false);
2835 y+=bspeed * accelaration;
2836 if (y>=ROWS)
2838 toolbar();
2839 restore_screen();
2841 inv_cursor(true);
2842 break;
2844 default:
2845 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2846 return PLUGIN_USB_CONNECTED;
2847 break;
2849 if( tool == Brush && prev_x == 1 )
2851 inv_cursor(false);
2852 draw_brush( x, y );
2853 inv_cursor(true);
2855 if( preview || tool == ColorPicker )
2856 /* always preview color picker */
2858 restore_screen();
2859 switch( tool )
2861 case SelectRectangle:
2862 if( prev_x2 == -1 || prev_y2 == -1 )
2864 /* we are defining the selection */
2865 draw_select_rectangle( prev_x, prev_y, x, y );
2867 else
2869 /* we are pasting the selected data */
2870 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2871 prev_y2, x, y, tool_mode );
2872 prev_x3 = prev_x2-prev_x;
2873 if( prev_x3 < 0 ) prev_x3 *= -1;
2874 prev_y3 = prev_y2-prev_y;
2875 if( prev_y3 < 0 ) prev_y3 *= -1;
2876 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2877 prev_x3 = -1;
2878 prev_y3 = -1;
2880 break;
2882 case Brush:
2883 break;
2885 case Line:
2886 draw_line( prev_x, prev_y, x, y );
2887 break;
2889 case Curve:
2890 if( prev_x2 == -1 || prev_y2 == -1 )
2892 draw_line( prev_x, prev_y, x, y );
2894 else
2896 draw_curve( prev_x, prev_y,
2897 prev_x2, prev_y2,
2898 prev_x3, prev_y3,
2899 x, y );
2901 break;
2903 case Rectangle:
2904 draw_rect( prev_x, prev_y, x, y );
2905 break;
2907 case RectangleFull:
2908 draw_rect_full( prev_x, prev_y, x, y );
2909 break;
2911 case Oval:
2912 draw_oval_empty( prev_x, prev_y, x, y );
2913 break;
2915 case OvalFull:
2916 draw_oval_full( prev_x, prev_y, x, y );
2917 break;
2919 case Fill:
2920 break;
2922 case ColorPicker:
2923 preview = true;
2924 color_picker( x, y );
2925 preview = false;
2926 break;
2928 case LinearGradient:
2929 line_gradient( prev_x, prev_y, x, y );
2930 break;
2932 case RadialGradient:
2933 line_gradient( prev_x, prev_y, x, y );
2934 break;
2936 case Text:
2937 default:
2938 break;
2940 inv_cursor( true );
2942 if( gridsize > 0 )
2944 show_grid( true );
2945 show_grid( false );
2949 return PLUGIN_OK;
2952 static int load_bitmap( const char *file )
2954 struct bitmap bm;
2955 bool ret;
2956 int l;
2958 bm.data = (char*)save_buffer;
2959 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2960 FORMAT_NATIVE );
2962 if((bm.width > COLS ) || ( bm.height > ROWS ))
2963 return -1;
2965 for( l = bm.height-1; l > 0; l-- )
2967 rb->memmove( save_buffer+l*COLS, save_buffer+l*bm.width,
2968 sizeof( fb_data )*bm.width );
2970 for( l = 0; l < bm.height; l++ )
2972 rb->memset( save_buffer+l*COLS+bm.width, rp_colors[ bgdrawcolor ],
2973 sizeof( fb_data )*(COLS-bm.width) );
2975 rb->memset( save_buffer+COLS*bm.height, rp_colors[ bgdrawcolor ],
2976 sizeof( fb_data )*COLS*(ROWS-bm.height) );
2978 return ret;
2981 static int save_bitmap( char *file )
2983 struct bitmap bm;
2984 bm.data = (char*)save_buffer;
2985 bm.height = ROWS;
2986 bm.width = COLS;
2987 bm.format = FORMAT_NATIVE;
2988 return save_bmp_file( file, &bm, rb );
2991 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
2993 /** must have stuff **/
2994 rb = api;
2996 rb->lcd_set_foreground(COLOR_WHITE);
2997 rb->lcd_set_backdrop(NULL);
2998 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
2999 rb->splash( HZ/2, "Rock Paint");
3001 rb->lcd_clear_display();
3003 filename[0] = '\0';
3005 if( parameter )
3007 if( load_bitmap( parameter ) <= 0 )
3009 rb->splash( 1*HZ, "File Open Error");
3010 clear_drawing();
3012 else
3014 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
3015 restore_screen();
3016 rb->strcpy( filename, parameter );
3019 else
3021 clear_drawing();
3023 inv_cursor(true);
3025 return rockpaint_loop();