Fix FS#11007: Lua didn't parse negative numbers correct when reading from files
[kugel-rb.git] / apps / plugins / rockpaint.c
blob57a23a34bdac20305273946b3120a6cd51e91122
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 == COWON_D2_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 == PHILIPS_SA9200_PAD
159 #define ROCKPAINT_QUIT BUTTON_POWER
160 #define ROCKPAINT_DRAW BUTTON_PLAY
161 #define ROCKPAINT_MENU BUTTON_MENU
162 #define ROCKPAINT_TOOLBAR BUTTON_RIGHT
163 #define ROCKPAINT_TOOLBAR2 BUTTON_LEFT
164 #define ROCKPAINT_UP BUTTON_UP
165 #define ROCKPAINT_DOWN BUTTON_DOWN
166 #define ROCKPAINT_LEFT BUTTON_PREV
167 #define ROCKPAINT_RIGHT BUTTON_NEXT
169 #elif ( CONFIG_KEYPAD == ONDAVX747_PAD )
170 #define ROCKPAINT_QUIT BUTTON_POWER
171 #define ROCKPAINT_MENU BUTTON_MENU
173 #elif ( CONFIG_KEYPAD == ONDAVX777_PAD )
174 #define ROCKPAINT_QUIT BUTTON_POWER
176 #elif CONFIG_KEYPAD == MROBE500_PAD
177 #define ROCKPAINT_QUIT BUTTON_POWER
179 #elif ( CONFIG_KEYPAD == SAMSUNG_YH_PAD )
180 #define ROCKPAINT_QUIT BUTTON_REC
181 #define ROCKPAINT_DRAW BUTTON_PLAY
182 #define ROCKPAINT_MENU BUTTON_FFWD
183 #define ROCKPAINT_TOOLBAR BUTTON_REW
184 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
185 #define ROCKPAINT_UP BUTTON_UP
186 #define ROCKPAINT_DOWN BUTTON_DOWN
187 #define ROCKPAINT_LEFT BUTTON_LEFT
188 #define ROCKPAINT_RIGHT BUTTON_RIGHT
190 #else
191 #error "Please define keys for this keypad"
192 #endif
194 #ifdef HAVE_TOUCHSCREEN
195 #ifndef ROCKPAINT_QUIT
196 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
197 #endif
198 #ifndef ROCKPAINT_DRAW
199 #define ROCKPAINT_DRAW BUTTON_CENTER
200 #endif
201 #ifndef ROCKPAINT_MENU
202 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
203 #endif
204 #ifndef ROCKPAINT_TOOLBAR
205 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
206 #endif
207 #ifndef ROCKPAINT_TOOLBAR2
208 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
209 #endif
210 #ifndef ROCKPAINT_UP
211 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
212 #endif
213 #ifndef ROCKPAINT_DOWN
214 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
215 #endif
216 #ifndef ROCKPAINT_LEFT
217 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
218 #endif
219 #ifndef ROCKPAINT_RIGHT
220 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
221 #endif
222 #endif
224 /***********************************************************************
225 * Palette Default Colors
226 ***********************************************************************/
227 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
228 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
229 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
230 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
231 #define COLOR_RED LCD_RGBPACK(128,0,0)
232 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
233 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
234 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
235 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
236 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
237 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
238 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
239 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
240 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
241 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
242 #define COLOR_PINK LCD_RGBPACK(255,0,255)
243 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
244 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
246 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
247 #define ROCKPAINT_TITLE_FONT 2
249 /***********************************************************************
250 * Program Colors
251 ***********************************************************************/
252 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
253 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
255 #define ROWS LCD_HEIGHT
256 #define COLS LCD_WIDTH
259 * Toolbar positioning stuff ... don't read this unless you really need to
261 * TB Toolbar
262 * SP Separator
263 * SC Selected Color
264 * PL Palette
265 * TL Tools
268 /* Separator sizes */
269 #define TB_SP_MARGIN 3
270 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
272 /* Selected color sizes */
273 #define TB_SC_SIZE 12
275 /* Palette sizes */
276 #define TB_PL_COLOR_SIZE 7
277 #define TB_PL_COLOR_SPACING 2
278 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
279 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
281 /* Tools sizes */
282 #define TB_TL_SIZE 8
283 #define TB_TL_SPACING 2
284 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
285 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
287 /* Menu button size ... gruik */
288 #define TB_MENU_MIN_WIDTH 30
290 /* Selected colors position */
291 #define TB_SC_FG_TOP 2
292 #define TB_SC_FG_LEFT 2
293 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
294 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
296 /* Palette position */
297 #define TB_PL_TOP TB_SC_FG_TOP
298 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
300 /* Tools position */
301 #define TB_TL_TOP TB_SC_FG_TOP
302 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
304 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
305 #undef TB_TL_TOP
306 #undef TB_TL_LEFT
307 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
308 #define TB_TL_LEFT TB_SC_FG_LEFT
309 #endif
311 /* Menu button position */
312 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
313 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
315 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
318 static void draw_pixel(int x,int y);
319 static void draw_line( int x1, int y1, int x2, int y2 );
320 static void draw_rect( int x1, int y1, int x2, int y2 );
321 static void draw_toolbars(bool update);
322 static void inv_cursor(bool update);
323 static void restore_screen(void);
324 static void clear_drawing(void);
325 static void goto_menu(void);
326 static int load_bitmap( const char *filename );
327 static int save_bitmap( char *filename );
328 static void draw_rect_full( int x1, int y1, int x2, int y2 );
330 /***********************************************************************
331 * Global variables
332 ***********************************************************************/
334 static int drawcolor=0; /* Current color (in palette) */
335 static int bgdrawcolor=9; /* Current background color (in palette) */
336 static int img_height = ROWS;
337 static int img_width = COLS;
338 bool isbg = false; /* gruik ugly hack alert */
340 static int preview=false; /* Is preview mode on ? */
342 /* TODO: clean this up */
343 static int x=0, y=0; /* cursor position */
344 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
345 static int prev_x2=-1, prev_y2=-1;
346 static int prev_x3=-1, prev_y3=-1;
347 static int tool_mode=-1;
350 static int bsize=1; /* brush size */
351 static int bspeed=1; /* brush speed */
353 enum Tools { Brush = 0, /* Regular brush */
354 Fill = 1, /* Fill a shape with current color */
355 SelectRectangle = 2,
356 ColorPicker = 3, /* Pick a color */
357 Line = 4, /* Draw a line between two points */
358 Unused = 5, /* THIS IS UNUSED ... */
359 Curve = 6,
360 Text = 7,
361 Rectangle = 8, /* Draw a rectangle */
362 RectangleFull = 9,
363 Oval = 10, /* Draw an oval */
364 OvalFull = 11,
365 LinearGradient = 12,
366 RadialGradient = 13
369 enum Tools tool = Brush;
371 static bool quit=false;
372 static int gridsize=0;
374 static fb_data rp_colors[18] =
376 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
377 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
378 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
379 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
380 COLOR_LIGHTBROWN
383 static fb_data save_buffer[ ROWS*COLS ];
385 extern fb_data rockpaint[];
386 extern fb_data rockpaint_hsvrgb[];
388 /* Maximum string size allowed for the text tool */
389 #define MAX_TEXT 256
391 static union
393 /* Used by fill and gradient algorithms */
394 struct
396 short x;
397 short y;
398 } coord[ ROWS*COLS ];
400 /* Used by bezier curve algorithms */
401 struct
403 short x1, y1;
404 short x2, y2;
405 short x3, y3;
406 short x4, y4;
407 short depth;
408 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
409 * than coord ... so we divide to take
410 * less memory. */
412 /* Used to cut/copy/paste data */
413 fb_data clipboard[ ROWS*COLS ];
415 /* Used for text mode */
416 struct
418 char text[MAX_TEXT];
419 char font[MAX_PATH];
420 char old_font[MAX_PATH];
421 int fh_buf[30];
422 int fw_buf[30];
423 char fontname_buf[30][MAX_PATH];
424 } text;
425 } buffer;
427 /* Current filename */
428 static char filename[MAX_PATH];
430 /* Font preview buffer */
431 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
432 //#define FONT_PREVIEW_HEIGHT 1000
433 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
435 /***********************************************************************
436 * Offscreen buffer/Text/Fonts handling
438 * Parts of code taken from firmware/drivers/lcd-16bit.c
439 ***********************************************************************/
440 static void buffer_mono_bitmap_part(
441 fb_data *buf, int buf_width, int buf_height,
442 const unsigned char *src, int src_x, int src_y,
443 int stride, int x, int y, int width, int height )
444 /* this function only draws the foreground part of the bitmap */
446 const unsigned char *src_end;
447 fb_data *dst, *dst_end;
448 unsigned fgcolor = rb->lcd_get_foreground();
450 /* nothing to draw? */
451 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
452 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
453 return;
455 /* clipping */
456 if( x < 0 )
458 width += x;
459 src_x -= x;
460 x = 0;
462 if( y < 0 )
464 height += y;
465 src_y -= y;
466 y = 0;
468 if( x + width > buf_width )
469 width = buf_width - x;
470 if( y + height > buf_height )
471 height = buf_height - y;
473 src += stride * (src_y >> 3) + src_x; /* move starting point */
474 src_y &= 7;
475 src_end = src + width;
477 dst = buf + y*buf_width + x;
481 const unsigned char *src_col = src++;
482 unsigned data = *src_col >> src_y;
483 fb_data *dst_col = dst++;
484 int numbits = 8 - src_y;
486 dst_end = dst_col + height * buf_width;
489 if( data & 0x01 )
490 *dst_col = fgcolor; /* FIXME ? */
492 dst_col += buf_width;
494 data >>= 1;
495 if( --numbits == 0 )
497 src_col += stride;
498 data = *src_col;
499 numbits = 8;
501 } while( dst_col < dst_end );
502 } while( src < src_end );
505 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
506 int x, int y, int ofs, const unsigned char *str )
508 unsigned short ch;
509 unsigned short *ucs;
511 struct font *pf = rb->font_get( FONT_UI );
512 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
514 ucs = rb->bidi_l2v( str, 1 );
516 while( (ch = *ucs++) != 0 && x < buf_width )
518 int width;
519 const unsigned char *bits;
521 /* get proportional width and glyph bits */
522 width = rb->font_get_width( pf, ch );
524 if( ofs > width )
526 ofs -= width;
527 continue;
530 bits = rb->font_get_bits( pf, ch );
532 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0,
533 width, x, y, width - ofs, pf->height);
535 x += width - ofs;
536 ofs = 0;
540 /***********************************************************************
541 * Menu handling
542 ***********************************************************************/
543 enum {
544 /* Main menu */
545 MAIN_MENU_RESUME,
546 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
547 MAIN_MENU_SET_WIDTH, MAIN_MENU_SET_HEIGHT,
548 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
549 MAIN_MENU_GRID_SIZE,
550 MAIN_MENU_PLAYBACK_CONTROL,
551 MAIN_MENU_EXIT,
553 enum {
554 /* Select action menu */
555 SELECT_MENU_CUT, SELECT_MENU_COPY,
556 SELECT_MENU_INVERT, SELECT_MENU_HFLIP, SELECT_MENU_VFLIP,
557 SELECT_MENU_ROTATE90, SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
558 SELECT_MENU_CANCEL,
560 enum {
561 /* Text menu */
562 TEXT_MENU_TEXT, TEXT_MENU_FONT,
563 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
566 MENUITEM_STRINGLIST(main_menu, "RockPaint", NULL,
567 "Resume", "New", "Load", "Save",
568 "Set Width", "Set Height",
569 "Brush Size", "Brush Speed",
570 "Choose Color", "Grid Size",
571 "Playback Control", "Exit");
572 MENUITEM_STRINGLIST(select_menu, "Select...", NULL,
573 "Cut", "Copy",
574 "Invert", "Horizontal Flip", "Vertical Flip",
575 "Rotate 90°", "Rotate 180°", "Rotate 270°",
576 "Cancel");
577 MENUITEM_STRINGLIST(text_menu, "Text", NULL,
578 "Set Text", "Change Font",
579 "Preview", "Apply", "Cancel");
580 static const int times_list[] = { 1, 2, 4, 8 };
581 static const int gridsize_list[] = { 0, 5, 10, 20 };
582 static const struct opt_items times_options[] = {
583 { "1x", -1 }, { "2x", -1 }, { "4x", -1 }, { "8x", -1 }
585 static const struct opt_items gridsize_options[] = {
586 { "No grid", -1 }, { "5px", -1 }, { "10px", -1 }, { "20px", -1 }
589 static int draw_window( int height, int width,
590 int *top, int *left,
591 const char *title )
593 int fh;
594 rb->lcd_getstringsize( title, NULL, &fh );
595 fh++;
597 const int _top = ( LCD_HEIGHT - height ) / 2;
598 const int _left = ( LCD_WIDTH - width ) / 2;
599 if( top ) *top = _top;
600 if( left ) *left = _left;
601 rb->lcd_set_background(COLOR_BLUE);
602 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
603 rb->lcd_fillrect( _left, _top, width, height );
604 rb->lcd_set_foreground(COLOR_BLUE);
605 rb->lcd_fillrect( _left, _top, width, fh+4 );
606 rb->lcd_set_foreground(COLOR_WHITE);
607 rb->lcd_putsxy( _left+2, _top+2, title );
608 rb->lcd_set_foreground(COLOR_BLACK);
609 rb->lcd_drawrect( _left, _top, width, height );
610 return _top+fh+4;
613 /***********************************************************************
614 * File browser
615 ***********************************************************************/
617 char bbuf[MAX_PATH]; /* used by file and font browsers */
618 char bbuf_s[MAX_PATH]; /* used by file and font browsers */
619 struct tree_context *tree = NULL;
621 static bool check_extention(const char *filename, const char *ext)
623 const char *p = rb->strrchr( filename, '.' );
624 return ( p != NULL && !rb->strcasecmp( p, ext ) );
627 static const char* browse_get_name_cb(int selected_item, void *data,
628 char *buffer, size_t buffer_len)
630 int *indexes = (int *) data;
631 struct entry* dc = tree->dircache;
632 struct entry* e = &dc[indexes[selected_item]];
633 (void) buffer;
634 (void) buffer_len;
636 return e->name;
639 static bool browse( char *dst, int dst_size, const char *start )
641 struct gui_synclist browse_list;
642 int item_count = 0, selected, button;
643 struct tree_context backup;
644 struct entry *dc;
645 bool reload = true;
646 int dirfilter = SHOW_ALL;
647 int *indexes = (int *) buffer.clipboard;
649 char *a;
651 rb->strcpy( bbuf, start );
652 a = bbuf+rb->strlen(bbuf)-1;
653 if( *a != '/' )
655 a[1] = '/';
656 a[2] = '\0';
658 bbuf_s[0] = '\0';
660 rb->gui_synclist_init(&browse_list, browse_get_name_cb,
661 (void*) indexes, false, 1, NULL);
663 tree = rb->tree_get_context();
664 backup = *tree;
665 dc = tree->dircache;
666 a = backup.currdir+rb->strlen(backup.currdir)-1;
667 if( *a != '/' )
669 *++a = '/';
670 *++a = '\0';
672 rb->strcpy( a, dc[tree->selected_item].name );
673 tree->dirfilter = &dirfilter;
674 while( 1 )
676 if( reload )
678 int i;
679 rb->set_current_file(bbuf);
680 item_count = 0;
681 selected = 0;
682 for( i = 0; i < tree->filesindir ; i++)
684 /* only displayes directories and .bmp files */
685 if( ((dc[i].attr & ATTR_DIRECTORY ) &&
686 rb->strcmp( dc[i].name, "." ) &&
687 rb->strcmp( dc[i].name, ".." )) ||
688 ( !(dc[i].attr & ATTR_DIRECTORY) &&
689 check_extention( dc[i].name, ".bmp" ) ) )
691 if( !rb->strcmp( dc[i].name, bbuf_s ) )
692 selected = item_count;
693 indexes[item_count++] = i;
697 rb->gui_synclist_set_nb_items(&browse_list,item_count);
698 rb->gui_synclist_select_item(&browse_list, selected);
699 rb->gui_synclist_set_title(&browse_list, bbuf, NOICON);
700 rb->gui_synclist_draw(&browse_list);
701 reload = false;
703 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
704 if (rb->gui_synclist_do_button(&browse_list,&button,LIST_WRAP_UNLESS_HELD))
705 continue;
706 switch( button )
708 case ACTION_STD_CANCEL:
709 if( !rb->strcmp( bbuf, "/" ) )
711 *tree = backup;
712 rb->set_current_file( backup.currdir );
713 return false;
715 rb->strcpy( bbuf_s, ".." );
716 case ACTION_STD_OK:
717 if( button == ACTION_STD_OK )
719 selected = rb->gui_synclist_get_sel_pos( &browse_list );
720 if( selected < 0 || selected >= item_count )
721 break;
722 struct entry* e = &dc[indexes[selected]];
723 rb->strlcpy( bbuf_s, e->name, sizeof( bbuf_s ) );
724 if( !( e->attr & ATTR_DIRECTORY ) )
726 *tree = backup;
727 rb->set_current_file( backup.currdir );
728 rb->snprintf( dst, dst_size, "%s%s", bbuf, bbuf_s );
729 return true;
732 if( !rb->strcmp( bbuf_s, "." ) ) break;
733 a = bbuf+rb->strlen(bbuf);
734 if( !rb->strcmp( bbuf_s, ".." ) )
736 a--;
737 if( a == bbuf ) break;
738 if( *a == '/' ) a--;
739 while( *a != '/' ) a--;
740 rb->strcpy( bbuf_s, ++a );
741 /* select parent directory */
742 bbuf_s[rb->strlen(bbuf_s)-1] = '\0';
743 *a = '\0';
744 reload = true;
745 break;
747 rb->snprintf( a, bbuf+sizeof(bbuf)-a, "%s/", bbuf_s );
748 reload = true;
749 break;
751 case ACTION_STD_MENU:
752 *tree = backup;
753 rb->set_current_file( backup.currdir );
754 return false;
759 /***********************************************************************
760 * Font browser
762 * FIXME: This still needs some work ... it currently only works fine
763 * on the simulators, disk spins too much on real targets -> rendered
764 * font buffer needed.
765 ***********************************************************************/
766 static bool browse_fonts( char *dst, int dst_size )
768 #define WIDTH ( LCD_WIDTH - 20 )
769 #define HEIGHT ( LCD_HEIGHT - 20 )
770 #define LINE_SPACE 2
771 int top, top_inside = 0, left;
773 DIR *d;
774 struct dirent *de;
775 int fvi = 0; /* first visible item */
776 int lvi = 0; /* last visible item */
777 int si = 0; /* selected item */
778 int osi = 0; /* old selected item */
779 int li = 0; /* last item */
780 int nvih = 0; /* next visible item height */
781 int i;
782 int b_need_redraw = 1; /* Do we need to redraw ? */
784 int cp = 0; /* current position */
785 int fh; /* font height */
787 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
788 #define fw_buf buffer.text.fw_buf
789 int fw;
790 #define fontname_buf buffer.text.fontname_buf
792 rb->snprintf( buffer.text.old_font, MAX_PATH,
793 FONT_DIR "/%s.fnt",
794 rb->global_settings->font_file );
796 while( 1 )
798 if( !b_need_redraw )
800 /* we don't need to redraw ... but we need to unselect
801 * the previously selected item */
802 cp = top_inside + LINE_SPACE;
803 for( i = 0; i+fvi < osi; i++ )
805 cp += fh_buf[i] + LINE_SPACE;
807 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
808 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
809 rb->lcd_set_drawmode(DRMODE_SOLID);
812 if( b_need_redraw )
814 b_need_redraw = 0;
816 d = rb->opendir( FONT_DIR "/" );
817 if( !d )
819 return false;
821 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
822 i = 0;
823 li = -1;
824 while( i < fvi )
826 rb->readdir( d );
827 i++;
829 cp = top_inside+LINE_SPACE;
831 rb->lcd_set_foreground(COLOR_BLACK);
832 rb->lcd_set_background(COLOR_LIGHTGRAY);
834 while( cp < top+HEIGHT )
836 de = rb->readdir( d );
837 if( !de )
839 li = i-1;
840 break;
842 if( !check_extention( de->d_name, ".fnt" ) )
843 continue;
844 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
845 de->d_name );
846 rb->font_load( bbuf );
847 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
848 if( nvih > 0 )
850 nvih -= fh;
851 fvi++;
852 if( nvih < 0 ) nvih = 0;
853 i++;
854 continue;
856 if( cp + fh >= top+HEIGHT )
858 nvih = fh;
859 break;
861 rb->lcd_putsxy( left+10, cp, de->d_name );
862 fh_buf[i-fvi] = fh;
863 fw_buf[i-fvi] = fw;
864 cp += fh + LINE_SPACE;
865 rb->strcpy( fontname_buf[i-fvi], bbuf );
866 i++;
868 lvi = i-1;
869 if( li == -1 )
871 if( !(de = rb->readdir( d ) ) )
873 li = lvi;
875 else if( !nvih && check_extention( de->d_name, ".fnt" ) )
877 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
878 de->d_name );
879 rb->font_load( bbuf );
880 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
881 nvih = fh;
884 rb->font_load( buffer.text.old_font );
885 rb->closedir( d );
888 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
889 cp = top_inside + LINE_SPACE;
890 for( i = 0; i+fvi < si; i++ )
892 cp += fh_buf[i] + LINE_SPACE;
894 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
895 rb->lcd_set_drawmode(DRMODE_SOLID);
897 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
899 osi = si;
900 i = fvi;
901 switch( rb->button_get(true) )
903 case ROCKPAINT_UP:
904 case ROCKPAINT_UP|BUTTON_REPEAT:
905 if( si > 0 )
907 si--;
908 if( si<fvi )
910 fvi = si;
913 break;
915 case ROCKPAINT_DOWN:
916 case ROCKPAINT_DOWN|BUTTON_REPEAT:
917 if( li == -1 || si < li )
919 si++;
921 break;
923 case ROCKPAINT_LEFT:
924 return false;
926 case ROCKPAINT_RIGHT:
927 case ROCKPAINT_DRAW:
928 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
929 return true;
932 if( i != fvi || si > lvi )
934 b_need_redraw = 1;
937 if( si<=lvi )
939 nvih = 0;
942 #undef fh_buf
943 #undef fw_buf
944 #undef fontname_buf
945 #undef WIDTH
946 #undef HEIGHT
947 #undef LINE_SPACE
950 /***********************************************************************
951 * HSVRGB Color chooser
952 ***********************************************************************/
953 static unsigned int color_chooser( unsigned int color )
955 int red = RGB_UNPACK_RED( color );
956 int green = RGB_UNPACK_GREEN( color );
957 int blue = RGB_UNPACK_BLUE( color );
958 int hue, saturation, value;
959 int r, g, b; /* temp variables */
960 int i, top, left;
962 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
963 Red = 3, Green = 4, Blue = 5 };
964 enum BaseColor current = Red;
965 bool has_changed;
967 char str[6] = "";
969 restore_screen();
971 rgb2hsv( red, green, blue, &hue, &saturation, &value );
973 while( 1 )
975 has_changed = false;
976 color = LCD_RGBPACK( red, green, blue );
978 #define HEIGHT ( 100 )
979 #define WIDTH ( 150 )
981 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
982 top -= 15;
984 for( i=0; i<100; i++ )
986 hsv2rgb( i*36, saturation, value, &r, &g, &b );
987 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
988 rb->lcd_vline( left+15+i, top+20, top+27 );
989 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
990 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
991 rb->lcd_vline( left+15+i, top+30, top+37 );
992 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
993 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
994 rb->lcd_vline( left+15+i, top+40, top+47 );
995 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
996 rb->lcd_vline( left+15+i, top+50, top+57 );
997 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
998 rb->lcd_vline( left+15+i, top+60, top+67 );
999 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
1000 rb->lcd_vline( left+15+i, top+70, top+77 );
1003 rb->lcd_set_foreground(COLOR_BLACK);
1004 #define POSITION( a, i ) \
1005 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
1006 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
1007 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
1008 rb->lcd_drawpixel( left+16+i, top + 28 + a );
1009 POSITION( 0, hue/36 );
1010 POSITION( 10, saturation*99/255 );
1011 POSITION( 20, value*99/255 );
1012 POSITION( 30, red*99/255 );
1013 POSITION( 40, green*99/255 );
1014 POSITION( 50, blue*99/255 );
1015 #undef POSITION
1016 rb->lcd_set_background(COLOR_LIGHTGRAY);
1017 rb->lcd_setfont( FONT_SYSFIXED );
1018 rb->snprintf( str, 6, "%d", hue/10 );
1019 rb->lcd_putsxy( left + 117, top + 20, str );
1020 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1021 rb->lcd_putsxy( left + 117, top + 30, str );
1022 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1023 rb->lcd_putsxy( left + 117, top + 40, str );
1024 rb->snprintf( str, 6, "%d", red );
1025 rb->lcd_putsxy( left + 117, top + 50, str );
1026 rb->snprintf( str, 6, "%d", green );
1027 rb->lcd_putsxy( left + 117, top + 60, str );
1028 rb->snprintf( str, 6, "%d", blue );
1029 rb->lcd_putsxy( left + 117, top + 70, str );
1030 rb->lcd_setfont( FONT_UI );
1032 #define CURSOR( l ) \
1033 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1034 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1035 CURSOR( 5 );
1036 #undef CURSOR
1038 rb->lcd_set_foreground( color );
1039 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1041 rb->lcd_update();
1043 switch( rb->button_get(true) )
1045 case ROCKPAINT_UP:
1046 current = ( current + 5 )%6;
1047 break;
1049 case ROCKPAINT_DOWN:
1050 current = (current + 1 )%6;
1051 break;
1053 case ROCKPAINT_LEFT:
1054 has_changed = true;
1055 switch( current )
1057 case Hue:
1058 hue = ( hue + 3600 - 10 )%3600;
1059 break;
1060 case Saturation:
1061 if( saturation ) saturation--;
1062 break;
1063 case Value:
1064 if( value ) value--;
1065 break;
1066 case Red:
1067 if( red ) red--;
1068 break;
1069 case Green:
1070 if( green ) green--;
1071 break;
1072 case Blue:
1073 if( blue ) blue--;
1074 break;
1076 break;
1078 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1079 has_changed = true;
1080 switch( current )
1082 case Hue:
1083 hue = ( hue + 3600 - 100 )%3600;
1084 break;
1085 case Saturation:
1086 if( saturation >= 8 ) saturation-=8;
1087 else saturation = 0;
1088 break;
1089 case Value:
1090 if( value >= 8 ) value-=8;
1091 else value = 0;
1092 break;
1093 case Red:
1094 if( red >= 8 ) red-=8;
1095 else red = 0;
1096 break;
1097 case Green:
1098 if( green >= 8 ) green-=8;
1099 else green = 0;
1100 break;
1101 case Blue:
1102 if( blue >= 8 ) blue-=8;
1103 else blue = 0;
1104 break;
1106 break;
1108 case ROCKPAINT_RIGHT:
1109 has_changed = true;
1110 switch( current )
1112 case Hue:
1113 hue = ( hue + 10 )%3600;
1114 break;
1115 case Saturation:
1116 if( saturation < 0xff ) saturation++;
1117 break;
1118 case Value:
1119 if( value < 0xff ) value++;
1120 break;
1121 case Red:
1122 if( red < 0xff ) red++;
1123 break;
1124 case Green:
1125 if( green < 0xff ) green++;
1126 break;
1127 case Blue:
1128 if( blue < 0xff ) blue++;
1129 break;
1131 break;
1133 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1134 has_changed = true;
1135 switch( current )
1137 case Hue:
1138 hue = ( hue + 100 )%3600;
1139 break;
1140 case Saturation:
1141 if( saturation < 0xff - 8 ) saturation+=8;
1142 else saturation = 0xff;
1143 break;
1144 case Value:
1145 if( value < 0xff - 8 ) value+=8;
1146 else value = 0xff;
1147 break;
1148 case Red:
1149 if( red < 0xff - 8 ) red+=8;
1150 else red = 0xff;
1151 break;
1152 case Green:
1153 if( green < 0xff - 8 ) green+=8;
1154 else green = 0xff;
1155 break;
1156 case Blue:
1157 if( blue < 0xff - 8 ) blue+=8;
1158 else blue = 0xff;
1159 break;
1161 break;
1163 case ROCKPAINT_DRAW:
1164 return color;
1166 if( has_changed )
1168 switch( current )
1170 case Hue:
1171 case Saturation:
1172 case Value:
1173 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1174 break;
1176 case Red:
1177 case Green:
1178 case Blue:
1179 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1180 break;
1183 #undef HEIGHT
1184 #undef WIDTH
1188 /***********************************************************************
1189 * Misc routines
1190 ***********************************************************************/
1191 static void init_buffer(void)
1193 int i;
1194 fb_data color = rp_colors[ bgdrawcolor ];
1195 for( i = 0; i < ROWS*COLS; i++ )
1197 save_buffer[i] = color;
1201 static void draw_pixel(int x,int y)
1203 if( !preview )
1205 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1206 if( isbg )
1208 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1210 else
1212 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1215 rb->lcd_drawpixel(x,y);
1218 static void color_picker( int x, int y )
1220 if( preview )
1222 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1223 #define PSIZE 12
1224 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1225 if( x >= COLS - PSIZE ) x -= PSIZE + 2;
1226 if( y >= ROWS - PSIZE ) y -= PSIZE + 2;
1227 rb->lcd_drawrect( x + 2, y + 2, PSIZE - 2, PSIZE - 2 );
1228 rb->lcd_set_drawmode(DRMODE_SOLID);
1229 rb->lcd_drawrect( x + 3, y + 3, PSIZE - 4, PSIZE - 4 );
1230 #undef PSIZE
1231 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1233 else
1235 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1239 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1240 /* This is a preview mode only function */
1242 int i,a;
1243 if( x1 > x2 )
1245 i = x1;
1246 x1 = x2;
1247 x2 = i;
1249 if( y1 > y2 )
1251 i = y1;
1252 y1 = y2;
1253 y2 = i;
1255 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1256 i = 0;
1257 for( a = x1; a < x2; a++, i++ )
1258 if( i%2 )
1259 rb->lcd_drawpixel( a, y1 );
1260 for( a = y1; a < y2; a++, i++ )
1261 if( i%2 )
1262 rb->lcd_drawpixel( x2, a );
1263 if( y2 != y1 )
1264 for( a = x2; a > x1; a--, i++ )
1265 if( i%2 )
1266 rb->lcd_drawpixel( a, y2 );
1267 if( x2 != x1 )
1268 for( a = y2; a > y1; a--, i++ )
1269 if( i%2 )
1270 rb->lcd_drawpixel( x1, a );
1271 rb->lcd_set_drawmode(DRMODE_SOLID);
1274 static void copy_to_clipboard( void )
1276 /* This needs to be optimised ... but i'm lazy ATM */
1277 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1280 /* no preview mode handling atm ... do we need it ? (one if) */
1281 static void draw_invert( int x1, int y1, int x2, int y2 )
1283 int i;
1284 if( x1 > x2 )
1286 i = x1;
1287 x1 = x2;
1288 x2 = i;
1290 if( y1 > y2 )
1292 i = y1;
1293 y1 = y2;
1294 y2 = i;
1297 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1298 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1299 rb->lcd_set_drawmode(DRMODE_SOLID);
1301 for( ; y1<=y2; y1++ )
1303 for( i = x1; i<=x2; i++ )
1305 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1308 /*if( update )*/ rb->lcd_update();
1311 static void draw_hflip( int x1, int y1, int x2, int y2 )
1313 int i;
1314 if( x1 > x2 )
1316 i = x1;
1317 x1 = x2;
1318 x2 = i;
1320 if( y1 > y2 )
1322 i = y1;
1323 y1 = y2;
1324 y2 = i;
1327 copy_to_clipboard();
1329 for( i = 0; i <= y2 - y1; i++ )
1331 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1332 buffer.clipboard+(y2-i)*COLS+x1,
1333 (x2-x1+1)*sizeof( fb_data ) );
1335 restore_screen();
1336 rb->lcd_update();
1339 static void draw_vflip( int x1, int y1, int x2, int y2 )
1341 int i;
1342 if( x1 > x2 )
1344 i = x1;
1345 x1 = x2;
1346 x2 = i;
1348 if( y1 > y2 )
1350 i = y1;
1351 y1 = y2;
1352 y2 = i;
1355 copy_to_clipboard();
1357 for( ; y1 <= y2; y1++ )
1359 for( i = 0; i <= x2 - x1; i++ )
1361 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1364 restore_screen();
1365 rb->lcd_update();
1368 /* direction: -1 = left, 1 = right */
1369 static void draw_rot_90_deg( int x1, int y1, int x2, int y2, int direction )
1371 int i, j;
1372 if( x1 > x2 )
1374 i = x1;
1375 x1 = x2;
1376 x2 = i;
1378 if( y1 > y2 )
1380 i = y1;
1381 y1 = y2;
1382 y2 = i;
1385 copy_to_clipboard();
1387 fb_data color = rp_colors[ bgdrawcolor ];
1388 const int width = x2 - x1, height = y2 - y1;
1389 const int sub_half = width/2-height/2, add_half = (width+height)/2;
1390 if( width > height )
1392 for( i = 0; i <= height; i++ )
1394 for( j = 0; j < sub_half; j++ )
1395 save_buffer[(y1+i)*COLS+x1+j] = color;
1396 for( j = add_half+1; j <= width; j++ )
1397 save_buffer[(y1+i)*COLS+x1+j] = color;
1400 else if( width < height )
1402 for( j = 0; j <= width; j++ )
1404 for( i = 0; i < -sub_half; i++ )
1405 save_buffer[(y1+i)*COLS+x1+j] = color;
1406 for( i = add_half+1; i <= height; i++ )
1407 save_buffer[(y1+i)*COLS+x1+j] = color;
1410 int x3 = x1 + sub_half, y3 = y1 - sub_half;
1411 int is = x3<0?-x3:0, ie = COLS-x3-1, js = y3<0?-y3:0, je = ROWS-y3-1;
1412 if( ie > height ) ie = height;
1413 if( je > width ) je = width;
1414 for( i = is; i <= ie; i++ )
1416 for( j = js; j <= je; j++ )
1418 int x, y;
1419 if(direction > 0)
1421 x = x1+j;
1422 y = y1+height-i;
1424 else
1426 x = x1+width-j;
1427 y = y1+i;
1429 save_buffer[(y3+j)*COLS+x3+i] = buffer.clipboard[y*COLS+x];
1432 restore_screen();
1433 rb->lcd_update();
1436 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1437 int src_y2, int x1, int y1, int mode )
1439 int i, width, height;
1440 if( mode == SELECT_MENU_CUT )
1442 i = drawcolor;
1443 drawcolor = bgdrawcolor;
1444 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1445 drawcolor = i;
1447 if( src_x1 > src_x2 )
1449 i = src_x1;
1450 src_x1 = src_x2;
1451 src_x2 = i;
1453 if( src_y1 > src_y2 )
1455 i = src_y1;
1456 src_y1 = src_y2;
1457 src_y2 = i;
1459 width = src_x2 - src_x1 + 1;
1460 height = src_y2 - src_y1 + 1;
1461 /* clipping */
1462 if( x1 + width > COLS )
1463 width = COLS - x1;
1464 if( y1 + height > ROWS )
1465 height = ROWS - y1;
1467 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1468 x1, y1, width, height );
1469 if( !preview )
1471 for( i = 0; i < height; i++ )
1473 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1474 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1475 width*sizeof( fb_data ) );
1480 static void show_grid( bool update )
1482 int i;
1483 if( gridsize > 0 )
1485 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1486 for( i = gridsize; i < img_width; i+= gridsize )
1488 rb->lcd_vline( i, 0, img_height-1 );
1490 for( i = gridsize; i < img_height; i+= gridsize )
1492 rb->lcd_hline( 0, img_width-1, i );
1494 rb->lcd_set_drawmode(DRMODE_SOLID);
1495 if( update ) rb->lcd_update();
1499 static void draw_text( int x, int y )
1501 int selected = 0;
1502 buffer.text.text[0] = '\0';
1503 rb->snprintf( buffer.text.old_font, MAX_PATH,
1504 FONT_DIR "/%s.fnt",
1505 rb->global_settings->font_file );
1506 while( 1 )
1508 switch( rb->do_menu( &text_menu, &selected, NULL, NULL ) )
1510 case TEXT_MENU_TEXT:
1511 rb->lcd_set_foreground(COLOR_BLACK);
1512 rb->kbd_input( buffer.text.text, MAX_TEXT );
1513 break;
1515 case TEXT_MENU_FONT:
1516 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1518 rb->font_load( buffer.text.font );
1520 break;
1522 case TEXT_MENU_PREVIEW:
1523 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1524 while( 1 )
1526 int button;
1527 restore_screen();
1528 rb->lcd_putsxy( x, y, buffer.text.text );
1529 rb->lcd_update();
1530 switch( button = rb->button_get( true ) )
1532 case ROCKPAINT_LEFT:
1533 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1534 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1535 if (x<0) x=COLS-1;
1536 break;
1538 case ROCKPAINT_RIGHT:
1539 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1540 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1541 if (x>=COLS) x=0;
1542 break;
1544 case ROCKPAINT_UP:
1545 case ROCKPAINT_UP | BUTTON_REPEAT:
1546 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1547 if (y<0) y=ROWS-1;
1548 break;
1550 case ROCKPAINT_DOWN:
1551 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1552 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1553 if (y>=ROWS-1) y=0;
1554 break;
1556 case ROCKPAINT_DRAW:
1557 break;
1558 default:
1559 if(rb->default_event_handler(button)
1560 == SYS_USB_CONNECTED)
1561 button = ROCKPAINT_DRAW;
1562 break;
1564 if( button == ROCKPAINT_DRAW ) break;
1566 break;
1568 case TEXT_MENU_APPLY:
1569 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1570 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1571 buffer.text.text );
1572 case TEXT_MENU_CANCEL:
1573 default:
1574 restore_screen();
1575 rb->font_load( buffer.text.old_font );
1576 return;
1581 static void draw_brush( int x, int y )
1583 int i,j;
1584 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1586 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1588 draw_pixel( x+i, y+j );
1593 /* This is an implementation of Bresenham's line algorithm.
1594 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1596 static void draw_line( int x1, int y1, int x2, int y2 )
1598 int x = x1;
1599 int y = y1;
1600 int deltax = x2 - x1;
1601 int deltay = y2 - y1;
1602 int i;
1604 int xerr = abs(deltax);
1605 int yerr = abs(deltay);
1606 int xstep = deltax > 0 ? 1 : -1;
1607 int ystep = deltay > 0 ? 1 : -1;
1608 int err;
1610 if (yerr > xerr)
1612 /* more vertical */
1613 err = yerr;
1614 xerr <<= 1;
1615 yerr <<= 1;
1617 /* to leave off the last pixel of the line, leave off the "+ 1" */
1618 for (i = abs(deltay) + 1; i; --i)
1620 draw_pixel(x, y);
1621 y += ystep;
1622 err -= xerr;
1623 if (err < 0) {
1624 x += xstep;
1625 err += yerr;
1629 else
1631 /* more horizontal */
1632 err = xerr;
1633 xerr <<= 1;
1634 yerr <<= 1;
1636 for (i = abs(deltax) + 1; i; --i)
1638 draw_pixel(x, y);
1639 x += xstep;
1640 err -= yerr;
1641 if (err < 0) {
1642 y += ystep;
1643 err += xerr;
1649 static void draw_curve( int x1, int y1, int x2, int y2,
1650 int xa, int ya, int xb, int yb )
1652 int i = 0;
1653 short xl1, yl1;
1654 short xl2, yl2;
1655 short xl3, yl3;
1656 short xl4, yl4;
1657 short xr1, yr1;
1658 short xr2, yr2;
1659 short xr3, yr3;
1660 short xr4, yr4;
1661 short depth;
1662 short xh, yh;
1664 if( x1 == x2 && y1 == y2 )
1666 draw_pixel( x1, y1 );
1667 return;
1670 // if( preview )
1672 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1673 if( xa == -1 || ya == -1 )
1675 rb->lcd_drawline( x1, y1, xb, yb );
1676 rb->lcd_drawline( x2, y2, xb, yb );
1678 else
1680 rb->lcd_drawline( x1, y1, xa, ya );
1681 rb->lcd_drawline( x2, y2, xb, yb );
1683 rb->lcd_set_drawmode(DRMODE_SOLID);
1686 if( xa == -1 || ya == -1 )
1687 /* We only have 3 of the points
1688 * This will currently only be used in preview mode */
1690 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1691 buffer.bezier[i].x1 = a1; \
1692 buffer.bezier[i].y1 = b1; \
1693 buffer.bezier[i].x2 = a2; \
1694 buffer.bezier[i].y2 = b2; \
1695 buffer.bezier[i].x3 = a3; \
1696 buffer.bezier[i].y3 = b3; \
1697 buffer.bezier[i].depth = d; \
1698 i++;
1699 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1700 i--; \
1701 a1 = buffer.bezier[i].x1; \
1702 b1 = buffer.bezier[i].y1; \
1703 a2 = buffer.bezier[i].x2; \
1704 b2 = buffer.bezier[i].y2; \
1705 a3 = buffer.bezier[i].x3; \
1706 b3 = buffer.bezier[i].y3; \
1707 d = buffer.bezier[i].depth;
1708 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1709 while( i )
1711 /* de Casteljau's algorithm (see wikipedia) */
1712 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1713 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1715 xl2 = ( xl1 + xb )>>1;
1716 yl2 = ( yl1 + yb )>>1;
1717 xr2 = ( xb + xr3 )>>1;
1718 yr2 = ( yb + yr3 )>>1;
1719 xr1 = ( xl2 + xr2 )>>1;
1720 yr1 = ( yl2 + yr2 )>>1;
1721 xl3 = xr1;
1722 yl3 = yr1;
1723 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1724 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1726 else
1728 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1729 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1732 #undef PUSH
1733 #undef POP
1735 else /* We have the 4 points */
1737 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1738 buffer.bezier[i].x1 = a1; \
1739 buffer.bezier[i].y1 = b1; \
1740 buffer.bezier[i].x2 = a2; \
1741 buffer.bezier[i].y2 = b2; \
1742 buffer.bezier[i].x3 = a3; \
1743 buffer.bezier[i].y3 = b3; \
1744 buffer.bezier[i].x4 = a4; \
1745 buffer.bezier[i].y4 = b4; \
1746 buffer.bezier[i].depth = d; \
1747 i++;
1748 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1749 i--; \
1750 a1 = buffer.bezier[i].x1; \
1751 b1 = buffer.bezier[i].y1; \
1752 a2 = buffer.bezier[i].x2; \
1753 b2 = buffer.bezier[i].y2; \
1754 a3 = buffer.bezier[i].x3; \
1755 b3 = buffer.bezier[i].y3; \
1756 a4 = buffer.bezier[i].x4; \
1757 b4 = buffer.bezier[i].y4; \
1758 d = buffer.bezier[i].depth;
1760 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1761 while( i )
1763 /* de Casteljau's algorithm (see wikipedia) */
1764 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1765 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1767 xl2 = ( xl1 + xa )>>1;
1768 yl2 = ( yl1 + ya )>>1;
1769 xh = ( xa + xb )>>1;
1770 yh = ( ya + yb )>>1;
1771 xr3 = ( xb + xr4 )>>1;
1772 yr3 = ( yb + yr4 )>>1;
1773 xl3 = ( xl2 + xh )>>1;
1774 yl3 = ( yl2 + yh )>>1;
1775 xr2 = ( xr3 + xh )>>1;
1776 yr2 = ( yr3 + yh )>>1;
1777 xl4 = ( xl3 + xr2 )>>1;
1778 yl4 = ( yl3 + yr2 )>>1;
1779 xr1 = xl4;
1780 yr1 = yl4;
1781 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1782 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1784 else
1786 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1787 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1790 #undef PUSH
1791 #undef POP
1795 static void draw_rect( int x1, int y1, int x2, int y2 )
1797 draw_line( x1, y1, x1, y2 );
1798 draw_line( x1, y1, x2, y1 );
1799 draw_line( x1, y2, x2, y2 );
1800 draw_line( x2, y1, x2, y2 );
1803 static void togglebg( void )
1805 if( isbg )
1807 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1809 else
1811 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1813 isbg = !isbg;
1816 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1818 /* GRUIK */
1819 int x;
1820 togglebg();
1821 if( x1 > x2 )
1823 x = x1;
1824 x1 = x2;
1825 x2 = x;
1827 x = x1;
1828 do {
1829 draw_line( x, y1, x, y2 );
1830 } while( ++x <= x2 );
1831 togglebg();
1832 draw_rect( x1, y1, x2, y2 );
1835 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1837 /* TODO: simplify :) */
1838 int cx = (x1+x2)>>1;
1839 int cy = (y1+y2)>>1;
1841 int rx = (x1-x2)>>1;
1842 int ry = (y1-y2)>>1;
1843 if( rx < 0 ) rx *= -1;
1844 if( ry < 0 ) ry *= -1;
1846 if( rx == 0 || ry == 0 )
1848 draw_line( x1, y1, x2, y2 );
1849 return;
1852 int x,y;
1853 int dst, old_dst;
1855 for( x = 0; x < rx; x++ )
1857 y = 0;
1858 dst = -0xfff;
1859 do {
1860 old_dst = dst;
1861 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1862 y++;
1863 } while( dst < 0 );
1864 if( -old_dst < dst ) y--;
1865 if( full )
1867 draw_line( cx+x, cy, cx+x, cy+y );
1868 draw_line( cx+x, cy, cx+x, cy-y );
1869 draw_line( cx-x, cy, cx-x, cy+y );
1870 draw_line( cx-x, cy, cx-x, cy-y );
1872 else
1874 draw_pixel( cx+x, cy+y );
1875 draw_pixel( cx+x, cy-y );
1876 draw_pixel( cx-x, cy+y );
1877 draw_pixel( cx-x, cy-y );
1880 for( y = 0; y < ry; y++ )
1882 x = 0;
1883 dst = -0xfff;
1884 do {
1885 old_dst = dst;
1886 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1887 x++;
1888 } while( dst < 0 );
1889 if( -old_dst < dst ) x--;
1890 if( full )
1892 draw_line( cx+x, cy, cx+x, cy+y );
1893 draw_line( cx+x, cy, cx+x, cy-y );
1894 draw_line( cx-x, cy, cx-x, cy+y );
1895 draw_line( cx-x, cy, cx-x, cy-y );
1897 else
1899 draw_pixel( cx+x, cy+y );
1900 draw_pixel( cx+x, cy-y );
1901 draw_pixel( cx-x, cy+y );
1902 draw_pixel( cx-x, cy-y );
1907 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1909 draw_oval( x1, y1, x2, y2, false );
1912 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1914 togglebg();
1915 draw_oval( x1, y1, x2, y2, true );
1916 togglebg();
1917 draw_oval( x1, y1, x2, y2, false );
1920 static void draw_fill( int x0, int y0 )
1922 #define PUSH( a, b ) \
1923 draw_pixel( (int)a, (int)b ); \
1924 buffer.coord[i].x = a; \
1925 buffer.coord[i].y = b; \
1926 i++;
1927 #define POP( a, b ) \
1928 i--; \
1929 a = buffer.coord[i].x; \
1930 b = buffer.coord[i].y;
1932 unsigned int i=0;
1933 short x = x0;
1934 short y = y0;
1935 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1937 if( prev_color == rp_colors[ drawcolor ] ) return;
1939 PUSH( x, y );
1941 while( i != 0 )
1943 POP( x, y );
1944 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1946 PUSH( x-1, y );
1948 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1950 PUSH( x+1, y );
1952 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1954 PUSH( x, y-1 );
1956 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1958 PUSH( x, y+1 );
1961 #undef PUSH
1962 #undef POP
1966 /* For preview purposes only */
1967 static void line_gradient( int x1, int y1, int x2, int y2 )
1969 int r1, g1, b1;
1970 int r2, g2, b2;
1971 int h1, s1, v1, h2, s2, v2, r, g, b;
1972 int w, h, x, y;
1974 bool a = false;
1976 x1 <<= 1;
1977 y1 <<= 1;
1978 x2 <<= 1;
1979 y2 <<= 1;
1981 w = x1 - x2;
1982 h = y1 - y2;
1984 if( w == 0 && h == 0 )
1986 draw_pixel( x1>>1, y1>>1 );
1987 return;
1990 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
1991 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
1992 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
1993 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
1994 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
1995 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
1997 if( w < 0 )
1999 w *= -1;
2000 a = true;
2002 if( h < 0 )
2004 h *= -1;
2005 a = !a;
2007 if( a )
2009 r = r1;
2010 r1 = r2;
2011 r2 = r;
2012 g = g1;
2013 g1 = g2;
2014 g2 = g;
2015 b = b1;
2016 b1 = b2;
2017 b2 = b;
2020 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2021 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2023 if( w > h )
2025 if( x1 > x2 )
2027 x = x2;
2028 y = y2;
2029 x2 = x1;
2030 y2 = y1;
2031 x1 = x;
2032 y1 = y;
2034 w = x1 - x2;
2035 h = y1 - y2;
2036 while( x1 <= x2 )
2038 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2039 s1+((s2-s1)*(x1-x2))/w,
2040 v1+((v2-v1)*(x1-x2))/w,
2041 &r, &g, &b );
2042 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2043 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2044 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2045 x1+=2;
2046 y1 = y2 - ( x2 - x1 ) * h / w;
2049 else /* h > w */
2051 if( y1 > y2 )
2053 x = x2;
2054 y = y2;
2055 x2 = x1;
2056 y2 = y1;
2057 x1 = x;
2058 y1 = y;
2060 w = x1 - x2;
2061 h = y1 - y2;
2062 while( y1 <= y2 )
2064 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2065 s1+((s2-s1)*(y1-y2))/h,
2066 v1+((v2-v1)*(y1-y2))/h,
2067 &r, &g, &b );
2068 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2069 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2070 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2071 y1+=2;
2072 x1 = x2 - ( y2 - y1 ) * w / h;
2075 if( a )
2077 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2079 else
2081 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2085 static void linear_gradient( int x1, int y1, int x2, int y2 )
2087 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2088 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2089 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2090 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2091 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2092 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2094 int h1, s1, v1, h2, s2, v2, r, g, b;
2096 /* radius^2 */
2097 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2098 int dist2, i=0;
2100 /* We only propagate the gradient to neighboring pixels with the same
2101 * color as ( x1, y1 ) */
2102 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2104 int x = x1;
2105 int y = y1;
2107 if( radius2 == 0 ) return;
2108 if( preview )
2110 line_gradient( x1, y1, x2, y2 );
2113 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2114 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2116 #define PUSH( x0, y0 ) \
2117 buffer.coord[i].x = (short)(x0); \
2118 buffer.coord[i].y = (short)(y0); \
2119 i++;
2120 #define POP( a, b ) \
2121 i--; \
2122 a = (int)buffer.coord[i].x; \
2123 b = (int)buffer.coord[i].y;
2125 PUSH( x, y );
2127 while( i != 0 )
2129 POP( x, y );
2131 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2132 if( dist2 <= 0 )
2134 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2136 else if( dist2 < radius2 )
2138 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2139 s1+((s2-s1)*dist2)/radius2,
2140 v1+((v2-v1)*dist2)/radius2,
2141 &r, &g, &b );
2142 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2144 else
2146 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2148 if( rp_colors[ drawcolor ] == prev_color )
2150 if( rp_colors[ drawcolor ])
2151 rp_colors[ drawcolor ]--; /* GRUIK */
2152 else
2153 rp_colors[ drawcolor ]++; /* GRUIK */
2155 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2156 draw_pixel( x, y );
2158 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2160 PUSH( x-1, y );
2162 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2164 PUSH( x+1, y );
2166 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2168 PUSH( x, y-1 );
2170 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2172 PUSH( x, y+1 );
2175 #undef PUSH
2176 #undef POP
2178 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2181 static void radial_gradient( int x1, int y1, int x2, int y2 )
2183 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2184 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2185 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2186 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2187 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2188 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2190 int h1, s1, v1, h2, s2, v2, r, g, b;
2192 /* radius^2 */
2193 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2194 int dist2, i=0;
2196 /* We only propagate the gradient to neighboring pixels with the same
2197 * color as ( x1, y1 ) */
2198 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2200 int x = x1;
2201 int y = y1;
2203 if( radius2 == 0 ) return;
2204 if( preview )
2206 line_gradient( x1, y1, x2, y2 );
2209 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2210 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2212 #define PUSH( x0, y0 ) \
2213 buffer.coord[i].x = (short)(x0); \
2214 buffer.coord[i].y = (short)(y0); \
2215 i++;
2216 #define POP( a, b ) \
2217 i--; \
2218 a = (int)buffer.coord[i].x; \
2219 b = (int)buffer.coord[i].y;
2221 PUSH( x, y );
2223 while( i != 0 )
2225 POP( x, y );
2227 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2229 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2230 s1+((s2-s1)*dist2)/radius2,
2231 v1+((v2-v1)*dist2)/radius2,
2232 &r, &g, &b );
2233 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2235 else
2237 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2239 if( rp_colors[ drawcolor ] == prev_color )
2241 if( rp_colors[ drawcolor ])
2242 rp_colors[ drawcolor ]--; /* GRUIK */
2243 else
2244 rp_colors[ drawcolor ]++; /* GRUIK */
2246 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2247 draw_pixel( x, y );
2249 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2251 PUSH( x-1, y );
2253 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2255 PUSH( x+1, y );
2257 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2259 PUSH( x, y-1 );
2261 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2263 PUSH( x, y+1 );
2266 #undef PUSH
2267 #undef POP
2269 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2272 static void draw_toolbars(bool update)
2274 int i;
2275 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2276 rb->lcd_set_background( COLOR_LIGHTGRAY );
2277 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2278 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2279 rb->lcd_set_foreground( COLOR_BLACK );
2280 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2282 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2283 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2284 TB_SC_SIZE, TB_SC_SIZE );
2285 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2286 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2287 TB_SC_SIZE, TB_SC_SIZE );
2288 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2289 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2290 TB_SC_SIZE, TB_SC_SIZE );
2291 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2292 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2293 TB_SC_SIZE, TB_SC_SIZE );
2295 for( i=0; i<18; i++ )
2297 rb->lcd_set_foreground( rp_colors[i] );
2298 rb->lcd_fillrect(
2299 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2300 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2301 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2302 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2303 rb->lcd_drawrect(
2304 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2305 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2306 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2309 #define SEPARATOR( x, y ) \
2310 rb->lcd_set_foreground( COLOR_WHITE ); \
2311 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2312 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2313 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2314 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2316 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2317 TB_TL_WIDTH, TB_TL_HEIGHT );
2318 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2319 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2320 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2321 TB_TL_SIZE, TB_TL_SIZE );
2323 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2325 rb->lcd_setfont( FONT_SYSFIXED );
2326 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2327 rb->lcd_setfont( FONT_UI );
2328 #undef TOP
2330 if( update ) rb->lcd_update();
2333 static void toolbar( void )
2335 int button, i, j;
2336 restore_screen();
2337 draw_toolbars( false );
2338 y = LCD_HEIGHT-TB_HEIGHT/2;
2339 inv_cursor( true );
2340 while( 1 )
2342 switch( button = rb->button_get( true ) )
2344 case ROCKPAINT_DRAW:
2345 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2346 if( y >= TOP + TB_SC_FG_TOP
2347 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2348 && x >= TB_SC_FG_LEFT
2349 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2351 /* click on the foreground color */
2352 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2354 else if( y >= TOP + TB_SC_BG_TOP
2355 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2356 && x >= TB_SC_BG_LEFT
2357 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2359 /* click on the background color */
2360 i = drawcolor;
2361 drawcolor = bgdrawcolor;
2362 bgdrawcolor = i;
2364 else if( y >= TOP + TB_PL_TOP
2365 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2366 && x >= TB_PL_LEFT
2367 && x < TB_PL_LEFT + TB_PL_WIDTH )
2369 /* click on the palette */
2370 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2371 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2372 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2373 break;
2374 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2375 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2376 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2378 else if( y >= TOP+TB_TL_TOP
2379 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2380 && x >= TB_TL_LEFT
2381 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2383 /* click on the tools */
2384 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2385 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2386 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2387 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2388 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2389 tool = i*2+j;
2390 prev_x = -1;
2391 prev_y = -1;
2392 prev_x2 = -1;
2393 prev_y2 = -1;
2394 prev_x3 = -1;
2395 prev_y3 = -1;
2396 preview = false;
2398 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2400 /* menu button */
2401 goto_menu();
2403 #undef TOP
2404 restore_screen();
2405 draw_toolbars( false );
2406 inv_cursor( true );
2407 break;
2409 case ROCKPAINT_LEFT:
2410 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2411 inv_cursor(false);
2412 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2413 if (x<0) x=COLS-1;
2414 inv_cursor(true);
2415 break;
2417 case ROCKPAINT_RIGHT:
2418 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2419 inv_cursor(false);
2420 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2421 if (x>=COLS) x=0;
2422 inv_cursor(true);
2423 break;
2425 case ROCKPAINT_UP:
2426 case ROCKPAINT_UP | BUTTON_REPEAT:
2427 inv_cursor(false);
2428 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2429 if (y<LCD_HEIGHT-TB_HEIGHT)
2431 return;
2433 inv_cursor(true);
2434 break;
2436 case ROCKPAINT_DOWN:
2437 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2438 inv_cursor(false);
2439 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2440 if (y>=LCD_HEIGHT)
2442 y = 0;
2443 return;
2445 inv_cursor(true);
2446 break;
2448 case ROCKPAINT_TOOLBAR:
2449 case ROCKPAINT_TOOLBAR2:
2450 return;
2452 if( quit ) return;
2456 static void inv_cursor(bool update)
2458 rb->lcd_set_foreground(COLOR_BLACK);
2459 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2460 /* cross painting */
2461 rb->lcd_hline(x-4,x+4,y);
2462 rb->lcd_vline(x,y-4,y+4);
2463 rb->lcd_set_foreground(rp_colors[drawcolor]);
2464 rb->lcd_set_drawmode(DRMODE_SOLID);
2466 if( update ) rb->lcd_update();
2469 static void restore_screen(void)
2471 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2472 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2473 rb->lcd_vline( img_width, 0, ROWS );
2474 rb->lcd_hline( 0, COLS, img_height );
2475 rb->lcd_drawpixel( img_width, img_height );
2476 rb->lcd_set_drawmode(DRMODE_SOLID);
2479 static void clear_drawing(void)
2481 init_buffer();
2482 img_height = ROWS;
2483 img_width = COLS;
2484 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2485 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2486 rb->lcd_update();
2489 static void goto_menu(void)
2491 int multi;
2492 int selected = 0;
2494 while( 1 )
2496 switch( rb->do_menu( &main_menu, &selected, NULL, false ) )
2498 case MAIN_MENU_NEW:
2499 clear_drawing();
2500 return;
2502 case MAIN_MENU_LOAD:
2503 if( browse( filename, MAX_PATH, "/" ) )
2505 if( load_bitmap( filename ) <= 0 )
2507 rb->splashf( 1*HZ, "Error while loading %s",
2508 filename );
2509 clear_drawing();
2511 else
2513 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2514 restore_screen();
2515 inv_cursor(true);
2516 return;
2519 break;
2521 case MAIN_MENU_SAVE:
2522 rb->lcd_set_foreground(COLOR_BLACK);
2523 if (!filename[0])
2524 rb->strcpy(filename,"/");
2525 if( !rb->kbd_input( filename, MAX_PATH ) )
2527 if( !check_extention( filename, ".bmp" ) )
2528 rb->strcat(filename, ".bmp");
2529 save_bitmap( filename );
2530 rb->splashf( 1*HZ, "File saved (%s)", filename );
2532 break;
2534 case MAIN_MENU_SET_WIDTH:
2535 rb->set_int( "Set Width", "px", UNIT_INT, &img_width,
2536 NULL, 1, 1, COLS, NULL );
2537 break;
2538 case MAIN_MENU_SET_HEIGHT:
2539 rb->set_int( "Set Height", "px", UNIT_INT, &img_height,
2540 NULL, 1, 1, ROWS, NULL );
2541 break;
2542 case MAIN_MENU_BRUSH_SIZE:
2543 for(multi = 0; multi<4; multi++)
2544 if(bsize == times_list[multi]) break;
2545 rb->set_option( "Brush Size", &multi, INT, times_options, 4, NULL );
2546 if( multi >= 0 )
2547 bsize = times_list[multi];
2548 break;
2550 case MAIN_MENU_BRUSH_SPEED:
2551 for(multi = 0; multi<3; multi++)
2552 if(bspeed == times_list[multi]) break;
2553 rb->set_option( "Brush Speed", &multi, INT, times_options, 3, NULL );
2554 if( multi >= 0 )
2555 bspeed = times_list[multi];
2556 break;
2558 case MAIN_MENU_COLOR:
2559 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2560 break;
2562 case MAIN_MENU_GRID_SIZE:
2563 for(multi = 0; multi<4; multi++)
2564 if(gridsize == gridsize_list[multi]) break;
2565 rb->set_option( "Grid Size", &multi, INT, gridsize_options, 4, NULL );
2566 if( multi >= 0 )
2567 gridsize = gridsize_list[multi];
2568 break;
2570 case MAIN_MENU_PLAYBACK_CONTROL:
2571 playback_control( NULL );
2572 break;
2574 case MAIN_MENU_EXIT:
2575 restore_screen();
2576 quit=true;
2577 return;
2579 case MAIN_MENU_RESUME:
2580 default:
2581 restore_screen();
2582 return;
2583 }/* end switch */
2584 }/* end while */
2587 static void reset_tool( void )
2589 prev_x = -1;
2590 prev_y = -1;
2591 prev_x2 = -1;
2592 prev_y2 = -1;
2593 prev_x3 = -1;
2594 prev_y3 = -1;
2595 tool_mode = -1;
2596 preview = false;
2599 static bool rockpaint_loop( void )
2601 int button=0,i,j;
2602 int accelaration;
2604 x = 10;
2605 toolbar();
2606 x = 0; y = 0;
2607 restore_screen();
2608 inv_cursor(true);
2610 while (!quit) {
2611 button = rb->button_get(true);
2613 if( tool == Brush && prev_x != -1 )
2615 accelaration = 1;
2617 else if( button & BUTTON_REPEAT )
2619 accelaration = 4;
2621 else
2623 accelaration = 1;
2626 switch(button)
2628 case ROCKPAINT_QUIT:
2629 rb->lcd_set_drawmode(DRMODE_SOLID);
2630 return PLUGIN_OK;
2632 case ROCKPAINT_MENU:
2633 inv_cursor(false);
2634 goto_menu();
2635 restore_screen();
2636 inv_cursor(true);
2637 break;
2639 case ROCKPAINT_DRAW:
2640 inv_cursor(false);
2641 switch( tool )
2643 case Brush:
2644 if( prev_x == -1 ) prev_x = 1;
2645 else prev_x = -1;
2646 break;
2648 case SelectRectangle:
2649 case Line:
2650 case Curve:
2651 case Rectangle:
2652 case RectangleFull:
2653 case Oval:
2654 case OvalFull:
2655 case LinearGradient:
2656 case RadialGradient:
2657 /* Curve uses 4 points, others use 2 */
2658 if( prev_x == -1 || prev_y == -1 )
2660 prev_x = x;
2661 prev_y = y;
2662 preview = true;
2664 else if( tool == Curve
2665 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2667 prev_x2 = x;
2668 prev_y2 = y;
2670 else if( tool == SelectRectangle
2671 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2673 tool_mode = rb->do_menu( &select_menu,
2674 NULL, NULL, false );
2675 switch( tool_mode )
2677 case SELECT_MENU_CUT:
2678 case SELECT_MENU_COPY:
2679 prev_x2 = x;
2680 prev_y2 = y;
2681 copy_to_clipboard();
2682 if( prev_x < x ) x = prev_x;
2683 if( prev_y < y ) y = prev_y;
2684 break;
2686 case SELECT_MENU_INVERT:
2687 draw_invert( prev_x, prev_y, x, y );
2688 reset_tool();
2689 break;
2691 case SELECT_MENU_HFLIP:
2692 draw_hflip( prev_x, prev_y, x, y );
2693 reset_tool();
2694 break;
2696 case SELECT_MENU_VFLIP:
2697 draw_vflip( prev_x, prev_y, x, y );
2698 reset_tool();
2699 break;
2701 case SELECT_MENU_ROTATE90:
2702 draw_rot_90_deg( prev_x, prev_y, x, y, 1 );
2703 reset_tool();
2704 break;
2706 case SELECT_MENU_ROTATE180:
2707 draw_hflip( prev_x, prev_y, x, y );
2708 draw_vflip( prev_x, prev_y, x, y );
2709 reset_tool();
2710 break;
2712 case SELECT_MENU_ROTATE270:
2713 draw_rot_90_deg( prev_x, prev_y, x, y, -1 );
2714 reset_tool();
2715 break;
2717 case SELECT_MENU_CANCEL:
2718 reset_tool();
2719 break;
2721 default:
2722 break;
2724 restore_screen();
2726 else if( tool == Curve
2727 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2729 prev_x3 = x;
2730 prev_y3 = y;
2732 else
2734 preview = false;
2735 switch( tool )
2737 case SelectRectangle:
2738 draw_paste_rectangle( prev_x, prev_y,
2739 prev_x2, prev_y2,
2740 x, y, tool_mode );
2741 break;
2742 case Line:
2743 draw_line( prev_x, prev_y, x, y );
2744 break;
2745 case Curve:
2746 draw_curve( prev_x, prev_y,
2747 prev_x2, prev_y2,
2748 prev_x3, prev_y3,
2749 x, y );
2750 break;
2751 case Rectangle:
2752 draw_rect( prev_x, prev_y, x, y );
2753 break;
2754 case RectangleFull:
2755 draw_rect_full( prev_x, prev_y, x, y );
2756 break;
2757 case Oval:
2758 draw_oval_empty( prev_x, prev_y, x, y );
2759 break;
2760 case OvalFull:
2761 draw_oval_full( prev_x, prev_y, x, y );
2762 break;
2763 case LinearGradient:
2764 linear_gradient( prev_x, prev_y, x, y );
2765 break;
2766 case RadialGradient:
2767 radial_gradient( prev_x, prev_y, x, y );
2768 break;
2769 default:
2770 break;
2772 reset_tool();
2773 restore_screen();
2775 break;
2777 case Fill:
2778 draw_fill( x, y );
2779 restore_screen();
2780 break;
2782 case ColorPicker:
2783 color_picker( x, y );
2784 break;
2786 case Text:
2787 draw_text( x, y );
2788 break;
2790 default:
2791 break;
2793 inv_cursor(true);
2794 break;
2796 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2797 if( tool == Curve )
2799 /* 3 point bezier curve */
2800 preview = false;
2801 draw_curve( prev_x, prev_y,
2802 prev_x2, prev_y2,
2803 -1, -1,
2804 x, y );
2805 reset_tool();
2806 restore_screen();
2807 inv_cursor( true );
2809 break;
2811 case ROCKPAINT_TOOLBAR:
2812 i = x; j = y;
2813 x = 10;
2814 toolbar();
2815 x = i; y = j;
2816 restore_screen();
2817 inv_cursor(true);
2818 break;
2820 case ROCKPAINT_TOOLBAR2:
2821 i = x; j = y;
2822 x = 110;
2823 toolbar();
2824 x = i; y = j;
2825 restore_screen();
2826 inv_cursor(true);
2827 break;
2829 case ROCKPAINT_LEFT:
2830 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2831 inv_cursor(false);
2832 x-=bspeed * accelaration;
2833 if (x<0) x=COLS-1;
2834 inv_cursor(true);
2835 break;
2837 case ROCKPAINT_RIGHT:
2838 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2839 inv_cursor(false);
2840 x+=bspeed * accelaration;
2841 if (x>=COLS) x=0;
2842 inv_cursor(true);
2843 break;
2845 case ROCKPAINT_UP:
2846 case ROCKPAINT_UP | BUTTON_REPEAT:
2847 inv_cursor(false);
2848 y-=bspeed * accelaration;
2849 if (y<0) y=ROWS-1;
2850 inv_cursor(true);
2851 break;
2853 case ROCKPAINT_DOWN:
2854 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2855 inv_cursor(false);
2856 y+=bspeed * accelaration;
2857 if (y>=ROWS)
2859 toolbar();
2860 restore_screen();
2862 inv_cursor(true);
2863 break;
2865 default:
2866 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2867 return PLUGIN_USB_CONNECTED;
2868 break;
2870 if( tool == Brush && prev_x == 1 )
2872 inv_cursor(false);
2873 draw_brush( x, y );
2874 inv_cursor(true);
2876 if( preview || tool == ColorPicker )
2877 /* always preview color picker */
2879 restore_screen();
2880 switch( tool )
2882 case SelectRectangle:
2883 if( prev_x2 == -1 || prev_y2 == -1 )
2885 /* we are defining the selection */
2886 draw_select_rectangle( prev_x, prev_y, x, y );
2888 else
2890 /* we are pasting the selected data */
2891 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2892 prev_y2, x, y, tool_mode );
2893 prev_x3 = prev_x2-prev_x;
2894 if( prev_x3 < 0 ) prev_x3 *= -1;
2895 prev_y3 = prev_y2-prev_y;
2896 if( prev_y3 < 0 ) prev_y3 *= -1;
2897 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2898 prev_x3 = -1;
2899 prev_y3 = -1;
2901 break;
2903 case Brush:
2904 break;
2906 case Line:
2907 draw_line( prev_x, prev_y, x, y );
2908 break;
2910 case Curve:
2911 if( prev_x2 == -1 || prev_y2 == -1 )
2913 draw_line( prev_x, prev_y, x, y );
2915 else
2917 draw_curve( prev_x, prev_y,
2918 prev_x2, prev_y2,
2919 prev_x3, prev_y3,
2920 x, y );
2922 break;
2924 case Rectangle:
2925 draw_rect( prev_x, prev_y, x, y );
2926 break;
2928 case RectangleFull:
2929 draw_rect_full( prev_x, prev_y, x, y );
2930 break;
2932 case Oval:
2933 draw_oval_empty( prev_x, prev_y, x, y );
2934 break;
2936 case OvalFull:
2937 draw_oval_full( prev_x, prev_y, x, y );
2938 break;
2940 case Fill:
2941 break;
2943 case ColorPicker:
2944 preview = true;
2945 color_picker( x, y );
2946 preview = false;
2947 break;
2949 case LinearGradient:
2950 line_gradient( prev_x, prev_y, x, y );
2951 break;
2953 case RadialGradient:
2954 line_gradient( prev_x, prev_y, x, y );
2955 break;
2957 case Text:
2958 default:
2959 break;
2961 inv_cursor( true );
2963 if( gridsize > 0 )
2965 show_grid( true );
2966 show_grid( false );
2970 return PLUGIN_OK;
2973 static int load_bitmap( const char *file )
2975 struct bitmap bm;
2976 bool ret;
2977 int i, j;
2978 fb_data color = rp_colors[ bgdrawcolor ];
2980 bm.data = (char*)save_buffer;
2981 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2982 FORMAT_NATIVE, NULL );
2984 if((bm.width > COLS ) || ( bm.height > ROWS ))
2985 return -1;
2987 img_width = bm.width;
2988 img_height = bm.height;
2989 for( i = bm.height-1; i >= 0; i-- )
2991 rb->memmove( save_buffer+i*COLS, save_buffer+i*bm.width,
2992 sizeof( fb_data )*bm.width );
2993 for( j = bm.width; j < COLS; j++ )
2994 save_buffer[j+i*COLS] = color;
2996 for( i = bm.height*COLS; i < ROWS*COLS; i++ )
2997 save_buffer[i] = color;
2999 return ret;
3002 static int save_bitmap( char *file )
3004 struct bitmap bm;
3005 int i;
3006 for(i = 0; i < img_height; i++)
3008 rb->memcpy( buffer.clipboard+i*img_width, save_buffer+i*COLS,
3009 sizeof( fb_data )*img_width );
3011 bm.data = (char*)buffer.clipboard;
3012 bm.height = img_height;
3013 bm.width = img_width;
3014 bm.format = FORMAT_NATIVE;
3015 return save_bmp_file( file, &bm );
3018 enum plugin_status plugin_start(const void* parameter)
3020 rb->lcd_set_foreground(COLOR_WHITE);
3021 rb->lcd_set_backdrop(NULL);
3022 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
3023 rb->splash( HZ/2, "Rock Paint");
3025 rb->lcd_clear_display();
3027 filename[0] = '\0';
3029 if( parameter )
3031 if( load_bitmap( parameter ) <= 0 )
3033 rb->splash( 1*HZ, "File Open Error");
3034 clear_drawing();
3036 else
3038 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
3039 restore_screen();
3040 rb->strcpy( filename, parameter );
3043 else
3045 clear_drawing();
3047 inv_cursor(true);
3049 return rockpaint_loop();