//-----------------------------------------------------------------------------
// Filename   : 
// Created by : 
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#ifndef __waveform__
#include "waveform.h"
#endif

//BEGIN_NAMESPACE_VSTGUI


//------------------------------------------------------------------------
// CWaveform
//------------------------------------------------------------------------
CWaveform::CWaveform(const CRect &size, CControlListener *listener, long tag, CBitmap *pBackground) 
:    CControl(size, listener, tag, pBackground) 
  {
	back = pBackground;
	canDraw = false;
	m_CColor = kWhiteCColor;
	values = NULL;
	zoom_x = zoom_y = 1.0f;
  }


//------------------------------------------------------------------------
CWaveform::~CWaveform ()
{}

//------------------------------------------------------------------------
void CWaveform::setValue (float pvalues[], DWORD size)
{
	if (pvalues && size) {
		values = pvalues;   //preda ukazatel z parametru do privatni promenne tridy
		m_dwSize = size;
		canDraw = true;     //priznak, ze muze vykreslovat waveform - aby necetl z neinicializovaneho pole
		setDirty ();
	}
	else {
		values = NULL;
		m_dwSize = 0;
		canDraw = false;
		setDirty ();
	}
}

//------------------------------------------------------------------------
void CWaveform::setColor (CColor color)
{
	m_CColor = color;;
}

//------------------------------------------------------------------------
void CWaveform::draw (CDrawContext *pContext)
{	
	CRect size;
	COffscreenContext screen(pContext,back);    //vytvori ofscreen kontext - (bitmapa se nezobrazi..!! ???)
	screen.setFrameColor(m_CColor);			    //kterou barvou se bude kreslit
	CPoint point;

	if (canDraw)
	{
		DWORD frame = 0;
		float *pSamples = values;
		DWORD frameWidth = back->getWidth();
		DWORD frameHeight = (back->getHeight())>>1;
		
		float scale = zoom_y*(float)frameHeight;
		float step = (float)m_dwSize / (float)frameWidth;
		if (step < 1.0f) step = 1.0f;

		screen.moveTo( point(0, frameHeight) );
		DWORD pos = 0;

		float max = 0, min = 0, sample = 0, value = 0;
		do {
			sample = fabs(*pSamples++);
			if( sample > max ) max = sample;
//			else if( sample < min ) min = sample;
			if(frame == (DWORD)(pos*step)) {
				value = /*( fabs(min)>max ? min : max )*/ /*(max+min)*/ max * scale;
			/* if(!pos)
					screen.moveTo( point( pos++, frameHeight - (int)(value) ) );
				else
					screen.lineTo( point( pos++, frameHeight - (int)(value) ) ); */
				screen.moveTo( point( pos, frameHeight + (int)value ) );
				screen.lineTo( point( pos++, frameHeight - (int)value ) );
				max = min = 0;
			} 

		} while (frame++ < m_dwSize);
		screen.moveTo( point(0, frameHeight) );
		screen.lineTo( point(frameWidth, frameHeight) );

		size(this->size.left,this->size.top,this->size.right,this->size.bottom );
		screen.copyFrom(pContext,size);				//prekopirovani ofscreenu na obrazovku

}

//------------------------------------------------------------------------
END_NAMESPACE_VSTGUI
//------------------------------------------------------------------------
// END.
//------------------------------------------------------------------------