/*------------------------------------------------------------------------------
 src/lib/djWidget/vecWidget.cxx
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

//#define DJ_DBG
#include "djVecWidget.h"
#include <djButtonWidget.h>
#include <djNumWidget.h>

namespace dj {

Vec2iWidget::Vec2iWidget() :
  _callbacks(true)
{
  _widget = new IntWidget;
  _widget2 = new IntWidget;
  
  sizePolicy(FIXED, FIXED);
  HLayout * layout = new HLayout(this);
  layout->margin(0);
  layout->add(_widget);
  layout->add(_widget2);

  widgetUpdate();
  
  _widget->sig().set(this, widgetCallback);
  _widget2->sig().set(this, widget2Callback);
}
  
void Vec2iWidget::set(const Vec2i & in)
{
  if (in == _value) return;
  _value = in;
  _sig.emit(_value);
  widgetUpdate();
}

void Vec2iWidget::widgetCallback(int in)
{
  if (! _callbacks) return;
  set(Vec2i(in, _value.y()));
}

void Vec2iWidget::widget2Callback(int in)
{
  if (! _callbacks) return;
  set(Vec2i(_value.x(), in));
}

void Vec2iWidget::widgetUpdate()
{
  _callbacks = false;
  _widget->set(_value.x());
  _widget2->set(_value.y());
  _callbacks = true;
}

Vec2fWidget::Vec2fWidget() :
  _widget(0), _widget2(0),
  _callbacks(true)
{
  _widget = new FloatWidget;
  _widget2 = new FloatWidget;
  
  sizePolicy(STRETCH, FIXED);
  HLayout * layout = new HLayout(this);
  layout->margin(0);
  layout->add(_widget);
  layout->add(_widget2);

  widgetUpdate();
  
  _widget->sig().set(this, widgetCallback);
  _widget2->sig().set(this, widget2Callback);
}

void Vec2fWidget::set(const Vec2f & in)
{
  if (in == _value) return;
  _value = in;
  _sig.emit(_value);
  widgetUpdate();
}

void Vec2fWidget::widgetCallback(double in)
{
  if (! _callbacks) return;
  set(Vec2f(in, _value.y()));
}

void Vec2fWidget::widget2Callback(double in)
{
  if (! _callbacks) return;
  set(Vec2f(_value.x(), in));
}

void Vec2fWidget::widgetUpdate()
{
  _callbacks = false;
  _widget->set(_value.x());
  _widget2->set(_value.y());
  _callbacks = true;
}

}
