logo
Main Page | Namespaces | Classes | Compounds | Files | Compound Members | Related

envDialog.cpp

00001 //
00002 //  SYNTOPIA. See http://Syntopia.sourceforge.net for details and documentation.        
00003 //
00004 //      Author of this file: Mikael Hvidtfeldt Christensen (mikaelc@users.sourceforge.net)
00005 //
00006 
00007 #include "EnvDialog.h"
00008 #include <vector>
00009 
00010 #include "MainDialog.h"
00011 
00012 namespace SynthGUI {
00013 
00014 void EnvDialog::EnvToScreen(float xx, float yy, int &x,int &y)
00015 {
00016         int w=waveCanvas->getWidth()-5;
00017         int h=waveCanvas->getHeight()-15;
00018         float MIN = -0.2f;
00019         float MAX = 1.2f;
00020         float LEFT;
00021         float RIGHT;
00022         myEnvData->getLimits(LEFT,MIN,RIGHT,MAX);
00023         x = 10+(w-20)*(xx-LEFT)/(RIGHT-LEFT);
00024         y = h-5-(h-10)*(yy-MIN)/(MAX-MIN);
00025 
00026 }
00027 
00028 void EnvDialog::ScreenToEnv(int x,int y,float &xx, float &yy )
00029 {
00030         int w=waveCanvas->getWidth()-5;
00031         int h=waveCanvas->getHeight()-15;
00032         float MIN = -0.2f;
00033         float MAX = 1.2f;
00034         float LEFT;
00035         float RIGHT;
00036         myEnvData->getLimits(LEFT,MIN,RIGHT,MAX);
00037 
00038         xx = LEFT+(x-10)*(RIGHT-LEFT)/(w-20);
00039         yy = MIN+(y-h+5)*(MAX-MIN)/(10-h);
00040 
00041 }
00042 
00043 void EnvDialog::drawEnv() {
00044         waveCanvas->clear();
00045         int w=waveCanvas->getWidth()-5;
00046         int h=waveCanvas->getHeight()-15;
00047         float MIN = -0.2f;
00048         float MAX = 1.2f;
00049         float LEFT;
00050         float RIGHT;
00051         myEnvData->getLimits(LEFT,MIN,RIGHT,MAX);
00052         
00053         for (int i=0; i<myEnvData->envsize; i++) 
00054         {       
00055                 float xpos = 10+(w-20)*(float) i/(float) myEnvData->envsize;
00056                 //waveCanvas->line((int)xpos,h-5-((h-10)*(myEnvData->envdata[i]-MIN)/(MAX-MIN)),
00057                 //                                      (int)xpos,h/2           );      
00058                 waveCanvas->putPixel((int)xpos,h-5-((h-10)*(myEnvData->envdata[i]-MIN)/(MAX-MIN)),1,1,1);
00059         }       
00060 
00061         for(int j = 0; j < myEnvData->points.size(); j++)
00062         {       waveCanvas->setColor(210,0,10);
00063                 if (myEnvData->getChoosen() == j) { waveCanvas->setColor(10,0,210); }
00064                 float x = 10+(w-20)*(myEnvData->points[j]->x-LEFT)/(RIGHT-LEFT);
00065                 float y = h-5-(h-10)*(myEnvData->points[j]->y-MIN)/(MAX-MIN);
00066                 waveCanvas->circle((int)x,(int)y,5); 
00067                 waveCanvas->circle((int)x,(int)y,3); 
00068 
00069                 // are this the left LP
00070                 if (j == myEnvData->beginLP) {
00071                         waveCanvas->setColor(0,0,110);
00072                         waveCanvas->line((int)x-7,(int)y-7,(int)x+7,(int)y-7);
00073                         waveCanvas->line((int)x-7,(int)y+7,(int)x+7,(int)y+7);
00074                         waveCanvas->line((int)x-7,(int)y-7,(int)x-7,(int)y+7);
00075                 }
00076 
00077                 if (j == myEnvData->endLP) {
00078                         waveCanvas->setColor(0,0,70);
00079                         waveCanvas->line((int)x-8,(int)y-8,(int)x+8,(int)y-8);
00080                         waveCanvas->line((int)x-8,(int)y+8,(int)x+8,(int)y+8);
00081                         waveCanvas->line((int)x+8,(int)y-8,(int)x+8,(int)y+8);
00082                 }
00083         }
00084 
00085         waveCanvas->doDraw();
00086 }
00087 
00088 EnvDialog::EnvDialog(mod * md) {
00089         myMod = md;
00090         myWindow = new WinForm(640,650, WinForm::standard, "Syntopia - Envelope setup");
00091         
00092         
00093         myEnvData = ((Envelope3 *)(myMod->mv[0]))->myData;
00094 
00095         int lx1 = 50;
00096         int lx2 = 280;
00097         int ly1 = 50;
00098         int lx3 = 510;
00099 
00100         int yy1 = 50;  // envelope
00101         int yy2 = yy1+30;       // canvas
00102         int yy3 = yy2+210;      
00103         int yy4 = yy3+50;       
00104         int yy5 = yy4+50;       
00105         int yy6 = yy5+50;       
00106         
00107 
00108 
00109         // setup Labels
00110 
00111         label0 = new Label("Envelope setup");
00112         label1 = new Label("Interpolation");
00113         label2 = new Label("Duration:");
00114         label3 = new Label("Envelope:");
00115 
00116         
00117         label0->PutInWinForm(myWindow, 10, 10, 200, 30);
00118         label3->PutInWinForm(myWindow, lx1, yy1, 200, 30);      
00119         label1->PutInWinForm(myWindow, lx1, yy5, 200, 30);
00120         label2->PutInWinForm(myWindow, lx1, yy6, 200, 30);
00121         
00122 
00123         // setup ComboBoxes
00124 
00125         WavSel1DD = new ComboBox();
00126         WavSel1DD->PutInWinForm(myWindow, lx1, yy3, 170, 140);
00127         WavSel1DD->add(WS1DD1 = new ComboBoxItem("Do not loop"));
00128         WavSel1DD->add(WS1DD2 = new ComboBoxItem("Loop while sustaining"));
00129         WavSel1DD->add(WS1DD3 = new ComboBoxItem("Always loop"));
00130         WavSel1DD->setSelected(WS1DD1);
00131 
00132 
00133         interpolDD = new ComboBox();
00134         interpolDD->PutInWinForm(myWindow, lx1+100, yy5,(lx3-80)-(lx1+100), 140);
00135         interpolDD->add(interpolDD1 = new ComboBoxItem("None."));
00136         interpolDD->add(interpolDD2 = new ComboBoxItem("Linear."));
00137         interpolDD->add(interpolDD3 = new ComboBoxItem("Hermitean."));
00138         interpolDD->add(interpolDD4 = new ComboBoxItem("Spline."));
00139         interpolDD->setSelected(interpolDD1);
00140 
00141                 
00142         // Setup Canvases.
00143 
00144         waveCanvas = new MCanvas();
00145         waveCanvas->PutInWinForm(myWindow, lx1,yy2,430,200);
00146         
00147         // Setup Inputs
00148         /*
00149         freqInput = new AInput("Freq", 100.0f, 22000.0f, 1000, false);
00150         FMInput = new AInput("FM", 0.0001f, 1.5f, 1000, false);
00151         PWMInput = new AInput("PWM", 0.0001f, 1.5f, 1000, false);
00152         midikeyInput = new SlideInput("Show Midi Key:", 21.0, 107.0, 86, true);
00153 
00154         freqInput->PutInWinForm(myWindow, lx1,yy4,200,200);
00155         FMInput->PutInWinForm(myWindow, lx1,yy5,200,200);
00156         PWMInput->PutInWinForm(myWindow, lx1,yy6,200,200);
00157         midikeyInput->PutInWinForm(myWindow, lx1, yy2 , 200,200);
00158         */
00159         // Setup buttons
00160 
00161         OKButton = new Button("OK");
00162         CancelButton = new Button("Cancel");
00163         button1 = new Button("Add Point");
00164         button2 = new Button("Del Point");
00165         button3 = new Button("Set Loop");
00166         button1->PutInWinForm(myWindow,lx1,yy4,100,28);
00167         button2->PutInWinForm(myWindow,lx1+110,yy4,100,28);
00168         button3->PutInWinForm(myWindow,lx1+310,yy3,100,28);
00169         OKButton->PutInWinForm(myWindow,lx3,ly1,100,28);
00170         CancelButton->PutInWinForm(myWindow,lx3,ly1+40,100,28);
00171         
00172         // Setup Dividers
00173         div1 = new divider();
00174         div2 = new divider();
00175 
00176 
00177         div1->PutInWinForm(myWindow, lx3-15, 5, 5, 600);
00178         div2->PutInWinForm(myWindow, 5, 29, lx3-35, 5);
00179                 // Debug:
00180         myText2 = new TextBox();
00181         myText2->PutInWinForm(myWindow,lx1+600,450,lx2+200-lx1,100);
00182 
00183         GUIControl::GUIInstance->ErrorTextBox = myText2;
00184         OKButton->setEventHandler(this);
00185         CancelButton->setEventHandler(this);
00186         waveCanvas->setEventHandler(this);      
00187         /*
00188         hSubMenu = CreatePopupMenu();
00189     AppendMenu(hSubMenu, MF_STRING, 441, "D&elete point");
00190         AppendMenu(hSubMenu, MF_STRING, 442, "S&et left LP");
00191         AppendMenu(hSubMenu, MF_STRING, 443, "S&et right LP");
00192 
00193         hSubMenu2 = CreatePopupMenu();
00194     AppendMenu(hSubMenu2, MF_STRING, 441, "N&ew point");
00195         */
00196 
00197 
00198 
00199 
00200         myPopup = new popupMenu(waveCanvas->getHWND());
00201         myPopup->setEventHandler(this);
00202 
00203         myPopup->add(mItem11 = new menuItem("D&elete point"));
00204         myPopup->add(mItem12 = new menuItem("S&et left LP"));
00205         myPopup->add(mItem13 = new menuItem("S&et right LP"));
00206 
00207         myPopup2 = new popupMenu(waveCanvas->getHWND());
00208         myPopup2->setEventHandler(this);
00209 
00210         myPopup2->add(mItem21 = new menuItem("N&ew point"));
00211         
00212         myWindow->show();
00213         myEnvData->interpolate();
00214         drawEnv();
00215 }
00216 
00217 void EnvDialog::MessageLoop() {
00218         // Enter Loop until Exit.
00219         GUIControl::GUIInstance->MessageLoop();
00220 }
00221 
00222 EnvDialog::~EnvDialog() {
00223         // Clean Up.
00224         delete(myWindow);
00225 }
00226 
00227 
00228 
00229 
00230 
00231 void EnvDialog::HandleEvents(event ev) {
00232         if ((ev.EventSource==OKButton)) 
00233         {   
00234                 drawEnv();
00235                 std::cout << "OK BUTTON" << std::endl;
00236         }
00237         
00238         if (ev.EventSource==mItem11) 
00239         {
00240                 std::cout << "DEL POINT" << std::endl;
00241                 myEnvData->removePoint(lastChoosen); myEnvData->interpolate(); drawEnv();
00242         }
00243         if (ev.EventSource==mItem12) 
00244         {
00245                 std::cout << "SET LP" << std::endl;
00246                 myEnvData->beginLP = lastChoosen; drawEnv();
00247         }
00248         if (ev.EventSource==mItem13) 
00249         {
00250                 std::cout << "SET RP" << std::endl;
00251                 myEnvData->endLP = lastChoosen; 
00252                 drawEnv();
00253         }
00254         if (ev.EventSource==mItem21) {
00255                 std::cout << "NEW POINT" << std::endl;
00256                 float x; float y;
00257                 ScreenToEnv(pt.x,pt.y, x,y);
00258                 myEnvData->addPoint(x,y); 
00259                 myEnvData->interpolate();
00260                 drawEnv();
00261         }
00262 
00263 
00264 
00265         if ((ev.EventSource==waveCanvas))
00266         {
00267                 if (ev.msg == WM_LBUTTONDOWN) {
00268                 
00269                         int xPos = LOWORD(ev.lParam);  // horizontal position of cursor
00270                         int yPos = HIWORD(ev.lParam);
00271                 
00272                         int sel = -1;
00273                         for(int j = 0; j < myEnvData->points.size(); j++)
00274                         {
00275                                 // Convert to screen coordinates
00276                                 int x; int y;
00277                                 EnvToScreen(myEnvData->points[j]->x,myEnvData->points[j]->y,x,y);
00278                                 std::cout << x << " " << y << " " << xPos << " " << yPos << std::endl;
00279                                 // Are we closer than Sqrt(20) pixels?
00280                                 if (((xPos-x)*(xPos-x)+(yPos-y)*(yPos-y))<20) {
00281                                         sel = j;
00282                                         std::cout << "SEL"  << std::endl;
00283 
00284                                 }
00285                         }
00286 
00287                         if (sel != -1) {myEnvData->setChoosen(sel);}
00288                         waveCanvas->putPixel(xPos,yPos,211,1,1);
00289                         myEnvData->interpolate();
00290                         drawEnv();
00291                 }
00292 
00293                 if (ev.msg == WM_LBUTTONUP) {
00294                 
00295                         myEnvData->setChoosen(-1);
00296                         myEnvData->interpolate();
00297                         drawEnv();
00298                         
00299                 }
00300 
00301                 if (ev.msg == WM_RBUTTONDOWN) {
00302                         int xPos = LOWORD(ev.lParam);  // horizontal position of cursor
00303                         int yPos = HIWORD(ev.lParam);
00304                         pt.x = xPos;
00305                         pt.y = yPos;
00306                         int sel = -1;
00307                         for(int j = 0; j < myEnvData->points.size(); j++)
00308                         {
00309                                 // Convert to screen coordinates
00310                                 int x; int y;
00311                                 EnvToScreen(myEnvData->points[j]->x,myEnvData->points[j]->y,x,y);
00312                                 std::cout << x << " " << y << " " << xPos << " " << yPos << std::endl;
00313                                 // Are we closer than Sqrt(20) pixels?
00314                                 if (((xPos-x)*(xPos-x)+(yPos-y)*(yPos-y))<20) { sel = j; }
00315                         }
00316                         
00317                         lastChoosen = sel;
00318                         //POINT pt;
00319                         
00320                         if (sel!=-1) {
00321                                 myPopup->showMenu();
00322                                 //      TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN,pt.x,pt.y,0,waveCanvas->getHWND(),NULL);
00323                         } else {
00324                                 myPopup2->showMenu();
00325                                 //TrackPopupMenu(hSubMenu2, TPM_LEFTALIGN|TPM_TOPALIGN,pt.x,pt.y,0,waveCanvas->getHWND(),NULL);
00326                         }
00327                         
00328                 }
00329 
00330                 
00331 
00332                 if ((ev.msg == WM_MOUSEMOVE) && (ev.wParam & MK_LBUTTON))
00333                 {
00334                         if (myEnvData->getChoosen()!=-1) {
00335 
00336                                 int xPos = LOWORD(ev.lParam);  // horizontal position of cursor
00337                                 int yPos = HIWORD(ev.lParam);
00338                                 float x; float y;
00339                                 ScreenToEnv(xPos,yPos, x,y);
00340                                 
00341                                 bool breaks = false;
00342                                 if (myEnvData->getChoosen()!=0)
00343                                         if (x < myEnvData->points[myEnvData->getChoosen()-1]->x) breaks = true;
00344 
00345                                 if (myEnvData->getChoosen()!=(myEnvData->points.size()-1))
00346                                         if (x > myEnvData->points[myEnvData->getChoosen()+1]->x) breaks = true;
00347                                         
00348                                 if (myEnvData->getChoosen() == 0) x=0.0;
00349                                 
00350                                 if (!breaks)                            
00351                                 {
00352                                         myEnvData->points[myEnvData->getChoosen()]->x=x;                                
00353                                         myEnvData->points[myEnvData->getChoosen()]->y=y;
00354                                 }
00355 
00356                                 waveCanvas->putPixel(xPos,yPos,211,211,1);
00357                                 myEnvData->interpolate();
00358                                 drawEnv();
00359                         }
00360                 }
00361         }
00362 
00363         
00364 
00365             
00366 }
00367 
00368 }; // end of namespace: SynthGUI

Syntopia Project. Visit the web page, or the SourceForge page.
Docs made by Doxygen. Email: Mikael Christensen