//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit3.h"
#include "Unit1.h"
#include "audio.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm3 *Form3;
AnsiString SendBuf;
char buff[1000];
PEN_TABLE pen1,pen2;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
        : TForm(Owner)
{
   StatusBar1->Panels->Items[0]->Width = 200;
   Image->Canvas->Pen->Style = psSolid;
   Saved = false;
   Figure = Draw;

   pen1.color=0;// black
   pen2.color=0;
   pen1.width=1;
   pen2.width=1;
   pen1.figure="Draw";
   pen2.figure="Draw";
}
//---------------------------------------------------------------------------
void __fastcall TForm3::SolidClick(TObject *Sender)
{
   if ( Sender == Solid )
   {
      pen1.style=psSolid;
      SendBuf = Image->Canvas->Pen->Style;
   }    
   else if ( Sender == Dash )
   {
      pen1.style=psDash;
      SendBuf = Image->Canvas->Pen->Style;
   }     
   else if ( Sender == Dot )
   {
      pen1.style=psDot;
      SendBuf = Image->Canvas->Pen->Style;
   }
   else if ( Sender == Ellip )
   {
      pen1.figure="Ellip";
      SendBuf = "Ellip";
   }
   else if ( Sender == SolidElli )
   {
      pen1.figure="SolidElli";
      SendBuf = "SolidElli";
   }
   else if ( Sender == Rectan )
   {
      pen1.figure="Rectan";
      SendBuf = "Rectan";
   }
   else if ( Sender == RoundRec )
   {
      pen1.figure = "RoundRec";
      SendBuf = "RoundRec";
   }
   else if ( Sender == SolidRoRec )
   {
      pen1.figure="SolidRoRec";
      SendBuf = "SolidRoRec";
   }
   else if ( Sender == SolidRec )
   {
      pen1.figure="SolidRoRec";
      SendBuf = "SolidRec";
   }
   else if ( Sender == Eraser )
   {
      SendBuf = "Eraser";
   }
   else if ( Sender == Draw )
   {
      Image->Canvas->Pen->Style = psSolid;
      pen1.figure="Draw";
      SendBuf = "Draw";
   }
   if ( Sender != Ellip && Sender != Rectan && Sender != RoundRec
         && Sender != Draw && Sender != SolidRec && Sender != SolidElli
         && Sender != SolidRoRec )
      if ( Sender == Eraser )
         pen1.figure="Eraser";
      else
         pen1.figure="Solid";
   else if ( Image->Canvas->Pen->Style == psClear )
      Image->Canvas->Pen->Style = psSolid;

   if ( Sender == Dash || Sender == Dot)
   {
      Image->Canvas->Brush->Style = bsClear;
      PreWidth = UpDown1->Position;
      Image->Canvas->Pen->Width = 1;
      Edit1->Enabled = false;
   }
   else
   {
      Image->Canvas->Pen->Width = PreWidth;
      Edit1->Enabled = true;
   }
   if ( Sender != Solid && Sender != Dot && Sender != Dash)
      SendBuf = "6::" + SendBuf + "::";
   else
      SendBuf = "6::Solid::7::" + SendBuf + "::";
   strcpy( buff, SendBuf.c_str());
   NMUDP1->SendBuffer(buff, sizeof( buff), sizeof( buff));
   //Form1->Memo1->Lines->Add(AnsiString(buff));
}
//---------------------------------------------------------------------------


void __fastcall TForm3::ImageMouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
/*
   Drawing = true;
   Image->Canvas->MoveTo( X, Y );
   Origin = Point( X, Y );
   MovePt = Origin;
   Previous = Origin;
   char msg[64];
   wsprintf( msg, "Current: (%d, %d)", X, Y);
   StatusBar1->Panels->Items[0]->Text = msg;

   if ( Figure == Eraser )
      PreCol = Image->Canvas->Pen->Color;
*/
   mouseDown( X, Y, true );
   SendBuf = "1::" + AnsiString( X) + "::" + AnsiString( Y) + "::";
   strcpy( buff, SendBuf.c_str());
   NMUDP1->SendBuffer(buff, sizeof( buff), sizeof( buff));
   //Form1->Memo1->Lines->Add(AnsiString(buff));
}
//---------------------------------------------------------------------------


void __fastcall TForm3::ImageMouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
/*
   if ( Drawing )
   {
      Image->Canvas->Pen->Mode = pmCopy;
      if ( Figure == Ellip || Figure == SolidElli )
         Image->Canvas->Ellipse( Origin.x, Origin.y, MovePt.x, MovePt.y );
      else if ( Figure == Rectan || Figure == SolidRec )
         Image->Canvas->Rectangle( Origin.x, Origin.y, MovePt.x, MovePt.y );
      else if ( Figure == RoundRec || Figure == SolidRoRec )
      {
         Image->Canvas->RoundRect( Origin.x, Origin.y, MovePt.x, MovePt.y,
            20, 20 );
      }
      else if ( Figure == Eraser )
         Image->Canvas->Pen->Color = PreCol;
      else if ( Figure == Draw )
         DrawLine( Previous, Point( X+1,Y+1 ), pmCopy );
      else
         DrawLine( Origin, Point( X, Y ), pmCopy );
      Drawing = false;
   }
*/

   mouseUp(X, Y, true);
   SendBuf = "3::" + AnsiString( X) + "::" + AnsiString( Y) + "::";
   strcpy( buff, SendBuf.c_str());
   NMUDP1->SendBuffer( buff, sizeof( buff), sizeof( buff));
   //Form1->Memo1->Lines->Add(AnsiString(buff));
}
//---------------------------------------------------------------------------

void __fastcall TForm3::ImageMouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
   /*
   if ( Drawing )
   {
      if ( Figure == Ellip || Figure == SolidElli )
      {
         if ( Figure == Ellip )
            Image->Canvas->Brush->Style = bsClear;
         else
         {
            Image->Canvas->Brush->Style = bsSolid;
            Image->Canvas->Brush->Color = Image->Canvas->Pen->Color;
         }
         Image->Canvas->Pen->Mode = pmNotXor;
         Image->Canvas->Ellipse( Origin.x, Origin.y, Previous.x, Previous.y );
         MovePt = Point( X, Y);
         Image->Canvas->Ellipse( Origin.x, Origin.y, MovePt.x, MovePt.y );
      }
      else if ( Figure == Rectan || Figure == SolidRec )
      {
         if ( Figure == Rectan )
            Image->Canvas->Brush->Style = bsClear;
         else
         {
            Image->Canvas->Brush->Style = bsSolid;
            Image->Canvas->Brush->Color = Image->Canvas->Pen->Color;
         }
         Image->Canvas->Pen->Mode = pmNotXor;
         Image->Canvas->Rectangle( Origin.x, Origin.y, Previous.x, Previous.y );
         MovePt = Point( X, Y );
         Image->Canvas->Rectangle( Origin.x, Origin.y, MovePt.x, MovePt.y );
      }
      else if ( Figure == RoundRec || Figure == SolidRoRec )
      {
         if ( Figure == RoundRec )
            Image->Canvas->Brush->Style = bsClear;
         else
         {
            Image->Canvas->Brush->Style = bsSolid;
            Image->Canvas->Brush->Color = Image->Canvas->Pen->Color;
         }
         Image->Canvas->Pen->Mode = pmNotXor;
         Image->Canvas->RoundRect( Origin.x, Origin.y, MovePt.x, MovePt.y,
            20, 20 );
         MovePt = Point( X, Y );
         Image->Canvas->RoundRect( Origin.x, Origin.y, MovePt.x, MovePt.y,
            20, 20 );
      }
      else if ( Figure == Eraser )
      {
         Image->Canvas->Pen->Style = psSolid;
         Image->Canvas->Pen->Color = clWhite;
         MovePt = Point( X, Y );
         DrawLine( Previous, MovePt, pmWhite );
      }
      else
      {
         if ( Figure == Draw )
         {
            MovePt = Point( X+1, Y+1 );
            DrawLine( Previous, MovePt, pmCopy );
         }
         else
         {
            DrawLine( Origin, MovePt, pmNotXor );
            MovePt = Point( X, Y );
            DrawLine( Origin, MovePt, pmNotXor );
         }
      }
   }

   Previous = MovePt;

   char msg[64];
   wsprintf( msg, "Current: (%d, %d)", X, Y );
   StatusBar1->Panels->Items[1]->Text = msg;
   */
   mouseMove(X,Y, true);

   SendBuf = "2::" + AnsiString( X) + "::" + AnsiString( Y) + "::";
   strcpy( buff, SendBuf.c_str());
   NMUDP1->SendBuffer( buff, sizeof( buff), sizeof( buff));
   //Form1->Memo1->Lines->Add(AnsiString(buff));
}
//---------------------------------------------------------------------------

void TForm3::DrawLine( TPoint TopLeft, TPoint BottomRight,
TPenMode AMode)
{
   Image->Canvas->Pen->Mode = AMode;
   Image->Canvas->MoveTo( TopLeft.x, TopLeft.y );
   Image->Canvas->LineTo( BottomRight.x, BottomRight.y );
}

void __fastcall TForm3::SpeedButton3Click(TObject *Sender)
{
   ColorDialog1->Color = Image->Canvas->Pen->Color;
   if ( ColorDialog1->Execute())
   {
      Image->Canvas->Pen->Color = ColorDialog1->Color;
      pen1.color=  ColorDialog1->Color;
      ColorBox1->Selected = Image->Canvas->Pen->Color;

      SendBuf = "4::" + AnsiString(Image->Canvas->Pen->Color) + "::";
      strcpy( buff, SendBuf.c_str());
      NMUDP1->SendBuffer(buff, sizeof( buff), sizeof( buff));
      //Form1->Memo1->Lines->Add(AnsiString(buff));
   }
}
//---------------------------------------------------------------------------


void __fastcall TForm3::O1Click(TObject *Sender)
{
   if ( OpenDialog1->Execute() )
   {
      Image->Picture->LoadFromFile(OpenDialog1->FileName);
      Caption = ExtractFileName( OpenDialog1->FileName);
      Saved = false;
      Image->Canvas->Pen->Width = PreWidth;
   }
}
//---------------------------------------------------------------------------


void __fastcall TForm3::UpDown1Changing(TObject *Sender, bool &AllowChange)
{
   Image->Canvas->Pen->Width = UpDown1->Position;
   pen1.width = UpDown1->Position;
   PreWidth = UpDown1->Position;
   SendBuf = "5::" + AnsiString( PreWidth) + "::";
   strcpy( buff, SendBuf.c_str());
   NMUDP1->SendBuffer(buff, sizeof( buff), sizeof( buff));
   //Form1->Memo1->Lines->Add(AnsiString(buff));
}
//---------------------------------------------------------------------------


void __fastcall TForm3::S1Click(TObject *Sender)
{
   if ( Saved == true )
      Image->Picture->SaveToFile( ExtractFileName( SaveDialog1->FileName )
         + ".bmp" );
   else
   {
      if ( SaveDialog1->Execute() )
      {
         Image->Picture->SaveToFile( ExtractFileName( SaveDialog1->FileName )
            + ".bmp" );
         Caption = ExtractFileName( SaveDialog1->FileName );
         Saved = true;
      }
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm3::A1Click(TObject *Sender)
{
    if ( SaveDialog1->Execute() )
    {
       Image->Picture->SaveToFile( ExtractFileName( SaveDialog1->FileName )
          + ".bmp" );
       Caption = ExtractFileName( SaveDialog1->FileName );
       Saved = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm3::N1Click(TObject *Sender)
{
   if ( Saved == false )
   {
      if ( SaveDialog1->Execute() )
         Image->Picture->SaveToFile( ExtractFileName( SaveDialog1->FileName )
            + ".bmp" );
      else
         return;
   }
   else
      Image->Picture->SaveToFile( ExtractFileName( SaveDialog1->FileName )
         + ".bmp" );
   Image->Canvas->Brush->Style = bsSolid;
   Image->Canvas->Brush->Color = clWhite;
   Image->Canvas->Pen->Color = clWhite;
   Image->Canvas->Rectangle( 0, 0, 955, 664 );
   Image->Canvas->Pen->Color = clBlack;
   Saved = false;
}
//---------------------------------------------------------------------------



void __fastcall TForm3::X1Click(TObject *Sender)
{
    Form3->Close();        
}
//---------------------------------------------------------------------------

void __fastcall TForm3::A2Click(TObject *Sender)
{
   OKBottomDlg->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TForm3::ColorBox1Change(TObject *Sender)
{
   Image->Canvas->Pen->Color = ColorBox1->Selected;

   pen1.color= Image->Canvas->Pen->Color;

   SendBuf = "4::" + AnsiString(Image->Canvas->Pen->Color) + "::";
   strcpy( buff, SendBuf.c_str());
   NMUDP1->SendBuffer(buff, sizeof( buff), sizeof( buff));
   //Form1->Memo1->Lines->Add(AnsiString(buff));
}
//---------------------------------------------------------------------------

void __fastcall TForm3::NMUDP1DataReceived(TComponent *Sender,
      int NumberBytes, AnsiString FromIP, int Port)
{
   char buff[1000];
   char* buffcolor;
   AnsiString recv,temp;
   int X,Y;
   NMUDP1->ReadBuffer(buff, NumberBytes, NumberBytes);
   //int j;
   //buff[j]=0;
   recv=AnsiString(buff);
   //Form1->Memo1->Lines->Add(FromIP);
   //Form1->Memo1->Lines->Add(AnsiString(Port));
   //Form1->Memo1->Lines->Add(recv);
   try{
   while(recv.AnsiPos("::")!=0){
     	temp=recv.SubString(1,recv.AnsiPos("::")-1);
        recv.Delete(1,recv.AnsiPos("::")+1);
        if(temp.AnsiCompare("1")==0){
        	temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                X=temp.ToInt();
        	temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                Y=temp.ToInt();
                mouseDown(X,Y,false);
        }
        else if(temp.AnsiCompare("2")==0){
        	temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                X=temp.ToInt();
        	temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                Y=temp.ToInt();
                mouseMove(X,Y,false);
        }
        else if(temp.AnsiCompare("3")==0){
        	temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                X=temp.ToInt();
        	temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                Y=temp.ToInt();
                mouseUp(X,Y,false);
        }
        else if(temp.AnsiCompare("4")==0){
		temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                pen2.color=temp.ToInt();
        }
        else if(temp.AnsiCompare("5")==0){
		temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                pen2.width=temp.ToInt();
        }
        else if(temp.AnsiCompare("6")==0){
		temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                pen2.figure=temp;
        }
        else if(temp.AnsiCompare("7")==0){
		temp=recv.SubString(1,recv.AnsiPos("::")-1);
        	recv.Delete(1,recv.AnsiPos("::")+1);
                pen2.style=temp.ToInt();
        }
        /*
        if(temp.AnsiCompare("psSolid"))
             	Form3->Image->Canvas->Pen->Style = psSolid;
        if(temp.AnsiCompare("psDash"))
              	Form3->Image->Canvas->Pen->Style = psDash;
        */
   }
   }
   catch(EConvertError *err){Form1->Memo1->Lines->Add(err->Message);}
   //mouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
}
//---------------------------------------------------------------------------

void TForm3::mouseDown(int X, int Y, bool my)
{
   PEN_TABLE *pen;
   if(my)
     pen = &pen1;
   else
     pen = &pen2;

   setPen(pen);
   pen->Drawing = true;

   Image->Canvas->MoveTo( X, Y );
   pen->Origin = Point( X, Y );
   pen->MovePt = pen->Origin;
   pen->Previous = pen->Origin;
/*
   if(my){
      Origin1 = Point( X, Y );
      MovePt1 = Origin1;
      Previous1 = Origin1;
   }
   else if(!my){
      Origin2 = Point( X, Y );
      MovePt2 = Origin2;
      Previous2 = Origin2;
   }
*/

   char msg[64];
   wsprintf( msg, "Current: (%d, %d)", X, Y);
   StatusBar1->Panels->Items[0]->Text = msg;

   if ( Figure == Eraser )
      PreCol = Image->Canvas->Pen->Color;
}
//---------------------------------------------------------------------------

void TForm3::mouseUp(int X, int Y, bool my)
{
   PEN_TABLE *pen;
   if(my)
     pen = &pen1;
   else
     pen = &pen2;

/*   if ( Drawing1 || Drawing2 )*/
   if ( pen->Drawing )
   {
      setPen(pen);
      Image->Canvas->Pen->Mode = pmCopy;
      if ( pen->figure.AnsiCompare("Ellip")==0 || pen->figure.AnsiCompare("SolidElli")==0 )
         Image->Canvas->Ellipse( pen->Origin.x, pen->Origin.y, pen->MovePt.x, pen->MovePt.y );
      else if ( pen->figure.AnsiCompare("Rectan")==0 || pen->figure.AnsiCompare("SolidRec")==0 )
         Image->Canvas->Rectangle( pen->Origin.x, pen->Origin.y, pen->MovePt.x, pen->MovePt.y );
      else if ( pen->figure.AnsiCompare("RoundRec")==0 || pen->figure.AnsiCompare("SolidRoRec")==0 )
      {
         Image->Canvas->RoundRect( pen->Origin.x, pen->Origin.y, pen->MovePt.x, pen->MovePt.y,
            20, 20 );
      }
      else if ( pen->figure.AnsiCompare("Eraser")==0 )
         Image->Canvas->Pen->Color = PreCol;
      else if ( pen->figure.AnsiCompare("Draw")==0 ){
	 DrawLine( pen->Previous, Point( X+1,Y+1 ), pmCopy );
/*         if(Drawing1 && my){
         	setPen(pen1);
	        DrawLine( Previous1, Point( X+1,Y+1 ), pmCopy );
         }
         if(Drawing2 && !my){
	         setPen(pen2);
         	DrawLine( Previous2, Point( X+1,Y+1 ), pmCopy );
         }
*/
      }
      else
         DrawLine( pen->Origin, Point( X, Y ), pmCopy );

      pen->Drawing = false;
/*
      if (Drawing1 && my) Drawing1 = false;
      if (Drawing2 && !my) Drawing2 = false;
*/
   }
}
//---------------------------------------------------------------------------
void TForm3::mouseMove(int X, int Y, bool my)
{
   PEN_TABLE *pen;
   if(my)
     pen = &pen1;
   else
     pen = &pen2;

   if ( pen->Drawing )
   {
      setPen(pen);
      if ( pen->figure.AnsiCompare("Ellip")==0 || pen->figure.AnsiCompare("SolidElli")==0 )
      {
         if ( pen->figure.AnsiCompare("Ellip")==0 )
            Image->Canvas->Brush->Style = bsClear;
         else
         {
            Image->Canvas->Brush->Style = bsSolid;
            Image->Canvas->Brush->Color = Image->Canvas->Pen->Color;
         }
         Image->Canvas->Pen->Mode = pmNotXor;
         Image->Canvas->Ellipse( pen->Origin.x, pen->Origin.y, pen->Previous.x, pen->Previous.y );
         pen->MovePt = Point( X, Y);
         Image->Canvas->Ellipse( pen->Origin.x, pen->Origin.y, pen->MovePt.x, pen->MovePt.y );
      }
      else if ( pen->figure.AnsiCompare("Rectan")==0 || pen->figure.AnsiCompare("SolidRec")==0 )
      {
         if ( pen->figure.AnsiCompare("Rectan")==0 )
            Image->Canvas->Brush->Style = bsClear;
         else
         {
            Image->Canvas->Brush->Style = bsSolid;
            Image->Canvas->Brush->Color = Image->Canvas->Pen->Color;
         }
         Image->Canvas->Pen->Mode = pmNotXor;
         Image->Canvas->Rectangle( pen->Origin.x, pen->Origin.y, pen->Previous.x, pen->Previous.y );
         pen->MovePt = Point( X, Y );
         Image->Canvas->Rectangle( pen->Origin.x, pen->Origin.y, pen->MovePt.x, pen->MovePt.y );
      }
      else if ( pen->figure.AnsiCompare("RoundRec")==0 || pen->figure.AnsiCompare("SolidRoRec")==0 )
      {
         if ( pen->figure.AnsiCompare("RoundRec")==0 )
            Image->Canvas->Brush->Style = bsClear;
         else
         {
            Image->Canvas->Brush->Style = bsSolid;
            Image->Canvas->Brush->Color = Image->Canvas->Pen->Color;
         }
         Image->Canvas->Pen->Mode = pmNotXor;
         Image->Canvas->RoundRect( pen->Origin.x, pen->Origin.y, pen->MovePt.x, pen->MovePt.y,
            20, 20 );
         pen->MovePt = Point( X, Y );
         Image->Canvas->RoundRect( pen->Origin.x, pen->Origin.y, pen->MovePt.x, pen->MovePt.y,
            20, 20 );
      }
      else if ( pen->figure.AnsiCompare("Eraser")==0 )
      {
         Image->Canvas->Pen->Style = psSolid;
         Image->Canvas->Pen->Color = clWhite;
         pen->MovePt = Point( X, Y );
         DrawLine( pen->Previous, pen->MovePt, pmWhite );
      }
      else
      {
         if ( pen->figure.AnsiCompare("Draw")==0 )
         {
            	pen->MovePt = Point( X+1, Y+1 );
            	DrawLine( pen->Previous, pen->MovePt, pmCopy );
         /*
            if(my && Drawing1){
            	setPen(pen1);
            	MovePt1 = Point( X+1, Y+1 );
            	DrawLine( Previous1, MovePt1, pmCopy );
            }else if(!my && Drawing2){
            	setPen(pen2);
            	MovePt2 = Point( X+1, Y+1 );
            	DrawLine( Previous2, MovePt2, pmCopy );
            }
            */
         }
         else
         {
            DrawLine( pen->Origin, pen->MovePt, pmNotXor );
            pen->MovePt = Point( X, Y );
            DrawLine( pen->Origin, pen->MovePt, pmNotXor );
         }
      }
   }

   pen->Previous = pen->MovePt;
   //Previous = MovePt;
/*
   if(my && Drawing1){
   	Previous1 = MovePt1;
   }else if(!my && Drawing2){
   	Previous2 = MovePt2;
   }
*/
   char msg[64];
   wsprintf( msg, "Current: (%d, %d)", X, Y );
   StatusBar1->Panels->Items[1]->Text = msg;

}
//---------------------------------------------------------------------------
void TForm3::setPen(PEN_TABLE *pen)
{
   Image->Canvas->Pen->Color=pen->color;
   Image->Canvas->Pen->Width=pen->width;
   Image->Canvas->Pen->Style=pen->style;

}

void __fastcall TForm3::FormClose(TObject *Sender, TCloseAction &Action)
{
   if ( Form1->useSer == 1)
      Form1->ServerSocket1->Socket->Connections[0]->SendText("CloseBoard");
   else
      Form1->ClientSocket1->Socket->SendText("CloseBoard");
}
//---------------------------------------------------------------------------

