/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: iconedit.c,v 1.2 2004/01/09 14:25:34 alex Exp $
 *
 * Copyright (C) 2004 Alex Pleiner
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"

#ifdef HAVE_LCD_BITMAP

static struct plugin_api* rb;

/* current cursor position */
int x = 1;
int y = 1;

/* current icon -- load from disc later - this is the musical note
 * for demonstration 
 */
unsigned char icon[] = {0x60, 0x7f, 0x03, 0x33, 0x3f, 0x00};

/* the magnified pixel in painting matrix */
static unsigned char pixel[] = {0xff, 0xff, 0xff, 0xff, 0xff};

/* cursor movement */
#define LEFT    0x08
#define RIGHT   0x04
#define UP      0x02
#define DOWN    0x01

/* icon size */
#define ICON_X  6
#define ICON_Y  8

/* pixel size, border size */
#define PIXEL   5
#define BORDER  1
#define SIZE    7 /* PIXEL + 2 * BORDER */

/* position of preview icon */
#define PRE_X   50
#define PRE_Y   0

void paint_cursor(void)
{
  int cx = 1+(x-1)*SIZE;
  int cy = 1+(y-1)*SIZE;
  int d  = PIXEL+BORDER;

  /* this could be a dotted rectangle ... */
  rb->lcd_drawline(cx, cy, cx+d, cy);
  rb->lcd_drawline(cx+d, cy, cx+d, cy+d);
  rb->lcd_drawline(cx+d, cy+d, cx, cy+d);
  rb->lcd_drawline(cx, cy+d, cx, cy);

  rb->lcd_update_rect(cx, cy, d+1, d+1);
}

void delete_cursor(void)
{
  int cx = 1+(x-1)*SIZE;
  int cy = 1+(y-1)*SIZE;
  int d  = PIXEL+BORDER;

  rb->lcd_clearline(cx, cy, cx+d, cy);
  rb->lcd_clearline(cx+d, cy, cx+d, cy+d);
  rb->lcd_clearline(cx+d, cy+d, cx, cy+d);
  rb->lcd_clearline(cx, cy+d, cx, cy);

  rb->lcd_update_rect(cx, cy, d+1, d+1);
}

void paint_matrix (void)
{
  int px,py;

  /* matrix */
  rb->lcd_clearrect(1,1, SIZE*ICON_X,SIZE*ICON_Y);

  for (py=0; py<ICON_Y; py++)
    for (px=0; px<ICON_X; px++)
      if ( ((icon[px] >>(py%8)) &1) )
	rb->lcd_bitmap((char *)pixel,
			     1+BORDER+px*SIZE, 1+BORDER+py*SIZE, 
			     PIXEL, PIXEL, false);
  /* preview */
  rb->lcd_clearrect(PRE_X, PRE_Y, ICON_X, ICON_Y);
  rb->lcd_bitmap((char *)icon, PRE_X, PRE_Y, ICON_X, ICON_Y, false);

  rb->lcd_update();

  /* cursor again */
  paint_cursor();
}

void paint_frame (void)
{
  /* there are many todos here to print some legend */
  /*
  char buffer[30];
  int w, h;
  */

  rb->lcd_clear_display();

  rb->lcd_drawrect(0,0, 2*BORDER+SIZE*ICON_X,2*BORDER+SIZE*ICON_Y);
  rb->lcd_update();  

  /* other stuff to do */
  /*
  rb->snprintf(buffer, sizeof(buffer), "%s", "Arrows: Move");
  rb->lcd_getstringsize(buffer,&w,&h);
  rb->lcd_putsxy(10+SIZE*ICON_X, 0, buffer);

  rb->lcd_putsxy(10+SIZE*ICON_X, h, "Play: Toggle");
  rb->lcd_putsxy(10+SIZE*ICON_X, 2*h, "F1: Help");
  rb->lcd_putsxy(10+SIZE*ICON_X, 3*h, "F2: Save");
  */

  paint_matrix();
}

void move (unsigned char dir)
{

  delete_cursor();

  x -= ( ((dir&LEFT )==LEFT ) ?1:0);
  x += ( ((dir&RIGHT)==RIGHT) ?1:0);
  y -= ( ((dir&UP   )==UP   ) ?1:0);
  y += ( ((dir&DOWN )==DOWN ) ?1:0);

  if (x > ICON_X) x = 1;
  if (x < 1)      x = ICON_X;
  if (y > ICON_Y) y = 1;
  if (y < 1)      y = ICON_Y;

  paint_cursor();
}


enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{

    TEST_PLUGIN_API(api);
    (void)parameter;

    rb = api;
 
    paint_frame();

    while (1)
    {
        switch (rb->button_get(true))
        {
            case BUTTON_OFF:
	      return false;

            case SYS_USB_CONNECTED:
	      rb->usb_screen();
	      return false;

            case BUTTON_F1:
	      /* todo */
	      rb->lcd_clear_display();
	      rb->lcd_puts(0, 0, "help");
	      rb->lcd_update();

              rb->sleep(HZ*2);
              paint_frame();
	      break;

            case BUTTON_F2:
	      /* todo */
	      rb->lcd_clear_display();
	      rb->lcd_puts(0, 0, "save");
	      rb->lcd_update();

              rb->sleep(HZ*2);
              paint_frame();
	      break;

            case BUTTON_F3:
	      /* todo */
	      rb->lcd_clear_display();
	      rb->lcd_puts(0, 0, "foo");
	      rb->lcd_update();

              rb->sleep(HZ*2);
              paint_frame();
	      break;

            case BUTTON_DOWN:
            case BUTTON_DOWN  | BUTTON_REPEAT:
	      move(DOWN);
	      break;

            case BUTTON_UP:
            case BUTTON_UP    | BUTTON_REPEAT:
	      move(UP);
	      break;

            case BUTTON_LEFT:
            case BUTTON_LEFT  | BUTTON_REPEAT:
	      move(LEFT);
	      break;

            case BUTTON_RIGHT:
            case BUTTON_RIGHT | BUTTON_REPEAT:
	      move(RIGHT);
	      break;

            case BUTTON_PLAY:
            case BUTTON_PLAY  | BUTTON_REPEAT:
	      /* toggle current pixel */
	      icon[x-1] ^= 1 << (y-1);
	      paint_matrix();
	      break;

        }
    }
   
    return PLUGIN_OK;
}

#endif

