* Fix plugin keymaps for VX777
[kugel-rb.git] / apps / plugins / rockpaint.c
blob78fe7b46feb6221e402e6d23993d5a93570e7da9
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 == ONDAVX777_PAD )
163 #define ROCKPAINT_QUIT BUTTON_POWER
165 #elif CONFIG_KEYPAD == MROBE500_PAD
166 #define ROCKPAINT_QUIT BUTTON_POWER
168 #elif ( CONFIG_KEYPAD == SAMSUNG_YH_PAD )
169 #define ROCKPAINT_QUIT BUTTON_REC
170 #define ROCKPAINT_DRAW BUTTON_PLAY
171 #define ROCKPAINT_MENU BUTTON_FFWD
172 #define ROCKPAINT_TOOLBAR BUTTON_REW
173 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
174 #define ROCKPAINT_UP BUTTON_UP
175 #define ROCKPAINT_DOWN BUTTON_DOWN
176 #define ROCKPAINT_LEFT BUTTON_LEFT
177 #define ROCKPAINT_RIGHT BUTTON_RIGHT
179 #else
180 #error "Please define keys for this keypad"
181 #endif
183 #ifdef HAVE_TOUCHSCREEN
184 #ifndef ROCKPAINT_QUIT
185 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
186 #endif
187 #ifndef ROCKPAINT_DRAW
188 #define ROCKPAINT_DRAW BUTTON_CENTER
189 #endif
190 #ifndef ROCKPAINT_MENU
191 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
192 #endif
193 #ifndef ROCKPAINT_TOOLBAR
194 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
195 #endif
196 #ifndef ROCKPAINT_TOOLBAR2
197 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
198 #endif
199 #ifndef ROCKPAINT_UP
200 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
201 #endif
202 #ifndef ROCKPAINT_DOWN
203 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
204 #endif
205 #ifndef ROCKPAINT_LEFT
206 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
207 #endif
208 #ifndef ROCKPAINT_RIGHT
209 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
210 #endif
211 #endif
213 /***********************************************************************
214 * Palette Default Colors
215 ***********************************************************************/
216 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
217 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
218 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
219 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
220 #define COLOR_RED LCD_RGBPACK(128,0,0)
221 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
222 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
223 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
224 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
225 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
226 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
227 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
228 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
229 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
230 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
231 #define COLOR_PINK LCD_RGBPACK(255,0,255)
232 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
233 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
235 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
236 #define ROCKPAINT_TITLE_FONT 2
238 /***********************************************************************
239 * Program Colors
240 ***********************************************************************/
241 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
242 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
244 #define ROWS LCD_HEIGHT
245 #define COLS LCD_WIDTH
248 * Toolbar positioning stuff ... don't read this unless you really need to
250 * TB Toolbar
251 * SP Separator
252 * SC Selected Color
253 * PL Palette
254 * TL Tools
257 /* Separator sizes */
258 #define TB_SP_MARGIN 3
259 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
261 /* Selected color sizes */
262 #define TB_SC_SIZE 12
264 /* Palette sizes */
265 #define TB_PL_COLOR_SIZE 7
266 #define TB_PL_COLOR_SPACING 2
267 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
268 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
270 /* Tools sizes */
271 #define TB_TL_SIZE 8
272 #define TB_TL_SPACING 2
273 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
274 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
276 /* Menu button size ... gruik */
277 #define TB_MENU_MIN_WIDTH 30
279 /* Selected colors position */
280 #define TB_SC_FG_TOP 2
281 #define TB_SC_FG_LEFT 2
282 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
283 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
285 /* Palette position */
286 #define TB_PL_TOP TB_SC_FG_TOP
287 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
289 /* Tools position */
290 #define TB_TL_TOP TB_SC_FG_TOP
291 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
293 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
294 #undef TB_TL_TOP
295 #undef TB_TL_LEFT
296 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
297 #define TB_TL_LEFT TB_SC_FG_LEFT
298 #endif
300 /* Menu button position */
301 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
302 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
304 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
307 static void draw_pixel(int x,int y);
308 static void draw_line( int x1, int y1, int x2, int y2 );
309 static void draw_rect( int x1, int y1, int x2, int y2 );
310 static void draw_toolbars(bool update);
311 static void inv_cursor(bool update);
312 static void restore_screen(void);
313 static void clear_drawing(void);
314 static void goto_menu(void);
315 static int load_bitmap( const char *filename );
316 static int save_bitmap( char *filename );
317 static void draw_rect_full( int x1, int y1, int x2, int y2 );
319 /***********************************************************************
320 * Global variables
321 ***********************************************************************/
323 static int drawcolor=0; /* Current color (in palette) */
324 static int bgdrawcolor=9; /* Current background color (in palette) */
325 bool isbg = false; /* gruik ugly hack alert */
327 static int preview=false; /* Is preview mode on ? */
329 /* TODO: clean this up */
330 static int x=0, y=0; /* cursor position */
331 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
332 static int prev_x2=-1, prev_y2=-1;
333 static int prev_x3=-1, prev_y3=-1;
334 static int tool_mode=-1;
337 static int bsize=1; /* brush size */
338 static int bspeed=1; /* brush speed */
340 enum Tools { Brush = 0, /* Regular brush */
341 Fill = 1, /* Fill a shape with current color */
342 SelectRectangle = 2,
343 ColorPicker = 3, /* Pick a color */
344 Line = 4, /* Draw a line between two points */
345 Unused = 5, /* THIS IS UNUSED ... */
346 Curve = 6,
347 Text = 7,
348 Rectangle = 8, /* Draw a rectangle */
349 RectangleFull = 9,
350 Oval = 10, /* Draw an oval */
351 OvalFull = 11,
352 LinearGradient = 12,
353 RadialGradient = 13
356 enum Tools tool = Brush;
358 static bool quit=false;
359 static int gridsize=0;
361 static fb_data rp_colors[18] =
363 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
364 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
365 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
366 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
367 COLOR_LIGHTBROWN
370 static fb_data save_buffer[ ROWS*COLS ];
372 extern fb_data rockpaint[];
373 extern fb_data rockpaint_hsvrgb[];
375 /* Maximum string size allowed for the text tool */
376 #define MAX_TEXT 256
378 static union
380 /* Used by fill and gradient algorithms */
381 struct
383 short x;
384 short y;
385 } coord[ ROWS*COLS ];
387 /* Used by bezier curve algorithms */
388 struct
390 short x1, y1;
391 short x2, y2;
392 short x3, y3;
393 short x4, y4;
394 short depth;
395 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
396 * than coord ... so we divide to take
397 * less memory. */
399 /* Used to cut/copy/paste data */
400 fb_data clipboard[ ROWS*COLS ];
402 /* Used for text mode */
403 struct
405 char text[MAX_TEXT];
406 char font[MAX_PATH];
407 char old_font[MAX_PATH];
408 int fh_buf[30];
409 int fw_buf[30];
410 char fontname_buf[30][MAX_PATH];
411 } text;
412 } buffer;
414 /* Current filename */
415 static char filename[MAX_PATH];
417 /* Font preview buffer */
418 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
419 //#define FONT_PREVIEW_HEIGHT 1000
420 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
422 /***********************************************************************
423 * Offscreen buffer/Text/Fonts handling
425 * Parts of code taken from firmware/drivers/lcd-16bit.c
426 ***********************************************************************/
427 static void buffer_mono_bitmap_part(
428 fb_data *buf, int buf_width, int buf_height,
429 const unsigned char *src, int src_x, int src_y,
430 int stride, int x, int y, int width, int height )
431 /* this function only draws the foreground part of the bitmap */
433 const unsigned char *src_end;
434 fb_data *dst, *dst_end;
435 unsigned fgcolor = rb->lcd_get_foreground();
437 /* nothing to draw? */
438 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
439 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
440 return;
442 /* clipping */
443 if( x < 0 )
445 width += x;
446 src_x -= x;
447 x = 0;
449 if( y < 0 )
451 height += y;
452 src_y -= y;
453 y = 0;
455 if( x + width > buf_width )
456 width = buf_width - x;
457 if( y + height > buf_height )
458 height = buf_height - y;
460 src += stride * (src_y >> 3) + src_x; /* move starting point */
461 src_y &= 7;
462 src_end = src + width;
464 dst = buf + y*buf_width + x;
468 const unsigned char *src_col = src++;
469 unsigned data = *src_col >> src_y;
470 fb_data *dst_col = dst++;
471 int numbits = 8 - src_y;
473 dst_end = dst_col + height * buf_width;
476 if( data & 0x01 )
477 *dst_col = fgcolor; /* FIXME ? */
479 dst_col += buf_width;
481 data >>= 1;
482 if( --numbits == 0 )
484 src_col += stride;
485 data = *src_col;
486 numbits = 8;
488 } while( dst_col < dst_end );
489 } while( src < src_end );
492 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
493 int x, int y, int ofs, const unsigned char *str )
495 unsigned short ch;
496 unsigned short *ucs;
498 struct font *pf = rb->font_get( FONT_UI );
499 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
501 ucs = rb->bidi_l2v( str, 1 );
503 while( (ch = *ucs++) != 0 && x < buf_width )
505 int width;
506 const unsigned char *bits;
508 /* get proportional width and glyph bits */
509 width = rb->font_get_width( pf, ch );
511 if( ofs > width )
513 ofs -= width;
514 continue;
517 bits = rb->font_get_bits( pf, ch );
519 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0,
520 width, x, y, width - ofs, pf->height);
522 x += width - ofs;
523 ofs = 0;
527 /***********************************************************************
528 * Menu handling
529 ***********************************************************************/
530 enum {
531 /* Main menu */
532 MAIN_MENU_RESUME,
533 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
534 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
535 MAIN_MENU_GRID_SIZE,
536 MAIN_MENU_PLAYBACK_CONTROL,
537 MAIN_MENU_EXIT,
539 enum {
540 /* Select action menu */
541 SELECT_MENU_CUT, SELECT_MENU_COPY,
542 SELECT_MENU_INVERT, SELECT_MENU_HFLIP, SELECT_MENU_VFLIP,
543 SELECT_MENU_ROTATE90, SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
544 SELECT_MENU_CANCEL,
546 enum {
547 /* Text menu */
548 TEXT_MENU_TEXT, TEXT_MENU_FONT,
549 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
552 MENUITEM_STRINGLIST(main_menu, "RockPaint", NULL,
553 "Resume", "New", "Load", "Save",
554 "Brush Size", "Brush Speed",
555 "Choose Color", "Grid Size",
556 "Playback Control", "Exit");
557 MENUITEM_STRINGLIST(select_menu, "Select...", NULL,
558 "Cut", "Copy",
559 "Invert", "Horizontal Flip", "Vertical Flip",
560 "Rotate 90°", "Rotate 180°", "Rotate 270°",
561 "Cancel");
562 MENUITEM_STRINGLIST(text_menu, "Text", NULL,
563 "Set Text", "Change Font",
564 "Preview", "Apply", "Cancel");
565 static const int times_list[] = { 1, 2, 4, 8 };
566 static const int gridsize_list[] = { 0, 5, 10, 20 };
567 static const struct opt_items times_options[] = {
568 { "1x", -1 }, { "2x", -1 }, { "4x", -1 }, { "8x", -1 }
570 static const struct opt_items gridsize_options[] = {
571 { "No grid", -1 }, { "5px", -1 }, { "10px", -1 }, { "20px", -1 }
574 static int draw_window( int height, int width,
575 int *top, int *left,
576 const char *title )
578 int fh;
579 rb->lcd_getstringsize( title, NULL, &fh );
580 fh++;
582 const int _top = ( LCD_HEIGHT - height ) / 2;
583 const int _left = ( LCD_WIDTH - width ) / 2;
584 if( top ) *top = _top;
585 if( left ) *left = _left;
586 rb->lcd_set_background(COLOR_BLUE);
587 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
588 rb->lcd_fillrect( _left, _top, width, height );
589 rb->lcd_set_foreground(COLOR_BLUE);
590 rb->lcd_fillrect( _left, _top, width, fh+4 );
591 rb->lcd_set_foreground(COLOR_WHITE);
592 rb->lcd_putsxy( _left+2, _top+2, title );
593 rb->lcd_set_foreground(COLOR_BLACK);
594 rb->lcd_drawrect( _left, _top, width, height );
595 return _top+fh+4;
598 /***********************************************************************
599 * File browser
600 ***********************************************************************/
602 char bbuf[MAX_PATH]; /* used by file and font browsers */
603 char bbuf_s[MAX_PATH]; /* used by file and font browsers */
604 struct tree_context *tree = NULL;
606 static bool check_extention(const char *filename, const char *ext)
608 const char *p = rb->strrchr( filename, '.' );
609 return ( p != NULL && !rb->strcasecmp( p, ext ) );
612 static const char* browse_get_name_cb(int selected_item, void *data,
613 char *buffer, size_t buffer_len)
615 int *indexes = (int *) data;
616 struct entry* dc = tree->dircache;
617 struct entry* e = &dc[indexes[selected_item]];
618 (void) buffer;
619 (void) buffer_len;
621 return e->name;
624 static bool browse( char *dst, int dst_size, const char *start )
626 struct gui_synclist browse_list;
627 int item_count = 0, selected, button;
628 struct tree_context backup;
629 struct entry *dc;
630 bool reload = true;
631 int dirfilter = SHOW_ALL;
632 int *indexes = (int *) buffer.clipboard;
634 char *a;
636 rb->strcpy( bbuf, start );
637 a = bbuf+rb->strlen(bbuf)-1;
638 if( *a != '/' )
640 a[1] = '/';
641 a[2] = '\0';
643 bbuf_s[0] = '\0';
645 rb->gui_synclist_init(&browse_list, browse_get_name_cb,
646 (void*) indexes, false, 1, NULL);
648 tree = rb->tree_get_context();
649 backup = *tree;
650 dc = tree->dircache;
651 a = backup.currdir+rb->strlen(backup.currdir)-1;
652 if( *a != '/' )
654 *++a = '/';
655 *++a = '\0';
657 rb->strcpy( a, dc[tree->selected_item].name );
658 tree->dirfilter = &dirfilter;
659 while( 1 )
661 if( reload )
663 int i;
664 rb->set_current_file(bbuf);
665 item_count = 0;
666 selected = 0;
667 for( i = 0; i < tree->filesindir ; i++)
669 /* only displayes directories and .bmp files */
670 if( ((dc[i].attr & ATTR_DIRECTORY ) &&
671 rb->strcmp( dc[i].name, "." ) &&
672 rb->strcmp( dc[i].name, ".." )) ||
673 ( !(dc[i].attr & ATTR_DIRECTORY) &&
674 check_extention( dc[i].name, ".bmp" ) ) )
676 if( !rb->strcmp( dc[i].name, bbuf_s ) )
677 selected = item_count;
678 indexes[item_count++] = i;
682 rb->gui_synclist_set_nb_items(&browse_list,item_count);
683 rb->gui_synclist_select_item(&browse_list, selected);
684 rb->gui_synclist_set_title(&browse_list, bbuf, NOICON);
685 rb->gui_synclist_draw(&browse_list);
686 reload = false;
688 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
689 if (rb->gui_synclist_do_button(&browse_list,&button,LIST_WRAP_UNLESS_HELD))
690 continue;
691 switch( button )
693 case ACTION_STD_CANCEL:
694 if( !rb->strcmp( bbuf, "/" ) )
696 *tree = backup;
697 rb->set_current_file( backup.currdir );
698 return false;
700 rb->strcpy( bbuf_s, ".." );
701 case ACTION_STD_OK:
702 if( button == ACTION_STD_OK )
704 selected = rb->gui_synclist_get_sel_pos( &browse_list );
705 if( selected < 0 || selected >= item_count )
706 break;
707 struct entry* e = &dc[indexes[selected]];
708 rb->strlcpy( bbuf_s, e->name, sizeof( bbuf_s ) );
709 if( !( e->attr & ATTR_DIRECTORY ) )
711 *tree = backup;
712 rb->set_current_file( backup.currdir );
713 rb->snprintf( dst, dst_size, "%s%s", bbuf, bbuf_s );
714 return true;
717 if( !rb->strcmp( bbuf_s, "." ) ) break;
718 a = bbuf+rb->strlen(bbuf);
719 if( !rb->strcmp( bbuf_s, ".." ) )
721 a--;
722 if( a == bbuf ) break;
723 if( *a == '/' ) a--;
724 while( *a != '/' ) a--;
725 rb->strcpy( bbuf_s, ++a );
726 /* select parent directory */
727 bbuf_s[rb->strlen(bbuf_s)-1] = '\0';
728 *a = '\0';
729 reload = true;
730 break;
732 rb->snprintf( a, bbuf+sizeof(bbuf)-a, "%s/", bbuf_s );
733 reload = true;
734 break;
736 case ACTION_STD_MENU:
737 *tree = backup;
738 rb->set_current_file( backup.currdir );
739 return false;
744 /***********************************************************************
745 * Font browser
747 * FIXME: This still needs some work ... it currently only works fine
748 * on the simulators, disk spins too much on real targets -> rendered
749 * font buffer needed.
750 ***********************************************************************/
751 static bool browse_fonts( char *dst, int dst_size )
753 #define WIDTH ( LCD_WIDTH - 20 )
754 #define HEIGHT ( LCD_HEIGHT - 20 )
755 #define LINE_SPACE 2
756 int top, top_inside = 0, left;
758 DIR *d;
759 struct dirent *de;
760 int fvi = 0; /* first visible item */
761 int lvi = 0; /* last visible item */
762 int si = 0; /* selected item */
763 int osi = 0; /* old selected item */
764 int li = 0; /* last item */
765 int nvih = 0; /* next visible item height */
766 int i;
767 int b_need_redraw = 1; /* Do we need to redraw ? */
769 int cp = 0; /* current position */
770 int fh; /* font height */
772 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
773 #define fw_buf buffer.text.fw_buf
774 int fw;
775 #define fontname_buf buffer.text.fontname_buf
777 rb->snprintf( buffer.text.old_font, MAX_PATH,
778 FONT_DIR "/%s.fnt",
779 rb->global_settings->font_file );
781 while( 1 )
783 if( !b_need_redraw )
785 /* we don't need to redraw ... but we need to unselect
786 * the previously selected item */
787 cp = top_inside + LINE_SPACE;
788 for( i = 0; i+fvi < osi; i++ )
790 cp += fh_buf[i] + LINE_SPACE;
792 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
793 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
794 rb->lcd_set_drawmode(DRMODE_SOLID);
797 if( b_need_redraw )
799 b_need_redraw = 0;
801 d = rb->opendir( FONT_DIR "/" );
802 if( !d )
804 return false;
806 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
807 i = 0;
808 li = -1;
809 while( i < fvi )
811 rb->readdir( d );
812 i++;
814 cp = top_inside+LINE_SPACE;
816 rb->lcd_set_foreground(COLOR_BLACK);
817 rb->lcd_set_background(COLOR_LIGHTGRAY);
819 while( cp < top+HEIGHT )
821 de = rb->readdir( d );
822 if( !de )
824 li = i-1;
825 break;
827 if( !check_extention( de->d_name, ".fnt" ) )
828 continue;
829 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
830 de->d_name );
831 rb->font_load( bbuf );
832 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
833 if( nvih > 0 )
835 nvih -= fh;
836 fvi++;
837 if( nvih < 0 ) nvih = 0;
838 i++;
839 continue;
841 if( cp + fh >= top+HEIGHT )
843 nvih = fh;
844 break;
846 rb->lcd_putsxy( left+10, cp, de->d_name );
847 fh_buf[i-fvi] = fh;
848 fw_buf[i-fvi] = fw;
849 cp += fh + LINE_SPACE;
850 rb->strcpy( fontname_buf[i-fvi], bbuf );
851 i++;
853 lvi = i-1;
854 if( li == -1 )
856 if( !(de = rb->readdir( d ) ) )
858 li = lvi;
860 else if( !nvih && check_extention( de->d_name, ".fnt" ) )
862 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
863 de->d_name );
864 rb->font_load( bbuf );
865 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
866 nvih = fh;
869 rb->font_load( buffer.text.old_font );
870 rb->closedir( d );
873 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
874 cp = top_inside + LINE_SPACE;
875 for( i = 0; i+fvi < si; i++ )
877 cp += fh_buf[i] + LINE_SPACE;
879 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
880 rb->lcd_set_drawmode(DRMODE_SOLID);
882 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
884 osi = si;
885 i = fvi;
886 switch( rb->button_get(true) )
888 case ROCKPAINT_UP:
889 case ROCKPAINT_UP|BUTTON_REPEAT:
890 if( si > 0 )
892 si--;
893 if( si<fvi )
895 fvi = si;
898 break;
900 case ROCKPAINT_DOWN:
901 case ROCKPAINT_DOWN|BUTTON_REPEAT:
902 if( li == -1 || si < li )
904 si++;
906 break;
908 case ROCKPAINT_LEFT:
909 return false;
911 case ROCKPAINT_RIGHT:
912 case ROCKPAINT_DRAW:
913 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
914 return true;
917 if( i != fvi || si > lvi )
919 b_need_redraw = 1;
922 if( si<=lvi )
924 nvih = 0;
927 #undef fh_buf
928 #undef fw_buf
929 #undef fontname_buf
930 #undef WIDTH
931 #undef HEIGHT
932 #undef LINE_SPACE
935 /***********************************************************************
936 * HSVRGB Color chooser
937 ***********************************************************************/
938 static unsigned int color_chooser( unsigned int color )
940 int red = RGB_UNPACK_RED( color );
941 int green = RGB_UNPACK_GREEN( color );
942 int blue = RGB_UNPACK_BLUE( color );
943 int hue, saturation, value;
944 int r, g, b; /* temp variables */
945 int i, top, left;
947 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
948 Red = 3, Green = 4, Blue = 5 };
949 enum BaseColor current = Red;
950 bool has_changed;
952 char str[6] = "";
954 restore_screen();
956 rgb2hsv( red, green, blue, &hue, &saturation, &value );
958 while( 1 )
960 has_changed = false;
961 color = LCD_RGBPACK( red, green, blue );
963 #define HEIGHT ( 100 )
964 #define WIDTH ( 150 )
966 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
967 top -= 15;
969 for( i=0; i<100; i++ )
971 hsv2rgb( i*36, saturation, value, &r, &g, &b );
972 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
973 rb->lcd_vline( left+15+i, top+20, top+27 );
974 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
975 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
976 rb->lcd_vline( left+15+i, top+30, top+37 );
977 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
978 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
979 rb->lcd_vline( left+15+i, top+40, top+47 );
980 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
981 rb->lcd_vline( left+15+i, top+50, top+57 );
982 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
983 rb->lcd_vline( left+15+i, top+60, top+67 );
984 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
985 rb->lcd_vline( left+15+i, top+70, top+77 );
988 rb->lcd_set_foreground(COLOR_BLACK);
989 #define POSITION( a, i ) \
990 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
991 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
992 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
993 rb->lcd_drawpixel( left+16+i, top + 28 + a );
994 POSITION( 0, hue/36 );
995 POSITION( 10, saturation*99/255 );
996 POSITION( 20, value*99/255 );
997 POSITION( 30, red*99/255 );
998 POSITION( 40, green*99/255 );
999 POSITION( 50, blue*99/255 );
1000 #undef POSITION
1001 rb->lcd_set_background(COLOR_LIGHTGRAY);
1002 rb->lcd_setfont( FONT_SYSFIXED );
1003 rb->snprintf( str, 6, "%d", hue/10 );
1004 rb->lcd_putsxy( left + 117, top + 20, str );
1005 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1006 rb->lcd_putsxy( left + 117, top + 30, str );
1007 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1008 rb->lcd_putsxy( left + 117, top + 40, str );
1009 rb->snprintf( str, 6, "%d", red );
1010 rb->lcd_putsxy( left + 117, top + 50, str );
1011 rb->snprintf( str, 6, "%d", green );
1012 rb->lcd_putsxy( left + 117, top + 60, str );
1013 rb->snprintf( str, 6, "%d", blue );
1014 rb->lcd_putsxy( left + 117, top + 70, str );
1015 rb->lcd_setfont( FONT_UI );
1017 #define CURSOR( l ) \
1018 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1019 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1020 CURSOR( 5 );
1021 #undef CURSOR
1023 rb->lcd_set_foreground( color );
1024 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1026 rb->lcd_update();
1028 switch( rb->button_get(true) )
1030 case ROCKPAINT_UP:
1031 current = ( current + 5 )%6;
1032 break;
1034 case ROCKPAINT_DOWN:
1035 current = (current + 1 )%6;
1036 break;
1038 case ROCKPAINT_LEFT:
1039 has_changed = true;
1040 switch( current )
1042 case Hue:
1043 hue = ( hue + 3600 - 10 )%3600;
1044 break;
1045 case Saturation:
1046 if( saturation ) saturation--;
1047 break;
1048 case Value:
1049 if( value ) value--;
1050 break;
1051 case Red:
1052 if( red ) red--;
1053 break;
1054 case Green:
1055 if( green ) green--;
1056 break;
1057 case Blue:
1058 if( blue ) blue--;
1059 break;
1061 break;
1063 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1064 has_changed = true;
1065 switch( current )
1067 case Hue:
1068 hue = ( hue + 3600 - 100 )%3600;
1069 break;
1070 case Saturation:
1071 if( saturation >= 8 ) saturation-=8;
1072 else saturation = 0;
1073 break;
1074 case Value:
1075 if( value >= 8 ) value-=8;
1076 else value = 0;
1077 break;
1078 case Red:
1079 if( red >= 8 ) red-=8;
1080 else red = 0;
1081 break;
1082 case Green:
1083 if( green >= 8 ) green-=8;
1084 else green = 0;
1085 break;
1086 case Blue:
1087 if( blue >= 8 ) blue-=8;
1088 else blue = 0;
1089 break;
1091 break;
1093 case ROCKPAINT_RIGHT:
1094 has_changed = true;
1095 switch( current )
1097 case Hue:
1098 hue = ( hue + 10 )%3600;
1099 break;
1100 case Saturation:
1101 if( saturation < 0xff ) saturation++;
1102 break;
1103 case Value:
1104 if( value < 0xff ) value++;
1105 break;
1106 case Red:
1107 if( red < 0xff ) red++;
1108 break;
1109 case Green:
1110 if( green < 0xff ) green++;
1111 break;
1112 case Blue:
1113 if( blue < 0xff ) blue++;
1114 break;
1116 break;
1118 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1119 has_changed = true;
1120 switch( current )
1122 case Hue:
1123 hue = ( hue + 100 )%3600;
1124 break;
1125 case Saturation:
1126 if( saturation < 0xff - 8 ) saturation+=8;
1127 else saturation = 0xff;
1128 break;
1129 case Value:
1130 if( value < 0xff - 8 ) value+=8;
1131 else value = 0xff;
1132 break;
1133 case Red:
1134 if( red < 0xff - 8 ) red+=8;
1135 else red = 0xff;
1136 break;
1137 case Green:
1138 if( green < 0xff - 8 ) green+=8;
1139 else green = 0xff;
1140 break;
1141 case Blue:
1142 if( blue < 0xff - 8 ) blue+=8;
1143 else blue = 0xff;
1144 break;
1146 break;
1148 case ROCKPAINT_DRAW:
1149 return color;
1151 if( has_changed )
1153 switch( current )
1155 case Hue:
1156 case Saturation:
1157 case Value:
1158 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1159 break;
1161 case Red:
1162 case Green:
1163 case Blue:
1164 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1165 break;
1168 #undef HEIGHT
1169 #undef WIDTH
1173 /***********************************************************************
1174 * Misc routines
1175 ***********************************************************************/
1176 static void init_buffer(void)
1178 int i;
1179 fb_data color = rp_colors[ bgdrawcolor ];
1180 for( i = 0; i < ROWS*COLS; i++ )
1182 save_buffer[i] = color;
1186 static void draw_pixel(int x,int y)
1188 if( !preview )
1190 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1191 if( isbg )
1193 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1195 else
1197 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1200 rb->lcd_drawpixel(x,y);
1203 static void color_picker( int x, int y )
1205 if( preview )
1207 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1208 #define PSIZE 12
1209 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1210 if( x >= COLS - PSIZE ) x -= PSIZE + 2;
1211 if( y >= ROWS - PSIZE ) y -= PSIZE + 2;
1212 rb->lcd_drawrect( x + 2, y + 2, PSIZE - 2, PSIZE - 2 );
1213 rb->lcd_set_drawmode(DRMODE_SOLID);
1214 rb->lcd_drawrect( x + 3, y + 3, PSIZE - 4, PSIZE - 4 );
1215 #undef PSIZE
1216 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1218 else
1220 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1224 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1225 /* This is a preview mode only function */
1227 int i,a;
1228 if( x1 > x2 )
1230 i = x1;
1231 x1 = x2;
1232 x2 = i;
1234 if( y1 > y2 )
1236 i = y1;
1237 y1 = y2;
1238 y2 = i;
1240 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1241 i = 0;
1242 for( a = x1; a < x2; a++, i++ )
1243 if( i%2 )
1244 rb->lcd_drawpixel( a, y1 );
1245 for( a = y1; a < y2; a++, i++ )
1246 if( i%2 )
1247 rb->lcd_drawpixel( x2, a );
1248 if( y2 != y1 )
1249 for( a = x2; a > x1; a--, i++ )
1250 if( i%2 )
1251 rb->lcd_drawpixel( a, y2 );
1252 if( x2 != x1 )
1253 for( a = y2; a > y1; a--, i++ )
1254 if( i%2 )
1255 rb->lcd_drawpixel( x1, a );
1256 rb->lcd_set_drawmode(DRMODE_SOLID);
1259 static void copy_to_clipboard( void )
1261 /* This needs to be optimised ... but i'm lazy ATM */
1262 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1265 /* no preview mode handling atm ... do we need it ? (one if) */
1266 static void draw_invert( int x1, int y1, int x2, int y2 )
1268 int i;
1269 if( x1 > x2 )
1271 i = x1;
1272 x1 = x2;
1273 x2 = i;
1275 if( y1 > y2 )
1277 i = y1;
1278 y1 = y2;
1279 y2 = i;
1282 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1283 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1284 rb->lcd_set_drawmode(DRMODE_SOLID);
1286 for( ; y1<=y2; y1++ )
1288 for( i = x1; i<=x2; i++ )
1290 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1293 /*if( update )*/ rb->lcd_update();
1296 static void draw_hflip( int x1, int y1, int x2, int y2 )
1298 int i;
1299 if( x1 > x2 )
1301 i = x1;
1302 x1 = x2;
1303 x2 = i;
1305 if( y1 > y2 )
1307 i = y1;
1308 y1 = y2;
1309 y2 = i;
1312 copy_to_clipboard();
1314 for( i = 0; i <= y2 - y1; i++ )
1316 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1317 buffer.clipboard+(y2-i)*COLS+x1,
1318 (x2-x1+1)*sizeof( fb_data ) );
1320 restore_screen();
1321 rb->lcd_update();
1324 static void draw_vflip( int x1, int y1, int x2, int y2 )
1326 int i;
1327 if( x1 > x2 )
1329 i = x1;
1330 x1 = x2;
1331 x2 = i;
1333 if( y1 > y2 )
1335 i = y1;
1336 y1 = y2;
1337 y2 = i;
1340 copy_to_clipboard();
1342 for( ; y1 <= y2; y1++ )
1344 for( i = 0; i <= x2 - x1; i++ )
1346 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1349 restore_screen();
1350 rb->lcd_update();
1353 /* direction: -1 = left, 1 = right */
1354 static void draw_rot_90_deg( int x1, int y1, int x2, int y2, int direction )
1356 int i, j;
1357 if( x1 > x2 )
1359 i = x1;
1360 x1 = x2;
1361 x2 = i;
1363 if( y1 > y2 )
1365 i = y1;
1366 y1 = y2;
1367 y2 = i;
1370 copy_to_clipboard();
1372 fb_data color = rp_colors[ bgdrawcolor ];
1373 const int width = x2 - x1, height = y2 - y1;
1374 const int sub_half = width/2-height/2, add_half = (width+height)/2;
1375 if( width > height )
1377 for( i = 0; i <= height; i++ )
1379 for( j = 0; j < sub_half; j++ )
1380 save_buffer[(y1+i)*COLS+x1+j] = color;
1381 for( j = add_half+1; j <= width; j++ )
1382 save_buffer[(y1+i)*COLS+x1+j] = color;
1385 else if( width < height )
1387 for( j = 0; j <= width; j++ )
1389 for( i = 0; i < -sub_half; i++ )
1390 save_buffer[(y1+i)*COLS+x1+j] = color;
1391 for( i = add_half+1; i <= height; i++ )
1392 save_buffer[(y1+i)*COLS+x1+j] = color;
1395 int x3 = x1 + sub_half, y3 = y1 - sub_half;
1396 int is = x3<0?-x3:0, ie = COLS-x3-1, js = y3<0?-y3:0, je = ROWS-y3-1;
1397 if( ie > height ) ie = height;
1398 if( je > width ) je = width;
1399 for( i = is; i <= ie; i++ )
1401 for( j = js; j <= je; j++ )
1403 int x, y;
1404 if(direction > 0)
1406 x = x1+j;
1407 y = y1+height-i;
1409 else
1411 x = x1+width-j;
1412 y = y1+i;
1414 save_buffer[(y3+j)*COLS+x3+i] = buffer.clipboard[y*COLS+x];
1417 restore_screen();
1418 rb->lcd_update();
1421 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1422 int src_y2, int x1, int y1, int mode )
1424 int i, width, height;
1425 if( mode == SELECT_MENU_CUT )
1427 i = drawcolor;
1428 drawcolor = bgdrawcolor;
1429 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1430 drawcolor = i;
1432 if( src_x1 > src_x2 )
1434 i = src_x1;
1435 src_x1 = src_x2;
1436 src_x2 = i;
1438 if( src_y1 > src_y2 )
1440 i = src_y1;
1441 src_y1 = src_y2;
1442 src_y2 = i;
1444 width = src_x2 - src_x1 + 1;
1445 height = src_y2 - src_y1 + 1;
1446 /* clipping */
1447 if( x1 + width > COLS )
1448 width = COLS - x1;
1449 if( y1 + height > ROWS )
1450 height = ROWS - y1;
1452 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1453 x1, y1, width, height );
1454 if( !preview )
1456 for( i = 0; i < height; i++ )
1458 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1459 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1460 width*sizeof( fb_data ) );
1465 static void show_grid( bool update )
1467 int i;
1468 if( gridsize > 0 )
1470 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1471 for( i = gridsize; i < COLS; i+= gridsize )
1473 rb->lcd_vline( i, 0, ROWS-1 );
1475 for( i = gridsize; i < ROWS; i+= gridsize )
1477 rb->lcd_hline( 0, COLS-1, i );
1479 rb->lcd_set_drawmode(DRMODE_SOLID);
1480 if( update ) rb->lcd_update();
1484 static void draw_text( int x, int y )
1486 int selected = 0;
1487 buffer.text.text[0] = '\0';
1488 rb->snprintf( buffer.text.old_font, MAX_PATH,
1489 FONT_DIR "/%s.fnt",
1490 rb->global_settings->font_file );
1491 while( 1 )
1493 switch( rb->do_menu( &text_menu, &selected, NULL, NULL ) )
1495 case TEXT_MENU_TEXT:
1496 rb->lcd_set_foreground(COLOR_BLACK);
1497 rb->kbd_input( buffer.text.text, MAX_TEXT );
1498 break;
1500 case TEXT_MENU_FONT:
1501 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1503 rb->font_load( buffer.text.font );
1505 break;
1507 case TEXT_MENU_PREVIEW:
1508 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1509 while( 1 )
1511 int button;
1512 restore_screen();
1513 rb->lcd_putsxy( x, y, buffer.text.text );
1514 rb->lcd_update();
1515 switch( button = rb->button_get( true ) )
1517 case ROCKPAINT_LEFT:
1518 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1519 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1520 if (x<0) x=COLS-1;
1521 break;
1523 case ROCKPAINT_RIGHT:
1524 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1525 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1526 if (x>=COLS) x=0;
1527 break;
1529 case ROCKPAINT_UP:
1530 case ROCKPAINT_UP | BUTTON_REPEAT:
1531 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1532 if (y<0) y=ROWS-1;
1533 break;
1535 case ROCKPAINT_DOWN:
1536 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1537 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1538 if (y>=ROWS-1) y=0;
1539 break;
1541 case ROCKPAINT_DRAW:
1542 break;
1543 default:
1544 if(rb->default_event_handler(button)
1545 == SYS_USB_CONNECTED)
1546 button = ROCKPAINT_DRAW;
1547 break;
1549 if( button == ROCKPAINT_DRAW ) break;
1551 break;
1553 case TEXT_MENU_APPLY:
1554 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1555 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1556 buffer.text.text );
1557 case TEXT_MENU_CANCEL:
1558 default:
1559 restore_screen();
1560 rb->font_load( buffer.text.old_font );
1561 return;
1566 static void draw_brush( int x, int y )
1568 int i,j;
1569 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1571 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1573 draw_pixel( x+i, y+j );
1578 /* This is an implementation of Bresenham's line algorithm.
1579 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1581 static void draw_line( int x1, int y1, int x2, int y2 )
1583 int x = x1;
1584 int y = y1;
1585 int deltax = x2 - x1;
1586 int deltay = y2 - y1;
1587 int i;
1589 int xerr = abs(deltax);
1590 int yerr = abs(deltay);
1591 int xstep = deltax > 0 ? 1 : -1;
1592 int ystep = deltay > 0 ? 1 : -1;
1593 int err;
1595 if (yerr > xerr)
1597 /* more vertical */
1598 err = yerr;
1599 xerr <<= 1;
1600 yerr <<= 1;
1602 /* to leave off the last pixel of the line, leave off the "+ 1" */
1603 for (i = abs(deltay) + 1; i; --i)
1605 draw_pixel(x, y);
1606 y += ystep;
1607 err -= xerr;
1608 if (err < 0) {
1609 x += xstep;
1610 err += yerr;
1614 else
1616 /* more horizontal */
1617 err = xerr;
1618 xerr <<= 1;
1619 yerr <<= 1;
1621 for (i = abs(deltax) + 1; i; --i)
1623 draw_pixel(x, y);
1624 x += xstep;
1625 err -= yerr;
1626 if (err < 0) {
1627 y += ystep;
1628 err += xerr;
1634 static void draw_curve( int x1, int y1, int x2, int y2,
1635 int xa, int ya, int xb, int yb )
1637 int i = 0;
1638 short xl1, yl1;
1639 short xl2, yl2;
1640 short xl3, yl3;
1641 short xl4, yl4;
1642 short xr1, yr1;
1643 short xr2, yr2;
1644 short xr3, yr3;
1645 short xr4, yr4;
1646 short depth;
1647 short xh, yh;
1649 if( x1 == x2 && y1 == y2 )
1651 draw_pixel( x1, y1 );
1652 return;
1655 // if( preview )
1657 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1658 if( xa == -1 || ya == -1 )
1660 rb->lcd_drawline( x1, y1, xb, yb );
1661 rb->lcd_drawline( x2, y2, xb, yb );
1663 else
1665 rb->lcd_drawline( x1, y1, xa, ya );
1666 rb->lcd_drawline( x2, y2, xb, yb );
1668 rb->lcd_set_drawmode(DRMODE_SOLID);
1671 if( xa == -1 || ya == -1 )
1672 /* We only have 3 of the points
1673 * This will currently only be used in preview mode */
1675 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1676 buffer.bezier[i].x1 = a1; \
1677 buffer.bezier[i].y1 = b1; \
1678 buffer.bezier[i].x2 = a2; \
1679 buffer.bezier[i].y2 = b2; \
1680 buffer.bezier[i].x3 = a3; \
1681 buffer.bezier[i].y3 = b3; \
1682 buffer.bezier[i].depth = d; \
1683 i++;
1684 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1685 i--; \
1686 a1 = buffer.bezier[i].x1; \
1687 b1 = buffer.bezier[i].y1; \
1688 a2 = buffer.bezier[i].x2; \
1689 b2 = buffer.bezier[i].y2; \
1690 a3 = buffer.bezier[i].x3; \
1691 b3 = buffer.bezier[i].y3; \
1692 d = buffer.bezier[i].depth;
1693 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1694 while( i )
1696 /* de Casteljau's algorithm (see wikipedia) */
1697 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1698 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1700 xl2 = ( xl1 + xb )>>1;
1701 yl2 = ( yl1 + yb )>>1;
1702 xr2 = ( xb + xr3 )>>1;
1703 yr2 = ( yb + yr3 )>>1;
1704 xr1 = ( xl2 + xr2 )>>1;
1705 yr1 = ( yl2 + yr2 )>>1;
1706 xl3 = xr1;
1707 yl3 = yr1;
1708 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1709 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1711 else
1713 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1714 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1717 #undef PUSH
1718 #undef POP
1720 else /* We have the 4 points */
1722 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1723 buffer.bezier[i].x1 = a1; \
1724 buffer.bezier[i].y1 = b1; \
1725 buffer.bezier[i].x2 = a2; \
1726 buffer.bezier[i].y2 = b2; \
1727 buffer.bezier[i].x3 = a3; \
1728 buffer.bezier[i].y3 = b3; \
1729 buffer.bezier[i].x4 = a4; \
1730 buffer.bezier[i].y4 = b4; \
1731 buffer.bezier[i].depth = d; \
1732 i++;
1733 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1734 i--; \
1735 a1 = buffer.bezier[i].x1; \
1736 b1 = buffer.bezier[i].y1; \
1737 a2 = buffer.bezier[i].x2; \
1738 b2 = buffer.bezier[i].y2; \
1739 a3 = buffer.bezier[i].x3; \
1740 b3 = buffer.bezier[i].y3; \
1741 a4 = buffer.bezier[i].x4; \
1742 b4 = buffer.bezier[i].y4; \
1743 d = buffer.bezier[i].depth;
1745 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1746 while( i )
1748 /* de Casteljau's algorithm (see wikipedia) */
1749 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1750 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1752 xl2 = ( xl1 + xa )>>1;
1753 yl2 = ( yl1 + ya )>>1;
1754 xh = ( xa + xb )>>1;
1755 yh = ( ya + yb )>>1;
1756 xr3 = ( xb + xr4 )>>1;
1757 yr3 = ( yb + yr4 )>>1;
1758 xl3 = ( xl2 + xh )>>1;
1759 yl3 = ( yl2 + yh )>>1;
1760 xr2 = ( xr3 + xh )>>1;
1761 yr2 = ( yr3 + yh )>>1;
1762 xl4 = ( xl3 + xr2 )>>1;
1763 yl4 = ( yl3 + yr2 )>>1;
1764 xr1 = xl4;
1765 yr1 = yl4;
1766 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1767 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1769 else
1771 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1772 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1775 #undef PUSH
1776 #undef POP
1780 static void draw_rect( int x1, int y1, int x2, int y2 )
1782 draw_line( x1, y1, x1, y2 );
1783 draw_line( x1, y1, x2, y1 );
1784 draw_line( x1, y2, x2, y2 );
1785 draw_line( x2, y1, x2, y2 );
1788 static void togglebg( void )
1790 if( isbg )
1792 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1794 else
1796 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1798 isbg = !isbg;
1801 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1803 /* GRUIK */
1804 int x;
1805 togglebg();
1806 if( x1 > x2 )
1808 x = x1;
1809 x1 = x2;
1810 x2 = x;
1812 x = x1;
1813 do {
1814 draw_line( x, y1, x, y2 );
1815 } while( ++x <= x2 );
1816 togglebg();
1817 draw_rect( x1, y1, x2, y2 );
1820 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1822 /* TODO: simplify :) */
1823 int cx = (x1+x2)>>1;
1824 int cy = (y1+y2)>>1;
1826 int rx = (x1-x2)>>1;
1827 int ry = (y1-y2)>>1;
1828 if( rx < 0 ) rx *= -1;
1829 if( ry < 0 ) ry *= -1;
1831 if( rx == 0 || ry == 0 )
1833 draw_line( x1, y1, x2, y2 );
1834 return;
1837 int x,y;
1838 int dst, old_dst;
1840 for( x = 0; x < rx; x++ )
1842 y = 0;
1843 dst = -0xfff;
1844 do {
1845 old_dst = dst;
1846 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1847 y++;
1848 } while( dst < 0 );
1849 if( -old_dst < dst ) y--;
1850 if( full )
1852 draw_line( cx+x, cy, cx+x, cy+y );
1853 draw_line( cx+x, cy, cx+x, cy-y );
1854 draw_line( cx-x, cy, cx-x, cy+y );
1855 draw_line( cx-x, cy, cx-x, cy-y );
1857 else
1859 draw_pixel( cx+x, cy+y );
1860 draw_pixel( cx+x, cy-y );
1861 draw_pixel( cx-x, cy+y );
1862 draw_pixel( cx-x, cy-y );
1865 for( y = 0; y < ry; y++ )
1867 x = 0;
1868 dst = -0xfff;
1869 do {
1870 old_dst = dst;
1871 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1872 x++;
1873 } while( dst < 0 );
1874 if( -old_dst < dst ) x--;
1875 if( full )
1877 draw_line( cx+x, cy, cx+x, cy+y );
1878 draw_line( cx+x, cy, cx+x, cy-y );
1879 draw_line( cx-x, cy, cx-x, cy+y );
1880 draw_line( cx-x, cy, cx-x, cy-y );
1882 else
1884 draw_pixel( cx+x, cy+y );
1885 draw_pixel( cx+x, cy-y );
1886 draw_pixel( cx-x, cy+y );
1887 draw_pixel( cx-x, cy-y );
1892 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1894 draw_oval( x1, y1, x2, y2, false );
1897 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1899 togglebg();
1900 draw_oval( x1, y1, x2, y2, true );
1901 togglebg();
1902 draw_oval( x1, y1, x2, y2, false );
1905 static void draw_fill( int x0, int y0 )
1907 #define PUSH( a, b ) \
1908 draw_pixel( (int)a, (int)b ); \
1909 buffer.coord[i].x = a; \
1910 buffer.coord[i].y = b; \
1911 i++;
1912 #define POP( a, b ) \
1913 i--; \
1914 a = buffer.coord[i].x; \
1915 b = buffer.coord[i].y;
1917 unsigned int i=0;
1918 short x = x0;
1919 short y = y0;
1920 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1922 if( prev_color == rp_colors[ drawcolor ] ) return;
1924 PUSH( x, y );
1926 while( i != 0 )
1928 POP( x, y );
1929 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1931 PUSH( x-1, y );
1933 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1935 PUSH( x+1, y );
1937 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1939 PUSH( x, y-1 );
1941 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1943 PUSH( x, y+1 );
1946 #undef PUSH
1947 #undef POP
1951 /* For preview purposes only */
1952 static void line_gradient( int x1, int y1, int x2, int y2 )
1954 int r1, g1, b1;
1955 int r2, g2, b2;
1956 int h1, s1, v1, h2, s2, v2, r, g, b;
1957 int w, h, x, y;
1959 bool a = false;
1961 x1 <<= 1;
1962 y1 <<= 1;
1963 x2 <<= 1;
1964 y2 <<= 1;
1966 w = x1 - x2;
1967 h = y1 - y2;
1969 if( w == 0 && h == 0 )
1971 draw_pixel( x1>>1, y1>>1 );
1972 return;
1975 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
1976 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
1977 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
1978 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
1979 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
1980 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
1982 if( w < 0 )
1984 w *= -1;
1985 a = true;
1987 if( h < 0 )
1989 h *= -1;
1990 a = !a;
1992 if( a )
1994 r = r1;
1995 r1 = r2;
1996 r2 = r;
1997 g = g1;
1998 g1 = g2;
1999 g2 = g;
2000 b = b1;
2001 b1 = b2;
2002 b2 = b;
2005 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2006 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2008 if( w > h )
2010 if( x1 > x2 )
2012 x = x2;
2013 y = y2;
2014 x2 = x1;
2015 y2 = y1;
2016 x1 = x;
2017 y1 = y;
2019 w = x1 - x2;
2020 h = y1 - y2;
2021 while( x1 <= x2 )
2023 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2024 s1+((s2-s1)*(x1-x2))/w,
2025 v1+((v2-v1)*(x1-x2))/w,
2026 &r, &g, &b );
2027 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2028 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2029 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2030 x1+=2;
2031 y1 = y2 - ( x2 - x1 ) * h / w;
2034 else /* h > w */
2036 if( y1 > y2 )
2038 x = x2;
2039 y = y2;
2040 x2 = x1;
2041 y2 = y1;
2042 x1 = x;
2043 y1 = y;
2045 w = x1 - x2;
2046 h = y1 - y2;
2047 while( y1 <= y2 )
2049 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2050 s1+((s2-s1)*(y1-y2))/h,
2051 v1+((v2-v1)*(y1-y2))/h,
2052 &r, &g, &b );
2053 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2054 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2055 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2056 y1+=2;
2057 x1 = x2 - ( y2 - y1 ) * w / h;
2060 if( a )
2062 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2064 else
2066 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2070 static void linear_gradient( int x1, int y1, int x2, int y2 )
2072 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2073 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2074 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2075 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2076 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2077 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2079 int h1, s1, v1, h2, s2, v2, r, g, b;
2081 /* radius^2 */
2082 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2083 int dist2, i=0;
2085 /* We only propagate the gradient to neighboring pixels with the same
2086 * color as ( x1, y1 ) */
2087 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2089 int x = x1;
2090 int y = y1;
2092 if( radius2 == 0 ) return;
2093 if( preview )
2095 line_gradient( x1, y1, x2, y2 );
2098 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2099 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2101 #define PUSH( x0, y0 ) \
2102 buffer.coord[i].x = (short)(x0); \
2103 buffer.coord[i].y = (short)(y0); \
2104 i++;
2105 #define POP( a, b ) \
2106 i--; \
2107 a = (int)buffer.coord[i].x; \
2108 b = (int)buffer.coord[i].y;
2110 PUSH( x, y );
2112 while( i != 0 )
2114 POP( x, y );
2116 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2117 if( dist2 <= 0 )
2119 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2121 else if( dist2 < radius2 )
2123 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2124 s1+((s2-s1)*dist2)/radius2,
2125 v1+((v2-v1)*dist2)/radius2,
2126 &r, &g, &b );
2127 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2129 else
2131 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2133 if( rp_colors[ drawcolor ] == prev_color )
2135 if( rp_colors[ drawcolor ])
2136 rp_colors[ drawcolor ]--; /* GRUIK */
2137 else
2138 rp_colors[ drawcolor ]++; /* GRUIK */
2140 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2141 draw_pixel( x, y );
2143 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2145 PUSH( x-1, y );
2147 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2149 PUSH( x+1, y );
2151 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2153 PUSH( x, y-1 );
2155 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2157 PUSH( x, y+1 );
2160 #undef PUSH
2161 #undef POP
2163 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2166 static void radial_gradient( int x1, int y1, int x2, int y2 )
2168 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2169 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2170 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2171 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2172 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2173 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2175 int h1, s1, v1, h2, s2, v2, r, g, b;
2177 /* radius^2 */
2178 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2179 int dist2, i=0;
2181 /* We only propagate the gradient to neighboring pixels with the same
2182 * color as ( x1, y1 ) */
2183 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2185 int x = x1;
2186 int y = y1;
2188 if( radius2 == 0 ) return;
2189 if( preview )
2191 line_gradient( x1, y1, x2, y2 );
2194 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2195 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2197 #define PUSH( x0, y0 ) \
2198 buffer.coord[i].x = (short)(x0); \
2199 buffer.coord[i].y = (short)(y0); \
2200 i++;
2201 #define POP( a, b ) \
2202 i--; \
2203 a = (int)buffer.coord[i].x; \
2204 b = (int)buffer.coord[i].y;
2206 PUSH( x, y );
2208 while( i != 0 )
2210 POP( x, y );
2212 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2214 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2215 s1+((s2-s1)*dist2)/radius2,
2216 v1+((v2-v1)*dist2)/radius2,
2217 &r, &g, &b );
2218 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2220 else
2222 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2224 if( rp_colors[ drawcolor ] == prev_color )
2226 if( rp_colors[ drawcolor ])
2227 rp_colors[ drawcolor ]--; /* GRUIK */
2228 else
2229 rp_colors[ drawcolor ]++; /* GRUIK */
2231 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2232 draw_pixel( x, y );
2234 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2236 PUSH( x-1, y );
2238 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2240 PUSH( x+1, y );
2242 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2244 PUSH( x, y-1 );
2246 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2248 PUSH( x, y+1 );
2251 #undef PUSH
2252 #undef POP
2254 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2257 static void draw_toolbars(bool update)
2259 int i;
2260 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2261 rb->lcd_set_background( COLOR_LIGHTGRAY );
2262 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2263 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2264 rb->lcd_set_foreground( COLOR_BLACK );
2265 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2267 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2268 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2269 TB_SC_SIZE, TB_SC_SIZE );
2270 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2271 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2272 TB_SC_SIZE, TB_SC_SIZE );
2273 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2274 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2275 TB_SC_SIZE, TB_SC_SIZE );
2276 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2277 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2278 TB_SC_SIZE, TB_SC_SIZE );
2280 for( i=0; i<18; i++ )
2282 rb->lcd_set_foreground( rp_colors[i] );
2283 rb->lcd_fillrect(
2284 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2285 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2286 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2287 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2288 rb->lcd_drawrect(
2289 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2290 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2291 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2294 #define SEPARATOR( x, y ) \
2295 rb->lcd_set_foreground( COLOR_WHITE ); \
2296 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2297 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2298 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2299 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2301 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2302 TB_TL_WIDTH, TB_TL_HEIGHT );
2303 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2304 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2305 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2306 TB_TL_SIZE, TB_TL_SIZE );
2308 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2310 rb->lcd_setfont( FONT_SYSFIXED );
2311 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2312 rb->lcd_setfont( FONT_UI );
2313 #undef TOP
2315 if( update ) rb->lcd_update();
2318 static void toolbar( void )
2320 int button, i, j;
2321 restore_screen();
2322 draw_toolbars( false );
2323 y = LCD_HEIGHT-TB_HEIGHT/2;
2324 inv_cursor( true );
2325 while( 1 )
2327 switch( button = rb->button_get( true ) )
2329 case ROCKPAINT_DRAW:
2330 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2331 if( y >= TOP + TB_SC_FG_TOP
2332 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2333 && x >= TB_SC_FG_LEFT
2334 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2336 /* click on the foreground color */
2337 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2339 else if( y >= TOP + TB_SC_BG_TOP
2340 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2341 && x >= TB_SC_BG_LEFT
2342 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2344 /* click on the background color */
2345 i = drawcolor;
2346 drawcolor = bgdrawcolor;
2347 bgdrawcolor = i;
2349 else if( y >= TOP + TB_PL_TOP
2350 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2351 && x >= TB_PL_LEFT
2352 && x < TB_PL_LEFT + TB_PL_WIDTH )
2354 /* click on the palette */
2355 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2356 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2357 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2358 break;
2359 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2360 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2361 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2363 else if( y >= TOP+TB_TL_TOP
2364 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2365 && x >= TB_TL_LEFT
2366 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2368 /* click on the tools */
2369 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2370 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2371 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2372 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2373 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2374 tool = i*2+j;
2375 prev_x = -1;
2376 prev_y = -1;
2377 prev_x2 = -1;
2378 prev_y2 = -1;
2379 prev_x3 = -1;
2380 prev_y3 = -1;
2381 preview = false;
2383 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2385 /* menu button */
2386 goto_menu();
2388 #undef TOP
2389 restore_screen();
2390 draw_toolbars( false );
2391 inv_cursor( true );
2392 break;
2394 case ROCKPAINT_LEFT:
2395 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2396 inv_cursor(false);
2397 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2398 if (x<0) x=COLS-1;
2399 inv_cursor(true);
2400 break;
2402 case ROCKPAINT_RIGHT:
2403 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2404 inv_cursor(false);
2405 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2406 if (x>=COLS) x=0;
2407 inv_cursor(true);
2408 break;
2410 case ROCKPAINT_UP:
2411 case ROCKPAINT_UP | BUTTON_REPEAT:
2412 inv_cursor(false);
2413 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2414 if (y<LCD_HEIGHT-TB_HEIGHT)
2416 return;
2418 inv_cursor(true);
2419 break;
2421 case ROCKPAINT_DOWN:
2422 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2423 inv_cursor(false);
2424 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2425 if (y>=LCD_HEIGHT)
2427 y = 0;
2428 return;
2430 inv_cursor(true);
2431 break;
2433 case ROCKPAINT_TOOLBAR:
2434 case ROCKPAINT_TOOLBAR2:
2435 return;
2437 if( quit ) return;
2441 static void inv_cursor(bool update)
2443 rb->lcd_set_foreground(COLOR_BLACK);
2444 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2445 /* cross painting */
2446 rb->lcd_hline(x-4,x-1,y);
2447 rb->lcd_hline(x+1,x+4,y);
2448 rb->lcd_vline(x,y-4,y-1);
2449 rb->lcd_vline(x,y+1,y+4);
2450 rb->lcd_set_foreground(rp_colors[drawcolor]);
2451 rb->lcd_set_drawmode(DRMODE_SOLID);
2453 if( update ) rb->lcd_update();
2456 static void restore_screen(void)
2458 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2461 static void clear_drawing(void)
2463 init_buffer();
2464 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2465 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2466 rb->lcd_update();
2469 static void goto_menu(void)
2471 int multi;
2472 int selected = 0;
2474 while( 1 )
2476 switch( rb->do_menu( &main_menu, &selected, NULL, false ) )
2478 case MAIN_MENU_NEW:
2479 clear_drawing();
2480 return;
2482 case MAIN_MENU_LOAD:
2483 if( browse( filename, MAX_PATH, "/" ) )
2485 if( load_bitmap( filename ) <= 0 )
2487 rb->splashf( 1*HZ, "Error while loading %s",
2488 filename );
2489 clear_drawing();
2491 else
2493 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2494 restore_screen();
2495 inv_cursor(true);
2496 return;
2499 break;
2501 case MAIN_MENU_SAVE:
2502 rb->lcd_set_foreground(COLOR_BLACK);
2503 if (!filename[0])
2504 rb->strcpy(filename,"/");
2505 if( !rb->kbd_input( filename, MAX_PATH ) )
2507 if( !check_extention( filename, ".bmp" ) )
2508 rb->strcat(filename, ".bmp");
2509 save_bitmap( filename );
2510 rb->splashf( 1*HZ, "File saved (%s)", filename );
2512 break;
2514 case MAIN_MENU_BRUSH_SIZE:
2515 for(multi = 0; multi<4; multi++)
2516 if(bsize == times_list[multi]) break;
2517 rb->set_option( "Brush Size", &multi, INT, times_options, 4, NULL );
2518 if( multi >= 0 )
2519 bsize = times_list[multi];
2520 break;
2522 case MAIN_MENU_BRUSH_SPEED:
2523 for(multi = 0; multi<3; multi++)
2524 if(bspeed == times_list[multi]) break;
2525 rb->set_option( "Brush Speed", &multi, INT, times_options, 3, NULL );
2526 if( multi >= 0 )
2527 bspeed = times_list[multi];
2528 break;
2530 case MAIN_MENU_COLOR:
2531 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2532 break;
2534 case MAIN_MENU_GRID_SIZE:
2535 for(multi = 0; multi<4; multi++)
2536 if(gridsize == gridsize_list[multi]) break;
2537 rb->set_option( "Grid Size", &multi, INT, gridsize_options, 4, NULL );
2538 if( multi >= 0 )
2539 gridsize = gridsize_list[multi];
2540 break;
2542 case MAIN_MENU_PLAYBACK_CONTROL:
2543 playback_control( NULL );
2544 break;
2546 case MAIN_MENU_EXIT:
2547 restore_screen();
2548 quit=true;
2549 return;
2551 case MAIN_MENU_RESUME:
2552 default:
2553 restore_screen();
2554 return;
2555 }/* end switch */
2556 }/* end while */
2559 static void reset_tool( void )
2561 prev_x = -1;
2562 prev_y = -1;
2563 prev_x2 = -1;
2564 prev_y2 = -1;
2565 prev_x3 = -1;
2566 prev_y3 = -1;
2567 tool_mode = -1;
2568 preview = false;
2571 static bool rockpaint_loop( void )
2573 int button=0,i,j;
2574 int accelaration;
2576 x = 10;
2577 toolbar();
2578 x = 0; y = 0;
2579 restore_screen();
2580 inv_cursor(true);
2582 while (!quit) {
2583 button = rb->button_get(true);
2585 if( tool == Brush && prev_x != -1 )
2587 accelaration = 1;
2589 else if( button & BUTTON_REPEAT )
2591 accelaration = 4;
2593 else
2595 accelaration = 1;
2598 switch(button)
2600 case ROCKPAINT_QUIT:
2601 rb->lcd_set_drawmode(DRMODE_SOLID);
2602 return PLUGIN_OK;
2604 case ROCKPAINT_MENU:
2605 inv_cursor(false);
2606 goto_menu();
2607 restore_screen();
2608 inv_cursor(true);
2609 break;
2611 case ROCKPAINT_DRAW:
2612 inv_cursor(false);
2613 switch( tool )
2615 case Brush:
2616 if( prev_x == -1 ) prev_x = 1;
2617 else prev_x = -1;
2618 break;
2620 case SelectRectangle:
2621 case Line:
2622 case Curve:
2623 case Rectangle:
2624 case RectangleFull:
2625 case Oval:
2626 case OvalFull:
2627 case LinearGradient:
2628 case RadialGradient:
2629 /* Curve uses 4 points, others use 2 */
2630 if( prev_x == -1 || prev_y == -1 )
2632 prev_x = x;
2633 prev_y = y;
2634 preview = true;
2636 else if( tool == Curve
2637 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2639 prev_x2 = x;
2640 prev_y2 = y;
2642 else if( tool == SelectRectangle
2643 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2645 tool_mode = rb->do_menu( &select_menu,
2646 NULL, NULL, false );
2647 switch( tool_mode )
2649 case SELECT_MENU_CUT:
2650 case SELECT_MENU_COPY:
2651 prev_x2 = x;
2652 prev_y2 = y;
2653 copy_to_clipboard();
2654 if( prev_x < x ) x = prev_x;
2655 if( prev_y < y ) y = prev_y;
2656 break;
2658 case SELECT_MENU_INVERT:
2659 draw_invert( prev_x, prev_y, x, y );
2660 reset_tool();
2661 break;
2663 case SELECT_MENU_HFLIP:
2664 draw_hflip( prev_x, prev_y, x, y );
2665 reset_tool();
2666 break;
2668 case SELECT_MENU_VFLIP:
2669 draw_vflip( prev_x, prev_y, x, y );
2670 reset_tool();
2671 break;
2673 case SELECT_MENU_ROTATE90:
2674 draw_rot_90_deg( prev_x, prev_y, x, y, 1 );
2675 reset_tool();
2676 break;
2678 case SELECT_MENU_ROTATE180:
2679 draw_hflip( prev_x, prev_y, x, y );
2680 draw_vflip( prev_x, prev_y, x, y );
2681 reset_tool();
2682 break;
2684 case SELECT_MENU_ROTATE270:
2685 draw_rot_90_deg( prev_x, prev_y, x, y, -1 );
2686 reset_tool();
2687 break;
2689 case SELECT_MENU_CANCEL:
2690 reset_tool();
2691 break;
2693 default:
2694 break;
2696 restore_screen();
2698 else if( tool == Curve
2699 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2701 prev_x3 = x;
2702 prev_y3 = y;
2704 else
2706 preview = false;
2707 switch( tool )
2709 case SelectRectangle:
2710 draw_paste_rectangle( prev_x, prev_y,
2711 prev_x2, prev_y2,
2712 x, y, tool_mode );
2713 break;
2714 case Line:
2715 draw_line( prev_x, prev_y, x, y );
2716 break;
2717 case Curve:
2718 draw_curve( prev_x, prev_y,
2719 prev_x2, prev_y2,
2720 prev_x3, prev_y3,
2721 x, y );
2722 break;
2723 case Rectangle:
2724 draw_rect( prev_x, prev_y, x, y );
2725 break;
2726 case RectangleFull:
2727 draw_rect_full( prev_x, prev_y, x, y );
2728 break;
2729 case Oval:
2730 draw_oval_empty( prev_x, prev_y, x, y );
2731 break;
2732 case OvalFull:
2733 draw_oval_full( prev_x, prev_y, x, y );
2734 break;
2735 case LinearGradient:
2736 linear_gradient( prev_x, prev_y, x, y );
2737 break;
2738 case RadialGradient:
2739 radial_gradient( prev_x, prev_y, x, y );
2740 break;
2741 default:
2742 break;
2744 reset_tool();
2746 break;
2748 case Fill:
2749 draw_fill( x, y );
2750 break;
2752 case ColorPicker:
2753 color_picker( x, y );
2754 break;
2756 case Text:
2757 draw_text( x, y );
2758 break;
2760 default:
2761 break;
2763 inv_cursor(true);
2764 break;
2766 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2767 if( tool == Curve )
2769 /* 3 point bezier curve */
2770 preview = false;
2771 draw_curve( prev_x, prev_y,
2772 prev_x2, prev_y2,
2773 -1, -1,
2774 x, y );
2775 reset_tool();
2776 restore_screen();
2777 inv_cursor( true );
2779 break;
2781 case ROCKPAINT_TOOLBAR:
2782 i = x; j = y;
2783 x = 10;
2784 toolbar();
2785 x = i; y = j;
2786 restore_screen();
2787 inv_cursor(true);
2788 break;
2790 case ROCKPAINT_TOOLBAR2:
2791 i = x; j = y;
2792 x = 110;
2793 toolbar();
2794 x = i; y = j;
2795 restore_screen();
2796 inv_cursor(true);
2797 break;
2799 case ROCKPAINT_LEFT:
2800 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2801 inv_cursor(false);
2802 x-=bspeed * accelaration;
2803 if (x<0) x=COLS-1;
2804 inv_cursor(true);
2805 break;
2807 case ROCKPAINT_RIGHT:
2808 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2809 inv_cursor(false);
2810 x+=bspeed * accelaration;
2811 if (x>=COLS) x=0;
2812 inv_cursor(true);
2813 break;
2815 case ROCKPAINT_UP:
2816 case ROCKPAINT_UP | BUTTON_REPEAT:
2817 inv_cursor(false);
2818 y-=bspeed * accelaration;
2819 if (y<0) y=ROWS-1;
2820 inv_cursor(true);
2821 break;
2823 case ROCKPAINT_DOWN:
2824 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2825 inv_cursor(false);
2826 y+=bspeed * accelaration;
2827 if (y>=ROWS)
2829 toolbar();
2830 restore_screen();
2832 inv_cursor(true);
2833 break;
2835 default:
2836 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2837 return PLUGIN_USB_CONNECTED;
2838 break;
2840 if( tool == Brush && prev_x == 1 )
2842 inv_cursor(false);
2843 draw_brush( x, y );
2844 inv_cursor(true);
2846 if( preview || tool == ColorPicker )
2847 /* always preview color picker */
2849 restore_screen();
2850 switch( tool )
2852 case SelectRectangle:
2853 if( prev_x2 == -1 || prev_y2 == -1 )
2855 /* we are defining the selection */
2856 draw_select_rectangle( prev_x, prev_y, x, y );
2858 else
2860 /* we are pasting the selected data */
2861 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2862 prev_y2, x, y, tool_mode );
2863 prev_x3 = prev_x2-prev_x;
2864 if( prev_x3 < 0 ) prev_x3 *= -1;
2865 prev_y3 = prev_y2-prev_y;
2866 if( prev_y3 < 0 ) prev_y3 *= -1;
2867 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2868 prev_x3 = -1;
2869 prev_y3 = -1;
2871 break;
2873 case Brush:
2874 break;
2876 case Line:
2877 draw_line( prev_x, prev_y, x, y );
2878 break;
2880 case Curve:
2881 if( prev_x2 == -1 || prev_y2 == -1 )
2883 draw_line( prev_x, prev_y, x, y );
2885 else
2887 draw_curve( prev_x, prev_y,
2888 prev_x2, prev_y2,
2889 prev_x3, prev_y3,
2890 x, y );
2892 break;
2894 case Rectangle:
2895 draw_rect( prev_x, prev_y, x, y );
2896 break;
2898 case RectangleFull:
2899 draw_rect_full( prev_x, prev_y, x, y );
2900 break;
2902 case Oval:
2903 draw_oval_empty( prev_x, prev_y, x, y );
2904 break;
2906 case OvalFull:
2907 draw_oval_full( prev_x, prev_y, x, y );
2908 break;
2910 case Fill:
2911 break;
2913 case ColorPicker:
2914 preview = true;
2915 color_picker( x, y );
2916 preview = false;
2917 break;
2919 case LinearGradient:
2920 line_gradient( prev_x, prev_y, x, y );
2921 break;
2923 case RadialGradient:
2924 line_gradient( prev_x, prev_y, x, y );
2925 break;
2927 case Text:
2928 default:
2929 break;
2931 inv_cursor( true );
2933 if( gridsize > 0 )
2935 show_grid( true );
2936 show_grid( false );
2940 return PLUGIN_OK;
2943 static int load_bitmap( const char *file )
2945 struct bitmap bm;
2946 bool ret;
2947 int i, j;
2948 fb_data color = rp_colors[ bgdrawcolor ];
2950 bm.data = (char*)save_buffer;
2951 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2952 FORMAT_NATIVE, NULL );
2954 if((bm.width > COLS ) || ( bm.height > ROWS ))
2955 return -1;
2957 for( i = bm.height-1; i >= 0; i-- )
2959 rb->memmove( save_buffer+i*COLS, save_buffer+i*bm.width,
2960 sizeof( fb_data )*bm.width );
2961 for( j = bm.width; j < COLS; j++ )
2962 save_buffer[j+i*COLS] = color;
2964 for( i = bm.height*COLS; i < ROWS*COLS; i++ )
2965 save_buffer[i] = color;
2967 return ret;
2970 static int save_bitmap( char *file )
2972 struct bitmap bm;
2973 bm.data = (char*)save_buffer;
2974 bm.height = ROWS;
2975 bm.width = COLS;
2976 bm.format = FORMAT_NATIVE;
2977 return save_bmp_file( file, &bm );
2980 enum plugin_status plugin_start(const void* parameter)
2982 rb->lcd_set_foreground(COLOR_WHITE);
2983 rb->lcd_set_backdrop(NULL);
2984 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
2985 rb->splash( HZ/2, "Rock Paint");
2987 rb->lcd_clear_display();
2989 filename[0] = '\0';
2991 if( parameter )
2993 if( load_bitmap( parameter ) <= 0 )
2995 rb->splash( 1*HZ, "File Open Error");
2996 clear_drawing();
2998 else
3000 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
3001 restore_screen();
3002 rb->strcpy( filename, parameter );
3005 else
3007 clear_drawing();
3009 inv_cursor(true);
3011 return rockpaint_loop();