Fix ipod nano 2g bootloader link problem, caused by my previous commit
[kugel-rb.git] / apps / plugins / rockpaint.c
blob0f57b2b11c38ef1fbfc55d5d0db39c03fb45ff43
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 static int img_height = ROWS;
326 static int img_width = COLS;
327 bool isbg = false; /* gruik ugly hack alert */
329 static int preview=false; /* Is preview mode on ? */
331 /* TODO: clean this up */
332 static int x=0, y=0; /* cursor position */
333 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
334 static int prev_x2=-1, prev_y2=-1;
335 static int prev_x3=-1, prev_y3=-1;
336 static int tool_mode=-1;
339 static int bsize=1; /* brush size */
340 static int bspeed=1; /* brush speed */
342 enum Tools { Brush = 0, /* Regular brush */
343 Fill = 1, /* Fill a shape with current color */
344 SelectRectangle = 2,
345 ColorPicker = 3, /* Pick a color */
346 Line = 4, /* Draw a line between two points */
347 Unused = 5, /* THIS IS UNUSED ... */
348 Curve = 6,
349 Text = 7,
350 Rectangle = 8, /* Draw a rectangle */
351 RectangleFull = 9,
352 Oval = 10, /* Draw an oval */
353 OvalFull = 11,
354 LinearGradient = 12,
355 RadialGradient = 13
358 enum Tools tool = Brush;
360 static bool quit=false;
361 static int gridsize=0;
363 static fb_data rp_colors[18] =
365 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
366 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
367 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
368 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
369 COLOR_LIGHTBROWN
372 static fb_data save_buffer[ ROWS*COLS ];
374 extern fb_data rockpaint[];
375 extern fb_data rockpaint_hsvrgb[];
377 /* Maximum string size allowed for the text tool */
378 #define MAX_TEXT 256
380 static union
382 /* Used by fill and gradient algorithms */
383 struct
385 short x;
386 short y;
387 } coord[ ROWS*COLS ];
389 /* Used by bezier curve algorithms */
390 struct
392 short x1, y1;
393 short x2, y2;
394 short x3, y3;
395 short x4, y4;
396 short depth;
397 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
398 * than coord ... so we divide to take
399 * less memory. */
401 /* Used to cut/copy/paste data */
402 fb_data clipboard[ ROWS*COLS ];
404 /* Used for text mode */
405 struct
407 char text[MAX_TEXT];
408 char font[MAX_PATH];
409 char old_font[MAX_PATH];
410 int fh_buf[30];
411 int fw_buf[30];
412 char fontname_buf[30][MAX_PATH];
413 } text;
414 } buffer;
416 /* Current filename */
417 static char filename[MAX_PATH];
419 /* Font preview buffer */
420 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
421 //#define FONT_PREVIEW_HEIGHT 1000
422 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
424 /***********************************************************************
425 * Offscreen buffer/Text/Fonts handling
427 * Parts of code taken from firmware/drivers/lcd-16bit.c
428 ***********************************************************************/
429 static void buffer_mono_bitmap_part(
430 fb_data *buf, int buf_width, int buf_height,
431 const unsigned char *src, int src_x, int src_y,
432 int stride, int x, int y, int width, int height )
433 /* this function only draws the foreground part of the bitmap */
435 const unsigned char *src_end;
436 fb_data *dst, *dst_end;
437 unsigned fgcolor = rb->lcd_get_foreground();
439 /* nothing to draw? */
440 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
441 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
442 return;
444 /* clipping */
445 if( x < 0 )
447 width += x;
448 src_x -= x;
449 x = 0;
451 if( y < 0 )
453 height += y;
454 src_y -= y;
455 y = 0;
457 if( x + width > buf_width )
458 width = buf_width - x;
459 if( y + height > buf_height )
460 height = buf_height - y;
462 src += stride * (src_y >> 3) + src_x; /* move starting point */
463 src_y &= 7;
464 src_end = src + width;
466 dst = buf + y*buf_width + x;
470 const unsigned char *src_col = src++;
471 unsigned data = *src_col >> src_y;
472 fb_data *dst_col = dst++;
473 int numbits = 8 - src_y;
475 dst_end = dst_col + height * buf_width;
478 if( data & 0x01 )
479 *dst_col = fgcolor; /* FIXME ? */
481 dst_col += buf_width;
483 data >>= 1;
484 if( --numbits == 0 )
486 src_col += stride;
487 data = *src_col;
488 numbits = 8;
490 } while( dst_col < dst_end );
491 } while( src < src_end );
494 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
495 int x, int y, int ofs, const unsigned char *str )
497 unsigned short ch;
498 unsigned short *ucs;
500 struct font *pf = rb->font_get( FONT_UI );
501 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
503 ucs = rb->bidi_l2v( str, 1 );
505 while( (ch = *ucs++) != 0 && x < buf_width )
507 int width;
508 const unsigned char *bits;
510 /* get proportional width and glyph bits */
511 width = rb->font_get_width( pf, ch );
513 if( ofs > width )
515 ofs -= width;
516 continue;
519 bits = rb->font_get_bits( pf, ch );
521 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0,
522 width, x, y, width - ofs, pf->height);
524 x += width - ofs;
525 ofs = 0;
529 /***********************************************************************
530 * Menu handling
531 ***********************************************************************/
532 enum {
533 /* Main menu */
534 MAIN_MENU_RESUME,
535 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
536 MAIN_MENU_SET_WIDTH, MAIN_MENU_SET_HEIGHT,
537 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
538 MAIN_MENU_GRID_SIZE,
539 MAIN_MENU_PLAYBACK_CONTROL,
540 MAIN_MENU_EXIT,
542 enum {
543 /* Select action menu */
544 SELECT_MENU_CUT, SELECT_MENU_COPY,
545 SELECT_MENU_INVERT, SELECT_MENU_HFLIP, SELECT_MENU_VFLIP,
546 SELECT_MENU_ROTATE90, SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
547 SELECT_MENU_CANCEL,
549 enum {
550 /* Text menu */
551 TEXT_MENU_TEXT, TEXT_MENU_FONT,
552 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
555 MENUITEM_STRINGLIST(main_menu, "RockPaint", NULL,
556 "Resume", "New", "Load", "Save",
557 "Set Width", "Set Height",
558 "Brush Size", "Brush Speed",
559 "Choose Color", "Grid Size",
560 "Playback Control", "Exit");
561 MENUITEM_STRINGLIST(select_menu, "Select...", NULL,
562 "Cut", "Copy",
563 "Invert", "Horizontal Flip", "Vertical Flip",
564 "Rotate 90°", "Rotate 180°", "Rotate 270°",
565 "Cancel");
566 MENUITEM_STRINGLIST(text_menu, "Text", NULL,
567 "Set Text", "Change Font",
568 "Preview", "Apply", "Cancel");
569 static const int times_list[] = { 1, 2, 4, 8 };
570 static const int gridsize_list[] = { 0, 5, 10, 20 };
571 static const struct opt_items times_options[] = {
572 { "1x", -1 }, { "2x", -1 }, { "4x", -1 }, { "8x", -1 }
574 static const struct opt_items gridsize_options[] = {
575 { "No grid", -1 }, { "5px", -1 }, { "10px", -1 }, { "20px", -1 }
578 static int draw_window( int height, int width,
579 int *top, int *left,
580 const char *title )
582 int fh;
583 rb->lcd_getstringsize( title, NULL, &fh );
584 fh++;
586 const int _top = ( LCD_HEIGHT - height ) / 2;
587 const int _left = ( LCD_WIDTH - width ) / 2;
588 if( top ) *top = _top;
589 if( left ) *left = _left;
590 rb->lcd_set_background(COLOR_BLUE);
591 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
592 rb->lcd_fillrect( _left, _top, width, height );
593 rb->lcd_set_foreground(COLOR_BLUE);
594 rb->lcd_fillrect( _left, _top, width, fh+4 );
595 rb->lcd_set_foreground(COLOR_WHITE);
596 rb->lcd_putsxy( _left+2, _top+2, title );
597 rb->lcd_set_foreground(COLOR_BLACK);
598 rb->lcd_drawrect( _left, _top, width, height );
599 return _top+fh+4;
602 /***********************************************************************
603 * File browser
604 ***********************************************************************/
606 char bbuf[MAX_PATH]; /* used by file and font browsers */
607 char bbuf_s[MAX_PATH]; /* used by file and font browsers */
608 struct tree_context *tree = NULL;
610 static bool check_extention(const char *filename, const char *ext)
612 const char *p = rb->strrchr( filename, '.' );
613 return ( p != NULL && !rb->strcasecmp( p, ext ) );
616 static const char* browse_get_name_cb(int selected_item, void *data,
617 char *buffer, size_t buffer_len)
619 int *indexes = (int *) data;
620 struct entry* dc = tree->dircache;
621 struct entry* e = &dc[indexes[selected_item]];
622 (void) buffer;
623 (void) buffer_len;
625 return e->name;
628 static bool browse( char *dst, int dst_size, const char *start )
630 struct gui_synclist browse_list;
631 int item_count = 0, selected, button;
632 struct tree_context backup;
633 struct entry *dc;
634 bool reload = true;
635 int dirfilter = SHOW_ALL;
636 int *indexes = (int *) buffer.clipboard;
638 char *a;
640 rb->strcpy( bbuf, start );
641 a = bbuf+rb->strlen(bbuf)-1;
642 if( *a != '/' )
644 a[1] = '/';
645 a[2] = '\0';
647 bbuf_s[0] = '\0';
649 rb->gui_synclist_init(&browse_list, browse_get_name_cb,
650 (void*) indexes, false, 1, NULL);
652 tree = rb->tree_get_context();
653 backup = *tree;
654 dc = tree->dircache;
655 a = backup.currdir+rb->strlen(backup.currdir)-1;
656 if( *a != '/' )
658 *++a = '/';
659 *++a = '\0';
661 rb->strcpy( a, dc[tree->selected_item].name );
662 tree->dirfilter = &dirfilter;
663 while( 1 )
665 if( reload )
667 int i;
668 rb->set_current_file(bbuf);
669 item_count = 0;
670 selected = 0;
671 for( i = 0; i < tree->filesindir ; i++)
673 /* only displayes directories and .bmp files */
674 if( ((dc[i].attr & ATTR_DIRECTORY ) &&
675 rb->strcmp( dc[i].name, "." ) &&
676 rb->strcmp( dc[i].name, ".." )) ||
677 ( !(dc[i].attr & ATTR_DIRECTORY) &&
678 check_extention( dc[i].name, ".bmp" ) ) )
680 if( !rb->strcmp( dc[i].name, bbuf_s ) )
681 selected = item_count;
682 indexes[item_count++] = i;
686 rb->gui_synclist_set_nb_items(&browse_list,item_count);
687 rb->gui_synclist_select_item(&browse_list, selected);
688 rb->gui_synclist_set_title(&browse_list, bbuf, NOICON);
689 rb->gui_synclist_draw(&browse_list);
690 reload = false;
692 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
693 if (rb->gui_synclist_do_button(&browse_list,&button,LIST_WRAP_UNLESS_HELD))
694 continue;
695 switch( button )
697 case ACTION_STD_CANCEL:
698 if( !rb->strcmp( bbuf, "/" ) )
700 *tree = backup;
701 rb->set_current_file( backup.currdir );
702 return false;
704 rb->strcpy( bbuf_s, ".." );
705 case ACTION_STD_OK:
706 if( button == ACTION_STD_OK )
708 selected = rb->gui_synclist_get_sel_pos( &browse_list );
709 if( selected < 0 || selected >= item_count )
710 break;
711 struct entry* e = &dc[indexes[selected]];
712 rb->strlcpy( bbuf_s, e->name, sizeof( bbuf_s ) );
713 if( !( e->attr & ATTR_DIRECTORY ) )
715 *tree = backup;
716 rb->set_current_file( backup.currdir );
717 rb->snprintf( dst, dst_size, "%s%s", bbuf, bbuf_s );
718 return true;
721 if( !rb->strcmp( bbuf_s, "." ) ) break;
722 a = bbuf+rb->strlen(bbuf);
723 if( !rb->strcmp( bbuf_s, ".." ) )
725 a--;
726 if( a == bbuf ) break;
727 if( *a == '/' ) a--;
728 while( *a != '/' ) a--;
729 rb->strcpy( bbuf_s, ++a );
730 /* select parent directory */
731 bbuf_s[rb->strlen(bbuf_s)-1] = '\0';
732 *a = '\0';
733 reload = true;
734 break;
736 rb->snprintf( a, bbuf+sizeof(bbuf)-a, "%s/", bbuf_s );
737 reload = true;
738 break;
740 case ACTION_STD_MENU:
741 *tree = backup;
742 rb->set_current_file( backup.currdir );
743 return false;
748 /***********************************************************************
749 * Font browser
751 * FIXME: This still needs some work ... it currently only works fine
752 * on the simulators, disk spins too much on real targets -> rendered
753 * font buffer needed.
754 ***********************************************************************/
755 static bool browse_fonts( char *dst, int dst_size )
757 #define WIDTH ( LCD_WIDTH - 20 )
758 #define HEIGHT ( LCD_HEIGHT - 20 )
759 #define LINE_SPACE 2
760 int top, top_inside = 0, left;
762 DIR *d;
763 struct dirent *de;
764 int fvi = 0; /* first visible item */
765 int lvi = 0; /* last visible item */
766 int si = 0; /* selected item */
767 int osi = 0; /* old selected item */
768 int li = 0; /* last item */
769 int nvih = 0; /* next visible item height */
770 int i;
771 int b_need_redraw = 1; /* Do we need to redraw ? */
773 int cp = 0; /* current position */
774 int fh; /* font height */
776 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
777 #define fw_buf buffer.text.fw_buf
778 int fw;
779 #define fontname_buf buffer.text.fontname_buf
781 rb->snprintf( buffer.text.old_font, MAX_PATH,
782 FONT_DIR "/%s.fnt",
783 rb->global_settings->font_file );
785 while( 1 )
787 if( !b_need_redraw )
789 /* we don't need to redraw ... but we need to unselect
790 * the previously selected item */
791 cp = top_inside + LINE_SPACE;
792 for( i = 0; i+fvi < osi; i++ )
794 cp += fh_buf[i] + LINE_SPACE;
796 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
797 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
798 rb->lcd_set_drawmode(DRMODE_SOLID);
801 if( b_need_redraw )
803 b_need_redraw = 0;
805 d = rb->opendir( FONT_DIR "/" );
806 if( !d )
808 return false;
810 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
811 i = 0;
812 li = -1;
813 while( i < fvi )
815 rb->readdir( d );
816 i++;
818 cp = top_inside+LINE_SPACE;
820 rb->lcd_set_foreground(COLOR_BLACK);
821 rb->lcd_set_background(COLOR_LIGHTGRAY);
823 while( cp < top+HEIGHT )
825 de = rb->readdir( d );
826 if( !de )
828 li = i-1;
829 break;
831 if( !check_extention( de->d_name, ".fnt" ) )
832 continue;
833 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
834 de->d_name );
835 rb->font_load( bbuf );
836 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
837 if( nvih > 0 )
839 nvih -= fh;
840 fvi++;
841 if( nvih < 0 ) nvih = 0;
842 i++;
843 continue;
845 if( cp + fh >= top+HEIGHT )
847 nvih = fh;
848 break;
850 rb->lcd_putsxy( left+10, cp, de->d_name );
851 fh_buf[i-fvi] = fh;
852 fw_buf[i-fvi] = fw;
853 cp += fh + LINE_SPACE;
854 rb->strcpy( fontname_buf[i-fvi], bbuf );
855 i++;
857 lvi = i-1;
858 if( li == -1 )
860 if( !(de = rb->readdir( d ) ) )
862 li = lvi;
864 else if( !nvih && check_extention( de->d_name, ".fnt" ) )
866 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
867 de->d_name );
868 rb->font_load( bbuf );
869 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
870 nvih = fh;
873 rb->font_load( buffer.text.old_font );
874 rb->closedir( d );
877 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
878 cp = top_inside + LINE_SPACE;
879 for( i = 0; i+fvi < si; i++ )
881 cp += fh_buf[i] + LINE_SPACE;
883 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
884 rb->lcd_set_drawmode(DRMODE_SOLID);
886 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
888 osi = si;
889 i = fvi;
890 switch( rb->button_get(true) )
892 case ROCKPAINT_UP:
893 case ROCKPAINT_UP|BUTTON_REPEAT:
894 if( si > 0 )
896 si--;
897 if( si<fvi )
899 fvi = si;
902 break;
904 case ROCKPAINT_DOWN:
905 case ROCKPAINT_DOWN|BUTTON_REPEAT:
906 if( li == -1 || si < li )
908 si++;
910 break;
912 case ROCKPAINT_LEFT:
913 return false;
915 case ROCKPAINT_RIGHT:
916 case ROCKPAINT_DRAW:
917 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
918 return true;
921 if( i != fvi || si > lvi )
923 b_need_redraw = 1;
926 if( si<=lvi )
928 nvih = 0;
931 #undef fh_buf
932 #undef fw_buf
933 #undef fontname_buf
934 #undef WIDTH
935 #undef HEIGHT
936 #undef LINE_SPACE
939 /***********************************************************************
940 * HSVRGB Color chooser
941 ***********************************************************************/
942 static unsigned int color_chooser( unsigned int color )
944 int red = RGB_UNPACK_RED( color );
945 int green = RGB_UNPACK_GREEN( color );
946 int blue = RGB_UNPACK_BLUE( color );
947 int hue, saturation, value;
948 int r, g, b; /* temp variables */
949 int i, top, left;
951 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
952 Red = 3, Green = 4, Blue = 5 };
953 enum BaseColor current = Red;
954 bool has_changed;
956 char str[6] = "";
958 restore_screen();
960 rgb2hsv( red, green, blue, &hue, &saturation, &value );
962 while( 1 )
964 has_changed = false;
965 color = LCD_RGBPACK( red, green, blue );
967 #define HEIGHT ( 100 )
968 #define WIDTH ( 150 )
970 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
971 top -= 15;
973 for( i=0; i<100; i++ )
975 hsv2rgb( i*36, saturation, value, &r, &g, &b );
976 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
977 rb->lcd_vline( left+15+i, top+20, top+27 );
978 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
979 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
980 rb->lcd_vline( left+15+i, top+30, top+37 );
981 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
982 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
983 rb->lcd_vline( left+15+i, top+40, top+47 );
984 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
985 rb->lcd_vline( left+15+i, top+50, top+57 );
986 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
987 rb->lcd_vline( left+15+i, top+60, top+67 );
988 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
989 rb->lcd_vline( left+15+i, top+70, top+77 );
992 rb->lcd_set_foreground(COLOR_BLACK);
993 #define POSITION( a, i ) \
994 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
995 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
996 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
997 rb->lcd_drawpixel( left+16+i, top + 28 + a );
998 POSITION( 0, hue/36 );
999 POSITION( 10, saturation*99/255 );
1000 POSITION( 20, value*99/255 );
1001 POSITION( 30, red*99/255 );
1002 POSITION( 40, green*99/255 );
1003 POSITION( 50, blue*99/255 );
1004 #undef POSITION
1005 rb->lcd_set_background(COLOR_LIGHTGRAY);
1006 rb->lcd_setfont( FONT_SYSFIXED );
1007 rb->snprintf( str, 6, "%d", hue/10 );
1008 rb->lcd_putsxy( left + 117, top + 20, str );
1009 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1010 rb->lcd_putsxy( left + 117, top + 30, str );
1011 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1012 rb->lcd_putsxy( left + 117, top + 40, str );
1013 rb->snprintf( str, 6, "%d", red );
1014 rb->lcd_putsxy( left + 117, top + 50, str );
1015 rb->snprintf( str, 6, "%d", green );
1016 rb->lcd_putsxy( left + 117, top + 60, str );
1017 rb->snprintf( str, 6, "%d", blue );
1018 rb->lcd_putsxy( left + 117, top + 70, str );
1019 rb->lcd_setfont( FONT_UI );
1021 #define CURSOR( l ) \
1022 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1023 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1024 CURSOR( 5 );
1025 #undef CURSOR
1027 rb->lcd_set_foreground( color );
1028 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1030 rb->lcd_update();
1032 switch( rb->button_get(true) )
1034 case ROCKPAINT_UP:
1035 current = ( current + 5 )%6;
1036 break;
1038 case ROCKPAINT_DOWN:
1039 current = (current + 1 )%6;
1040 break;
1042 case ROCKPAINT_LEFT:
1043 has_changed = true;
1044 switch( current )
1046 case Hue:
1047 hue = ( hue + 3600 - 10 )%3600;
1048 break;
1049 case Saturation:
1050 if( saturation ) saturation--;
1051 break;
1052 case Value:
1053 if( value ) value--;
1054 break;
1055 case Red:
1056 if( red ) red--;
1057 break;
1058 case Green:
1059 if( green ) green--;
1060 break;
1061 case Blue:
1062 if( blue ) blue--;
1063 break;
1065 break;
1067 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1068 has_changed = true;
1069 switch( current )
1071 case Hue:
1072 hue = ( hue + 3600 - 100 )%3600;
1073 break;
1074 case Saturation:
1075 if( saturation >= 8 ) saturation-=8;
1076 else saturation = 0;
1077 break;
1078 case Value:
1079 if( value >= 8 ) value-=8;
1080 else value = 0;
1081 break;
1082 case Red:
1083 if( red >= 8 ) red-=8;
1084 else red = 0;
1085 break;
1086 case Green:
1087 if( green >= 8 ) green-=8;
1088 else green = 0;
1089 break;
1090 case Blue:
1091 if( blue >= 8 ) blue-=8;
1092 else blue = 0;
1093 break;
1095 break;
1097 case ROCKPAINT_RIGHT:
1098 has_changed = true;
1099 switch( current )
1101 case Hue:
1102 hue = ( hue + 10 )%3600;
1103 break;
1104 case Saturation:
1105 if( saturation < 0xff ) saturation++;
1106 break;
1107 case Value:
1108 if( value < 0xff ) value++;
1109 break;
1110 case Red:
1111 if( red < 0xff ) red++;
1112 break;
1113 case Green:
1114 if( green < 0xff ) green++;
1115 break;
1116 case Blue:
1117 if( blue < 0xff ) blue++;
1118 break;
1120 break;
1122 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1123 has_changed = true;
1124 switch( current )
1126 case Hue:
1127 hue = ( hue + 100 )%3600;
1128 break;
1129 case Saturation:
1130 if( saturation < 0xff - 8 ) saturation+=8;
1131 else saturation = 0xff;
1132 break;
1133 case Value:
1134 if( value < 0xff - 8 ) value+=8;
1135 else value = 0xff;
1136 break;
1137 case Red:
1138 if( red < 0xff - 8 ) red+=8;
1139 else red = 0xff;
1140 break;
1141 case Green:
1142 if( green < 0xff - 8 ) green+=8;
1143 else green = 0xff;
1144 break;
1145 case Blue:
1146 if( blue < 0xff - 8 ) blue+=8;
1147 else blue = 0xff;
1148 break;
1150 break;
1152 case ROCKPAINT_DRAW:
1153 return color;
1155 if( has_changed )
1157 switch( current )
1159 case Hue:
1160 case Saturation:
1161 case Value:
1162 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1163 break;
1165 case Red:
1166 case Green:
1167 case Blue:
1168 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1169 break;
1172 #undef HEIGHT
1173 #undef WIDTH
1177 /***********************************************************************
1178 * Misc routines
1179 ***********************************************************************/
1180 static void init_buffer(void)
1182 int i;
1183 fb_data color = rp_colors[ bgdrawcolor ];
1184 for( i = 0; i < ROWS*COLS; i++ )
1186 save_buffer[i] = color;
1190 static void draw_pixel(int x,int y)
1192 if( !preview )
1194 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1195 if( isbg )
1197 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1199 else
1201 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1204 rb->lcd_drawpixel(x,y);
1207 static void color_picker( int x, int y )
1209 if( preview )
1211 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1212 #define PSIZE 12
1213 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1214 if( x >= COLS - PSIZE ) x -= PSIZE + 2;
1215 if( y >= ROWS - PSIZE ) y -= PSIZE + 2;
1216 rb->lcd_drawrect( x + 2, y + 2, PSIZE - 2, PSIZE - 2 );
1217 rb->lcd_set_drawmode(DRMODE_SOLID);
1218 rb->lcd_drawrect( x + 3, y + 3, PSIZE - 4, PSIZE - 4 );
1219 #undef PSIZE
1220 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1222 else
1224 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1228 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1229 /* This is a preview mode only function */
1231 int i,a;
1232 if( x1 > x2 )
1234 i = x1;
1235 x1 = x2;
1236 x2 = i;
1238 if( y1 > y2 )
1240 i = y1;
1241 y1 = y2;
1242 y2 = i;
1244 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1245 i = 0;
1246 for( a = x1; a < x2; a++, i++ )
1247 if( i%2 )
1248 rb->lcd_drawpixel( a, y1 );
1249 for( a = y1; a < y2; a++, i++ )
1250 if( i%2 )
1251 rb->lcd_drawpixel( x2, a );
1252 if( y2 != y1 )
1253 for( a = x2; a > x1; a--, i++ )
1254 if( i%2 )
1255 rb->lcd_drawpixel( a, y2 );
1256 if( x2 != x1 )
1257 for( a = y2; a > y1; a--, i++ )
1258 if( i%2 )
1259 rb->lcd_drawpixel( x1, a );
1260 rb->lcd_set_drawmode(DRMODE_SOLID);
1263 static void copy_to_clipboard( void )
1265 /* This needs to be optimised ... but i'm lazy ATM */
1266 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1269 /* no preview mode handling atm ... do we need it ? (one if) */
1270 static void draw_invert( int x1, int y1, int x2, int y2 )
1272 int i;
1273 if( x1 > x2 )
1275 i = x1;
1276 x1 = x2;
1277 x2 = i;
1279 if( y1 > y2 )
1281 i = y1;
1282 y1 = y2;
1283 y2 = i;
1286 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1287 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1288 rb->lcd_set_drawmode(DRMODE_SOLID);
1290 for( ; y1<=y2; y1++ )
1292 for( i = x1; i<=x2; i++ )
1294 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1297 /*if( update )*/ rb->lcd_update();
1300 static void draw_hflip( int x1, int y1, int x2, int y2 )
1302 int i;
1303 if( x1 > x2 )
1305 i = x1;
1306 x1 = x2;
1307 x2 = i;
1309 if( y1 > y2 )
1311 i = y1;
1312 y1 = y2;
1313 y2 = i;
1316 copy_to_clipboard();
1318 for( i = 0; i <= y2 - y1; i++ )
1320 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1321 buffer.clipboard+(y2-i)*COLS+x1,
1322 (x2-x1+1)*sizeof( fb_data ) );
1324 restore_screen();
1325 rb->lcd_update();
1328 static void draw_vflip( int x1, int y1, int x2, int y2 )
1330 int i;
1331 if( x1 > x2 )
1333 i = x1;
1334 x1 = x2;
1335 x2 = i;
1337 if( y1 > y2 )
1339 i = y1;
1340 y1 = y2;
1341 y2 = i;
1344 copy_to_clipboard();
1346 for( ; y1 <= y2; y1++ )
1348 for( i = 0; i <= x2 - x1; i++ )
1350 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1353 restore_screen();
1354 rb->lcd_update();
1357 /* direction: -1 = left, 1 = right */
1358 static void draw_rot_90_deg( int x1, int y1, int x2, int y2, int direction )
1360 int i, j;
1361 if( x1 > x2 )
1363 i = x1;
1364 x1 = x2;
1365 x2 = i;
1367 if( y1 > y2 )
1369 i = y1;
1370 y1 = y2;
1371 y2 = i;
1374 copy_to_clipboard();
1376 fb_data color = rp_colors[ bgdrawcolor ];
1377 const int width = x2 - x1, height = y2 - y1;
1378 const int sub_half = width/2-height/2, add_half = (width+height)/2;
1379 if( width > height )
1381 for( i = 0; i <= height; i++ )
1383 for( j = 0; j < sub_half; j++ )
1384 save_buffer[(y1+i)*COLS+x1+j] = color;
1385 for( j = add_half+1; j <= width; j++ )
1386 save_buffer[(y1+i)*COLS+x1+j] = color;
1389 else if( width < height )
1391 for( j = 0; j <= width; j++ )
1393 for( i = 0; i < -sub_half; i++ )
1394 save_buffer[(y1+i)*COLS+x1+j] = color;
1395 for( i = add_half+1; i <= height; i++ )
1396 save_buffer[(y1+i)*COLS+x1+j] = color;
1399 int x3 = x1 + sub_half, y3 = y1 - sub_half;
1400 int is = x3<0?-x3:0, ie = COLS-x3-1, js = y3<0?-y3:0, je = ROWS-y3-1;
1401 if( ie > height ) ie = height;
1402 if( je > width ) je = width;
1403 for( i = is; i <= ie; i++ )
1405 for( j = js; j <= je; j++ )
1407 int x, y;
1408 if(direction > 0)
1410 x = x1+j;
1411 y = y1+height-i;
1413 else
1415 x = x1+width-j;
1416 y = y1+i;
1418 save_buffer[(y3+j)*COLS+x3+i] = buffer.clipboard[y*COLS+x];
1421 restore_screen();
1422 rb->lcd_update();
1425 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1426 int src_y2, int x1, int y1, int mode )
1428 int i, width, height;
1429 if( mode == SELECT_MENU_CUT )
1431 i = drawcolor;
1432 drawcolor = bgdrawcolor;
1433 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1434 drawcolor = i;
1436 if( src_x1 > src_x2 )
1438 i = src_x1;
1439 src_x1 = src_x2;
1440 src_x2 = i;
1442 if( src_y1 > src_y2 )
1444 i = src_y1;
1445 src_y1 = src_y2;
1446 src_y2 = i;
1448 width = src_x2 - src_x1 + 1;
1449 height = src_y2 - src_y1 + 1;
1450 /* clipping */
1451 if( x1 + width > COLS )
1452 width = COLS - x1;
1453 if( y1 + height > ROWS )
1454 height = ROWS - y1;
1456 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1457 x1, y1, width, height );
1458 if( !preview )
1460 for( i = 0; i < height; i++ )
1462 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1463 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1464 width*sizeof( fb_data ) );
1469 static void show_grid( bool update )
1471 int i;
1472 if( gridsize > 0 )
1474 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1475 for( i = gridsize; i < img_width; i+= gridsize )
1477 rb->lcd_vline( i, 0, img_height-1 );
1479 for( i = gridsize; i < img_height; i+= gridsize )
1481 rb->lcd_hline( 0, img_width-1, i );
1483 rb->lcd_set_drawmode(DRMODE_SOLID);
1484 if( update ) rb->lcd_update();
1488 static void draw_text( int x, int y )
1490 int selected = 0;
1491 buffer.text.text[0] = '\0';
1492 rb->snprintf( buffer.text.old_font, MAX_PATH,
1493 FONT_DIR "/%s.fnt",
1494 rb->global_settings->font_file );
1495 while( 1 )
1497 switch( rb->do_menu( &text_menu, &selected, NULL, NULL ) )
1499 case TEXT_MENU_TEXT:
1500 rb->lcd_set_foreground(COLOR_BLACK);
1501 rb->kbd_input( buffer.text.text, MAX_TEXT );
1502 break;
1504 case TEXT_MENU_FONT:
1505 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1507 rb->font_load( buffer.text.font );
1509 break;
1511 case TEXT_MENU_PREVIEW:
1512 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1513 while( 1 )
1515 int button;
1516 restore_screen();
1517 rb->lcd_putsxy( x, y, buffer.text.text );
1518 rb->lcd_update();
1519 switch( button = rb->button_get( true ) )
1521 case ROCKPAINT_LEFT:
1522 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1523 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1524 if (x<0) x=COLS-1;
1525 break;
1527 case ROCKPAINT_RIGHT:
1528 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1529 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1530 if (x>=COLS) x=0;
1531 break;
1533 case ROCKPAINT_UP:
1534 case ROCKPAINT_UP | BUTTON_REPEAT:
1535 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1536 if (y<0) y=ROWS-1;
1537 break;
1539 case ROCKPAINT_DOWN:
1540 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1541 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1542 if (y>=ROWS-1) y=0;
1543 break;
1545 case ROCKPAINT_DRAW:
1546 break;
1547 default:
1548 if(rb->default_event_handler(button)
1549 == SYS_USB_CONNECTED)
1550 button = ROCKPAINT_DRAW;
1551 break;
1553 if( button == ROCKPAINT_DRAW ) break;
1555 break;
1557 case TEXT_MENU_APPLY:
1558 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1559 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1560 buffer.text.text );
1561 case TEXT_MENU_CANCEL:
1562 default:
1563 restore_screen();
1564 rb->font_load( buffer.text.old_font );
1565 return;
1570 static void draw_brush( int x, int y )
1572 int i,j;
1573 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1575 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1577 draw_pixel( x+i, y+j );
1582 /* This is an implementation of Bresenham's line algorithm.
1583 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1585 static void draw_line( int x1, int y1, int x2, int y2 )
1587 int x = x1;
1588 int y = y1;
1589 int deltax = x2 - x1;
1590 int deltay = y2 - y1;
1591 int i;
1593 int xerr = abs(deltax);
1594 int yerr = abs(deltay);
1595 int xstep = deltax > 0 ? 1 : -1;
1596 int ystep = deltay > 0 ? 1 : -1;
1597 int err;
1599 if (yerr > xerr)
1601 /* more vertical */
1602 err = yerr;
1603 xerr <<= 1;
1604 yerr <<= 1;
1606 /* to leave off the last pixel of the line, leave off the "+ 1" */
1607 for (i = abs(deltay) + 1; i; --i)
1609 draw_pixel(x, y);
1610 y += ystep;
1611 err -= xerr;
1612 if (err < 0) {
1613 x += xstep;
1614 err += yerr;
1618 else
1620 /* more horizontal */
1621 err = xerr;
1622 xerr <<= 1;
1623 yerr <<= 1;
1625 for (i = abs(deltax) + 1; i; --i)
1627 draw_pixel(x, y);
1628 x += xstep;
1629 err -= yerr;
1630 if (err < 0) {
1631 y += ystep;
1632 err += xerr;
1638 static void draw_curve( int x1, int y1, int x2, int y2,
1639 int xa, int ya, int xb, int yb )
1641 int i = 0;
1642 short xl1, yl1;
1643 short xl2, yl2;
1644 short xl3, yl3;
1645 short xl4, yl4;
1646 short xr1, yr1;
1647 short xr2, yr2;
1648 short xr3, yr3;
1649 short xr4, yr4;
1650 short depth;
1651 short xh, yh;
1653 if( x1 == x2 && y1 == y2 )
1655 draw_pixel( x1, y1 );
1656 return;
1659 // if( preview )
1661 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1662 if( xa == -1 || ya == -1 )
1664 rb->lcd_drawline( x1, y1, xb, yb );
1665 rb->lcd_drawline( x2, y2, xb, yb );
1667 else
1669 rb->lcd_drawline( x1, y1, xa, ya );
1670 rb->lcd_drawline( x2, y2, xb, yb );
1672 rb->lcd_set_drawmode(DRMODE_SOLID);
1675 if( xa == -1 || ya == -1 )
1676 /* We only have 3 of the points
1677 * This will currently only be used in preview mode */
1679 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1680 buffer.bezier[i].x1 = a1; \
1681 buffer.bezier[i].y1 = b1; \
1682 buffer.bezier[i].x2 = a2; \
1683 buffer.bezier[i].y2 = b2; \
1684 buffer.bezier[i].x3 = a3; \
1685 buffer.bezier[i].y3 = b3; \
1686 buffer.bezier[i].depth = d; \
1687 i++;
1688 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1689 i--; \
1690 a1 = buffer.bezier[i].x1; \
1691 b1 = buffer.bezier[i].y1; \
1692 a2 = buffer.bezier[i].x2; \
1693 b2 = buffer.bezier[i].y2; \
1694 a3 = buffer.bezier[i].x3; \
1695 b3 = buffer.bezier[i].y3; \
1696 d = buffer.bezier[i].depth;
1697 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1698 while( i )
1700 /* de Casteljau's algorithm (see wikipedia) */
1701 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1702 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1704 xl2 = ( xl1 + xb )>>1;
1705 yl2 = ( yl1 + yb )>>1;
1706 xr2 = ( xb + xr3 )>>1;
1707 yr2 = ( yb + yr3 )>>1;
1708 xr1 = ( xl2 + xr2 )>>1;
1709 yr1 = ( yl2 + yr2 )>>1;
1710 xl3 = xr1;
1711 yl3 = yr1;
1712 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1713 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1715 else
1717 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1718 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1721 #undef PUSH
1722 #undef POP
1724 else /* We have the 4 points */
1726 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1727 buffer.bezier[i].x1 = a1; \
1728 buffer.bezier[i].y1 = b1; \
1729 buffer.bezier[i].x2 = a2; \
1730 buffer.bezier[i].y2 = b2; \
1731 buffer.bezier[i].x3 = a3; \
1732 buffer.bezier[i].y3 = b3; \
1733 buffer.bezier[i].x4 = a4; \
1734 buffer.bezier[i].y4 = b4; \
1735 buffer.bezier[i].depth = d; \
1736 i++;
1737 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1738 i--; \
1739 a1 = buffer.bezier[i].x1; \
1740 b1 = buffer.bezier[i].y1; \
1741 a2 = buffer.bezier[i].x2; \
1742 b2 = buffer.bezier[i].y2; \
1743 a3 = buffer.bezier[i].x3; \
1744 b3 = buffer.bezier[i].y3; \
1745 a4 = buffer.bezier[i].x4; \
1746 b4 = buffer.bezier[i].y4; \
1747 d = buffer.bezier[i].depth;
1749 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1750 while( i )
1752 /* de Casteljau's algorithm (see wikipedia) */
1753 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1754 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1756 xl2 = ( xl1 + xa )>>1;
1757 yl2 = ( yl1 + ya )>>1;
1758 xh = ( xa + xb )>>1;
1759 yh = ( ya + yb )>>1;
1760 xr3 = ( xb + xr4 )>>1;
1761 yr3 = ( yb + yr4 )>>1;
1762 xl3 = ( xl2 + xh )>>1;
1763 yl3 = ( yl2 + yh )>>1;
1764 xr2 = ( xr3 + xh )>>1;
1765 yr2 = ( yr3 + yh )>>1;
1766 xl4 = ( xl3 + xr2 )>>1;
1767 yl4 = ( yl3 + yr2 )>>1;
1768 xr1 = xl4;
1769 yr1 = yl4;
1770 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1771 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1773 else
1775 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1776 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1779 #undef PUSH
1780 #undef POP
1784 static void draw_rect( int x1, int y1, int x2, int y2 )
1786 draw_line( x1, y1, x1, y2 );
1787 draw_line( x1, y1, x2, y1 );
1788 draw_line( x1, y2, x2, y2 );
1789 draw_line( x2, y1, x2, y2 );
1792 static void togglebg( void )
1794 if( isbg )
1796 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1798 else
1800 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1802 isbg = !isbg;
1805 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1807 /* GRUIK */
1808 int x;
1809 togglebg();
1810 if( x1 > x2 )
1812 x = x1;
1813 x1 = x2;
1814 x2 = x;
1816 x = x1;
1817 do {
1818 draw_line( x, y1, x, y2 );
1819 } while( ++x <= x2 );
1820 togglebg();
1821 draw_rect( x1, y1, x2, y2 );
1824 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1826 /* TODO: simplify :) */
1827 int cx = (x1+x2)>>1;
1828 int cy = (y1+y2)>>1;
1830 int rx = (x1-x2)>>1;
1831 int ry = (y1-y2)>>1;
1832 if( rx < 0 ) rx *= -1;
1833 if( ry < 0 ) ry *= -1;
1835 if( rx == 0 || ry == 0 )
1837 draw_line( x1, y1, x2, y2 );
1838 return;
1841 int x,y;
1842 int dst, old_dst;
1844 for( x = 0; x < rx; x++ )
1846 y = 0;
1847 dst = -0xfff;
1848 do {
1849 old_dst = dst;
1850 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1851 y++;
1852 } while( dst < 0 );
1853 if( -old_dst < dst ) y--;
1854 if( full )
1856 draw_line( cx+x, cy, cx+x, cy+y );
1857 draw_line( cx+x, cy, cx+x, cy-y );
1858 draw_line( cx-x, cy, cx-x, cy+y );
1859 draw_line( cx-x, cy, cx-x, cy-y );
1861 else
1863 draw_pixel( cx+x, cy+y );
1864 draw_pixel( cx+x, cy-y );
1865 draw_pixel( cx-x, cy+y );
1866 draw_pixel( cx-x, cy-y );
1869 for( y = 0; y < ry; y++ )
1871 x = 0;
1872 dst = -0xfff;
1873 do {
1874 old_dst = dst;
1875 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1876 x++;
1877 } while( dst < 0 );
1878 if( -old_dst < dst ) x--;
1879 if( full )
1881 draw_line( cx+x, cy, cx+x, cy+y );
1882 draw_line( cx+x, cy, cx+x, cy-y );
1883 draw_line( cx-x, cy, cx-x, cy+y );
1884 draw_line( cx-x, cy, cx-x, cy-y );
1886 else
1888 draw_pixel( cx+x, cy+y );
1889 draw_pixel( cx+x, cy-y );
1890 draw_pixel( cx-x, cy+y );
1891 draw_pixel( cx-x, cy-y );
1896 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1898 draw_oval( x1, y1, x2, y2, false );
1901 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1903 togglebg();
1904 draw_oval( x1, y1, x2, y2, true );
1905 togglebg();
1906 draw_oval( x1, y1, x2, y2, false );
1909 static void draw_fill( int x0, int y0 )
1911 #define PUSH( a, b ) \
1912 draw_pixel( (int)a, (int)b ); \
1913 buffer.coord[i].x = a; \
1914 buffer.coord[i].y = b; \
1915 i++;
1916 #define POP( a, b ) \
1917 i--; \
1918 a = buffer.coord[i].x; \
1919 b = buffer.coord[i].y;
1921 unsigned int i=0;
1922 short x = x0;
1923 short y = y0;
1924 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1926 if( prev_color == rp_colors[ drawcolor ] ) return;
1928 PUSH( x, y );
1930 while( i != 0 )
1932 POP( x, y );
1933 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1935 PUSH( x-1, y );
1937 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1939 PUSH( x+1, y );
1941 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1943 PUSH( x, y-1 );
1945 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1947 PUSH( x, y+1 );
1950 #undef PUSH
1951 #undef POP
1955 /* For preview purposes only */
1956 static void line_gradient( int x1, int y1, int x2, int y2 )
1958 int r1, g1, b1;
1959 int r2, g2, b2;
1960 int h1, s1, v1, h2, s2, v2, r, g, b;
1961 int w, h, x, y;
1963 bool a = false;
1965 x1 <<= 1;
1966 y1 <<= 1;
1967 x2 <<= 1;
1968 y2 <<= 1;
1970 w = x1 - x2;
1971 h = y1 - y2;
1973 if( w == 0 && h == 0 )
1975 draw_pixel( x1>>1, y1>>1 );
1976 return;
1979 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
1980 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
1981 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
1982 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
1983 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
1984 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
1986 if( w < 0 )
1988 w *= -1;
1989 a = true;
1991 if( h < 0 )
1993 h *= -1;
1994 a = !a;
1996 if( a )
1998 r = r1;
1999 r1 = r2;
2000 r2 = r;
2001 g = g1;
2002 g1 = g2;
2003 g2 = g;
2004 b = b1;
2005 b1 = b2;
2006 b2 = b;
2009 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2010 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2012 if( w > h )
2014 if( x1 > x2 )
2016 x = x2;
2017 y = y2;
2018 x2 = x1;
2019 y2 = y1;
2020 x1 = x;
2021 y1 = y;
2023 w = x1 - x2;
2024 h = y1 - y2;
2025 while( x1 <= x2 )
2027 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2028 s1+((s2-s1)*(x1-x2))/w,
2029 v1+((v2-v1)*(x1-x2))/w,
2030 &r, &g, &b );
2031 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2032 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2033 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2034 x1+=2;
2035 y1 = y2 - ( x2 - x1 ) * h / w;
2038 else /* h > w */
2040 if( y1 > y2 )
2042 x = x2;
2043 y = y2;
2044 x2 = x1;
2045 y2 = y1;
2046 x1 = x;
2047 y1 = y;
2049 w = x1 - x2;
2050 h = y1 - y2;
2051 while( y1 <= y2 )
2053 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2054 s1+((s2-s1)*(y1-y2))/h,
2055 v1+((v2-v1)*(y1-y2))/h,
2056 &r, &g, &b );
2057 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2058 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2059 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2060 y1+=2;
2061 x1 = x2 - ( y2 - y1 ) * w / h;
2064 if( a )
2066 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2068 else
2070 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2074 static void linear_gradient( int x1, int y1, int x2, int y2 )
2076 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2077 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2078 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2079 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2080 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2081 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2083 int h1, s1, v1, h2, s2, v2, r, g, b;
2085 /* radius^2 */
2086 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2087 int dist2, i=0;
2089 /* We only propagate the gradient to neighboring pixels with the same
2090 * color as ( x1, y1 ) */
2091 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2093 int x = x1;
2094 int y = y1;
2096 if( radius2 == 0 ) return;
2097 if( preview )
2099 line_gradient( x1, y1, x2, y2 );
2102 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2103 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2105 #define PUSH( x0, y0 ) \
2106 buffer.coord[i].x = (short)(x0); \
2107 buffer.coord[i].y = (short)(y0); \
2108 i++;
2109 #define POP( a, b ) \
2110 i--; \
2111 a = (int)buffer.coord[i].x; \
2112 b = (int)buffer.coord[i].y;
2114 PUSH( x, y );
2116 while( i != 0 )
2118 POP( x, y );
2120 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2121 if( dist2 <= 0 )
2123 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2125 else if( dist2 < radius2 )
2127 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2128 s1+((s2-s1)*dist2)/radius2,
2129 v1+((v2-v1)*dist2)/radius2,
2130 &r, &g, &b );
2131 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2133 else
2135 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2137 if( rp_colors[ drawcolor ] == prev_color )
2139 if( rp_colors[ drawcolor ])
2140 rp_colors[ drawcolor ]--; /* GRUIK */
2141 else
2142 rp_colors[ drawcolor ]++; /* GRUIK */
2144 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2145 draw_pixel( x, y );
2147 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2149 PUSH( x-1, y );
2151 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2153 PUSH( x+1, y );
2155 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2157 PUSH( x, y-1 );
2159 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2161 PUSH( x, y+1 );
2164 #undef PUSH
2165 #undef POP
2167 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2170 static void radial_gradient( int x1, int y1, int x2, int y2 )
2172 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2173 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2174 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2175 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2176 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2177 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2179 int h1, s1, v1, h2, s2, v2, r, g, b;
2181 /* radius^2 */
2182 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2183 int dist2, i=0;
2185 /* We only propagate the gradient to neighboring pixels with the same
2186 * color as ( x1, y1 ) */
2187 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2189 int x = x1;
2190 int y = y1;
2192 if( radius2 == 0 ) return;
2193 if( preview )
2195 line_gradient( x1, y1, x2, y2 );
2198 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2199 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2201 #define PUSH( x0, y0 ) \
2202 buffer.coord[i].x = (short)(x0); \
2203 buffer.coord[i].y = (short)(y0); \
2204 i++;
2205 #define POP( a, b ) \
2206 i--; \
2207 a = (int)buffer.coord[i].x; \
2208 b = (int)buffer.coord[i].y;
2210 PUSH( x, y );
2212 while( i != 0 )
2214 POP( x, y );
2216 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2218 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2219 s1+((s2-s1)*dist2)/radius2,
2220 v1+((v2-v1)*dist2)/radius2,
2221 &r, &g, &b );
2222 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2224 else
2226 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2228 if( rp_colors[ drawcolor ] == prev_color )
2230 if( rp_colors[ drawcolor ])
2231 rp_colors[ drawcolor ]--; /* GRUIK */
2232 else
2233 rp_colors[ drawcolor ]++; /* GRUIK */
2235 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2236 draw_pixel( x, y );
2238 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2240 PUSH( x-1, y );
2242 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2244 PUSH( x+1, y );
2246 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2248 PUSH( x, y-1 );
2250 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2252 PUSH( x, y+1 );
2255 #undef PUSH
2256 #undef POP
2258 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2261 static void draw_toolbars(bool update)
2263 int i;
2264 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2265 rb->lcd_set_background( COLOR_LIGHTGRAY );
2266 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2267 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2268 rb->lcd_set_foreground( COLOR_BLACK );
2269 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2271 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2272 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2273 TB_SC_SIZE, TB_SC_SIZE );
2274 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2275 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2276 TB_SC_SIZE, TB_SC_SIZE );
2277 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2278 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2279 TB_SC_SIZE, TB_SC_SIZE );
2280 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2281 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2282 TB_SC_SIZE, TB_SC_SIZE );
2284 for( i=0; i<18; i++ )
2286 rb->lcd_set_foreground( rp_colors[i] );
2287 rb->lcd_fillrect(
2288 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2289 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2290 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2291 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2292 rb->lcd_drawrect(
2293 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2294 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2295 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2298 #define SEPARATOR( x, y ) \
2299 rb->lcd_set_foreground( COLOR_WHITE ); \
2300 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2301 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2302 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2303 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2305 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2306 TB_TL_WIDTH, TB_TL_HEIGHT );
2307 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2308 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2309 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2310 TB_TL_SIZE, TB_TL_SIZE );
2312 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2314 rb->lcd_setfont( FONT_SYSFIXED );
2315 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2316 rb->lcd_setfont( FONT_UI );
2317 #undef TOP
2319 if( update ) rb->lcd_update();
2322 static void toolbar( void )
2324 int button, i, j;
2325 restore_screen();
2326 draw_toolbars( false );
2327 y = LCD_HEIGHT-TB_HEIGHT/2;
2328 inv_cursor( true );
2329 while( 1 )
2331 switch( button = rb->button_get( true ) )
2333 case ROCKPAINT_DRAW:
2334 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2335 if( y >= TOP + TB_SC_FG_TOP
2336 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2337 && x >= TB_SC_FG_LEFT
2338 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2340 /* click on the foreground color */
2341 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2343 else if( y >= TOP + TB_SC_BG_TOP
2344 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2345 && x >= TB_SC_BG_LEFT
2346 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2348 /* click on the background color */
2349 i = drawcolor;
2350 drawcolor = bgdrawcolor;
2351 bgdrawcolor = i;
2353 else if( y >= TOP + TB_PL_TOP
2354 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2355 && x >= TB_PL_LEFT
2356 && x < TB_PL_LEFT + TB_PL_WIDTH )
2358 /* click on the palette */
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 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2362 break;
2363 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2364 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2365 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2367 else if( y >= TOP+TB_TL_TOP
2368 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2369 && x >= TB_TL_LEFT
2370 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2372 /* click on the tools */
2373 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2374 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2375 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2376 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2377 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2378 tool = i*2+j;
2379 prev_x = -1;
2380 prev_y = -1;
2381 prev_x2 = -1;
2382 prev_y2 = -1;
2383 prev_x3 = -1;
2384 prev_y3 = -1;
2385 preview = false;
2387 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2389 /* menu button */
2390 goto_menu();
2392 #undef TOP
2393 restore_screen();
2394 draw_toolbars( false );
2395 inv_cursor( true );
2396 break;
2398 case ROCKPAINT_LEFT:
2399 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2400 inv_cursor(false);
2401 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2402 if (x<0) x=COLS-1;
2403 inv_cursor(true);
2404 break;
2406 case ROCKPAINT_RIGHT:
2407 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2408 inv_cursor(false);
2409 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2410 if (x>=COLS) x=0;
2411 inv_cursor(true);
2412 break;
2414 case ROCKPAINT_UP:
2415 case ROCKPAINT_UP | BUTTON_REPEAT:
2416 inv_cursor(false);
2417 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2418 if (y<LCD_HEIGHT-TB_HEIGHT)
2420 return;
2422 inv_cursor(true);
2423 break;
2425 case ROCKPAINT_DOWN:
2426 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2427 inv_cursor(false);
2428 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2429 if (y>=LCD_HEIGHT)
2431 y = 0;
2432 return;
2434 inv_cursor(true);
2435 break;
2437 case ROCKPAINT_TOOLBAR:
2438 case ROCKPAINT_TOOLBAR2:
2439 return;
2441 if( quit ) return;
2445 static void inv_cursor(bool update)
2447 rb->lcd_set_foreground(COLOR_BLACK);
2448 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2449 /* cross painting */
2450 rb->lcd_hline(x-4,x+4,y);
2451 rb->lcd_vline(x,y-4,y+4);
2452 rb->lcd_set_foreground(rp_colors[drawcolor]);
2453 rb->lcd_set_drawmode(DRMODE_SOLID);
2455 if( update ) rb->lcd_update();
2458 static void restore_screen(void)
2460 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2461 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2462 rb->lcd_vline( img_width, 0, ROWS );
2463 rb->lcd_hline( 0, COLS, img_height );
2464 rb->lcd_drawpixel( img_width, img_height );
2465 rb->lcd_set_drawmode(DRMODE_SOLID);
2468 static void clear_drawing(void)
2470 init_buffer();
2471 img_height = ROWS;
2472 img_width = COLS;
2473 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2474 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2475 rb->lcd_update();
2478 static void goto_menu(void)
2480 int multi;
2481 int selected = 0;
2483 while( 1 )
2485 switch( rb->do_menu( &main_menu, &selected, NULL, false ) )
2487 case MAIN_MENU_NEW:
2488 clear_drawing();
2489 return;
2491 case MAIN_MENU_LOAD:
2492 if( browse( filename, MAX_PATH, "/" ) )
2494 if( load_bitmap( filename ) <= 0 )
2496 rb->splashf( 1*HZ, "Error while loading %s",
2497 filename );
2498 clear_drawing();
2500 else
2502 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2503 restore_screen();
2504 inv_cursor(true);
2505 return;
2508 break;
2510 case MAIN_MENU_SAVE:
2511 rb->lcd_set_foreground(COLOR_BLACK);
2512 if (!filename[0])
2513 rb->strcpy(filename,"/");
2514 if( !rb->kbd_input( filename, MAX_PATH ) )
2516 if( !check_extention( filename, ".bmp" ) )
2517 rb->strcat(filename, ".bmp");
2518 save_bitmap( filename );
2519 rb->splashf( 1*HZ, "File saved (%s)", filename );
2521 break;
2523 case MAIN_MENU_SET_WIDTH:
2524 rb->set_int( "Set Width", "px", UNIT_INT, &img_width,
2525 NULL, 1, 1, COLS, NULL );
2526 break;
2527 case MAIN_MENU_SET_HEIGHT:
2528 rb->set_int( "Set Height", "px", UNIT_INT, &img_height,
2529 NULL, 1, 1, ROWS, NULL );
2530 break;
2531 case MAIN_MENU_BRUSH_SIZE:
2532 for(multi = 0; multi<4; multi++)
2533 if(bsize == times_list[multi]) break;
2534 rb->set_option( "Brush Size", &multi, INT, times_options, 4, NULL );
2535 if( multi >= 0 )
2536 bsize = times_list[multi];
2537 break;
2539 case MAIN_MENU_BRUSH_SPEED:
2540 for(multi = 0; multi<3; multi++)
2541 if(bspeed == times_list[multi]) break;
2542 rb->set_option( "Brush Speed", &multi, INT, times_options, 3, NULL );
2543 if( multi >= 0 )
2544 bspeed = times_list[multi];
2545 break;
2547 case MAIN_MENU_COLOR:
2548 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2549 break;
2551 case MAIN_MENU_GRID_SIZE:
2552 for(multi = 0; multi<4; multi++)
2553 if(gridsize == gridsize_list[multi]) break;
2554 rb->set_option( "Grid Size", &multi, INT, gridsize_options, 4, NULL );
2555 if( multi >= 0 )
2556 gridsize = gridsize_list[multi];
2557 break;
2559 case MAIN_MENU_PLAYBACK_CONTROL:
2560 playback_control( NULL );
2561 break;
2563 case MAIN_MENU_EXIT:
2564 restore_screen();
2565 quit=true;
2566 return;
2568 case MAIN_MENU_RESUME:
2569 default:
2570 restore_screen();
2571 return;
2572 }/* end switch */
2573 }/* end while */
2576 static void reset_tool( void )
2578 prev_x = -1;
2579 prev_y = -1;
2580 prev_x2 = -1;
2581 prev_y2 = -1;
2582 prev_x3 = -1;
2583 prev_y3 = -1;
2584 tool_mode = -1;
2585 preview = false;
2588 static bool rockpaint_loop( void )
2590 int button=0,i,j;
2591 int accelaration;
2593 x = 10;
2594 toolbar();
2595 x = 0; y = 0;
2596 restore_screen();
2597 inv_cursor(true);
2599 while (!quit) {
2600 button = rb->button_get(true);
2602 if( tool == Brush && prev_x != -1 )
2604 accelaration = 1;
2606 else if( button & BUTTON_REPEAT )
2608 accelaration = 4;
2610 else
2612 accelaration = 1;
2615 switch(button)
2617 case ROCKPAINT_QUIT:
2618 rb->lcd_set_drawmode(DRMODE_SOLID);
2619 return PLUGIN_OK;
2621 case ROCKPAINT_MENU:
2622 inv_cursor(false);
2623 goto_menu();
2624 restore_screen();
2625 inv_cursor(true);
2626 break;
2628 case ROCKPAINT_DRAW:
2629 inv_cursor(false);
2630 switch( tool )
2632 case Brush:
2633 if( prev_x == -1 ) prev_x = 1;
2634 else prev_x = -1;
2635 break;
2637 case SelectRectangle:
2638 case Line:
2639 case Curve:
2640 case Rectangle:
2641 case RectangleFull:
2642 case Oval:
2643 case OvalFull:
2644 case LinearGradient:
2645 case RadialGradient:
2646 /* Curve uses 4 points, others use 2 */
2647 if( prev_x == -1 || prev_y == -1 )
2649 prev_x = x;
2650 prev_y = y;
2651 preview = true;
2653 else if( tool == Curve
2654 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2656 prev_x2 = x;
2657 prev_y2 = y;
2659 else if( tool == SelectRectangle
2660 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2662 tool_mode = rb->do_menu( &select_menu,
2663 NULL, NULL, false );
2664 switch( tool_mode )
2666 case SELECT_MENU_CUT:
2667 case SELECT_MENU_COPY:
2668 prev_x2 = x;
2669 prev_y2 = y;
2670 copy_to_clipboard();
2671 if( prev_x < x ) x = prev_x;
2672 if( prev_y < y ) y = prev_y;
2673 break;
2675 case SELECT_MENU_INVERT:
2676 draw_invert( prev_x, prev_y, x, y );
2677 reset_tool();
2678 break;
2680 case SELECT_MENU_HFLIP:
2681 draw_hflip( prev_x, prev_y, x, y );
2682 reset_tool();
2683 break;
2685 case SELECT_MENU_VFLIP:
2686 draw_vflip( prev_x, prev_y, x, y );
2687 reset_tool();
2688 break;
2690 case SELECT_MENU_ROTATE90:
2691 draw_rot_90_deg( prev_x, prev_y, x, y, 1 );
2692 reset_tool();
2693 break;
2695 case SELECT_MENU_ROTATE180:
2696 draw_hflip( prev_x, prev_y, x, y );
2697 draw_vflip( prev_x, prev_y, x, y );
2698 reset_tool();
2699 break;
2701 case SELECT_MENU_ROTATE270:
2702 draw_rot_90_deg( prev_x, prev_y, x, y, -1 );
2703 reset_tool();
2704 break;
2706 case SELECT_MENU_CANCEL:
2707 reset_tool();
2708 break;
2710 default:
2711 break;
2713 restore_screen();
2715 else if( tool == Curve
2716 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2718 prev_x3 = x;
2719 prev_y3 = y;
2721 else
2723 preview = false;
2724 switch( tool )
2726 case SelectRectangle:
2727 draw_paste_rectangle( prev_x, prev_y,
2728 prev_x2, prev_y2,
2729 x, y, tool_mode );
2730 break;
2731 case Line:
2732 draw_line( prev_x, prev_y, x, y );
2733 break;
2734 case Curve:
2735 draw_curve( prev_x, prev_y,
2736 prev_x2, prev_y2,
2737 prev_x3, prev_y3,
2738 x, y );
2739 break;
2740 case Rectangle:
2741 draw_rect( prev_x, prev_y, x, y );
2742 break;
2743 case RectangleFull:
2744 draw_rect_full( prev_x, prev_y, x, y );
2745 break;
2746 case Oval:
2747 draw_oval_empty( prev_x, prev_y, x, y );
2748 break;
2749 case OvalFull:
2750 draw_oval_full( prev_x, prev_y, x, y );
2751 break;
2752 case LinearGradient:
2753 linear_gradient( prev_x, prev_y, x, y );
2754 break;
2755 case RadialGradient:
2756 radial_gradient( prev_x, prev_y, x, y );
2757 break;
2758 default:
2759 break;
2761 reset_tool();
2762 restore_screen();
2764 break;
2766 case Fill:
2767 draw_fill( x, y );
2768 restore_screen();
2769 break;
2771 case ColorPicker:
2772 color_picker( x, y );
2773 break;
2775 case Text:
2776 draw_text( x, y );
2777 break;
2779 default:
2780 break;
2782 inv_cursor(true);
2783 break;
2785 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2786 if( tool == Curve )
2788 /* 3 point bezier curve */
2789 preview = false;
2790 draw_curve( prev_x, prev_y,
2791 prev_x2, prev_y2,
2792 -1, -1,
2793 x, y );
2794 reset_tool();
2795 restore_screen();
2796 inv_cursor( true );
2798 break;
2800 case ROCKPAINT_TOOLBAR:
2801 i = x; j = y;
2802 x = 10;
2803 toolbar();
2804 x = i; y = j;
2805 restore_screen();
2806 inv_cursor(true);
2807 break;
2809 case ROCKPAINT_TOOLBAR2:
2810 i = x; j = y;
2811 x = 110;
2812 toolbar();
2813 x = i; y = j;
2814 restore_screen();
2815 inv_cursor(true);
2816 break;
2818 case ROCKPAINT_LEFT:
2819 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2820 inv_cursor(false);
2821 x-=bspeed * accelaration;
2822 if (x<0) x=COLS-1;
2823 inv_cursor(true);
2824 break;
2826 case ROCKPAINT_RIGHT:
2827 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2828 inv_cursor(false);
2829 x+=bspeed * accelaration;
2830 if (x>=COLS) x=0;
2831 inv_cursor(true);
2832 break;
2834 case ROCKPAINT_UP:
2835 case ROCKPAINT_UP | BUTTON_REPEAT:
2836 inv_cursor(false);
2837 y-=bspeed * accelaration;
2838 if (y<0) y=ROWS-1;
2839 inv_cursor(true);
2840 break;
2842 case ROCKPAINT_DOWN:
2843 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2844 inv_cursor(false);
2845 y+=bspeed * accelaration;
2846 if (y>=ROWS)
2848 toolbar();
2849 restore_screen();
2851 inv_cursor(true);
2852 break;
2854 default:
2855 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2856 return PLUGIN_USB_CONNECTED;
2857 break;
2859 if( tool == Brush && prev_x == 1 )
2861 inv_cursor(false);
2862 draw_brush( x, y );
2863 inv_cursor(true);
2865 if( preview || tool == ColorPicker )
2866 /* always preview color picker */
2868 restore_screen();
2869 switch( tool )
2871 case SelectRectangle:
2872 if( prev_x2 == -1 || prev_y2 == -1 )
2874 /* we are defining the selection */
2875 draw_select_rectangle( prev_x, prev_y, x, y );
2877 else
2879 /* we are pasting the selected data */
2880 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2881 prev_y2, x, y, tool_mode );
2882 prev_x3 = prev_x2-prev_x;
2883 if( prev_x3 < 0 ) prev_x3 *= -1;
2884 prev_y3 = prev_y2-prev_y;
2885 if( prev_y3 < 0 ) prev_y3 *= -1;
2886 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2887 prev_x3 = -1;
2888 prev_y3 = -1;
2890 break;
2892 case Brush:
2893 break;
2895 case Line:
2896 draw_line( prev_x, prev_y, x, y );
2897 break;
2899 case Curve:
2900 if( prev_x2 == -1 || prev_y2 == -1 )
2902 draw_line( prev_x, prev_y, x, y );
2904 else
2906 draw_curve( prev_x, prev_y,
2907 prev_x2, prev_y2,
2908 prev_x3, prev_y3,
2909 x, y );
2911 break;
2913 case Rectangle:
2914 draw_rect( prev_x, prev_y, x, y );
2915 break;
2917 case RectangleFull:
2918 draw_rect_full( prev_x, prev_y, x, y );
2919 break;
2921 case Oval:
2922 draw_oval_empty( prev_x, prev_y, x, y );
2923 break;
2925 case OvalFull:
2926 draw_oval_full( prev_x, prev_y, x, y );
2927 break;
2929 case Fill:
2930 break;
2932 case ColorPicker:
2933 preview = true;
2934 color_picker( x, y );
2935 preview = false;
2936 break;
2938 case LinearGradient:
2939 line_gradient( prev_x, prev_y, x, y );
2940 break;
2942 case RadialGradient:
2943 line_gradient( prev_x, prev_y, x, y );
2944 break;
2946 case Text:
2947 default:
2948 break;
2950 inv_cursor( true );
2952 if( gridsize > 0 )
2954 show_grid( true );
2955 show_grid( false );
2959 return PLUGIN_OK;
2962 static int load_bitmap( const char *file )
2964 struct bitmap bm;
2965 bool ret;
2966 int i, j;
2967 fb_data color = rp_colors[ bgdrawcolor ];
2969 bm.data = (char*)save_buffer;
2970 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2971 FORMAT_NATIVE, NULL );
2973 if((bm.width > COLS ) || ( bm.height > ROWS ))
2974 return -1;
2976 img_width = bm.width;
2977 img_height = bm.height;
2978 for( i = bm.height-1; i >= 0; i-- )
2980 rb->memmove( save_buffer+i*COLS, save_buffer+i*bm.width,
2981 sizeof( fb_data )*bm.width );
2982 for( j = bm.width; j < COLS; j++ )
2983 save_buffer[j+i*COLS] = color;
2985 for( i = bm.height*COLS; i < ROWS*COLS; i++ )
2986 save_buffer[i] = color;
2988 return ret;
2991 static int save_bitmap( char *file )
2993 struct bitmap bm;
2994 int i;
2995 for(i = 0; i < img_height; i++)
2997 rb->memcpy( buffer.clipboard+i*img_width, save_buffer+i*COLS,
2998 sizeof( fb_data )*img_width );
3000 bm.data = (char*)buffer.clipboard;
3001 bm.height = img_height;
3002 bm.width = img_width;
3003 bm.format = FORMAT_NATIVE;
3004 return save_bmp_file( file, &bm );
3007 enum plugin_status plugin_start(const void* parameter)
3009 rb->lcd_set_foreground(COLOR_WHITE);
3010 rb->lcd_set_backdrop(NULL);
3011 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
3012 rb->splash( HZ/2, "Rock Paint");
3014 rb->lcd_clear_display();
3016 filename[0] = '\0';
3018 if( parameter )
3020 if( load_bitmap( parameter ) <= 0 )
3022 rb->splash( 1*HZ, "File Open Error");
3023 clear_drawing();
3025 else
3027 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
3028 restore_screen();
3029 rb->strcpy( filename, parameter );
3032 else
3034 clear_drawing();
3036 inv_cursor(true);
3038 return rockpaint_loop();