Update the french translation regarding the previous revision.
[kugel-rb.git] / apps / plugins / rockpaint.c
blob07351ffb44b0082129cbd95b921f55d5b9b78a72
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"
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_HDD6330_PAD
159 #define ROCKPAINT_QUIT BUTTON_POWER
160 #define ROCKPAINT_DRAW BUTTON_PLAY
161 #define ROCKPAINT_MENU BUTTON_MENU
162 #define ROCKPAINT_TOOLBAR BUTTON_PREV
163 #define ROCKPAINT_TOOLBAR2 BUTTON_NEXT
164 #define ROCKPAINT_UP BUTTON_UP
165 #define ROCKPAINT_DOWN BUTTON_DOWN
166 #define ROCKPAINT_LEFT BUTTON_LEFT
167 #define ROCKPAINT_RIGHT BUTTON_RIGHT
169 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
170 #define ROCKPAINT_QUIT BUTTON_POWER
171 #define ROCKPAINT_DRAW BUTTON_PLAY
172 #define ROCKPAINT_MENU BUTTON_MENU
173 #define ROCKPAINT_TOOLBAR BUTTON_RIGHT
174 #define ROCKPAINT_TOOLBAR2 BUTTON_LEFT
175 #define ROCKPAINT_UP BUTTON_UP
176 #define ROCKPAINT_DOWN BUTTON_DOWN
177 #define ROCKPAINT_LEFT BUTTON_PREV
178 #define ROCKPAINT_RIGHT BUTTON_NEXT
180 #elif ( CONFIG_KEYPAD == ONDAVX747_PAD )
181 #define ROCKPAINT_QUIT BUTTON_POWER
182 #define ROCKPAINT_MENU BUTTON_MENU
184 #elif ( CONFIG_KEYPAD == ONDAVX777_PAD )
185 #define ROCKPAINT_QUIT BUTTON_POWER
187 #elif CONFIG_KEYPAD == MROBE500_PAD
188 #define ROCKPAINT_QUIT BUTTON_POWER
190 #elif ( CONFIG_KEYPAD == SAMSUNG_YH_PAD )
191 #define ROCKPAINT_QUIT BUTTON_REC
192 #define ROCKPAINT_DRAW BUTTON_PLAY
193 #define ROCKPAINT_MENU BUTTON_FFWD
194 #define ROCKPAINT_TOOLBAR BUTTON_REW
195 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
196 #define ROCKPAINT_UP BUTTON_UP
197 #define ROCKPAINT_DOWN BUTTON_DOWN
198 #define ROCKPAINT_LEFT BUTTON_LEFT
199 #define ROCKPAINT_RIGHT BUTTON_RIGHT
201 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
202 #define ROCKPAINT_QUIT BUTTON_REC
203 #define ROCKPAINT_DRAW BUTTON_PLAY
204 #define ROCKPAINT_MENU BUTTON_MENU
205 #define ROCKPAINT_TOOLBAR BUTTON_OK
206 #define ROCKPAINT_TOOLBAR2 BUTTON_CANCEL
207 #define ROCKPAINT_UP BUTTON_UP
208 #define ROCKPAINT_DOWN BUTTON_DOWN
209 #define ROCKPAINT_LEFT BUTTON_PREV
210 #define ROCKPAINT_RIGHT BUTTON_NEXT
212 #else
213 #error "Please define keys for this keypad"
214 #endif
216 #ifdef HAVE_TOUCHSCREEN
217 #ifndef ROCKPAINT_QUIT
218 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
219 #endif
220 #ifndef ROCKPAINT_DRAW
221 #define ROCKPAINT_DRAW BUTTON_CENTER
222 #endif
223 #ifndef ROCKPAINT_MENU
224 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
225 #endif
226 #ifndef ROCKPAINT_TOOLBAR
227 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
228 #endif
229 #ifndef ROCKPAINT_TOOLBAR2
230 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
231 #endif
232 #ifndef ROCKPAINT_UP
233 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
234 #endif
235 #ifndef ROCKPAINT_DOWN
236 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
237 #endif
238 #ifndef ROCKPAINT_LEFT
239 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
240 #endif
241 #ifndef ROCKPAINT_RIGHT
242 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
243 #endif
244 #endif
246 /***********************************************************************
247 * Palette Default Colors
248 ***********************************************************************/
249 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
250 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
251 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
252 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
253 #define COLOR_RED LCD_RGBPACK(128,0,0)
254 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
255 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
256 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
257 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
258 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
259 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
260 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
261 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
262 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
263 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
264 #define COLOR_PINK LCD_RGBPACK(255,0,255)
265 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
266 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
268 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
269 #define ROCKPAINT_TITLE_FONT 2
271 /***********************************************************************
272 * Program Colors
273 ***********************************************************************/
274 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
275 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
277 #define ROWS LCD_HEIGHT
278 #define COLS LCD_WIDTH
281 * Toolbar positioning stuff ... don't read this unless you really need to
283 * TB Toolbar
284 * SP Separator
285 * SC Selected Color
286 * PL Palette
287 * TL Tools
290 /* Separator sizes */
291 #define TB_SP_MARGIN 3
292 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
294 /* Selected color sizes */
295 #define TB_SC_SIZE 12
297 /* Palette sizes */
298 #define TB_PL_COLOR_SIZE 7
299 #define TB_PL_COLOR_SPACING 2
300 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
301 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
303 /* Tools sizes */
304 #define TB_TL_SIZE 8
305 #define TB_TL_SPACING 2
306 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
307 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
309 /* Menu button size ... gruik */
310 #define TB_MENU_MIN_WIDTH 30
312 /* Selected colors position */
313 #define TB_SC_FG_TOP 2
314 #define TB_SC_FG_LEFT 2
315 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
316 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
318 /* Palette position */
319 #define TB_PL_TOP TB_SC_FG_TOP
320 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
322 /* Tools position */
323 #define TB_TL_TOP TB_SC_FG_TOP
324 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
326 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
327 #undef TB_TL_TOP
328 #undef TB_TL_LEFT
329 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
330 #define TB_TL_LEFT TB_SC_FG_LEFT
331 #endif
333 /* Menu button position */
334 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
335 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
337 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
340 static void draw_pixel(int x,int y);
341 static void draw_line( int x1, int y1, int x2, int y2 );
342 static void draw_rect( int x1, int y1, int x2, int y2 );
343 static void draw_toolbars(bool update);
344 static void inv_cursor(bool update);
345 static void restore_screen(void);
346 static void clear_drawing(void);
347 static void goto_menu(void);
348 static int load_bitmap( const char *filename );
349 static int save_bitmap( char *filename );
350 static void draw_rect_full( int x1, int y1, int x2, int y2 );
352 /***********************************************************************
353 * Global variables
354 ***********************************************************************/
356 static int drawcolor=0; /* Current color (in palette) */
357 static int bgdrawcolor=9; /* Current background color (in palette) */
358 static int img_height = ROWS;
359 static int img_width = COLS;
360 bool isbg = false; /* gruik ugly hack alert */
362 static int preview=false; /* Is preview mode on ? */
364 /* TODO: clean this up */
365 static int x=0, y=0; /* cursor position */
366 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
367 static int prev_x2=-1, prev_y2=-1;
368 static int prev_x3=-1, prev_y3=-1;
369 static int tool_mode=-1;
372 static int bsize=1; /* brush size */
373 static int bspeed=1; /* brush speed */
375 enum Tools { Brush = 0, /* Regular brush */
376 Fill = 1, /* Fill a shape with current color */
377 SelectRectangle = 2,
378 ColorPicker = 3, /* Pick a color */
379 Line = 4, /* Draw a line between two points */
380 Unused = 5, /* THIS IS UNUSED ... */
381 Curve = 6,
382 Text = 7,
383 Rectangle = 8, /* Draw a rectangle */
384 RectangleFull = 9,
385 Oval = 10, /* Draw an oval */
386 OvalFull = 11,
387 LinearGradient = 12,
388 RadialGradient = 13
391 enum Tools tool = Brush;
393 static bool quit=false;
394 static int gridsize=0;
396 static fb_data rp_colors[18] =
398 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
399 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
400 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
401 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
402 COLOR_LIGHTBROWN
405 static fb_data save_buffer[ ROWS*COLS ];
407 extern fb_data rockpaint[];
408 extern fb_data rockpaint_hsvrgb[];
410 /* Maximum string size allowed for the text tool */
411 #define MAX_TEXT 256
413 typedef union
415 /* Used by fill and gradient algorithms */
416 struct
418 short x;
419 short y;
420 } coord[ ROWS*COLS ];
422 /* Used by bezier curve algorithms */
423 struct
425 short x1, y1;
426 short x2, y2;
427 short x3, y3;
428 short x4, y4;
429 short depth;
430 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
431 * than coord ... so we divide to take
432 * less memory. */
434 /* Used to cut/copy/paste data */
435 fb_data clipboard[ ROWS*COLS ];
437 /* Used for text mode */
438 struct
440 char text[MAX_TEXT];
441 char font[MAX_PATH];
442 char old_font[MAX_PATH];
443 int fh_buf[30];
444 int fw_buf[30];
445 char fontname_buf[30][MAX_PATH];
446 } text;
447 } buf;
449 static buf *buffer;
451 /* Current filename */
452 static char filename[MAX_PATH];
454 /* Font preview buffer */
455 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
456 //#define FONT_PREVIEW_HEIGHT 1000
457 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
459 /***********************************************************************
460 * Offscreen buffer/Text/Fonts handling
462 * Parts of code taken from firmware/drivers/lcd-16bit.c
463 ***********************************************************************/
464 static void buffer_mono_bitmap_part(
465 fb_data *buf, int buf_width, int buf_height,
466 const unsigned char *src, int src_x, int src_y,
467 int stride, int x, int y, int width, int height )
468 /* this function only draws the foreground part of the bitmap */
470 const unsigned char *src_end;
471 fb_data *dst, *dst_end;
472 unsigned fgcolor = rb->lcd_get_foreground();
474 /* nothing to draw? */
475 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
476 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
477 return;
479 /* clipping */
480 if( x < 0 )
482 width += x;
483 src_x -= x;
484 x = 0;
486 if( y < 0 )
488 height += y;
489 src_y -= y;
490 y = 0;
492 if( x + width > buf_width )
493 width = buf_width - x;
494 if( y + height > buf_height )
495 height = buf_height - y;
497 src += stride * (src_y >> 3) + src_x; /* move starting point */
498 src_y &= 7;
499 src_end = src + width;
501 dst = buf + y*buf_width + x;
505 const unsigned char *src_col = src++;
506 unsigned data = *src_col >> src_y;
507 fb_data *dst_col = dst++;
508 int numbits = 8 - src_y;
510 dst_end = dst_col + height * buf_width;
513 if( data & 0x01 )
514 *dst_col = fgcolor; /* FIXME ? */
516 dst_col += buf_width;
518 data >>= 1;
519 if( --numbits == 0 )
521 src_col += stride;
522 data = *src_col;
523 numbits = 8;
525 } while( dst_col < dst_end );
526 } while( src < src_end );
529 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
530 int x, int y, int ofs, const unsigned char *str )
532 unsigned short ch;
533 unsigned short *ucs;
535 struct font *pf = rb->font_get( FONT_UI );
536 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
538 ucs = rb->bidi_l2v( str, 1 );
540 while( (ch = *ucs++) != 0 && x < buf_width )
542 int width;
543 const unsigned char *bits;
545 /* get proportional width and glyph bits */
546 width = rb->font_get_width( pf, ch );
548 if( ofs > width )
550 ofs -= width;
551 continue;
554 bits = rb->font_get_bits( pf, ch );
556 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0,
557 width, x, y, width - ofs, pf->height);
559 x += width - ofs;
560 ofs = 0;
564 /***********************************************************************
565 * Menu handling
566 ***********************************************************************/
567 enum {
568 /* Main menu */
569 MAIN_MENU_RESUME,
570 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
571 MAIN_MENU_SET_WIDTH, MAIN_MENU_SET_HEIGHT,
572 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
573 MAIN_MENU_GRID_SIZE,
574 MAIN_MENU_PLAYBACK_CONTROL,
575 MAIN_MENU_EXIT,
577 enum {
578 /* Select action menu */
579 SELECT_MENU_CUT, SELECT_MENU_COPY,
580 SELECT_MENU_INVERT, SELECT_MENU_HFLIP, SELECT_MENU_VFLIP,
581 SELECT_MENU_ROTATE90, SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
582 SELECT_MENU_CANCEL,
584 enum {
585 /* Text menu */
586 TEXT_MENU_TEXT, TEXT_MENU_FONT,
587 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
590 MENUITEM_STRINGLIST(main_menu, "RockPaint", NULL,
591 "Resume", "New", "Load", "Save",
592 "Set Width", "Set Height",
593 "Brush Size", "Brush Speed",
594 "Choose Color", "Grid Size",
595 "Playback Control", "Exit");
596 MENUITEM_STRINGLIST(select_menu, "Select...", NULL,
597 "Cut", "Copy",
598 "Invert", "Horizontal Flip", "Vertical Flip",
599 "Rotate 90°", "Rotate 180°", "Rotate 270°",
600 "Cancel");
601 MENUITEM_STRINGLIST(text_menu, "Text", NULL,
602 "Set Text", "Change Font",
603 "Preview", "Apply", "Cancel");
604 static const int times_list[] = { 1, 2, 4, 8 };
605 static const int gridsize_list[] = { 0, 5, 10, 20 };
606 static const struct opt_items times_options[] = {
607 { "1x", -1 }, { "2x", -1 }, { "4x", -1 }, { "8x", -1 }
609 static const struct opt_items gridsize_options[] = {
610 { "No grid", -1 }, { "5px", -1 }, { "10px", -1 }, { "20px", -1 }
613 static int draw_window( int height, int width,
614 int *top, int *left,
615 const char *title )
617 int fh;
618 rb->lcd_getstringsize( title, NULL, &fh );
619 fh++;
621 const int _top = ( LCD_HEIGHT - height ) / 2;
622 const int _left = ( LCD_WIDTH - width ) / 2;
623 if( top ) *top = _top;
624 if( left ) *left = _left;
625 rb->lcd_set_background(COLOR_BLUE);
626 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
627 rb->lcd_fillrect( _left, _top, width, height );
628 rb->lcd_set_foreground(COLOR_BLUE);
629 rb->lcd_fillrect( _left, _top, width, fh+4 );
630 rb->lcd_set_foreground(COLOR_WHITE);
631 rb->lcd_putsxy( _left+2, _top+2, title );
632 rb->lcd_set_foreground(COLOR_BLACK);
633 rb->lcd_drawrect( _left, _top, width, height );
634 return _top+fh+4;
637 /***********************************************************************
638 * File browser
639 ***********************************************************************/
641 char bbuf[MAX_PATH]; /* used by file and font browsers */
642 char bbuf_s[MAX_PATH]; /* used by file and font browsers */
643 struct tree_context *tree = NULL;
645 static bool check_extention(const char *filename, const char *ext)
647 const char *p = rb->strrchr( filename, '.' );
648 return ( p != NULL && !rb->strcasecmp( p, ext ) );
651 static const char* browse_get_name_cb(int selected_item, void *data,
652 char *buffer, size_t buffer_len)
654 int *indexes = (int *) data;
655 struct entry* dc = tree->dircache;
656 struct entry* e = &dc[indexes[selected_item]];
657 (void) buffer;
658 (void) buffer_len;
660 return e->name;
663 static bool browse( char *dst, int dst_size, const char *start )
665 struct gui_synclist browse_list;
666 int item_count = 0, selected, button;
667 struct tree_context backup;
668 struct entry *dc;
669 bool reload = true;
670 int dirfilter = SHOW_ALL;
671 int *indexes = (int *) buffer->clipboard;
673 char *a;
675 rb->strcpy( bbuf, start );
676 a = bbuf+rb->strlen(bbuf)-1;
677 if( *a != '/' )
679 a[1] = '/';
680 a[2] = '\0';
682 bbuf_s[0] = '\0';
684 rb->gui_synclist_init(&browse_list, browse_get_name_cb,
685 (void*) indexes, false, 1, NULL);
687 tree = rb->tree_get_context();
688 backup = *tree;
689 dc = tree->dircache;
690 a = backup.currdir+rb->strlen(backup.currdir)-1;
691 if( *a != '/' )
693 *++a = '/';
694 *++a = '\0';
696 rb->strcpy( a, dc[tree->selected_item].name );
697 tree->dirfilter = &dirfilter;
698 while( 1 )
700 if( reload )
702 int i;
703 rb->set_current_file(bbuf);
704 item_count = 0;
705 selected = 0;
706 for( i = 0; i < tree->filesindir ; i++)
708 /* only displayes directories and .bmp files */
709 if( ((dc[i].attr & ATTR_DIRECTORY ) &&
710 rb->strcmp( dc[i].name, "." ) &&
711 rb->strcmp( dc[i].name, ".." )) ||
712 ( !(dc[i].attr & ATTR_DIRECTORY) &&
713 check_extention( dc[i].name, ".bmp" ) ) )
715 if( !rb->strcmp( dc[i].name, bbuf_s ) )
716 selected = item_count;
717 indexes[item_count++] = i;
721 rb->gui_synclist_set_nb_items(&browse_list,item_count);
722 rb->gui_synclist_select_item(&browse_list, selected);
723 rb->gui_synclist_set_title(&browse_list, bbuf, NOICON);
724 rb->gui_synclist_draw(&browse_list);
725 reload = false;
727 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
728 if (rb->gui_synclist_do_button(&browse_list,&button,LIST_WRAP_UNLESS_HELD))
729 continue;
730 switch( button )
732 case ACTION_STD_CANCEL:
733 if( !rb->strcmp( bbuf, "/" ) )
735 *tree = backup;
736 rb->set_current_file( backup.currdir );
737 return false;
739 rb->strcpy( bbuf_s, ".." );
740 case ACTION_STD_OK:
741 if( button == ACTION_STD_OK )
743 selected = rb->gui_synclist_get_sel_pos( &browse_list );
744 if( selected < 0 || selected >= item_count )
745 break;
746 struct entry* e = &dc[indexes[selected]];
747 rb->strlcpy( bbuf_s, e->name, sizeof( bbuf_s ) );
748 if( !( e->attr & ATTR_DIRECTORY ) )
750 *tree = backup;
751 rb->set_current_file( backup.currdir );
752 rb->snprintf( dst, dst_size, "%s%s", bbuf, bbuf_s );
753 return true;
756 if( !rb->strcmp( bbuf_s, "." ) ) break;
757 a = bbuf+rb->strlen(bbuf);
758 if( !rb->strcmp( bbuf_s, ".." ) )
760 a--;
761 if( a == bbuf ) break;
762 if( *a == '/' ) a--;
763 while( *a != '/' ) a--;
764 rb->strcpy( bbuf_s, ++a );
765 /* select parent directory */
766 bbuf_s[rb->strlen(bbuf_s)-1] = '\0';
767 *a = '\0';
768 reload = true;
769 break;
771 rb->snprintf( a, bbuf+sizeof(bbuf)-a, "%s/", bbuf_s );
772 reload = true;
773 break;
775 case ACTION_STD_MENU:
776 *tree = backup;
777 rb->set_current_file( backup.currdir );
778 return false;
783 /***********************************************************************
784 * Font browser
786 * FIXME: This still needs some work ... it currently only works fine
787 * on the simulators, disk spins too much on real targets -> rendered
788 * font buffer needed.
789 ***********************************************************************/
790 static bool browse_fonts( char *dst, int dst_size )
792 #define WIDTH ( LCD_WIDTH - 20 )
793 #define HEIGHT ( LCD_HEIGHT - 20 )
794 #define LINE_SPACE 2
795 int top, top_inside = 0, left;
797 DIR *d;
798 struct dirent *de;
799 int fvi = 0; /* first visible item */
800 int lvi = 0; /* last visible item */
801 int si = 0; /* selected item */
802 int osi = 0; /* old selected item */
803 int li = 0; /* last item */
804 int nvih = 0; /* next visible item height */
805 int i;
806 int b_need_redraw = 1; /* Do we need to redraw ? */
808 int cp = 0; /* current position */
809 int fh; /* font height */
811 #define fh_buf buffer->text.fh_buf /* 30 might not be enough ... */
812 #define fw_buf buffer->text.fw_buf
813 int fw;
814 #define fontname_buf buffer->text.fontname_buf
816 rb->snprintf( buffer->text.old_font, MAX_PATH,
817 FONT_DIR "/%s.fnt",
818 rb->global_settings->font_file );
820 while( 1 )
822 if( !b_need_redraw )
824 /* we don't need to redraw ... but we need to unselect
825 * the previously selected item */
826 cp = top_inside + LINE_SPACE;
827 for( i = 0; i+fvi < osi; i++ )
829 cp += fh_buf[i] + LINE_SPACE;
831 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
832 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
833 rb->lcd_set_drawmode(DRMODE_SOLID);
836 if( b_need_redraw )
838 b_need_redraw = 0;
840 d = rb->opendir( FONT_DIR "/" );
841 if( !d )
843 return false;
845 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
846 i = 0;
847 li = -1;
848 while( i < fvi )
850 rb->readdir( d );
851 i++;
853 cp = top_inside+LINE_SPACE;
855 rb->lcd_set_foreground(COLOR_BLACK);
856 rb->lcd_set_background(COLOR_LIGHTGRAY);
858 while( cp < top+HEIGHT )
860 de = rb->readdir( d );
861 if( !de )
863 li = i-1;
864 break;
866 if( !check_extention( de->d_name, ".fnt" ) )
867 continue;
868 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
869 de->d_name );
870 rb->font_load(NULL, bbuf );
871 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
872 if( nvih > 0 )
874 nvih -= fh;
875 fvi++;
876 if( nvih < 0 ) nvih = 0;
877 i++;
878 continue;
880 if( cp + fh >= top+HEIGHT )
882 nvih = fh;
883 break;
885 rb->lcd_putsxy( left+10, cp, de->d_name );
886 fh_buf[i-fvi] = fh;
887 fw_buf[i-fvi] = fw;
888 cp += fh + LINE_SPACE;
889 rb->strcpy( fontname_buf[i-fvi], bbuf );
890 i++;
892 lvi = i-1;
893 if( li == -1 )
895 if( !(de = rb->readdir( d ) ) )
897 li = lvi;
899 else if( !nvih && check_extention( de->d_name, ".fnt" ) )
901 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
902 de->d_name );
903 rb->font_load(NULL, bbuf );
904 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
905 nvih = fh;
908 rb->font_load(NULL, buffer->text.old_font );
909 rb->closedir( d );
912 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
913 cp = top_inside + LINE_SPACE;
914 for( i = 0; i+fvi < si; i++ )
916 cp += fh_buf[i] + LINE_SPACE;
918 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
919 rb->lcd_set_drawmode(DRMODE_SOLID);
921 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
923 osi = si;
924 i = fvi;
925 switch( rb->button_get(true) )
927 case ROCKPAINT_UP:
928 case ROCKPAINT_UP|BUTTON_REPEAT:
929 if( si > 0 )
931 si--;
932 if( si<fvi )
934 fvi = si;
937 break;
939 case ROCKPAINT_DOWN:
940 case ROCKPAINT_DOWN|BUTTON_REPEAT:
941 if( li == -1 || si < li )
943 si++;
945 break;
947 case ROCKPAINT_LEFT:
948 return false;
950 case ROCKPAINT_RIGHT:
951 case ROCKPAINT_DRAW:
952 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
953 return true;
956 if( i != fvi || si > lvi )
958 b_need_redraw = 1;
961 if( si<=lvi )
963 nvih = 0;
966 #undef fh_buf
967 #undef fw_buf
968 #undef fontname_buf
969 #undef WIDTH
970 #undef HEIGHT
971 #undef LINE_SPACE
974 /***********************************************************************
975 * HSVRGB Color chooser
976 ***********************************************************************/
977 static unsigned int color_chooser( unsigned int color )
979 int red = RGB_UNPACK_RED( color );
980 int green = RGB_UNPACK_GREEN( color );
981 int blue = RGB_UNPACK_BLUE( color );
982 int hue, saturation, value;
983 int r, g, b; /* temp variables */
984 int i, top, left;
986 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
987 Red = 3, Green = 4, Blue = 5 };
988 enum BaseColor current = Red;
989 bool has_changed;
991 restore_screen();
993 rgb2hsv( red, green, blue, &hue, &saturation, &value );
995 while( 1 )
997 has_changed = false;
998 color = LCD_RGBPACK( red, green, blue );
1000 #define HEIGHT ( 100 )
1001 #define WIDTH ( 150 )
1003 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
1004 top -= 15;
1006 for( i=0; i<100; i++ )
1008 hsv2rgb( i*36, saturation, value, &r, &g, &b );
1009 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1010 rb->lcd_vline( left+15+i, top+20, top+27 );
1011 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
1012 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1013 rb->lcd_vline( left+15+i, top+30, top+37 );
1014 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
1015 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1016 rb->lcd_vline( left+15+i, top+40, top+47 );
1017 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
1018 rb->lcd_vline( left+15+i, top+50, top+57 );
1019 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
1020 rb->lcd_vline( left+15+i, top+60, top+67 );
1021 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
1022 rb->lcd_vline( left+15+i, top+70, top+77 );
1025 rb->lcd_set_foreground(COLOR_BLACK);
1026 #define POSITION( a, i ) \
1027 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
1028 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
1029 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
1030 rb->lcd_drawpixel( left+16+i, top + 28 + a );
1031 POSITION( 0, hue/36 );
1032 POSITION( 10, saturation*99/255 );
1033 POSITION( 20, value*99/255 );
1034 POSITION( 30, red*99/255 );
1035 POSITION( 40, green*99/255 );
1036 POSITION( 50, blue*99/255 );
1037 #undef POSITION
1038 rb->lcd_set_background(COLOR_LIGHTGRAY);
1039 rb->lcd_setfont( FONT_SYSFIXED );
1040 rb->lcd_putsxyf( left + 117, top + 20, "%d", hue/10 );
1041 rb->lcd_putsxyf( left + 117, top + 30, "%d.%d",
1042 saturation/255, ((saturation*100)/255)%100 );
1043 rb->lcd_putsxyf( left + 117, top + 40, "%d.%d",
1044 value/255, ((value*100)/255)%100 );
1045 rb->lcd_putsxyf( left + 117, top + 50, "%d", red );
1046 rb->lcd_putsxyf( left + 117, top + 60, "%d", green );
1047 rb->lcd_putsxyf( left + 117, top + 70, "%d", blue );
1048 rb->lcd_setfont( FONT_UI );
1050 #define CURSOR( l ) \
1051 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1052 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1053 CURSOR( 5 );
1054 #undef CURSOR
1056 rb->lcd_set_foreground( color );
1057 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1059 rb->lcd_update();
1061 switch( rb->button_get(true) )
1063 case ROCKPAINT_UP:
1064 current = ( current + 5 )%6;
1065 break;
1067 case ROCKPAINT_DOWN:
1068 current = (current + 1 )%6;
1069 break;
1071 case ROCKPAINT_LEFT:
1072 has_changed = true;
1073 switch( current )
1075 case Hue:
1076 hue = ( hue + 3600 - 10 )%3600;
1077 break;
1078 case Saturation:
1079 if( saturation ) saturation--;
1080 break;
1081 case Value:
1082 if( value ) value--;
1083 break;
1084 case Red:
1085 if( red ) red--;
1086 break;
1087 case Green:
1088 if( green ) green--;
1089 break;
1090 case Blue:
1091 if( blue ) blue--;
1092 break;
1094 break;
1096 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1097 has_changed = true;
1098 switch( current )
1100 case Hue:
1101 hue = ( hue + 3600 - 100 )%3600;
1102 break;
1103 case Saturation:
1104 if( saturation >= 8 ) saturation-=8;
1105 else saturation = 0;
1106 break;
1107 case Value:
1108 if( value >= 8 ) value-=8;
1109 else value = 0;
1110 break;
1111 case Red:
1112 if( red >= 8 ) red-=8;
1113 else red = 0;
1114 break;
1115 case Green:
1116 if( green >= 8 ) green-=8;
1117 else green = 0;
1118 break;
1119 case Blue:
1120 if( blue >= 8 ) blue-=8;
1121 else blue = 0;
1122 break;
1124 break;
1126 case ROCKPAINT_RIGHT:
1127 has_changed = true;
1128 switch( current )
1130 case Hue:
1131 hue = ( hue + 10 )%3600;
1132 break;
1133 case Saturation:
1134 if( saturation < 0xff ) saturation++;
1135 break;
1136 case Value:
1137 if( value < 0xff ) value++;
1138 break;
1139 case Red:
1140 if( red < 0xff ) red++;
1141 break;
1142 case Green:
1143 if( green < 0xff ) green++;
1144 break;
1145 case Blue:
1146 if( blue < 0xff ) blue++;
1147 break;
1149 break;
1151 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1152 has_changed = true;
1153 switch( current )
1155 case Hue:
1156 hue = ( hue + 100 )%3600;
1157 break;
1158 case Saturation:
1159 if( saturation < 0xff - 8 ) saturation+=8;
1160 else saturation = 0xff;
1161 break;
1162 case Value:
1163 if( value < 0xff - 8 ) value+=8;
1164 else value = 0xff;
1165 break;
1166 case Red:
1167 if( red < 0xff - 8 ) red+=8;
1168 else red = 0xff;
1169 break;
1170 case Green:
1171 if( green < 0xff - 8 ) green+=8;
1172 else green = 0xff;
1173 break;
1174 case Blue:
1175 if( blue < 0xff - 8 ) blue+=8;
1176 else blue = 0xff;
1177 break;
1179 break;
1181 case ROCKPAINT_DRAW:
1182 return color;
1184 if( has_changed )
1186 switch( current )
1188 case Hue:
1189 case Saturation:
1190 case Value:
1191 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1192 break;
1194 case Red:
1195 case Green:
1196 case Blue:
1197 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1198 break;
1201 #undef HEIGHT
1202 #undef WIDTH
1206 /***********************************************************************
1207 * Misc routines
1208 ***********************************************************************/
1209 static void init_buffer(void)
1211 int i;
1212 fb_data color = rp_colors[ bgdrawcolor ];
1213 for( i = 0; i < ROWS*COLS; i++ )
1215 save_buffer[i] = color;
1219 static void draw_pixel(int x,int y)
1221 if( !preview )
1223 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1224 if( isbg )
1226 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1228 else
1230 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1233 rb->lcd_drawpixel(x,y);
1236 static void color_picker( int x, int y )
1238 if( preview )
1240 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1241 #define PSIZE 12
1242 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1243 if( x >= COLS - PSIZE ) x -= PSIZE + 2;
1244 if( y >= ROWS - PSIZE ) y -= PSIZE + 2;
1245 rb->lcd_drawrect( x + 2, y + 2, PSIZE - 2, PSIZE - 2 );
1246 rb->lcd_set_drawmode(DRMODE_SOLID);
1247 rb->lcd_drawrect( x + 3, y + 3, PSIZE - 4, PSIZE - 4 );
1248 #undef PSIZE
1249 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1251 else
1253 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1257 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1258 /* This is a preview mode only function */
1260 int i,a;
1261 if( x1 > x2 )
1263 i = x1;
1264 x1 = x2;
1265 x2 = i;
1267 if( y1 > y2 )
1269 i = y1;
1270 y1 = y2;
1271 y2 = i;
1273 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1274 i = 0;
1275 for( a = x1; a < x2; a++, i++ )
1276 if( i%2 )
1277 rb->lcd_drawpixel( a, y1 );
1278 for( a = y1; a < y2; a++, i++ )
1279 if( i%2 )
1280 rb->lcd_drawpixel( x2, a );
1281 if( y2 != y1 )
1282 for( a = x2; a > x1; a--, i++ )
1283 if( i%2 )
1284 rb->lcd_drawpixel( a, y2 );
1285 if( x2 != x1 )
1286 for( a = y2; a > y1; a--, i++ )
1287 if( i%2 )
1288 rb->lcd_drawpixel( x1, a );
1289 rb->lcd_set_drawmode(DRMODE_SOLID);
1292 static void copy_to_clipboard( void )
1294 /* This needs to be optimised ... but i'm lazy ATM */
1295 rb->memcpy( buffer->clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1298 /* no preview mode handling atm ... do we need it ? (one if) */
1299 static void draw_invert( int x1, int y1, int x2, int y2 )
1301 int i;
1302 if( x1 > x2 )
1304 i = x1;
1305 x1 = x2;
1306 x2 = i;
1308 if( y1 > y2 )
1310 i = y1;
1311 y1 = y2;
1312 y2 = i;
1315 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1316 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1317 rb->lcd_set_drawmode(DRMODE_SOLID);
1319 for( ; y1<=y2; y1++ )
1321 for( i = x1; i<=x2; i++ )
1323 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1326 /*if( update )*/ rb->lcd_update();
1329 static void draw_hflip( int x1, int y1, int x2, int y2 )
1331 int i;
1332 if( x1 > x2 )
1334 i = x1;
1335 x1 = x2;
1336 x2 = i;
1338 if( y1 > y2 )
1340 i = y1;
1341 y1 = y2;
1342 y2 = i;
1345 copy_to_clipboard();
1347 for( i = 0; i <= y2 - y1; i++ )
1349 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1350 buffer->clipboard+(y2-i)*COLS+x1,
1351 (x2-x1+1)*sizeof( fb_data ) );
1353 restore_screen();
1354 rb->lcd_update();
1357 static void draw_vflip( int x1, int y1, int x2, int y2 )
1359 int i;
1360 if( x1 > x2 )
1362 i = x1;
1363 x1 = x2;
1364 x2 = i;
1366 if( y1 > y2 )
1368 i = y1;
1369 y1 = y2;
1370 y2 = i;
1373 copy_to_clipboard();
1375 for( ; y1 <= y2; y1++ )
1377 for( i = 0; i <= x2 - x1; i++ )
1379 save_buffer[y1*COLS+x1+i] = buffer->clipboard[y1*COLS+x2-i];
1382 restore_screen();
1383 rb->lcd_update();
1386 /* direction: -1 = left, 1 = right */
1387 static void draw_rot_90_deg( int x1, int y1, int x2, int y2, int direction )
1389 int i, j;
1390 if( x1 > x2 )
1392 i = x1;
1393 x1 = x2;
1394 x2 = i;
1396 if( y1 > y2 )
1398 i = y1;
1399 y1 = y2;
1400 y2 = i;
1403 copy_to_clipboard();
1405 fb_data color = rp_colors[ bgdrawcolor ];
1406 const int width = x2 - x1, height = y2 - y1;
1407 const int sub_half = width/2-height/2, add_half = (width+height)/2;
1408 if( width > height )
1410 for( i = 0; i <= height; i++ )
1412 for( j = 0; j < sub_half; j++ )
1413 save_buffer[(y1+i)*COLS+x1+j] = color;
1414 for( j = add_half+1; j <= width; j++ )
1415 save_buffer[(y1+i)*COLS+x1+j] = color;
1418 else if( width < height )
1420 for( j = 0; j <= width; j++ )
1422 for( i = 0; i < -sub_half; i++ )
1423 save_buffer[(y1+i)*COLS+x1+j] = color;
1424 for( i = add_half+1; i <= height; i++ )
1425 save_buffer[(y1+i)*COLS+x1+j] = color;
1428 int x3 = x1 + sub_half, y3 = y1 - sub_half;
1429 int is = x3<0?-x3:0, ie = COLS-x3-1, js = y3<0?-y3:0, je = ROWS-y3-1;
1430 if( ie > height ) ie = height;
1431 if( je > width ) je = width;
1432 for( i = is; i <= ie; i++ )
1434 for( j = js; j <= je; j++ )
1436 int x, y;
1437 if(direction > 0)
1439 x = x1+j;
1440 y = y1+height-i;
1442 else
1444 x = x1+width-j;
1445 y = y1+i;
1447 save_buffer[(y3+j)*COLS+x3+i] = buffer->clipboard[y*COLS+x];
1450 restore_screen();
1451 rb->lcd_update();
1454 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1455 int src_y2, int x1, int y1, int mode )
1457 int i, width, height;
1458 if( mode == SELECT_MENU_CUT )
1460 i = drawcolor;
1461 drawcolor = bgdrawcolor;
1462 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1463 drawcolor = i;
1465 if( src_x1 > src_x2 )
1467 i = src_x1;
1468 src_x1 = src_x2;
1469 src_x2 = i;
1471 if( src_y1 > src_y2 )
1473 i = src_y1;
1474 src_y1 = src_y2;
1475 src_y2 = i;
1477 width = src_x2 - src_x1 + 1;
1478 height = src_y2 - src_y1 + 1;
1479 /* clipping */
1480 if( x1 + width > COLS )
1481 width = COLS - x1;
1482 if( y1 + height > ROWS )
1483 height = ROWS - y1;
1485 rb->lcd_bitmap_part( buffer->clipboard, src_x1, src_y1, COLS,
1486 x1, y1, width, height );
1487 if( !preview )
1489 for( i = 0; i < height; i++ )
1491 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1492 buffer->clipboard+(src_y1+i)*COLS+src_x1,
1493 width*sizeof( fb_data ) );
1498 static void show_grid( bool update )
1500 int i;
1501 if( gridsize > 0 )
1503 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1504 for( i = gridsize; i < img_width; i+= gridsize )
1506 rb->lcd_vline( i, 0, img_height-1 );
1508 for( i = gridsize; i < img_height; i+= gridsize )
1510 rb->lcd_hline( 0, img_width-1, i );
1512 rb->lcd_set_drawmode(DRMODE_SOLID);
1513 if( update ) rb->lcd_update();
1517 static void draw_text( int x, int y )
1519 int selected = 0;
1520 buffer->text.text[0] = '\0';
1521 rb->snprintf( buffer->text.old_font, MAX_PATH,
1522 FONT_DIR "/%s.fnt",
1523 rb->global_settings->font_file );
1524 while( 1 )
1526 switch( rb->do_menu( &text_menu, &selected, NULL, NULL ) )
1528 case TEXT_MENU_TEXT:
1529 rb->lcd_set_foreground(COLOR_BLACK);
1530 rb->kbd_input( buffer->text.text, MAX_TEXT );
1531 break;
1533 case TEXT_MENU_FONT:
1534 if( browse_fonts( buffer->text.font, MAX_PATH ) )
1536 rb->font_load(NULL, buffer->text.font );
1538 break;
1540 case TEXT_MENU_PREVIEW:
1541 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1542 while( 1 )
1544 int button;
1545 restore_screen();
1546 rb->lcd_putsxy( x, y, buffer->text.text );
1547 rb->lcd_update();
1548 switch( button = rb->button_get( true ) )
1550 case ROCKPAINT_LEFT:
1551 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1552 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1553 if (x<0) x=COLS-1;
1554 break;
1556 case ROCKPAINT_RIGHT:
1557 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1558 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1559 if (x>=COLS) x=0;
1560 break;
1562 case ROCKPAINT_UP:
1563 case ROCKPAINT_UP | BUTTON_REPEAT:
1564 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1565 if (y<0) y=ROWS-1;
1566 break;
1568 case ROCKPAINT_DOWN:
1569 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1570 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1571 if (y>=ROWS-1) y=0;
1572 break;
1574 case ROCKPAINT_DRAW:
1575 break;
1576 default:
1577 if(rb->default_event_handler(button)
1578 == SYS_USB_CONNECTED)
1579 button = ROCKPAINT_DRAW;
1580 break;
1582 if( button == ROCKPAINT_DRAW ) break;
1584 break;
1586 case TEXT_MENU_APPLY:
1587 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1588 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1589 buffer->text.text );
1590 case TEXT_MENU_CANCEL:
1591 default:
1592 restore_screen();
1593 rb->font_load(NULL, buffer->text.old_font );
1594 return;
1599 static void draw_brush( int x, int y )
1601 int i,j;
1602 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1604 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1606 draw_pixel( x+i, y+j );
1611 /* This is an implementation of Bresenham's line algorithm.
1612 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1614 static void draw_line( int x1, int y1, int x2, int y2 )
1616 int x = x1;
1617 int y = y1;
1618 int deltax = x2 - x1;
1619 int deltay = y2 - y1;
1620 int i;
1622 int xerr = abs(deltax);
1623 int yerr = abs(deltay);
1624 int xstep = deltax > 0 ? 1 : -1;
1625 int ystep = deltay > 0 ? 1 : -1;
1626 int err;
1628 if (yerr > xerr)
1630 /* more vertical */
1631 err = yerr;
1632 xerr <<= 1;
1633 yerr <<= 1;
1635 /* to leave off the last pixel of the line, leave off the "+ 1" */
1636 for (i = abs(deltay) + 1; i; --i)
1638 draw_pixel(x, y);
1639 y += ystep;
1640 err -= xerr;
1641 if (err < 0) {
1642 x += xstep;
1643 err += yerr;
1647 else
1649 /* more horizontal */
1650 err = xerr;
1651 xerr <<= 1;
1652 yerr <<= 1;
1654 for (i = abs(deltax) + 1; i; --i)
1656 draw_pixel(x, y);
1657 x += xstep;
1658 err -= yerr;
1659 if (err < 0) {
1660 y += ystep;
1661 err += xerr;
1667 static void draw_curve( int x1, int y1, int x2, int y2,
1668 int xa, int ya, int xb, int yb )
1670 int i = 0;
1671 short xl1, yl1;
1672 short xl2, yl2;
1673 short xl3, yl3;
1674 short xl4, yl4;
1675 short xr1, yr1;
1676 short xr2, yr2;
1677 short xr3, yr3;
1678 short xr4, yr4;
1679 short depth;
1680 short xh, yh;
1682 if( x1 == x2 && y1 == y2 )
1684 draw_pixel( x1, y1 );
1685 return;
1688 // if( preview )
1690 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1691 if( xa == -1 || ya == -1 )
1693 rb->lcd_drawline( x1, y1, xb, yb );
1694 rb->lcd_drawline( x2, y2, xb, yb );
1696 else
1698 rb->lcd_drawline( x1, y1, xa, ya );
1699 rb->lcd_drawline( x2, y2, xb, yb );
1701 rb->lcd_set_drawmode(DRMODE_SOLID);
1704 if( xa == -1 || ya == -1 )
1705 /* We only have 3 of the points
1706 * This will currently only be used in preview mode */
1708 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1709 buffer->bezier[i].x1 = a1; \
1710 buffer->bezier[i].y1 = b1; \
1711 buffer->bezier[i].x2 = a2; \
1712 buffer->bezier[i].y2 = b2; \
1713 buffer->bezier[i].x3 = a3; \
1714 buffer->bezier[i].y3 = b3; \
1715 buffer->bezier[i].depth = d; \
1716 i++;
1717 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1718 i--; \
1719 a1 = buffer->bezier[i].x1; \
1720 b1 = buffer->bezier[i].y1; \
1721 a2 = buffer->bezier[i].x2; \
1722 b2 = buffer->bezier[i].y2; \
1723 a3 = buffer->bezier[i].x3; \
1724 b3 = buffer->bezier[i].y3; \
1725 d = buffer->bezier[i].depth;
1726 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1727 while( i )
1729 /* de Casteljau's algorithm (see wikipedia) */
1730 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1731 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1733 xl2 = ( xl1 + xb )>>1;
1734 yl2 = ( yl1 + yb )>>1;
1735 xr2 = ( xb + xr3 )>>1;
1736 yr2 = ( yb + yr3 )>>1;
1737 xr1 = ( xl2 + xr2 )>>1;
1738 yr1 = ( yl2 + yr2 )>>1;
1739 xl3 = xr1;
1740 yl3 = yr1;
1741 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1742 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1744 else
1746 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1747 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1750 #undef PUSH
1751 #undef POP
1753 else /* We have the 4 points */
1755 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1756 buffer->bezier[i].x1 = a1; \
1757 buffer->bezier[i].y1 = b1; \
1758 buffer->bezier[i].x2 = a2; \
1759 buffer->bezier[i].y2 = b2; \
1760 buffer->bezier[i].x3 = a3; \
1761 buffer->bezier[i].y3 = b3; \
1762 buffer->bezier[i].x4 = a4; \
1763 buffer->bezier[i].y4 = b4; \
1764 buffer->bezier[i].depth = d; \
1765 i++;
1766 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1767 i--; \
1768 a1 = buffer->bezier[i].x1; \
1769 b1 = buffer->bezier[i].y1; \
1770 a2 = buffer->bezier[i].x2; \
1771 b2 = buffer->bezier[i].y2; \
1772 a3 = buffer->bezier[i].x3; \
1773 b3 = buffer->bezier[i].y3; \
1774 a4 = buffer->bezier[i].x4; \
1775 b4 = buffer->bezier[i].y4; \
1776 d = buffer->bezier[i].depth;
1778 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1779 while( i )
1781 /* de Casteljau's algorithm (see wikipedia) */
1782 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1783 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1785 xl2 = ( xl1 + xa )>>1;
1786 yl2 = ( yl1 + ya )>>1;
1787 xh = ( xa + xb )>>1;
1788 yh = ( ya + yb )>>1;
1789 xr3 = ( xb + xr4 )>>1;
1790 yr3 = ( yb + yr4 )>>1;
1791 xl3 = ( xl2 + xh )>>1;
1792 yl3 = ( yl2 + yh )>>1;
1793 xr2 = ( xr3 + xh )>>1;
1794 yr2 = ( yr3 + yh )>>1;
1795 xl4 = ( xl3 + xr2 )>>1;
1796 yl4 = ( yl3 + yr2 )>>1;
1797 xr1 = xl4;
1798 yr1 = yl4;
1799 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1800 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1802 else
1804 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1805 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1808 #undef PUSH
1809 #undef POP
1813 static void draw_rect( int x1, int y1, int x2, int y2 )
1815 draw_line( x1, y1, x1, y2 );
1816 draw_line( x1, y1, x2, y1 );
1817 draw_line( x1, y2, x2, y2 );
1818 draw_line( x2, y1, x2, y2 );
1821 static void togglebg( void )
1823 if( isbg )
1825 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1827 else
1829 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1831 isbg = !isbg;
1834 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1836 /* GRUIK */
1837 int x;
1838 togglebg();
1839 if( x1 > x2 )
1841 x = x1;
1842 x1 = x2;
1843 x2 = x;
1845 x = x1;
1846 do {
1847 draw_line( x, y1, x, y2 );
1848 } while( ++x <= x2 );
1849 togglebg();
1850 draw_rect( x1, y1, x2, y2 );
1853 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1855 /* TODO: simplify :) */
1856 int cx = (x1+x2)>>1;
1857 int cy = (y1+y2)>>1;
1859 int rx = (x1-x2)>>1;
1860 int ry = (y1-y2)>>1;
1861 if( rx < 0 ) rx *= -1;
1862 if( ry < 0 ) ry *= -1;
1864 if( rx == 0 || ry == 0 )
1866 draw_line( x1, y1, x2, y2 );
1867 return;
1870 int x,y;
1871 int dst, old_dst;
1873 for( x = 0; x < rx; x++ )
1875 y = 0;
1876 dst = -0xfff;
1877 do {
1878 old_dst = dst;
1879 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1880 y++;
1881 } while( dst < 0 );
1882 if( -old_dst < dst ) y--;
1883 if( full )
1885 draw_line( cx+x, cy, cx+x, cy+y );
1886 draw_line( cx+x, cy, cx+x, cy-y );
1887 draw_line( cx-x, cy, cx-x, cy+y );
1888 draw_line( cx-x, cy, cx-x, cy-y );
1890 else
1892 draw_pixel( cx+x, cy+y );
1893 draw_pixel( cx+x, cy-y );
1894 draw_pixel( cx-x, cy+y );
1895 draw_pixel( cx-x, cy-y );
1898 for( y = 0; y < ry; y++ )
1900 x = 0;
1901 dst = -0xfff;
1902 do {
1903 old_dst = dst;
1904 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1905 x++;
1906 } while( dst < 0 );
1907 if( -old_dst < dst ) x--;
1908 if( full )
1910 draw_line( cx+x, cy, cx+x, cy+y );
1911 draw_line( cx+x, cy, cx+x, cy-y );
1912 draw_line( cx-x, cy, cx-x, cy+y );
1913 draw_line( cx-x, cy, cx-x, cy-y );
1915 else
1917 draw_pixel( cx+x, cy+y );
1918 draw_pixel( cx+x, cy-y );
1919 draw_pixel( cx-x, cy+y );
1920 draw_pixel( cx-x, cy-y );
1925 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1927 draw_oval( x1, y1, x2, y2, false );
1930 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1932 togglebg();
1933 draw_oval( x1, y1, x2, y2, true );
1934 togglebg();
1935 draw_oval( x1, y1, x2, y2, false );
1938 static void draw_fill( int x0, int y0 )
1940 #define PUSH( a, b ) \
1941 draw_pixel( (int)a, (int)b ); \
1942 buffer->coord[i].x = a; \
1943 buffer->coord[i].y = b; \
1944 i++;
1945 #define POP( a, b ) \
1946 i--; \
1947 a = buffer->coord[i].x; \
1948 b = buffer->coord[i].y;
1950 unsigned int i=0;
1951 short x = x0;
1952 short y = y0;
1953 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1955 if( prev_color == rp_colors[ drawcolor ] ) return;
1957 PUSH( x, y );
1959 while( i != 0 )
1961 POP( x, y );
1962 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1964 PUSH( x-1, y );
1966 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1968 PUSH( x+1, y );
1970 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1972 PUSH( x, y-1 );
1974 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1976 PUSH( x, y+1 );
1979 #undef PUSH
1980 #undef POP
1984 /* For preview purposes only */
1985 static void line_gradient( int x1, int y1, int x2, int y2 )
1987 int r1, g1, b1;
1988 int r2, g2, b2;
1989 int h1, s1, v1, h2, s2, v2, r, g, b;
1990 int w, h, x, y;
1992 bool a = false;
1994 x1 <<= 1;
1995 y1 <<= 1;
1996 x2 <<= 1;
1997 y2 <<= 1;
1999 w = x1 - x2;
2000 h = y1 - y2;
2002 if( w == 0 && h == 0 )
2004 draw_pixel( x1>>1, y1>>1 );
2005 return;
2008 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2009 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2010 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2011 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2012 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2013 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2015 if( w < 0 )
2017 w *= -1;
2018 a = true;
2020 if( h < 0 )
2022 h *= -1;
2023 a = !a;
2025 if( a )
2027 r = r1;
2028 r1 = r2;
2029 r2 = r;
2030 g = g1;
2031 g1 = g2;
2032 g2 = g;
2033 b = b1;
2034 b1 = b2;
2035 b2 = b;
2038 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2039 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2041 if( w > h )
2043 if( x1 > x2 )
2045 x = x2;
2046 y = y2;
2047 x2 = x1;
2048 y2 = y1;
2049 x1 = x;
2050 y1 = y;
2052 w = x1 - x2;
2053 h = y1 - y2;
2054 while( x1 <= x2 )
2056 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2057 s1+((s2-s1)*(x1-x2))/w,
2058 v1+((v2-v1)*(x1-x2))/w,
2059 &r, &g, &b );
2060 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2061 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2062 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2063 x1+=2;
2064 y1 = y2 - ( x2 - x1 ) * h / w;
2067 else /* h > w */
2069 if( y1 > y2 )
2071 x = x2;
2072 y = y2;
2073 x2 = x1;
2074 y2 = y1;
2075 x1 = x;
2076 y1 = y;
2078 w = x1 - x2;
2079 h = y1 - y2;
2080 while( y1 <= y2 )
2082 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2083 s1+((s2-s1)*(y1-y2))/h,
2084 v1+((v2-v1)*(y1-y2))/h,
2085 &r, &g, &b );
2086 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2087 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2088 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2089 y1+=2;
2090 x1 = x2 - ( y2 - y1 ) * w / h;
2093 if( a )
2095 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2097 else
2099 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2103 static void linear_gradient( int x1, int y1, int x2, int y2 )
2105 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2106 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2107 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2108 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2109 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2110 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2112 int h1, s1, v1, h2, s2, v2, r, g, b;
2114 /* radius^2 */
2115 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2116 int dist2, i=0;
2118 /* We only propagate the gradient to neighboring pixels with the same
2119 * color as ( x1, y1 ) */
2120 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2122 int x = x1;
2123 int y = y1;
2125 if( radius2 == 0 ) return;
2126 if( preview )
2128 line_gradient( x1, y1, x2, y2 );
2131 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2132 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2134 #define PUSH( x0, y0 ) \
2135 buffer->coord[i].x = (short)(x0); \
2136 buffer->coord[i].y = (short)(y0); \
2137 i++;
2138 #define POP( a, b ) \
2139 i--; \
2140 a = (int)buffer->coord[i].x; \
2141 b = (int)buffer->coord[i].y;
2143 PUSH( x, y );
2145 while( i != 0 )
2147 POP( x, y );
2149 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2150 if( dist2 <= 0 )
2152 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2154 else if( dist2 < radius2 )
2156 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2157 s1+((s2-s1)*dist2)/radius2,
2158 v1+((v2-v1)*dist2)/radius2,
2159 &r, &g, &b );
2160 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2162 else
2164 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2166 if( rp_colors[ drawcolor ] == prev_color )
2168 if( rp_colors[ drawcolor ])
2169 rp_colors[ drawcolor ]--; /* GRUIK */
2170 else
2171 rp_colors[ drawcolor ]++; /* GRUIK */
2173 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2174 draw_pixel( x, y );
2176 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2178 PUSH( x-1, y );
2180 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2182 PUSH( x+1, y );
2184 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2186 PUSH( x, y-1 );
2188 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2190 PUSH( x, y+1 );
2193 #undef PUSH
2194 #undef POP
2196 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2199 static void radial_gradient( int x1, int y1, int x2, int y2 )
2201 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2202 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2203 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2204 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2205 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2206 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2208 int h1, s1, v1, h2, s2, v2, r, g, b;
2210 /* radius^2 */
2211 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2212 int dist2, i=0;
2214 /* We only propagate the gradient to neighboring pixels with the same
2215 * color as ( x1, y1 ) */
2216 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2218 int x = x1;
2219 int y = y1;
2221 if( radius2 == 0 ) return;
2222 if( preview )
2224 line_gradient( x1, y1, x2, y2 );
2227 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2228 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2230 #define PUSH( x0, y0 ) \
2231 buffer->coord[i].x = (short)(x0); \
2232 buffer->coord[i].y = (short)(y0); \
2233 i++;
2234 #define POP( a, b ) \
2235 i--; \
2236 a = (int)buffer->coord[i].x; \
2237 b = (int)buffer->coord[i].y;
2239 PUSH( x, y );
2241 while( i != 0 )
2243 POP( x, y );
2245 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2247 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2248 s1+((s2-s1)*dist2)/radius2,
2249 v1+((v2-v1)*dist2)/radius2,
2250 &r, &g, &b );
2251 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2253 else
2255 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2257 if( rp_colors[ drawcolor ] == prev_color )
2259 if( rp_colors[ drawcolor ])
2260 rp_colors[ drawcolor ]--; /* GRUIK */
2261 else
2262 rp_colors[ drawcolor ]++; /* GRUIK */
2264 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2265 draw_pixel( x, y );
2267 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2269 PUSH( x-1, y );
2271 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2273 PUSH( x+1, y );
2275 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2277 PUSH( x, y-1 );
2279 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2281 PUSH( x, y+1 );
2284 #undef PUSH
2285 #undef POP
2287 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2290 static void draw_toolbars(bool update)
2292 int i;
2293 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2294 rb->lcd_set_background( COLOR_LIGHTGRAY );
2295 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2296 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2297 rb->lcd_set_foreground( COLOR_BLACK );
2298 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2300 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2301 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2302 TB_SC_SIZE, TB_SC_SIZE );
2303 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2304 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2305 TB_SC_SIZE, TB_SC_SIZE );
2306 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2307 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2308 TB_SC_SIZE, TB_SC_SIZE );
2309 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2310 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2311 TB_SC_SIZE, TB_SC_SIZE );
2313 for( i=0; i<18; i++ )
2315 rb->lcd_set_foreground( rp_colors[i] );
2316 rb->lcd_fillrect(
2317 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2318 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2319 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2320 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2321 rb->lcd_drawrect(
2322 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2323 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2324 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2327 #define SEPARATOR( x, y ) \
2328 rb->lcd_set_foreground( COLOR_WHITE ); \
2329 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2330 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2331 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2332 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2334 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2335 TB_TL_WIDTH, TB_TL_HEIGHT );
2336 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2337 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2338 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2339 TB_TL_SIZE, TB_TL_SIZE );
2341 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2343 rb->lcd_setfont( FONT_SYSFIXED );
2344 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2345 rb->lcd_setfont( FONT_UI );
2346 #undef TOP
2348 if( update ) rb->lcd_update();
2351 static void toolbar( void )
2353 int button, i, j;
2354 restore_screen();
2355 draw_toolbars( false );
2356 y = LCD_HEIGHT-TB_HEIGHT/2;
2357 inv_cursor( true );
2358 while( 1 )
2360 switch( button = rb->button_get( true ) )
2362 case ROCKPAINT_DRAW:
2363 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2364 if( y >= TOP + TB_SC_FG_TOP
2365 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2366 && x >= TB_SC_FG_LEFT
2367 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2369 /* click on the foreground color */
2370 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2372 else if( y >= TOP + TB_SC_BG_TOP
2373 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2374 && x >= TB_SC_BG_LEFT
2375 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2377 /* click on the background color */
2378 i = drawcolor;
2379 drawcolor = bgdrawcolor;
2380 bgdrawcolor = i;
2382 else if( y >= TOP + TB_PL_TOP
2383 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2384 && x >= TB_PL_LEFT
2385 && x < TB_PL_LEFT + TB_PL_WIDTH )
2387 /* click on the palette */
2388 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2389 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2390 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2391 break;
2392 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2393 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2394 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2396 else if( y >= TOP+TB_TL_TOP
2397 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2398 && x >= TB_TL_LEFT
2399 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2401 /* click on the tools */
2402 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2403 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2404 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2405 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2406 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2407 tool = i*2+j;
2408 prev_x = -1;
2409 prev_y = -1;
2410 prev_x2 = -1;
2411 prev_y2 = -1;
2412 prev_x3 = -1;
2413 prev_y3 = -1;
2414 preview = false;
2416 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2418 /* menu button */
2419 goto_menu();
2421 #undef TOP
2422 restore_screen();
2423 draw_toolbars( false );
2424 inv_cursor( true );
2425 break;
2427 case ROCKPAINT_LEFT:
2428 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2429 inv_cursor(false);
2430 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2431 if (x<0) x=COLS-1;
2432 inv_cursor(true);
2433 break;
2435 case ROCKPAINT_RIGHT:
2436 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2437 inv_cursor(false);
2438 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2439 if (x>=COLS) x=0;
2440 inv_cursor(true);
2441 break;
2443 case ROCKPAINT_UP:
2444 case ROCKPAINT_UP | BUTTON_REPEAT:
2445 inv_cursor(false);
2446 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2447 if (y<LCD_HEIGHT-TB_HEIGHT)
2449 return;
2451 inv_cursor(true);
2452 break;
2454 case ROCKPAINT_DOWN:
2455 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2456 inv_cursor(false);
2457 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2458 if (y>=LCD_HEIGHT)
2460 y = 0;
2461 return;
2463 inv_cursor(true);
2464 break;
2466 case ROCKPAINT_TOOLBAR:
2467 case ROCKPAINT_TOOLBAR2:
2468 return;
2470 if( quit ) return;
2474 static void inv_cursor(bool update)
2476 rb->lcd_set_foreground(COLOR_BLACK);
2477 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2478 /* cross painting */
2479 rb->lcd_hline(x-4,x+4,y);
2480 rb->lcd_vline(x,y-4,y+4);
2481 rb->lcd_set_foreground(rp_colors[drawcolor]);
2482 rb->lcd_set_drawmode(DRMODE_SOLID);
2484 if( update ) rb->lcd_update();
2487 static void restore_screen(void)
2489 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2490 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2491 rb->lcd_vline( img_width, 0, ROWS );
2492 rb->lcd_hline( 0, COLS, img_height );
2493 rb->lcd_drawpixel( img_width, img_height );
2494 rb->lcd_set_drawmode(DRMODE_SOLID);
2497 static void clear_drawing(void)
2499 init_buffer();
2500 img_height = ROWS;
2501 img_width = COLS;
2502 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2503 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2504 rb->lcd_update();
2507 static void goto_menu(void)
2509 int multi;
2510 int selected = 0;
2512 while( 1 )
2514 switch( rb->do_menu( &main_menu, &selected, NULL, false ) )
2516 case MAIN_MENU_NEW:
2517 clear_drawing();
2518 return;
2520 case MAIN_MENU_LOAD:
2521 if( browse( filename, MAX_PATH, "/" ) )
2523 if( load_bitmap( filename ) <= 0 )
2525 rb->splashf( 1*HZ, "Error while loading %s",
2526 filename );
2527 clear_drawing();
2529 else
2531 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2532 restore_screen();
2533 inv_cursor(true);
2534 return;
2537 break;
2539 case MAIN_MENU_SAVE:
2540 rb->lcd_set_foreground(COLOR_BLACK);
2541 if (!filename[0])
2542 rb->strcpy(filename,"/");
2543 if( !rb->kbd_input( filename, MAX_PATH ) )
2545 if( !check_extention( filename, ".bmp" ) )
2546 rb->strcat(filename, ".bmp");
2547 save_bitmap( filename );
2548 rb->splashf( 1*HZ, "File saved (%s)", filename );
2550 break;
2552 case MAIN_MENU_SET_WIDTH:
2553 rb->set_int( "Set Width", "px", UNIT_INT, &img_width,
2554 NULL, 1, 1, COLS, NULL );
2555 break;
2556 case MAIN_MENU_SET_HEIGHT:
2557 rb->set_int( "Set Height", "px", UNIT_INT, &img_height,
2558 NULL, 1, 1, ROWS, NULL );
2559 break;
2560 case MAIN_MENU_BRUSH_SIZE:
2561 for(multi = 0; multi<4; multi++)
2562 if(bsize == times_list[multi]) break;
2563 rb->set_option( "Brush Size", &multi, INT, times_options, 4, NULL );
2564 if( multi >= 0 )
2565 bsize = times_list[multi];
2566 break;
2568 case MAIN_MENU_BRUSH_SPEED:
2569 for(multi = 0; multi<3; multi++)
2570 if(bspeed == times_list[multi]) break;
2571 rb->set_option( "Brush Speed", &multi, INT, times_options, 3, NULL );
2572 if( multi >= 0 )
2573 bspeed = times_list[multi];
2574 break;
2576 case MAIN_MENU_COLOR:
2577 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2578 break;
2580 case MAIN_MENU_GRID_SIZE:
2581 for(multi = 0; multi<4; multi++)
2582 if(gridsize == gridsize_list[multi]) break;
2583 rb->set_option( "Grid Size", &multi, INT, gridsize_options, 4, NULL );
2584 if( multi >= 0 )
2585 gridsize = gridsize_list[multi];
2586 break;
2588 case MAIN_MENU_PLAYBACK_CONTROL:
2589 playback_control( NULL );
2590 break;
2592 case MAIN_MENU_EXIT:
2593 restore_screen();
2594 quit=true;
2595 return;
2597 case MAIN_MENU_RESUME:
2598 default:
2599 restore_screen();
2600 return;
2601 }/* end switch */
2602 }/* end while */
2605 static void reset_tool( void )
2607 prev_x = -1;
2608 prev_y = -1;
2609 prev_x2 = -1;
2610 prev_y2 = -1;
2611 prev_x3 = -1;
2612 prev_y3 = -1;
2613 tool_mode = -1;
2614 preview = false;
2617 static bool rockpaint_loop( void )
2619 int button=0,i,j;
2620 int accelaration;
2622 x = 10;
2623 toolbar();
2624 x = 0; y = 0;
2625 restore_screen();
2626 inv_cursor(true);
2628 while (!quit) {
2629 button = rb->button_get(true);
2631 if( tool == Brush && prev_x != -1 )
2633 accelaration = 1;
2635 else if( button & BUTTON_REPEAT )
2637 accelaration = 4;
2639 else
2641 accelaration = 1;
2644 switch(button)
2646 case ROCKPAINT_QUIT:
2647 rb->lcd_set_drawmode(DRMODE_SOLID);
2648 return PLUGIN_OK;
2650 case ROCKPAINT_MENU:
2651 inv_cursor(false);
2652 goto_menu();
2653 restore_screen();
2654 inv_cursor(true);
2655 break;
2657 case ROCKPAINT_DRAW:
2658 inv_cursor(false);
2659 switch( tool )
2661 case Brush:
2662 if( prev_x == -1 ) prev_x = 1;
2663 else prev_x = -1;
2664 break;
2666 case SelectRectangle:
2667 case Line:
2668 case Curve:
2669 case Rectangle:
2670 case RectangleFull:
2671 case Oval:
2672 case OvalFull:
2673 case LinearGradient:
2674 case RadialGradient:
2675 /* Curve uses 4 points, others use 2 */
2676 if( prev_x == -1 || prev_y == -1 )
2678 prev_x = x;
2679 prev_y = y;
2680 preview = true;
2682 else if( tool == Curve
2683 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2685 prev_x2 = x;
2686 prev_y2 = y;
2688 else if( tool == SelectRectangle
2689 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2691 tool_mode = rb->do_menu( &select_menu,
2692 NULL, NULL, false );
2693 switch( tool_mode )
2695 case SELECT_MENU_CUT:
2696 case SELECT_MENU_COPY:
2697 prev_x2 = x;
2698 prev_y2 = y;
2699 copy_to_clipboard();
2700 if( prev_x < x ) x = prev_x;
2701 if( prev_y < y ) y = prev_y;
2702 break;
2704 case SELECT_MENU_INVERT:
2705 draw_invert( prev_x, prev_y, x, y );
2706 reset_tool();
2707 break;
2709 case SELECT_MENU_HFLIP:
2710 draw_hflip( prev_x, prev_y, x, y );
2711 reset_tool();
2712 break;
2714 case SELECT_MENU_VFLIP:
2715 draw_vflip( prev_x, prev_y, x, y );
2716 reset_tool();
2717 break;
2719 case SELECT_MENU_ROTATE90:
2720 draw_rot_90_deg( prev_x, prev_y, x, y, 1 );
2721 reset_tool();
2722 break;
2724 case SELECT_MENU_ROTATE180:
2725 draw_hflip( prev_x, prev_y, x, y );
2726 draw_vflip( prev_x, prev_y, x, y );
2727 reset_tool();
2728 break;
2730 case SELECT_MENU_ROTATE270:
2731 draw_rot_90_deg( prev_x, prev_y, x, y, -1 );
2732 reset_tool();
2733 break;
2735 case SELECT_MENU_CANCEL:
2736 reset_tool();
2737 break;
2739 default:
2740 break;
2742 restore_screen();
2744 else if( tool == Curve
2745 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2747 prev_x3 = x;
2748 prev_y3 = y;
2750 else
2752 preview = false;
2753 switch( tool )
2755 case SelectRectangle:
2756 draw_paste_rectangle( prev_x, prev_y,
2757 prev_x2, prev_y2,
2758 x, y, tool_mode );
2759 break;
2760 case Line:
2761 draw_line( prev_x, prev_y, x, y );
2762 break;
2763 case Curve:
2764 draw_curve( prev_x, prev_y,
2765 prev_x2, prev_y2,
2766 prev_x3, prev_y3,
2767 x, y );
2768 break;
2769 case Rectangle:
2770 draw_rect( prev_x, prev_y, x, y );
2771 break;
2772 case RectangleFull:
2773 draw_rect_full( prev_x, prev_y, x, y );
2774 break;
2775 case Oval:
2776 draw_oval_empty( prev_x, prev_y, x, y );
2777 break;
2778 case OvalFull:
2779 draw_oval_full( prev_x, prev_y, x, y );
2780 break;
2781 case LinearGradient:
2782 linear_gradient( prev_x, prev_y, x, y );
2783 break;
2784 case RadialGradient:
2785 radial_gradient( prev_x, prev_y, x, y );
2786 break;
2787 default:
2788 break;
2790 reset_tool();
2791 restore_screen();
2793 break;
2795 case Fill:
2796 draw_fill( x, y );
2797 restore_screen();
2798 break;
2800 case ColorPicker:
2801 color_picker( x, y );
2802 break;
2804 case Text:
2805 draw_text( x, y );
2806 break;
2808 default:
2809 break;
2811 inv_cursor(true);
2812 break;
2814 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2815 if( tool == Curve )
2817 /* 3 point bezier curve */
2818 preview = false;
2819 draw_curve( prev_x, prev_y,
2820 prev_x2, prev_y2,
2821 -1, -1,
2822 x, y );
2823 reset_tool();
2824 restore_screen();
2825 inv_cursor( true );
2827 break;
2829 case ROCKPAINT_TOOLBAR:
2830 i = x; j = y;
2831 x = 10;
2832 toolbar();
2833 x = i; y = j;
2834 restore_screen();
2835 inv_cursor(true);
2836 break;
2838 case ROCKPAINT_TOOLBAR2:
2839 i = x; j = y;
2840 x = 110;
2841 toolbar();
2842 x = i; y = j;
2843 restore_screen();
2844 inv_cursor(true);
2845 break;
2847 case ROCKPAINT_LEFT:
2848 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2849 inv_cursor(false);
2850 x-=bspeed * accelaration;
2851 if (x<0) x=COLS-1;
2852 inv_cursor(true);
2853 break;
2855 case ROCKPAINT_RIGHT:
2856 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2857 inv_cursor(false);
2858 x+=bspeed * accelaration;
2859 if (x>=COLS) x=0;
2860 inv_cursor(true);
2861 break;
2863 case ROCKPAINT_UP:
2864 case ROCKPAINT_UP | BUTTON_REPEAT:
2865 inv_cursor(false);
2866 y-=bspeed * accelaration;
2867 if (y<0) y=ROWS-1;
2868 inv_cursor(true);
2869 break;
2871 case ROCKPAINT_DOWN:
2872 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2873 inv_cursor(false);
2874 y+=bspeed * accelaration;
2875 if (y>=ROWS)
2877 toolbar();
2878 restore_screen();
2880 inv_cursor(true);
2881 break;
2883 default:
2884 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2885 return PLUGIN_USB_CONNECTED;
2886 break;
2888 if( tool == Brush && prev_x == 1 )
2890 inv_cursor(false);
2891 draw_brush( x, y );
2892 inv_cursor(true);
2894 if( preview || tool == ColorPicker )
2895 /* always preview color picker */
2897 restore_screen();
2898 switch( tool )
2900 case SelectRectangle:
2901 if( prev_x2 == -1 || prev_y2 == -1 )
2903 /* we are defining the selection */
2904 draw_select_rectangle( prev_x, prev_y, x, y );
2906 else
2908 /* we are pasting the selected data */
2909 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2910 prev_y2, x, y, tool_mode );
2911 prev_x3 = prev_x2-prev_x;
2912 if( prev_x3 < 0 ) prev_x3 *= -1;
2913 prev_y3 = prev_y2-prev_y;
2914 if( prev_y3 < 0 ) prev_y3 *= -1;
2915 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2916 prev_x3 = -1;
2917 prev_y3 = -1;
2919 break;
2921 case Brush:
2922 break;
2924 case Line:
2925 draw_line( prev_x, prev_y, x, y );
2926 break;
2928 case Curve:
2929 if( prev_x2 == -1 || prev_y2 == -1 )
2931 draw_line( prev_x, prev_y, x, y );
2933 else
2935 draw_curve( prev_x, prev_y,
2936 prev_x2, prev_y2,
2937 prev_x3, prev_y3,
2938 x, y );
2940 break;
2942 case Rectangle:
2943 draw_rect( prev_x, prev_y, x, y );
2944 break;
2946 case RectangleFull:
2947 draw_rect_full( prev_x, prev_y, x, y );
2948 break;
2950 case Oval:
2951 draw_oval_empty( prev_x, prev_y, x, y );
2952 break;
2954 case OvalFull:
2955 draw_oval_full( prev_x, prev_y, x, y );
2956 break;
2958 case Fill:
2959 break;
2961 case ColorPicker:
2962 preview = true;
2963 color_picker( x, y );
2964 preview = false;
2965 break;
2967 case LinearGradient:
2968 line_gradient( prev_x, prev_y, x, y );
2969 break;
2971 case RadialGradient:
2972 line_gradient( prev_x, prev_y, x, y );
2973 break;
2975 case Text:
2976 default:
2977 break;
2979 inv_cursor( true );
2981 if( gridsize > 0 )
2983 show_grid( true );
2984 show_grid( false );
2988 return PLUGIN_OK;
2991 static int load_bitmap( const char *file )
2993 struct bitmap bm;
2994 bool ret;
2995 int i, j;
2996 fb_data color = rp_colors[ bgdrawcolor ];
2998 bm.data = (char*)save_buffer;
2999 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
3000 FORMAT_NATIVE, NULL );
3002 if((bm.width > COLS ) || ( bm.height > ROWS ))
3003 return -1;
3005 img_width = bm.width;
3006 img_height = bm.height;
3007 for( i = bm.height-1; i >= 0; i-- )
3009 rb->memmove( save_buffer+i*COLS, save_buffer+i*bm.width,
3010 sizeof( fb_data )*bm.width );
3011 for( j = bm.width; j < COLS; j++ )
3012 save_buffer[j+i*COLS] = color;
3014 for( i = bm.height*COLS; i < ROWS*COLS; i++ )
3015 save_buffer[i] = color;
3017 return ret;
3020 static int save_bitmap( char *file )
3022 struct bitmap bm;
3023 int i;
3024 for(i = 0; i < img_height; i++)
3026 rb->memcpy( buffer->clipboard+i*img_width, save_buffer+i*COLS,
3027 sizeof( fb_data )*img_width );
3029 bm.data = (char*)buffer->clipboard;
3030 bm.height = img_height;
3031 bm.width = img_width;
3032 bm.format = FORMAT_NATIVE;
3033 return save_bmp_file( file, &bm );
3036 enum plugin_status plugin_start(const void* parameter)
3038 size_t buffer_size;
3039 buffer = (buf*) (((uintptr_t)rb->plugin_get_buffer(&buffer_size) + 3) & ~3);
3040 if (buffer_size < sizeof(*buffer) + 3)
3042 /* steal from audiobuffer if plugin buffer is too small */
3043 buffer = (buf*)
3044 (((uintptr_t)rb->plugin_get_audio_buffer(&buffer_size) + 3) & ~3);
3046 if (buffer_size < sizeof(*buffer) + 3)
3048 rb->splash(HZ, "Not enough memory");
3049 return PLUGIN_ERROR;
3053 rb->lcd_set_foreground(COLOR_WHITE);
3054 rb->lcd_set_backdrop(NULL);
3055 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
3056 rb->splash( HZ/2, "Rock Paint");
3058 rb->lcd_clear_display();
3060 filename[0] = '\0';
3062 if( parameter )
3064 if( load_bitmap( parameter ) <= 0 )
3066 rb->splash( 1*HZ, "File Open Error");
3067 clear_drawing();
3069 else
3071 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
3072 restore_screen();
3073 rb->strcpy( filename, parameter );
3076 else
3078 clear_drawing();
3080 inv_cursor(true);
3082 return rockpaint_loop();