FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / apps / plugins / rockpaint.c
blob12b30d484d0c41f9df880a7c8befc1e9b8be6640
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 256
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];
403 char font[MAX_PATH];
404 char old_font[MAX_PATH];
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];
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]; /* used by file and font browsers */
600 char bbuf_s[MAX_PATH]; /* used by file and font browsers */
601 struct tree_context *tree = NULL;
603 static bool check_extention(const char *filename, const char *ext)
605 const char *p = rb->strrchr( filename, '.' );
606 return ( p != NULL && !rb->strcasecmp( p, ext ) );
609 static const char* browse_get_name_cb(int selected_item, void *data,
610 char *buffer, size_t buffer_len)
612 int *indexes = (int *) data;
613 struct entry* dc = tree->dircache;
614 struct entry* e = &dc[indexes[selected_item]];
615 (void) buffer;
616 (void) buffer_len;
618 return e->name;
621 static bool browse( char *dst, int dst_size, const char *start )
623 struct gui_synclist browse_list;
624 int item_count = 0, selected, button;
625 struct tree_context backup;
626 struct entry *dc;
627 bool reload = true;
628 int dirfilter = SHOW_ALL;
629 int *indexes = (int *) buffer.clipboard;
631 char *a;
633 rb->strcpy( bbuf, start );
634 a = bbuf+rb->strlen(bbuf)-1;
635 if( *a != '/' )
637 a[1] = '/';
638 a[2] = '\0';
640 bbuf_s[0] = '\0';
642 rb->gui_synclist_init(&browse_list, browse_get_name_cb,
643 (void*) indexes, false, 1, NULL);
645 tree = rb->tree_get_context();
646 backup = *tree;
647 dc = tree->dircache;
648 a = backup.currdir+rb->strlen(backup.currdir)-1;
649 if( *a != '/' )
651 *++a = '/';
652 *++a = '\0';
654 rb->strcpy( a, dc[tree->selected_item].name );
655 tree->dirfilter = &dirfilter;
656 while( 1 )
658 if( reload )
660 int i;
661 rb->set_current_file(bbuf);
662 item_count = 0;
663 selected = 0;
664 for( i = 0; i < tree->filesindir ; i++)
666 /* only displayes directories and .bmp files */
667 if( ((dc[i].attr & ATTR_DIRECTORY ) &&
668 rb->strcmp( dc[i].name, "." ) &&
669 rb->strcmp( dc[i].name, ".." )) ||
670 ( !(dc[i].attr & ATTR_DIRECTORY) &&
671 check_extention( dc[i].name, ".bmp" ) ) )
673 if( !rb->strcmp( dc[i].name, bbuf_s ) )
674 selected = item_count;
675 indexes[item_count++] = i;
679 rb->gui_synclist_set_nb_items(&browse_list,item_count);
680 rb->gui_synclist_select_item(&browse_list, selected);
681 rb->gui_synclist_set_title(&browse_list, bbuf, NOICON);
682 rb->gui_synclist_draw(&browse_list);
683 reload = false;
685 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
686 if (rb->gui_synclist_do_button(&browse_list,&button,LIST_WRAP_UNLESS_HELD))
687 continue;
688 switch( button )
690 case ACTION_STD_CANCEL:
691 if( !rb->strcmp( bbuf, "/" ) )
693 *tree = backup;
694 rb->set_current_file( backup.currdir );
695 return false;
697 rb->strcpy( bbuf_s, ".." );
698 case ACTION_STD_OK:
699 if( button == ACTION_STD_OK )
701 selected = rb->gui_synclist_get_sel_pos( &browse_list );
702 if( selected < 0 || selected >= item_count )
703 break;
704 struct entry* e = &dc[indexes[selected]];
705 rb->strlcpy( bbuf_s, e->name, sizeof( bbuf_s ) );
706 if( !( e->attr & ATTR_DIRECTORY ) )
708 *tree = backup;
709 rb->set_current_file( backup.currdir );
710 rb->snprintf( dst, dst_size, "%s%s", bbuf, bbuf_s );
711 return true;
714 if( !rb->strcmp( bbuf_s, "." ) ) break;
715 a = bbuf+rb->strlen(bbuf);
716 if( !rb->strcmp( bbuf_s, ".." ) )
718 a--;
719 if( a == bbuf ) break;
720 if( *a == '/' ) a--;
721 while( *a != '/' ) a--;
722 rb->strcpy( bbuf_s, ++a );
723 /* select parent directory */
724 bbuf_s[rb->strlen(bbuf_s)-1] = '\0';
725 *a = '\0';
726 reload = true;
727 break;
729 rb->snprintf( a, bbuf+sizeof(bbuf)-a, "%s/", bbuf_s );
730 reload = true;
731 break;
733 case ACTION_STD_MENU:
734 *tree = backup;
735 rb->set_current_file( backup.currdir );
736 return false;
741 /***********************************************************************
742 * Font browser
744 * FIXME: This still needs some work ... it currently only works fine
745 * on the simulators, disk spins too much on real targets -> rendered
746 * font buffer needed.
747 ***********************************************************************/
748 static bool browse_fonts( char *dst, int dst_size )
750 #define WIDTH ( LCD_WIDTH - 20 )
751 #define HEIGHT ( LCD_HEIGHT - 20 )
752 #define LINE_SPACE 2
753 int top, top_inside = 0, left;
755 DIR *d;
756 struct dirent *de;
757 int fvi = 0; /* first visible item */
758 int lvi = 0; /* last visible item */
759 int si = 0; /* selected item */
760 int osi = 0; /* old selected item */
761 int li = 0; /* last item */
762 int nvih = 0; /* next visible item height */
763 int i;
764 int b_need_redraw = 1; /* Do we need to redraw ? */
766 int cp = 0; /* current position */
767 int fh; /* font height */
769 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
770 #define fw_buf buffer.text.fw_buf
771 int fw;
772 #define fontname_buf buffer.text.fontname_buf
774 rb->snprintf( buffer.text.old_font, MAX_PATH,
775 FONT_DIR "/%s.fnt",
776 rb->global_settings->font_file );
778 while( 1 )
780 if( !b_need_redraw )
782 /* we don't need to redraw ... but we need to unselect
783 * the previously selected item */
784 cp = top_inside + LINE_SPACE;
785 for( i = 0; i+fvi < osi; i++ )
787 cp += fh_buf[i] + LINE_SPACE;
789 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
790 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
791 rb->lcd_set_drawmode(DRMODE_SOLID);
794 if( b_need_redraw )
796 b_need_redraw = 0;
798 d = rb->opendir( FONT_DIR "/" );
799 if( !d )
801 return false;
803 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
804 i = 0;
805 li = -1;
806 while( i < fvi )
808 rb->readdir( d );
809 i++;
811 cp = top_inside+LINE_SPACE;
813 rb->lcd_set_foreground(COLOR_BLACK);
814 rb->lcd_set_background(COLOR_LIGHTGRAY);
816 while( cp < top+HEIGHT )
818 de = rb->readdir( d );
819 if( !de )
821 li = i-1;
822 break;
824 if( !check_extention( de->d_name, ".fnt" ) )
825 continue;
826 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
827 de->d_name );
828 rb->font_load( bbuf );
829 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
830 if( nvih > 0 )
832 nvih -= fh;
833 fvi++;
834 if( nvih < 0 ) nvih = 0;
835 i++;
836 continue;
838 if( cp + fh >= top+HEIGHT )
840 nvih = fh;
841 break;
843 rb->lcd_putsxy( left+10, cp, de->d_name );
844 fh_buf[i-fvi] = fh;
845 fw_buf[i-fvi] = fw;
846 cp += fh + LINE_SPACE;
847 rb->strcpy( fontname_buf[i-fvi], bbuf );
848 i++;
850 lvi = i-1;
851 if( li == -1 )
853 if( !(de = rb->readdir( d ) ) )
855 li = lvi;
857 else if( !nvih && check_extention( de->d_name, ".fnt" ) )
859 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
860 de->d_name );
861 rb->font_load( bbuf );
862 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
863 nvih = fh;
866 rb->font_load( buffer.text.old_font );
867 rb->closedir( d );
870 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
871 cp = top_inside + LINE_SPACE;
872 for( i = 0; i+fvi < si; i++ )
874 cp += fh_buf[i] + LINE_SPACE;
876 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
877 rb->lcd_set_drawmode(DRMODE_SOLID);
879 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
881 osi = si;
882 i = fvi;
883 switch( rb->button_get(true) )
885 case ROCKPAINT_UP:
886 case ROCKPAINT_UP|BUTTON_REPEAT:
887 if( si > 0 )
889 si--;
890 if( si<fvi )
892 fvi = si;
895 break;
897 case ROCKPAINT_DOWN:
898 case ROCKPAINT_DOWN|BUTTON_REPEAT:
899 if( li == -1 || si < li )
901 si++;
903 break;
905 case ROCKPAINT_LEFT:
906 return false;
908 case ROCKPAINT_RIGHT:
909 case ROCKPAINT_DRAW:
910 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
911 return true;
914 if( i != fvi || si > lvi )
916 b_need_redraw = 1;
919 if( si<=lvi )
921 nvih = 0;
924 #undef fh_buf
925 #undef fw_buf
926 #undef fontname_buf
927 #undef WIDTH
928 #undef HEIGHT
929 #undef LINE_SPACE
932 /***********************************************************************
933 * HSVRGB Color chooser
934 ***********************************************************************/
935 static unsigned int color_chooser( unsigned int color )
937 int red = RGB_UNPACK_RED( color );
938 int green = RGB_UNPACK_GREEN( color );
939 int blue = RGB_UNPACK_BLUE( color );
940 int hue, saturation, value;
941 int r, g, b; /* temp variables */
942 int i, top, left;
944 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
945 Red = 3, Green = 4, Blue = 5 };
946 enum BaseColor current = Red;
947 bool has_changed;
949 char str[6] = "";
951 restore_screen();
953 rgb2hsv( red, green, blue, &hue, &saturation, &value );
955 while( 1 )
957 has_changed = false;
958 color = LCD_RGBPACK( red, green, blue );
960 #define HEIGHT ( 100 )
961 #define WIDTH ( 150 )
963 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
964 top -= 15;
966 for( i=0; i<100; i++ )
968 hsv2rgb( i*36, saturation, value, &r, &g, &b );
969 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
970 rb->lcd_vline( left+15+i, top+20, top+27 );
971 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
972 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
973 rb->lcd_vline( left+15+i, top+30, top+37 );
974 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
975 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
976 rb->lcd_vline( left+15+i, top+40, top+47 );
977 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
978 rb->lcd_vline( left+15+i, top+50, top+57 );
979 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
980 rb->lcd_vline( left+15+i, top+60, top+67 );
981 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
982 rb->lcd_vline( left+15+i, top+70, top+77 );
985 rb->lcd_set_foreground(COLOR_BLACK);
986 #define POSITION( a, i ) \
987 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
988 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
989 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
990 rb->lcd_drawpixel( left+16+i, top + 28 + a );
991 POSITION( 0, hue/36 );
992 POSITION( 10, saturation*99/255 );
993 POSITION( 20, value*99/255 );
994 POSITION( 30, red*99/255 );
995 POSITION( 40, green*99/255 );
996 POSITION( 50, blue*99/255 );
997 #undef POSITION
998 rb->lcd_set_background(COLOR_LIGHTGRAY);
999 rb->lcd_setfont( FONT_SYSFIXED );
1000 rb->snprintf( str, 6, "%d", hue/10 );
1001 rb->lcd_putsxy( left + 117, top + 20, str );
1002 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1003 rb->lcd_putsxy( left + 117, top + 30, str );
1004 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1005 rb->lcd_putsxy( left + 117, top + 40, str );
1006 rb->snprintf( str, 6, "%d", red );
1007 rb->lcd_putsxy( left + 117, top + 50, str );
1008 rb->snprintf( str, 6, "%d", green );
1009 rb->lcd_putsxy( left + 117, top + 60, str );
1010 rb->snprintf( str, 6, "%d", blue );
1011 rb->lcd_putsxy( left + 117, top + 70, str );
1012 rb->lcd_setfont( FONT_UI );
1014 #define CURSOR( l ) \
1015 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1016 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1017 CURSOR( 5 );
1018 #undef CURSOR
1020 rb->lcd_set_foreground( color );
1021 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1023 rb->lcd_update();
1025 switch( rb->button_get(true) )
1027 case ROCKPAINT_UP:
1028 current = ( current + 5 )%6;
1029 break;
1031 case ROCKPAINT_DOWN:
1032 current = (current + 1 )%6;
1033 break;
1035 case ROCKPAINT_LEFT:
1036 has_changed = true;
1037 switch( current )
1039 case Hue:
1040 hue = ( hue + 3600 - 10 )%3600;
1041 break;
1042 case Saturation:
1043 if( saturation ) saturation--;
1044 break;
1045 case Value:
1046 if( value ) value--;
1047 break;
1048 case Red:
1049 if( red ) red--;
1050 break;
1051 case Green:
1052 if( green ) green--;
1053 break;
1054 case Blue:
1055 if( blue ) blue--;
1056 break;
1058 break;
1060 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1061 has_changed = true;
1062 switch( current )
1064 case Hue:
1065 hue = ( hue + 3600 - 100 )%3600;
1066 break;
1067 case Saturation:
1068 if( saturation >= 8 ) saturation-=8;
1069 else saturation = 0;
1070 break;
1071 case Value:
1072 if( value >= 8 ) value-=8;
1073 else value = 0;
1074 break;
1075 case Red:
1076 if( red >= 8 ) red-=8;
1077 else red = 0;
1078 break;
1079 case Green:
1080 if( green >= 8 ) green-=8;
1081 else green = 0;
1082 break;
1083 case Blue:
1084 if( blue >= 8 ) blue-=8;
1085 else blue = 0;
1086 break;
1088 break;
1090 case ROCKPAINT_RIGHT:
1091 has_changed = true;
1092 switch( current )
1094 case Hue:
1095 hue = ( hue + 10 )%3600;
1096 break;
1097 case Saturation:
1098 if( saturation < 0xff ) saturation++;
1099 break;
1100 case Value:
1101 if( value < 0xff ) value++;
1102 break;
1103 case Red:
1104 if( red < 0xff ) red++;
1105 break;
1106 case Green:
1107 if( green < 0xff ) green++;
1108 break;
1109 case Blue:
1110 if( blue < 0xff ) blue++;
1111 break;
1113 break;
1115 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1116 has_changed = true;
1117 switch( current )
1119 case Hue:
1120 hue = ( hue + 100 )%3600;
1121 break;
1122 case Saturation:
1123 if( saturation < 0xff - 8 ) saturation+=8;
1124 else saturation = 0xff;
1125 break;
1126 case Value:
1127 if( value < 0xff - 8 ) value+=8;
1128 else value = 0xff;
1129 break;
1130 case Red:
1131 if( red < 0xff - 8 ) red+=8;
1132 else red = 0xff;
1133 break;
1134 case Green:
1135 if( green < 0xff - 8 ) green+=8;
1136 else green = 0xff;
1137 break;
1138 case Blue:
1139 if( blue < 0xff - 8 ) blue+=8;
1140 else blue = 0xff;
1141 break;
1143 break;
1145 case ROCKPAINT_DRAW:
1146 return color;
1148 if( has_changed )
1150 switch( current )
1152 case Hue:
1153 case Saturation:
1154 case Value:
1155 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1156 break;
1158 case Red:
1159 case Green:
1160 case Blue:
1161 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1162 break;
1165 #undef HEIGHT
1166 #undef WIDTH
1170 /***********************************************************************
1171 * Misc routines
1172 ***********************************************************************/
1173 static void init_buffer(void)
1175 int i;
1176 fb_data color = rp_colors[ bgdrawcolor ];
1177 for( i = 0; i < ROWS*COLS; i++ )
1179 save_buffer[i] = color;
1183 static void draw_pixel(int x,int y)
1185 if( !preview )
1187 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1188 if( isbg )
1190 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1192 else
1194 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1197 rb->lcd_drawpixel(x,y);
1200 static void color_picker( int x, int y )
1202 if( preview )
1204 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1205 #define PSIZE 12
1206 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1207 if( x >= COLS - PSIZE ) x -= PSIZE + 2;
1208 if( y >= ROWS - PSIZE ) y -= PSIZE + 2;
1209 rb->lcd_drawrect( x + 2, y + 2, PSIZE - 2, PSIZE - 2 );
1210 rb->lcd_set_drawmode(DRMODE_SOLID);
1211 rb->lcd_drawrect( x + 3, y + 3, PSIZE - 4, PSIZE - 4 );
1212 #undef PSIZE
1213 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1215 else
1217 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1221 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1222 /* This is a preview mode only function */
1224 int i,a;
1225 if( x1 > x2 )
1227 i = x1;
1228 x1 = x2;
1229 x2 = i;
1231 if( y1 > y2 )
1233 i = y1;
1234 y1 = y2;
1235 y2 = i;
1237 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1238 i = 0;
1239 for( a = x1; a < x2; a++, i++ )
1240 if( i%2 )
1241 rb->lcd_drawpixel( a, y1 );
1242 for( a = y1; a < y2; a++, i++ )
1243 if( i%2 )
1244 rb->lcd_drawpixel( x2, a );
1245 if( y2 != y1 )
1246 for( a = x2; a > x1; a--, i++ )
1247 if( i%2 )
1248 rb->lcd_drawpixel( a, y2 );
1249 if( x2 != x1 )
1250 for( a = y2; a > y1; a--, i++ )
1251 if( i%2 )
1252 rb->lcd_drawpixel( x1, a );
1253 rb->lcd_set_drawmode(DRMODE_SOLID);
1256 static void copy_to_clipboard( void )
1258 /* This needs to be optimised ... but i'm lazy ATM */
1259 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1262 /* no preview mode handling atm ... do we need it ? (one if) */
1263 static void draw_invert( int x1, int y1, int x2, int y2 )
1265 int i;
1266 if( x1 > x2 )
1268 i = x1;
1269 x1 = x2;
1270 x2 = i;
1272 if( y1 > y2 )
1274 i = y1;
1275 y1 = y2;
1276 y2 = i;
1279 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1280 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1281 rb->lcd_set_drawmode(DRMODE_SOLID);
1283 for( ; y1<=y2; y1++ )
1285 for( i = x1; i<=x2; i++ )
1287 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1290 /*if( update )*/ rb->lcd_update();
1293 static void draw_hflip( int x1, int y1, int x2, int y2 )
1295 int i;
1296 if( x1 > x2 )
1298 i = x1;
1299 x1 = x2;
1300 x2 = i;
1302 if( y1 > y2 )
1304 i = y1;
1305 y1 = y2;
1306 y2 = i;
1309 copy_to_clipboard();
1311 for( i = 0; i <= y2 - y1; i++ )
1313 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1314 buffer.clipboard+(y2-i)*COLS+x1,
1315 (x2-x1+1)*sizeof( fb_data ) );
1317 restore_screen();
1318 rb->lcd_update();
1321 static void draw_vflip( int x1, int y1, int x2, int y2 )
1323 int i;
1324 if( x1 > x2 )
1326 i = x1;
1327 x1 = x2;
1328 x2 = i;
1330 if( y1 > y2 )
1332 i = y1;
1333 y1 = y2;
1334 y2 = i;
1337 copy_to_clipboard();
1339 for( ; y1 <= y2; y1++ )
1341 for( i = 0; i <= x2 - x1; i++ )
1343 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1346 restore_screen();
1347 rb->lcd_update();
1350 /* direction: -1 = left, 1 = right */
1351 static void draw_rot_90_deg( int x1, int y1, int x2, int y2, int direction )
1353 int i, j;
1354 if( x1 > x2 )
1356 i = x1;
1357 x1 = x2;
1358 x2 = i;
1360 if( y1 > y2 )
1362 i = y1;
1363 y1 = y2;
1364 y2 = i;
1367 copy_to_clipboard();
1369 fb_data color = rp_colors[ bgdrawcolor ];
1370 const int width = x2 - x1, height = y2 - y1;
1371 const int sub_half = width/2-height/2, add_half = (width+height)/2;
1372 if( width > height )
1374 for( i = 0; i <= height; i++ )
1376 for( j = 0; j < sub_half; j++ )
1377 save_buffer[(y1+i)*COLS+x1+j] = color;
1378 for( j = add_half+1; j <= width; j++ )
1379 save_buffer[(y1+i)*COLS+x1+j] = color;
1382 else if( width < height )
1384 for( j = 0; j <= width; j++ )
1386 for( i = 0; i < -sub_half; i++ )
1387 save_buffer[(y1+i)*COLS+x1+j] = color;
1388 for( i = add_half+1; i <= height; i++ )
1389 save_buffer[(y1+i)*COLS+x1+j] = color;
1392 int x3 = x1 + sub_half, y3 = y1 - sub_half;
1393 int is = x3<0?-x3:0, ie = COLS-x3-1, js = y3<0?-y3:0, je = ROWS-y3-1;
1394 if( ie > height ) ie = height;
1395 if( je > width ) je = width;
1396 for( i = is; i <= ie; i++ )
1398 for( j = js; j <= je; j++ )
1400 int x, y;
1401 if(direction > 0)
1403 x = x1+j;
1404 y = y1+height-i;
1406 else
1408 x = x1+width-j;
1409 y = y1+i;
1411 save_buffer[(y3+j)*COLS+x3+i] = buffer.clipboard[y*COLS+x];
1414 restore_screen();
1415 rb->lcd_update();
1418 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1419 int src_y2, int x1, int y1, int mode )
1421 int i, width, height;
1422 if( mode == SELECT_MENU_CUT )
1424 i = drawcolor;
1425 drawcolor = bgdrawcolor;
1426 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1427 drawcolor = i;
1429 if( src_x1 > src_x2 )
1431 i = src_x1;
1432 src_x1 = src_x2;
1433 src_x2 = i;
1435 if( src_y1 > src_y2 )
1437 i = src_y1;
1438 src_y1 = src_y2;
1439 src_y2 = i;
1441 width = src_x2 - src_x1 + 1;
1442 height = src_y2 - src_y1 + 1;
1443 /* clipping */
1444 if( x1 + width > COLS )
1445 width = COLS - x1;
1446 if( y1 + height > ROWS )
1447 height = ROWS - y1;
1449 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1450 x1, y1, width, height );
1451 if( !preview )
1453 for( i = 0; i < height; i++ )
1455 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1456 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1457 width*sizeof( fb_data ) );
1462 static void show_grid( bool update )
1464 int i;
1465 if( gridsize > 0 )
1467 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1468 for( i = gridsize; i < COLS; i+= gridsize )
1470 rb->lcd_vline( i, 0, ROWS-1 );
1472 for( i = gridsize; i < ROWS; i+= gridsize )
1474 rb->lcd_hline( 0, COLS-1, i );
1476 rb->lcd_set_drawmode(DRMODE_SOLID);
1477 if( update ) rb->lcd_update();
1481 static void draw_text( int x, int y )
1483 int selected = 0;
1484 buffer.text.text[0] = '\0';
1485 rb->snprintf( buffer.text.old_font, MAX_PATH,
1486 FONT_DIR "/%s.fnt",
1487 rb->global_settings->font_file );
1488 while( 1 )
1490 switch( rb->do_menu( &text_menu, &selected, NULL, NULL ) )
1492 case TEXT_MENU_TEXT:
1493 rb->lcd_set_foreground(COLOR_BLACK);
1494 rb->kbd_input( buffer.text.text, MAX_TEXT );
1495 break;
1497 case TEXT_MENU_FONT:
1498 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1500 rb->font_load( buffer.text.font );
1502 break;
1504 case TEXT_MENU_PREVIEW:
1505 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1506 while( 1 )
1508 int button;
1509 restore_screen();
1510 rb->lcd_putsxy( x, y, buffer.text.text );
1511 rb->lcd_update();
1512 switch( button = rb->button_get( true ) )
1514 case ROCKPAINT_LEFT:
1515 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1516 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1517 if (x<0) x=COLS-1;
1518 break;
1520 case ROCKPAINT_RIGHT:
1521 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1522 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1523 if (x>=COLS) x=0;
1524 break;
1526 case ROCKPAINT_UP:
1527 case ROCKPAINT_UP | BUTTON_REPEAT:
1528 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1529 if (y<0) y=ROWS-1;
1530 break;
1532 case ROCKPAINT_DOWN:
1533 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1534 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1535 if (y>=ROWS-1) y=0;
1536 break;
1538 case ROCKPAINT_DRAW:
1539 break;
1540 default:
1541 if(rb->default_event_handler(button)
1542 == SYS_USB_CONNECTED)
1543 button = ROCKPAINT_DRAW;
1544 break;
1546 if( button == ROCKPAINT_DRAW ) break;
1548 break;
1550 case TEXT_MENU_APPLY:
1551 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1552 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1553 buffer.text.text );
1554 case TEXT_MENU_CANCEL:
1555 default:
1556 restore_screen();
1557 rb->font_load( buffer.text.old_font );
1558 return;
1563 static void draw_brush( int x, int y )
1565 int i,j;
1566 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1568 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1570 draw_pixel( x+i, y+j );
1575 /* This is an implementation of Bresenham's line algorithm.
1576 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1578 static void draw_line( int x1, int y1, int x2, int y2 )
1580 int x = x1;
1581 int y = y1;
1582 int deltax = x2 - x1;
1583 int deltay = y2 - y1;
1584 int i;
1586 int xerr = abs(deltax);
1587 int yerr = abs(deltay);
1588 int xstep = deltax > 0 ? 1 : -1;
1589 int ystep = deltay > 0 ? 1 : -1;
1590 int err;
1592 if (yerr > xerr)
1594 /* more vertical */
1595 err = yerr;
1596 xerr <<= 1;
1597 yerr <<= 1;
1599 /* to leave off the last pixel of the line, leave off the "+ 1" */
1600 for (i = abs(deltay) + 1; i; --i)
1602 draw_pixel(x, y);
1603 y += ystep;
1604 err -= xerr;
1605 if (err < 0) {
1606 x += xstep;
1607 err += yerr;
1611 else
1613 /* more horizontal */
1614 err = xerr;
1615 xerr <<= 1;
1616 yerr <<= 1;
1618 for (i = abs(deltax) + 1; i; --i)
1620 draw_pixel(x, y);
1621 x += xstep;
1622 err -= yerr;
1623 if (err < 0) {
1624 y += ystep;
1625 err += xerr;
1631 static void draw_curve( int x1, int y1, int x2, int y2,
1632 int xa, int ya, int xb, int yb )
1634 int i = 0;
1635 short xl1, yl1;
1636 short xl2, yl2;
1637 short xl3, yl3;
1638 short xl4, yl4;
1639 short xr1, yr1;
1640 short xr2, yr2;
1641 short xr3, yr3;
1642 short xr4, yr4;
1643 short depth;
1644 short xh, yh;
1646 if( x1 == x2 && y1 == y2 )
1648 draw_pixel( x1, y1 );
1649 return;
1652 // if( preview )
1654 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1655 if( xa == -1 || ya == -1 )
1657 rb->lcd_drawline( x1, y1, xb, yb );
1658 rb->lcd_drawline( x2, y2, xb, yb );
1660 else
1662 rb->lcd_drawline( x1, y1, xa, ya );
1663 rb->lcd_drawline( x2, y2, xb, yb );
1665 rb->lcd_set_drawmode(DRMODE_SOLID);
1668 if( xa == -1 || ya == -1 )
1669 /* We only have 3 of the points
1670 * This will currently only be used in preview mode */
1672 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1673 buffer.bezier[i].x1 = a1; \
1674 buffer.bezier[i].y1 = b1; \
1675 buffer.bezier[i].x2 = a2; \
1676 buffer.bezier[i].y2 = b2; \
1677 buffer.bezier[i].x3 = a3; \
1678 buffer.bezier[i].y3 = b3; \
1679 buffer.bezier[i].depth = d; \
1680 i++;
1681 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1682 i--; \
1683 a1 = buffer.bezier[i].x1; \
1684 b1 = buffer.bezier[i].y1; \
1685 a2 = buffer.bezier[i].x2; \
1686 b2 = buffer.bezier[i].y2; \
1687 a3 = buffer.bezier[i].x3; \
1688 b3 = buffer.bezier[i].y3; \
1689 d = buffer.bezier[i].depth;
1690 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1691 while( i )
1693 /* de Casteljau's algorithm (see wikipedia) */
1694 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1695 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1697 xl2 = ( xl1 + xb )>>1;
1698 yl2 = ( yl1 + yb )>>1;
1699 xr2 = ( xb + xr3 )>>1;
1700 yr2 = ( yb + yr3 )>>1;
1701 xr1 = ( xl2 + xr2 )>>1;
1702 yr1 = ( yl2 + yr2 )>>1;
1703 xl3 = xr1;
1704 yl3 = yr1;
1705 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1706 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1708 else
1710 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1711 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1714 #undef PUSH
1715 #undef POP
1717 else /* We have the 4 points */
1719 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1720 buffer.bezier[i].x1 = a1; \
1721 buffer.bezier[i].y1 = b1; \
1722 buffer.bezier[i].x2 = a2; \
1723 buffer.bezier[i].y2 = b2; \
1724 buffer.bezier[i].x3 = a3; \
1725 buffer.bezier[i].y3 = b3; \
1726 buffer.bezier[i].x4 = a4; \
1727 buffer.bezier[i].y4 = b4; \
1728 buffer.bezier[i].depth = d; \
1729 i++;
1730 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1731 i--; \
1732 a1 = buffer.bezier[i].x1; \
1733 b1 = buffer.bezier[i].y1; \
1734 a2 = buffer.bezier[i].x2; \
1735 b2 = buffer.bezier[i].y2; \
1736 a3 = buffer.bezier[i].x3; \
1737 b3 = buffer.bezier[i].y3; \
1738 a4 = buffer.bezier[i].x4; \
1739 b4 = buffer.bezier[i].y4; \
1740 d = buffer.bezier[i].depth;
1742 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1743 while( i )
1745 /* de Casteljau's algorithm (see wikipedia) */
1746 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1747 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1749 xl2 = ( xl1 + xa )>>1;
1750 yl2 = ( yl1 + ya )>>1;
1751 xh = ( xa + xb )>>1;
1752 yh = ( ya + yb )>>1;
1753 xr3 = ( xb + xr4 )>>1;
1754 yr3 = ( yb + yr4 )>>1;
1755 xl3 = ( xl2 + xh )>>1;
1756 yl3 = ( yl2 + yh )>>1;
1757 xr2 = ( xr3 + xh )>>1;
1758 yr2 = ( yr3 + yh )>>1;
1759 xl4 = ( xl3 + xr2 )>>1;
1760 yl4 = ( yl3 + yr2 )>>1;
1761 xr1 = xl4;
1762 yr1 = yl4;
1763 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1764 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1766 else
1768 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1769 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1772 #undef PUSH
1773 #undef POP
1777 static void draw_rect( int x1, int y1, int x2, int y2 )
1779 draw_line( x1, y1, x1, y2 );
1780 draw_line( x1, y1, x2, y1 );
1781 draw_line( x1, y2, x2, y2 );
1782 draw_line( x2, y1, x2, y2 );
1785 static void togglebg( void )
1787 if( isbg )
1789 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1791 else
1793 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1795 isbg = !isbg;
1798 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1800 /* GRUIK */
1801 int x;
1802 togglebg();
1803 if( x1 > x2 )
1805 x = x1;
1806 x1 = x2;
1807 x2 = x;
1809 x = x1;
1810 do {
1811 draw_line( x, y1, x, y2 );
1812 } while( ++x <= x2 );
1813 togglebg();
1814 draw_rect( x1, y1, x2, y2 );
1817 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1819 /* TODO: simplify :) */
1820 int cx = (x1+x2)>>1;
1821 int cy = (y1+y2)>>1;
1823 int rx = (x1-x2)>>1;
1824 int ry = (y1-y2)>>1;
1825 if( rx < 0 ) rx *= -1;
1826 if( ry < 0 ) ry *= -1;
1828 if( rx == 0 || ry == 0 )
1830 draw_line( x1, y1, x2, y2 );
1831 return;
1834 int x,y;
1835 int dst, old_dst;
1837 for( x = 0; x < rx; x++ )
1839 y = 0;
1840 dst = -0xfff;
1841 do {
1842 old_dst = dst;
1843 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1844 y++;
1845 } while( dst < 0 );
1846 if( -old_dst < dst ) y--;
1847 if( full )
1849 draw_line( cx+x, cy, cx+x, cy+y );
1850 draw_line( cx+x, cy, cx+x, cy-y );
1851 draw_line( cx-x, cy, cx-x, cy+y );
1852 draw_line( cx-x, cy, cx-x, cy-y );
1854 else
1856 draw_pixel( cx+x, cy+y );
1857 draw_pixel( cx+x, cy-y );
1858 draw_pixel( cx-x, cy+y );
1859 draw_pixel( cx-x, cy-y );
1862 for( y = 0; y < ry; y++ )
1864 x = 0;
1865 dst = -0xfff;
1866 do {
1867 old_dst = dst;
1868 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1869 x++;
1870 } while( dst < 0 );
1871 if( -old_dst < dst ) x--;
1872 if( full )
1874 draw_line( cx+x, cy, cx+x, cy+y );
1875 draw_line( cx+x, cy, cx+x, cy-y );
1876 draw_line( cx-x, cy, cx-x, cy+y );
1877 draw_line( cx-x, cy, cx-x, cy-y );
1879 else
1881 draw_pixel( cx+x, cy+y );
1882 draw_pixel( cx+x, cy-y );
1883 draw_pixel( cx-x, cy+y );
1884 draw_pixel( cx-x, cy-y );
1889 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1891 draw_oval( x1, y1, x2, y2, false );
1894 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1896 togglebg();
1897 draw_oval( x1, y1, x2, y2, true );
1898 togglebg();
1899 draw_oval( x1, y1, x2, y2, false );
1902 static void draw_fill( int x0, int y0 )
1904 #define PUSH( a, b ) \
1905 draw_pixel( (int)a, (int)b ); \
1906 buffer.coord[i].x = a; \
1907 buffer.coord[i].y = b; \
1908 i++;
1909 #define POP( a, b ) \
1910 i--; \
1911 a = buffer.coord[i].x; \
1912 b = buffer.coord[i].y;
1914 unsigned int i=0;
1915 short x = x0;
1916 short y = y0;
1917 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1919 if( prev_color == rp_colors[ drawcolor ] ) return;
1921 PUSH( x, y );
1923 while( i != 0 )
1925 POP( x, y );
1926 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1928 PUSH( x-1, y );
1930 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1932 PUSH( x+1, y );
1934 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1936 PUSH( x, y-1 );
1938 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1940 PUSH( x, y+1 );
1943 #undef PUSH
1944 #undef POP
1948 /* For preview purposes only */
1949 static void line_gradient( int x1, int y1, int x2, int y2 )
1951 int r1, g1, b1;
1952 int r2, g2, b2;
1953 int h1, s1, v1, h2, s2, v2, r, g, b;
1954 int w, h, x, y;
1956 bool a = false;
1958 x1 <<= 1;
1959 y1 <<= 1;
1960 x2 <<= 1;
1961 y2 <<= 1;
1963 w = x1 - x2;
1964 h = y1 - y2;
1966 if( w == 0 && h == 0 )
1968 draw_pixel( x1>>1, y1>>1 );
1969 return;
1972 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
1973 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
1974 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
1975 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
1976 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
1977 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
1979 if( w < 0 )
1981 w *= -1;
1982 a = true;
1984 if( h < 0 )
1986 h *= -1;
1987 a = !a;
1989 if( a )
1991 r = r1;
1992 r1 = r2;
1993 r2 = r;
1994 g = g1;
1995 g1 = g2;
1996 g2 = g;
1997 b = b1;
1998 b1 = b2;
1999 b2 = b;
2002 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2003 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2005 if( w > h )
2007 if( x1 > x2 )
2009 x = x2;
2010 y = y2;
2011 x2 = x1;
2012 y2 = y1;
2013 x1 = x;
2014 y1 = y;
2016 w = x1 - x2;
2017 h = y1 - y2;
2018 while( x1 <= x2 )
2020 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2021 s1+((s2-s1)*(x1-x2))/w,
2022 v1+((v2-v1)*(x1-x2))/w,
2023 &r, &g, &b );
2024 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2025 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2026 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2027 x1+=2;
2028 y1 = y2 - ( x2 - x1 ) * h / w;
2031 else /* h > w */
2033 if( y1 > y2 )
2035 x = x2;
2036 y = y2;
2037 x2 = x1;
2038 y2 = y1;
2039 x1 = x;
2040 y1 = y;
2042 w = x1 - x2;
2043 h = y1 - y2;
2044 while( y1 <= y2 )
2046 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2047 s1+((s2-s1)*(y1-y2))/h,
2048 v1+((v2-v1)*(y1-y2))/h,
2049 &r, &g, &b );
2050 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2051 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2052 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2053 y1+=2;
2054 x1 = x2 - ( y2 - y1 ) * w / h;
2057 if( a )
2059 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2061 else
2063 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2067 static void linear_gradient( int x1, int y1, int x2, int y2 )
2069 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2070 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2071 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2072 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2073 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2074 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2076 int h1, s1, v1, h2, s2, v2, r, g, b;
2078 /* radius^2 */
2079 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2080 int dist2, i=0;
2082 /* We only propagate the gradient to neighboring pixels with the same
2083 * color as ( x1, y1 ) */
2084 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2086 int x = x1;
2087 int y = y1;
2089 if( radius2 == 0 ) return;
2090 if( preview )
2092 line_gradient( x1, y1, x2, y2 );
2095 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2096 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2098 #define PUSH( x0, y0 ) \
2099 buffer.coord[i].x = (short)(x0); \
2100 buffer.coord[i].y = (short)(y0); \
2101 i++;
2102 #define POP( a, b ) \
2103 i--; \
2104 a = (int)buffer.coord[i].x; \
2105 b = (int)buffer.coord[i].y;
2107 PUSH( x, y );
2109 while( i != 0 )
2111 POP( x, y );
2113 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2114 if( dist2 <= 0 )
2116 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2118 else if( dist2 < radius2 )
2120 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2121 s1+((s2-s1)*dist2)/radius2,
2122 v1+((v2-v1)*dist2)/radius2,
2123 &r, &g, &b );
2124 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2126 else
2128 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2130 if( rp_colors[ drawcolor ] == prev_color )
2132 if( rp_colors[ drawcolor ])
2133 rp_colors[ drawcolor ]--; /* GRUIK */
2134 else
2135 rp_colors[ drawcolor ]++; /* GRUIK */
2137 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2138 draw_pixel( x, y );
2140 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2142 PUSH( x-1, y );
2144 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2146 PUSH( x+1, y );
2148 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2150 PUSH( x, y-1 );
2152 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2154 PUSH( x, y+1 );
2157 #undef PUSH
2158 #undef POP
2160 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2163 static void radial_gradient( int x1, int y1, int x2, int y2 )
2165 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2166 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2167 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2168 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2169 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2170 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2172 int h1, s1, v1, h2, s2, v2, r, g, b;
2174 /* radius^2 */
2175 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2176 int dist2, i=0;
2178 /* We only propagate the gradient to neighboring pixels with the same
2179 * color as ( x1, y1 ) */
2180 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2182 int x = x1;
2183 int y = y1;
2185 if( radius2 == 0 ) return;
2186 if( preview )
2188 line_gradient( x1, y1, x2, y2 );
2191 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2192 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2194 #define PUSH( x0, y0 ) \
2195 buffer.coord[i].x = (short)(x0); \
2196 buffer.coord[i].y = (short)(y0); \
2197 i++;
2198 #define POP( a, b ) \
2199 i--; \
2200 a = (int)buffer.coord[i].x; \
2201 b = (int)buffer.coord[i].y;
2203 PUSH( x, y );
2205 while( i != 0 )
2207 POP( x, y );
2209 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2211 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2212 s1+((s2-s1)*dist2)/radius2,
2213 v1+((v2-v1)*dist2)/radius2,
2214 &r, &g, &b );
2215 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2217 else
2219 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2221 if( rp_colors[ drawcolor ] == prev_color )
2223 if( rp_colors[ drawcolor ])
2224 rp_colors[ drawcolor ]--; /* GRUIK */
2225 else
2226 rp_colors[ drawcolor ]++; /* GRUIK */
2228 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2229 draw_pixel( x, y );
2231 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2233 PUSH( x-1, y );
2235 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2237 PUSH( x+1, y );
2239 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2241 PUSH( x, y-1 );
2243 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2245 PUSH( x, y+1 );
2248 #undef PUSH
2249 #undef POP
2251 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2254 static void draw_toolbars(bool update)
2256 int i;
2257 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2258 rb->lcd_set_background( COLOR_LIGHTGRAY );
2259 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2260 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2261 rb->lcd_set_foreground( COLOR_BLACK );
2262 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2264 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2265 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2266 TB_SC_SIZE, TB_SC_SIZE );
2267 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2268 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2269 TB_SC_SIZE, TB_SC_SIZE );
2270 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2271 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2272 TB_SC_SIZE, TB_SC_SIZE );
2273 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2274 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2275 TB_SC_SIZE, TB_SC_SIZE );
2277 for( i=0; i<18; i++ )
2279 rb->lcd_set_foreground( rp_colors[i] );
2280 rb->lcd_fillrect(
2281 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2282 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2283 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2284 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2285 rb->lcd_drawrect(
2286 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2287 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2288 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2291 #define SEPARATOR( x, y ) \
2292 rb->lcd_set_foreground( COLOR_WHITE ); \
2293 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2294 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2295 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2296 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2298 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2299 TB_TL_WIDTH, TB_TL_HEIGHT );
2300 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2301 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2302 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2303 TB_TL_SIZE, TB_TL_SIZE );
2305 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2307 rb->lcd_setfont( FONT_SYSFIXED );
2308 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2309 rb->lcd_setfont( FONT_UI );
2310 #undef TOP
2312 if( update ) rb->lcd_update();
2315 static void toolbar( void )
2317 int button, i, j;
2318 restore_screen();
2319 draw_toolbars( false );
2320 y = LCD_HEIGHT-TB_HEIGHT/2;
2321 inv_cursor( true );
2322 while( 1 )
2324 switch( button = rb->button_get( true ) )
2326 case ROCKPAINT_DRAW:
2327 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2328 if( y >= TOP + TB_SC_FG_TOP
2329 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2330 && x >= TB_SC_FG_LEFT
2331 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2333 /* click on the foreground color */
2334 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2336 else if( y >= TOP + TB_SC_BG_TOP
2337 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2338 && x >= TB_SC_BG_LEFT
2339 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2341 /* click on the background color */
2342 i = drawcolor;
2343 drawcolor = bgdrawcolor;
2344 bgdrawcolor = i;
2346 else if( y >= TOP + TB_PL_TOP
2347 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2348 && x >= TB_PL_LEFT
2349 && x < TB_PL_LEFT + TB_PL_WIDTH )
2351 /* click on the palette */
2352 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2353 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2354 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2355 break;
2356 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2357 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2358 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2360 else if( y >= TOP+TB_TL_TOP
2361 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2362 && x >= TB_TL_LEFT
2363 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2365 /* click on the tools */
2366 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2367 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2368 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
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 tool = i*2+j;
2372 prev_x = -1;
2373 prev_y = -1;
2374 prev_x2 = -1;
2375 prev_y2 = -1;
2376 prev_x3 = -1;
2377 prev_y3 = -1;
2378 preview = false;
2380 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2382 /* menu button */
2383 goto_menu();
2385 #undef TOP
2386 restore_screen();
2387 draw_toolbars( false );
2388 inv_cursor( true );
2389 break;
2391 case ROCKPAINT_LEFT:
2392 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2393 inv_cursor(false);
2394 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2395 if (x<0) x=COLS-1;
2396 inv_cursor(true);
2397 break;
2399 case ROCKPAINT_RIGHT:
2400 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2401 inv_cursor(false);
2402 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2403 if (x>=COLS) x=0;
2404 inv_cursor(true);
2405 break;
2407 case ROCKPAINT_UP:
2408 case ROCKPAINT_UP | BUTTON_REPEAT:
2409 inv_cursor(false);
2410 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2411 if (y<LCD_HEIGHT-TB_HEIGHT)
2413 return;
2415 inv_cursor(true);
2416 break;
2418 case ROCKPAINT_DOWN:
2419 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2420 inv_cursor(false);
2421 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2422 if (y>=LCD_HEIGHT)
2424 y = 0;
2425 return;
2427 inv_cursor(true);
2428 break;
2430 case ROCKPAINT_TOOLBAR:
2431 case ROCKPAINT_TOOLBAR2:
2432 return;
2434 if( quit ) return;
2438 static void inv_cursor(bool update)
2440 rb->lcd_set_foreground(COLOR_BLACK);
2441 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2442 /* cross painting */
2443 rb->lcd_hline(x-4,x-1,y);
2444 rb->lcd_hline(x+1,x+4,y);
2445 rb->lcd_vline(x,y-4,y-1);
2446 rb->lcd_vline(x,y+1,y+4);
2447 rb->lcd_set_foreground(rp_colors[drawcolor]);
2448 rb->lcd_set_drawmode(DRMODE_SOLID);
2450 if( update ) rb->lcd_update();
2453 static void restore_screen(void)
2455 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2458 static void clear_drawing(void)
2460 init_buffer();
2461 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2462 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2463 rb->lcd_update();
2466 static void goto_menu(void)
2468 int multi;
2469 int selected = 0;
2471 while( 1 )
2473 switch( rb->do_menu( &main_menu, &selected, NULL, false ) )
2475 case MAIN_MENU_NEW:
2476 clear_drawing();
2477 return;
2479 case MAIN_MENU_LOAD:
2480 if( browse( filename, MAX_PATH, "/" ) )
2482 if( load_bitmap( filename ) <= 0 )
2484 rb->splashf( 1*HZ, "Error while loading %s",
2485 filename );
2486 clear_drawing();
2488 else
2490 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2491 restore_screen();
2492 inv_cursor(true);
2493 return;
2496 break;
2498 case MAIN_MENU_SAVE:
2499 rb->lcd_set_foreground(COLOR_BLACK);
2500 if (!filename[0])
2501 rb->strcpy(filename,"/");
2502 if( !rb->kbd_input( filename, MAX_PATH ) )
2504 if( !check_extention( filename, ".bmp" ) )
2505 rb->strcat(filename, ".bmp");
2506 save_bitmap( filename );
2507 rb->splashf( 1*HZ, "File saved (%s)", filename );
2509 break;
2511 case MAIN_MENU_BRUSH_SIZE:
2512 for(multi = 0; multi<4; multi++)
2513 if(bsize == times_list[multi]) break;
2514 rb->set_option( "Brush Size", &multi, INT, times_options, 4, NULL );
2515 if( multi >= 0 )
2516 bsize = times_list[multi];
2517 break;
2519 case MAIN_MENU_BRUSH_SPEED:
2520 for(multi = 0; multi<3; multi++)
2521 if(bspeed == times_list[multi]) break;
2522 rb->set_option( "Brush Speed", &multi, INT, times_options, 3, NULL );
2523 if( multi >= 0 )
2524 bspeed = times_list[multi];
2525 break;
2527 case MAIN_MENU_COLOR:
2528 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2529 break;
2531 case MAIN_MENU_GRID_SIZE:
2532 for(multi = 0; multi<4; multi++)
2533 if(gridsize == gridsize_list[multi]) break;
2534 rb->set_option( "Grid Size", &multi, INT, gridsize_options, 4, NULL );
2535 if( multi >= 0 )
2536 gridsize = gridsize_list[multi];
2537 break;
2539 case MAIN_MENU_PLAYBACK_CONTROL:
2540 playback_control( NULL );
2541 break;
2543 case MAIN_MENU_EXIT:
2544 restore_screen();
2545 quit=true;
2546 return;
2548 case MAIN_MENU_RESUME:
2549 default:
2550 restore_screen();
2551 return;
2552 }/* end switch */
2553 }/* end while */
2556 static void reset_tool( void )
2558 prev_x = -1;
2559 prev_y = -1;
2560 prev_x2 = -1;
2561 prev_y2 = -1;
2562 prev_x3 = -1;
2563 prev_y3 = -1;
2564 tool_mode = -1;
2565 preview = false;
2568 static bool rockpaint_loop( void )
2570 int button=0,i,j;
2571 int accelaration;
2573 x = 10;
2574 toolbar();
2575 x = 0; y = 0;
2576 restore_screen();
2577 inv_cursor(true);
2579 while (!quit) {
2580 button = rb->button_get(true);
2582 if( tool == Brush && prev_x != -1 )
2584 accelaration = 1;
2586 else if( button & BUTTON_REPEAT )
2588 accelaration = 4;
2590 else
2592 accelaration = 1;
2595 switch(button)
2597 case ROCKPAINT_QUIT:
2598 rb->lcd_set_drawmode(DRMODE_SOLID);
2599 return PLUGIN_OK;
2601 case ROCKPAINT_MENU:
2602 inv_cursor(false);
2603 goto_menu();
2604 restore_screen();
2605 inv_cursor(true);
2606 break;
2608 case ROCKPAINT_DRAW:
2609 inv_cursor(false);
2610 switch( tool )
2612 case Brush:
2613 if( prev_x == -1 ) prev_x = 1;
2614 else prev_x = -1;
2615 break;
2617 case SelectRectangle:
2618 case Line:
2619 case Curve:
2620 case Rectangle:
2621 case RectangleFull:
2622 case Oval:
2623 case OvalFull:
2624 case LinearGradient:
2625 case RadialGradient:
2626 /* Curve uses 4 points, others use 2 */
2627 if( prev_x == -1 || prev_y == -1 )
2629 prev_x = x;
2630 prev_y = y;
2631 preview = true;
2633 else if( tool == Curve
2634 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2636 prev_x2 = x;
2637 prev_y2 = y;
2639 else if( tool == SelectRectangle
2640 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2642 tool_mode = rb->do_menu( &select_menu,
2643 NULL, NULL, false );
2644 switch( tool_mode )
2646 case SELECT_MENU_CUT:
2647 case SELECT_MENU_COPY:
2648 prev_x2 = x;
2649 prev_y2 = y;
2650 copy_to_clipboard();
2651 if( prev_x < x ) x = prev_x;
2652 if( prev_y < y ) y = prev_y;
2653 break;
2655 case SELECT_MENU_INVERT:
2656 draw_invert( prev_x, prev_y, x, y );
2657 reset_tool();
2658 break;
2660 case SELECT_MENU_HFLIP:
2661 draw_hflip( prev_x, prev_y, x, y );
2662 reset_tool();
2663 break;
2665 case SELECT_MENU_VFLIP:
2666 draw_vflip( prev_x, prev_y, x, y );
2667 reset_tool();
2668 break;
2670 case SELECT_MENU_ROTATE90:
2671 draw_rot_90_deg( prev_x, prev_y, x, y, 1 );
2672 reset_tool();
2673 break;
2675 case SELECT_MENU_ROTATE180:
2676 draw_hflip( prev_x, prev_y, x, y );
2677 draw_vflip( prev_x, prev_y, x, y );
2678 reset_tool();
2679 break;
2681 case SELECT_MENU_ROTATE270:
2682 draw_rot_90_deg( prev_x, prev_y, x, y, -1 );
2683 reset_tool();
2684 break;
2686 case SELECT_MENU_CANCEL:
2687 reset_tool();
2688 break;
2690 default:
2691 break;
2693 restore_screen();
2695 else if( tool == Curve
2696 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2698 prev_x3 = x;
2699 prev_y3 = y;
2701 else
2703 preview = false;
2704 switch( tool )
2706 case SelectRectangle:
2707 draw_paste_rectangle( prev_x, prev_y,
2708 prev_x2, prev_y2,
2709 x, y, tool_mode );
2710 break;
2711 case Line:
2712 draw_line( prev_x, prev_y, x, y );
2713 break;
2714 case Curve:
2715 draw_curve( prev_x, prev_y,
2716 prev_x2, prev_y2,
2717 prev_x3, prev_y3,
2718 x, y );
2719 break;
2720 case Rectangle:
2721 draw_rect( prev_x, prev_y, x, y );
2722 break;
2723 case RectangleFull:
2724 draw_rect_full( prev_x, prev_y, x, y );
2725 break;
2726 case Oval:
2727 draw_oval_empty( prev_x, prev_y, x, y );
2728 break;
2729 case OvalFull:
2730 draw_oval_full( prev_x, prev_y, x, y );
2731 break;
2732 case LinearGradient:
2733 linear_gradient( prev_x, prev_y, x, y );
2734 break;
2735 case RadialGradient:
2736 radial_gradient( prev_x, prev_y, x, y );
2737 break;
2738 default:
2739 break;
2741 reset_tool();
2743 break;
2745 case Fill:
2746 draw_fill( x, y );
2747 break;
2749 case ColorPicker:
2750 color_picker( x, y );
2751 break;
2753 case Text:
2754 draw_text( x, y );
2755 break;
2757 default:
2758 break;
2760 inv_cursor(true);
2761 break;
2763 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2764 if( tool == Curve )
2766 /* 3 point bezier curve */
2767 preview = false;
2768 draw_curve( prev_x, prev_y,
2769 prev_x2, prev_y2,
2770 -1, -1,
2771 x, y );
2772 reset_tool();
2773 restore_screen();
2774 inv_cursor( true );
2776 break;
2778 case ROCKPAINT_TOOLBAR:
2779 i = x; j = y;
2780 x = 10;
2781 toolbar();
2782 x = i; y = j;
2783 restore_screen();
2784 inv_cursor(true);
2785 break;
2787 case ROCKPAINT_TOOLBAR2:
2788 i = x; j = y;
2789 x = 110;
2790 toolbar();
2791 x = i; y = j;
2792 restore_screen();
2793 inv_cursor(true);
2794 break;
2796 case ROCKPAINT_LEFT:
2797 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2798 inv_cursor(false);
2799 x-=bspeed * accelaration;
2800 if (x<0) x=COLS-1;
2801 inv_cursor(true);
2802 break;
2804 case ROCKPAINT_RIGHT:
2805 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2806 inv_cursor(false);
2807 x+=bspeed * accelaration;
2808 if (x>=COLS) x=0;
2809 inv_cursor(true);
2810 break;
2812 case ROCKPAINT_UP:
2813 case ROCKPAINT_UP | BUTTON_REPEAT:
2814 inv_cursor(false);
2815 y-=bspeed * accelaration;
2816 if (y<0) y=ROWS-1;
2817 inv_cursor(true);
2818 break;
2820 case ROCKPAINT_DOWN:
2821 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2822 inv_cursor(false);
2823 y+=bspeed * accelaration;
2824 if (y>=ROWS)
2826 toolbar();
2827 restore_screen();
2829 inv_cursor(true);
2830 break;
2832 default:
2833 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2834 return PLUGIN_USB_CONNECTED;
2835 break;
2837 if( tool == Brush && prev_x == 1 )
2839 inv_cursor(false);
2840 draw_brush( x, y );
2841 inv_cursor(true);
2843 if( preview || tool == ColorPicker )
2844 /* always preview color picker */
2846 restore_screen();
2847 switch( tool )
2849 case SelectRectangle:
2850 if( prev_x2 == -1 || prev_y2 == -1 )
2852 /* we are defining the selection */
2853 draw_select_rectangle( prev_x, prev_y, x, y );
2855 else
2857 /* we are pasting the selected data */
2858 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2859 prev_y2, x, y, tool_mode );
2860 prev_x3 = prev_x2-prev_x;
2861 if( prev_x3 < 0 ) prev_x3 *= -1;
2862 prev_y3 = prev_y2-prev_y;
2863 if( prev_y3 < 0 ) prev_y3 *= -1;
2864 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2865 prev_x3 = -1;
2866 prev_y3 = -1;
2868 break;
2870 case Brush:
2871 break;
2873 case Line:
2874 draw_line( prev_x, prev_y, x, y );
2875 break;
2877 case Curve:
2878 if( prev_x2 == -1 || prev_y2 == -1 )
2880 draw_line( prev_x, prev_y, x, y );
2882 else
2884 draw_curve( prev_x, prev_y,
2885 prev_x2, prev_y2,
2886 prev_x3, prev_y3,
2887 x, y );
2889 break;
2891 case Rectangle:
2892 draw_rect( prev_x, prev_y, x, y );
2893 break;
2895 case RectangleFull:
2896 draw_rect_full( prev_x, prev_y, x, y );
2897 break;
2899 case Oval:
2900 draw_oval_empty( prev_x, prev_y, x, y );
2901 break;
2903 case OvalFull:
2904 draw_oval_full( prev_x, prev_y, x, y );
2905 break;
2907 case Fill:
2908 break;
2910 case ColorPicker:
2911 preview = true;
2912 color_picker( x, y );
2913 preview = false;
2914 break;
2916 case LinearGradient:
2917 line_gradient( prev_x, prev_y, x, y );
2918 break;
2920 case RadialGradient:
2921 line_gradient( prev_x, prev_y, x, y );
2922 break;
2924 case Text:
2925 default:
2926 break;
2928 inv_cursor( true );
2930 if( gridsize > 0 )
2932 show_grid( true );
2933 show_grid( false );
2937 return PLUGIN_OK;
2940 static int load_bitmap( const char *file )
2942 struct bitmap bm;
2943 bool ret;
2944 int i, j;
2945 fb_data color = rp_colors[ bgdrawcolor ];
2947 bm.data = (char*)save_buffer;
2948 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2949 FORMAT_NATIVE, NULL );
2951 if((bm.width > COLS ) || ( bm.height > ROWS ))
2952 return -1;
2954 for( i = bm.height-1; i >= 0; i-- )
2956 rb->memmove( save_buffer+i*COLS, save_buffer+i*bm.width,
2957 sizeof( fb_data )*bm.width );
2958 for( j = bm.width; j < COLS; j++ )
2959 save_buffer[j+i*COLS] = color;
2961 for( i = bm.height*COLS; i < ROWS*COLS; i++ )
2962 save_buffer[i] = color;
2964 return ret;
2967 static int save_bitmap( char *file )
2969 struct bitmap bm;
2970 bm.data = (char*)save_buffer;
2971 bm.height = ROWS;
2972 bm.width = COLS;
2973 bm.format = FORMAT_NATIVE;
2974 return save_bmp_file( file, &bm );
2977 enum plugin_status plugin_start(const void* parameter)
2979 rb->lcd_set_foreground(COLOR_WHITE);
2980 rb->lcd_set_backdrop(NULL);
2981 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
2982 rb->splash( HZ/2, "Rock Paint");
2984 rb->lcd_clear_display();
2986 filename[0] = '\0';
2988 if( parameter )
2990 if( load_bitmap( parameter ) <= 0 )
2992 rb->splash( 1*HZ, "File Open Error");
2993 clear_drawing();
2995 else
2997 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
2998 restore_screen();
2999 rb->strcpy( filename, parameter );
3002 else
3004 clear_drawing();
3006 inv_cursor(true);
3008 return rockpaint_loop();