/*
  ==============================================================================

  This is an automatically generated file created by the Jucer!

  Creation date:  18 Sep 2012 6:24:20am

  Be careful when adding custom code to these files, as only the code within
  the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  and re-saved.

  Jucer version: 1.12

  ------------------------------------------------------------------------------

  The Jucer is part of the JUCE library - "Jules' Utility Class Extensions"
  Copyright 2004-6 by Raw Material Software ltd.

  ==============================================================================
*/

//[Headers] You can add your own extra header files here...
//[/Headers]

#include "graph_window.h"


//[MiscUserDefs] You can add your own user definitions and misc code here...
#ifndef max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b)            (((a) < (b)) ? (a) : (b))
#endif
//[/MiscUserDefs]

//==============================================================================
GraphWindow::GraphWindow ()
    : Component (L"graph_window")
{

    //[UserPreSize]
    //[/UserPreSize]

    setSize (160, 120);


    //[Constructor] You can add your own custom stuff here..
	frame_width = 1;
	m_markerX = 0;
	paramInfoInit(&curve, 0, NULL);
	paramInfoSet(&curve, 1, 1, 0.5, -1, 0, +1, 0);

    //[/Constructor]
}

GraphWindow::~GraphWindow()
{
    //[Destructor_pre]. You can add your own custom destruction code here..
    //[/Destructor_pre]

    //[Destructor]. You can add your own custom destruction code here..
	paramInfoFree(&curve);
    //[/Destructor]
}

//==============================================================================
void GraphWindow::paint (Graphics& g)
{
    //[UserPrePaint] Add your own custom painting code here..
	goto skip;
    //[/UserPrePaint]

    g.setColour (Colours::white);
    g.drawRoundedRectangle (3.0f, 3.0f, 156.0f, 116.0f, 1.0000f, 2.0000f);

    //[UserPaint] Add your own custom painting code here..

skip:
	int minX = 0;
	int minY = frame_width;
	int maxX = getWidth();
	int maxY = getHeight()-frame_width;
	int sizeX = maxX-minX;
	int sizeY = maxY-minY;

	g.setColour (Colour(0xFFB0B0B0));
	g.drawLine(minX, (int)((1-evalCurve(m_markerX))*sizeY) + 1.5*minY, maxX, (int)((1-evalCurve(m_markerX))*sizeY) + 1.5*minY);
	g.drawLine(m_markerX*sizeX, minY, m_markerX*sizeX, maxY);

	g.setColour (Colours::white);

	float dx = 1.0/sizeX;
	float x0 = 0;
	float x1 = dx;
	float y0, y1;

	for (int i=minX+1; i <= maxX; i++)
	{
		y0 = evalCurve(x0);
		y1 = evalCurve(x1);
		x0 = x1;
		x1 += dx;
		g.drawLine(i-1, /*(1-0.5*(1+y0))*sizeY*/(1-y0)*sizeY + minY, i, /*(1-0.5*(1+y1))*sizeY*/(1-y1)*sizeY + minY);
	}

	g.setColour (Colours::white);
    g.drawRect (0.0, 0.0, (float)getWidth(), (float)getHeight(), frame_width);

	//[/UserPaint]
}

void GraphWindow::resized()
{
    //[UserResized] Add your own custom resize handling here..
	repaint();
    //[/UserResized]
}


float GraphWindow::evalCurve(float x)
{
//	return min(m_max, max(m_min, toParam(&curve, x)));

	if (m_max > m_min)
		return (toParam(&curve, x)-m_min)/(m_max - m_min);
	else
		return (toParam(&curve, x)-m_max)/(m_min - m_max);

}

//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
void GraphWindow::Draw(float minimum, float maximum, param_info_t *pParamInfo)
{
	m_min = minimum;
	m_max = maximum;
	paramInfoCopy(&curve, pParamInfo);
	repaint();
}

void GraphWindow::SetMarkerX(float x)
{
	m_markerX = x;
	repaint();

}

//[/MiscUserCode]


//==============================================================================
#if 0
/*  -- Jucer information section --

    This is where the Jucer puts all of its metadata, so don't change anything in here!

BEGIN_JUCER_METADATA

<JUCER_COMPONENT documentType="Component" className="GraphWindow" componentName="graph_window"
                 parentClasses="public Component" constructorParams="" variableInitialisers=""
                 snapPixels="4" snapActive="1" snapShown="1" overlayOpacity="0.330000013"
                 fixedSize="1" initialWidth="160" initialHeight="120">
  <BACKGROUND backgroundColour="ffffff">
    <ROUNDRECT pos="3 3 156 116" cornerSize="1" fill="solid: ffffff" hasStroke="1"
               stroke="2, mitered, butt" strokeColour="solid: ffffffff"/>
  </BACKGROUND>
</JUCER_COMPONENT>

END_JUCER_METADATA
*/
#endif
