home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / guicast / bcpan.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  4KB  |  215 lines

  1. #include <math.h>
  2.  
  3. #include "bcpan.h"
  4. #include "bcpixmap.h"
  5. #include "bcresources.h"
  6. #include "colors.h"
  7.  
  8.  
  9. BC_Pan::BC_Pan(int x, 
  10.         int y, 
  11.         int virtual_r, 
  12.         float maxvalue, 
  13.         int total_values, 
  14.         int *value_positions, 
  15.         int stick_x, 
  16.         int stick_y,
  17.         float *values)
  18.  : BC_SubWindow(x, y, -1, -1, -1)
  19. {
  20.     this->virtual_r = virtual_r;
  21.     this->maxvalue = maxvalue;
  22.     this->total_values = total_values;
  23.     this->value_positions = new int[total_values];
  24.     this->stick_x = stick_x;
  25.     this->stick_y = stick_y;
  26.     this->values = new float[total_values];
  27.     highlighted = 0;
  28. }
  29.  
  30. BC_Pan::~BC_Pan()
  31. {
  32.     delete values;
  33.     delete value_positions;
  34.     delete value_x;
  35.     delete value_y;
  36.     delete bg_pixmap;
  37.     delete bg_pixmap_hi;
  38.     delete handle_pixmap;
  39.     delete channel_pixmap;
  40. }
  41.  
  42. int BC_Pan::initialize()
  43. {
  44.     bg_pixmap = new BC_Pixmap(parent_window, get_resources()->pan_bg, PIXMAP_ALPHA);
  45.     bg_pixmap_hi = new BC_Pixmap(parent_window, get_resources()->pan_bg_hi, PIXMAP_ALPHA);
  46.     handle_pixmap = new BC_Pixmap(parent_window, get_resources()->pan_stick, PIXMAP_ALPHA);
  47.     channel_pixmap = new BC_Pixmap(parent_window, get_resources()->pan_channel, PIXMAP_ALPHA);
  48.     w = bg_pixmap->get_w();
  49.     h = bg_pixmap->get_h();
  50.     BC_SubWindow::initialize();
  51.     draw();
  52.     return 0;
  53. }
  54.  
  55. int BC_Pan::update(int x, int y)
  56. {
  57.     stick_x = x;
  58.     stick_y = y;
  59.     stick_to_values();
  60.     draw();
  61.     return 0;
  62. }
  63.  
  64. int BC_Pan::draw()
  65. {
  66.     if(highlighted)
  67.     {
  68.         draw_pixmap(bg_pixmap_hi);
  69.     }
  70.     else
  71.     {
  72.         draw_pixmap(bg_pixmap);
  73.     }
  74.  
  75. // draw channels
  76.     int x1, y1, x2, y2, w, h, j;
  77.     float scale = (float)get_w() / virtual_r * 2;
  78.     w = channel_pixmap->get_w();
  79.     h = channel_pixmap->get_h();
  80.     set_color(RED);
  81.     for(int i = 0; i < total_values; i++)
  82.     {
  83.         x1 = (int)(value_x[i] * scale) - w / 2;
  84.         y1 = (int)(value_y[i] * scale) - h / 2;
  85.         x2 = x1 + w;
  86.         y2 = y1 + h;
  87.         if(x1 < 0) { x1 = 0; }
  88.         if(y1 < 0) { y1 = 0; }
  89.         if(x2 > get_w()) { x1 = get_w() - w; }
  90.         if(y2 > get_w()) { y1 = get_w() - h; }
  91.         if(y2 < h) { y2 = h; y1 = 0; }
  92.         draw_pixmap(channel_pixmap, x1, y1);
  93.     }
  94.  
  95. // draw stick
  96.     set_color(MEYELLOW);
  97.     w = h = 6;
  98.     x1 = (int)(stick_x * scale);
  99.     x2 = x1;
  100.     y1 = (int)(stick_y * scale - h);
  101.     y2 = y1 + h * 2;
  102.     draw_line(x1, y1, x2, y2);
  103.  
  104.     x1 = (int)(stick_x * scale - w);
  105.     x2 = x1 + w * 2;
  106.     y1 = (int)(stick_y * scale);
  107.     y2 = y1;
  108.     draw_line(x1, y1, x2, y2);
  109.  
  110.     flash();
  111.     return 0;
  112. }
  113.  
  114. int BC_Pan::stick_to_values()
  115. {
  116. // find shortest distance to a channel
  117.     float shortest = 2 * virtual_r, test_distance;
  118.     int i;
  119.  
  120.     for(i = 0; i < total_values; i++)
  121.     {
  122.         if((test_distance = distance(stick_x, value_x[i], stick_y, value_y[i])) < shortest)
  123.             shortest = test_distance;
  124.     }
  125.  
  126. // get values for channels
  127.     if(shortest == 0)
  128.     {
  129.         for(i = 0; i < total_values; i++)
  130.         {
  131.             if(distance(stick_x, value_x[i], stick_y, value_y[i]) == shortest)
  132.                 values[i] = maxvalue;
  133.             else
  134.                 values[i] = 0;
  135.         }
  136.     }
  137.     else
  138.     {
  139.         for(i = 0; i < total_values; i++)
  140.         {
  141.             values[i] = shortest;
  142.             values[i] -= (float)(distance(stick_x, value_x[i], stick_y, value_y[i]) - shortest);
  143.             if(values[i] < 0) values[i] = 0;
  144.             values[i] = values[i] / shortest * maxvalue;
  145.         }
  146.     }
  147.     return 0;
  148. }
  149.  
  150. float BC_Pan::distance(int x1, int x2, int y1, int y2)
  151. {
  152.     return hypot(x2 - x1, y2 - y1);
  153. }
  154.  
  155. int BC_Pan::change_channels(int new_channels, int *value_positions)
  156. {
  157.     delete values;
  158.     delete this->value_positions;
  159.     delete value_x;
  160.     delete value_y;
  161.     
  162.     values = new float[new_channels];
  163.     this->value_positions = new int[new_channels];
  164.     value_x = new int[new_channels];
  165.     value_y = new int[new_channels];
  166.     total_values = new_channels;
  167.     for(int i = 0; i < new_channels; i++)
  168.     {
  169.         this->value_positions[i] = value_positions[i];
  170.     }
  171.     get_channel_positions();
  172.     stick_to_values();
  173.     draw();
  174.     return 0;
  175. }
  176.  
  177. int BC_Pan::get_channel_positions()
  178. {
  179.     for(int i = 0; i < total_values; i++)
  180.     {
  181.         rdtoxy(value_x[i], value_y[i], virtual_r, value_positions[i], virtual_r);
  182.     }
  183.     return 0;
  184. }
  185.  
  186. int BC_Pan::rdtoxy(int &x, int &y, int r, int a, int virtual_r)
  187. {
  188.     float radians = (float)(a - 90) / 360 * 2 * M_PI;
  189.     y = (int)(sin(radians) * r);
  190.     x = (int)(cos(radians) * r);
  191.     x += virtual_r;
  192.     y += virtual_r;
  193.     return 0;
  194. }
  195.  
  196. int BC_Pan::get_total_values()
  197. {
  198.     return total_values;
  199. }
  200.  
  201. float BC_Pan::get_value(int channel)
  202. {
  203.     return values[channel];
  204. }
  205.  
  206. int BC_Pan::get_stick_x() 
  207.     return stick_x; 
  208. }
  209.  
  210. int BC_Pan::get_stick_y() 
  211.     return stick_y; 
  212. }
  213.