home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / flcroppad.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  5.5 KB  |  248 lines

  1. /* 
  2.  *  flcroppad.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include "flcroppad.h"
  25. #include "debug.h"
  26.  
  27.  
  28. #define AREA_SIZE 36
  29.  
  30.  
  31. FlCropPad::FlCropPad()
  32. {
  33.   m_clx = m_cly = 0;
  34.   m_bLocked = false;
  35.   m_nLockedType = 0;
  36. }
  37.  
  38. bool FlCropPad::SetConfig( int nWidth, int nHeight, TCropConfig *cfg )
  39. {
  40.   m_nWidth = nWidth;
  41.   m_nHeight = nHeight;
  42.   m_cfg = *cfg;
  43.   BuildAreas();
  44.   return true;
  45. }
  46.  
  47.  
  48. bool FlCropPad::OnDrag( int x, int y )
  49. {
  50.   bool res = false;
  51.   
  52.   if( m_bLocked )
  53.     res = Notify( x, y, m_nLockedType );
  54.  
  55.   return res;
  56. }
  57.  
  58. bool FlCropPad::OnHover( int x, int y, LPCTSTR &mousetype)
  59. {
  60.   for( int i=0; i<9; i++ ) {
  61.     if( m_vAreas[i].Poll( x, y ) ) {
  62.       int type = m_bLocked ? m_nLockedType : m_vAreas[i].GetType();
  63.       switch( type )
  64.       {
  65.         case topleft:
  66.         case bottomright:
  67.           mousetype = IDC_SIZENWSE;
  68.           break;
  69.         case top:
  70.         case bottom:
  71.           mousetype = IDC_SIZENS;
  72.           break;
  73.         case topright:
  74.         case bottomleft:
  75.           mousetype = IDC_SIZENESW;
  76.           break;
  77.         case left:
  78.         case right:
  79.           mousetype = IDC_SIZEWE;
  80.           break;
  81.         case center:
  82.           mousetype = IDC_SIZEALL;
  83.           break;
  84.       }
  85.       return true;
  86.     }
  87.   }
  88.   return false;
  89. }
  90.  
  91.  
  92. bool FlCropPad::BuildAreas()
  93.   int w = m_nWidth;
  94.   int h = m_nHeight;
  95.  
  96.   int b = m_cfg.bottom;
  97.   int l = m_cfg.left;
  98.   int r = m_cfg.right;
  99.   int t = m_cfg.top;
  100.  
  101.   // corners
  102.   m_vAreas[0].Set( l, t, AREA_SIZE, AREA_SIZE, topleft );
  103.   m_vAreas[1].Set( r, t, AREA_SIZE, AREA_SIZE, topright );
  104.   m_vAreas[2].Set( l, b, AREA_SIZE, AREA_SIZE, bottomleft );
  105.   m_vAreas[3].Set( r, b, AREA_SIZE, AREA_SIZE, bottomright );
  106.  
  107.   // sides
  108.   int sidew = (r - l) - AREA_SIZE;
  109.   m_vAreas[4].Set( (l+r)/2, t, sidew, AREA_SIZE, top );
  110.   m_vAreas[5].Set( (l+r)/2, b, sidew, AREA_SIZE, bottom );
  111.  
  112.   int sideh = (b - t) - AREA_SIZE;
  113.   m_vAreas[6].Set( l, (b+t)/2, AREA_SIZE, sideh, left );
  114.   m_vAreas[7].Set( r, (b+t)/2, AREA_SIZE, sideh, right );
  115.  
  116.   // center
  117.   m_vAreas[8].Set( (l+r)/2, (b+t)/2, sidew, sideh, center );
  118.  
  119.   return true;
  120. }
  121.  
  122. int FlCropPad::SnapCoordinate( int c, int oldc )
  123. {
  124.   int rem, res;
  125.  
  126.   if( c >= oldc ) {
  127.     // calculate the difference with the old
  128.     //  point
  129.     rem = (c-oldc)%16;
  130.     res = rem < 8 ? c-rem : c+(16-rem);
  131.   }
  132.   else { // c< oldc
  133.     rem = (oldc -c)%16;
  134.     res = rem < 8 ? c+rem : c-(16-rem);
  135.   }
  136.  
  137.   return res;
  138. }
  139.  
  140. bool FlCropPad::OnClick( int x, int y )
  141. {
  142.   bool bSuccess = false;
  143.   m_clx = x;
  144.   m_cly = y;
  145.   for( int i=0; i<9; i++ ) {
  146.     if( m_vAreas[i].Poll( x, y ) ) {
  147.       m_bLocked = true;
  148.       m_nLockedType = m_vAreas[i].GetType();
  149.       bSuccess = true;
  150.     }
  151.   }
  152.   return bSuccess;
  153. }
  154.  
  155. bool FlCropPad::OnRelease()
  156. {
  157.   m_bLocked = false;
  158.   return true;
  159. }
  160.  
  161. bool FlCropPad::Notify( int x, int y, int nType )
  162. {
  163.   int w = m_nWidth;
  164.   int h = m_nHeight;
  165.   int xoff, yoff;
  166.  
  167.   TCropConfig cfg;
  168.  
  169.   // clip x, y first
  170.   //x = x<0 ? 0 : x>w ? w : x;
  171.   //y = y<0 ? 0 : y>h ? h : y;
  172.  
  173.   cfg = m_cfg;
  174.   switch( nType )
  175.   {
  176.     case topleft:
  177.       cfg.top = SnapCoordinate(y, cfg.top);
  178.       cfg.left = SnapCoordinate(x, cfg.left);
  179.       break;
  180.     case top:
  181.       cfg.top = SnapCoordinate(y, cfg.top);
  182.       break;
  183.     case topright:
  184.       cfg.right = SnapCoordinate(x, cfg.right);
  185.       cfg.top = SnapCoordinate(y, cfg.top);
  186.       break;
  187.     case left:
  188.       cfg.left = SnapCoordinate(x, cfg.left);
  189.       break;
  190.     case center:
  191.       xoff = x - m_clx;
  192.       yoff = y - m_cly;
  193.  
  194.       if( (cfg.right - cfg.left) == w )
  195.         xoff = 0;
  196.  
  197.       if( (cfg.bottom - cfg.top) == w )
  198.         yoff = 0;
  199.  
  200.       cfg.left += xoff;
  201.       cfg.right += xoff;
  202.       cfg.top += yoff;
  203.       cfg.bottom += yoff;
  204.  
  205.       m_clx = x;
  206.       m_cly = y;
  207.       
  208.       break;
  209.     case right:
  210.       cfg.right = SnapCoordinate(x, cfg.right);
  211.       break;
  212.     case bottomleft:
  213.       cfg.bottom = SnapCoordinate(y, cfg.bottom);
  214.       cfg.left = SnapCoordinate(x, cfg.left);
  215.       break;
  216.     case bottom:
  217.       cfg.bottom = SnapCoordinate(y, cfg.bottom);
  218.       break;
  219.     case bottomright:
  220.       cfg.right = SnapCoordinate(x, cfg.right);
  221.       cfg.bottom = SnapCoordinate(y, cfg.bottom);
  222.       break;
  223.   }
  224.  
  225.   FlCrop oCrop;
  226.  
  227.   oCrop.Configure( &cfg, sizeof (TCropConfig) );
  228.  
  229.   flfilter_conf fc;
  230.   memset( &fc, 0, sizeof flfilter_conf );
  231.   fc.iw = w;
  232.   fc.ih = h;
  233.  
  234.   // Validate configuration
  235.   if( oCrop.ValFilterConf(&fc) ) 
  236.   {
  237.     // if nothing to do
  238.     if( memcmp(&cfg, &m_cfg, sizeof TCropConfig)==0 )
  239.       return false;
  240.  
  241.     DBG_STR((str, "Crop pad changed settings\n"))
  242.     m_cfg = cfg;
  243.     BuildAreas();
  244.     return true;
  245.   }
  246.   return false;
  247. }