New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / plugins / rockpaint.c
blobc9a0c6a8405b4274cdd418c6ab201a899aeb2f1c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Antoine Cellerier <dionoea -at- videolan -dot- org>
11 * Based on parts of rockpaint 0.45, Copyright (C) 2005 Eli Sherer
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 /**
24 * TODO:
25 * - implement 2 layers with alpha colors
26 * - take brush width into account when drawing shapes
27 * - handle bigger than screen bitmaps
28 * - cache fonts when building the font preview (else it only works well on simulators because they have "fast" disk read)
31 #include "plugin.h"
32 #include "lib/bmp.h"
33 #include "lib/rgb_hsv.h"
35 PLUGIN_HEADER
37 /***********************************************************************
38 * Buttons
39 ***********************************************************************/
41 #if CONFIG_KEYPAD == IRIVER_H300_PAD
42 #define ROCKPAINT_QUIT BUTTON_OFF
43 #define ROCKPAINT_DRAW BUTTON_SELECT
44 #define ROCKPAINT_MENU BUTTON_ON
45 #define ROCKPAINT_TOOLBAR BUTTON_REC
46 #define ROCKPAINT_TOOLBAR2 BUTTON_MODE
47 #define ROCKPAINT_UP BUTTON_UP
48 #define ROCKPAINT_DOWN BUTTON_DOWN
49 #define ROCKPAINT_LEFT BUTTON_LEFT
50 #define ROCKPAINT_RIGHT BUTTON_RIGHT
52 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
53 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
54 #define ROCKPAINT_QUIT ( ~BUTTON_MAIN )
55 #define ROCKPAINT_DRAW BUTTON_SELECT
56 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_MENU )
57 #define ROCKPAINT_TOOLBAR ( BUTTON_MENU | BUTTON_LEFT )
58 #define ROCKPAINT_TOOLBAR2 ( BUTTON_MENU | BUTTON_RIGHT )
59 #define ROCKPAINT_UP BUTTON_MENU
60 #define ROCKPAINT_DOWN BUTTON_PLAY
61 #define ROCKPAINT_LEFT BUTTON_LEFT
62 #define ROCKPAINT_RIGHT BUTTON_RIGHT
64 #elif ( CONFIG_KEYPAD == IAUDIO_X5M5_PAD )
65 #define ROCKPAINT_QUIT BUTTON_POWER
66 #define ROCKPAINT_DRAW BUTTON_SELECT
67 #define ROCKPAINT_MENU BUTTON_PLAY
68 #define ROCKPAINT_TOOLBAR BUTTON_REC
69 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
70 #define ROCKPAINT_UP BUTTON_UP
71 #define ROCKPAINT_DOWN BUTTON_DOWN
72 #define ROCKPAINT_LEFT BUTTON_LEFT
73 #define ROCKPAINT_RIGHT BUTTON_RIGHT
75 #elif CONFIG_KEYPAD == GIGABEAT_PAD
76 #define ROCKPAINT_QUIT BUTTON_POWER
77 #define ROCKPAINT_DRAW BUTTON_SELECT
78 #define ROCKPAINT_MENU BUTTON_MENU
79 #define ROCKPAINT_TOOLBAR BUTTON_A
80 #define ROCKPAINT_TOOLBAR2 ( BUTTON_A | BUTTON_LEFT )
81 #define ROCKPAINT_UP BUTTON_UP
82 #define ROCKPAINT_DOWN BUTTON_DOWN
83 #define ROCKPAINT_LEFT BUTTON_LEFT
84 #define ROCKPAINT_RIGHT BUTTON_RIGHT
86 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
87 (CONFIG_KEYPAD == SANSA_C200_PAD)
88 #define ROCKPAINT_QUIT BUTTON_POWER
89 #define ROCKPAINT_DRAW BUTTON_SELECT
90 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_POWER )
91 #define ROCKPAINT_TOOLBAR BUTTON_REC
92 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
93 #define ROCKPAINT_UP BUTTON_UP
94 #define ROCKPAINT_DOWN BUTTON_DOWN
95 #define ROCKPAINT_LEFT BUTTON_LEFT
96 #define ROCKPAINT_RIGHT BUTTON_RIGHT
98 #elif ( CONFIG_KEYPAD == IRIVER_H10_PAD )
99 #define ROCKPAINT_QUIT BUTTON_POWER
100 #define ROCKPAINT_DRAW BUTTON_FF
101 #define ROCKPAINT_MENU BUTTON_PLAY
102 #define ROCKPAINT_TOOLBAR BUTTON_REW
103 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
104 #define ROCKPAINT_UP BUTTON_SCROLL_UP
105 #define ROCKPAINT_DOWN BUTTON_SCROLL_DOWN
106 #define ROCKPAINT_LEFT BUTTON_LEFT
107 #define ROCKPAINT_RIGHT BUTTON_RIGHT
109 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
110 #define ROCKPAINT_QUIT BUTTON_BACK
111 #define ROCKPAINT_DRAW BUTTON_SELECT
112 #define ROCKPAINT_MENU BUTTON_MENU
113 #define ROCKPAINT_TOOLBAR BUTTON_PLAY
114 #define ROCKPAINT_TOOLBAR2 ( BUTTON_PLAY | BUTTON_LEFT )
115 #define ROCKPAINT_UP BUTTON_UP
116 #define ROCKPAINT_DOWN BUTTON_DOWN
117 #define ROCKPAINT_LEFT BUTTON_LEFT
118 #define ROCKPAINT_RIGHT BUTTON_RIGHT
120 #elif ( CONFIG_KEYPAD == COWOND2_PAD )
121 #define ROCKPAINT_QUIT BUTTON_POWER
122 #define ROCKPAINT_MENU BUTTON_MENU
124 #else
125 #error "Please define keys for this keypad"
126 #endif
128 #ifdef HAVE_TOUCHSCREEN
129 #ifndef ROCKPAINT_QUIT
130 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
131 #endif
132 #ifndef ROCKPAINT_DRAW
133 #define ROCKPAINT_DRAW BUTTON_CENTER
134 #endif
135 #ifndef ROCKPAINT_MENU
136 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
137 #endif
138 #ifndef ROCKPAINT_TOOLBAR
139 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
140 #endif
141 #ifndef ROCKPAINT_TOOLBAR2
142 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
143 #endif
144 #ifndef ROCKPAINT_UP
145 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
146 #endif
147 #ifndef ROCKPAINT_DOWN
148 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
149 #endif
150 #ifndef ROCKPAINT_LEFT
151 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
152 #endif
153 #ifndef ROCKPAINT_RIGHT
154 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
155 #endif
156 #endif
158 /***********************************************************************
159 * Palette Default Colors
160 ***********************************************************************/
161 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
162 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
163 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
164 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
165 #define COLOR_RED LCD_RGBPACK(128,0,0)
166 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
167 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
168 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
169 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
170 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
171 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
172 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
173 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
174 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
175 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
176 #define COLOR_PINK LCD_RGBPACK(255,0,255)
177 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
178 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
180 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
181 #define ROCKPAINT_TITLE_FONT 2
183 /***********************************************************************
184 * Program Colors
185 ***********************************************************************/
186 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
187 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
189 #define ROWS LCD_HEIGHT
190 #define COLS LCD_WIDTH
193 * Toolbar positioning stuff ... don't read this unless you really need to
195 * TB Toolbar
196 * SP Separator
197 * SC Selected Color
198 * PL Palette
199 * TL Tools
202 /* Separator sizes */
203 #define TB_SP_MARGIN 3
204 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
206 /* Selected color sizes */
207 #define TB_SC_SIZE 12
209 /* Palette sizes */
210 #define TB_PL_COLOR_SIZE 7
211 #define TB_PL_COLOR_SPACING 2
212 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
213 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
215 /* Tools sizes */
216 #define TB_TL_SIZE 8
217 #define TB_TL_SPACING 2
218 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
219 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
221 /* Menu button size ... gruik */
222 #define TB_MENU_MIN_WIDTH 30
224 /* Selected colors position */
225 #define TB_SC_FG_TOP 2
226 #define TB_SC_FG_LEFT 2
227 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
228 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
230 /* Palette position */
231 #define TB_PL_TOP TB_SC_FG_TOP
232 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
234 /* Tools position */
235 #define TB_TL_TOP TB_SC_FG_TOP
236 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
238 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
239 #undef TB_TL_TOP
240 #undef TB_TL_LEFT
241 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
242 #define TB_TL_LEFT TB_SC_FG_LEFT
243 #endif
245 /* Menu button position */
246 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
247 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
249 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
252 static void draw_pixel(int x,int y);
253 static void draw_line( int x1, int y1, int x2, int y2 );
254 static void draw_rect( int x1, int y1, int x2, int y2 );
255 static void draw_toolbars(bool update);
256 static void inv_cursor(bool update);
257 static void restore_screen(void);
258 static void clear_drawing(void);
259 static void goto_menu(void);
260 static int load_bitmap( const char *filename );
261 static int save_bitmap( char *filename );
262 static void draw_rect_full( int x1, int y1, int x2, int y2 );
263 extern int errno;
265 /***********************************************************************
266 * Global variables
267 ***********************************************************************/
269 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
270 int errno;
271 #endif
273 static const struct plugin_api* rb;
275 MEM_FUNCTION_WRAPPERS(rb);
277 static int drawcolor=0; /* Current color (in palette) */
278 static int bgdrawcolor=9; /* Current background color (in palette) */
279 bool isbg = false; /* gruik ugly hack alert */
281 static int preview=false; /* Is preview mode on ? */
283 /* TODO: clean this up */
284 static int x=0, y=0; /* cursor position */
285 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
286 static int prev_x2=-1, prev_y2=-1;
287 static int prev_x3=-1, prev_y3=-1;
288 static int tool_mode=-1;
291 static int bsize=1; /* brush size */
292 static int bspeed=1; /* brush speed */
294 enum Tools { Brush = 0, /* Regular brush */
295 Fill = 1, /* Fill a shape with current color */
296 SelectRectangle = 2,
297 ColorPicker = 3, /* Pick a color */
298 Line = 4, /* Draw a line between two points */
299 Unused = 5, /* THIS IS UNUSED ... */
300 Curve = 6,
301 Text = 7,
302 Rectangle = 8, /* Draw a rectangle */
303 RectangleFull = 9,
304 Oval = 10, /* Draw an oval */
305 OvalFull = 11,
306 LinearGradient = 12,
307 RadialGradient = 13
310 enum Tools tool = Brush;
312 static bool quit=false;
313 static int gridsize=0;
315 static fb_data rp_colors[18] =
317 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
318 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
319 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
320 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
321 COLOR_LIGHTBROWN
324 static fb_data save_buffer[ ROWS*COLS ];
326 extern fb_data rockpaint[];
327 extern fb_data rockpaint_hsvrgb[];
329 /* Maximum string size allowed for the text tool */
330 #define MAX_TEXT 255
332 static union
334 /* Used by fill and gradient algorithms */
335 struct
337 short x;
338 short y;
339 } coord[ ROWS*COLS ];
341 /* Used by bezier curve algorithms */
342 struct
344 short x1, y1;
345 short x2, y2;
346 short x3, y3;
347 short x4, y4;
348 short depth;
349 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
350 * than coord ... so we divide to take
351 * less memory. */
353 /* Used to cut/copy/paste data */
354 fb_data clipboard[ ROWS*COLS ];
356 /* Used for text mode */
357 struct
359 char text[MAX_TEXT+1];
360 char font[MAX_PATH+1];
361 char old_font[MAX_PATH+1];
362 int fh_buf[30];
363 int fw_buf[30];
364 char fontname_buf[30][MAX_PATH];
365 } text;
366 } buffer;
368 /* Current filename */
369 static char filename[MAX_PATH+1];
371 /* Font preview buffer */
372 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
373 //#define FONT_PREVIEW_HEIGHT 1000
374 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
376 /***********************************************************************
377 * Offscreen buffer/Text/Fonts handling
379 * Parts of code taken from firmware/drivers/lcd-16bit.c
380 ***********************************************************************/
381 static void buffer_mono_bitmap_part(
382 fb_data *buf, int buf_width, int buf_height,
383 const unsigned char *src, int src_x, int src_y,
384 int stride, int x, int y, int width, int height )
385 /* this function only draws the foreground part of the bitmap */
387 const unsigned char *src_end;
388 fb_data *dst, *dst_end;
389 unsigned fgcolor = rb->lcd_get_foreground();
391 /* nothing to draw? */
392 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
393 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
394 return;
396 /* clipping */
397 if( x < 0 )
399 width += x;
400 src_x -= x;
401 x = 0;
403 if( y < 0 )
405 height += y;
406 src_y -= y;
407 y = 0;
409 if( x + width > buf_width )
410 width = buf_width - x;
411 if( y + height > buf_height )
412 height = buf_height - y;
414 src += stride * (src_y >> 3) + src_x; /* move starting point */
415 src_y &= 7;
416 src_end = src + width;
418 dst = buf + y*buf_width + x;
422 const unsigned char *src_col = src++;
423 unsigned data = *src_col >> src_y;
424 fb_data *dst_col = dst++;
425 int numbits = 8 - src_y;
427 dst_end = dst_col + height * buf_width;
430 if( data & 0x01 )
431 *dst_col = fgcolor; /* FIXME ? */
433 dst_col += buf_width;
435 data >>= 1;
436 if( --numbits == 0 )
438 src_col += stride;
439 data = *src_col;
440 numbits = 8;
442 } while( dst_col < dst_end );
443 } while( src < src_end );
446 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
447 int x, int y, int ofs, const unsigned char *str )
449 unsigned short ch;
450 unsigned short *ucs;
452 struct font *pf = rb->font_get( FONT_UI );
453 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
455 ucs = rb->bidi_l2v( str, 1 );
457 while( (ch = *ucs++) != 0 && x < buf_width )
459 int width;
460 const unsigned char *bits;
462 /* get proportional width and glyph bits */
463 width = rb->font_get_width( pf, ch );
465 if( ofs > width )
467 ofs -= width;
468 continue;
471 bits = rb->font_get_bits( pf, ch );
473 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0, width, x, y, width - ofs, pf->height);
475 x += width - ofs;
476 ofs = 0;
480 /***********************************************************************
481 * Menu handling
482 ***********************************************************************/
483 struct menu_items
485 int value;
486 char label[16]; /* GRUIK ? */
489 #define MENU_ESC -1242
490 enum {
491 /* Generic menu items */
492 MENU_END = -42, MENU_TITLE = -12,
493 /* Main menu */
494 MAIN_MENU_RESUME,
495 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
496 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
497 MAIN_MENU_GRID_SIZE,
498 MAIN_MENU_EXIT,
499 /* Select action menu */
500 SELECT_MENU_CUT, SELECT_MENU_COPY, SELECT_MENU_INVERT,
501 SELECT_MENU_HFLIP, SELECT_MENU_VFLIP, SELECT_MENU_ROTATE90,
502 SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
503 SELECT_MENU_CANCEL,
504 /* Text menu */
505 TEXT_MENU_TEXT, TEXT_MENU_FONT,
506 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
509 static struct menu_items main_menu[]=
510 { { MENU_TITLE, "RockPaint" },
511 { MAIN_MENU_RESUME, "Resume" },
512 { MAIN_MENU_NEW, "New" },
513 { MAIN_MENU_LOAD, "Load" },
514 { MAIN_MENU_SAVE, "Save" },
515 { MAIN_MENU_BRUSH_SIZE, "Brush Size" },
516 { MAIN_MENU_BRUSH_SPEED, "Brush Speed" },
517 { MAIN_MENU_COLOR, "Choose Color" },
518 { MAIN_MENU_GRID_SIZE, "Grid Size" },
519 { MAIN_MENU_EXIT, "Exit" },
520 { MENU_END, "" } };
522 static struct menu_items size_menu[] =
523 { { MENU_TITLE, "Choose Size" },
524 { 1, "1x" },
525 { 2, "2x" },
526 { 4, "4x" },
527 { 8, "8x" },
528 { MENU_END, "" } };
530 static struct menu_items speed_menu[] =
531 { { MENU_TITLE, "Choose Speed" },
532 { 1, "1x" },
533 { 2, "2x" },
534 { 4, "4x" },
535 { MENU_END, "" } };
537 static struct menu_items gridsize_menu[] =
538 { { MENU_TITLE, "Grid Size" },
539 { 0, "No grid" },
540 { 5, "5px" },
541 { 10, "10px" },
542 { 20, "20px" },
543 { MENU_END, "" } };
545 static struct menu_items select_menu[] =
546 { { MENU_TITLE, "Select..." },
547 { SELECT_MENU_CUT, "Cut" },
548 { SELECT_MENU_COPY, "Copy" },
549 { SELECT_MENU_INVERT, "Invert" },
550 { SELECT_MENU_HFLIP, "Horizontal flip" },
551 { SELECT_MENU_VFLIP, "Vertical flip" },
552 // { SELECT_MENU_ROTATE90, "Rotate 90°" },
553 { SELECT_MENU_ROTATE180, "Rotate 180°" },
554 // { SELECT_MENU_ROTATE270, "Rotate 270°" },
555 { SELECT_MENU_CANCEL, "Cancel" },
556 { MENU_END, "" } };
558 static struct menu_items text_menu[] =
559 { { MENU_TITLE, "Text" },
560 { TEXT_MENU_TEXT, "Set text" },
561 { TEXT_MENU_FONT, "Change font" },
562 { TEXT_MENU_PREVIEW, "Preview" },
563 { TEXT_MENU_APPLY, "Apply" },
564 { TEXT_MENU_CANCEL, "Cancel" },
565 { MENU_END, "" } };
567 static int draw_window( int height, int width,
568 int *top, int *left,
569 const char *title )
571 int fh;
572 rb->lcd_getstringsize( title, NULL, &fh );
573 fh++;
575 const int _top = ( LCD_HEIGHT - height ) / 2;
576 const int _left = ( LCD_WIDTH - width ) / 2;
577 if( top ) *top = _top;
578 if( left ) *left = _left;
579 rb->lcd_set_background(COLOR_BLUE);
580 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
581 rb->lcd_fillrect( _left, _top, width, height );
582 rb->lcd_set_foreground(COLOR_BLUE);
583 rb->lcd_fillrect( _left, _top, width, fh+4 );
584 rb->lcd_set_foreground(COLOR_WHITE);
585 rb->lcd_putsxy( _left+2, _top+2, title );
586 rb->lcd_set_foreground(COLOR_BLACK);
587 rb->lcd_drawrect( _left, _top, width, height );
588 return _top+fh+4;
591 static int menu_display( struct menu_items menu[], int prev_value )
593 int i,
594 fh, /* font height */
595 width, height,
596 a, b,
597 selection=1,
598 menu_length,
599 top, left;
600 int scroll = 0, onscreen = 0;
602 rb->lcd_getstringsize( menu[0].label, &width, &fh );
603 for( i=1; menu[i].value != MENU_END; i++ )
605 if( prev_value == menu[i].value )
607 selection = i;
609 rb->lcd_getstringsize( menu[i].label, &a, &b );
610 if( a > width ) width = a;
611 if( b > fh ) fh = b;
613 menu_length = i;
614 fh++;
615 width += 10;
617 height = menu_length * fh + 4 + 2 + 2;
618 if( height >= LCD_HEIGHT )
620 scroll = 1;
621 onscreen = ( LCD_HEIGHT - 4 - 2 - 2 )/fh;
622 height = onscreen * fh + 4 + 2 + 2;
623 width += 5;
625 else
627 onscreen = menu_length;
630 draw_window( height, width, &top, &left, menu[0].label );
632 while( 1 )
634 for( i = (scroll == 0 ? 1 : scroll);
635 i < (scroll ? scroll + onscreen - 1:onscreen);
636 i++ )
638 if( i == selection )
640 rb->lcd_set_foreground( COLOR_WHITE );
641 rb->lcd_set_background( COLOR_BLUE );
643 else
645 rb->lcd_set_foreground( COLOR_BLACK );
646 rb->lcd_set_background( COLOR_LIGHTGRAY );
648 rb->lcd_putsxy( left+2,
649 top+6+fh*(i-(scroll == 0 ? 0 : scroll-1 )),
650 menu[i].label );
652 if( scroll )
654 int scroll_height = ((height-fh-4-2)*onscreen)/menu_length;
655 rb->lcd_set_foreground( COLOR_BLACK );
656 rb->lcd_vline( left+width-5, top+fh+4, top+height-2 );
657 rb->lcd_fillrect( left+width-4,
658 top+fh+4+((height-4-2-fh-scroll_height)*(scroll-1))/(menu_length-onscreen),
659 3, scroll_height );
661 rb->lcd_update();
663 switch( rb->button_get(true) )
665 case ROCKPAINT_UP:
666 case ROCKPAINT_UP|BUTTON_REPEAT:
667 selection = (selection + menu_length-1)%menu_length;
668 if( !selection ) selection = menu_length-1;
669 break;
671 case ROCKPAINT_DOWN:
672 case ROCKPAINT_DOWN|BUTTON_REPEAT:
673 selection = (selection + 1)%menu_length;
674 if( !selection ) selection++;
675 break;
677 case ROCKPAINT_LEFT:
678 restore_screen();
679 return MENU_ESC;
681 case ROCKPAINT_RIGHT:
682 case ROCKPAINT_DRAW:
683 restore_screen();
684 return menu[selection].value;
686 if( scroll )
688 if( selection < scroll )
690 scroll = selection;
691 draw_window( height, width, NULL, NULL, menu[0].label );
693 if( selection >= scroll + onscreen - 1 )
695 scroll++;
696 draw_window( height, width, NULL, NULL, menu[0].label );
702 /***********************************************************************
703 * File browser
704 ***********************************************************************/
706 char bbuf[MAX_PATH+1]; /* used by file and font browsers */
707 char bbuf_s[MAX_PATH+1]; /* used by file and font browsers */
709 static bool browse( char *dst, int dst_size, const char *start )
711 #define WIDTH ( LCD_WIDTH - 20 )
712 #define HEIGHT ( LCD_HEIGHT - 20 )
713 #define LINE_SPACE 2
714 int top, top_inside, left;
716 DIR *d;
717 struct dirent *de;
718 int fvi = 0; /* first visible item */
719 int lvi = 0; /* last visible item */
720 int si = 0; /* selected item */
721 int li = 0; /* last item */
722 int i;
724 int fh;
725 char *a;
727 rb->lcd_getstringsize( "Ap", NULL, &fh );
729 rb->strcpy( bbuf, start );
730 a = bbuf+rb->strlen(bbuf)-1;
731 if( *a != '/' )
733 a[1] = '/';
734 a[2] = '\0';
737 while( 1 )
739 d = rb->opendir( bbuf );
740 if( !d )
743 if( errno == ENOTDIR )
745 /* this is a file */
746 bbuf[rb->strlen(bbuf)-1] = '\0';
747 rb->strncpy( dst, bbuf, dst_size );
748 return true;
750 else if( errno == EACCES || errno == ENOENT )
752 bbuf[0] = '/'; bbuf[1] = '\0';
753 d = rb->opendir( "/" );
755 else
757 return false;
760 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, bbuf );
761 i = 0;
762 li = -1;
763 while( i < fvi )
765 rb->readdir( d );
766 i++;
768 while( top_inside+(i-fvi)*(fh+LINE_SPACE) < HEIGHT )
770 de = rb->readdir( d );
771 if( !de )
773 li = i-1;
774 break;
776 rb->lcd_set_foreground((si==i?COLOR_WHITE:COLOR_BLACK));
777 rb->lcd_set_background((si==i?COLOR_BLUE:COLOR_LIGHTGRAY));
778 rb->lcd_putsxy( left+10,
779 top_inside+(i-fvi)*(fh+LINE_SPACE),
780 de->d_name );
781 if( si == i )
782 rb->strcpy( bbuf_s, de->d_name );
783 i++;
785 lvi = i-1;
786 if( li == -1 )
788 if( !rb->readdir( d ) )
790 li = lvi;
793 rb->closedir( d );
795 rb->lcd_update();
797 switch( rb->button_get(true) )
799 case ROCKPAINT_UP:
800 case ROCKPAINT_UP|BUTTON_REPEAT:
801 if( si > 0 )
803 si--;
804 if( si<fvi )
806 fvi--;
809 break;
811 case ROCKPAINT_DOWN:
812 case ROCKPAINT_DOWN|BUTTON_REPEAT:
813 if( li == -1 || si < li )
815 si++;
816 if( si>lvi )
818 fvi++;
821 break;
823 case ROCKPAINT_LEFT:
824 if( bbuf[0] == '/' && !bbuf[1] ) return false;
825 bbuf_s[0] = '.';
826 bbuf_s[1] = '.';
827 bbuf_s[2] = '\0';
828 case ROCKPAINT_RIGHT:
829 case ROCKPAINT_DRAW:
830 if( *bbuf_s == '.' && !bbuf_s[1] ) break;
831 a = bbuf;
832 while( *a ) a++;
833 if( *bbuf_s == '.' && bbuf_s[1] == '.' && !bbuf_s[2] )
835 a--;
836 if( a == bbuf ) break;
837 if( *a == '/' ) a--;
838 while( *a != '/' ) a--;
839 *++a = '\0';
840 break;
842 rb->strcpy( a, bbuf_s );
843 while( *a ) a++;
844 *a++ = '/';
845 *a = '\0';
846 fvi = si = 0;
847 break;
851 #undef WIDTH
852 #undef HEIGHT
853 #undef LINE_SPACE
856 /***********************************************************************
857 * Font browser
859 * FIXME: This still needs some work ... it currently only works fine
860 * on the simulators, disk spins too much on real targets -> rendered
861 * font buffer needed.
862 ***********************************************************************/
863 static bool browse_fonts( char *dst, int dst_size )
865 char old_font[MAX_PATH];
866 #define WIDTH ( LCD_WIDTH - 20 )
867 #define HEIGHT ( LCD_HEIGHT - 20 )
868 #define LINE_SPACE 2
869 int top, top_inside = 0, left;
871 DIR *d;
872 struct dirent *de;
873 int fvi = 0; /* first visible item */
874 int lvi = 0; /* last visible item */
875 int si = 0; /* selected item */
876 int osi = 0; /* old selected item */
877 int li = 0; /* last item */
878 int nvih = 0; /* next visible item height */
879 int i;
880 int b_need_redraw = 1; /* Do we need to redraw ? */
882 int cp = 0; /* current position */
883 int fh; /* font height */
885 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
886 #define fw_buf buffer.text.fw_buf
887 int fw;
888 #define fontname_buf buffer.text.fontname_buf
890 rb->snprintf( old_font, MAX_PATH,
891 FONT_DIR "/%s.fnt",
892 rb->global_settings->font_file );
894 while( 1 )
896 if( !b_need_redraw )
898 /* we don't need to redraw ... but we need to unselect
899 * the previously selected item */
900 cp = top_inside + LINE_SPACE;
901 for( i = 0; i+fvi < osi; i++ )
903 cp += fh_buf[i] + LINE_SPACE;
905 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
906 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
907 rb->lcd_set_drawmode(DRMODE_SOLID);
910 if( b_need_redraw )
912 b_need_redraw = 0;
914 d = rb->opendir( FONT_DIR "/" );
915 if( !d )
917 return false;
919 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
920 i = 0;
921 li = -1;
922 while( i < fvi )
924 rb->readdir( d );
925 i++;
927 cp = top_inside+LINE_SPACE;
929 rb->lcd_set_foreground(COLOR_BLACK);
930 rb->lcd_set_background(COLOR_LIGHTGRAY);
932 while( cp < top+HEIGHT )
934 de = rb->readdir( d );
935 if( !de )
937 li = i-1;
938 break;
940 if( rb->strlen( de->d_name ) < 4
941 || rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
942 ".fnt" ) )
943 continue;
944 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
945 de->d_name );
946 rb->font_load( bbuf );
947 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
948 if( nvih > 0 )
950 nvih -= fh;
951 fvi++;
952 if( nvih < 0 ) nvih = 0;
953 i++;
954 continue;
956 if( cp + fh >= top+HEIGHT )
958 nvih = fh;
959 break;
961 rb->lcd_putsxy( left+10, cp, de->d_name );
962 fh_buf[i-fvi] = fh;
963 fw_buf[i-fvi] = fw;
964 cp += fh + LINE_SPACE;
965 rb->strcpy( fontname_buf[i-fvi], bbuf );
966 i++;
968 lvi = i-1;
969 if( li == -1 )
971 if( !(de = rb->readdir( d ) ) )
973 li = lvi;
975 else if( !nvih && !rb->strlen( de->d_name ) < 4
976 && !rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
977 ".fnt" ) )
979 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
980 de->d_name );
981 rb->font_load( bbuf );
982 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
983 nvih = fh;
986 rb->font_load( old_font );
987 rb->closedir( d );
990 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
991 cp = top_inside + LINE_SPACE;
992 for( i = 0; i+fvi < si; i++ )
994 cp += fh_buf[i] + LINE_SPACE;
996 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
997 rb->lcd_set_drawmode(DRMODE_SOLID);
999 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
1001 osi = si;
1002 i = fvi;
1003 switch( rb->button_get(true) )
1005 case ROCKPAINT_UP:
1006 case ROCKPAINT_UP|BUTTON_REPEAT:
1007 if( si > 0 )
1009 si--;
1010 if( si<fvi )
1012 fvi = si;
1015 break;
1017 case ROCKPAINT_DOWN:
1018 case ROCKPAINT_DOWN|BUTTON_REPEAT:
1019 if( li == -1 || si < li )
1021 si++;
1023 break;
1025 case ROCKPAINT_LEFT:
1026 return false;
1028 case ROCKPAINT_RIGHT:
1029 case ROCKPAINT_DRAW:
1030 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
1031 return true;
1034 if( i != fvi || si > lvi )
1036 b_need_redraw = 1;
1039 if( si<=lvi )
1041 nvih = 0;
1044 #undef fh_buf
1045 #undef fw_buf
1046 #undef fontname_buf
1047 #undef WIDTH
1048 #undef HEIGHT
1049 #undef LINE_SPACE
1052 /***********************************************************************
1053 * HSVRGB Color chooser
1054 ***********************************************************************/
1055 static unsigned int color_chooser( unsigned int color )
1057 int red = RGB_UNPACK_RED( color );
1058 int green = RGB_UNPACK_GREEN( color );
1059 int blue = RGB_UNPACK_BLUE( color );
1060 int hue, saturation, value;
1061 int r, g, b; /* temp variables */
1062 int i, top, left;
1064 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
1065 Red = 3, Green = 4, Blue = 5 };
1066 enum BaseColor current = Red;
1067 bool has_changed;
1069 char str[6] = "";
1071 restore_screen();
1073 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1075 while( 1 )
1077 has_changed = false;
1078 color = LCD_RGBPACK( red, green, blue );
1080 #define HEIGHT ( 100 )
1081 #define WIDTH ( 150 )
1083 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
1084 top -= 15;
1086 for( i=0; i<100; i++ )
1088 hsv2rgb( i*36, saturation, value, &r, &g, &b );
1089 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1090 rb->lcd_vline( left+15+i, top+20, top+27 );
1091 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
1092 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1093 rb->lcd_vline( left+15+i, top+30, top+37 );
1094 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
1095 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1096 rb->lcd_vline( left+15+i, top+40, top+47 );
1097 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
1098 rb->lcd_vline( left+15+i, top+50, top+57 );
1099 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
1100 rb->lcd_vline( left+15+i, top+60, top+67 );
1101 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
1102 rb->lcd_vline( left+15+i, top+70, top+77 );
1105 rb->lcd_set_foreground(COLOR_BLACK);
1106 #define POSITION( a, i ) \
1107 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
1108 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
1109 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
1110 rb->lcd_drawpixel( left+16+i, top + 28 + a );
1111 POSITION( 0, hue/36 );
1112 POSITION( 10, saturation*99/255 );
1113 POSITION( 20, value*99/255 );
1114 POSITION( 30, red*99/255 );
1115 POSITION( 40, green*99/255 );
1116 POSITION( 50, blue*99/255 );
1117 #undef POSITION
1118 rb->lcd_set_background(COLOR_LIGHTGRAY);
1119 rb->lcd_setfont( FONT_SYSFIXED );
1120 rb->snprintf( str, 6, "%d", hue/10 );
1121 rb->lcd_putsxy( left + 117, top + 20, str );
1122 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1123 rb->lcd_putsxy( left + 117, top + 30, str );
1124 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1125 rb->lcd_putsxy( left + 117, top + 40, str );
1126 rb->snprintf( str, 6, "%d", red );
1127 rb->lcd_putsxy( left + 117, top + 50, str );
1128 rb->snprintf( str, 6, "%d", green );
1129 rb->lcd_putsxy( left + 117, top + 60, str );
1130 rb->snprintf( str, 6, "%d", blue );
1131 rb->lcd_putsxy( left + 117, top + 70, str );
1132 rb->lcd_setfont( FONT_UI );
1134 #define CURSOR( l ) \
1135 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1136 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1137 CURSOR( 5 );
1138 #undef CURSOR
1140 rb->lcd_set_foreground( color );
1141 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1143 rb->lcd_update();
1145 switch( rb->button_get(true) )
1147 case ROCKPAINT_UP:
1148 current = ( current + 5 )%6;
1149 break;
1151 case ROCKPAINT_DOWN:
1152 current = (current + 1 )%6;
1153 break;
1155 case ROCKPAINT_LEFT:
1156 has_changed = true;
1157 switch( current )
1159 case Hue:
1160 hue = ( hue + 3600 - 10 )%3600;
1161 break;
1162 case Saturation:
1163 if( saturation ) saturation--;
1164 break;
1165 case Value:
1166 if( value ) value--;
1167 break;
1168 case Red:
1169 if( red ) red--;
1170 break;
1171 case Green:
1172 if( green ) green--;
1173 break;
1174 case Blue:
1175 if( blue ) blue--;
1176 break;
1178 break;
1180 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1181 has_changed = true;
1182 switch( current )
1184 case Hue:
1185 hue = ( hue + 3600 - 100 )%3600;
1186 break;
1187 case Saturation:
1188 if( saturation >= 8 ) saturation-=8;
1189 else saturation = 0;
1190 break;
1191 case Value:
1192 if( value >= 8 ) value-=8;
1193 else value = 0;
1194 break;
1195 case Red:
1196 if( red >= 8 ) red-=8;
1197 else red = 0;
1198 break;
1199 case Green:
1200 if( green >= 8 ) green-=8;
1201 else green = 0;
1202 break;
1203 case Blue:
1204 if( blue >= 8 ) blue-=8;
1205 else blue = 0;
1206 break;
1208 break;
1210 case ROCKPAINT_RIGHT:
1211 has_changed = true;
1212 switch( current )
1214 case Hue:
1215 hue = ( hue + 10 )%3600;
1216 break;
1217 case Saturation:
1218 if( saturation < 0xff ) saturation++;
1219 break;
1220 case Value:
1221 if( value < 0xff ) value++;
1222 break;
1223 case Red:
1224 if( red < 0xff ) red++;
1225 break;
1226 case Green:
1227 if( green < 0xff ) green++;
1228 break;
1229 case Blue:
1230 if( blue < 0xff ) blue++;
1231 break;
1233 break;
1235 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1236 has_changed = true;
1237 switch( current )
1239 case Hue:
1240 hue = ( hue + 100 )%3600;
1241 break;
1242 case Saturation:
1243 if( saturation < 0xff - 8 ) saturation+=8;
1244 else saturation = 0xff;
1245 break;
1246 case Value:
1247 if( value < 0xff - 8 ) value+=8;
1248 else value = 0xff;
1249 break;
1250 case Red:
1251 if( red < 0xff - 8 ) red+=8;
1252 else red = 0xff;
1253 break;
1254 case Green:
1255 if( green < 0xff - 8 ) green+=8;
1256 else green = 0xff;
1257 break;
1258 case Blue:
1259 if( blue < 0xff - 8 ) blue+=8;
1260 else blue = 0xff;
1261 break;
1263 break;
1265 case ROCKPAINT_DRAW:
1266 return color;
1268 if( has_changed )
1270 switch( current )
1272 case Hue:
1273 case Saturation:
1274 case Value:
1275 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1276 break;
1278 case Red:
1279 case Green:
1280 case Blue:
1281 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1282 break;
1285 #undef HEIGHT
1286 #undef WIDTH
1290 /***********************************************************************
1291 * Misc routines
1292 ***********************************************************************/
1293 static void init_buffer(void)
1295 int i;
1296 fb_data color = rp_colors[ bgdrawcolor ];
1297 for( i = 0; i < ROWS*COLS; i++ )
1299 save_buffer[i] = color;
1303 static void draw_pixel(int x,int y)
1305 if( !preview )
1307 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1308 if( isbg )
1310 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1312 else
1314 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1317 rb->lcd_drawpixel(x,y);
1320 static void color_picker( int x, int y )
1322 if( preview )
1324 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1325 #define PSIZE 12
1326 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1327 rb->lcd_drawrect( x<COLS-PSIZE ? x + 2 : x - PSIZE, y<ROWS-PSIZE ? y + 2: y - PSIZE, PSIZE - 2, PSIZE - 2 );
1328 rb->lcd_set_drawmode(DRMODE_SOLID);
1329 rb->lcd_fillrect( x<COLS-PSIZE ? x+3 : x - PSIZE+1, y<ROWS-PSIZE ? y +3: y - PSIZE+1, PSIZE-4, PSIZE-4 );
1330 #undef PSIZE
1331 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1333 else
1335 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1339 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1340 /* This is a preview mode only function */
1342 int i,a;
1343 if( x1 > x2 )
1345 i = x1;
1346 x1 = x2;
1347 x2 = i;
1349 if( y1 > y2 )
1351 i = y1;
1352 y1 = y2;
1353 y2 = i;
1355 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1356 i = 0;
1357 for( a = x1; a < x2; a++, i++ )
1358 if( i%2 )
1359 rb->lcd_drawpixel( a, y1 );
1360 for( a = y1; a < y2; a++, i++ )
1361 if( i%2 )
1362 rb->lcd_drawpixel( x2, a );
1363 if( y2 != y1 )
1364 for( a = x2; a > x1; a--, i++ )
1365 if( i%2 )
1366 rb->lcd_drawpixel( a, y2 );
1367 if( x2 != x1 )
1368 for( a = y2; a > y1; a--, i++ )
1369 if( i%2 )
1370 rb->lcd_drawpixel( x1, a );
1371 rb->lcd_set_drawmode(DRMODE_SOLID);
1374 static void copy_to_clipboard( void )
1376 /* This needs to be optimised ... but i'm lazy ATM */
1377 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1380 /* no preview mode handling atm ... do we need it ? (one if) */
1381 static void draw_invert( int x1, int y1, int x2, int y2 )
1383 int i;
1384 if( x1 > x2 )
1386 i = x1;
1387 x1 = x2;
1388 x2 = i;
1390 if( y1 > y2 )
1392 i = y1;
1393 y1 = y2;
1394 y2 = i;
1397 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1398 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1399 rb->lcd_set_drawmode(DRMODE_SOLID);
1401 for( ; y1<=y2; y1++ )
1403 for( i = x1; i<=x2; i++ )
1405 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1408 /*if( update )*/ rb->lcd_update();
1411 static void draw_hflip( int x1, int y1, int x2, int y2 )
1413 int i;
1414 if( x1 > x2 )
1416 i = x1;
1417 x1 = x2;
1418 x2 = i;
1420 if( y1 > y2 )
1422 i = y1;
1423 y1 = y2;
1424 y2 = i;
1427 copy_to_clipboard();
1429 for( i = 0; i <= y2 - y1; i++ )
1431 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1432 buffer.clipboard+(y2-i)*COLS+x1,
1433 (x2-x1+1)*sizeof( fb_data ) );
1435 restore_screen();
1436 rb->lcd_update();
1439 static void draw_vflip( int x1, int y1, int x2, int y2 )
1441 int i;
1442 if( x1 > x2 )
1444 i = x1;
1445 x1 = x2;
1446 x2 = i;
1448 if( y1 > y2 )
1450 i = y1;
1451 y1 = y2;
1452 y2 = i;
1455 copy_to_clipboard();
1457 for( ; y1 <= y2; y1++ )
1459 for( i = 0; i <= x2 - x1; i++ )
1461 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1464 restore_screen();
1465 rb->lcd_update();
1468 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1469 int src_y2, int x1, int y1, int mode )
1471 int i;
1472 if( mode == SELECT_MENU_CUT )
1474 i = drawcolor;
1475 drawcolor = bgdrawcolor;
1476 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1477 drawcolor = i;
1479 if( src_x1 > src_x2 )
1481 i = src_x1;
1482 src_x1 = src_x2;
1483 src_x2 = i;
1485 if( src_y1 > src_y2 )
1487 i = src_y1;
1488 src_y1 = src_y2;
1489 src_y2 = i;
1491 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1492 x1, y1, src_x2-src_x1+1, src_y2-src_y1+1 );
1493 if( !preview )
1495 for( i = 0; i <= src_y2 - src_y1; i++ )
1497 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1498 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1499 (src_x2 - src_x1 + 1)*sizeof( fb_data ) );
1504 static void show_grid( bool update )
1506 int i;
1507 if( gridsize > 0 )
1509 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1510 for( i = gridsize; i < COLS; i+= gridsize )
1512 rb->lcd_vline( i, 0, ROWS-1 );
1514 for( i = gridsize; i < ROWS; i+= gridsize )
1516 rb->lcd_hline( 0, COLS-1, i );
1518 rb->lcd_set_drawmode(DRMODE_SOLID);
1519 if( update ) rb->lcd_update();
1523 static void draw_text( int x, int y )
1525 buffer.text.text[0] = '\0';
1526 rb->snprintf( buffer.text.old_font, MAX_PATH,
1527 FONT_DIR "/%s.fnt",
1528 rb->global_settings->font_file );
1529 while( 1 )
1531 int m = TEXT_MENU_TEXT;
1532 switch( m = menu_display( text_menu, m ) )
1534 case TEXT_MENU_TEXT:
1535 rb->kbd_input( buffer.text.text, MAX_TEXT );
1536 restore_screen();
1537 break;
1539 case TEXT_MENU_FONT:
1540 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1542 rb->font_load( buffer.text.font );
1544 break;
1546 case TEXT_MENU_PREVIEW:
1547 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1548 while( 1 )
1550 unsigned int button;
1551 restore_screen();
1552 rb->lcd_putsxy( x, y, buffer.text.text );
1553 rb->lcd_update();
1554 switch( button = rb->button_get( true ) )
1556 case ROCKPAINT_LEFT:
1557 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1558 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1559 if (x<0) x=COLS-1;
1560 break;
1562 case ROCKPAINT_RIGHT:
1563 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1564 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1565 if (x>=COLS) x=0;
1566 break;
1568 case ROCKPAINT_UP:
1569 case ROCKPAINT_UP | BUTTON_REPEAT:
1570 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1571 if (y<0) y=ROWS-1;
1572 break;
1574 case ROCKPAINT_DOWN:
1575 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1576 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1577 if (y>=ROWS-1) y=0;
1578 break;
1580 case ROCKPAINT_DRAW:
1581 button = 1242;
1582 break;
1584 if( button == 1242 ) break;
1586 break;
1588 case TEXT_MENU_APPLY:
1589 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1590 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1591 buffer.text.text );
1592 case TEXT_MENU_CANCEL:
1593 restore_screen();
1594 rb->font_load( buffer.text.old_font );
1595 return;
1600 static void draw_brush( int x, int y )
1602 int i,j;
1603 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1605 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1607 draw_pixel( x+i, y+j );
1612 /* This is an implementation of Bresenham's line algorithm.
1613 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1615 static void draw_line( int x1, int y1, int x2, int y2 )
1617 int x = x1;
1618 int y = y1;
1619 int deltax = x2 - x1;
1620 int deltay = y2 - y1;
1621 int i;
1623 int xerr = abs(deltax);
1624 int yerr = abs(deltay);
1625 int xstep = deltax > 0 ? 1 : -1;
1626 int ystep = deltay > 0 ? 1 : -1;
1627 int err;
1629 if (yerr > xerr)
1631 /* more vertical */
1632 err = yerr;
1633 xerr <<= 1;
1634 yerr <<= 1;
1636 /* to leave off the last pixel of the line, leave off the "+ 1" */
1637 for (i = abs(deltay) + 1; i; --i)
1639 draw_pixel(x, y);
1640 y += ystep;
1641 err -= xerr;
1642 if (err < 0) {
1643 x += xstep;
1644 err += yerr;
1648 else
1650 /* more horizontal */
1651 err = xerr;
1652 xerr <<= 1;
1653 yerr <<= 1;
1655 for (i = abs(deltax) + 1; i; --i)
1657 draw_pixel(x, y);
1658 x += xstep;
1659 err -= yerr;
1660 if (err < 0) {
1661 y += ystep;
1662 err += xerr;
1668 static void draw_curve( int x1, int y1, int x2, int y2,
1669 int xa, int ya, int xb, int yb )
1671 int i = 0;
1672 short xl1, yl1;
1673 short xl2, yl2;
1674 short xl3, yl3;
1675 short xl4, yl4;
1676 short xr1, yr1;
1677 short xr2, yr2;
1678 short xr3, yr3;
1679 short xr4, yr4;
1680 short depth;
1681 short xh, yh;
1683 if( x1 == x2 && y1 == y2 )
1685 draw_pixel( x1, y1 );
1686 return;
1689 // if( preview )
1691 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1692 if( xa == -1 || ya == -1 )
1694 rb->lcd_drawline( x1, y1, xb, yb );
1695 rb->lcd_drawline( x2, y2, xb, yb );
1697 else
1699 rb->lcd_drawline( x1, y1, xa, ya );
1700 rb->lcd_drawline( x2, y2, xb, yb );
1702 rb->lcd_set_drawmode(DRMODE_SOLID);
1705 if( xa == -1 || ya == -1 )
1706 /* We only have 3 of the points
1707 * This will currently only be used in preview mode */
1709 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1710 buffer.bezier[i].x1 = a1; \
1711 buffer.bezier[i].y1 = b1; \
1712 buffer.bezier[i].x2 = a2; \
1713 buffer.bezier[i].y2 = b2; \
1714 buffer.bezier[i].x3 = a3; \
1715 buffer.bezier[i].y3 = b3; \
1716 buffer.bezier[i].depth = d; \
1717 i++;
1718 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1719 i--; \
1720 a1 = buffer.bezier[i].x1; \
1721 b1 = buffer.bezier[i].y1; \
1722 a2 = buffer.bezier[i].x2; \
1723 b2 = buffer.bezier[i].y2; \
1724 a3 = buffer.bezier[i].x3; \
1725 b3 = buffer.bezier[i].y3; \
1726 d = buffer.bezier[i].depth;
1727 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1728 while( i )
1730 /* de Casteljau's algorithm (see wikipedia) */
1731 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1732 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1734 xl2 = ( xl1 + xb )>>1;
1735 yl2 = ( yl1 + yb )>>1;
1736 xr2 = ( xb + xr3 )>>1;
1737 yr2 = ( yb + yr3 )>>1;
1738 xr1 = ( xl2 + xr2 )>>1;
1739 yr1 = ( yl2 + yr2 )>>1;
1740 xl3 = xr1;
1741 yl3 = yr1;
1742 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1743 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1745 else
1747 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1748 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1751 #undef PUSH
1752 #undef POP
1754 else /* We have the 4 points */
1756 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1757 buffer.bezier[i].x1 = a1; \
1758 buffer.bezier[i].y1 = b1; \
1759 buffer.bezier[i].x2 = a2; \
1760 buffer.bezier[i].y2 = b2; \
1761 buffer.bezier[i].x3 = a3; \
1762 buffer.bezier[i].y3 = b3; \
1763 buffer.bezier[i].x4 = a4; \
1764 buffer.bezier[i].y4 = b4; \
1765 buffer.bezier[i].depth = d; \
1766 i++;
1767 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1768 i--; \
1769 a1 = buffer.bezier[i].x1; \
1770 b1 = buffer.bezier[i].y1; \
1771 a2 = buffer.bezier[i].x2; \
1772 b2 = buffer.bezier[i].y2; \
1773 a3 = buffer.bezier[i].x3; \
1774 b3 = buffer.bezier[i].y3; \
1775 a4 = buffer.bezier[i].x4; \
1776 b4 = buffer.bezier[i].y4; \
1777 d = buffer.bezier[i].depth;
1779 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1780 while( i )
1782 /* de Casteljau's algorithm (see wikipedia) */
1783 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1784 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1786 xl2 = ( xl1 + xa )>>1;
1787 yl2 = ( yl1 + ya )>>1;
1788 xh = ( xa + xb )>>1;
1789 yh = ( ya + yb )>>1;
1790 xr3 = ( xb + xr4 )>>1;
1791 yr3 = ( yb + yr4 )>>1;
1792 xl3 = ( xl2 + xh )>>1;
1793 yl3 = ( yl2 + yh )>>1;
1794 xr2 = ( xr3 + xh )>>1;
1795 yr2 = ( yr3 + yh )>>1;
1796 xl4 = ( xl3 + xr2 )>>1;
1797 yl4 = ( yl3 + yr2 )>>1;
1798 xr1 = xl4;
1799 yr1 = yl4;
1800 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1801 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1803 else
1805 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1806 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1809 #undef PUSH
1810 #undef POP
1814 static void draw_rect( int x1, int y1, int x2, int y2 )
1816 draw_line( x1, y1, x1, y2 );
1817 draw_line( x1, y1, x2, y1 );
1818 draw_line( x1, y2, x2, y2 );
1819 draw_line( x2, y1, x2, y2 );
1822 static void togglebg( void )
1824 if( isbg )
1826 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1828 else
1830 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1832 isbg = !isbg;
1835 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1837 /* GRUIK */
1838 int x = x1;
1839 togglebg();
1840 if( x < x2 )
1842 do {
1843 draw_line( x, y1, x, y2 );
1844 } while( ++x <= x2 );
1846 else
1848 do {
1849 draw_line( x, y1, x, y2 );
1850 } while( --x >= x2 );
1852 togglebg();
1853 draw_rect( x1, y1, x2, y2 );
1856 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1858 /* TODO: simplify :) */
1859 int cx = (x1+x2)>>1;
1860 int cy = (y1+y2)>>1;
1862 int rx = (x1-x2)>>1;
1863 int ry = (y1-y2)>>1;
1864 if( rx < 0 ) rx *= -1;
1865 if( ry < 0 ) ry *= -1;
1867 if( rx == 0 || ry == 0 )
1869 draw_line( x1, y1, x2, y2 );
1870 return;
1873 int x,y;
1874 int dst, old_dst;
1876 for( x = 0; x < rx; x++ )
1878 y = 0;
1879 dst = -0xfff;
1880 do {
1881 old_dst = dst;
1882 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1883 y++;
1884 } while( dst < 0 );
1885 if( -old_dst < dst ) y--;
1886 if( full )
1888 draw_line( cx+x, cy, cx+x, cy+y );
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 );
1893 else
1895 draw_pixel( cx+x, cy+y );
1896 draw_pixel( cx+x, cy-y );
1897 draw_pixel( cx-x, cy+y );
1898 draw_pixel( cx-x, cy-y );
1901 for( y = 0; y < ry; y++ )
1903 x = 0;
1904 dst = -0xfff;
1905 do {
1906 old_dst = dst;
1907 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1908 x++;
1909 } while( dst < 0 );
1910 if( -old_dst < dst ) x--;
1911 if( full )
1913 draw_line( cx+x, cy, cx+x, cy+y );
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 );
1918 else
1920 draw_pixel( cx+x, cy+y );
1921 draw_pixel( cx+x, cy-y );
1922 draw_pixel( cx-x, cy+y );
1923 draw_pixel( cx-x, cy-y );
1928 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1930 draw_oval( x1, y1, x2, y2, false );
1933 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1935 togglebg();
1936 draw_oval( x1, y1, x2, y2, true );
1937 togglebg();
1938 draw_oval( x1, y1, x2, y2, false );
1941 static void draw_fill( int x0, int y0 )
1943 #define PUSH( a, b ) \
1944 draw_pixel( (int)a, (int)b ); \
1945 buffer.coord[i].x = a; \
1946 buffer.coord[i].y = b; \
1947 i++;
1948 #define POP( a, b ) \
1949 i--; \
1950 a = buffer.coord[i].x; \
1951 b = buffer.coord[i].y;
1953 unsigned int i=0;
1954 short x = x0;
1955 short y = y0;
1956 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1958 if( prev_color == rp_colors[ drawcolor ] ) return;
1960 PUSH( x, y );
1962 while( i != 0 )
1964 POP( x, y );
1965 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1967 PUSH( x-1, y );
1969 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1971 PUSH( x+1, y );
1973 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1975 PUSH( x, y-1 );
1977 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1979 PUSH( x, y+1 );
1982 #undef PUSH
1983 #undef POP
1987 /* For preview purposes only */
1988 static void line_gradient( int x1, int y1, int x2, int y2 )
1990 int r1, g1, b1;
1991 int r2, g2, b2;
1992 int h1, s1, v1, h2, s2, v2, r, g, b;
1993 int w, h, x, y;
1995 bool a = false;
1997 x1 <<= 1;
1998 y1 <<= 1;
1999 x2 <<= 1;
2000 y2 <<= 1;
2002 w = x1 - x2;
2003 h = y1 - y2;
2005 if( w == 0 && h == 0 )
2007 draw_pixel( x1>>1, y1>>1 );
2008 return;
2011 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2012 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2013 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2014 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2015 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2016 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2018 if( w < 0 )
2020 w *= -1;
2021 a = true;
2023 if( h < 0 )
2025 h *= -1;
2026 a = !a;
2028 if( a )
2030 r = r1;
2031 r1 = r2;
2032 r2 = r;
2033 g = g1;
2034 g1 = g2;
2035 g2 = g;
2036 b = b1;
2037 b1 = b2;
2038 b2 = b;
2041 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2042 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2044 if( w > h )
2046 if( x1 > x2 )
2048 x = x2;
2049 y = y2;
2050 x2 = x1;
2051 y2 = y1;
2052 x1 = x;
2053 y1 = y;
2055 w = x1 - x2;
2056 h = y1 - y2;
2057 while( x1 <= x2 )
2059 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2060 s1+((s2-s1)*(x1-x2))/w,
2061 v1+((v2-v1)*(x1-x2))/w,
2062 &r, &g, &b );
2063 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2064 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2065 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2066 x1+=2;
2067 y1 = y2 - ( x2 - x1 ) * h / w;
2070 else /* h > w */
2072 if( y1 > y2 )
2074 x = x2;
2075 y = y2;
2076 x2 = x1;
2077 y2 = y1;
2078 x1 = x;
2079 y1 = y;
2081 w = x1 - x2;
2082 h = y1 - y2;
2083 while( y1 <= y2 )
2085 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2086 s1+((s2-s1)*(y1-y2))/h,
2087 v1+((v2-v1)*(y1-y2))/h,
2088 &r, &g, &b );
2089 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2090 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2091 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2092 y1+=2;
2093 x1 = x2 - ( y2 - y1 ) * w / h;
2096 if( a )
2098 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2100 else
2102 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2106 static void linear_gradient( int x1, int y1, int x2, int y2 )
2108 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2109 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2110 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2111 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2112 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2113 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2115 int h1, s1, v1, h2, s2, v2, r, g, b;
2117 /* radius^2 */
2118 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2119 int dist2, i=0;
2121 /* We only propagate the gradient to neighboring pixels with the same
2122 * color as ( x1, y1 ) */
2123 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2125 int x = x1;
2126 int y = y1;
2128 if( radius2 == 0 ) return;
2129 if( preview )
2131 line_gradient( x1, y1, x2, y2 );
2134 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2135 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2137 #define PUSH( x0, y0 ) \
2138 buffer.coord[i].x = (short)(x0); \
2139 buffer.coord[i].y = (short)(y0); \
2140 i++;
2141 #define POP( a, b ) \
2142 i--; \
2143 a = (int)buffer.coord[i].x; \
2144 b = (int)buffer.coord[i].y;
2146 PUSH( x, y );
2148 while( i != 0 )
2150 POP( x, y );
2152 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2153 if( dist2 <= 0 )
2155 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2157 else if( dist2 < radius2 )
2159 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2160 s1+((s2-s1)*dist2)/radius2,
2161 v1+((v2-v1)*dist2)/radius2,
2162 &r, &g, &b );
2163 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2165 else
2167 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2169 if( rp_colors[ drawcolor ] == prev_color )
2171 if( rp_colors[ drawcolor ])
2172 rp_colors[ drawcolor ]--; /* GRUIK */
2173 else
2174 rp_colors[ drawcolor ]++; /* GRUIK */
2176 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2177 draw_pixel( x, y );
2179 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2181 PUSH( x-1, y );
2183 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2185 PUSH( x+1, y );
2187 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2189 PUSH( x, y-1 );
2191 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2193 PUSH( x, y+1 );
2196 #undef PUSH
2197 #undef POP
2199 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2202 static void radial_gradient( int x1, int y1, int x2, int y2 )
2204 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2205 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2206 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2207 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2208 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2209 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2211 int h1, s1, v1, h2, s2, v2, r, g, b;
2213 /* radius^2 */
2214 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2215 int dist2, i=0;
2217 /* We only propagate the gradient to neighboring pixels with the same
2218 * color as ( x1, y1 ) */
2219 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2221 int x = x1;
2222 int y = y1;
2224 if( radius2 == 0 ) return;
2225 if( preview )
2227 line_gradient( x1, y1, x2, y2 );
2230 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2231 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2233 #define PUSH( x0, y0 ) \
2234 buffer.coord[i].x = (short)(x0); \
2235 buffer.coord[i].y = (short)(y0); \
2236 i++;
2237 #define POP( a, b ) \
2238 i--; \
2239 a = (int)buffer.coord[i].x; \
2240 b = (int)buffer.coord[i].y;
2242 PUSH( x, y );
2244 while( i != 0 )
2246 POP( x, y );
2248 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2250 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2251 s1+((s2-s1)*dist2)/radius2,
2252 v1+((v2-v1)*dist2)/radius2,
2253 &r, &g, &b );
2254 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2256 else
2258 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2260 if( rp_colors[ drawcolor ] == prev_color )
2262 if( rp_colors[ drawcolor ])
2263 rp_colors[ drawcolor ]--; /* GRUIK */
2264 else
2265 rp_colors[ drawcolor ]++; /* GRUIK */
2267 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2268 draw_pixel( x, y );
2270 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2272 PUSH( x-1, y );
2274 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2276 PUSH( x+1, y );
2278 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2280 PUSH( x, y-1 );
2282 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2284 PUSH( x, y+1 );
2287 #undef PUSH
2288 #undef POP
2290 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2293 static void draw_toolbars(bool update)
2295 int i;
2296 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2297 rb->lcd_set_background( COLOR_LIGHTGRAY );
2298 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2299 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2300 rb->lcd_set_foreground( COLOR_BLACK );
2301 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2303 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2304 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2305 TB_SC_SIZE, TB_SC_SIZE );
2306 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2307 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2308 TB_SC_SIZE, TB_SC_SIZE );
2309 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2310 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2311 TB_SC_SIZE, TB_SC_SIZE );
2312 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2313 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2314 TB_SC_SIZE, TB_SC_SIZE );
2316 for( i=0; i<18; i++ )
2318 rb->lcd_set_foreground( rp_colors[i] );
2319 rb->lcd_fillrect(
2320 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2321 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2322 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2323 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2324 rb->lcd_drawrect(
2325 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2326 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2327 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2330 #define SEPARATOR( x, y ) \
2331 rb->lcd_set_foreground( COLOR_WHITE ); \
2332 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2333 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2334 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2335 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2337 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2338 TB_TL_WIDTH, TB_TL_HEIGHT );
2339 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2340 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2341 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2342 TB_TL_SIZE, TB_TL_SIZE );
2344 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2346 rb->lcd_setfont( FONT_SYSFIXED );
2347 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2348 rb->lcd_setfont( FONT_UI );
2349 #undef TOP
2351 if( update ) rb->lcd_update();
2354 static void toolbar( void )
2356 int button, i, j;
2357 restore_screen();
2358 draw_toolbars( false );
2359 y = LCD_HEIGHT-TB_HEIGHT/2;
2360 inv_cursor( true );
2361 while( 1 )
2363 switch( button = rb->button_get( true ) )
2365 case ROCKPAINT_DRAW:
2366 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2367 if( y >= TOP + TB_SC_FG_TOP
2368 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2369 && x >= TB_SC_FG_LEFT
2370 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2372 /* click on the foreground color */
2373 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2375 else if( y >= TOP + TB_SC_BG_TOP
2376 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2377 && x >= TB_SC_BG_LEFT
2378 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2380 /* click on the background color */
2381 i = drawcolor;
2382 drawcolor = bgdrawcolor;
2383 bgdrawcolor = i;
2385 else if( y >= TOP + TB_PL_TOP
2386 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2387 && x >= TB_PL_LEFT
2388 && x < TB_PL_LEFT + TB_PL_WIDTH )
2390 /* click on the palette */
2391 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2392 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2393 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2394 break;
2395 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2396 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2397 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2399 else if( y >= TOP+TB_TL_TOP
2400 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2401 && x >= TB_TL_LEFT
2402 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2404 /* click on the tools */
2405 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2406 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2407 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2408 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2409 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2410 tool = i*2+j;
2411 prev_x = -1;
2412 prev_y = -1;
2413 prev_x2 = -1;
2414 prev_y2 = -1;
2415 prev_x3 = -1;
2416 prev_y3 = -1;
2417 preview = false;
2419 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2421 /* menu button */
2422 goto_menu();
2424 #undef TOP
2425 restore_screen();
2426 draw_toolbars( false );
2427 inv_cursor( true );
2428 break;
2430 case ROCKPAINT_LEFT:
2431 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2432 inv_cursor(false);
2433 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2434 if (x<0) x=COLS-1;
2435 inv_cursor(true);
2436 break;
2438 case ROCKPAINT_RIGHT:
2439 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2440 inv_cursor(false);
2441 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2442 if (x>=COLS) x=0;
2443 inv_cursor(true);
2444 break;
2446 case ROCKPAINT_UP:
2447 case ROCKPAINT_UP | BUTTON_REPEAT:
2448 inv_cursor(false);
2449 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2450 if (y<LCD_HEIGHT-TB_HEIGHT)
2452 return;
2454 inv_cursor(true);
2455 break;
2457 case ROCKPAINT_DOWN:
2458 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2459 inv_cursor(false);
2460 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2461 if (y>=LCD_HEIGHT)
2463 y = 0;
2464 return;
2466 inv_cursor(true);
2467 break;
2469 case ROCKPAINT_TOOLBAR:
2470 case ROCKPAINT_TOOLBAR2:
2471 return;
2473 if( quit ) return;
2477 static void inv_cursor(bool update)
2479 rb->lcd_set_foreground(COLOR_BLACK);
2480 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2481 /* cross painting */
2482 rb->lcd_hline(x-4,x-1,y);
2483 rb->lcd_hline(x+1,x+4,y);
2484 rb->lcd_vline(x,y-4,y-1);
2485 rb->lcd_vline(x,y+1,y+4);
2486 rb->lcd_set_foreground(rp_colors[drawcolor]);
2487 rb->lcd_set_drawmode(DRMODE_SOLID);
2489 if( update ) rb->lcd_update();
2492 static void restore_screen(void)
2494 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2497 static void clear_drawing(void)
2499 init_buffer();
2500 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2501 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2502 rb->lcd_update();
2505 static void goto_menu(void)
2507 int multi;
2509 while( 1 )
2511 switch( menu_display( main_menu, 1 ) )
2513 case MAIN_MENU_NEW:
2514 clear_drawing();
2515 return;
2517 case MAIN_MENU_LOAD:
2518 if( browse( filename, MAX_PATH, "/" ) )
2520 if( load_bitmap( filename ) <= 0 )
2522 rb->splashf( 1*HZ, "Error while loading %s",
2523 filename );
2525 else
2527 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2528 restore_screen();
2529 inv_cursor(true);
2530 return;
2533 break;
2535 case MAIN_MENU_SAVE:
2536 if (!filename[0])
2537 rb->strcpy(filename,"/");
2538 if( !rb->kbd_input( filename, MAX_PATH ) )
2540 if(rb->strlen(filename) <= 4 ||
2541 rb->strcasecmp(&filename[rb->strlen(filename)-4], ".bmp"))
2542 rb->strcat(filename, ".bmp");
2543 save_bitmap( filename );
2544 rb->splashf( 1*HZ, "File saved (%s)", filename );
2546 break;
2548 case MAIN_MENU_BRUSH_SIZE:
2549 multi = menu_display( size_menu, bsize );
2550 if( multi != - 1 )
2551 bsize = multi;
2552 break;
2554 case MAIN_MENU_BRUSH_SPEED:
2555 multi = menu_display( speed_menu, bspeed );
2556 if( multi != -1 )
2557 bspeed = multi;
2558 break;
2560 case MAIN_MENU_COLOR:
2561 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2562 break;
2564 case MAIN_MENU_GRID_SIZE:
2565 multi = menu_display( gridsize_menu, gridsize );
2566 if( multi != - 1 )
2567 gridsize = multi;
2568 break;
2570 case MAIN_MENU_EXIT:
2571 quit=true;
2572 return;
2574 case MAIN_MENU_RESUME:
2575 case MENU_ESC:
2576 return;
2577 }/* end switch */
2578 }/* end while */
2581 static void reset_tool( void )
2583 prev_x = -1;
2584 prev_y = -1;
2585 prev_x2 = -1;
2586 prev_y2 = -1;
2587 prev_x3 = -1;
2588 prev_y3 = -1;
2589 tool_mode = -1;
2590 preview = false;
2593 static bool rockpaint_loop( void )
2595 int button=0,i,j;
2596 int accelaration;
2598 while (!quit) {
2599 button = rb->button_get(true);
2601 if( tool == Brush && prev_x != -1 )
2603 accelaration = 1;
2605 else if( button & BUTTON_REPEAT )
2607 accelaration = 4;
2609 else
2611 accelaration = 1;
2614 switch(button)
2616 case ROCKPAINT_QUIT:
2617 rb->lcd_set_drawmode(DRMODE_SOLID);
2618 return PLUGIN_OK;
2620 case ROCKPAINT_MENU:
2621 inv_cursor(false);
2622 goto_menu();
2623 inv_cursor(true);
2624 break;
2626 case ROCKPAINT_DRAW:
2627 inv_cursor(false);
2628 switch( tool )
2630 case Brush:
2631 if( prev_x == -1 ) prev_x = 1;
2632 else prev_x = -1;
2633 break;
2635 case SelectRectangle:
2636 case Line:
2637 case Curve:
2638 case Rectangle:
2639 case RectangleFull:
2640 case Oval:
2641 case OvalFull:
2642 case LinearGradient:
2643 case RadialGradient:
2644 /* Curve uses 4 points, others use 2 */
2645 if( prev_x == -1 || prev_y == -1 )
2647 prev_x = x;
2648 prev_y = y;
2649 preview = true;
2651 else if( tool == Curve
2652 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2654 prev_x2 = x;
2655 prev_y2 = y;
2657 else if( tool == SelectRectangle
2658 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2660 tool_mode = menu_display( select_menu,
2661 SELECT_MENU_CUT );
2662 switch( tool_mode )
2664 case SELECT_MENU_CUT:
2665 case SELECT_MENU_COPY:
2666 prev_x2 = x;
2667 prev_y2 = y;
2668 copy_to_clipboard();
2669 if( prev_x < x ) x = prev_x;
2670 if( prev_y < y ) y = prev_y;
2671 break;
2673 case SELECT_MENU_INVERT:
2674 draw_invert( prev_x, prev_y, x, y );
2675 reset_tool();
2676 break;
2678 case SELECT_MENU_HFLIP:
2679 draw_hflip( prev_x, prev_y, x, y );
2680 reset_tool();
2681 break;
2683 case SELECT_MENU_VFLIP:
2684 draw_vflip( prev_x, prev_y, x, y );
2685 reset_tool();
2686 break;
2688 case SELECT_MENU_ROTATE90:
2689 break;
2690 case SELECT_MENU_ROTATE180:
2691 draw_hflip( prev_x, prev_y, x, y );
2692 draw_vflip( prev_x, prev_y, x, y );
2693 reset_tool();
2694 break;
2695 case SELECT_MENU_ROTATE270:
2696 break;
2698 case SELECT_MENU_CANCEL:
2699 reset_tool();
2700 break;
2702 case MENU_ESC:
2703 break;
2706 else if( tool == Curve
2707 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2709 prev_x3 = x;
2710 prev_y3 = y;
2712 else
2714 preview = false;
2715 switch( tool )
2717 case SelectRectangle:
2718 draw_paste_rectangle( prev_x, prev_y,
2719 prev_x2, prev_y2,
2720 x, y, tool_mode );
2721 break;
2722 case Line:
2723 draw_line( prev_x, prev_y, x, y );
2724 break;
2725 case Curve:
2726 draw_curve( prev_x, prev_y,
2727 prev_x2, prev_y2,
2728 prev_x3, prev_y3,
2729 x, y );
2730 break;
2731 case Rectangle:
2732 draw_rect( prev_x, prev_y, x, y );
2733 break;
2734 case RectangleFull:
2735 draw_rect_full( prev_x, prev_y, x, y );
2736 break;
2737 case Oval:
2738 draw_oval_empty( prev_x, prev_y, x, y );
2739 break;
2740 case OvalFull:
2741 draw_oval_full( prev_x, prev_y, x, y );
2742 break;
2743 case LinearGradient:
2744 linear_gradient( prev_x, prev_y, x, y );
2745 break;
2746 case RadialGradient:
2747 radial_gradient( prev_x, prev_y, x, y );
2748 break;
2749 default:
2750 break;
2752 reset_tool();
2754 break;
2756 case Fill:
2757 draw_fill( x, y );
2758 break;
2760 case ColorPicker:
2761 color_picker( x, y );
2762 break;
2764 case Text:
2765 draw_text( x, y );
2766 break;
2768 default:
2769 break;
2771 inv_cursor(true);
2772 break;
2774 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2775 if( tool == Curve )
2777 /* 3 point bezier curve */
2778 preview = false;
2779 draw_curve( prev_x, prev_y,
2780 prev_x2, prev_y2,
2781 -1, -1,
2782 x, y );
2783 reset_tool();
2784 restore_screen();
2785 inv_cursor( true );
2787 break;
2789 case ROCKPAINT_TOOLBAR:
2790 i = x; j = y;
2791 x = 10;
2792 toolbar();
2793 x = i; y = j;
2794 restore_screen();
2795 inv_cursor(true);
2796 break;
2798 case ROCKPAINT_TOOLBAR2:
2799 i = x; j = y;
2800 x = 110;
2801 toolbar();
2802 x = i; y = j;
2803 restore_screen();
2804 inv_cursor(true);
2805 break;
2807 case ROCKPAINT_LEFT:
2808 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2809 inv_cursor(false);
2810 x-=bspeed * accelaration;
2811 if (x<0) x=COLS-1;
2812 inv_cursor(true);
2813 break;
2815 case ROCKPAINT_RIGHT:
2816 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2817 inv_cursor(false);
2818 x+=bspeed * accelaration;
2819 if (x>=COLS) x=0;
2820 inv_cursor(true);
2821 break;
2823 case ROCKPAINT_UP:
2824 case ROCKPAINT_UP | BUTTON_REPEAT:
2825 inv_cursor(false);
2826 y-=bspeed * accelaration;
2827 if (y<0) y=ROWS-1;
2828 inv_cursor(true);
2829 break;
2831 case ROCKPAINT_DOWN:
2832 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2833 inv_cursor(false);
2834 y+=bspeed * accelaration;
2835 if (y>=ROWS)
2837 toolbar();
2838 restore_screen();
2840 inv_cursor(true);
2841 break;
2843 default:
2844 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2845 return PLUGIN_USB_CONNECTED;
2846 break;
2848 if( tool == Brush && prev_x == 1 )
2850 inv_cursor(false);
2851 draw_brush( x, y );
2852 inv_cursor(true);
2854 if( preview || tool == ColorPicker )
2855 /* always preview color picker */
2857 restore_screen();
2858 switch( tool )
2860 case SelectRectangle:
2861 if( prev_x2 == -1 || prev_y2 == -1 )
2863 /* we are defining the selection */
2864 draw_select_rectangle( prev_x, prev_y, x, y );
2866 else
2868 /* we are pasting the selected data */
2869 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2870 prev_y2, x, y, tool_mode );
2871 prev_x3 = prev_x2-prev_x;
2872 if( prev_x3 < 0 ) prev_x3 *= -1;
2873 prev_y3 = prev_y2-prev_y;
2874 if( prev_y3 < 0 ) prev_y3 *= -1;
2875 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2876 prev_x3 = -1;
2877 prev_y3 = -1;
2879 break;
2881 case Brush:
2882 break;
2884 case Line:
2885 draw_line( prev_x, prev_y, x, y );
2886 break;
2888 case Curve:
2889 if( prev_x2 == -1 || prev_y2 == -1 )
2891 draw_line( prev_x, prev_y, x, y );
2893 else
2895 draw_curve( prev_x, prev_y,
2896 prev_x2, prev_y2,
2897 prev_x3, prev_y3,
2898 x, y );
2900 break;
2902 case Rectangle:
2903 draw_rect( prev_x, prev_y, x, y );
2904 break;
2906 case RectangleFull:
2907 draw_rect_full( prev_x, prev_y, x, y );
2908 break;
2910 case Oval:
2911 draw_oval_empty( prev_x, prev_y, x, y );
2912 break;
2914 case OvalFull:
2915 draw_oval_full( prev_x, prev_y, x, y );
2916 break;
2918 case Fill:
2919 break;
2921 case ColorPicker:
2922 preview = true;
2923 color_picker( x, y );
2924 preview = false;
2925 break;
2927 case LinearGradient:
2928 line_gradient( prev_x, prev_y, x, y );
2929 break;
2931 case RadialGradient:
2932 line_gradient( prev_x, prev_y, x, y );
2933 break;
2935 case Text:
2936 default:
2937 break;
2939 inv_cursor( true );
2941 if( gridsize > 0 )
2943 show_grid( true );
2944 show_grid( false );
2948 return PLUGIN_OK;
2951 static int load_bitmap( const char *file )
2953 struct bitmap bm;
2954 bool ret;
2955 int l;
2957 bm.data = (char*)save_buffer;
2958 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2959 FORMAT_NATIVE );
2961 if((bm.width > COLS ) || ( bm.height > ROWS ))
2962 return -1;
2964 for( l = bm.height-1; l > 0; l-- )
2966 rb->memmove( save_buffer+l*COLS, save_buffer+l*bm.width,
2967 sizeof( fb_data )*bm.width );
2969 for( l = 0; l < bm.height; l++ )
2971 rb->memset( save_buffer+l*COLS+bm.width, rp_colors[ bgdrawcolor ],
2972 sizeof( fb_data )*(COLS-bm.width) );
2974 rb->memset( save_buffer+COLS*bm.height, rp_colors[ bgdrawcolor ],
2975 sizeof( fb_data )*COLS*(ROWS-bm.height) );
2977 return ret;
2980 static int save_bitmap( char *file )
2982 struct bitmap bm;
2983 bm.data = (char*)save_buffer;
2984 bm.height = ROWS;
2985 bm.width = COLS;
2986 bm.format = FORMAT_NATIVE;
2987 return save_bmp_file( file, &bm, rb );
2990 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
2992 /** must have stuff **/
2993 rb = api;
2995 rb->lcd_set_foreground(COLOR_WHITE);
2996 rb->lcd_set_backdrop(NULL);
2997 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
2998 rb->splash( HZ/2, "Rock Paint");
3000 rb->lcd_clear_display();
3002 filename[0] = '\0';
3004 if( parameter )
3006 if( load_bitmap( parameter ) <= 0 )
3008 rb->splash( 1*HZ, "File Open Error");
3009 clear_drawing();
3011 else
3013 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
3014 restore_screen();
3015 rb->strcpy( filename, parameter );
3018 else
3020 clear_drawing();
3022 inv_cursor(true);
3024 return rockpaint_loop();