Use albumart opt in sliding puzzle manual
[kugel-rb.git] / apps / plugins / rockpaint.c
blobae28258e3bb5a08469b36a1e08687fa0b9bd2748
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 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
191 #define ROCKPAINT_QUIT BUTTON_REC
192 #define ROCKPAINT_DRAW BUTTON_PLAY
193 #define ROCKPAINT_MENU BUTTON_MENU
194 #define ROCKPAINT_TOOLBAR BUTTON_OK
195 #define ROCKPAINT_TOOLBAR2 BUTTON_CANCEL
196 #define ROCKPAINT_UP BUTTON_UP
197 #define ROCKPAINT_DOWN BUTTON_DOWN
198 #define ROCKPAINT_LEFT BUTTON_PREV
199 #define ROCKPAINT_RIGHT BUTTON_NEXT
201 #else
202 #error "Please define keys for this keypad"
203 #endif
205 #ifdef HAVE_TOUCHSCREEN
206 #ifndef ROCKPAINT_QUIT
207 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
208 #endif
209 #ifndef ROCKPAINT_DRAW
210 #define ROCKPAINT_DRAW BUTTON_CENTER
211 #endif
212 #ifndef ROCKPAINT_MENU
213 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
214 #endif
215 #ifndef ROCKPAINT_TOOLBAR
216 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
217 #endif
218 #ifndef ROCKPAINT_TOOLBAR2
219 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
220 #endif
221 #ifndef ROCKPAINT_UP
222 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
223 #endif
224 #ifndef ROCKPAINT_DOWN
225 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
226 #endif
227 #ifndef ROCKPAINT_LEFT
228 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
229 #endif
230 #ifndef ROCKPAINT_RIGHT
231 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
232 #endif
233 #endif
235 /***********************************************************************
236 * Palette Default Colors
237 ***********************************************************************/
238 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
239 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
240 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
241 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
242 #define COLOR_RED LCD_RGBPACK(128,0,0)
243 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
244 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
245 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
246 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
247 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
248 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
249 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
250 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
251 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
252 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
253 #define COLOR_PINK LCD_RGBPACK(255,0,255)
254 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
255 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
257 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
258 #define ROCKPAINT_TITLE_FONT 2
260 /***********************************************************************
261 * Program Colors
262 ***********************************************************************/
263 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
264 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
266 #define ROWS LCD_HEIGHT
267 #define COLS LCD_WIDTH
270 * Toolbar positioning stuff ... don't read this unless you really need to
272 * TB Toolbar
273 * SP Separator
274 * SC Selected Color
275 * PL Palette
276 * TL Tools
279 /* Separator sizes */
280 #define TB_SP_MARGIN 3
281 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
283 /* Selected color sizes */
284 #define TB_SC_SIZE 12
286 /* Palette sizes */
287 #define TB_PL_COLOR_SIZE 7
288 #define TB_PL_COLOR_SPACING 2
289 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
290 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
292 /* Tools sizes */
293 #define TB_TL_SIZE 8
294 #define TB_TL_SPACING 2
295 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
296 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
298 /* Menu button size ... gruik */
299 #define TB_MENU_MIN_WIDTH 30
301 /* Selected colors position */
302 #define TB_SC_FG_TOP 2
303 #define TB_SC_FG_LEFT 2
304 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
305 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
307 /* Palette position */
308 #define TB_PL_TOP TB_SC_FG_TOP
309 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
311 /* Tools position */
312 #define TB_TL_TOP TB_SC_FG_TOP
313 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
315 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
316 #undef TB_TL_TOP
317 #undef TB_TL_LEFT
318 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
319 #define TB_TL_LEFT TB_SC_FG_LEFT
320 #endif
322 /* Menu button position */
323 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
324 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
326 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
329 static void draw_pixel(int x,int y);
330 static void draw_line( int x1, int y1, int x2, int y2 );
331 static void draw_rect( int x1, int y1, int x2, int y2 );
332 static void draw_toolbars(bool update);
333 static void inv_cursor(bool update);
334 static void restore_screen(void);
335 static void clear_drawing(void);
336 static void goto_menu(void);
337 static int load_bitmap( const char *filename );
338 static int save_bitmap( char *filename );
339 static void draw_rect_full( int x1, int y1, int x2, int y2 );
341 /***********************************************************************
342 * Global variables
343 ***********************************************************************/
345 static int drawcolor=0; /* Current color (in palette) */
346 static int bgdrawcolor=9; /* Current background color (in palette) */
347 static int img_height = ROWS;
348 static int img_width = COLS;
349 bool isbg = false; /* gruik ugly hack alert */
351 static int preview=false; /* Is preview mode on ? */
353 /* TODO: clean this up */
354 static int x=0, y=0; /* cursor position */
355 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
356 static int prev_x2=-1, prev_y2=-1;
357 static int prev_x3=-1, prev_y3=-1;
358 static int tool_mode=-1;
361 static int bsize=1; /* brush size */
362 static int bspeed=1; /* brush speed */
364 enum Tools { Brush = 0, /* Regular brush */
365 Fill = 1, /* Fill a shape with current color */
366 SelectRectangle = 2,
367 ColorPicker = 3, /* Pick a color */
368 Line = 4, /* Draw a line between two points */
369 Unused = 5, /* THIS IS UNUSED ... */
370 Curve = 6,
371 Text = 7,
372 Rectangle = 8, /* Draw a rectangle */
373 RectangleFull = 9,
374 Oval = 10, /* Draw an oval */
375 OvalFull = 11,
376 LinearGradient = 12,
377 RadialGradient = 13
380 enum Tools tool = Brush;
382 static bool quit=false;
383 static int gridsize=0;
385 static fb_data rp_colors[18] =
387 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
388 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
389 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
390 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
391 COLOR_LIGHTBROWN
394 static fb_data save_buffer[ ROWS*COLS ];
396 extern fb_data rockpaint[];
397 extern fb_data rockpaint_hsvrgb[];
399 /* Maximum string size allowed for the text tool */
400 #define MAX_TEXT 256
402 static union
404 /* Used by fill and gradient algorithms */
405 struct
407 short x;
408 short y;
409 } coord[ ROWS*COLS ];
411 /* Used by bezier curve algorithms */
412 struct
414 short x1, y1;
415 short x2, y2;
416 short x3, y3;
417 short x4, y4;
418 short depth;
419 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
420 * than coord ... so we divide to take
421 * less memory. */
423 /* Used to cut/copy/paste data */
424 fb_data clipboard[ ROWS*COLS ];
426 /* Used for text mode */
427 struct
429 char text[MAX_TEXT];
430 char font[MAX_PATH];
431 char old_font[MAX_PATH];
432 int fh_buf[30];
433 int fw_buf[30];
434 char fontname_buf[30][MAX_PATH];
435 } text;
436 } buffer;
438 /* Current filename */
439 static char filename[MAX_PATH];
441 /* Font preview buffer */
442 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
443 //#define FONT_PREVIEW_HEIGHT 1000
444 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
446 /***********************************************************************
447 * Offscreen buffer/Text/Fonts handling
449 * Parts of code taken from firmware/drivers/lcd-16bit.c
450 ***********************************************************************/
451 static void buffer_mono_bitmap_part(
452 fb_data *buf, int buf_width, int buf_height,
453 const unsigned char *src, int src_x, int src_y,
454 int stride, int x, int y, int width, int height )
455 /* this function only draws the foreground part of the bitmap */
457 const unsigned char *src_end;
458 fb_data *dst, *dst_end;
459 unsigned fgcolor = rb->lcd_get_foreground();
461 /* nothing to draw? */
462 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
463 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
464 return;
466 /* clipping */
467 if( x < 0 )
469 width += x;
470 src_x -= x;
471 x = 0;
473 if( y < 0 )
475 height += y;
476 src_y -= y;
477 y = 0;
479 if( x + width > buf_width )
480 width = buf_width - x;
481 if( y + height > buf_height )
482 height = buf_height - y;
484 src += stride * (src_y >> 3) + src_x; /* move starting point */
485 src_y &= 7;
486 src_end = src + width;
488 dst = buf + y*buf_width + x;
492 const unsigned char *src_col = src++;
493 unsigned data = *src_col >> src_y;
494 fb_data *dst_col = dst++;
495 int numbits = 8 - src_y;
497 dst_end = dst_col + height * buf_width;
500 if( data & 0x01 )
501 *dst_col = fgcolor; /* FIXME ? */
503 dst_col += buf_width;
505 data >>= 1;
506 if( --numbits == 0 )
508 src_col += stride;
509 data = *src_col;
510 numbits = 8;
512 } while( dst_col < dst_end );
513 } while( src < src_end );
516 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
517 int x, int y, int ofs, const unsigned char *str )
519 unsigned short ch;
520 unsigned short *ucs;
522 struct font *pf = rb->font_get( FONT_UI );
523 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
525 ucs = rb->bidi_l2v( str, 1 );
527 while( (ch = *ucs++) != 0 && x < buf_width )
529 int width;
530 const unsigned char *bits;
532 /* get proportional width and glyph bits */
533 width = rb->font_get_width( pf, ch );
535 if( ofs > width )
537 ofs -= width;
538 continue;
541 bits = rb->font_get_bits( pf, ch );
543 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0,
544 width, x, y, width - ofs, pf->height);
546 x += width - ofs;
547 ofs = 0;
551 /***********************************************************************
552 * Menu handling
553 ***********************************************************************/
554 enum {
555 /* Main menu */
556 MAIN_MENU_RESUME,
557 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
558 MAIN_MENU_SET_WIDTH, MAIN_MENU_SET_HEIGHT,
559 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
560 MAIN_MENU_GRID_SIZE,
561 MAIN_MENU_PLAYBACK_CONTROL,
562 MAIN_MENU_EXIT,
564 enum {
565 /* Select action menu */
566 SELECT_MENU_CUT, SELECT_MENU_COPY,
567 SELECT_MENU_INVERT, SELECT_MENU_HFLIP, SELECT_MENU_VFLIP,
568 SELECT_MENU_ROTATE90, SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
569 SELECT_MENU_CANCEL,
571 enum {
572 /* Text menu */
573 TEXT_MENU_TEXT, TEXT_MENU_FONT,
574 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
577 MENUITEM_STRINGLIST(main_menu, "RockPaint", NULL,
578 "Resume", "New", "Load", "Save",
579 "Set Width", "Set Height",
580 "Brush Size", "Brush Speed",
581 "Choose Color", "Grid Size",
582 "Playback Control", "Exit");
583 MENUITEM_STRINGLIST(select_menu, "Select...", NULL,
584 "Cut", "Copy",
585 "Invert", "Horizontal Flip", "Vertical Flip",
586 "Rotate 90°", "Rotate 180°", "Rotate 270°",
587 "Cancel");
588 MENUITEM_STRINGLIST(text_menu, "Text", NULL,
589 "Set Text", "Change Font",
590 "Preview", "Apply", "Cancel");
591 static const int times_list[] = { 1, 2, 4, 8 };
592 static const int gridsize_list[] = { 0, 5, 10, 20 };
593 static const struct opt_items times_options[] = {
594 { "1x", -1 }, { "2x", -1 }, { "4x", -1 }, { "8x", -1 }
596 static const struct opt_items gridsize_options[] = {
597 { "No grid", -1 }, { "5px", -1 }, { "10px", -1 }, { "20px", -1 }
600 static int draw_window( int height, int width,
601 int *top, int *left,
602 const char *title )
604 int fh;
605 rb->lcd_getstringsize( title, NULL, &fh );
606 fh++;
608 const int _top = ( LCD_HEIGHT - height ) / 2;
609 const int _left = ( LCD_WIDTH - width ) / 2;
610 if( top ) *top = _top;
611 if( left ) *left = _left;
612 rb->lcd_set_background(COLOR_BLUE);
613 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
614 rb->lcd_fillrect( _left, _top, width, height );
615 rb->lcd_set_foreground(COLOR_BLUE);
616 rb->lcd_fillrect( _left, _top, width, fh+4 );
617 rb->lcd_set_foreground(COLOR_WHITE);
618 rb->lcd_putsxy( _left+2, _top+2, title );
619 rb->lcd_set_foreground(COLOR_BLACK);
620 rb->lcd_drawrect( _left, _top, width, height );
621 return _top+fh+4;
624 /***********************************************************************
625 * File browser
626 ***********************************************************************/
628 char bbuf[MAX_PATH]; /* used by file and font browsers */
629 char bbuf_s[MAX_PATH]; /* used by file and font browsers */
630 struct tree_context *tree = NULL;
632 static bool check_extention(const char *filename, const char *ext)
634 const char *p = rb->strrchr( filename, '.' );
635 return ( p != NULL && !rb->strcasecmp( p, ext ) );
638 static const char* browse_get_name_cb(int selected_item, void *data,
639 char *buffer, size_t buffer_len)
641 int *indexes = (int *) data;
642 struct entry* dc = tree->dircache;
643 struct entry* e = &dc[indexes[selected_item]];
644 (void) buffer;
645 (void) buffer_len;
647 return e->name;
650 static bool browse( char *dst, int dst_size, const char *start )
652 struct gui_synclist browse_list;
653 int item_count = 0, selected, button;
654 struct tree_context backup;
655 struct entry *dc;
656 bool reload = true;
657 int dirfilter = SHOW_ALL;
658 int *indexes = (int *) buffer.clipboard;
660 char *a;
662 rb->strcpy( bbuf, start );
663 a = bbuf+rb->strlen(bbuf)-1;
664 if( *a != '/' )
666 a[1] = '/';
667 a[2] = '\0';
669 bbuf_s[0] = '\0';
671 rb->gui_synclist_init(&browse_list, browse_get_name_cb,
672 (void*) indexes, false, 1, NULL);
674 tree = rb->tree_get_context();
675 backup = *tree;
676 dc = tree->dircache;
677 a = backup.currdir+rb->strlen(backup.currdir)-1;
678 if( *a != '/' )
680 *++a = '/';
681 *++a = '\0';
683 rb->strcpy( a, dc[tree->selected_item].name );
684 tree->dirfilter = &dirfilter;
685 while( 1 )
687 if( reload )
689 int i;
690 rb->set_current_file(bbuf);
691 item_count = 0;
692 selected = 0;
693 for( i = 0; i < tree->filesindir ; i++)
695 /* only displayes directories and .bmp files */
696 if( ((dc[i].attr & ATTR_DIRECTORY ) &&
697 rb->strcmp( dc[i].name, "." ) &&
698 rb->strcmp( dc[i].name, ".." )) ||
699 ( !(dc[i].attr & ATTR_DIRECTORY) &&
700 check_extention( dc[i].name, ".bmp" ) ) )
702 if( !rb->strcmp( dc[i].name, bbuf_s ) )
703 selected = item_count;
704 indexes[item_count++] = i;
708 rb->gui_synclist_set_nb_items(&browse_list,item_count);
709 rb->gui_synclist_select_item(&browse_list, selected);
710 rb->gui_synclist_set_title(&browse_list, bbuf, NOICON);
711 rb->gui_synclist_draw(&browse_list);
712 reload = false;
714 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
715 if (rb->gui_synclist_do_button(&browse_list,&button,LIST_WRAP_UNLESS_HELD))
716 continue;
717 switch( button )
719 case ACTION_STD_CANCEL:
720 if( !rb->strcmp( bbuf, "/" ) )
722 *tree = backup;
723 rb->set_current_file( backup.currdir );
724 return false;
726 rb->strcpy( bbuf_s, ".." );
727 case ACTION_STD_OK:
728 if( button == ACTION_STD_OK )
730 selected = rb->gui_synclist_get_sel_pos( &browse_list );
731 if( selected < 0 || selected >= item_count )
732 break;
733 struct entry* e = &dc[indexes[selected]];
734 rb->strlcpy( bbuf_s, e->name, sizeof( bbuf_s ) );
735 if( !( e->attr & ATTR_DIRECTORY ) )
737 *tree = backup;
738 rb->set_current_file( backup.currdir );
739 rb->snprintf( dst, dst_size, "%s%s", bbuf, bbuf_s );
740 return true;
743 if( !rb->strcmp( bbuf_s, "." ) ) break;
744 a = bbuf+rb->strlen(bbuf);
745 if( !rb->strcmp( bbuf_s, ".." ) )
747 a--;
748 if( a == bbuf ) break;
749 if( *a == '/' ) a--;
750 while( *a != '/' ) a--;
751 rb->strcpy( bbuf_s, ++a );
752 /* select parent directory */
753 bbuf_s[rb->strlen(bbuf_s)-1] = '\0';
754 *a = '\0';
755 reload = true;
756 break;
758 rb->snprintf( a, bbuf+sizeof(bbuf)-a, "%s/", bbuf_s );
759 reload = true;
760 break;
762 case ACTION_STD_MENU:
763 *tree = backup;
764 rb->set_current_file( backup.currdir );
765 return false;
770 /***********************************************************************
771 * Font browser
773 * FIXME: This still needs some work ... it currently only works fine
774 * on the simulators, disk spins too much on real targets -> rendered
775 * font buffer needed.
776 ***********************************************************************/
777 static bool browse_fonts( char *dst, int dst_size )
779 #define WIDTH ( LCD_WIDTH - 20 )
780 #define HEIGHT ( LCD_HEIGHT - 20 )
781 #define LINE_SPACE 2
782 int top, top_inside = 0, left;
784 DIR *d;
785 struct dirent *de;
786 int fvi = 0; /* first visible item */
787 int lvi = 0; /* last visible item */
788 int si = 0; /* selected item */
789 int osi = 0; /* old selected item */
790 int li = 0; /* last item */
791 int nvih = 0; /* next visible item height */
792 int i;
793 int b_need_redraw = 1; /* Do we need to redraw ? */
795 int cp = 0; /* current position */
796 int fh; /* font height */
798 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
799 #define fw_buf buffer.text.fw_buf
800 int fw;
801 #define fontname_buf buffer.text.fontname_buf
803 rb->snprintf( buffer.text.old_font, MAX_PATH,
804 FONT_DIR "/%s.fnt",
805 rb->global_settings->font_file );
807 while( 1 )
809 if( !b_need_redraw )
811 /* we don't need to redraw ... but we need to unselect
812 * the previously selected item */
813 cp = top_inside + LINE_SPACE;
814 for( i = 0; i+fvi < osi; i++ )
816 cp += fh_buf[i] + LINE_SPACE;
818 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
819 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
820 rb->lcd_set_drawmode(DRMODE_SOLID);
823 if( b_need_redraw )
825 b_need_redraw = 0;
827 d = rb->opendir( FONT_DIR "/" );
828 if( !d )
830 return false;
832 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
833 i = 0;
834 li = -1;
835 while( i < fvi )
837 rb->readdir( d );
838 i++;
840 cp = top_inside+LINE_SPACE;
842 rb->lcd_set_foreground(COLOR_BLACK);
843 rb->lcd_set_background(COLOR_LIGHTGRAY);
845 while( cp < top+HEIGHT )
847 de = rb->readdir( d );
848 if( !de )
850 li = i-1;
851 break;
853 if( !check_extention( de->d_name, ".fnt" ) )
854 continue;
855 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
856 de->d_name );
857 rb->font_load(NULL, bbuf );
858 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
859 if( nvih > 0 )
861 nvih -= fh;
862 fvi++;
863 if( nvih < 0 ) nvih = 0;
864 i++;
865 continue;
867 if( cp + fh >= top+HEIGHT )
869 nvih = fh;
870 break;
872 rb->lcd_putsxy( left+10, cp, de->d_name );
873 fh_buf[i-fvi] = fh;
874 fw_buf[i-fvi] = fw;
875 cp += fh + LINE_SPACE;
876 rb->strcpy( fontname_buf[i-fvi], bbuf );
877 i++;
879 lvi = i-1;
880 if( li == -1 )
882 if( !(de = rb->readdir( d ) ) )
884 li = lvi;
886 else if( !nvih && check_extention( de->d_name, ".fnt" ) )
888 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
889 de->d_name );
890 rb->font_load(NULL, bbuf );
891 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
892 nvih = fh;
895 rb->font_load(NULL, buffer.text.old_font );
896 rb->closedir( d );
899 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
900 cp = top_inside + LINE_SPACE;
901 for( i = 0; i+fvi < si; i++ )
903 cp += fh_buf[i] + LINE_SPACE;
905 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
906 rb->lcd_set_drawmode(DRMODE_SOLID);
908 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
910 osi = si;
911 i = fvi;
912 switch( rb->button_get(true) )
914 case ROCKPAINT_UP:
915 case ROCKPAINT_UP|BUTTON_REPEAT:
916 if( si > 0 )
918 si--;
919 if( si<fvi )
921 fvi = si;
924 break;
926 case ROCKPAINT_DOWN:
927 case ROCKPAINT_DOWN|BUTTON_REPEAT:
928 if( li == -1 || si < li )
930 si++;
932 break;
934 case ROCKPAINT_LEFT:
935 return false;
937 case ROCKPAINT_RIGHT:
938 case ROCKPAINT_DRAW:
939 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
940 return true;
943 if( i != fvi || si > lvi )
945 b_need_redraw = 1;
948 if( si<=lvi )
950 nvih = 0;
953 #undef fh_buf
954 #undef fw_buf
955 #undef fontname_buf
956 #undef WIDTH
957 #undef HEIGHT
958 #undef LINE_SPACE
961 /***********************************************************************
962 * HSVRGB Color chooser
963 ***********************************************************************/
964 static unsigned int color_chooser( unsigned int color )
966 int red = RGB_UNPACK_RED( color );
967 int green = RGB_UNPACK_GREEN( color );
968 int blue = RGB_UNPACK_BLUE( color );
969 int hue, saturation, value;
970 int r, g, b; /* temp variables */
971 int i, top, left;
973 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
974 Red = 3, Green = 4, Blue = 5 };
975 enum BaseColor current = Red;
976 bool has_changed;
978 char str[6] = "";
980 restore_screen();
982 rgb2hsv( red, green, blue, &hue, &saturation, &value );
984 while( 1 )
986 has_changed = false;
987 color = LCD_RGBPACK( red, green, blue );
989 #define HEIGHT ( 100 )
990 #define WIDTH ( 150 )
992 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
993 top -= 15;
995 for( i=0; i<100; i++ )
997 hsv2rgb( i*36, saturation, value, &r, &g, &b );
998 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
999 rb->lcd_vline( left+15+i, top+20, top+27 );
1000 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
1001 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1002 rb->lcd_vline( left+15+i, top+30, top+37 );
1003 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
1004 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1005 rb->lcd_vline( left+15+i, top+40, top+47 );
1006 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
1007 rb->lcd_vline( left+15+i, top+50, top+57 );
1008 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
1009 rb->lcd_vline( left+15+i, top+60, top+67 );
1010 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
1011 rb->lcd_vline( left+15+i, top+70, top+77 );
1014 rb->lcd_set_foreground(COLOR_BLACK);
1015 #define POSITION( a, i ) \
1016 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
1017 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
1018 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
1019 rb->lcd_drawpixel( left+16+i, top + 28 + a );
1020 POSITION( 0, hue/36 );
1021 POSITION( 10, saturation*99/255 );
1022 POSITION( 20, value*99/255 );
1023 POSITION( 30, red*99/255 );
1024 POSITION( 40, green*99/255 );
1025 POSITION( 50, blue*99/255 );
1026 #undef POSITION
1027 rb->lcd_set_background(COLOR_LIGHTGRAY);
1028 rb->lcd_setfont( FONT_SYSFIXED );
1029 rb->snprintf( str, 6, "%d", hue/10 );
1030 rb->lcd_putsxy( left + 117, top + 20, str );
1031 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1032 rb->lcd_putsxy( left + 117, top + 30, str );
1033 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1034 rb->lcd_putsxy( left + 117, top + 40, str );
1035 rb->snprintf( str, 6, "%d", red );
1036 rb->lcd_putsxy( left + 117, top + 50, str );
1037 rb->snprintf( str, 6, "%d", green );
1038 rb->lcd_putsxy( left + 117, top + 60, str );
1039 rb->snprintf( str, 6, "%d", blue );
1040 rb->lcd_putsxy( left + 117, top + 70, str );
1041 rb->lcd_setfont( FONT_UI );
1043 #define CURSOR( l ) \
1044 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1045 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1046 CURSOR( 5 );
1047 #undef CURSOR
1049 rb->lcd_set_foreground( color );
1050 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1052 rb->lcd_update();
1054 switch( rb->button_get(true) )
1056 case ROCKPAINT_UP:
1057 current = ( current + 5 )%6;
1058 break;
1060 case ROCKPAINT_DOWN:
1061 current = (current + 1 )%6;
1062 break;
1064 case ROCKPAINT_LEFT:
1065 has_changed = true;
1066 switch( current )
1068 case Hue:
1069 hue = ( hue + 3600 - 10 )%3600;
1070 break;
1071 case Saturation:
1072 if( saturation ) saturation--;
1073 break;
1074 case Value:
1075 if( value ) value--;
1076 break;
1077 case Red:
1078 if( red ) red--;
1079 break;
1080 case Green:
1081 if( green ) green--;
1082 break;
1083 case Blue:
1084 if( blue ) blue--;
1085 break;
1087 break;
1089 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1090 has_changed = true;
1091 switch( current )
1093 case Hue:
1094 hue = ( hue + 3600 - 100 )%3600;
1095 break;
1096 case Saturation:
1097 if( saturation >= 8 ) saturation-=8;
1098 else saturation = 0;
1099 break;
1100 case Value:
1101 if( value >= 8 ) value-=8;
1102 else value = 0;
1103 break;
1104 case Red:
1105 if( red >= 8 ) red-=8;
1106 else red = 0;
1107 break;
1108 case Green:
1109 if( green >= 8 ) green-=8;
1110 else green = 0;
1111 break;
1112 case Blue:
1113 if( blue >= 8 ) blue-=8;
1114 else blue = 0;
1115 break;
1117 break;
1119 case ROCKPAINT_RIGHT:
1120 has_changed = true;
1121 switch( current )
1123 case Hue:
1124 hue = ( hue + 10 )%3600;
1125 break;
1126 case Saturation:
1127 if( saturation < 0xff ) saturation++;
1128 break;
1129 case Value:
1130 if( value < 0xff ) value++;
1131 break;
1132 case Red:
1133 if( red < 0xff ) red++;
1134 break;
1135 case Green:
1136 if( green < 0xff ) green++;
1137 break;
1138 case Blue:
1139 if( blue < 0xff ) blue++;
1140 break;
1142 break;
1144 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1145 has_changed = true;
1146 switch( current )
1148 case Hue:
1149 hue = ( hue + 100 )%3600;
1150 break;
1151 case Saturation:
1152 if( saturation < 0xff - 8 ) saturation+=8;
1153 else saturation = 0xff;
1154 break;
1155 case Value:
1156 if( value < 0xff - 8 ) value+=8;
1157 else value = 0xff;
1158 break;
1159 case Red:
1160 if( red < 0xff - 8 ) red+=8;
1161 else red = 0xff;
1162 break;
1163 case Green:
1164 if( green < 0xff - 8 ) green+=8;
1165 else green = 0xff;
1166 break;
1167 case Blue:
1168 if( blue < 0xff - 8 ) blue+=8;
1169 else blue = 0xff;
1170 break;
1172 break;
1174 case ROCKPAINT_DRAW:
1175 return color;
1177 if( has_changed )
1179 switch( current )
1181 case Hue:
1182 case Saturation:
1183 case Value:
1184 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1185 break;
1187 case Red:
1188 case Green:
1189 case Blue:
1190 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1191 break;
1194 #undef HEIGHT
1195 #undef WIDTH
1199 /***********************************************************************
1200 * Misc routines
1201 ***********************************************************************/
1202 static void init_buffer(void)
1204 int i;
1205 fb_data color = rp_colors[ bgdrawcolor ];
1206 for( i = 0; i < ROWS*COLS; i++ )
1208 save_buffer[i] = color;
1212 static void draw_pixel(int x,int y)
1214 if( !preview )
1216 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1217 if( isbg )
1219 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1221 else
1223 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1226 rb->lcd_drawpixel(x,y);
1229 static void color_picker( int x, int y )
1231 if( preview )
1233 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1234 #define PSIZE 12
1235 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1236 if( x >= COLS - PSIZE ) x -= PSIZE + 2;
1237 if( y >= ROWS - PSIZE ) y -= PSIZE + 2;
1238 rb->lcd_drawrect( x + 2, y + 2, PSIZE - 2, PSIZE - 2 );
1239 rb->lcd_set_drawmode(DRMODE_SOLID);
1240 rb->lcd_drawrect( x + 3, y + 3, PSIZE - 4, PSIZE - 4 );
1241 #undef PSIZE
1242 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1244 else
1246 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1250 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1251 /* This is a preview mode only function */
1253 int i,a;
1254 if( x1 > x2 )
1256 i = x1;
1257 x1 = x2;
1258 x2 = i;
1260 if( y1 > y2 )
1262 i = y1;
1263 y1 = y2;
1264 y2 = i;
1266 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1267 i = 0;
1268 for( a = x1; a < x2; a++, i++ )
1269 if( i%2 )
1270 rb->lcd_drawpixel( a, y1 );
1271 for( a = y1; a < y2; a++, i++ )
1272 if( i%2 )
1273 rb->lcd_drawpixel( x2, a );
1274 if( y2 != y1 )
1275 for( a = x2; a > x1; a--, i++ )
1276 if( i%2 )
1277 rb->lcd_drawpixel( a, y2 );
1278 if( x2 != x1 )
1279 for( a = y2; a > y1; a--, i++ )
1280 if( i%2 )
1281 rb->lcd_drawpixel( x1, a );
1282 rb->lcd_set_drawmode(DRMODE_SOLID);
1285 static void copy_to_clipboard( void )
1287 /* This needs to be optimised ... but i'm lazy ATM */
1288 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1291 /* no preview mode handling atm ... do we need it ? (one if) */
1292 static void draw_invert( int x1, int y1, int x2, int y2 )
1294 int i;
1295 if( x1 > x2 )
1297 i = x1;
1298 x1 = x2;
1299 x2 = i;
1301 if( y1 > y2 )
1303 i = y1;
1304 y1 = y2;
1305 y2 = i;
1308 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1309 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1310 rb->lcd_set_drawmode(DRMODE_SOLID);
1312 for( ; y1<=y2; y1++ )
1314 for( i = x1; i<=x2; i++ )
1316 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1319 /*if( update )*/ rb->lcd_update();
1322 static void draw_hflip( int x1, int y1, int x2, int y2 )
1324 int i;
1325 if( x1 > x2 )
1327 i = x1;
1328 x1 = x2;
1329 x2 = i;
1331 if( y1 > y2 )
1333 i = y1;
1334 y1 = y2;
1335 y2 = i;
1338 copy_to_clipboard();
1340 for( i = 0; i <= y2 - y1; i++ )
1342 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1343 buffer.clipboard+(y2-i)*COLS+x1,
1344 (x2-x1+1)*sizeof( fb_data ) );
1346 restore_screen();
1347 rb->lcd_update();
1350 static void draw_vflip( int x1, int y1, int x2, int y2 )
1352 int i;
1353 if( x1 > x2 )
1355 i = x1;
1356 x1 = x2;
1357 x2 = i;
1359 if( y1 > y2 )
1361 i = y1;
1362 y1 = y2;
1363 y2 = i;
1366 copy_to_clipboard();
1368 for( ; y1 <= y2; y1++ )
1370 for( i = 0; i <= x2 - x1; i++ )
1372 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1375 restore_screen();
1376 rb->lcd_update();
1379 /* direction: -1 = left, 1 = right */
1380 static void draw_rot_90_deg( int x1, int y1, int x2, int y2, int direction )
1382 int i, j;
1383 if( x1 > x2 )
1385 i = x1;
1386 x1 = x2;
1387 x2 = i;
1389 if( y1 > y2 )
1391 i = y1;
1392 y1 = y2;
1393 y2 = i;
1396 copy_to_clipboard();
1398 fb_data color = rp_colors[ bgdrawcolor ];
1399 const int width = x2 - x1, height = y2 - y1;
1400 const int sub_half = width/2-height/2, add_half = (width+height)/2;
1401 if( width > height )
1403 for( i = 0; i <= height; i++ )
1405 for( j = 0; j < sub_half; j++ )
1406 save_buffer[(y1+i)*COLS+x1+j] = color;
1407 for( j = add_half+1; j <= width; j++ )
1408 save_buffer[(y1+i)*COLS+x1+j] = color;
1411 else if( width < height )
1413 for( j = 0; j <= width; j++ )
1415 for( i = 0; i < -sub_half; i++ )
1416 save_buffer[(y1+i)*COLS+x1+j] = color;
1417 for( i = add_half+1; i <= height; i++ )
1418 save_buffer[(y1+i)*COLS+x1+j] = color;
1421 int x3 = x1 + sub_half, y3 = y1 - sub_half;
1422 int is = x3<0?-x3:0, ie = COLS-x3-1, js = y3<0?-y3:0, je = ROWS-y3-1;
1423 if( ie > height ) ie = height;
1424 if( je > width ) je = width;
1425 for( i = is; i <= ie; i++ )
1427 for( j = js; j <= je; j++ )
1429 int x, y;
1430 if(direction > 0)
1432 x = x1+j;
1433 y = y1+height-i;
1435 else
1437 x = x1+width-j;
1438 y = y1+i;
1440 save_buffer[(y3+j)*COLS+x3+i] = buffer.clipboard[y*COLS+x];
1443 restore_screen();
1444 rb->lcd_update();
1447 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1448 int src_y2, int x1, int y1, int mode )
1450 int i, width, height;
1451 if( mode == SELECT_MENU_CUT )
1453 i = drawcolor;
1454 drawcolor = bgdrawcolor;
1455 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1456 drawcolor = i;
1458 if( src_x1 > src_x2 )
1460 i = src_x1;
1461 src_x1 = src_x2;
1462 src_x2 = i;
1464 if( src_y1 > src_y2 )
1466 i = src_y1;
1467 src_y1 = src_y2;
1468 src_y2 = i;
1470 width = src_x2 - src_x1 + 1;
1471 height = src_y2 - src_y1 + 1;
1472 /* clipping */
1473 if( x1 + width > COLS )
1474 width = COLS - x1;
1475 if( y1 + height > ROWS )
1476 height = ROWS - y1;
1478 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1479 x1, y1, width, height );
1480 if( !preview )
1482 for( i = 0; i < height; i++ )
1484 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1485 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1486 width*sizeof( fb_data ) );
1491 static void show_grid( bool update )
1493 int i;
1494 if( gridsize > 0 )
1496 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1497 for( i = gridsize; i < img_width; i+= gridsize )
1499 rb->lcd_vline( i, 0, img_height-1 );
1501 for( i = gridsize; i < img_height; i+= gridsize )
1503 rb->lcd_hline( 0, img_width-1, i );
1505 rb->lcd_set_drawmode(DRMODE_SOLID);
1506 if( update ) rb->lcd_update();
1510 static void draw_text( int x, int y )
1512 int selected = 0;
1513 buffer.text.text[0] = '\0';
1514 rb->snprintf( buffer.text.old_font, MAX_PATH,
1515 FONT_DIR "/%s.fnt",
1516 rb->global_settings->font_file );
1517 while( 1 )
1519 switch( rb->do_menu( &text_menu, &selected, NULL, NULL ) )
1521 case TEXT_MENU_TEXT:
1522 rb->lcd_set_foreground(COLOR_BLACK);
1523 rb->kbd_input( buffer.text.text, MAX_TEXT );
1524 break;
1526 case TEXT_MENU_FONT:
1527 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1529 rb->font_load(NULL, buffer.text.font );
1531 break;
1533 case TEXT_MENU_PREVIEW:
1534 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1535 while( 1 )
1537 int button;
1538 restore_screen();
1539 rb->lcd_putsxy( x, y, buffer.text.text );
1540 rb->lcd_update();
1541 switch( button = rb->button_get( true ) )
1543 case ROCKPAINT_LEFT:
1544 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1545 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1546 if (x<0) x=COLS-1;
1547 break;
1549 case ROCKPAINT_RIGHT:
1550 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1551 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1552 if (x>=COLS) x=0;
1553 break;
1555 case ROCKPAINT_UP:
1556 case ROCKPAINT_UP | BUTTON_REPEAT:
1557 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1558 if (y<0) y=ROWS-1;
1559 break;
1561 case ROCKPAINT_DOWN:
1562 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1563 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1564 if (y>=ROWS-1) y=0;
1565 break;
1567 case ROCKPAINT_DRAW:
1568 break;
1569 default:
1570 if(rb->default_event_handler(button)
1571 == SYS_USB_CONNECTED)
1572 button = ROCKPAINT_DRAW;
1573 break;
1575 if( button == ROCKPAINT_DRAW ) break;
1577 break;
1579 case TEXT_MENU_APPLY:
1580 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1581 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1582 buffer.text.text );
1583 case TEXT_MENU_CANCEL:
1584 default:
1585 restore_screen();
1586 rb->font_load(NULL, buffer.text.old_font );
1587 return;
1592 static void draw_brush( int x, int y )
1594 int i,j;
1595 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1597 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1599 draw_pixel( x+i, y+j );
1604 /* This is an implementation of Bresenham's line algorithm.
1605 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1607 static void draw_line( int x1, int y1, int x2, int y2 )
1609 int x = x1;
1610 int y = y1;
1611 int deltax = x2 - x1;
1612 int deltay = y2 - y1;
1613 int i;
1615 int xerr = abs(deltax);
1616 int yerr = abs(deltay);
1617 int xstep = deltax > 0 ? 1 : -1;
1618 int ystep = deltay > 0 ? 1 : -1;
1619 int err;
1621 if (yerr > xerr)
1623 /* more vertical */
1624 err = yerr;
1625 xerr <<= 1;
1626 yerr <<= 1;
1628 /* to leave off the last pixel of the line, leave off the "+ 1" */
1629 for (i = abs(deltay) + 1; i; --i)
1631 draw_pixel(x, y);
1632 y += ystep;
1633 err -= xerr;
1634 if (err < 0) {
1635 x += xstep;
1636 err += yerr;
1640 else
1642 /* more horizontal */
1643 err = xerr;
1644 xerr <<= 1;
1645 yerr <<= 1;
1647 for (i = abs(deltax) + 1; i; --i)
1649 draw_pixel(x, y);
1650 x += xstep;
1651 err -= yerr;
1652 if (err < 0) {
1653 y += ystep;
1654 err += xerr;
1660 static void draw_curve( int x1, int y1, int x2, int y2,
1661 int xa, int ya, int xb, int yb )
1663 int i = 0;
1664 short xl1, yl1;
1665 short xl2, yl2;
1666 short xl3, yl3;
1667 short xl4, yl4;
1668 short xr1, yr1;
1669 short xr2, yr2;
1670 short xr3, yr3;
1671 short xr4, yr4;
1672 short depth;
1673 short xh, yh;
1675 if( x1 == x2 && y1 == y2 )
1677 draw_pixel( x1, y1 );
1678 return;
1681 // if( preview )
1683 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1684 if( xa == -1 || ya == -1 )
1686 rb->lcd_drawline( x1, y1, xb, yb );
1687 rb->lcd_drawline( x2, y2, xb, yb );
1689 else
1691 rb->lcd_drawline( x1, y1, xa, ya );
1692 rb->lcd_drawline( x2, y2, xb, yb );
1694 rb->lcd_set_drawmode(DRMODE_SOLID);
1697 if( xa == -1 || ya == -1 )
1698 /* We only have 3 of the points
1699 * This will currently only be used in preview mode */
1701 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1702 buffer.bezier[i].x1 = a1; \
1703 buffer.bezier[i].y1 = b1; \
1704 buffer.bezier[i].x2 = a2; \
1705 buffer.bezier[i].y2 = b2; \
1706 buffer.bezier[i].x3 = a3; \
1707 buffer.bezier[i].y3 = b3; \
1708 buffer.bezier[i].depth = d; \
1709 i++;
1710 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1711 i--; \
1712 a1 = buffer.bezier[i].x1; \
1713 b1 = buffer.bezier[i].y1; \
1714 a2 = buffer.bezier[i].x2; \
1715 b2 = buffer.bezier[i].y2; \
1716 a3 = buffer.bezier[i].x3; \
1717 b3 = buffer.bezier[i].y3; \
1718 d = buffer.bezier[i].depth;
1719 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1720 while( i )
1722 /* de Casteljau's algorithm (see wikipedia) */
1723 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1724 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1726 xl2 = ( xl1 + xb )>>1;
1727 yl2 = ( yl1 + yb )>>1;
1728 xr2 = ( xb + xr3 )>>1;
1729 yr2 = ( yb + yr3 )>>1;
1730 xr1 = ( xl2 + xr2 )>>1;
1731 yr1 = ( yl2 + yr2 )>>1;
1732 xl3 = xr1;
1733 yl3 = yr1;
1734 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1735 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1737 else
1739 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1740 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1743 #undef PUSH
1744 #undef POP
1746 else /* We have the 4 points */
1748 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1749 buffer.bezier[i].x1 = a1; \
1750 buffer.bezier[i].y1 = b1; \
1751 buffer.bezier[i].x2 = a2; \
1752 buffer.bezier[i].y2 = b2; \
1753 buffer.bezier[i].x3 = a3; \
1754 buffer.bezier[i].y3 = b3; \
1755 buffer.bezier[i].x4 = a4; \
1756 buffer.bezier[i].y4 = b4; \
1757 buffer.bezier[i].depth = d; \
1758 i++;
1759 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1760 i--; \
1761 a1 = buffer.bezier[i].x1; \
1762 b1 = buffer.bezier[i].y1; \
1763 a2 = buffer.bezier[i].x2; \
1764 b2 = buffer.bezier[i].y2; \
1765 a3 = buffer.bezier[i].x3; \
1766 b3 = buffer.bezier[i].y3; \
1767 a4 = buffer.bezier[i].x4; \
1768 b4 = buffer.bezier[i].y4; \
1769 d = buffer.bezier[i].depth;
1771 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1772 while( i )
1774 /* de Casteljau's algorithm (see wikipedia) */
1775 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1776 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1778 xl2 = ( xl1 + xa )>>1;
1779 yl2 = ( yl1 + ya )>>1;
1780 xh = ( xa + xb )>>1;
1781 yh = ( ya + yb )>>1;
1782 xr3 = ( xb + xr4 )>>1;
1783 yr3 = ( yb + yr4 )>>1;
1784 xl3 = ( xl2 + xh )>>1;
1785 yl3 = ( yl2 + yh )>>1;
1786 xr2 = ( xr3 + xh )>>1;
1787 yr2 = ( yr3 + yh )>>1;
1788 xl4 = ( xl3 + xr2 )>>1;
1789 yl4 = ( yl3 + yr2 )>>1;
1790 xr1 = xl4;
1791 yr1 = yl4;
1792 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1793 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1795 else
1797 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1798 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1801 #undef PUSH
1802 #undef POP
1806 static void draw_rect( int x1, int y1, int x2, int y2 )
1808 draw_line( x1, y1, x1, y2 );
1809 draw_line( x1, y1, x2, y1 );
1810 draw_line( x1, y2, x2, y2 );
1811 draw_line( x2, y1, x2, y2 );
1814 static void togglebg( void )
1816 if( isbg )
1818 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1820 else
1822 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1824 isbg = !isbg;
1827 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1829 /* GRUIK */
1830 int x;
1831 togglebg();
1832 if( x1 > x2 )
1834 x = x1;
1835 x1 = x2;
1836 x2 = x;
1838 x = x1;
1839 do {
1840 draw_line( x, y1, x, y2 );
1841 } while( ++x <= x2 );
1842 togglebg();
1843 draw_rect( x1, y1, x2, y2 );
1846 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1848 /* TODO: simplify :) */
1849 int cx = (x1+x2)>>1;
1850 int cy = (y1+y2)>>1;
1852 int rx = (x1-x2)>>1;
1853 int ry = (y1-y2)>>1;
1854 if( rx < 0 ) rx *= -1;
1855 if( ry < 0 ) ry *= -1;
1857 if( rx == 0 || ry == 0 )
1859 draw_line( x1, y1, x2, y2 );
1860 return;
1863 int x,y;
1864 int dst, old_dst;
1866 for( x = 0; x < rx; x++ )
1868 y = 0;
1869 dst = -0xfff;
1870 do {
1871 old_dst = dst;
1872 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1873 y++;
1874 } while( dst < 0 );
1875 if( -old_dst < dst ) y--;
1876 if( full )
1878 draw_line( cx+x, cy, cx+x, cy+y );
1879 draw_line( cx+x, cy, cx+x, cy-y );
1880 draw_line( cx-x, cy, cx-x, cy+y );
1881 draw_line( cx-x, cy, cx-x, cy-y );
1883 else
1885 draw_pixel( cx+x, cy+y );
1886 draw_pixel( cx+x, cy-y );
1887 draw_pixel( cx-x, cy+y );
1888 draw_pixel( cx-x, cy-y );
1891 for( y = 0; y < ry; y++ )
1893 x = 0;
1894 dst = -0xfff;
1895 do {
1896 old_dst = dst;
1897 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1898 x++;
1899 } while( dst < 0 );
1900 if( -old_dst < dst ) x--;
1901 if( full )
1903 draw_line( cx+x, cy, cx+x, cy+y );
1904 draw_line( cx+x, cy, cx+x, cy-y );
1905 draw_line( cx-x, cy, cx-x, cy+y );
1906 draw_line( cx-x, cy, cx-x, cy-y );
1908 else
1910 draw_pixel( cx+x, cy+y );
1911 draw_pixel( cx+x, cy-y );
1912 draw_pixel( cx-x, cy+y );
1913 draw_pixel( cx-x, cy-y );
1918 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1920 draw_oval( x1, y1, x2, y2, false );
1923 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1925 togglebg();
1926 draw_oval( x1, y1, x2, y2, true );
1927 togglebg();
1928 draw_oval( x1, y1, x2, y2, false );
1931 static void draw_fill( int x0, int y0 )
1933 #define PUSH( a, b ) \
1934 draw_pixel( (int)a, (int)b ); \
1935 buffer.coord[i].x = a; \
1936 buffer.coord[i].y = b; \
1937 i++;
1938 #define POP( a, b ) \
1939 i--; \
1940 a = buffer.coord[i].x; \
1941 b = buffer.coord[i].y;
1943 unsigned int i=0;
1944 short x = x0;
1945 short y = y0;
1946 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1948 if( prev_color == rp_colors[ drawcolor ] ) return;
1950 PUSH( x, y );
1952 while( i != 0 )
1954 POP( x, y );
1955 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
1957 PUSH( x-1, y );
1959 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
1961 PUSH( x+1, y );
1963 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
1965 PUSH( x, y-1 );
1967 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
1969 PUSH( x, y+1 );
1972 #undef PUSH
1973 #undef POP
1977 /* For preview purposes only */
1978 static void line_gradient( int x1, int y1, int x2, int y2 )
1980 int r1, g1, b1;
1981 int r2, g2, b2;
1982 int h1, s1, v1, h2, s2, v2, r, g, b;
1983 int w, h, x, y;
1985 bool a = false;
1987 x1 <<= 1;
1988 y1 <<= 1;
1989 x2 <<= 1;
1990 y2 <<= 1;
1992 w = x1 - x2;
1993 h = y1 - y2;
1995 if( w == 0 && h == 0 )
1997 draw_pixel( x1>>1, y1>>1 );
1998 return;
2001 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2002 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2003 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2004 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2005 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2006 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2008 if( w < 0 )
2010 w *= -1;
2011 a = true;
2013 if( h < 0 )
2015 h *= -1;
2016 a = !a;
2018 if( a )
2020 r = r1;
2021 r1 = r2;
2022 r2 = r;
2023 g = g1;
2024 g1 = g2;
2025 g2 = g;
2026 b = b1;
2027 b1 = b2;
2028 b2 = b;
2031 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2032 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2034 if( w > h )
2036 if( x1 > x2 )
2038 x = x2;
2039 y = y2;
2040 x2 = x1;
2041 y2 = y1;
2042 x1 = x;
2043 y1 = y;
2045 w = x1 - x2;
2046 h = y1 - y2;
2047 while( x1 <= x2 )
2049 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2050 s1+((s2-s1)*(x1-x2))/w,
2051 v1+((v2-v1)*(x1-x2))/w,
2052 &r, &g, &b );
2053 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2054 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2055 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2056 x1+=2;
2057 y1 = y2 - ( x2 - x1 ) * h / w;
2060 else /* h > w */
2062 if( y1 > y2 )
2064 x = x2;
2065 y = y2;
2066 x2 = x1;
2067 y2 = y1;
2068 x1 = x;
2069 y1 = y;
2071 w = x1 - x2;
2072 h = y1 - y2;
2073 while( y1 <= y2 )
2075 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2076 s1+((s2-s1)*(y1-y2))/h,
2077 v1+((v2-v1)*(y1-y2))/h,
2078 &r, &g, &b );
2079 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2080 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2081 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2082 y1+=2;
2083 x1 = x2 - ( y2 - y1 ) * w / h;
2086 if( a )
2088 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2090 else
2092 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2096 static void linear_gradient( int x1, int y1, int x2, int y2 )
2098 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2099 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2100 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2101 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2102 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2103 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2105 int h1, s1, v1, h2, s2, v2, r, g, b;
2107 /* radius^2 */
2108 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2109 int dist2, i=0;
2111 /* We only propagate the gradient to neighboring pixels with the same
2112 * color as ( x1, y1 ) */
2113 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2115 int x = x1;
2116 int y = y1;
2118 if( radius2 == 0 ) return;
2119 if( preview )
2121 line_gradient( x1, y1, x2, y2 );
2124 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2125 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2127 #define PUSH( x0, y0 ) \
2128 buffer.coord[i].x = (short)(x0); \
2129 buffer.coord[i].y = (short)(y0); \
2130 i++;
2131 #define POP( a, b ) \
2132 i--; \
2133 a = (int)buffer.coord[i].x; \
2134 b = (int)buffer.coord[i].y;
2136 PUSH( x, y );
2138 while( i != 0 )
2140 POP( x, y );
2142 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2143 if( dist2 <= 0 )
2145 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2147 else if( dist2 < radius2 )
2149 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2150 s1+((s2-s1)*dist2)/radius2,
2151 v1+((v2-v1)*dist2)/radius2,
2152 &r, &g, &b );
2153 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2155 else
2157 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2159 if( rp_colors[ drawcolor ] == prev_color )
2161 if( rp_colors[ drawcolor ])
2162 rp_colors[ drawcolor ]--; /* GRUIK */
2163 else
2164 rp_colors[ drawcolor ]++; /* GRUIK */
2166 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2167 draw_pixel( x, y );
2169 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2171 PUSH( x-1, y );
2173 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2175 PUSH( x+1, y );
2177 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2179 PUSH( x, y-1 );
2181 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2183 PUSH( x, y+1 );
2186 #undef PUSH
2187 #undef POP
2189 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2192 static void radial_gradient( int x1, int y1, int x2, int y2 )
2194 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2195 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2196 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2197 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2198 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2199 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2201 int h1, s1, v1, h2, s2, v2, r, g, b;
2203 /* radius^2 */
2204 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2205 int dist2, i=0;
2207 /* We only propagate the gradient to neighboring pixels with the same
2208 * color as ( x1, y1 ) */
2209 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2211 int x = x1;
2212 int y = y1;
2214 if( radius2 == 0 ) return;
2215 if( preview )
2217 line_gradient( x1, y1, x2, y2 );
2220 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2221 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2223 #define PUSH( x0, y0 ) \
2224 buffer.coord[i].x = (short)(x0); \
2225 buffer.coord[i].y = (short)(y0); \
2226 i++;
2227 #define POP( a, b ) \
2228 i--; \
2229 a = (int)buffer.coord[i].x; \
2230 b = (int)buffer.coord[i].y;
2232 PUSH( x, y );
2234 while( i != 0 )
2236 POP( x, y );
2238 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2240 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2241 s1+((s2-s1)*dist2)/radius2,
2242 v1+((v2-v1)*dist2)/radius2,
2243 &r, &g, &b );
2244 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2246 else
2248 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2250 if( rp_colors[ drawcolor ] == prev_color )
2252 if( rp_colors[ drawcolor ])
2253 rp_colors[ drawcolor ]--; /* GRUIK */
2254 else
2255 rp_colors[ drawcolor ]++; /* GRUIK */
2257 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2258 draw_pixel( x, y );
2260 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2262 PUSH( x-1, y );
2264 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2266 PUSH( x+1, y );
2268 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2270 PUSH( x, y-1 );
2272 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2274 PUSH( x, y+1 );
2277 #undef PUSH
2278 #undef POP
2280 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2283 static void draw_toolbars(bool update)
2285 int i;
2286 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2287 rb->lcd_set_background( COLOR_LIGHTGRAY );
2288 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2289 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2290 rb->lcd_set_foreground( COLOR_BLACK );
2291 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2293 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2294 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2295 TB_SC_SIZE, TB_SC_SIZE );
2296 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2297 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2298 TB_SC_SIZE, TB_SC_SIZE );
2299 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2300 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2301 TB_SC_SIZE, TB_SC_SIZE );
2302 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2303 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2304 TB_SC_SIZE, TB_SC_SIZE );
2306 for( i=0; i<18; i++ )
2308 rb->lcd_set_foreground( rp_colors[i] );
2309 rb->lcd_fillrect(
2310 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2311 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2312 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2313 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2314 rb->lcd_drawrect(
2315 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2316 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2317 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2320 #define SEPARATOR( x, y ) \
2321 rb->lcd_set_foreground( COLOR_WHITE ); \
2322 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2323 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2324 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2325 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2327 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2328 TB_TL_WIDTH, TB_TL_HEIGHT );
2329 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2330 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2331 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2332 TB_TL_SIZE, TB_TL_SIZE );
2334 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2336 rb->lcd_setfont( FONT_SYSFIXED );
2337 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2338 rb->lcd_setfont( FONT_UI );
2339 #undef TOP
2341 if( update ) rb->lcd_update();
2344 static void toolbar( void )
2346 int button, i, j;
2347 restore_screen();
2348 draw_toolbars( false );
2349 y = LCD_HEIGHT-TB_HEIGHT/2;
2350 inv_cursor( true );
2351 while( 1 )
2353 switch( button = rb->button_get( true ) )
2355 case ROCKPAINT_DRAW:
2356 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2357 if( y >= TOP + TB_SC_FG_TOP
2358 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2359 && x >= TB_SC_FG_LEFT
2360 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2362 /* click on the foreground color */
2363 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2365 else if( y >= TOP + TB_SC_BG_TOP
2366 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2367 && x >= TB_SC_BG_LEFT
2368 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2370 /* click on the background color */
2371 i = drawcolor;
2372 drawcolor = bgdrawcolor;
2373 bgdrawcolor = i;
2375 else if( y >= TOP + TB_PL_TOP
2376 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2377 && x >= TB_PL_LEFT
2378 && x < TB_PL_LEFT + TB_PL_WIDTH )
2380 /* click on the palette */
2381 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2382 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2383 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2384 break;
2385 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2386 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2387 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2389 else if( y >= TOP+TB_TL_TOP
2390 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2391 && x >= TB_TL_LEFT
2392 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2394 /* click on the tools */
2395 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2396 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2397 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2398 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2399 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2400 tool = i*2+j;
2401 prev_x = -1;
2402 prev_y = -1;
2403 prev_x2 = -1;
2404 prev_y2 = -1;
2405 prev_x3 = -1;
2406 prev_y3 = -1;
2407 preview = false;
2409 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2411 /* menu button */
2412 goto_menu();
2414 #undef TOP
2415 restore_screen();
2416 draw_toolbars( false );
2417 inv_cursor( true );
2418 break;
2420 case ROCKPAINT_LEFT:
2421 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2422 inv_cursor(false);
2423 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2424 if (x<0) x=COLS-1;
2425 inv_cursor(true);
2426 break;
2428 case ROCKPAINT_RIGHT:
2429 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2430 inv_cursor(false);
2431 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2432 if (x>=COLS) x=0;
2433 inv_cursor(true);
2434 break;
2436 case ROCKPAINT_UP:
2437 case ROCKPAINT_UP | BUTTON_REPEAT:
2438 inv_cursor(false);
2439 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2440 if (y<LCD_HEIGHT-TB_HEIGHT)
2442 return;
2444 inv_cursor(true);
2445 break;
2447 case ROCKPAINT_DOWN:
2448 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2449 inv_cursor(false);
2450 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2451 if (y>=LCD_HEIGHT)
2453 y = 0;
2454 return;
2456 inv_cursor(true);
2457 break;
2459 case ROCKPAINT_TOOLBAR:
2460 case ROCKPAINT_TOOLBAR2:
2461 return;
2463 if( quit ) return;
2467 static void inv_cursor(bool update)
2469 rb->lcd_set_foreground(COLOR_BLACK);
2470 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2471 /* cross painting */
2472 rb->lcd_hline(x-4,x+4,y);
2473 rb->lcd_vline(x,y-4,y+4);
2474 rb->lcd_set_foreground(rp_colors[drawcolor]);
2475 rb->lcd_set_drawmode(DRMODE_SOLID);
2477 if( update ) rb->lcd_update();
2480 static void restore_screen(void)
2482 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2483 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2484 rb->lcd_vline( img_width, 0, ROWS );
2485 rb->lcd_hline( 0, COLS, img_height );
2486 rb->lcd_drawpixel( img_width, img_height );
2487 rb->lcd_set_drawmode(DRMODE_SOLID);
2490 static void clear_drawing(void)
2492 init_buffer();
2493 img_height = ROWS;
2494 img_width = COLS;
2495 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2496 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2497 rb->lcd_update();
2500 static void goto_menu(void)
2502 int multi;
2503 int selected = 0;
2505 while( 1 )
2507 switch( rb->do_menu( &main_menu, &selected, NULL, false ) )
2509 case MAIN_MENU_NEW:
2510 clear_drawing();
2511 return;
2513 case MAIN_MENU_LOAD:
2514 if( browse( filename, MAX_PATH, "/" ) )
2516 if( load_bitmap( filename ) <= 0 )
2518 rb->splashf( 1*HZ, "Error while loading %s",
2519 filename );
2520 clear_drawing();
2522 else
2524 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2525 restore_screen();
2526 inv_cursor(true);
2527 return;
2530 break;
2532 case MAIN_MENU_SAVE:
2533 rb->lcd_set_foreground(COLOR_BLACK);
2534 if (!filename[0])
2535 rb->strcpy(filename,"/");
2536 if( !rb->kbd_input( filename, MAX_PATH ) )
2538 if( !check_extention( filename, ".bmp" ) )
2539 rb->strcat(filename, ".bmp");
2540 save_bitmap( filename );
2541 rb->splashf( 1*HZ, "File saved (%s)", filename );
2543 break;
2545 case MAIN_MENU_SET_WIDTH:
2546 rb->set_int( "Set Width", "px", UNIT_INT, &img_width,
2547 NULL, 1, 1, COLS, NULL );
2548 break;
2549 case MAIN_MENU_SET_HEIGHT:
2550 rb->set_int( "Set Height", "px", UNIT_INT, &img_height,
2551 NULL, 1, 1, ROWS, NULL );
2552 break;
2553 case MAIN_MENU_BRUSH_SIZE:
2554 for(multi = 0; multi<4; multi++)
2555 if(bsize == times_list[multi]) break;
2556 rb->set_option( "Brush Size", &multi, INT, times_options, 4, NULL );
2557 if( multi >= 0 )
2558 bsize = times_list[multi];
2559 break;
2561 case MAIN_MENU_BRUSH_SPEED:
2562 for(multi = 0; multi<3; multi++)
2563 if(bspeed == times_list[multi]) break;
2564 rb->set_option( "Brush Speed", &multi, INT, times_options, 3, NULL );
2565 if( multi >= 0 )
2566 bspeed = times_list[multi];
2567 break;
2569 case MAIN_MENU_COLOR:
2570 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2571 break;
2573 case MAIN_MENU_GRID_SIZE:
2574 for(multi = 0; multi<4; multi++)
2575 if(gridsize == gridsize_list[multi]) break;
2576 rb->set_option( "Grid Size", &multi, INT, gridsize_options, 4, NULL );
2577 if( multi >= 0 )
2578 gridsize = gridsize_list[multi];
2579 break;
2581 case MAIN_MENU_PLAYBACK_CONTROL:
2582 playback_control( NULL );
2583 break;
2585 case MAIN_MENU_EXIT:
2586 restore_screen();
2587 quit=true;
2588 return;
2590 case MAIN_MENU_RESUME:
2591 default:
2592 restore_screen();
2593 return;
2594 }/* end switch */
2595 }/* end while */
2598 static void reset_tool( void )
2600 prev_x = -1;
2601 prev_y = -1;
2602 prev_x2 = -1;
2603 prev_y2 = -1;
2604 prev_x3 = -1;
2605 prev_y3 = -1;
2606 tool_mode = -1;
2607 preview = false;
2610 static bool rockpaint_loop( void )
2612 int button=0,i,j;
2613 int accelaration;
2615 x = 10;
2616 toolbar();
2617 x = 0; y = 0;
2618 restore_screen();
2619 inv_cursor(true);
2621 while (!quit) {
2622 button = rb->button_get(true);
2624 if( tool == Brush && prev_x != -1 )
2626 accelaration = 1;
2628 else if( button & BUTTON_REPEAT )
2630 accelaration = 4;
2632 else
2634 accelaration = 1;
2637 switch(button)
2639 case ROCKPAINT_QUIT:
2640 rb->lcd_set_drawmode(DRMODE_SOLID);
2641 return PLUGIN_OK;
2643 case ROCKPAINT_MENU:
2644 inv_cursor(false);
2645 goto_menu();
2646 restore_screen();
2647 inv_cursor(true);
2648 break;
2650 case ROCKPAINT_DRAW:
2651 inv_cursor(false);
2652 switch( tool )
2654 case Brush:
2655 if( prev_x == -1 ) prev_x = 1;
2656 else prev_x = -1;
2657 break;
2659 case SelectRectangle:
2660 case Line:
2661 case Curve:
2662 case Rectangle:
2663 case RectangleFull:
2664 case Oval:
2665 case OvalFull:
2666 case LinearGradient:
2667 case RadialGradient:
2668 /* Curve uses 4 points, others use 2 */
2669 if( prev_x == -1 || prev_y == -1 )
2671 prev_x = x;
2672 prev_y = y;
2673 preview = true;
2675 else if( tool == Curve
2676 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2678 prev_x2 = x;
2679 prev_y2 = y;
2681 else if( tool == SelectRectangle
2682 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2684 tool_mode = rb->do_menu( &select_menu,
2685 NULL, NULL, false );
2686 switch( tool_mode )
2688 case SELECT_MENU_CUT:
2689 case SELECT_MENU_COPY:
2690 prev_x2 = x;
2691 prev_y2 = y;
2692 copy_to_clipboard();
2693 if( prev_x < x ) x = prev_x;
2694 if( prev_y < y ) y = prev_y;
2695 break;
2697 case SELECT_MENU_INVERT:
2698 draw_invert( prev_x, prev_y, x, y );
2699 reset_tool();
2700 break;
2702 case SELECT_MENU_HFLIP:
2703 draw_hflip( prev_x, prev_y, x, y );
2704 reset_tool();
2705 break;
2707 case SELECT_MENU_VFLIP:
2708 draw_vflip( prev_x, prev_y, x, y );
2709 reset_tool();
2710 break;
2712 case SELECT_MENU_ROTATE90:
2713 draw_rot_90_deg( prev_x, prev_y, x, y, 1 );
2714 reset_tool();
2715 break;
2717 case SELECT_MENU_ROTATE180:
2718 draw_hflip( prev_x, prev_y, x, y );
2719 draw_vflip( prev_x, prev_y, x, y );
2720 reset_tool();
2721 break;
2723 case SELECT_MENU_ROTATE270:
2724 draw_rot_90_deg( prev_x, prev_y, x, y, -1 );
2725 reset_tool();
2726 break;
2728 case SELECT_MENU_CANCEL:
2729 reset_tool();
2730 break;
2732 default:
2733 break;
2735 restore_screen();
2737 else if( tool == Curve
2738 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2740 prev_x3 = x;
2741 prev_y3 = y;
2743 else
2745 preview = false;
2746 switch( tool )
2748 case SelectRectangle:
2749 draw_paste_rectangle( prev_x, prev_y,
2750 prev_x2, prev_y2,
2751 x, y, tool_mode );
2752 break;
2753 case Line:
2754 draw_line( prev_x, prev_y, x, y );
2755 break;
2756 case Curve:
2757 draw_curve( prev_x, prev_y,
2758 prev_x2, prev_y2,
2759 prev_x3, prev_y3,
2760 x, y );
2761 break;
2762 case Rectangle:
2763 draw_rect( prev_x, prev_y, x, y );
2764 break;
2765 case RectangleFull:
2766 draw_rect_full( prev_x, prev_y, x, y );
2767 break;
2768 case Oval:
2769 draw_oval_empty( prev_x, prev_y, x, y );
2770 break;
2771 case OvalFull:
2772 draw_oval_full( prev_x, prev_y, x, y );
2773 break;
2774 case LinearGradient:
2775 linear_gradient( prev_x, prev_y, x, y );
2776 break;
2777 case RadialGradient:
2778 radial_gradient( prev_x, prev_y, x, y );
2779 break;
2780 default:
2781 break;
2783 reset_tool();
2784 restore_screen();
2786 break;
2788 case Fill:
2789 draw_fill( x, y );
2790 restore_screen();
2791 break;
2793 case ColorPicker:
2794 color_picker( x, y );
2795 break;
2797 case Text:
2798 draw_text( x, y );
2799 break;
2801 default:
2802 break;
2804 inv_cursor(true);
2805 break;
2807 case ROCKPAINT_DRAW|BUTTON_REPEAT:
2808 if( tool == Curve )
2810 /* 3 point bezier curve */
2811 preview = false;
2812 draw_curve( prev_x, prev_y,
2813 prev_x2, prev_y2,
2814 -1, -1,
2815 x, y );
2816 reset_tool();
2817 restore_screen();
2818 inv_cursor( true );
2820 break;
2822 case ROCKPAINT_TOOLBAR:
2823 i = x; j = y;
2824 x = 10;
2825 toolbar();
2826 x = i; y = j;
2827 restore_screen();
2828 inv_cursor(true);
2829 break;
2831 case ROCKPAINT_TOOLBAR2:
2832 i = x; j = y;
2833 x = 110;
2834 toolbar();
2835 x = i; y = j;
2836 restore_screen();
2837 inv_cursor(true);
2838 break;
2840 case ROCKPAINT_LEFT:
2841 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2842 inv_cursor(false);
2843 x-=bspeed * accelaration;
2844 if (x<0) x=COLS-1;
2845 inv_cursor(true);
2846 break;
2848 case ROCKPAINT_RIGHT:
2849 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2850 inv_cursor(false);
2851 x+=bspeed * accelaration;
2852 if (x>=COLS) x=0;
2853 inv_cursor(true);
2854 break;
2856 case ROCKPAINT_UP:
2857 case ROCKPAINT_UP | BUTTON_REPEAT:
2858 inv_cursor(false);
2859 y-=bspeed * accelaration;
2860 if (y<0) y=ROWS-1;
2861 inv_cursor(true);
2862 break;
2864 case ROCKPAINT_DOWN:
2865 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2866 inv_cursor(false);
2867 y+=bspeed * accelaration;
2868 if (y>=ROWS)
2870 toolbar();
2871 restore_screen();
2873 inv_cursor(true);
2874 break;
2876 default:
2877 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2878 return PLUGIN_USB_CONNECTED;
2879 break;
2881 if( tool == Brush && prev_x == 1 )
2883 inv_cursor(false);
2884 draw_brush( x, y );
2885 inv_cursor(true);
2887 if( preview || tool == ColorPicker )
2888 /* always preview color picker */
2890 restore_screen();
2891 switch( tool )
2893 case SelectRectangle:
2894 if( prev_x2 == -1 || prev_y2 == -1 )
2896 /* we are defining the selection */
2897 draw_select_rectangle( prev_x, prev_y, x, y );
2899 else
2901 /* we are pasting the selected data */
2902 draw_paste_rectangle( prev_x, prev_y, prev_x2,
2903 prev_y2, x, y, tool_mode );
2904 prev_x3 = prev_x2-prev_x;
2905 if( prev_x3 < 0 ) prev_x3 *= -1;
2906 prev_y3 = prev_y2-prev_y;
2907 if( prev_y3 < 0 ) prev_y3 *= -1;
2908 draw_select_rectangle( x, y, x+prev_x3, y+prev_y3 );
2909 prev_x3 = -1;
2910 prev_y3 = -1;
2912 break;
2914 case Brush:
2915 break;
2917 case Line:
2918 draw_line( prev_x, prev_y, x, y );
2919 break;
2921 case Curve:
2922 if( prev_x2 == -1 || prev_y2 == -1 )
2924 draw_line( prev_x, prev_y, x, y );
2926 else
2928 draw_curve( prev_x, prev_y,
2929 prev_x2, prev_y2,
2930 prev_x3, prev_y3,
2931 x, y );
2933 break;
2935 case Rectangle:
2936 draw_rect( prev_x, prev_y, x, y );
2937 break;
2939 case RectangleFull:
2940 draw_rect_full( prev_x, prev_y, x, y );
2941 break;
2943 case Oval:
2944 draw_oval_empty( prev_x, prev_y, x, y );
2945 break;
2947 case OvalFull:
2948 draw_oval_full( prev_x, prev_y, x, y );
2949 break;
2951 case Fill:
2952 break;
2954 case ColorPicker:
2955 preview = true;
2956 color_picker( x, y );
2957 preview = false;
2958 break;
2960 case LinearGradient:
2961 line_gradient( prev_x, prev_y, x, y );
2962 break;
2964 case RadialGradient:
2965 line_gradient( prev_x, prev_y, x, y );
2966 break;
2968 case Text:
2969 default:
2970 break;
2972 inv_cursor( true );
2974 if( gridsize > 0 )
2976 show_grid( true );
2977 show_grid( false );
2981 return PLUGIN_OK;
2984 static int load_bitmap( const char *file )
2986 struct bitmap bm;
2987 bool ret;
2988 int i, j;
2989 fb_data color = rp_colors[ bgdrawcolor ];
2991 bm.data = (char*)save_buffer;
2992 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2993 FORMAT_NATIVE, NULL );
2995 if((bm.width > COLS ) || ( bm.height > ROWS ))
2996 return -1;
2998 img_width = bm.width;
2999 img_height = bm.height;
3000 for( i = bm.height-1; i >= 0; i-- )
3002 rb->memmove( save_buffer+i*COLS, save_buffer+i*bm.width,
3003 sizeof( fb_data )*bm.width );
3004 for( j = bm.width; j < COLS; j++ )
3005 save_buffer[j+i*COLS] = color;
3007 for( i = bm.height*COLS; i < ROWS*COLS; i++ )
3008 save_buffer[i] = color;
3010 return ret;
3013 static int save_bitmap( char *file )
3015 struct bitmap bm;
3016 int i;
3017 for(i = 0; i < img_height; i++)
3019 rb->memcpy( buffer.clipboard+i*img_width, save_buffer+i*COLS,
3020 sizeof( fb_data )*img_width );
3022 bm.data = (char*)buffer.clipboard;
3023 bm.height = img_height;
3024 bm.width = img_width;
3025 bm.format = FORMAT_NATIVE;
3026 return save_bmp_file( file, &bm );
3029 enum plugin_status plugin_start(const void* parameter)
3031 rb->lcd_set_foreground(COLOR_WHITE);
3032 rb->lcd_set_backdrop(NULL);
3033 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
3034 rb->splash( HZ/2, "Rock Paint");
3036 rb->lcd_clear_display();
3038 filename[0] = '\0';
3040 if( parameter )
3042 if( load_bitmap( parameter ) <= 0 )
3044 rb->splash( 1*HZ, "File Open Error");
3045 clear_drawing();
3047 else
3049 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
3050 restore_screen();
3051 rb->strcpy( filename, parameter );
3054 else
3056 clear_drawing();
3058 inv_cursor(true);
3060 return rockpaint_loop();