/** @file Function for drawing the robot and to calculate the absolute
    postion of the robot out of the mouse-position.

*/

/*
  Copyright (c) Andreas Hofmeier
  (www.an-h.de, www.an-h.de.vu, www.lgut.uni-bremen.de/an-h/)
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include <time.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib.h>


/** Function for drawing the robot and to calculate the absolute
    postion of the robot out of the mouse-position.

    @param widget (GtkWidget *) pointer to affected object (drawing
    area).

    @param x (int *) pointer to absolute x position of the robot (will
    changed if mouseaction equal to 1 or 2)

    @param y (int *) pointer to absolute y position of the robot (will
    changed if mouseaction equal to 1 or 2)

    @param mx (int) x mouse position (only relevant if mouseaction
    equal to 1 or 2)

    @param my (int) y mouse position (only relevant if mouseaction
    equal to 1 or 2)

    @param mouseaction (int) current action: 1: start moving, 2:
    moving or finish moving, other: plot only

*/
void gui_common_drawrobot(GtkWidget *widget, int *x, int *y,
			  int mx, int my, int mouseaction) {

  // drawing area properties
  gint new_width, new_height;
  int height, width;

  // width and hieght of the illustration of the moving platform of
  // the robot.
  int hwidth, wwidth;

  // current position of the moving platform
  int x0, y0;

  // new position of the moving platform
  int x0n, y0n;

  // current action: 0: draw only, 1: during moving, 2: error: click
  // outside from platform
  static int moving = 0;

  // offset for mouse-moving
  static int ox, oy;

  // get size of the drawing area
  gdk_drawable_get_size(widget->window,
			&new_width,
			&new_height);
  width = (int) new_width;
  height = (int) new_height;

  // clear it
  gdk_window_clear_area(widget->window, 
			0,0,
			width, height); 

  // calculate the width and hieght of the illustration of the moving
  // platform of the robot.
  hwidth = 10 * height / 100;
  wwidth = 10 * width / 100;

  // moving or moving finished
  if (mouseaction == 2) {
    moving = 0;
  }
  if (moving == 1) {
    // calculate the new position of the platform and set the
    // variables
    *x = ((mx - ox) * 100) / (width - wwidth);
    *y = (((my + oy) * (-1) + (height - hwidth)) * 100) / (height - hwidth);

    // platform must not beyond the window bondaries
    if (*x > 100) {
      *x = 100;
    }
    if (*x < 0) {
      *x = 0;
    }
    if (*y > 100) {
      *y = 100;
    }
    if (*y < 0) {
      *y = 0;
    }
  }

  // draw the horizontal (X) axies track of the robot, whish is NOT
  // moving
  y0 = ((0 * (height - hwidth) / 100) - (height  - hwidth)) * (-1);
  gdk_draw_line(widget->window,
		widget->style->black_gc,
		0,y0,
		width,y0);
  gdk_draw_line(widget->window,
		widget->style->black_gc,
		0,y0 + hwidth,
		width,y0 + hwidth);

  // Draw the current possition of the movind platform of the
  // robot. Illustrated as a black rectangular.
  // +- calculate it's current position
  x0 = *x * (width - wwidth) / 100;
  y0 = ((*y * (height - hwidth) / 100) - (height  - hwidth)) * (-1);
  // +- draw lines under it.
  gdk_draw_line(widget->window,
		widget->style->black_gc,
		x0,y0,
		x0,height);
  gdk_draw_line(widget->window,
		widget->style->black_gc,
		x0 + wwidth,y0,
		x0 + wwidth,height);
  // +- draw the platform
  gdk_draw_rectangle(widget->window,
		     widget->style->black_gc,
		     1,
		     x0,y0,
		     wwidth, hwidth);

  // start moving the platform
  if ((moving == 0) && (mouseaction == 1)) {
    if ((mx > x0) && (mx < (x0 + wwidth))
	&& (my > y0) && (my < (y0 + hwidth))) {
      // click inside form platform -- moving...
      moving = 1;
      // offset for moving
      ox = mx - x0;
      oy = y0 - my;
    } else {
      // error, click outside from platform
      moving = 2;
    }
  }
}


