Use the AMS_LOWMEM define to indicate memory size as the .lds files do in config...
[kugel-rb.git] / apps / plugins / rockpaint.c
blobf92c616e073da2eb88dd30241829fdacbc076373
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"
35 PLUGIN_HEADER
37 /***********************************************************************
38 * Buttons
39 ***********************************************************************/
41 #if CONFIG_KEYPAD == IRIVER_H300_PAD
42 #define ROCKPAINT_QUIT BUTTON_OFF
43 #define ROCKPAINT_DRAW BUTTON_SELECT
44 #define ROCKPAINT_MENU BUTTON_ON
45 #define ROCKPAINT_TOOLBAR BUTTON_REC
46 #define ROCKPAINT_TOOLBAR2 BUTTON_MODE
47 #define ROCKPAINT_UP BUTTON_UP
48 #define ROCKPAINT_DOWN BUTTON_DOWN
49 #define ROCKPAINT_LEFT BUTTON_LEFT
50 #define ROCKPAINT_RIGHT BUTTON_RIGHT
52 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
53 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
54 #define ROCKPAINT_QUIT ( ~BUTTON_MAIN )
55 #define ROCKPAINT_DRAW BUTTON_SELECT
56 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_MENU )
57 #define ROCKPAINT_TOOLBAR ( BUTTON_MENU | BUTTON_LEFT )
58 #define ROCKPAINT_TOOLBAR2 ( BUTTON_MENU | BUTTON_RIGHT )
59 #define ROCKPAINT_UP BUTTON_MENU
60 #define ROCKPAINT_DOWN BUTTON_PLAY
61 #define ROCKPAINT_LEFT BUTTON_LEFT
62 #define ROCKPAINT_RIGHT BUTTON_RIGHT
64 #elif ( CONFIG_KEYPAD == IAUDIO_X5M5_PAD )
65 #define ROCKPAINT_QUIT BUTTON_POWER
66 #define ROCKPAINT_DRAW BUTTON_SELECT
67 #define ROCKPAINT_MENU BUTTON_PLAY
68 #define ROCKPAINT_TOOLBAR BUTTON_REC
69 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
70 #define ROCKPAINT_UP BUTTON_UP
71 #define ROCKPAINT_DOWN BUTTON_DOWN
72 #define ROCKPAINT_LEFT BUTTON_LEFT
73 #define ROCKPAINT_RIGHT BUTTON_RIGHT
75 #elif CONFIG_KEYPAD == GIGABEAT_PAD
76 #define ROCKPAINT_QUIT BUTTON_POWER
77 #define ROCKPAINT_DRAW BUTTON_SELECT
78 #define ROCKPAINT_MENU BUTTON_MENU
79 #define ROCKPAINT_TOOLBAR BUTTON_A
80 #define ROCKPAINT_TOOLBAR2 ( BUTTON_A | BUTTON_LEFT )
81 #define ROCKPAINT_UP BUTTON_UP
82 #define ROCKPAINT_DOWN BUTTON_DOWN
83 #define ROCKPAINT_LEFT BUTTON_LEFT
84 #define ROCKPAINT_RIGHT BUTTON_RIGHT
86 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
87 (CONFIG_KEYPAD == SANSA_C200_PAD)
88 #define ROCKPAINT_QUIT BUTTON_POWER
89 #define ROCKPAINT_DRAW BUTTON_SELECT
90 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_POWER )
91 #define ROCKPAINT_TOOLBAR BUTTON_REC
92 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REC | BUTTON_LEFT )
93 #define ROCKPAINT_UP BUTTON_UP
94 #define ROCKPAINT_DOWN BUTTON_DOWN
95 #define ROCKPAINT_LEFT BUTTON_LEFT
96 #define ROCKPAINT_RIGHT BUTTON_RIGHT
98 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
99 #define ROCKPAINT_QUIT (BUTTON_HOME|BUTTON_REPEAT)
100 #define ROCKPAINT_DRAW BUTTON_SELECT
101 #define ROCKPAINT_MENU ( BUTTON_SELECT | BUTTON_DOWN )
102 #define ROCKPAINT_TOOLBAR ( BUTTON_SELECT | BUTTON_LEFT )
103 #define ROCKPAINT_TOOLBAR2 ( BUTTON_SELECT | BUTTON_RIGHT )
104 #define ROCKPAINT_UP BUTTON_UP
105 #define ROCKPAINT_DOWN BUTTON_DOWN
106 #define ROCKPAINT_LEFT BUTTON_LEFT
107 #define ROCKPAINT_RIGHT BUTTON_RIGHT
109 #elif ( CONFIG_KEYPAD == IRIVER_H10_PAD )
110 #define ROCKPAINT_QUIT BUTTON_POWER
111 #define ROCKPAINT_DRAW BUTTON_FF
112 #define ROCKPAINT_MENU BUTTON_PLAY
113 #define ROCKPAINT_TOOLBAR BUTTON_REW
114 #define ROCKPAINT_TOOLBAR2 ( BUTTON_REW | BUTTON_LEFT )
115 #define ROCKPAINT_UP BUTTON_SCROLL_UP
116 #define ROCKPAINT_DOWN BUTTON_SCROLL_DOWN
117 #define ROCKPAINT_LEFT BUTTON_LEFT
118 #define ROCKPAINT_RIGHT BUTTON_RIGHT
120 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
121 #define ROCKPAINT_QUIT BUTTON_BACK
122 #define ROCKPAINT_DRAW BUTTON_SELECT
123 #define ROCKPAINT_MENU BUTTON_MENU
124 #define ROCKPAINT_TOOLBAR BUTTON_PLAY
125 #define ROCKPAINT_TOOLBAR2 ( BUTTON_PLAY | BUTTON_LEFT )
126 #define ROCKPAINT_UP BUTTON_UP
127 #define ROCKPAINT_DOWN BUTTON_DOWN
128 #define ROCKPAINT_LEFT BUTTON_LEFT
129 #define ROCKPAINT_RIGHT BUTTON_RIGHT
131 #elif ( CONFIG_KEYPAD == COWOND2_PAD )
132 #define ROCKPAINT_QUIT BUTTON_POWER
133 #define ROCKPAINT_MENU BUTTON_MENU
135 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
136 #define ROCKPAINT_QUIT BUTTON_BACK
137 #define ROCKPAINT_DRAW BUTTON_SELECT
138 #define ROCKPAINT_MENU BUTTON_MENU
139 #define ROCKPAINT_TOOLBAR BUTTON_PLAY
140 #define ROCKPAINT_TOOLBAR2 ( BUTTON_PLAY | BUTTON_LEFT )
141 #define ROCKPAINT_UP BUTTON_UP
142 #define ROCKPAINT_DOWN BUTTON_DOWN
143 #define ROCKPAINT_LEFT BUTTON_LEFT
144 #define ROCKPAINT_RIGHT BUTTON_RIGHT
146 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
147 #define ROCKPAINT_QUIT BUTTON_POWER
148 #define ROCKPAINT_DRAW BUTTON_SELECT
149 #define ROCKPAINT_MENU BUTTON_MENU
150 #define ROCKPAINT_TOOLBAR BUTTON_VIEW
151 #define ROCKPAINT_TOOLBAR2 BUTTON_PLAYLIST
152 #define ROCKPAINT_UP BUTTON_UP
153 #define ROCKPAINT_DOWN BUTTON_DOWN
154 #define ROCKPAINT_LEFT BUTTON_LEFT
155 #define ROCKPAINT_RIGHT BUTTON_RIGHT
157 #elif ( CONFIG_KEYPAD == ONDAVX747_PAD )
158 #define ROCKPAINT_QUIT BUTTON_POWER
159 #define ROCKPAINT_MENU BUTTON_MENU
161 #elif CONFIG_KEYPAD == MROBE500_PAD
162 #define ROCKPAINT_QUIT BUTTON_POWER
164 #else
165 #error "Please define keys for this keypad"
166 #endif
168 #ifdef HAVE_TOUCHSCREEN
169 #ifndef ROCKPAINT_QUIT
170 #define ROCKPAINT_QUIT BUTTON_TOPLEFT
171 #endif
172 #ifndef ROCKPAINT_DRAW
173 #define ROCKPAINT_DRAW BUTTON_CENTER
174 #endif
175 #ifndef ROCKPAINT_MENU
176 #define ROCKPAINT_MENU BUTTON_TOPRIGHT
177 #endif
178 #ifndef ROCKPAINT_TOOLBAR
179 #define ROCKPAINT_TOOLBAR BUTTON_BOTTOMLEFT
180 #endif
181 #ifndef ROCKPAINT_TOOLBAR2
182 #define ROCKPAINT_TOOLBAR2 BUTTON_BOTTOMRIGHT
183 #endif
184 #ifndef ROCKPAINT_UP
185 #define ROCKPAINT_UP BUTTON_TOPMIDDLE
186 #endif
187 #ifndef ROCKPAINT_DOWN
188 #define ROCKPAINT_DOWN BUTTON_BOTTOMMIDDLE
189 #endif
190 #ifndef ROCKPAINT_LEFT
191 #define ROCKPAINT_LEFT BUTTON_MIDLEFT
192 #endif
193 #ifndef ROCKPAINT_RIGHT
194 #define ROCKPAINT_RIGHT BUTTON_MIDRIGHT
195 #endif
196 #endif
198 /***********************************************************************
199 * Palette Default Colors
200 ***********************************************************************/
201 #define COLOR_BLACK LCD_RGBPACK(0,0,0)
202 #define COLOR_WHITE LCD_RGBPACK(255,255,255)
203 #define COLOR_DARKGRAY LCD_RGBPACK(128,128,128)
204 #define COLOR_LIGHTGRAY LCD_RGBPACK(192,192,192)
205 #define COLOR_RED LCD_RGBPACK(128,0,0)
206 #define COLOR_LIGHTRED LCD_RGBPACK(255,0,0)
207 #define COLOR_DARKYELLOW LCD_RGBPACK(128,128,0)
208 #define COLOR_YELLOW LCD_RGBPACK(255,255,0)
209 #define COLOR_GREEN LCD_RGBPACK(0,128,0)
210 #define COLOR_LIGHTGREN LCD_RGBPACK(0,255,0)
211 #define COLOR_CYAN LCD_RGBPACK(0,128,128)
212 #define COLOR_LIGHTCYAN LCD_RGBPACK(0,255,255)
213 #define COLOR_BLUE LCD_RGBPACK(0,0,128)
214 #define COLOR_LIGHTBLUE LCD_RGBPACK(0,0,255)
215 #define COLOR_PURPLE LCD_RGBPACK(128,0,128)
216 #define COLOR_PINK LCD_RGBPACK(255,0,255)
217 #define COLOR_BROWN LCD_RGBPACK(128,64,0)
218 #define COLOR_LIGHTBROWN LCD_RGBPACK(255,128,64)
220 #define SPLASH_SCREEN PLUGIN_APPS_DIR "/rockpaint/splash.bmp"
221 #define ROCKPAINT_TITLE_FONT 2
223 /***********************************************************************
224 * Program Colors
225 ***********************************************************************/
226 #define ROCKPAINT_PALETTE LCD_RGBPACK(0,64,128)
227 #define ROCKPAINT_SELECTED LCD_RGBPACK(128,192,255)
229 #define ROWS LCD_HEIGHT
230 #define COLS LCD_WIDTH
233 * Toolbar positioning stuff ... don't read this unless you really need to
235 * TB Toolbar
236 * SP Separator
237 * SC Selected Color
238 * PL Palette
239 * TL Tools
242 /* Separator sizes */
243 #define TB_SP_MARGIN 3
244 #define TB_SP_WIDTH (2+2*TB_SP_MARGIN)
246 /* Selected color sizes */
247 #define TB_SC_SIZE 12
249 /* Palette sizes */
250 #define TB_PL_COLOR_SIZE 7
251 #define TB_PL_COLOR_SPACING 2
252 #define TB_PL_WIDTH ( 9 * TB_PL_COLOR_SIZE + 8 * TB_PL_COLOR_SPACING )
253 #define TB_PL_HEIGHT ( TB_PL_COLOR_SIZE * 2 + TB_PL_COLOR_SPACING )
255 /* Tools sizes */
256 #define TB_TL_SIZE 8
257 #define TB_TL_SPACING 2
258 #define TB_TL_WIDTH ( 7 * ( TB_TL_SIZE + TB_TL_SPACING ) - TB_TL_SPACING )
259 #define TB_TL_HEIGHT ( 2 * TB_TL_SIZE + TB_TL_SPACING )
261 /* Menu button size ... gruik */
262 #define TB_MENU_MIN_WIDTH 30
264 /* Selected colors position */
265 #define TB_SC_FG_TOP 2
266 #define TB_SC_FG_LEFT 2
267 #define TB_SC_BG_TOP (TB_SC_FG_TOP+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
268 #define TB_SC_BG_LEFT (TB_SC_FG_LEFT+TB_PL_COLOR_SIZE*2+TB_PL_COLOR_SPACING-TB_SC_SIZE)
270 /* Palette position */
271 #define TB_PL_TOP TB_SC_FG_TOP
272 #define TB_PL_LEFT (TB_SC_BG_LEFT + TB_SC_SIZE + TB_PL_COLOR_SPACING)
274 /* Tools position */
275 #define TB_TL_TOP TB_SC_FG_TOP
276 #define TB_TL_LEFT ( TB_PL_LEFT + TB_PL_WIDTH-1 + TB_SP_WIDTH )
278 #if TB_TL_LEFT + TB_TL_WIDTH + TB_MENU_MIN_WIDTH >= LCD_WIDTH
279 #undef TB_TL_TOP
280 #undef TB_TL_LEFT
281 #define TB_TL_TOP ( TB_PL_TOP + TB_PL_HEIGHT + 4 )
282 #define TB_TL_LEFT TB_SC_FG_LEFT
283 #endif
285 /* Menu button position */
286 #define TB_MENU_TOP ( TB_TL_TOP + (TB_TL_HEIGHT-8)/2 )
287 #define TB_MENU_LEFT ( TB_TL_LEFT + TB_TL_WIDTH-1 + TB_SP_WIDTH )
289 #define TB_HEIGHT ( TB_TL_TOP + TB_TL_HEIGHT + 1 )
292 static void draw_pixel(int x,int y);
293 static void draw_line( int x1, int y1, int x2, int y2 );
294 static void draw_rect( int x1, int y1, int x2, int y2 );
295 static void draw_toolbars(bool update);
296 static void inv_cursor(bool update);
297 static void restore_screen(void);
298 static void clear_drawing(void);
299 static void goto_menu(void);
300 static int load_bitmap( const char *filename );
301 static int save_bitmap( char *filename );
302 static void draw_rect_full( int x1, int y1, int x2, int y2 );
303 extern int errno;
305 /***********************************************************************
306 * Global variables
307 ***********************************************************************/
309 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
310 int errno;
311 #endif
313 static int drawcolor=0; /* Current color (in palette) */
314 static int bgdrawcolor=9; /* Current background color (in palette) */
315 bool isbg = false; /* gruik ugly hack alert */
317 static int preview=false; /* Is preview mode on ? */
319 /* TODO: clean this up */
320 static int x=0, y=0; /* cursor position */
321 static int prev_x=-1, prev_y=-1; /* previous saved cursor position */
322 static int prev_x2=-1, prev_y2=-1;
323 static int prev_x3=-1, prev_y3=-1;
324 static int tool_mode=-1;
327 static int bsize=1; /* brush size */
328 static int bspeed=1; /* brush speed */
330 enum Tools { Brush = 0, /* Regular brush */
331 Fill = 1, /* Fill a shape with current color */
332 SelectRectangle = 2,
333 ColorPicker = 3, /* Pick a color */
334 Line = 4, /* Draw a line between two points */
335 Unused = 5, /* THIS IS UNUSED ... */
336 Curve = 6,
337 Text = 7,
338 Rectangle = 8, /* Draw a rectangle */
339 RectangleFull = 9,
340 Oval = 10, /* Draw an oval */
341 OvalFull = 11,
342 LinearGradient = 12,
343 RadialGradient = 13
346 enum Tools tool = Brush;
348 static bool quit=false;
349 static int gridsize=0;
351 static fb_data rp_colors[18] =
353 COLOR_BLACK, COLOR_DARKGRAY, COLOR_RED, COLOR_DARKYELLOW,
354 COLOR_GREEN, COLOR_CYAN, COLOR_BLUE, COLOR_PURPLE, COLOR_BROWN,
355 COLOR_WHITE, COLOR_LIGHTGRAY, COLOR_LIGHTRED, COLOR_YELLOW,
356 COLOR_LIGHTGREN, COLOR_LIGHTCYAN, COLOR_LIGHTBLUE, COLOR_PINK,
357 COLOR_LIGHTBROWN
360 static fb_data save_buffer[ ROWS*COLS ];
362 extern fb_data rockpaint[];
363 extern fb_data rockpaint_hsvrgb[];
365 /* Maximum string size allowed for the text tool */
366 #define MAX_TEXT 255
368 static union
370 /* Used by fill and gradient algorithms */
371 struct
373 short x;
374 short y;
375 } coord[ ROWS*COLS ];
377 /* Used by bezier curve algorithms */
378 struct
380 short x1, y1;
381 short x2, y2;
382 short x3, y3;
383 short x4, y4;
384 short depth;
385 } bezier[ (ROWS*COLS)/5 ]; /* We have 4.5 times more data per struct
386 * than coord ... so we divide to take
387 * less memory. */
389 /* Used to cut/copy/paste data */
390 fb_data clipboard[ ROWS*COLS ];
392 /* Used for text mode */
393 struct
395 char text[MAX_TEXT+1];
396 char font[MAX_PATH+1];
397 char old_font[MAX_PATH+1];
398 int fh_buf[30];
399 int fw_buf[30];
400 char fontname_buf[30][MAX_PATH];
401 } text;
402 } buffer;
404 /* Current filename */
405 static char filename[MAX_PATH+1];
407 /* Font preview buffer */
408 //#define FONT_PREVIEW_WIDTH ((LCD_WIDTH-30)/8)
409 //#define FONT_PREVIEW_HEIGHT 1000
410 //static unsigned char fontpreview[FONT_PREVIEW_WIDTH*FONT_PREVIEW_HEIGHT];
412 /***********************************************************************
413 * Offscreen buffer/Text/Fonts handling
415 * Parts of code taken from firmware/drivers/lcd-16bit.c
416 ***********************************************************************/
417 static void buffer_mono_bitmap_part(
418 fb_data *buf, int buf_width, int buf_height,
419 const unsigned char *src, int src_x, int src_y,
420 int stride, int x, int y, int width, int height )
421 /* this function only draws the foreground part of the bitmap */
423 const unsigned char *src_end;
424 fb_data *dst, *dst_end;
425 unsigned fgcolor = rb->lcd_get_foreground();
427 /* nothing to draw? */
428 if( ( width <= 0 ) || ( height <= 0 ) || ( x >= buf_width )
429 || ( y >= buf_height ) || ( x + width <= 0 ) || ( y + height <= 0 ) )
430 return;
432 /* clipping */
433 if( x < 0 )
435 width += x;
436 src_x -= x;
437 x = 0;
439 if( y < 0 )
441 height += y;
442 src_y -= y;
443 y = 0;
445 if( x + width > buf_width )
446 width = buf_width - x;
447 if( y + height > buf_height )
448 height = buf_height - y;
450 src += stride * (src_y >> 3) + src_x; /* move starting point */
451 src_y &= 7;
452 src_end = src + width;
454 dst = buf + y*buf_width + x;
458 const unsigned char *src_col = src++;
459 unsigned data = *src_col >> src_y;
460 fb_data *dst_col = dst++;
461 int numbits = 8 - src_y;
463 dst_end = dst_col + height * buf_width;
466 if( data & 0x01 )
467 *dst_col = fgcolor; /* FIXME ? */
469 dst_col += buf_width;
471 data >>= 1;
472 if( --numbits == 0 )
474 src_col += stride;
475 data = *src_col;
476 numbits = 8;
478 } while( dst_col < dst_end );
479 } while( src < src_end );
482 static void buffer_putsxyofs( fb_data *buf, int buf_width, int buf_height,
483 int x, int y, int ofs, const unsigned char *str )
485 unsigned short ch;
486 unsigned short *ucs;
488 struct font *pf = rb->font_get( FONT_UI );
489 if( !pf ) pf = rb->font_get( FONT_SYSFIXED );
491 ucs = rb->bidi_l2v( str, 1 );
493 while( (ch = *ucs++) != 0 && x < buf_width )
495 int width;
496 const unsigned char *bits;
498 /* get proportional width and glyph bits */
499 width = rb->font_get_width( pf, ch );
501 if( ofs > width )
503 ofs -= width;
504 continue;
507 bits = rb->font_get_bits( pf, ch );
509 buffer_mono_bitmap_part( buf, buf_width, buf_height, bits, ofs, 0, width, x, y, width - ofs, pf->height);
511 x += width - ofs;
512 ofs = 0;
516 /***********************************************************************
517 * Menu handling
518 ***********************************************************************/
519 struct menu_items
521 int value;
522 char label[16]; /* GRUIK ? */
525 #define MENU_ESC -1242
526 enum {
527 /* Generic menu items */
528 MENU_END = -42, MENU_TITLE = -12,
529 /* Main menu */
530 MAIN_MENU_RESUME,
531 MAIN_MENU_NEW, MAIN_MENU_LOAD, MAIN_MENU_SAVE,
532 MAIN_MENU_BRUSH_SIZE, MAIN_MENU_BRUSH_SPEED, MAIN_MENU_COLOR,
533 MAIN_MENU_GRID_SIZE,
534 MAIN_MENU_EXIT,
535 /* Select action menu */
536 SELECT_MENU_CUT, SELECT_MENU_COPY, SELECT_MENU_INVERT,
537 SELECT_MENU_HFLIP, SELECT_MENU_VFLIP, SELECT_MENU_ROTATE90,
538 SELECT_MENU_ROTATE180, SELECT_MENU_ROTATE270,
539 SELECT_MENU_CANCEL,
540 /* Text menu */
541 TEXT_MENU_TEXT, TEXT_MENU_FONT,
542 TEXT_MENU_PREVIEW, TEXT_MENU_APPLY, TEXT_MENU_CANCEL,
545 static struct menu_items main_menu[]=
546 { { MENU_TITLE, "RockPaint" },
547 { MAIN_MENU_RESUME, "Resume" },
548 { MAIN_MENU_NEW, "New" },
549 { MAIN_MENU_LOAD, "Load" },
550 { MAIN_MENU_SAVE, "Save" },
551 { MAIN_MENU_BRUSH_SIZE, "Brush Size" },
552 { MAIN_MENU_BRUSH_SPEED, "Brush Speed" },
553 { MAIN_MENU_COLOR, "Choose Color" },
554 { MAIN_MENU_GRID_SIZE, "Grid Size" },
555 { MAIN_MENU_EXIT, "Exit" },
556 { MENU_END, "" } };
558 static struct menu_items size_menu[] =
559 { { MENU_TITLE, "Choose Size" },
560 { 1, "1x" },
561 { 2, "2x" },
562 { 4, "4x" },
563 { 8, "8x" },
564 { MENU_END, "" } };
566 static struct menu_items speed_menu[] =
567 { { MENU_TITLE, "Choose Speed" },
568 { 1, "1x" },
569 { 2, "2x" },
570 { 4, "4x" },
571 { MENU_END, "" } };
573 static struct menu_items gridsize_menu[] =
574 { { MENU_TITLE, "Grid Size" },
575 { 0, "No grid" },
576 { 5, "5px" },
577 { 10, "10px" },
578 { 20, "20px" },
579 { MENU_END, "" } };
581 static struct menu_items select_menu[] =
582 { { MENU_TITLE, "Select..." },
583 { SELECT_MENU_CUT, "Cut" },
584 { SELECT_MENU_COPY, "Copy" },
585 { SELECT_MENU_INVERT, "Invert" },
586 { SELECT_MENU_HFLIP, "Horizontal flip" },
587 { SELECT_MENU_VFLIP, "Vertical flip" },
588 // { SELECT_MENU_ROTATE90, "Rotate 90°" },
589 { SELECT_MENU_ROTATE180, "Rotate 180°" },
590 // { SELECT_MENU_ROTATE270, "Rotate 270°" },
591 { SELECT_MENU_CANCEL, "Cancel" },
592 { MENU_END, "" } };
594 static struct menu_items text_menu[] =
595 { { MENU_TITLE, "Text" },
596 { TEXT_MENU_TEXT, "Set text" },
597 { TEXT_MENU_FONT, "Change font" },
598 { TEXT_MENU_PREVIEW, "Preview" },
599 { TEXT_MENU_APPLY, "Apply" },
600 { TEXT_MENU_CANCEL, "Cancel" },
601 { MENU_END, "" } };
603 static int draw_window( int height, int width,
604 int *top, int *left,
605 const char *title )
607 int fh;
608 rb->lcd_getstringsize( title, NULL, &fh );
609 fh++;
611 const int _top = ( LCD_HEIGHT - height ) / 2;
612 const int _left = ( LCD_WIDTH - width ) / 2;
613 if( top ) *top = _top;
614 if( left ) *left = _left;
615 rb->lcd_set_background(COLOR_BLUE);
616 rb->lcd_set_foreground(COLOR_LIGHTGRAY);
617 rb->lcd_fillrect( _left, _top, width, height );
618 rb->lcd_set_foreground(COLOR_BLUE);
619 rb->lcd_fillrect( _left, _top, width, fh+4 );
620 rb->lcd_set_foreground(COLOR_WHITE);
621 rb->lcd_putsxy( _left+2, _top+2, title );
622 rb->lcd_set_foreground(COLOR_BLACK);
623 rb->lcd_drawrect( _left, _top, width, height );
624 return _top+fh+4;
627 static int menu_display( struct menu_items menu[], int prev_value )
629 int i,
630 fh, /* font height */
631 width, height,
632 a, b,
633 selection=1,
634 menu_length,
635 top, left;
636 int scroll = 0, onscreen = 0;
638 rb->lcd_getstringsize( menu[0].label, &width, &fh );
639 for( i=1; menu[i].value != MENU_END; i++ )
641 if( prev_value == menu[i].value )
643 selection = i;
645 rb->lcd_getstringsize( menu[i].label, &a, &b );
646 if( a > width ) width = a;
647 if( b > fh ) fh = b;
649 menu_length = i;
650 fh++;
651 width += 10;
653 height = menu_length * fh + 4 + 2 + 2;
654 if( height >= LCD_HEIGHT )
656 scroll = 1;
657 onscreen = ( LCD_HEIGHT - 4 - 2 - 2 )/fh;
658 height = onscreen * fh + 4 + 2 + 2;
659 width += 5;
661 else
663 onscreen = menu_length;
666 draw_window( height, width, &top, &left, menu[0].label );
668 while( 1 )
670 for( i = (scroll == 0 ? 1 : scroll);
671 i < (scroll ? scroll + onscreen - 1:onscreen);
672 i++ )
674 if( i == selection )
676 rb->lcd_set_foreground( COLOR_WHITE );
677 rb->lcd_set_background( COLOR_BLUE );
679 else
681 rb->lcd_set_foreground( COLOR_BLACK );
682 rb->lcd_set_background( COLOR_LIGHTGRAY );
684 rb->lcd_putsxy( left+2,
685 top+6+fh*(i-(scroll == 0 ? 0 : scroll-1 )),
686 menu[i].label );
688 if( scroll )
690 int scroll_height = ((height-fh-4-2)*onscreen)/menu_length;
691 rb->lcd_set_foreground( COLOR_BLACK );
692 rb->lcd_vline( left+width-5, top+fh+4, top+height-2 );
693 rb->lcd_fillrect( left+width-4,
694 top+fh+4+((height-4-2-fh-scroll_height)*(scroll-1))/(menu_length-onscreen),
695 3, scroll_height );
697 rb->lcd_update();
699 switch( rb->button_get(true) )
701 case ROCKPAINT_UP:
702 case ROCKPAINT_UP|BUTTON_REPEAT:
703 selection = (selection + menu_length-1)%menu_length;
704 if( !selection ) selection = menu_length-1;
705 break;
707 case ROCKPAINT_DOWN:
708 case ROCKPAINT_DOWN|BUTTON_REPEAT:
709 selection = (selection + 1)%menu_length;
710 if( !selection ) selection++;
711 break;
713 case ROCKPAINT_LEFT:
714 restore_screen();
715 return MENU_ESC;
717 case ROCKPAINT_RIGHT:
718 case ROCKPAINT_DRAW:
719 restore_screen();
720 return menu[selection].value;
722 if( scroll )
724 if( selection < scroll )
726 scroll = selection;
727 draw_window( height, width, NULL, NULL, menu[0].label );
729 if( selection >= scroll + onscreen - 1 )
731 scroll++;
732 draw_window( height, width, NULL, NULL, menu[0].label );
738 /***********************************************************************
739 * File browser
740 ***********************************************************************/
742 char bbuf[MAX_PATH+1]; /* used by file and font browsers */
743 char bbuf_s[MAX_PATH+1]; /* used by file and font browsers */
745 static bool browse( char *dst, int dst_size, const char *start )
747 #define WIDTH ( LCD_WIDTH - 20 )
748 #define HEIGHT ( LCD_HEIGHT - 20 )
749 #define LINE_SPACE 2
750 int top, top_inside, left;
752 DIR *d;
753 struct dirent *de;
754 int fvi = 0; /* first visible item */
755 int lvi = 0; /* last visible item */
756 int si = 0; /* selected item */
757 int li = 0; /* last item */
758 int i;
760 int fh;
761 char *a;
763 rb->lcd_getstringsize( "Ap", NULL, &fh );
765 rb->strcpy( bbuf, start );
766 a = bbuf+rb->strlen(bbuf)-1;
767 if( *a != '/' )
769 a[1] = '/';
770 a[2] = '\0';
773 while( 1 )
775 d = rb->opendir( bbuf );
776 if( !d )
779 if( errno == ENOTDIR )
781 /* this is a file */
782 bbuf[rb->strlen(bbuf)-1] = '\0';
783 rb->strncpy( dst, bbuf, dst_size );
784 return true;
786 else if( errno == EACCES || errno == ENOENT )
788 bbuf[0] = '/'; bbuf[1] = '\0';
789 d = rb->opendir( "/" );
791 else
793 return false;
796 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, bbuf );
797 i = 0;
798 li = -1;
799 while( i < fvi )
801 rb->readdir( d );
802 i++;
804 while( top_inside+(i-fvi)*(fh+LINE_SPACE) < HEIGHT )
806 de = rb->readdir( d );
807 if( !de )
809 li = i-1;
810 break;
812 rb->lcd_set_foreground((si==i?COLOR_WHITE:COLOR_BLACK));
813 rb->lcd_set_background((si==i?COLOR_BLUE:COLOR_LIGHTGRAY));
814 rb->lcd_putsxy( left+10,
815 top_inside+(i-fvi)*(fh+LINE_SPACE),
816 de->d_name );
817 if( si == i )
818 rb->strcpy( bbuf_s, de->d_name );
819 i++;
821 lvi = i-1;
822 if( li == -1 )
824 if( !rb->readdir( d ) )
826 li = lvi;
829 rb->closedir( d );
831 rb->lcd_update();
833 switch( rb->button_get(true) )
835 case ROCKPAINT_UP:
836 case ROCKPAINT_UP|BUTTON_REPEAT:
837 if( si > 0 )
839 si--;
840 if( si<fvi )
842 fvi--;
845 break;
847 case ROCKPAINT_DOWN:
848 case ROCKPAINT_DOWN|BUTTON_REPEAT:
849 if( li == -1 || si < li )
851 si++;
852 if( si>lvi )
854 fvi++;
857 break;
859 case ROCKPAINT_LEFT:
860 if( bbuf[0] == '/' && !bbuf[1] ) return false;
861 bbuf_s[0] = '.';
862 bbuf_s[1] = '.';
863 bbuf_s[2] = '\0';
864 case ROCKPAINT_RIGHT:
865 case ROCKPAINT_DRAW:
866 if( *bbuf_s == '.' && !bbuf_s[1] ) break;
867 a = bbuf;
868 while( *a ) a++;
869 if( *bbuf_s == '.' && bbuf_s[1] == '.' && !bbuf_s[2] )
871 a--;
872 if( a == bbuf ) break;
873 if( *a == '/' ) a--;
874 while( *a != '/' ) a--;
875 *++a = '\0';
876 break;
878 rb->strcpy( a, bbuf_s );
879 while( *a ) a++;
880 *a++ = '/';
881 *a = '\0';
882 fvi = si = 0;
883 break;
887 #undef WIDTH
888 #undef HEIGHT
889 #undef LINE_SPACE
892 /***********************************************************************
893 * Font browser
895 * FIXME: This still needs some work ... it currently only works fine
896 * on the simulators, disk spins too much on real targets -> rendered
897 * font buffer needed.
898 ***********************************************************************/
899 static bool browse_fonts( char *dst, int dst_size )
901 char old_font[MAX_PATH];
902 #define WIDTH ( LCD_WIDTH - 20 )
903 #define HEIGHT ( LCD_HEIGHT - 20 )
904 #define LINE_SPACE 2
905 int top, top_inside = 0, left;
907 DIR *d;
908 struct dirent *de;
909 int fvi = 0; /* first visible item */
910 int lvi = 0; /* last visible item */
911 int si = 0; /* selected item */
912 int osi = 0; /* old selected item */
913 int li = 0; /* last item */
914 int nvih = 0; /* next visible item height */
915 int i;
916 int b_need_redraw = 1; /* Do we need to redraw ? */
918 int cp = 0; /* current position */
919 int fh; /* font height */
921 #define fh_buf buffer.text.fh_buf /* 30 might not be enough ... */
922 #define fw_buf buffer.text.fw_buf
923 int fw;
924 #define fontname_buf buffer.text.fontname_buf
926 rb->snprintf( old_font, MAX_PATH,
927 FONT_DIR "/%s.fnt",
928 rb->global_settings->font_file );
930 while( 1 )
932 if( !b_need_redraw )
934 /* we don't need to redraw ... but we need to unselect
935 * the previously selected item */
936 cp = top_inside + LINE_SPACE;
937 for( i = 0; i+fvi < osi; i++ )
939 cp += fh_buf[i] + LINE_SPACE;
941 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
942 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
943 rb->lcd_set_drawmode(DRMODE_SOLID);
946 if( b_need_redraw )
948 b_need_redraw = 0;
950 d = rb->opendir( FONT_DIR "/" );
951 if( !d )
953 return false;
955 top_inside = draw_window( HEIGHT, WIDTH, &top, &left, "Fonts" );
956 i = 0;
957 li = -1;
958 while( i < fvi )
960 rb->readdir( d );
961 i++;
963 cp = top_inside+LINE_SPACE;
965 rb->lcd_set_foreground(COLOR_BLACK);
966 rb->lcd_set_background(COLOR_LIGHTGRAY);
968 while( cp < top+HEIGHT )
970 de = rb->readdir( d );
971 if( !de )
973 li = i-1;
974 break;
976 if( rb->strlen( de->d_name ) < 4
977 || rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
978 ".fnt" ) )
979 continue;
980 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
981 de->d_name );
982 rb->font_load( bbuf );
983 rb->font_getstringsize( de->d_name, &fw, &fh, FONT_UI );
984 if( nvih > 0 )
986 nvih -= fh;
987 fvi++;
988 if( nvih < 0 ) nvih = 0;
989 i++;
990 continue;
992 if( cp + fh >= top+HEIGHT )
994 nvih = fh;
995 break;
997 rb->lcd_putsxy( left+10, cp, de->d_name );
998 fh_buf[i-fvi] = fh;
999 fw_buf[i-fvi] = fw;
1000 cp += fh + LINE_SPACE;
1001 rb->strcpy( fontname_buf[i-fvi], bbuf );
1002 i++;
1004 lvi = i-1;
1005 if( li == -1 )
1007 if( !(de = rb->readdir( d ) ) )
1009 li = lvi;
1011 else if( !nvih && !rb->strlen( de->d_name ) < 4
1012 && !rb->strcmp( de->d_name + rb->strlen( de->d_name ) - 4,
1013 ".fnt" ) )
1015 rb->snprintf( bbuf, MAX_PATH, FONT_DIR "/%s",
1016 de->d_name );
1017 rb->font_load( bbuf );
1018 rb->font_getstringsize( de->d_name, NULL, &fh, FONT_UI );
1019 nvih = fh;
1022 rb->font_load( old_font );
1023 rb->closedir( d );
1026 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1027 cp = top_inside + LINE_SPACE;
1028 for( i = 0; i+fvi < si; i++ )
1030 cp += fh_buf[i] + LINE_SPACE;
1032 rb->lcd_fillrect( left+10, cp, fw_buf[i], fh_buf[i] );
1033 rb->lcd_set_drawmode(DRMODE_SOLID);
1035 rb->lcd_update_rect( left, top, WIDTH, HEIGHT );
1037 osi = si;
1038 i = fvi;
1039 switch( rb->button_get(true) )
1041 case ROCKPAINT_UP:
1042 case ROCKPAINT_UP|BUTTON_REPEAT:
1043 if( si > 0 )
1045 si--;
1046 if( si<fvi )
1048 fvi = si;
1051 break;
1053 case ROCKPAINT_DOWN:
1054 case ROCKPAINT_DOWN|BUTTON_REPEAT:
1055 if( li == -1 || si < li )
1057 si++;
1059 break;
1061 case ROCKPAINT_LEFT:
1062 return false;
1064 case ROCKPAINT_RIGHT:
1065 case ROCKPAINT_DRAW:
1066 rb->snprintf( dst, dst_size, "%s", fontname_buf[si-fvi] );
1067 return true;
1070 if( i != fvi || si > lvi )
1072 b_need_redraw = 1;
1075 if( si<=lvi )
1077 nvih = 0;
1080 #undef fh_buf
1081 #undef fw_buf
1082 #undef fontname_buf
1083 #undef WIDTH
1084 #undef HEIGHT
1085 #undef LINE_SPACE
1088 /***********************************************************************
1089 * HSVRGB Color chooser
1090 ***********************************************************************/
1091 static unsigned int color_chooser( unsigned int color )
1093 int red = RGB_UNPACK_RED( color );
1094 int green = RGB_UNPACK_GREEN( color );
1095 int blue = RGB_UNPACK_BLUE( color );
1096 int hue, saturation, value;
1097 int r, g, b; /* temp variables */
1098 int i, top, left;
1100 enum BaseColor { Hue = 0, Saturation = 1, Value = 2,
1101 Red = 3, Green = 4, Blue = 5 };
1102 enum BaseColor current = Red;
1103 bool has_changed;
1105 char str[6] = "";
1107 restore_screen();
1109 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1111 while( 1 )
1113 has_changed = false;
1114 color = LCD_RGBPACK( red, green, blue );
1116 #define HEIGHT ( 100 )
1117 #define WIDTH ( 150 )
1119 top = draw_window( HEIGHT, WIDTH, NULL, &left, "Color chooser" );
1120 top -= 15;
1122 for( i=0; i<100; i++ )
1124 hsv2rgb( i*36, saturation, value, &r, &g, &b );
1125 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1126 rb->lcd_vline( left+15+i, top+20, top+27 );
1127 hsv2rgb( hue, i*255/100, value, &r, &g, &b );
1128 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1129 rb->lcd_vline( left+15+i, top+30, top+37 );
1130 hsv2rgb( hue, saturation, i*255/100, &r, &g, &b );
1131 rb->lcd_set_foreground( LCD_RGBPACK( r, g, b ) );
1132 rb->lcd_vline( left+15+i, top+40, top+47 );
1133 rb->lcd_set_foreground( LCD_RGBPACK( i*255/100, green, blue ) );
1134 rb->lcd_vline( left+15+i, top+50, top+57 );
1135 rb->lcd_set_foreground( LCD_RGBPACK( red, i*255/100, blue ) );
1136 rb->lcd_vline( left+15+i, top+60, top+67 );
1137 rb->lcd_set_foreground( LCD_RGBPACK( red, green, i*255/100 ) );
1138 rb->lcd_vline( left+15+i, top+70, top+77 );
1141 rb->lcd_set_foreground(COLOR_BLACK);
1142 #define POSITION( a, i ) \
1143 rb->lcd_drawpixel( left+14+i, top + 19 + a ); \
1144 rb->lcd_drawpixel( left+16+i, top + 19 + a ); \
1145 rb->lcd_drawpixel( left+14+i, top + 28 + a ); \
1146 rb->lcd_drawpixel( left+16+i, top + 28 + a );
1147 POSITION( 0, hue/36 );
1148 POSITION( 10, saturation*99/255 );
1149 POSITION( 20, value*99/255 );
1150 POSITION( 30, red*99/255 );
1151 POSITION( 40, green*99/255 );
1152 POSITION( 50, blue*99/255 );
1153 #undef POSITION
1154 rb->lcd_set_background(COLOR_LIGHTGRAY);
1155 rb->lcd_setfont( FONT_SYSFIXED );
1156 rb->snprintf( str, 6, "%d", hue/10 );
1157 rb->lcd_putsxy( left + 117, top + 20, str );
1158 rb->snprintf( str, 6, "%d.%d", saturation/255, ((saturation*100)/255)%100 );
1159 rb->lcd_putsxy( left + 117, top + 30, str );
1160 rb->snprintf( str, 6, "%d.%d", value/255, ((value*100)/255)%100 );
1161 rb->lcd_putsxy( left + 117, top + 40, str );
1162 rb->snprintf( str, 6, "%d", red );
1163 rb->lcd_putsxy( left + 117, top + 50, str );
1164 rb->snprintf( str, 6, "%d", green );
1165 rb->lcd_putsxy( left + 117, top + 60, str );
1166 rb->snprintf( str, 6, "%d", blue );
1167 rb->lcd_putsxy( left + 117, top + 70, str );
1168 rb->lcd_setfont( FONT_UI );
1170 #define CURSOR( l ) \
1171 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 1, 1, 16, left+l+1, top+20, 6, 58 ); \
1172 rb->lcd_bitmap_transparent_part( rockpaint_hsvrgb, 8, 10*current, 16, left+l, top+19+10*current, 8, 10 );
1173 CURSOR( 5 );
1174 #undef CURSOR
1176 rb->lcd_set_foreground( color );
1177 rb->lcd_fillrect( left+15, top+85, 100, 8 );
1179 rb->lcd_update();
1181 switch( rb->button_get(true) )
1183 case ROCKPAINT_UP:
1184 current = ( current + 5 )%6;
1185 break;
1187 case ROCKPAINT_DOWN:
1188 current = (current + 1 )%6;
1189 break;
1191 case ROCKPAINT_LEFT:
1192 has_changed = true;
1193 switch( current )
1195 case Hue:
1196 hue = ( hue + 3600 - 10 )%3600;
1197 break;
1198 case Saturation:
1199 if( saturation ) saturation--;
1200 break;
1201 case Value:
1202 if( value ) value--;
1203 break;
1204 case Red:
1205 if( red ) red--;
1206 break;
1207 case Green:
1208 if( green ) green--;
1209 break;
1210 case Blue:
1211 if( blue ) blue--;
1212 break;
1214 break;
1216 case ROCKPAINT_LEFT|BUTTON_REPEAT:
1217 has_changed = true;
1218 switch( current )
1220 case Hue:
1221 hue = ( hue + 3600 - 100 )%3600;
1222 break;
1223 case Saturation:
1224 if( saturation >= 8 ) saturation-=8;
1225 else saturation = 0;
1226 break;
1227 case Value:
1228 if( value >= 8 ) value-=8;
1229 else value = 0;
1230 break;
1231 case Red:
1232 if( red >= 8 ) red-=8;
1233 else red = 0;
1234 break;
1235 case Green:
1236 if( green >= 8 ) green-=8;
1237 else green = 0;
1238 break;
1239 case Blue:
1240 if( blue >= 8 ) blue-=8;
1241 else blue = 0;
1242 break;
1244 break;
1246 case ROCKPAINT_RIGHT:
1247 has_changed = true;
1248 switch( current )
1250 case Hue:
1251 hue = ( hue + 10 )%3600;
1252 break;
1253 case Saturation:
1254 if( saturation < 0xff ) saturation++;
1255 break;
1256 case Value:
1257 if( value < 0xff ) value++;
1258 break;
1259 case Red:
1260 if( red < 0xff ) red++;
1261 break;
1262 case Green:
1263 if( green < 0xff ) green++;
1264 break;
1265 case Blue:
1266 if( blue < 0xff ) blue++;
1267 break;
1269 break;
1271 case ROCKPAINT_RIGHT|BUTTON_REPEAT:
1272 has_changed = true;
1273 switch( current )
1275 case Hue:
1276 hue = ( hue + 100 )%3600;
1277 break;
1278 case Saturation:
1279 if( saturation < 0xff - 8 ) saturation+=8;
1280 else saturation = 0xff;
1281 break;
1282 case Value:
1283 if( value < 0xff - 8 ) value+=8;
1284 else value = 0xff;
1285 break;
1286 case Red:
1287 if( red < 0xff - 8 ) red+=8;
1288 else red = 0xff;
1289 break;
1290 case Green:
1291 if( green < 0xff - 8 ) green+=8;
1292 else green = 0xff;
1293 break;
1294 case Blue:
1295 if( blue < 0xff - 8 ) blue+=8;
1296 else blue = 0xff;
1297 break;
1299 break;
1301 case ROCKPAINT_DRAW:
1302 return color;
1304 if( has_changed )
1306 switch( current )
1308 case Hue:
1309 case Saturation:
1310 case Value:
1311 hsv2rgb( hue, saturation, value, &red, &green, &blue );
1312 break;
1314 case Red:
1315 case Green:
1316 case Blue:
1317 rgb2hsv( red, green, blue, &hue, &saturation, &value );
1318 break;
1321 #undef HEIGHT
1322 #undef WIDTH
1326 /***********************************************************************
1327 * Misc routines
1328 ***********************************************************************/
1329 static void init_buffer(void)
1331 int i;
1332 fb_data color = rp_colors[ bgdrawcolor ];
1333 for( i = 0; i < ROWS*COLS; i++ )
1335 save_buffer[i] = color;
1339 static void draw_pixel(int x,int y)
1341 if( !preview )
1343 if( x < 0 || x >= COLS || y < 0 || y >= ROWS ) return;
1344 if( isbg )
1346 save_buffer[ x+y*COLS ] = rp_colors[bgdrawcolor];
1348 else
1350 save_buffer[ x+y*COLS ] = rp_colors[drawcolor];
1353 rb->lcd_drawpixel(x,y);
1356 static void color_picker( int x, int y )
1358 if( preview )
1360 rb->lcd_set_foreground( save_buffer[ x+y*COLS ] );
1361 #define PSIZE 12
1362 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1363 rb->lcd_drawrect( x<COLS-PSIZE ? x + 2 : x - PSIZE, y<ROWS-PSIZE ? y + 2: y - PSIZE, PSIZE - 2, PSIZE - 2 );
1364 rb->lcd_set_drawmode(DRMODE_SOLID);
1365 rb->lcd_fillrect( x<COLS-PSIZE ? x+3 : x - PSIZE+1, y<ROWS-PSIZE ? y +3: y - PSIZE+1, PSIZE-4, PSIZE-4 );
1366 #undef PSIZE
1367 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1369 else
1371 rp_colors[ drawcolor ] = save_buffer[ x+y*COLS ];
1375 static void draw_select_rectangle( int x1, int y1, int x2, int y2 )
1376 /* This is a preview mode only function */
1378 int i,a;
1379 if( x1 > x2 )
1381 i = x1;
1382 x1 = x2;
1383 x2 = i;
1385 if( y1 > y2 )
1387 i = y1;
1388 y1 = y2;
1389 y2 = i;
1391 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1392 i = 0;
1393 for( a = x1; a < x2; a++, i++ )
1394 if( i%2 )
1395 rb->lcd_drawpixel( a, y1 );
1396 for( a = y1; a < y2; a++, i++ )
1397 if( i%2 )
1398 rb->lcd_drawpixel( x2, a );
1399 if( y2 != y1 )
1400 for( a = x2; a > x1; a--, i++ )
1401 if( i%2 )
1402 rb->lcd_drawpixel( a, y2 );
1403 if( x2 != x1 )
1404 for( a = y2; a > y1; a--, i++ )
1405 if( i%2 )
1406 rb->lcd_drawpixel( x1, a );
1407 rb->lcd_set_drawmode(DRMODE_SOLID);
1410 static void copy_to_clipboard( void )
1412 /* This needs to be optimised ... but i'm lazy ATM */
1413 rb->memcpy( buffer.clipboard, save_buffer, COLS*ROWS*sizeof( fb_data ) );
1416 /* no preview mode handling atm ... do we need it ? (one if) */
1417 static void draw_invert( int x1, int y1, int x2, int y2 )
1419 int i;
1420 if( x1 > x2 )
1422 i = x1;
1423 x1 = x2;
1424 x2 = i;
1426 if( y1 > y2 )
1428 i = y1;
1429 y1 = y2;
1430 y2 = i;
1433 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1434 rb->lcd_fillrect( x1, y1, x2-x1+1, y2-y1+1 );
1435 rb->lcd_set_drawmode(DRMODE_SOLID);
1437 for( ; y1<=y2; y1++ )
1439 for( i = x1; i<=x2; i++ )
1441 save_buffer[ y1*COLS + i ] = ~save_buffer[ y1*COLS + i ];
1444 /*if( update )*/ rb->lcd_update();
1447 static void draw_hflip( int x1, int y1, int x2, int y2 )
1449 int i;
1450 if( x1 > x2 )
1452 i = x1;
1453 x1 = x2;
1454 x2 = i;
1456 if( y1 > y2 )
1458 i = y1;
1459 y1 = y2;
1460 y2 = i;
1463 copy_to_clipboard();
1465 for( i = 0; i <= y2 - y1; i++ )
1467 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1468 buffer.clipboard+(y2-i)*COLS+x1,
1469 (x2-x1+1)*sizeof( fb_data ) );
1471 restore_screen();
1472 rb->lcd_update();
1475 static void draw_vflip( int x1, int y1, int x2, int y2 )
1477 int i;
1478 if( x1 > x2 )
1480 i = x1;
1481 x1 = x2;
1482 x2 = i;
1484 if( y1 > y2 )
1486 i = y1;
1487 y1 = y2;
1488 y2 = i;
1491 copy_to_clipboard();
1493 for( ; y1 <= y2; y1++ )
1495 for( i = 0; i <= x2 - x1; i++ )
1497 save_buffer[y1*COLS+x1+i] = buffer.clipboard[y1*COLS+x2-i];
1500 restore_screen();
1501 rb->lcd_update();
1504 static void draw_paste_rectangle( int src_x1, int src_y1, int src_x2,
1505 int src_y2, int x1, int y1, int mode )
1507 int i;
1508 if( mode == SELECT_MENU_CUT )
1510 i = drawcolor;
1511 drawcolor = bgdrawcolor;
1512 draw_rect_full( src_x1, src_y1, src_x2, src_y2 );
1513 drawcolor = i;
1515 if( src_x1 > src_x2 )
1517 i = src_x1;
1518 src_x1 = src_x2;
1519 src_x2 = i;
1521 if( src_y1 > src_y2 )
1523 i = src_y1;
1524 src_y1 = src_y2;
1525 src_y2 = i;
1527 rb->lcd_bitmap_part( buffer.clipboard, src_x1, src_y1, COLS,
1528 x1, y1, src_x2-src_x1+1, src_y2-src_y1+1 );
1529 if( !preview )
1531 for( i = 0; i <= src_y2 - src_y1; i++ )
1533 rb->memcpy( save_buffer+(y1+i)*COLS+x1,
1534 buffer.clipboard+(src_y1+i)*COLS+src_x1,
1535 (src_x2 - src_x1 + 1)*sizeof( fb_data ) );
1540 static void show_grid( bool update )
1542 int i;
1543 if( gridsize > 0 )
1545 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1546 for( i = gridsize; i < COLS; i+= gridsize )
1548 rb->lcd_vline( i, 0, ROWS-1 );
1550 for( i = gridsize; i < ROWS; i+= gridsize )
1552 rb->lcd_hline( 0, COLS-1, i );
1554 rb->lcd_set_drawmode(DRMODE_SOLID);
1555 if( update ) rb->lcd_update();
1559 static void draw_text( int x, int y )
1561 buffer.text.text[0] = '\0';
1562 rb->snprintf( buffer.text.old_font, MAX_PATH,
1563 FONT_DIR "/%s.fnt",
1564 rb->global_settings->font_file );
1565 while( 1 )
1567 int m = TEXT_MENU_TEXT;
1568 switch( m = menu_display( text_menu, m ) )
1570 case TEXT_MENU_TEXT:
1571 rb->kbd_input( buffer.text.text, MAX_TEXT );
1572 restore_screen();
1573 break;
1575 case TEXT_MENU_FONT:
1576 if( browse_fonts( buffer.text.font, MAX_PATH ) )
1578 rb->font_load( buffer.text.font );
1580 break;
1582 case TEXT_MENU_PREVIEW:
1583 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1584 while( 1 )
1586 unsigned int button;
1587 restore_screen();
1588 rb->lcd_putsxy( x, y, buffer.text.text );
1589 rb->lcd_update();
1590 switch( button = rb->button_get( true ) )
1592 case ROCKPAINT_LEFT:
1593 case ROCKPAINT_LEFT | BUTTON_REPEAT:
1594 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1595 if (x<0) x=COLS-1;
1596 break;
1598 case ROCKPAINT_RIGHT:
1599 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
1600 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1601 if (x>=COLS) x=0;
1602 break;
1604 case ROCKPAINT_UP:
1605 case ROCKPAINT_UP | BUTTON_REPEAT:
1606 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1607 if (y<0) y=ROWS-1;
1608 break;
1610 case ROCKPAINT_DOWN:
1611 case ROCKPAINT_DOWN | BUTTON_REPEAT:
1612 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
1613 if (y>=ROWS-1) y=0;
1614 break;
1616 case ROCKPAINT_DRAW:
1617 button = 1242;
1618 break;
1620 if( button == 1242 ) break;
1622 break;
1624 case TEXT_MENU_APPLY:
1625 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1626 buffer_putsxyofs( save_buffer, COLS, ROWS, x, y, 0,
1627 buffer.text.text );
1628 case TEXT_MENU_CANCEL:
1629 restore_screen();
1630 rb->font_load( buffer.text.old_font );
1631 return;
1636 static void draw_brush( int x, int y )
1638 int i,j;
1639 for( i=-bsize/2+(bsize+1)%2; i<=bsize/2; i++ )
1641 for( j=-bsize/2+(bsize+1)%2; j<=bsize/2; j++ )
1643 draw_pixel( x+i, y+j );
1648 /* This is an implementation of Bresenham's line algorithm.
1649 * See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm.
1651 static void draw_line( int x1, int y1, int x2, int y2 )
1653 int x = x1;
1654 int y = y1;
1655 int deltax = x2 - x1;
1656 int deltay = y2 - y1;
1657 int i;
1659 int xerr = abs(deltax);
1660 int yerr = abs(deltay);
1661 int xstep = deltax > 0 ? 1 : -1;
1662 int ystep = deltay > 0 ? 1 : -1;
1663 int err;
1665 if (yerr > xerr)
1667 /* more vertical */
1668 err = yerr;
1669 xerr <<= 1;
1670 yerr <<= 1;
1672 /* to leave off the last pixel of the line, leave off the "+ 1" */
1673 for (i = abs(deltay) + 1; i; --i)
1675 draw_pixel(x, y);
1676 y += ystep;
1677 err -= xerr;
1678 if (err < 0) {
1679 x += xstep;
1680 err += yerr;
1684 else
1686 /* more horizontal */
1687 err = xerr;
1688 xerr <<= 1;
1689 yerr <<= 1;
1691 for (i = abs(deltax) + 1; i; --i)
1693 draw_pixel(x, y);
1694 x += xstep;
1695 err -= yerr;
1696 if (err < 0) {
1697 y += ystep;
1698 err += xerr;
1704 static void draw_curve( int x1, int y1, int x2, int y2,
1705 int xa, int ya, int xb, int yb )
1707 int i = 0;
1708 short xl1, yl1;
1709 short xl2, yl2;
1710 short xl3, yl3;
1711 short xl4, yl4;
1712 short xr1, yr1;
1713 short xr2, yr2;
1714 short xr3, yr3;
1715 short xr4, yr4;
1716 short depth;
1717 short xh, yh;
1719 if( x1 == x2 && y1 == y2 )
1721 draw_pixel( x1, y1 );
1722 return;
1725 // if( preview )
1727 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1728 if( xa == -1 || ya == -1 )
1730 rb->lcd_drawline( x1, y1, xb, yb );
1731 rb->lcd_drawline( x2, y2, xb, yb );
1733 else
1735 rb->lcd_drawline( x1, y1, xa, ya );
1736 rb->lcd_drawline( x2, y2, xb, yb );
1738 rb->lcd_set_drawmode(DRMODE_SOLID);
1741 if( xa == -1 || ya == -1 )
1742 /* We only have 3 of the points
1743 * This will currently only be used in preview mode */
1745 #define PUSH( a1, b1, a2, b2, a3, b3, d ) \
1746 buffer.bezier[i].x1 = a1; \
1747 buffer.bezier[i].y1 = b1; \
1748 buffer.bezier[i].x2 = a2; \
1749 buffer.bezier[i].y2 = b2; \
1750 buffer.bezier[i].x3 = a3; \
1751 buffer.bezier[i].y3 = b3; \
1752 buffer.bezier[i].depth = d; \
1753 i++;
1754 #define POP( a1, b1, a2, b2, a3, b3, d ) \
1755 i--; \
1756 a1 = buffer.bezier[i].x1; \
1757 b1 = buffer.bezier[i].y1; \
1758 a2 = buffer.bezier[i].x2; \
1759 b2 = buffer.bezier[i].y2; \
1760 a3 = buffer.bezier[i].x3; \
1761 b3 = buffer.bezier[i].y3; \
1762 d = buffer.bezier[i].depth;
1763 PUSH( x1<<4, y1<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1764 while( i )
1766 /* de Casteljau's algorithm (see wikipedia) */
1767 POP( xl1, yl1, xb, yb, xr3, yr3, depth );
1768 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1770 xl2 = ( xl1 + xb )>>1;
1771 yl2 = ( yl1 + yb )>>1;
1772 xr2 = ( xb + xr3 )>>1;
1773 yr2 = ( yb + yr3 )>>1;
1774 xr1 = ( xl2 + xr2 )>>1;
1775 yr1 = ( yl2 + yr2 )>>1;
1776 xl3 = xr1;
1777 yl3 = yr1;
1778 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, depth+1 );
1779 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, depth+1 );
1781 else
1783 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1784 ((xr3>>3)+1)>>1, ((yr3>>3)+1)>>1 );
1787 #undef PUSH
1788 #undef POP
1790 else /* We have the 4 points */
1792 #define PUSH( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1793 buffer.bezier[i].x1 = a1; \
1794 buffer.bezier[i].y1 = b1; \
1795 buffer.bezier[i].x2 = a2; \
1796 buffer.bezier[i].y2 = b2; \
1797 buffer.bezier[i].x3 = a3; \
1798 buffer.bezier[i].y3 = b3; \
1799 buffer.bezier[i].x4 = a4; \
1800 buffer.bezier[i].y4 = b4; \
1801 buffer.bezier[i].depth = d; \
1802 i++;
1803 #define POP( a1, b1, a2, b2, a3, b3, a4, b4, d ) \
1804 i--; \
1805 a1 = buffer.bezier[i].x1; \
1806 b1 = buffer.bezier[i].y1; \
1807 a2 = buffer.bezier[i].x2; \
1808 b2 = buffer.bezier[i].y2; \
1809 a3 = buffer.bezier[i].x3; \
1810 b3 = buffer.bezier[i].y3; \
1811 a4 = buffer.bezier[i].x4; \
1812 b4 = buffer.bezier[i].y4; \
1813 d = buffer.bezier[i].depth;
1815 PUSH( x1<<4, y1<<4, xa<<4, ya<<4, xb<<4, yb<<4, x2<<4, y2<<4, 0 );
1816 while( i )
1818 /* de Casteljau's algorithm (see wikipedia) */
1819 POP( xl1, yl1, xa, ya, xb, yb, xr4, yr4, depth );
1820 if( depth < 10 ) /* check that the stack's 'i' doesn't overflow */
1822 xl2 = ( xl1 + xa )>>1;
1823 yl2 = ( yl1 + ya )>>1;
1824 xh = ( xa + xb )>>1;
1825 yh = ( ya + yb )>>1;
1826 xr3 = ( xb + xr4 )>>1;
1827 yr3 = ( yb + yr4 )>>1;
1828 xl3 = ( xl2 + xh )>>1;
1829 yl3 = ( yl2 + yh )>>1;
1830 xr2 = ( xr3 + xh )>>1;
1831 yr2 = ( yr3 + yh )>>1;
1832 xl4 = ( xl3 + xr2 )>>1;
1833 yl4 = ( yl3 + yr2 )>>1;
1834 xr1 = xl4;
1835 yr1 = yl4;
1836 PUSH( xl1, yl1, xl2, yl2, xl3, yl3, xl4, yl4, depth+1 );
1837 PUSH( xr1, yr1, xr2, yr2, xr3, yr3, xr4, yr4, depth+1 );
1839 else
1841 draw_line( ((xl1>>3)+1)>>1, ((yl1>>3)+1)>>1,
1842 ((xr4>>3)+1)>>1, ((yr4>>3)+1)>>1 );
1845 #undef PUSH
1846 #undef POP
1850 static void draw_rect( int x1, int y1, int x2, int y2 )
1852 draw_line( x1, y1, x1, y2 );
1853 draw_line( x1, y1, x2, y1 );
1854 draw_line( x1, y2, x2, y2 );
1855 draw_line( x2, y1, x2, y2 );
1858 static void togglebg( void )
1860 if( isbg )
1862 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
1864 else
1866 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
1868 isbg = !isbg;
1871 static void draw_rect_full( int x1, int y1, int x2, int y2 )
1873 /* GRUIK */
1874 int x = x1;
1875 togglebg();
1876 if( x < x2 )
1878 do {
1879 draw_line( x, y1, x, y2 );
1880 } while( ++x <= x2 );
1882 else
1884 do {
1885 draw_line( x, y1, x, y2 );
1886 } while( --x >= x2 );
1888 togglebg();
1889 draw_rect( x1, y1, x2, y2 );
1892 static void draw_oval( int x1, int y1, int x2, int y2, bool full )
1894 /* TODO: simplify :) */
1895 int cx = (x1+x2)>>1;
1896 int cy = (y1+y2)>>1;
1898 int rx = (x1-x2)>>1;
1899 int ry = (y1-y2)>>1;
1900 if( rx < 0 ) rx *= -1;
1901 if( ry < 0 ) ry *= -1;
1903 if( rx == 0 || ry == 0 )
1905 draw_line( x1, y1, x2, y2 );
1906 return;
1909 int x,y;
1910 int dst, old_dst;
1912 for( x = 0; x < rx; x++ )
1914 y = 0;
1915 dst = -0xfff;
1916 do {
1917 old_dst = dst;
1918 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1919 y++;
1920 } while( dst < 0 );
1921 if( -old_dst < dst ) y--;
1922 if( full )
1924 draw_line( cx+x, cy, cx+x, cy+y );
1925 draw_line( cx+x, cy, cx+x, cy-y );
1926 draw_line( cx-x, cy, cx-x, cy+y );
1927 draw_line( cx-x, cy, cx-x, cy-y );
1929 else
1931 draw_pixel( cx+x, cy+y );
1932 draw_pixel( cx+x, cy-y );
1933 draw_pixel( cx-x, cy+y );
1934 draw_pixel( cx-x, cy-y );
1937 for( y = 0; y < ry; y++ )
1939 x = 0;
1940 dst = -0xfff;
1941 do {
1942 old_dst = dst;
1943 dst = ry * ry * x * x + rx * rx * y * y - rx * rx * ry * ry;
1944 x++;
1945 } while( dst < 0 );
1946 if( -old_dst < dst ) x--;
1947 if( full )
1949 draw_line( cx+x, cy, cx+x, cy+y );
1950 draw_line( cx+x, cy, cx+x, cy-y );
1951 draw_line( cx-x, cy, cx-x, cy+y );
1952 draw_line( cx-x, cy, cx-x, cy-y );
1954 else
1956 draw_pixel( cx+x, cy+y );
1957 draw_pixel( cx+x, cy-y );
1958 draw_pixel( cx-x, cy+y );
1959 draw_pixel( cx-x, cy-y );
1964 static void draw_oval_empty( int x1, int y1, int x2, int y2 )
1966 draw_oval( x1, y1, x2, y2, false );
1969 static void draw_oval_full( int x1, int y1, int x2, int y2 )
1971 togglebg();
1972 draw_oval( x1, y1, x2, y2, true );
1973 togglebg();
1974 draw_oval( x1, y1, x2, y2, false );
1977 static void draw_fill( int x0, int y0 )
1979 #define PUSH( a, b ) \
1980 draw_pixel( (int)a, (int)b ); \
1981 buffer.coord[i].x = a; \
1982 buffer.coord[i].y = b; \
1983 i++;
1984 #define POP( a, b ) \
1985 i--; \
1986 a = buffer.coord[i].x; \
1987 b = buffer.coord[i].y;
1989 unsigned int i=0;
1990 short x = x0;
1991 short y = y0;
1992 unsigned int prev_color = save_buffer[ x0+y0*COLS ];
1994 if( prev_color == rp_colors[ drawcolor ] ) return;
1996 PUSH( x, y );
1998 while( i != 0 )
2000 POP( x, y );
2001 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2003 PUSH( x-1, y );
2005 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2007 PUSH( x+1, y );
2009 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2011 PUSH( x, y-1 );
2013 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2015 PUSH( x, y+1 );
2018 #undef PUSH
2019 #undef POP
2023 /* For preview purposes only */
2024 static void line_gradient( int x1, int y1, int x2, int y2 )
2026 int r1, g1, b1;
2027 int r2, g2, b2;
2028 int h1, s1, v1, h2, s2, v2, r, g, b;
2029 int w, h, x, y;
2031 bool a = false;
2033 x1 <<= 1;
2034 y1 <<= 1;
2035 x2 <<= 1;
2036 y2 <<= 1;
2038 w = x1 - x2;
2039 h = y1 - y2;
2041 if( w == 0 && h == 0 )
2043 draw_pixel( x1>>1, y1>>1 );
2044 return;
2047 r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2048 g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2049 b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2050 r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2051 g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2052 b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2054 if( w < 0 )
2056 w *= -1;
2057 a = true;
2059 if( h < 0 )
2061 h *= -1;
2062 a = !a;
2064 if( a )
2066 r = r1;
2067 r1 = r2;
2068 r2 = r;
2069 g = g1;
2070 g1 = g2;
2071 g2 = g;
2072 b = b1;
2073 b1 = b2;
2074 b2 = b;
2077 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2078 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2080 if( w > h )
2082 if( x1 > x2 )
2084 x = x2;
2085 y = y2;
2086 x2 = x1;
2087 y2 = y1;
2088 x1 = x;
2089 y1 = y;
2091 w = x1 - x2;
2092 h = y1 - y2;
2093 while( x1 <= x2 )
2095 hsv2rgb( h1+((h2-h1)*(x1-x2))/w,
2096 s1+((s2-s1)*(x1-x2))/w,
2097 v1+((v2-v1)*(x1-x2))/w,
2098 &r, &g, &b );
2099 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2100 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2101 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2102 x1+=2;
2103 y1 = y2 - ( x2 - x1 ) * h / w;
2106 else /* h > w */
2108 if( y1 > y2 )
2110 x = x2;
2111 y = y2;
2112 x2 = x1;
2113 y2 = y1;
2114 x1 = x;
2115 y1 = y;
2117 w = x1 - x2;
2118 h = y1 - y2;
2119 while( y1 <= y2 )
2121 hsv2rgb( h1+((h2-h1)*(y1-y2))/h,
2122 s1+((s2-s1)*(y1-y2))/h,
2123 v1+((v2-v1)*(y1-y2))/h,
2124 &r, &g, &b );
2125 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2126 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2127 draw_pixel( (x1+1)>>1, (y1+1)>>1 );
2128 y1+=2;
2129 x1 = x2 - ( y2 - y1 ) * w / h;
2132 if( a )
2134 rp_colors[ drawcolor ] = LCD_RGBPACK( r1, g1, b1 );
2136 else
2138 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2142 static void linear_gradient( int x1, int y1, int x2, int y2 )
2144 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2145 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2146 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2147 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2148 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2149 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2151 int h1, s1, v1, h2, s2, v2, r, g, b;
2153 /* radius^2 */
2154 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2155 int dist2, i=0;
2157 /* We only propagate the gradient to neighboring pixels with the same
2158 * color as ( x1, y1 ) */
2159 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2161 int x = x1;
2162 int y = y1;
2164 if( radius2 == 0 ) return;
2165 if( preview )
2167 line_gradient( x1, y1, x2, y2 );
2170 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2171 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2173 #define PUSH( x0, y0 ) \
2174 buffer.coord[i].x = (short)(x0); \
2175 buffer.coord[i].y = (short)(y0); \
2176 i++;
2177 #define POP( a, b ) \
2178 i--; \
2179 a = (int)buffer.coord[i].x; \
2180 b = (int)buffer.coord[i].y;
2182 PUSH( x, y );
2184 while( i != 0 )
2186 POP( x, y );
2188 dist2 = ( x2 - x1 ) * ( x - x1 ) + ( y2 - y1 ) * ( y - y1 );
2189 if( dist2 <= 0 )
2191 rp_colors[ drawcolor ] = rp_colors[ bgdrawcolor ];
2193 else if( dist2 < radius2 )
2195 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2196 s1+((s2-s1)*dist2)/radius2,
2197 v1+((v2-v1)*dist2)/radius2,
2198 &r, &g, &b );
2199 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2201 else
2203 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2205 if( rp_colors[ drawcolor ] == prev_color )
2207 if( rp_colors[ drawcolor ])
2208 rp_colors[ drawcolor ]--; /* GRUIK */
2209 else
2210 rp_colors[ drawcolor ]++; /* GRUIK */
2212 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2213 draw_pixel( x, y );
2215 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2217 PUSH( x-1, y );
2219 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2221 PUSH( x+1, y );
2223 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2225 PUSH( x, y-1 );
2227 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2229 PUSH( x, y+1 );
2232 #undef PUSH
2233 #undef POP
2235 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2238 static void radial_gradient( int x1, int y1, int x2, int y2 )
2240 int r1 = RGB_UNPACK_RED( rp_colors[ bgdrawcolor ] );
2241 int g1 = RGB_UNPACK_GREEN( rp_colors[ bgdrawcolor ] );
2242 int b1 = RGB_UNPACK_BLUE( rp_colors[ bgdrawcolor ] );
2243 int r2 = RGB_UNPACK_RED( rp_colors[ drawcolor ] );
2244 int g2 = RGB_UNPACK_GREEN( rp_colors[ drawcolor ] );
2245 int b2 = RGB_UNPACK_BLUE( rp_colors[ drawcolor ] );
2247 int h1, s1, v1, h2, s2, v2, r, g, b;
2249 /* radius^2 */
2250 int radius2 = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
2251 int dist2, i=0;
2253 /* We only propagate the gradient to neighboring pixels with the same
2254 * color as ( x1, y1 ) */
2255 unsigned int prev_color = save_buffer[ x1+y1*COLS ];
2257 int x = x1;
2258 int y = y1;
2260 if( radius2 == 0 ) return;
2261 if( preview )
2263 line_gradient( x1, y1, x2, y2 );
2266 rgb2hsv( r1, g1, b1, &h1, &s1, &v1 );
2267 rgb2hsv( r2, g2, b2, &h2, &s2, &v2 );
2269 #define PUSH( x0, y0 ) \
2270 buffer.coord[i].x = (short)(x0); \
2271 buffer.coord[i].y = (short)(y0); \
2272 i++;
2273 #define POP( a, b ) \
2274 i--; \
2275 a = (int)buffer.coord[i].x; \
2276 b = (int)buffer.coord[i].y;
2278 PUSH( x, y );
2280 while( i != 0 )
2282 POP( x, y );
2284 if( ( dist2 = (x1-(x))*(x1-(x))+(y1-(y))*(y1-(y)) ) < radius2 )
2286 hsv2rgb( h1+((h2-h1)*dist2)/radius2,
2287 s1+((s2-s1)*dist2)/radius2,
2288 v1+((v2-v1)*dist2)/radius2,
2289 &r, &g, &b );
2290 rp_colors[ drawcolor ] = LCD_RGBPACK( r, g, b );
2292 else
2294 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2296 if( rp_colors[ drawcolor ] == prev_color )
2298 if( rp_colors[ drawcolor ])
2299 rp_colors[ drawcolor ]--; /* GRUIK */
2300 else
2301 rp_colors[ drawcolor ]++; /* GRUIK */
2303 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2304 draw_pixel( x, y );
2306 if( x > 0 && save_buffer[x-1+y*COLS] == prev_color )
2308 PUSH( x-1, y );
2310 if( x < COLS-1 && save_buffer[x+1+y*COLS] == prev_color )
2312 PUSH( x+1, y );
2314 if( y > 0 && save_buffer[x+(y-1)*COLS] == prev_color )
2316 PUSH( x, y-1 );
2318 if( y < ROWS - 1 && save_buffer[x+(y+1)*COLS] == prev_color )
2320 PUSH( x, y+1 );
2323 #undef PUSH
2324 #undef POP
2326 rp_colors[ drawcolor ] = LCD_RGBPACK( r2, g2, b2 );
2329 static void draw_toolbars(bool update)
2331 int i;
2332 #define TOP (LCD_HEIGHT-TB_HEIGHT)
2333 rb->lcd_set_background( COLOR_LIGHTGRAY );
2334 rb->lcd_set_foreground( COLOR_LIGHTGRAY );
2335 rb->lcd_fillrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2336 rb->lcd_set_foreground( COLOR_BLACK );
2337 rb->lcd_drawrect( 0, TOP, LCD_WIDTH, TB_HEIGHT );
2339 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2340 rb->lcd_fillrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2341 TB_SC_SIZE, TB_SC_SIZE );
2342 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2343 rb->lcd_drawrect( TB_SC_BG_LEFT, TOP+TB_SC_BG_TOP,
2344 TB_SC_SIZE, TB_SC_SIZE );
2345 rb->lcd_set_foreground( rp_colors[ drawcolor ] );
2346 rb->lcd_fillrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2347 TB_SC_SIZE, TB_SC_SIZE );
2348 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2349 rb->lcd_drawrect( TB_SC_FG_LEFT, TOP+TB_SC_FG_TOP,
2350 TB_SC_SIZE, TB_SC_SIZE );
2352 for( i=0; i<18; i++ )
2354 rb->lcd_set_foreground( rp_colors[i] );
2355 rb->lcd_fillrect(
2356 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2357 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2358 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2359 rb->lcd_set_foreground( ROCKPAINT_PALETTE );
2360 rb->lcd_drawrect(
2361 TB_PL_LEFT+(i%9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING ),
2362 TOP+TB_PL_TOP+(i/9)*( TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING),
2363 TB_PL_COLOR_SIZE, TB_PL_COLOR_SIZE );
2366 #define SEPARATOR( x, y ) \
2367 rb->lcd_set_foreground( COLOR_WHITE ); \
2368 rb->lcd_vline( x, TOP+y, TOP+y+TB_PL_HEIGHT-1 ); \
2369 rb->lcd_set_foreground( COLOR_DARKGRAY ); \
2370 rb->lcd_vline( x+1, TOP+y, TOP+y+TB_PL_HEIGHT-1 );
2371 SEPARATOR( TB_PL_LEFT + TB_PL_WIDTH - 1 + TB_SP_MARGIN, TB_PL_TOP );
2373 rb->lcd_bitmap_transparent( rockpaint, TB_TL_LEFT, TOP+TB_TL_TOP,
2374 TB_TL_WIDTH, TB_TL_HEIGHT );
2375 rb->lcd_set_foreground(ROCKPAINT_PALETTE);
2376 rb->lcd_drawrect( TB_TL_LEFT+(TB_TL_SIZE+TB_TL_SPACING)*(tool/2),
2377 TOP+TB_TL_TOP+(TB_TL_SIZE+TB_TL_SPACING)*(tool%2),
2378 TB_TL_SIZE, TB_TL_SIZE );
2380 SEPARATOR( TB_TL_LEFT + TB_TL_WIDTH - 1 + TB_SP_MARGIN, TB_TL_TOP );
2382 rb->lcd_setfont( FONT_SYSFIXED );
2383 rb->lcd_putsxy( TB_MENU_LEFT, TOP+TB_MENU_TOP, "Menu" );
2384 rb->lcd_setfont( FONT_UI );
2385 #undef TOP
2387 if( update ) rb->lcd_update();
2390 static void toolbar( void )
2392 int button, i, j;
2393 restore_screen();
2394 draw_toolbars( false );
2395 y = LCD_HEIGHT-TB_HEIGHT/2;
2396 inv_cursor( true );
2397 while( 1 )
2399 switch( button = rb->button_get( true ) )
2401 case ROCKPAINT_DRAW:
2402 #define TOP ( LCD_HEIGHT - TB_HEIGHT )
2403 if( y >= TOP + TB_SC_FG_TOP
2404 && y < TOP + TB_SC_FG_TOP + TB_SC_SIZE
2405 && x >= TB_SC_FG_LEFT
2406 && x < TB_SC_FG_LEFT + TB_SC_SIZE )
2408 /* click on the foreground color */
2409 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2411 else if( y >= TOP + TB_SC_BG_TOP
2412 && y < TOP + TB_SC_BG_TOP + TB_SC_SIZE
2413 && x >= TB_SC_BG_LEFT
2414 && x < TB_SC_BG_LEFT + TB_SC_SIZE )
2416 /* click on the background color */
2417 i = drawcolor;
2418 drawcolor = bgdrawcolor;
2419 bgdrawcolor = i;
2421 else if( y >= TOP + TB_PL_TOP
2422 && y < TOP + TB_PL_TOP + TB_PL_HEIGHT
2423 && x >= TB_PL_LEFT
2424 && x < TB_PL_LEFT + TB_PL_WIDTH )
2426 /* click on the palette */
2427 i = (x - TB_PL_LEFT)%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2428 j = (y - (TOP+TB_PL_TOP) )%(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2429 if( i >= TB_PL_COLOR_SIZE || j >= TB_PL_COLOR_SIZE )
2430 break;
2431 i = ( x - TB_PL_LEFT )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2432 j = ( y - (TOP+TB_PL_TOP) )/(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING);
2433 drawcolor = j*(TB_PL_COLOR_SIZE+TB_PL_COLOR_SPACING)+i;
2435 else if( y >= TOP+TB_TL_TOP
2436 && y < TOP + TB_TL_TOP + TB_TL_HEIGHT
2437 && x >= TB_TL_LEFT
2438 && x <= TB_TL_LEFT + TB_TL_WIDTH )
2440 /* click on the tools */
2441 i = (x - TB_TL_LEFT ) % (TB_TL_SIZE+TB_TL_SPACING);
2442 j = (y - (TOP+TB_TL_TOP) ) %(TB_TL_SIZE+TB_TL_SPACING);
2443 if( i >= TB_TL_SIZE || j >= TB_TL_SIZE ) break;
2444 i = ( x - TB_TL_LEFT )/(TB_TL_SIZE+TB_TL_SPACING);
2445 j = ( y - (TOP+TB_TL_TOP) )/(TB_TL_SIZE+TB_TL_SPACING);
2446 tool = i*2+j;
2447 prev_x = -1;
2448 prev_y = -1;
2449 prev_x2 = -1;
2450 prev_y2 = -1;
2451 prev_x3 = -1;
2452 prev_y3 = -1;
2453 preview = false;
2455 else if( x >= TB_MENU_LEFT && y >= TOP+TB_MENU_TOP-2)
2457 /* menu button */
2458 goto_menu();
2460 #undef TOP
2461 restore_screen();
2462 draw_toolbars( false );
2463 inv_cursor( true );
2464 break;
2466 case ROCKPAINT_LEFT:
2467 case ROCKPAINT_LEFT | BUTTON_REPEAT:
2468 inv_cursor(false);
2469 x-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2470 if (x<0) x=COLS-1;
2471 inv_cursor(true);
2472 break;
2474 case ROCKPAINT_RIGHT:
2475 case ROCKPAINT_RIGHT | BUTTON_REPEAT:
2476 inv_cursor(false);
2477 x+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2478 if (x>=COLS) x=0;
2479 inv_cursor(true);
2480 break;
2482 case ROCKPAINT_UP:
2483 case ROCKPAINT_UP | BUTTON_REPEAT:
2484 inv_cursor(false);
2485 y-=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2486 if (y<LCD_HEIGHT-TB_HEIGHT)
2488 return;
2490 inv_cursor(true);
2491 break;
2493 case ROCKPAINT_DOWN:
2494 case ROCKPAINT_DOWN | BUTTON_REPEAT:
2495 inv_cursor(false);
2496 y+=bspeed * ( button & BUTTON_REPEAT ? 4 : 1 );
2497 if (y>=LCD_HEIGHT)
2499 y = 0;
2500 return;
2502 inv_cursor(true);
2503 break;
2505 case ROCKPAINT_TOOLBAR:
2506 case ROCKPAINT_TOOLBAR2:
2507 return;
2509 if( quit ) return;
2513 static void inv_cursor(bool update)
2515 rb->lcd_set_foreground(COLOR_BLACK);
2516 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2517 /* cross painting */
2518 rb->lcd_hline(x-4,x-1,y);
2519 rb->lcd_hline(x+1,x+4,y);
2520 rb->lcd_vline(x,y-4,y-1);
2521 rb->lcd_vline(x,y+1,y+4);
2522 rb->lcd_set_foreground(rp_colors[drawcolor]);
2523 rb->lcd_set_drawmode(DRMODE_SOLID);
2525 if( update ) rb->lcd_update();
2528 static void restore_screen(void)
2530 rb->lcd_bitmap( save_buffer, 0, 0, COLS, ROWS );
2533 static void clear_drawing(void)
2535 init_buffer();
2536 rb->lcd_set_foreground( rp_colors[ bgdrawcolor ] );
2537 rb->lcd_fillrect( 0, 0, COLS, ROWS );
2538 rb->lcd_update();
2541 static void goto_menu(void)
2543 int multi;
2545 while( 1 )
2547 switch( menu_display( main_menu, 1 ) )
2549 case MAIN_MENU_NEW:
2550 clear_drawing();
2551 return;
2553 case MAIN_MENU_LOAD:
2554 if( browse( filename, MAX_PATH, "/" ) )
2556 if( load_bitmap( filename ) <= 0 )
2558 rb->splashf( 1*HZ, "Error while loading %s",
2559 filename );
2561 else
2563 rb->splashf( 1*HZ, "Image loaded (%s)", filename );
2564 restore_screen();
2565 inv_cursor(true);
2566 return;
2569 break;
2571 case MAIN_MENU_SAVE:
2572 if (!filename[0])
2573 rb->strcpy(filename,"/");
2574 if( !rb->kbd_input( filename, MAX_PATH ) )
2576 if(rb->strlen(filename) <= 4 ||
2577 rb->strcasecmp(&filename[rb->strlen(filename)-4], ".bmp"))
2578 rb->strcat(filename, ".bmp");
2579 save_bitmap( filename );
2580 rb->splashf( 1*HZ, "File saved (%s)", filename );
2582 break;
2584 case MAIN_MENU_BRUSH_SIZE:
2585 multi = menu_display( size_menu, bsize );
2586 if( multi != - 1 )
2587 bsize = multi;
2588 break;
2590 case MAIN_MENU_BRUSH_SPEED:
2591 multi = menu_display( speed_menu, bspeed );
2592 if( multi != -1 )
2593 bspeed = multi;
2594 break;
2596 case MAIN_MENU_COLOR:
2597 rp_colors[drawcolor] = color_chooser( rp_colors[drawcolor] );
2598 break;
2600 case MAIN_MENU_GRID_SIZE:
2601 multi = menu_display( gridsize_menu, gridsize );
2602 if( multi != - 1 )
2603 gridsize = multi;
2604 break;
2606 case MAIN_MENU_EXIT:
2607 quit=true;
2608 return;
2610 case MAIN_MENU_RESUME:
2611 case MENU_ESC:
2612 return;
2613 }/* end switch */
2614 }/* end while */
2617 static void reset_tool( void )
2619 prev_x = -1;
2620 prev_y = -1;
2621 prev_x2 = -1;
2622 prev_y2 = -1;
2623 prev_x3 = -1;
2624 prev_y3 = -1;
2625 tool_mode = -1;
2626 preview = false;
2629 static bool rockpaint_loop( void )
2631 int button=0,i,j;
2632 int accelaration;
2634 toolbar();
2635 restore_screen();
2636 inv_cursor(true);
2638 while (!quit) {
2639 button = rb->button_get(true);
2641 if( tool == Brush && prev_x != -1 )
2643 accelaration = 1;
2645 else if( button & BUTTON_REPEAT )
2647 accelaration = 4;
2649 else
2651 accelaration = 1;
2654 switch(button)
2656 case ROCKPAINT_QUIT:
2657 rb->lcd_set_drawmode(DRMODE_SOLID);
2658 return PLUGIN_OK;
2660 case ROCKPAINT_MENU:
2661 inv_cursor(false);
2662 goto_menu();
2663 inv_cursor(true);
2664 break;
2666 case ROCKPAINT_DRAW:
2667 inv_cursor(false);
2668 switch( tool )
2670 case Brush:
2671 if( prev_x == -1 ) prev_x = 1;
2672 else prev_x = -1;
2673 break;
2675 case SelectRectangle:
2676 case Line:
2677 case Curve:
2678 case Rectangle:
2679 case RectangleFull:
2680 case Oval:
2681 case OvalFull:
2682 case LinearGradient:
2683 case RadialGradient:
2684 /* Curve uses 4 points, others use 2 */
2685 if( prev_x == -1 || prev_y == -1 )
2687 prev_x = x;
2688 prev_y = y;
2689 preview = true;
2691 else if( tool == Curve
2692 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2694 prev_x2 = x;
2695 prev_y2 = y;
2697 else if( tool == SelectRectangle
2698 && ( prev_x2 == -1 || prev_y2 == -1 ) )
2700 tool_mode = menu_display( select_menu,
2701 SELECT_MENU_CUT );
2702 switch( tool_mode )
2704 case SELECT_MENU_CUT:
2705 case SELECT_MENU_COPY:
2706 prev_x2 = x;
2707 prev_y2 = y;
2708 copy_to_clipboard();
2709 if( prev_x < x ) x = prev_x;
2710 if( prev_y < y ) y = prev_y;
2711 break;
2713 case SELECT_MENU_INVERT:
2714 draw_invert( prev_x, prev_y, x, y );
2715 reset_tool();
2716 break;
2718 case SELECT_MENU_HFLIP:
2719 draw_hflip( prev_x, prev_y, x, y );
2720 reset_tool();
2721 break;
2723 case SELECT_MENU_VFLIP:
2724 draw_vflip( prev_x, prev_y, x, y );
2725 reset_tool();
2726 break;
2728 case SELECT_MENU_ROTATE90:
2729 break;
2730 case SELECT_MENU_ROTATE180:
2731 draw_hflip( prev_x, prev_y, x, y );
2732 draw_vflip( prev_x, prev_y, x, y );
2733 reset_tool();
2734 break;
2735 case SELECT_MENU_ROTATE270:
2736 break;
2738 case SELECT_MENU_CANCEL:
2739 reset_tool();
2740 break;
2742 case MENU_ESC:
2743 break;
2746 else if( tool == Curve
2747 && ( prev_x3 == -1 || prev_y3 == -1 ) )
2749 prev_x3 = x;
2750 prev_y3 = y;
2752 else
2754 preview = false;
2755 switch( tool )
2757 case SelectRectangle:
2758 draw_paste_rectangle( prev_x, prev_y,
2759 prev_x2, prev_y2,
2760 x, y, tool_mode );
2761 break;
2762 case Line:
2763 draw_line( prev_x, prev_y, x, y );
2764 break;
2765 case Curve:
2766 draw_curve( prev_x, prev_y,
2767 prev_x2, prev_y2,
2768 prev_x3, prev_y3,
2769 x, y );
2770 break;
2771 case Rectangle:
2772 draw_rect( prev_x, prev_y, x, y );
2773 break;
2774 case RectangleFull:
2775 draw_rect_full( prev_x, prev_y, x, y );
2776 break;
2777 case Oval:
2778 draw_oval_empty( prev_x, prev_y, x, y );
2779 break;
2780 case OvalFull:
2781 draw_oval_full( prev_x, prev_y, x, y );
2782 break;
2783 case LinearGradient:
2784 linear_gradient( prev_x, prev_y, x, y );
2785 break;
2786 case RadialGradient:
2787 radial_gradient( prev_x, prev_y, x, y );
2788 break;
2789 default:
2790 break;
2792 reset_tool();
2794 break;
2796 case Fill:
2797 draw_fill( x, y );
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 l;
2997 bm.data = (char*)save_buffer;
2998 ret = rb->read_bmp_file( file, &bm, ROWS*COLS*sizeof( fb_data ),
2999 FORMAT_NATIVE, NULL );
3001 if((bm.width > COLS ) || ( bm.height > ROWS ))
3002 return -1;
3004 for( l = bm.height-1; l > 0; l-- )
3006 rb->memmove( save_buffer+l*COLS, save_buffer+l*bm.width,
3007 sizeof( fb_data )*bm.width );
3009 for( l = 0; l < bm.height; l++ )
3011 rb->memset( save_buffer+l*COLS+bm.width, rp_colors[ bgdrawcolor ],
3012 sizeof( fb_data )*(COLS-bm.width) );
3014 rb->memset( save_buffer+COLS*bm.height, rp_colors[ bgdrawcolor ],
3015 sizeof( fb_data )*COLS*(ROWS-bm.height) );
3017 return ret;
3020 static int save_bitmap( char *file )
3022 struct bitmap bm;
3023 bm.data = (char*)save_buffer;
3024 bm.height = ROWS;
3025 bm.width = COLS;
3026 bm.format = FORMAT_NATIVE;
3027 return save_bmp_file( file, &bm );
3030 enum plugin_status plugin_start(const void* parameter)
3032 rb->lcd_set_foreground(COLOR_WHITE);
3033 rb->lcd_set_backdrop(NULL);
3034 rb->lcd_fillrect(0,0,LCD_WIDTH,LCD_HEIGHT);
3035 rb->splash( HZ/2, "Rock Paint");
3037 rb->lcd_clear_display();
3039 filename[0] = '\0';
3041 if( parameter )
3043 if( load_bitmap( parameter ) <= 0 )
3045 rb->splash( 1*HZ, "File Open Error");
3046 clear_drawing();
3048 else
3050 rb->splashf( 1*HZ, "Image loaded (%s)", (char *)parameter );
3051 restore_screen();
3052 rb->strcpy( filename, parameter );
3055 else
3057 clear_drawing();
3059 inv_cursor(true);
3061 return rockpaint_loop();