Introduce __attribute__((unused)) (#defined to UNUSED_ATTR) to mark possibly unused...
[kugel-rb.git] / apps / plugins / rockpaint.c
blob804daf3ccd1e646b4569f65dd033ba571e23de75
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Antoine Cellerier <dionoea -at- videolan -dot- org>
11 * Based on parts of rockpaint 0.45, Copyright (C) 2005 Eli Sherer
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 /**
24 * TODO:
25 * - implement 2 layers with alpha colors
26 * - take brush width into account when drawing shapes
27 * - handle bigger than screen bitmaps
28 * - cache fonts when building the font preview (else it only works well on simulators because they have "fast" disk read)
31 #include "plugin.h"
32 #include "lib/pluginlib_bmp.h"
33 #include "lib/rgb_hsv.h"
34 #include "lib/playback_control.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 == SANSA_FUZE_PAD)
100 #define ROCKPAINT_QUIT (BUTTON_HOME|BUTTON_REPEAT)
101 #define ROCKPAINT_DRAW BUTTON_SELECT
102 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_DOWN )
103 #define ROCKPAINT_TOOLBAR ( BUTTON_SELECT | BUTTON_LEFT )
104 #define ROCKPAINT_TOOLBAR2 ( BUTTON_SELECT | BUTTON_RIGHT )
105 #define ROCKPAINT_UP BUTTON_UP
106 #define ROCKPAINT_DOWN BUTTON_DOWN
107 #define ROCKPAINT_LEFT BUTTON_LEFT
108 #define ROCKPAINT_RIGHT BUTTON_RIGHT
110 #elif ( CONFIG_KEYPAD == IRIVER_H10_PAD )
111 #define ROCKPAINT_QUIT BUTTON_POWER
112 #define ROCKPAINT_DRAW BUTTON_FF
113 #define ROCKPAINT_MENU BUTTON_PLAY
114 #define ROCKPAINT_TOOLBAR BUTTON_REW
115 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
116 #define ROCKPAINT_UP BUTTON_SCROLL_UP
117 #define ROCKPAINT_DOWN BUTTON_SCROLL_DOWN
118 #define ROCKPAINT_LEFT BUTTON_LEFT
119 #define ROCKPAINT_RIGHT BUTTON_RIGHT
121 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
122 #define ROCKPAINT_QUIT BUTTON_BACK
123 #define ROCKPAINT_DRAW BUTTON_SELECT
124 #define ROCKPAINT_MENU BUTTON_MENU
125 #define ROCKPAINT_TOOLBAR BUTTON_PLAY
126 #define ROCKPAINT_TOOLBAR2 ( BUTTON_PLAY | BUTTON_LEFT )
127 #define ROCKPAINT_UP BUTTON_UP
128 #define ROCKPAINT_DOWN BUTTON_DOWN
129 #define ROCKPAINT_LEFT BUTTON_LEFT
130 #define ROCKPAINT_RIGHT BUTTON_RIGHT
132 #elif ( CONFIG_KEYPAD == COWOND2_PAD )
133 #define ROCKPAINT_QUIT BUTTON_POWER
134 #define ROCKPAINT_MENU BUTTON_MENU
136 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
137 #define ROCKPAINT_QUIT BUTTON_BACK
138 #define ROCKPAINT_DRAW BUTTON_SELECT
139 #define ROCKPAINT_MENU BUTTON_MENU
140 #define ROCKPAINT_TOOLBAR BUTTON_PLAY
141 #define ROCKPAINT_TOOLBAR2 ( BUTTON_PLAY | BUTTON_LEFT )
142 #define ROCKPAINT_UP BUTTON_UP
143 #define ROCKPAINT_DOWN BUTTON_DOWN
144 #define ROCKPAINT_LEFT BUTTON_LEFT
145 #define ROCKPAINT_RIGHT BUTTON_RIGHT
147 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
148 #define ROCKPAINT_QUIT BUTTON_POWER
149 #define ROCKPAINT_DRAW BUTTON_SELECT
150 #define ROCKPAINT_MENU BUTTON_MENU
151 #define ROCKPAINT_TOOLBAR BUTTON_VIEW
152 #define ROCKPAINT_TOOLBAR2 BUTTON_PLAYLIST
153 #define ROCKPAINT_UP BUTTON_UP
154 #define ROCKPAINT_DOWN BUTTON_DOWN
155 #define ROCKPAINT_LEFT BUTTON_LEFT
156 #define ROCKPAINT_RIGHT BUTTON_RIGHT
158 #elif ( CONFIG_KEYPAD == ONDAVX747_PAD )
159 #define ROCKPAINT_QUIT BUTTON_POWER
160 #define ROCKPAINT_MENU BUTTON_MENU
162 #elif CONFIG_KEYPAD == MROBE500_PAD
163 #define ROCKPAINT_QUIT BUTTON_POWER
165 #elif ( CONFIG_KEYPAD == SAMSUNG_YH_PAD )
166 #define ROCKPAINT_QUIT BUTTON_REC
167 #define ROCKPAINT_DRAW BUTTON_PLAY
168 #define ROCKPAINT_MENU BUTTON_FFWD
169 #define ROCKPAINT_TOOLBAR BUTTON_REW
170 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
171 #define ROCKPAINT_UP BUTTON_UP
172 #define ROCKPAINT_DOWN BUTTON_DOWN
173 #define ROCKPAINT_LEFT BUTTON_LEFT
174 #define ROCKPAINT_RIGHT BUTTON_RIGHT
176 #else
177 #error "Please define keys for this keypad"
178 #endif
180 #ifdef HAVE_TOUCHSCREEN
181 #ifndef ROCKPAINT_QUIT
182 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
183 #endif
184 #ifndef ROCKPAINT_DRAW
185 #define ROCKPAINT_DRAW BUTTON_CENTER
186 #endif
187 #ifndef ROCKPAINT_MENU
188 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
189 #endif
190 #ifndef ROCKPAINT_TOOLBAR
191 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
192 #endif
193 #ifndef ROCKPAINT_TOOLBAR2
194 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
195 #endif
196 #ifndef ROCKPAINT_UP
197 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
198 #endif
199 #ifndef ROCKPAINT_DOWN
200 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
201 #endif
202 #ifndef ROCKPAINT_LEFT
203 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
204 #endif
205 #ifndef ROCKPAINT_RIGHT
206 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
207 #endif
208 #endif
210 /***********************************************************************
211 * Palette Default Colors
212 ***********************************************************************/
213 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
214 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
215 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
216 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
217 #define COLOR_RED LCD_RGBPACK(128,0,0)
218 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
219 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
220 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
221 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
222 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
223 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
224 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
225 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
226 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
227 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
228 #define COLOR_PINK LCD_RGBPACK(255,0,255)
229 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
230 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
232 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
233 #define ROCKPAINT_TITLE_FONT 2
235 /***********************************************************************
236 * Program Colors
237 ***********************************************************************/
238 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
239 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
241 #define ROWS LCD_HEIGHT
242 #define COLS LCD_WIDTH
245 * Toolbar positioning stuff ... don't read this unless you really need to
247 * TB Toolbar
248 * SP Separator
249 * SC Selected Color
250 * PL Palette
251 * TL Tools
254 /* Separator sizes */
255 #define TB_SP_MARGIN 3
256 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
258 /* Selected color sizes */
259 #define TB_SC_SIZE 12
261 /* Palette sizes */
262 #define TB_PL_COLOR_SIZE 7
263 #define TB_PL_COLOR_SPACING 2
264 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
265 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
267 /* Tools sizes */
268 #define TB_TL_SIZE 8
269 #define TB_TL_SPACING 2
270 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
271 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
273 /* Menu button size ... gruik */
274 #define TB_MENU_MIN_WIDTH 30
276 /* Selected colors position */
277 #define TB_SC_FG_TOP 2
278 #define TB_SC_FG_LEFT 2
279 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
280 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
282 /* Palette position */
283 #define TB_PL_TOP TB_SC_FG_TOP
284 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
286 /* Tools position */
287 #define TB_TL_TOP TB_SC_FG_TOP
288 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
290 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
291 #undef TB_TL_TOP
292 #undef TB_TL_LEFT
293 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
294 #define TB_TL_LEFT TB_SC_FG_LEFT
295 #endif
297 /* Menu button position */
298 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
299 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
301 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
304 static void draw_pixel(int x,int y);
305 static void draw_line( int x1, int y1, int x2, int y2 );
306 static void draw_rect( int x1, int y1, int x2, int y2 );
307 static void draw_toolbars(bool update);
308 static void inv_cursor(bool update);
309 static void restore_screen(void);
310 static void clear_drawing(void);
311 static void goto_menu(void);
312 static int load_bitmap( const char *filename );
313 static int save_bitmap( char *filename );
314 static void draw_rect_full( int x1, int y1, int x2, int y2 );
316 /***********************************************************************
317 * Global variables
318 ***********************************************************************/
320 static int drawcolor=0; /* Current color (in palette) */
321 static int bgdrawcolor=9; /* Current background color (in palette) */
322 bool isbg = false; /* gruik ugly hack alert */
324 static int preview=false; /* Is preview mode on ? */
326 /* TODO: clean this up */
327 static int x=0, y=0; /* cursor position */
328 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
329 static int prev_x2=-1, prev_y2=-1;
330 static int prev_x3=-1, prev_y3=-1;
331 static int tool_mode=-1;
334 static int bsize=1; /* brush size */
335 static int bspeed=1; /* brush speed */
337 enum Tools { Brush = 0, /* Regular brush */
338 Fill = 1, /* Fill a shape with current color */
339 SelectRectangle = 2,
340 ColorPicker = 3, /* Pick a color */
341 Line = 4, /* Draw a line between two points */
342 Unused = 5, /* THIS IS UNUSED ... */
343 Curve = 6,
344 Text = 7,
345 Rectangle = 8, /* Draw a rectangle */
346 RectangleFull = 9,
347 Oval = 10, /* Draw an oval */
348 OvalFull = 11,
349 LinearGradient = 12,
350 RadialGradient = 13
353 enum Tools tool = Brush;
355 static bool quit=false;
356 static int gridsize=0;
358 static fb_data rp_colors[18] =
360 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
361 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
362 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
363 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
364 COLOR_LIGHTBROWN
367 static fb_data save_buffer[ ROWS*COLS ];
369 extern fb_data rockpaint[];
370 extern fb_data rockpaint_hsvrgb[];
372 /* Maximum string size allowed for the text tool */
373 #define MAX_TEXT 255
375 static union
377 /* Used by fill and gradient algorithms */
378 struct
380 short x;
381 short y;
382 } coord[ ROWS*COLS ];
384 /* Used by bezier curve algorithms */
385 struct
387 short x1, y1;
388 short x2, y2;
389 short x3, y3;
390 short x4, y4;
391 short depth;
392 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
393 * than coord ... so we divide to take
394 * less memory. */
396 /* Used to cut/copy/paste data */
397 fb_data clipboard[ ROWS*COLS ];
399 /* Used for text mode */
400 struct
402 char text[MAX_TEXT+1];
403 char font[MAX_PATH+1];
404 char old_font[MAX_PATH+1];
405 int fh_buf[30];
406 int fw_buf[30];
407 char fontname_buf[30][MAX_PATH];
408 } text;
409 } buffer;
411 /* Current filename */
412 static char filename[MAX_PATH+1];
414 /* Font preview buffer */
415 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
416 //#define FONT_PREVIEW_HEIGHT 1000
417 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
419 /***********************************************************************
420 * Offscreen buffer/Text/Fonts handling
422 * Parts of code taken from firmware/drivers/lcd-16bit.c
423 ***********************************************************************/
424 static void buffer_mono_bitmap_part(
425 fb_data *buf, int buf_width, int buf_height,
426 const unsigned char *src, int src_x, int src_y,
427 int stride, int x, int y, int width, int height )
428 /* this function only draws the foreground part of the bitmap */
430 const unsigned char *src_end;
431 fb_data *dst, *dst_end;
432 unsigned fgcolor = rb->lcd_get_foreground();
434 /* nothing to draw? */
435 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
436 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
437 return;
439 /* clipping */
440 if( x < 0 )
442 width += x;
443 src_x -= x;
444 x = 0;
446 if( y < 0 )
448 height += y;
449 src_y -= y;
450 y = 0;
452 if( x + width > buf_width )
453 width = buf_width - x;
454 if( y + height > buf_height )
455 height = buf_height - y;
457 src += stride * (src_y >> 3) + src_x; /* move starting point */
458 src_y &= 7;
459 src_end = src + width;
461 dst = buf + y*buf_width + x;
465 const unsigned char *src_col = src++;
466 unsigned data = *src_col >> src_y;
467 fb_data *dst_col = dst++;
468 int numbits = 8 - src_y;
470 dst_end = dst_col + height * buf_width;
473 if( data & 0x01 )
474 *dst_col = fgcolor; /* FIXME ? */
476 dst_col += buf_width;
478 data >>= 1;
479 if( --numbits == 0 )
481 src_col += stride;
482 data = *src_col;
483 numbits = 8;
485 } while( dst_col < dst_end );
486 } while( src < src_end );
489 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
490 int x, int y, int ofs, const unsigned char *str )
492 unsigned short ch;
493 unsigned short *ucs;
495 struct font *pf = rb->font_get( FONT_UI );
496 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
498 ucs = rb->bidi_l2v( str, 1 );
500 while( (ch = *ucs++) != 0 && x < buf_width )
502 int width;
503 const unsigned char *bits;
505 /* get proportional width and glyph bits */
506 width = rb->font_get_width( pf, ch );
508 if( ofs > width )
510 ofs -= width;
511 continue;
514 bits = rb->font_get_bits( pf, ch );
516 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0,
517 width, x, y, width - ofs, pf->height);
519 x += width - ofs;
520 ofs = 0;
524 /***********************************************************************
525 * Menu handling
526 ***********************************************************************/
527 enum {
528 /* Main menu */
529 MAIN_MENU_RESUME,
530 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
531 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
532 MAIN_MENU_GRID_SIZE,
533 MAIN_MENU_PLAYBACK_CONTROL,
534 MAIN_MENU_EXIT,
536 enum {
537 /* Select action menu */
538 SELECT_MENU_CUT, SELECT_MENU_COPY,
539 SELECT_MENU_INVERT, SELECT_MENU_HFLIP, SELECT_MENU_VFLIP,
540 SELECT_MENU_ROTATE90, SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
541 SELECT_MENU_CANCEL,
543 enum {
544 /* Text menu */
545 TEXT_MENU_TEXT, TEXT_MENU_FONT,
546 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
549 MENUITEM_STRINGLIST(main_menu, "RockPaint", NULL,
550 "Resume", "New", "Load", "Save",
551 "Brush Size", "Brush Speed",
552 "Choose Color", "Grid Size",
553 "Playback Control", "Exit");
554 MENUITEM_STRINGLIST(select_menu, "Select...", NULL,
555 "Cut", "Copy",
556 "Invert", "Horizontal Flip", "Vertical Flip",
557 "Rotate 90°", "Rotate 180°", "Rotate 270°",
558 "Cancel");
559 MENUITEM_STRINGLIST(text_menu, "Text", NULL,
560 "Set Text", "Change Font",
561 "Preview", "Apply", "Cancel");
562 static const int times_list[] = { 1, 2, 4, 8 };
563 static const int gridsize_list[] = { 0, 5, 10, 20 };
564 static const struct opt_items times_options[] = {
565 { "1x", -1 }, { "2x", -1 }, { "4x", -1 }, { "8x", -1 }
567 static const struct opt_items gridsize_options[] = {
568 { "No grid", -1 }, { "5px", -1 }, { "10px", -1 }, { "20px", -1 }
571 static int draw_window( int height, int width,
572 int *top, int *left,
573 const char *title )
575 int fh;
576 rb->lcd_getstringsize( title, NULL, &fh );
577 fh++;
579 const int _top = ( LCD_HEIGHT - height ) / 2;
580 const int _left = ( LCD_WIDTH - width ) / 2;
581 if( top ) *top = _top;
582 if( left ) *left = _left;
583 rb->lcd_set_background(COLOR_BLUE);
584 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
585 rb->lcd_fillrect( _left, _top, width, height );
586 rb->lcd_set_foreground(COLOR_BLUE);
587 rb->lcd_fillrect( _left, _top, width, fh+4 );
588 rb->lcd_set_foreground(COLOR_WHITE);
589 rb->lcd_putsxy( _left+2, _top+2, title );
590 rb->lcd_set_foreground(COLOR_BLACK);
591 rb->lcd_drawrect( _left, _top, width, height );
592 return _top+fh+4;
595 /***********************************************************************
596 * File browser
597 ***********************************************************************/
599 char bbuf[MAX_PATH+1]; /* used by file and font browsers */
600 char bbuf_s[MAX_PATH+1]; /* used by file and font browsers */
601 struct tree_context *tree = NULL;
603 static char * browse_get_name_cb( int selected_item, void *data,
604 char *buffer, size_t buffer_len )
606 int *indexes = (int *) data;
607 struct entry* dc = tree->dircache;
608 struct entry* e = &dc[indexes[selected_item]];
609 (void) buffer;
610 (void) buffer_len;
612 return (e->name);
615 static bool browse( char *dst, int dst_size, const char *start )
617 struct gui_synclist browse_list;
618 int item_count = 0, selected, button;
619 struct tree_context backup;
620 struct entry *dc;
621 bool reload = true;
622 int dirfilter = SHOW_ALL;
623 int *indexes = (int *) buffer.clipboard;
625 char *a;
627 rb->strcpy( bbuf, start );
628 a = bbuf+rb->strlen(bbuf)-1;
629 if( *a != '/' )
631 a[1] = '/';
632 a[2] = '\0';
634 bbuf_s[0] = '\0';
636 rb->gui_synclist_init( &browse_list, browse_get_name_cb,
637 (void*) indexes, false, 1, NULL );
639 tree = rb->tree_get_context();
640 backup = *tree;
641 dc = tree->dircache;
642 a = backup.currdir+rb->strlen(backup.currdir)-1;
643 if( *a != '/' )
645 *++a = '/';
646 *++a = '\0';
648 rb->strcpy( a, dc[tree->selected_item].name );
649 tree->dirfilter = &dirfilter;
650 while( 1 )
652 if( reload )
654 int i;
655 rb->set_current_file(bbuf);
656 item_count = 0;
657 selected = 0;
658 for( i = 0; i < tree->filesindir ; i++)
660 /* only displayes directories and .bmp files */
661 if( ((dc[i].attr & ATTR_DIRECTORY ) &&
662 rb->strcmp( dc[i].name, "." ) &&
663 rb->strcmp( dc[i].name, ".." )) ||
664 ( !(dc[i].attr & ATTR_DIRECTORY) &&
665 (a = rb->strrchr( dc[i].name,'.' )) &&
666 !rb->strcmp( a, ".bmp" ) ))
668 if( !rb->strcmp( dc[i].name, bbuf_s ) )
669 selected = item_count;
670 indexes[item_count++] = i;
674 rb->gui_synclist_set_nb_items(&browse_list,item_count);
675 rb->gui_synclist_select_item(&browse_list, selected);
676 rb->gui_synclist_set_title(&browse_list, bbuf, NOICON);
677 rb->gui_synclist_draw(&browse_list);
678 reload = false;
680 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
681 if (rb->gui_synclist_do_button(&browse_list,&button,LIST_WRAP_UNLESS_HELD))
682 continue;
683 switch( button )
685 case ACTION_STD_CANCEL:
686 if( !rb->strcmp( bbuf, "/" ) )
688 *tree = backup;
689 rb->set_current_file( backup.currdir );
690 return false;
692 rb->strcpy( bbuf_s, ".." );
693 case ACTION_STD_OK:
694 if( button == ACTION_STD_OK )
696 selected = rb->gui_synclist_get_sel_pos( &browse_list );
697 if( selected < 0 || selected >= item_count )
698 break;
699 struct entry* e = &dc[indexes[selected]];
700 rb->strlcpy( bbuf_s, e->name, sizeof( bbuf_s ) );
701 if( !( e->attr & ATTR_DIRECTORY ) )
703 *tree = backup;
704 rb->set_current_file( backup.currdir );
705 rb->snprintf( dst, dst_size, "%s%s", bbuf, bbuf_s );
706 return true;
709 if( !rb->strcmp( bbuf_s, "." ) ) break;
710 a = bbuf+rb->strlen(bbuf);
711 if( !rb->strcmp( bbuf_s, ".." ) )
713 a--;
714 if( a == bbuf ) break;
715 if( *a == '/' ) a--;
716 while( *a != '/' ) a--;
717 rb->strcpy( bbuf_s, ++a );
718 /* select parent directory */
719 bbuf_s[rb->strlen(bbuf_s)-1] = '\0';
720 *a = '\0';
721 reload = true;
722 break;
724 rb->snprintf( a, bbuf+sizeof(bbuf)-a, "%s/", bbuf_s );
725 reload = true;
726 break;
728 case ACTION_STD_MENU:
729 *tree = backup;
730 rb->set_current_file( backup.currdir );
731 return false;
736 /***********************************************************************
737 * Font browser
739 * FIXME: This still needs some work ... it currently only works fine
740 * on the simulators, disk spins too much on real targets -> rendered
741 * font buffer needed.
742 ***********************************************************************/
743 static bool browse_fonts( char *dst, int dst_size )
745 #define WIDTH ( LCD_WIDTH - 20 )
746 #define HEIGHT ( LCD_HEIGHT - 20 )
747 #define LINE_SPACE 2
748 int top, top_inside = 0, left;
750 DIR *d;
751 struct dirent *de;
752 int fvi = 0; /* first visible item */
753 int lvi = 0; /* last visible item */
754 int si = 0; /* selected item */
755 int osi = 0; /* old selected item */
756 int li = 0; /* last item */
757 int nvih = 0; /* next visible item height */
758 int i;
759 int b_need_redraw = 1; /* Do we need to redraw ? */
761 int cp = 0; /* current position */
762 int fh; /* font height */
764 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
765 #define fw_buf buffer.text.fw_buf
766 int fw;
767 #define fontname_buf buffer.text.fontname_buf
769 rb->snprintf( buffer.text.old_font, MAX_PATH,
770 FONT_DIR "/%s.fnt",
771 rb->global_settings->font_file );
773 while( 1 )
775 if( !b_need_redraw )
777 /* we don't need to redraw ... but we need to unselect
778 * the previously selected item */
779 cp = top_inside + LINE_SPACE;
780 for( i = 0; i+fvi < osi; i++ )
782 cp += fh_buf[i] + LINE_SPACE;
784 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
785 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
786 rb->lcd_set_drawmode(DRMODE_SOLID);
789 if( b_need_redraw )
791 b_need_redraw = 0;
793 d = rb->opendir( FONT_DIR "/" );
794 if( !d )
796 return false;
798 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
799 i = 0;
800 li = -1;
801 while( i < fvi )
803 rb->readdir( d );
804 i++;
806 cp = top_inside+LINE_SPACE;
808 rb->lcd_set_foreground(COLOR_BLACK);
809 rb->lcd_set_background(COLOR_LIGHTGRAY);
811 while( cp < top+HEIGHT )
813 de = rb->readdir( d );
814 if( !de )
816 li = i-1;
817 break;
819 if( rb->strlen( de->d_name ) < 4
820 || rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
821 ".fnt" ) )
822 continue;
823 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
824 de->d_name );
825 rb->font_load( bbuf );
826 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
827 if( nvih > 0 )
829 nvih -= fh;
830 fvi++;
831 if( nvih < 0 ) nvih = 0;
832 i++;
833 continue;
835 if( cp + fh >= top+HEIGHT )
837 nvih = fh;
838 break;
840 rb->lcd_putsxy( left+10, cp, de->d_name );
841 fh_buf[i-fvi] = fh;
842 fw_buf[i-fvi] = fw;
843 cp += fh + LINE_SPACE;
844 rb->strcpy( fontname_buf[i-fvi], bbuf );
845 i++;
847 lvi = i-1;
848 if( li == -1 )
850 if( !(de = rb->readdir( d ) ) )
852 li = lvi;
854 else if( !nvih && !rb->strlen( de->d_name ) < 4
855 && !rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
856 ".fnt" ) )
858 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
859 de->d_name );
860 rb->font_load( bbuf );
861 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
862 nvih = fh;
865 rb->font_load( buffer.text.old_font );
866 rb->closedir( d );
869 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
870 cp = top_inside + LINE_SPACE;
871 for( i = 0; i+fvi < si; i++ )
873 cp += fh_buf[i] + LINE_SPACE;
875 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
876 rb->lcd_set_drawmode(DRMODE_SOLID);
878 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
880 osi = si;
881 i = fvi;
882 switch( rb->button_get(true) )
884 case ROCKPAINT_UP:
885 case ROCKPAINT_UP|BUTTON_REPEAT:
886 if( si > 0 )
888 si--;
889 if( si<fvi )
891 fvi = si;
894 break;
896 case ROCKPAINT_DOWN:
897 case ROCKPAINT_DOWN|BUTTON_REPEAT:
898 if( li == -1 || si < li )
900 si++;
902 break;
904 case ROCKPAINT_LEFT:
905 return false;
907 case ROCKPAINT_RIGHT:
908 case ROCKPAINT_DRAW:
909 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
910 return true;
913 if( i != fvi || si > lvi )
915 b_need_redraw = 1;
918 if( si<=lvi )
920 nvih = 0;
923 #undef fh_buf
924 #undef fw_buf
925 #undef fontname_buf
926 #undef WIDTH
927 #undef HEIGHT
928 #undef LINE_SPACE
931 /***********************************************************************
932 * HSVRGB Color chooser
933 ***********************************************************************/
934 static unsigned int color_chooser( unsigned int color )
936 int red = RGB_UNPACK_RED( color );
937 int green = RGB_UNPACK_GREEN( color );
938 int blue = RGB_UNPACK_BLUE( color );
939 int hue, saturation, value;
940 int r, g, b; /* temp variables */
941 int i, top, left;
943 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
944 Red = 3, Green = 4, Blue = 5 };
945 enum BaseColor current = Red;
946 bool has_changed;
948 char str[6] = "";
950 restore_screen();
952 rgb2hsv( red, green, blue, &hue, &saturation, &value );
954 while( 1 )
956 has_changed = false;
957 color = LCD_RGBPACK( red, green, blue );
959 #define HEIGHT ( 100 )
960 #define WIDTH ( 150 )
962 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
963 top -= 15;
965 for( i=0; i<100; i++ )
967 hsv2rgb( i*36, saturation, value, &r, &g, &b );
968 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
969 rb->lcd_vline( left+15+i, top+20, top+27 );
970 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
971 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
972 rb->lcd_vline( left+15+i, top+30, top+37 );
973 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
974 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
975 rb->lcd_vline( left+15+i, top+40, top+47 );
976 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
977 rb->lcd_vline( left+15+i, top+50, top+57 );
978 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
979 rb->lcd_vline( left+15+i, top+60, top+67 );
980 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
981 rb->lcd_vline( left+15+i, top+70, top+77 );
984 rb->lcd_set_foreground(COLOR_BLACK);
985 #define POSITION( a, i ) \
986 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
987 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
988 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
989 rb->lcd_drawpixel( left+16+i, top + 28 + a );
990 POSITION( 0, hue/36 );
991 POSITION( 10, saturation*99/255 );
992 POSITION( 20, value*99/255 );
993 POSITION( 30, red*99/255 );
994 POSITION( 40, green*99/255 );
995 POSITION( 50, blue*99/255 );
996 #undef POSITION
997 rb->lcd_set_background(COLOR_LIGHTGRAY);
998 rb->lcd_setfont( FONT_SYSFIXED );
999 rb->snprintf( str, 6, "%d", hue/10 );
1000 rb->lcd_putsxy( left + 117, top + 20, str );
1001 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1002 rb->lcd_putsxy( left + 117, top + 30, str );
1003 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1004 rb->lcd_putsxy( left + 117, top + 40, str );
1005 rb->snprintf( str, 6, "%d", red );
1006 rb->lcd_putsxy( left + 117, top + 50, str );
1007 rb->snprintf( str, 6, "%d", green );
1008 rb->lcd_putsxy( left + 117, top + 60, str );
1009 rb->snprintf( str, 6, "%d", blue );
1010 rb->lcd_putsxy( left + 117, top + 70, str );
1011 rb->lcd_setfont( FONT_UI );
1013 #define CURSOR( l ) \
1014 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1015 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1016 CURSOR( 5 );
1017 #undef CURSOR
1019 rb->lcd_set_foreground( color );
1020 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1022 rb->lcd_update();
1024 switch( rb->button_get(true) )
1026 case ROCKPAINT_UP:
1027 current = ( current + 5 )%6;
1028 break;
1030 case ROCKPAINT_DOWN:
1031 current = (current + 1 )%6;
1032 break;
1034 case ROCKPAINT_LEFT:
1035 has_changed = true;
1036 switch( current )
1038 case Hue:
1039 hue = ( hue + 3600 - 10 )%3600;
1040 break;
1041 case Saturation:
1042 if( saturation ) saturation--;
1043 break;
1044 case Value:
1045 if( value ) value--;
1046 break;
1047 case Red:
1048 if( red ) red--;
1049 break;
1050 case Green:
1051 if( green ) green--;
1052 break;
1053 case Blue:
1054 if( blue ) blue--;
1055 break;
1057 break;
1059 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1060 has_changed = true;
1061 switch( current )
1063 case Hue:
1064 hue = ( hue + 3600 - 100 )%3600;
1065 break;
1066 case Saturation:
1067 if( saturation >= 8 ) saturation-=8;
1068 else saturation = 0;
1069 break;
1070 case Value:
1071 if( value >= 8 ) value-=8;
1072 else value = 0;
1073 break;
1074 case Red:
1075 if( red >= 8 ) red-=8;
1076 else red = 0;
1077 break;
1078 case Green:
1079 if( green >= 8 ) green-=8;
1080 else green = 0;
1081 break;
1082 case Blue:
1083 if( blue >= 8 ) blue-=8;
1084 else blue = 0;
1085 break;
1087 break;
1089 case ROCKPAINT_RIGHT:
1090 has_changed = true;
1091 switch( current )
1093 case Hue:
1094 hue = ( hue + 10 )%3600;
1095 break;
1096 case Saturation:
1097 if( saturation < 0xff ) saturation++;
1098 break;
1099 case Value:
1100 if( value < 0xff ) value++;
1101 break;
1102 case Red:
1103 if( red < 0xff ) red++;
1104 break;
1105 case Green:
1106 if( green < 0xff ) green++;
1107 break;
1108 case Blue:
1109 if( blue < 0xff ) blue++;
1110 break;
1112 break;
1114 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1115 has_changed = true;
1116 switch( current )
1118 case Hue:
1119 hue = ( hue + 100 )%3600;
1120 break;
1121 case Saturation:
1122 if( saturation < 0xff - 8 ) saturation+=8;
1123 else saturation = 0xff;
1124 break;
1125 case Value:
1126 if( value < 0xff - 8 ) value+=8;
1127 else value = 0xff;
1128 break;
1129 case Red:
1130 if( red < 0xff - 8 ) red+=8;
1131 else red = 0xff;
1132 break;
1133 case Green:
1134 if( green < 0xff - 8 ) green+=8;
1135 else green = 0xff;
1136 break;
1137 case Blue:
1138 if( blue < 0xff - 8 ) blue+=8;
1139 else blue = 0xff;
1140 break;
1142 break;
1144 case ROCKPAINT_DRAW:
1145 return color;
1147 if( has_changed )
1149 switch( current )
1151 case Hue:
1152 case Saturation:
1153 case Value:
1154 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1155 break;
1157 case Red:
1158 case Green:
1159 case Blue:
1160 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1161 break;
1164 #undef HEIGHT
1165 #undef WIDTH
1169 /***********************************************************************
1170 * Misc routines
1171 ***********************************************************************/
1172 static void init_buffer(void)
1174 int i;
1175 fb_data color = rp_colors[ bgdrawcolor ];
1176 for( i = 0; i < ROWS*COLS; i++ )
1178 save_buffer[i] = color;
1182 static void draw_pixel(int x,int y)
1184 if( !preview )
1186 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1187 if( isbg )
1189 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1191 else
1193 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1196 rb->lcd_drawpixel(x,y);
1199 static void color_picker( int x, int y )
1201 if( preview )
1203 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1204 #define PSIZE 12
1205 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1206 rb->lcd_drawrect( x<COLS-PSIZE ? x + 2 : x - PSIZE, y<ROWS-PSIZE ? y + 2: y - PSIZE, PSIZE - 2, PSIZE - 2 );
1207 rb->lcd_set_drawmode(DRMODE_SOLID);
1208 rb->lcd_fillrect( x<COLS-PSIZE ? x+3 : x - PSIZE+1, y<ROWS-PSIZE ? y +3: y - PSIZE+1, PSIZE-4, PSIZE-4 );
1209 #undef PSIZE
1210 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1212 else
1214 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1218 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1219 /* This is a preview mode only function */
1221 int i,a;
1222 if( x1 > x2 )
1224 i = x1;
1225 x1 = x2;
1226 x2 = i;
1228 if( y1 > y2 )
1230 i = y1;
1231 y1 = y2;
1232 y2 = i;
1234 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1235 i = 0;
1236 for( a = x1; a < x2; a++, i++ )
1237 if( i%2 )
1238 rb->lcd_drawpixel( a, y1 );
1239 for( a = y1; a < y2; a++, i++ )
1240 if( i%2 )
1241 rb->lcd_drawpixel( x2, a );
1242 if( y2 != y1 )
1243 for( a = x2; a > x1; a--, i++ )
1244 if( i%2 )
1245 rb->lcd_drawpixel( a, y2 );
1246 if( x2 != x1 )
1247 for( a = y2; a > y1; a--, i++ )
1248 if( i%2 )
1249 rb->lcd_drawpixel( x1, a );
1250 rb->lcd_set_drawmode(DRMODE_SOLID);
1253 static void copy_to_clipboard( void )
1255 /* This needs to be optimised ... but i'm lazy ATM */
1256 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1259 /* no preview mode handling atm ... do we need it ? (one if) */
1260 static void draw_invert( int x1, int y1, int x2, int y2 )
1262 int i;
1263 if( x1 > x2 )
1265 i = x1;
1266 x1 = x2;
1267 x2 = i;
1269 if( y1 > y2 )
1271 i = y1;
1272 y1 = y2;
1273 y2 = i;
1276 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1277 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1278 rb->lcd_set_drawmode(DRMODE_SOLID);
1280 for( ; y1<=y2; y1++ )
1282 for( i = x1; i<=x2; i++ )
1284 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1287 /*if( update )*/ rb->lcd_update();
1290 static void draw_hflip( int x1, int y1, int x2, int y2 )
1292 int i;
1293 if( x1 > x2 )
1295 i = x1;
1296 x1 = x2;
1297 x2 = i;
1299 if( y1 > y2 )
1301 i = y1;
1302 y1 = y2;
1303 y2 = i;
1306 copy_to_clipboard();
1308 for( i = 0; i <= y2 - y1; i++ )
1310 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1311 buffer.clipboard+(y2-i)*COLS+x1,
1312 (x2-x1+1)*sizeof( fb_data ) );
1314 restore_screen();
1315 rb->lcd_update();
1318 static void draw_vflip( int x1, int y1, int x2, int y2 )
1320 int i;
1321 if( x1 > x2 )
1323 i = x1;
1324 x1 = x2;
1325 x2 = i;
1327 if( y1 > y2 )
1329 i = y1;
1330 y1 = y2;
1331 y2 = i;
1334 copy_to_clipboard();
1336 for( ; y1 <= y2; y1++ )
1338 for( i = 0; i <= x2 - x1; i++ )
1340 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1343 restore_screen();
1344 rb->lcd_update();
1347 /* direction: -1 = left, 1 = right */
1348 static void draw_rot_90_deg( int x1, int y1, int x2, int y2, int direction )
1350 int i, j;
1351 if( x1 > x2 )
1353 i = x1;
1354 x1 = x2;
1355 x2 = i;
1357 if( y1 > y2 )
1359 i = y1;
1360 y1 = y2;
1361 y2 = i;
1364 copy_to_clipboard();
1366 fb_data color = rp_colors[ bgdrawcolor ];
1367 const int width = x2 - x1, height = y2 - y1;
1368 const int sub_half = width/2-height/2, add_half = (width+height)/2;
1369 if( width > height )
1371 for( i = 0; i <= height; i++ )
1373 for( j = 0; j < sub_half; j++ )
1374 save_buffer[(y1+i)*COLS+x1+j] = color;
1375 for( j = add_half+1; j <= width; j++ )
1376 save_buffer[(y1+i)*COLS+x1+j] = color;
1379 else if( width < height )
1381 for( j = 0; j <= width; j++ )
1383 for( i = 0; i < -sub_half; i++ )
1384 save_buffer[(y1+i)*COLS+x1+j] = color;
1385 for( i = add_half+1; i <= height; i++ )
1386 save_buffer[(y1+i)*COLS+x1+j] = color;
1389 int x3 = x1 + sub_half, y3 = y1 - sub_half;
1390 int is = x3<0?-x3:0, ie = COLS-x3-1, js = y3<0?-y3:0, je = ROWS-y3-1;
1391 if( ie > height ) ie = height;
1392 if( je > width ) je = width;
1393 for( i = is; i <= ie; i++ )
1395 for( j = js; j <= je; j++ )
1397 int x, y;
1398 if(direction > 0)
1400 x = x1+j;
1401 y = y1+height-i;
1403 else
1405 x = x1+width-j;
1406 y = y1+i;
1408 save_buffer[(y3+j)*COLS+x3+i] = buffer.clipboard[y*COLS+x];
1411 restore_screen();
1412 rb->lcd_update();
1415 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1416 int src_y2, int x1, int y1, int mode )
1418 int i;
1419 if( mode == SELECT_MENU_CUT )
1421 i = drawcolor;
1422 drawcolor = bgdrawcolor;
1423 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1424 drawcolor = i;
1426 if( src_x1 > src_x2 )
1428 i = src_x1;
1429 src_x1 = src_x2;
1430 src_x2 = i;
1432 if( src_y1 > src_y2 )
1434 i = src_y1;
1435 src_y1 = src_y2;
1436 src_y2 = i;
1438 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1439 x1, y1, src_x2-src_x1+1, src_y2-src_y1+1 );
1440 if( !preview )
1442 for( i = 0; i <= src_y2 - src_y1; i++ )
1444 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1445 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1446 (src_x2 - src_x1 + 1)*sizeof( fb_data ) );
1451 static void show_grid( bool update )
1453 int i;
1454 if( gridsize > 0 )
1456 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1457 for( i = gridsize; i < COLS; i+= gridsize )
1459 rb->lcd_vline( i, 0, ROWS-1 );
1461 for( i = gridsize; i < ROWS; i+= gridsize )
1463 rb->lcd_hline( 0, COLS-1, i );
1465 rb->lcd_set_drawmode(DRMODE_SOLID);
1466 if( update ) rb->lcd_update();
1470 static void draw_text( int x, int y )
1472 int selected = 0;
1473 buffer.text.text[0] = '\0';
1474 rb->snprintf( buffer.text.old_font, MAX_PATH,
1475 FONT_DIR "/%s.fnt",
1476 rb->global_settings->font_file );
1477 while( 1 )
1479 switch( rb->do_menu( &text_menu, &selected, NULL, NULL ) )
1481 case TEXT_MENU_TEXT:
1482 rb->lcd_set_foreground(COLOR_BLACK);
1483 rb->kbd_input( buffer.text.text, MAX_TEXT );
1484 break;
1486 case TEXT_MENU_FONT:
1487 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1489 rb->font_load( buffer.text.font );
1491 break;
1493 case TEXT_MENU_PREVIEW:
1494 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1495 while( 1 )
1497 int button;
1498 restore_screen();
1499 rb->lcd_putsxy( x, y, buffer.text.text );
1500 rb->lcd_update();
1501 switch( button = rb->button_get( true ) )
1503 case ROCKPAINT_LEFT:
1504 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1505 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1506 if (x<0) x=COLS-1;
1507 break;
1509 case ROCKPAINT_RIGHT:
1510 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1511 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1512 if (x>=COLS) x=0;
1513 break;
1515 case ROCKPAINT_UP:
1516 case ROCKPAINT_UP | BUTTON_REPEAT:
1517 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1518 if (y<0) y=ROWS-1;
1519 break;
1521 case ROCKPAINT_DOWN:
1522 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1523 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1524 if (y>=ROWS-1) y=0;
1525 break;
1527 case ROCKPAINT_DRAW:
1528 break;
1529 default:
1530 if(rb->default_event_handler(button)
1531 == SYS_USB_CONNECTED)
1532 button = ROCKPAINT_DRAW;
1533 break;
1535 if( button == ROCKPAINT_DRAW ) break;
1537 break;
1539 case TEXT_MENU_APPLY:
1540 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1541 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1542 buffer.text.text );
1543 case TEXT_MENU_CANCEL:
1544 default:
1545 restore_screen();
1546 rb->font_load( buffer.text.old_font );
1547 return;
1552 static void draw_brush( int x, int y )
1554 int i,j;
1555 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1557 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1559 draw_pixel( x+i, y+j );
1564 /* This is an implementation of Bresenham's line algorithm.
1565 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1567 static void draw_line( int x1, int y1, int x2, int y2 )
1569 int x = x1;
1570 int y = y1;
1571 int deltax = x2 - x1;
1572 int deltay = y2 - y1;
1573 int i;
1575 int xerr = abs(deltax);
1576 int yerr = abs(deltay);
1577 int xstep = deltax > 0 ? 1 : -1;
1578 int ystep = deltay > 0 ? 1 : -1;
1579 int err;
1581 if (yerr > xerr)
1583 /* more vertical */
1584 err = yerr;
1585 xerr <<= 1;
1586 yerr <<= 1;
1588 /* to leave off the last pixel of the line, leave off the "+ 1" */
1589 for (i = abs(deltay) + 1; i; --i)
1591 draw_pixel(x, y);
1592 y += ystep;
1593 err -= xerr;
1594 if (err < 0) {
1595 x += xstep;
1596 err += yerr;
1600 else
1602 /* more horizontal */
1603 err = xerr;
1604 xerr <<= 1;
1605 yerr <<= 1;
1607 for (i = abs(deltax) + 1; i; --i)
1609 draw_pixel(x, y);
1610 x += xstep;
1611 err -= yerr;
1612 if (err < 0) {
1613 y += ystep;
1614 err += xerr;
1620 static void draw_curve( int x1, int y1, int x2, int y2,
1621 int xa, int ya, int xb, int yb )
1623 int i = 0;
1624 short xl1, yl1;
1625 short xl2, yl2;
1626 short xl3, yl3;
1627 short xl4, yl4;
1628 short xr1, yr1;
1629 short xr2, yr2;
1630 short xr3, yr3;
1631 short xr4, yr4;
1632 short depth;
1633 short xh, yh;
1635 if( x1 == x2 && y1 == y2 )
1637 draw_pixel( x1, y1 );
1638 return;
1641 // if( preview )
1643 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1644 if( xa == -1 || ya == -1 )
1646 rb->lcd_drawline( x1, y1, xb, yb );
1647 rb->lcd_drawline( x2, y2, xb, yb );
1649 else
1651 rb->lcd_drawline( x1, y1, xa, ya );
1652 rb->lcd_drawline( x2, y2, xb, yb );
1654 rb->lcd_set_drawmode(DRMODE_SOLID);
1657 if( xa == -1 || ya == -1 )
1658 /* We only have 3 of the points
1659 * This will currently only be used in preview mode */
1661 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1662 buffer.bezier[i].x1 = a1; \
1663 buffer.bezier[i].y1 = b1; \
1664 buffer.bezier[i].x2 = a2; \
1665 buffer.bezier[i].y2 = b2; \
1666 buffer.bezier[i].x3 = a3; \
1667 buffer.bezier[i].y3 = b3; \
1668 buffer.bezier[i].depth = d; \
1669 i++;
1670 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1671 i--; \
1672 a1 = buffer.bezier[i].x1; \
1673 b1 = buffer.bezier[i].y1; \
1674 a2 = buffer.bezier[i].x2; \
1675 b2 = buffer.bezier[i].y2; \
1676 a3 = buffer.bezier[i].x3; \
1677 b3 = buffer.bezier[i].y3; \
1678 d = buffer.bezier[i].depth;
1679 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1680 while( i )
1682 /* de Casteljau's algorithm (see wikipedia) */
1683 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1684 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1686 xl2 = ( xl1 + xb )>>1;
1687 yl2 = ( yl1 + yb )>>1;
1688 xr2 = ( xb + xr3 )>>1;
1689 yr2 = ( yb + yr3 )>>1;
1690 xr1 = ( xl2 + xr2 )>>1;
1691 yr1 = ( yl2 + yr2 )>>1;
1692 xl3 = xr1;
1693 yl3 = yr1;
1694 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1695 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1697 else
1699 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1700 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1703 #undef PUSH
1704 #undef POP
1706 else /* We have the 4 points */
1708 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1709 buffer.bezier[i].x1 = a1; \
1710 buffer.bezier[i].y1 = b1; \
1711 buffer.bezier[i].x2 = a2; \
1712 buffer.bezier[i].y2 = b2; \
1713 buffer.bezier[i].x3 = a3; \
1714 buffer.bezier[i].y3 = b3; \
1715 buffer.bezier[i].x4 = a4; \
1716 buffer.bezier[i].y4 = b4; \
1717 buffer.bezier[i].depth = d; \
1718 i++;
1719 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, 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 a4 = buffer.bezier[i].x4; \
1728 b4 = buffer.bezier[i].y4; \
1729 d = buffer.bezier[i].depth;
1731 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1732 while( i )
1734 /* de Casteljau's algorithm (see wikipedia) */
1735 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1736 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1738 xl2 = ( xl1 + xa )>>1;
1739 yl2 = ( yl1 + ya )>>1;
1740 xh = ( xa + xb )>>1;
1741 yh = ( ya + yb )>>1;
1742 xr3 = ( xb + xr4 )>>1;
1743 yr3 = ( yb + yr4 )>>1;
1744 xl3 = ( xl2 + xh )>>1;
1745 yl3 = ( yl2 + yh )>>1;
1746 xr2 = ( xr3 + xh )>>1;
1747 yr2 = ( yr3 + yh )>>1;
1748 xl4 = ( xl3 + xr2 )>>1;
1749 yl4 = ( yl3 + yr2 )>>1;
1750 xr1 = xl4;
1751 yr1 = yl4;
1752 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1753 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1755 else
1757 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1758 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1761 #undef PUSH
1762 #undef POP
1766 static void draw_rect( int x1, int y1, int x2, int y2 )
1768 draw_line( x1, y1, x1, y2 );
1769 draw_line( x1, y1, x2, y1 );
1770 draw_line( x1, y2, x2, y2 );
1771 draw_line( x2, y1, x2, y2 );
1774 static void togglebg( void )
1776 if( isbg )
1778 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1780 else
1782 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1784 isbg = !isbg;
1787 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1789 /* GRUIK */
1790 int x = x1;
1791 togglebg();
1792 if( x < x2 )
1794 do {
1795 draw_line( x, y1, x, y2 );
1796 } while( ++x <= x2 );
1798 else
1800 do {
1801 draw_line( x, y1, x, y2 );
1802 } while( --x >= x2 );
1804 togglebg();
1805 draw_rect( x1, y1, x2, y2 );
1808 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1810 /* TODO: simplify :) */
1811 int cx = (x1+x2)>>1;
1812 int cy = (y1+y2)>>1;
1814 int rx = (x1-x2)>>1;
1815 int ry = (y1-y2)>>1;
1816 if( rx < 0 ) rx *= -1;
1817 if( ry < 0 ) ry *= -1;
1819 if( rx == 0 || ry == 0 )
1821 draw_line( x1, y1, x2, y2 );
1822 return;
1825 int x,y;
1826 int dst, old_dst;
1828 for( x = 0; x < rx; x++ )
1830 y = 0;
1831 dst = -0xfff;
1832 do {
1833 old_dst = dst;
1834 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1835 y++;
1836 } while( dst < 0 );
1837 if( -old_dst < dst ) y--;
1838 if( full )
1840 draw_line( cx+x, cy, cx+x, cy+y );
1841 draw_line( cx+x, cy, cx+x, cy-y );
1842 draw_line( cx-x, cy, cx-x, cy+y );
1843 draw_line( cx-x, cy, cx-x, cy-y );
1845 else
1847 draw_pixel( cx+x, cy+y );
1848 draw_pixel( cx+x, cy-y );
1849 draw_pixel( cx-x, cy+y );
1850 draw_pixel( cx-x, cy-y );
1853 for( y = 0; y < ry; y++ )
1855 x = 0;
1856 dst = -0xfff;
1857 do {
1858 old_dst = dst;
1859 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1860 x++;
1861 } while( dst < 0 );
1862 if( -old_dst < dst ) x--;
1863 if( full )
1865 draw_line( cx+x, cy, cx+x, cy+y );
1866 draw_line( cx+x, cy, cx+x, cy-y );
1867 draw_line( cx-x, cy, cx-x, cy+y );
1868 draw_line( cx-x, cy, cx-x, cy-y );
1870 else
1872 draw_pixel( cx+x, cy+y );
1873 draw_pixel( cx+x, cy-y );
1874 draw_pixel( cx-x, cy+y );
1875 draw_pixel( cx-x, cy-y );
1880 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1882 draw_oval( x1, y1, x2, y2, false );
1885 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1887 togglebg();
1888 draw_oval( x1, y1, x2, y2, true );
1889 togglebg();
1890 draw_oval( x1, y1, x2, y2, false );
1893 static void draw_fill( int x0, int y0 )
1895 #define PUSH( a, b ) \
1896 draw_pixel( (int)a, (int)b ); \
1897 buffer.coord[i].x = a; \
1898 buffer.coord[i].y = b; \
1899 i++;
1900 #define POP( a, b ) \
1901 i--; \
1902 a = buffer.coord[i].x; \
1903 b = buffer.coord[i].y;
1905 unsigned int i=0;
1906 short x = x0;
1907 short y = y0;
1908 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1910 if( prev_color == rp_colors[ drawcolor ] ) return;
1912 PUSH( x, y );
1914 while( i != 0 )
1916 POP( x, y );
1917 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1919 PUSH( x-1, y );
1921 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1923 PUSH( x+1, y );
1925 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1927 PUSH( x, y-1 );
1929 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1931 PUSH( x, y+1 );
1934 #undef PUSH
1935 #undef POP
1939 /* For preview purposes only */
1940 static void line_gradient( int x1, int y1, int x2, int y2 )
1942 int r1, g1, b1;
1943 int r2, g2, b2;
1944 int h1, s1, v1, h2, s2, v2, r, g, b;
1945 int w, h, x, y;
1947 bool a = false;
1949 x1 <<= 1;
1950 y1 <<= 1;
1951 x2 <<= 1;
1952 y2 <<= 1;
1954 w = x1 - x2;
1955 h = y1 - y2;
1957 if( w == 0 && h == 0 )
1959 draw_pixel( x1>>1, y1>>1 );
1960 return;
1963 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
1964 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
1965 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
1966 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
1967 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
1968 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
1970 if( w < 0 )
1972 w *= -1;
1973 a = true;
1975 if( h < 0 )
1977 h *= -1;
1978 a = !a;
1980 if( a )
1982 r = r1;
1983 r1 = r2;
1984 r2 = r;
1985 g = g1;
1986 g1 = g2;
1987 g2 = g;
1988 b = b1;
1989 b1 = b2;
1990 b2 = b;
1993 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
1994 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
1996 if( w > h )
1998 if( x1 > x2 )
2000 x = x2;
2001 y = y2;
2002 x2 = x1;
2003 y2 = y1;
2004 x1 = x;
2005 y1 = y;
2007 w = x1 - x2;
2008 h = y1 - y2;
2009 while( x1 <= x2 )
2011 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2012 s1+((s2-s1)*(x1-x2))/w,
2013 v1+((v2-v1)*(x1-x2))/w,
2014 &r, &g, &b );
2015 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2016 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2017 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2018 x1+=2;
2019 y1 = y2 - ( x2 - x1 ) * h / w;
2022 else /* h > w */
2024 if( y1 > y2 )
2026 x = x2;
2027 y = y2;
2028 x2 = x1;
2029 y2 = y1;
2030 x1 = x;
2031 y1 = y;
2033 w = x1 - x2;
2034 h = y1 - y2;
2035 while( y1 <= y2 )
2037 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2038 s1+((s2-s1)*(y1-y2))/h,
2039 v1+((v2-v1)*(y1-y2))/h,
2040 &r, &g, &b );
2041 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2042 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2043 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2044 y1+=2;
2045 x1 = x2 - ( y2 - y1 ) * w / h;
2048 if( a )
2050 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2052 else
2054 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2058 static void linear_gradient( int x1, int y1, int x2, int y2 )
2060 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2061 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2062 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2063 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2064 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2065 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2067 int h1, s1, v1, h2, s2, v2, r, g, b;
2069 /* radius^2 */
2070 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2071 int dist2, i=0;
2073 /* We only propagate the gradient to neighboring pixels with the same
2074 * color as ( x1, y1 ) */
2075 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2077 int x = x1;
2078 int y = y1;
2080 if( radius2 == 0 ) return;
2081 if( preview )
2083 line_gradient( x1, y1, x2, y2 );
2086 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2087 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2089 #define PUSH( x0, y0 ) \
2090 buffer.coord[i].x = (short)(x0); \
2091 buffer.coord[i].y = (short)(y0); \
2092 i++;
2093 #define POP( a, b ) \
2094 i--; \
2095 a = (int)buffer.coord[i].x; \
2096 b = (int)buffer.coord[i].y;
2098 PUSH( x, y );
2100 while( i != 0 )
2102 POP( x, y );
2104 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2105 if( dist2 <= 0 )
2107 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2109 else if( dist2 < radius2 )
2111 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2112 s1+((s2-s1)*dist2)/radius2,
2113 v1+((v2-v1)*dist2)/radius2,
2114 &r, &g, &b );
2115 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2117 else
2119 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2121 if( rp_colors[ drawcolor ] == prev_color )
2123 if( rp_colors[ drawcolor ])
2124 rp_colors[ drawcolor ]--; /* GRUIK */
2125 else
2126 rp_colors[ drawcolor ]++; /* GRUIK */
2128 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2129 draw_pixel( x, y );
2131 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2133 PUSH( x-1, y );
2135 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2137 PUSH( x+1, y );
2139 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2141 PUSH( x, y-1 );
2143 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2145 PUSH( x, y+1 );
2148 #undef PUSH
2149 #undef POP
2151 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2154 static void radial_gradient( int x1, int y1, int x2, int y2 )
2156 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2157 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2158 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2159 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2160 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2161 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2163 int h1, s1, v1, h2, s2, v2, r, g, b;
2165 /* radius^2 */
2166 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2167 int dist2, i=0;
2169 /* We only propagate the gradient to neighboring pixels with the same
2170 * color as ( x1, y1 ) */
2171 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2173 int x = x1;
2174 int y = y1;
2176 if( radius2 == 0 ) return;
2177 if( preview )
2179 line_gradient( x1, y1, x2, y2 );
2182 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2183 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2185 #define PUSH( x0, y0 ) \
2186 buffer.coord[i].x = (short)(x0); \
2187 buffer.coord[i].y = (short)(y0); \
2188 i++;
2189 #define POP( a, b ) \
2190 i--; \
2191 a = (int)buffer.coord[i].x; \
2192 b = (int)buffer.coord[i].y;
2194 PUSH( x, y );
2196 while( i != 0 )
2198 POP( x, y );
2200 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2202 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2203 s1+((s2-s1)*dist2)/radius2,
2204 v1+((v2-v1)*dist2)/radius2,
2205 &r, &g, &b );
2206 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2208 else
2210 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2212 if( rp_colors[ drawcolor ] == prev_color )
2214 if( rp_colors[ drawcolor ])
2215 rp_colors[ drawcolor ]--; /* GRUIK */
2216 else
2217 rp_colors[ drawcolor ]++; /* GRUIK */
2219 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2220 draw_pixel( x, y );
2222 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2224 PUSH( x-1, y );
2226 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2228 PUSH( x+1, y );
2230 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2232 PUSH( x, y-1 );
2234 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2236 PUSH( x, y+1 );
2239 #undef PUSH
2240 #undef POP
2242 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2245 static void draw_toolbars(bool update)
2247 int i;
2248 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2249 rb->lcd_set_background( COLOR_LIGHTGRAY );
2250 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2251 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2252 rb->lcd_set_foreground( COLOR_BLACK );
2253 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2255 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2256 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2257 TB_SC_SIZE, TB_SC_SIZE );
2258 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2259 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2260 TB_SC_SIZE, TB_SC_SIZE );
2261 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2262 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2263 TB_SC_SIZE, TB_SC_SIZE );
2264 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2265 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2266 TB_SC_SIZE, TB_SC_SIZE );
2268 for( i=0; i<18; i++ )
2270 rb->lcd_set_foreground( rp_colors[i] );
2271 rb->lcd_fillrect(
2272 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2273 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2274 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2275 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2276 rb->lcd_drawrect(
2277 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2278 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2279 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2282 #define SEPARATOR( x, y ) \
2283 rb->lcd_set_foreground( COLOR_WHITE ); \
2284 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2285 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2286 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2287 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2289 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2290 TB_TL_WIDTH, TB_TL_HEIGHT );
2291 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2292 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2293 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2294 TB_TL_SIZE, TB_TL_SIZE );
2296 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2298 rb->lcd_setfont( FONT_SYSFIXED );
2299 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2300 rb->lcd_setfont( FONT_UI );
2301 #undef TOP
2303 if( update ) rb->lcd_update();
2306 static void toolbar( void )
2308 int button, i, j;
2309 restore_screen();
2310 draw_toolbars( false );
2311 y = LCD_HEIGHT-TB_HEIGHT/2;
2312 inv_cursor( true );
2313 while( 1 )
2315 switch( button = rb->button_get( true ) )
2317 case ROCKPAINT_DRAW:
2318 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2319 if( y >= TOP + TB_SC_FG_TOP
2320 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2321 && x >= TB_SC_FG_LEFT
2322 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2324 /* click on the foreground color */
2325 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2327 else if( y >= TOP + TB_SC_BG_TOP
2328 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2329 && x >= TB_SC_BG_LEFT
2330 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2332 /* click on the background color */
2333 i = drawcolor;
2334 drawcolor = bgdrawcolor;
2335 bgdrawcolor = i;
2337 else if( y >= TOP + TB_PL_TOP
2338 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2339 && x >= TB_PL_LEFT
2340 && x < TB_PL_LEFT + TB_PL_WIDTH )
2342 /* click on the palette */
2343 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2344 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2345 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2346 break;
2347 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2348 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2349 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2351 else if( y >= TOP+TB_TL_TOP
2352 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2353 && x >= TB_TL_LEFT
2354 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2356 /* click on the tools */
2357 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2358 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2359 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2360 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2361 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2362 tool = i*2+j;
2363 prev_x = -1;
2364 prev_y = -1;
2365 prev_x2 = -1;
2366 prev_y2 = -1;
2367 prev_x3 = -1;
2368 prev_y3 = -1;
2369 preview = false;
2371 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2373 /* menu button */
2374 goto_menu();
2376 #undef TOP
2377 restore_screen();
2378 draw_toolbars( false );
2379 inv_cursor( true );
2380 break;
2382 case ROCKPAINT_LEFT:
2383 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2384 inv_cursor(false);
2385 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2386 if (x<0) x=COLS-1;
2387 inv_cursor(true);
2388 break;
2390 case ROCKPAINT_RIGHT:
2391 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2392 inv_cursor(false);
2393 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2394 if (x>=COLS) x=0;
2395 inv_cursor(true);
2396 break;
2398 case ROCKPAINT_UP:
2399 case ROCKPAINT_UP | BUTTON_REPEAT:
2400 inv_cursor(false);
2401 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2402 if (y<LCD_HEIGHT-TB_HEIGHT)
2404 return;
2406 inv_cursor(true);
2407 break;
2409 case ROCKPAINT_DOWN:
2410 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2411 inv_cursor(false);
2412 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2413 if (y>=LCD_HEIGHT)
2415 y = 0;
2416 return;
2418 inv_cursor(true);
2419 break;
2421 case ROCKPAINT_TOOLBAR:
2422 case ROCKPAINT_TOOLBAR2:
2423 return;
2425 if( quit ) return;
2429 static void inv_cursor(bool update)
2431 rb->lcd_set_foreground(COLOR_BLACK);
2432 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2433 /* cross painting */
2434 rb->lcd_hline(x-4,x-1,y);
2435 rb->lcd_hline(x+1,x+4,y);
2436 rb->lcd_vline(x,y-4,y-1);
2437 rb->lcd_vline(x,y+1,y+4);
2438 rb->lcd_set_foreground(rp_colors[drawcolor]);
2439 rb->lcd_set_drawmode(DRMODE_SOLID);
2441 if( update ) rb->lcd_update();
2444 static void restore_screen(void)
2446 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2449 static void clear_drawing(void)
2451 init_buffer();
2452 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2453 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2454 rb->lcd_update();
2457 static void goto_menu(void)
2459 int multi;
2460 int selected = 0;
2462 while( 1 )
2464 switch( rb->do_menu( &main_menu, &selected, NULL, false ) )
2466 case MAIN_MENU_NEW:
2467 clear_drawing();
2468 return;
2470 case MAIN_MENU_LOAD:
2471 if( browse( filename, MAX_PATH, "/" ) )
2473 if( load_bitmap( filename ) <= 0 )
2475 rb->splashf( 1*HZ, "Error while loading %s",
2476 filename );
2478 else
2480 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2481 restore_screen();
2482 inv_cursor(true);
2483 return;
2486 break;
2488 case MAIN_MENU_SAVE:
2489 rb->lcd_set_foreground(COLOR_BLACK);
2490 if (!filename[0])
2491 rb->strcpy(filename,"/");
2492 if( !rb->kbd_input( filename, MAX_PATH ) )
2494 if(rb->strlen(filename) <= 4 ||
2495 rb->strcasecmp(&filename[rb->strlen(filename)-4], ".bmp"))
2496 rb->strcat(filename, ".bmp");
2497 save_bitmap( filename );
2498 rb->splashf( 1*HZ, "File saved (%s)", filename );
2500 break;
2502 case MAIN_MENU_BRUSH_SIZE:
2503 for(multi = 0; multi<4; multi++)
2504 if(bsize == times_list[multi]) break;
2505 rb->set_option( "Brush Size", &multi, INT, times_options, 4, NULL );
2506 if( multi >= 0 )
2507 bsize = times_list[multi];
2508 break;
2510 case MAIN_MENU_BRUSH_SPEED:
2511 for(multi = 0; multi<3; multi++)
2512 if(bspeed == times_list[multi]) break;
2513 rb->set_option( "Brush Speed", &multi, INT, times_options, 3, NULL );
2514 if( multi >= 0 )
2515 bspeed = times_list[multi];
2516 break;
2518 case MAIN_MENU_COLOR:
2519 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2520 break;
2522 case MAIN_MENU_GRID_SIZE:
2523 for(multi = 0; multi<4; multi++)
2524 if(gridsize == gridsize_list[multi]) break;
2525 rb->set_option( "Grid Size", &multi, INT, gridsize_options, 4, NULL );
2526 if( multi >= 0 )
2527 gridsize = gridsize_list[multi];
2528 break;
2530 case MAIN_MENU_PLAYBACK_CONTROL:
2531 playback_control( NULL );
2532 break;
2534 case MAIN_MENU_EXIT:
2535 restore_screen();
2536 quit=true;
2537 return;
2539 case MAIN_MENU_RESUME:
2540 default:
2541 restore_screen();
2542 return;
2543 }/* end switch */
2544 }/* end while */
2547 static void reset_tool( void )
2549 prev_x = -1;
2550 prev_y = -1;
2551 prev_x2 = -1;
2552 prev_y2 = -1;
2553 prev_x3 = -1;
2554 prev_y3 = -1;
2555 tool_mode = -1;
2556 preview = false;
2559 static bool rockpaint_loop( void )
2561 int button=0,i,j;
2562 int accelaration;
2564 x = 10;
2565 toolbar();
2566 x = 0; y = 0;
2567 restore_screen();
2568 inv_cursor(true);
2570 while (!quit) {
2571 button = rb->button_get(true);
2573 if( tool == Brush && prev_x != -1 )
2575 accelaration = 1;
2577 else if( button & BUTTON_REPEAT )
2579 accelaration = 4;
2581 else
2583 accelaration = 1;
2586 switch(button)
2588 case ROCKPAINT_QUIT:
2589 rb->lcd_set_drawmode(DRMODE_SOLID);
2590 return PLUGIN_OK;
2592 case ROCKPAINT_MENU:
2593 inv_cursor(false);
2594 goto_menu();
2595 restore_screen();
2596 inv_cursor(true);
2597 break;
2599 case ROCKPAINT_DRAW:
2600 inv_cursor(false);
2601 switch( tool )
2603 case Brush:
2604 if( prev_x == -1 ) prev_x = 1;
2605 else prev_x = -1;
2606 break;
2608 case SelectRectangle:
2609 case Line:
2610 case Curve:
2611 case Rectangle:
2612 case RectangleFull:
2613 case Oval:
2614 case OvalFull:
2615 case LinearGradient:
2616 case RadialGradient:
2617 /* Curve uses 4 points, others use 2 */
2618 if( prev_x == -1 || prev_y == -1 )
2620 prev_x = x;
2621 prev_y = y;
2622 preview = true;
2624 else if( tool == Curve
2625 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2627 prev_x2 = x;
2628 prev_y2 = y;
2630 else if( tool == SelectRectangle
2631 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2633 tool_mode = rb->do_menu( &select_menu,
2634 NULL, NULL, false );
2635 switch( tool_mode )
2637 case SELECT_MENU_CUT:
2638 case SELECT_MENU_COPY:
2639 prev_x2 = x;
2640 prev_y2 = y;
2641 copy_to_clipboard();
2642 if( prev_x < x ) x = prev_x;
2643 if( prev_y < y ) y = prev_y;
2644 break;
2646 case SELECT_MENU_INVERT:
2647 draw_invert( prev_x, prev_y, x, y );
2648 reset_tool();
2649 break;
2651 case SELECT_MENU_HFLIP:
2652 draw_hflip( prev_x, prev_y, x, y );
2653 reset_tool();
2654 break;
2656 case SELECT_MENU_VFLIP:
2657 draw_vflip( prev_x, prev_y, x, y );
2658 reset_tool();
2659 break;
2661 case SELECT_MENU_ROTATE90:
2662 draw_rot_90_deg( prev_x, prev_y, x, y, 1 );
2663 reset_tool();
2664 break;
2666 case SELECT_MENU_ROTATE180:
2667 draw_hflip( prev_x, prev_y, x, y );
2668 draw_vflip( prev_x, prev_y, x, y );
2669 reset_tool();
2670 break;
2672 case SELECT_MENU_ROTATE270:
2673 draw_rot_90_deg( prev_x, prev_y, x, y, -1 );
2674 reset_tool();
2675 break;
2677 case SELECT_MENU_CANCEL:
2678 reset_tool();
2679 break;
2681 default:
2682 break;
2684 restore_screen();
2686 else if( tool == Curve
2687 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2689 prev_x3 = x;
2690 prev_y3 = y;
2692 else
2694 preview = false;
2695 switch( tool )
2697 case SelectRectangle:
2698 draw_paste_rectangle( prev_x, prev_y,
2699 prev_x2, prev_y2,
2700 x, y, tool_mode );
2701 break;
2702 case Line:
2703 draw_line( prev_x, prev_y, x, y );
2704 break;
2705 case Curve:
2706 draw_curve( prev_x, prev_y,
2707 prev_x2, prev_y2,
2708 prev_x3, prev_y3,
2709 x, y );
2710 break;
2711 case Rectangle:
2712 draw_rect( prev_x, prev_y, x, y );
2713 break;
2714 case RectangleFull:
2715 draw_rect_full( prev_x, prev_y, x, y );
2716 break;
2717 case Oval:
2718 draw_oval_empty( prev_x, prev_y, x, y );
2719 break;
2720 case OvalFull:
2721 draw_oval_full( prev_x, prev_y, x, y );
2722 break;
2723 case LinearGradient:
2724 linear_gradient( prev_x, prev_y, x, y );
2725 break;
2726 case RadialGradient:
2727 radial_gradient( prev_x, prev_y, x, y );
2728 break;
2729 default:
2730 break;
2732 reset_tool();
2734 break;
2736 case Fill:
2737 draw_fill( x, y );
2738 break;
2740 case ColorPicker:
2741 color_picker( x, y );
2742 break;
2744 case Text:
2745 draw_text( x, y );
2746 break;
2748 default:
2749 break;
2751 inv_cursor(true);
2752 break;
2754 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2755 if( tool == Curve )
2757 /* 3 point bezier curve */
2758 preview = false;
2759 draw_curve( prev_x, prev_y,
2760 prev_x2, prev_y2,
2761 -1, -1,
2762 x, y );
2763 reset_tool();
2764 restore_screen();
2765 inv_cursor( true );
2767 break;
2769 case ROCKPAINT_TOOLBAR:
2770 i = x; j = y;
2771 x = 10;
2772 toolbar();
2773 x = i; y = j;
2774 restore_screen();
2775 inv_cursor(true);
2776 break;
2778 case ROCKPAINT_TOOLBAR2:
2779 i = x; j = y;
2780 x = 110;
2781 toolbar();
2782 x = i; y = j;
2783 restore_screen();
2784 inv_cursor(true);
2785 break;
2787 case ROCKPAINT_LEFT:
2788 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2789 inv_cursor(false);
2790 x-=bspeed * accelaration;
2791 if (x<0) x=COLS-1;
2792 inv_cursor(true);
2793 break;
2795 case ROCKPAINT_RIGHT:
2796 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2797 inv_cursor(false);
2798 x+=bspeed * accelaration;
2799 if (x>=COLS) x=0;
2800 inv_cursor(true);
2801 break;
2803 case ROCKPAINT_UP:
2804 case ROCKPAINT_UP | BUTTON_REPEAT:
2805 inv_cursor(false);
2806 y-=bspeed * accelaration;
2807 if (y<0) y=ROWS-1;
2808 inv_cursor(true);
2809 break;
2811 case ROCKPAINT_DOWN:
2812 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2813 inv_cursor(false);
2814 y+=bspeed * accelaration;
2815 if (y>=ROWS)
2817 toolbar();
2818 restore_screen();
2820 inv_cursor(true);
2821 break;
2823 default:
2824 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2825 return PLUGIN_USB_CONNECTED;
2826 break;
2828 if( tool == Brush && prev_x == 1 )
2830 inv_cursor(false);
2831 draw_brush( x, y );
2832 inv_cursor(true);
2834 if( preview || tool == ColorPicker )
2835 /* always preview color picker */
2837 restore_screen();
2838 switch( tool )
2840 case SelectRectangle:
2841 if( prev_x2 == -1 || prev_y2 == -1 )
2843 /* we are defining the selection */
2844 draw_select_rectangle( prev_x, prev_y, x, y );
2846 else
2848 /* we are pasting the selected data */
2849 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2850 prev_y2, x, y, tool_mode );
2851 prev_x3 = prev_x2-prev_x;
2852 if( prev_x3 < 0 ) prev_x3 *= -1;
2853 prev_y3 = prev_y2-prev_y;
2854 if( prev_y3 < 0 ) prev_y3 *= -1;
2855 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2856 prev_x3 = -1;
2857 prev_y3 = -1;
2859 break;
2861 case Brush:
2862 break;
2864 case Line:
2865 draw_line( prev_x, prev_y, x, y );
2866 break;
2868 case Curve:
2869 if( prev_x2 == -1 || prev_y2 == -1 )
2871 draw_line( prev_x, prev_y, x, y );
2873 else
2875 draw_curve( prev_x, prev_y,
2876 prev_x2, prev_y2,
2877 prev_x3, prev_y3,
2878 x, y );
2880 break;
2882 case Rectangle:
2883 draw_rect( prev_x, prev_y, x, y );
2884 break;
2886 case RectangleFull:
2887 draw_rect_full( prev_x, prev_y, x, y );
2888 break;
2890 case Oval:
2891 draw_oval_empty( prev_x, prev_y, x, y );
2892 break;
2894 case OvalFull:
2895 draw_oval_full( prev_x, prev_y, x, y );
2896 break;
2898 case Fill:
2899 break;
2901 case ColorPicker:
2902 preview = true;
2903 color_picker( x, y );
2904 preview = false;
2905 break;
2907 case LinearGradient:
2908 line_gradient( prev_x, prev_y, x, y );
2909 break;
2911 case RadialGradient:
2912 line_gradient( prev_x, prev_y, x, y );
2913 break;
2915 case Text:
2916 default:
2917 break;
2919 inv_cursor( true );
2921 if( gridsize > 0 )
2923 show_grid( true );
2924 show_grid( false );
2928 return PLUGIN_OK;
2931 static int load_bitmap( const char *file )
2933 struct bitmap bm;
2934 bool ret;
2935 int l;
2937 bm.data = (char*)save_buffer;
2938 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2939 FORMAT_NATIVE, NULL );
2941 if((bm.width > COLS ) || ( bm.height > ROWS ))
2942 return -1;
2944 for( l = bm.height-1; l > 0; l-- )
2946 rb->memmove( save_buffer+l*COLS, save_buffer+l*bm.width,
2947 sizeof( fb_data )*bm.width );
2949 for( l = 0; l < bm.height; l++ )
2951 rb->memset( save_buffer+l*COLS+bm.width, rp_colors[ bgdrawcolor ],
2952 sizeof( fb_data )*(COLS-bm.width) );
2954 rb->memset( save_buffer+COLS*bm.height, rp_colors[ bgdrawcolor ],
2955 sizeof( fb_data )*COLS*(ROWS-bm.height) );
2957 return ret;
2960 static int save_bitmap( char *file )
2962 struct bitmap bm;
2963 bm.data = (char*)save_buffer;
2964 bm.height = ROWS;
2965 bm.width = COLS;
2966 bm.format = FORMAT_NATIVE;
2967 return save_bmp_file( file, &bm );
2970 enum plugin_status plugin_start(UNUSED_ATTR const void* parameter)
2972 rb->lcd_set_foreground(COLOR_WHITE);
2973 rb->lcd_set_backdrop(NULL);
2974 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
2975 rb->splash( HZ/2, "Rock Paint");
2977 rb->lcd_clear_display();
2979 filename[0] = '\0';
2981 if( parameter )
2983 if( load_bitmap( parameter ) <= 0 )
2985 rb->splash( 1*HZ, "File Open Error");
2986 clear_drawing();
2988 else
2990 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
2991 restore_screen();
2992 rb->strcpy( filename, parameter );
2995 else
2997 clear_drawing();
2999 inv_cursor(true);
3001 return rockpaint_loop();