Blob Blame Raw
#include "view.h"

#include <gtkmm/container.h>


void
View::Point::draw(
	const Cairo::RefPtr<Cairo::Context>& context,
	View &view )
{
	const Real ps = view.get_pixel_size();
	context->save();
	context->arc(position.x, position.y, radius*ps, 0.0, 2.0*M_PI);
	context->set_source_rgba(color.r, color.g, color.b, color.a);
	context->fill_preserve();
	context->set_source_rgba(0.0, 0.0, 0.0, color.a);
	context->set_line_width(selected ? 2.0*ps : ps);
	context->stroke();
	context->restore();
}


View::View():
	dragging()
{
	set_events(
		Gdk::POINTER_MOTION_MASK
	  | Gdk::BUTTON_PRESS_MASK
	  | Gdk::BUTTON_RELEASE_MASK );

	signal_draw_view.connect(sigc::mem_fun(*this, &View::on_draw_view));
	signal_point_motion.connect(sigc::mem_fun(*this, &View::on_point_motion));
	signal_point_changed.connect(sigc::mem_fun(*this, &View::on_point_changed));
	signal_transform_changed.connect(sigc::mem_fun(*this, &View::on_transform_changed));
}

View::~View() { }

Real
View::get_pixel_size() const
	{ return 1.0/sqrt(fabs(transform.m00 * transform.m11)); }

Pair2
View::get_bounds() const {
	Matrix m = transform_from_pixels();
	return Pair2(
		(m*Vector4(0          , 0           , 0, 1)).vec2(),
		(m*Vector4(get_width(), get_height(), 0, 1)).vec2() );
}

Matrix
View::transform_to_pixels() const {
	Matrix matrix = transform;
	matrix.row_w().vec2() += Vector2(get_width(), get_height())*0.5;
	return matrix;
}

Matrix
View::transform_from_pixels() const
	{ return transform_to_pixels().inverted(); }

bool
View::on_draw(const Cairo::RefPtr<Cairo::Context>& context) {
	context->save();

	const Real border_size = 1.0;
	const Real border_margin = 2.0;
	
	context->save();
	context->rectangle(0, 0, get_width(), get_height());
	context->set_source_rgba(0, 0, 0, 1);	
	context->fill();
	context->rectangle(
		border_margin, border_margin,
		get_width() - 2*border_margin,
		get_height() - 2*border_margin );
	context->set_line_width(border_size);
	context->set_source_rgba(1, 1, 0, 1);
	context->stroke();
	context->restore();

	context->transform( transform_to_pixels().to_cairo() );
	signal_draw_view(context);
	for(PointList::const_iterator i = points.begin(); i != points.end(); ++i)
		(*i)->draw(context, *this);
	context->restore();
	return true;
}

bool
View::on_motion_notify_event(GdkEventMotion *event) {
	Vector2 prev_mouse_position = mouse_position;
	mouse_position = Vector2(event->x, event->y);

	Matrix to_pixels = transform_to_pixels();
	Matrix from_pixels = transform_from_pixels();
	
	if (dragging) {
		if (selected_point) {
			selected_point->position = (from_pixels*Vector4(mouse_position + selected_point_offset, 0, 1)).vec2();
			point_motion(selected_point);
			queue_draw();
		}
		return true;
	}

	PointPtr point;
	for(PointList::iterator i = points.begin(); i != points.end(); ++i) {
		(*i)->selected = false;
		Vector2 point_position = (to_pixels*Vector4((*i)->position, 0, 1)).vec2();
		Vector2 offset = point_position - mouse_position;
		if (offset.square() <= (*i)->radius * (*i)->radius) {
			point = *i;
			selected_point_offset = offset;
		}
	}
	if (point)
		point->selected = true;
	if (selected_point != point) {
		selected_point = point;
		queue_draw();
	}

	if (event->state & Gdk::BUTTON2_MASK) {
		// translate
		transform.row_w() += Vector4(mouse_position - prev_mouse_position);
		queue_draw();
	}

	if (event->state & Gdk::BUTTON3_MASK) {
		// zoom
		Real scale = pow(2.0, (prev_mouse_position.y - mouse_position.y)/50.0); // zoom x2 by motion to 50 pixels
		Vector2 center = Vector2(transform.inverted() * Vector4(0, 0, 0, 1));
		transform.scale(scale, scale);
		transform.row_w() = Vector4(0, 0, 0, 1);
		transform.row_w() = transform * Vector4(-center, 0, 1);
		queue_draw();
	}
	
	return true;
}

bool
View::on_button_press_event(GdkEventButton *event) {
	if (event->button == 1)
		dragging = true;
	return true;
}

bool
View::on_button_release_event(GdkEventButton *event) {
	if (event->button == 1) {
		dragging = false;
		if (selected_point)
			point_changed(selected_point);
	}
	if (event->button == 2 || event->button == 3)
		signal_transform_changed();
	return true;
}


void
View::on_draw_view(const Cairo::RefPtr<Cairo::Context>&)
	{ }

void
View::on_point_motion(const PointPtr&)
	{ }

void
View::on_point_changed(const PointPtr&)
	{ }

void
View::on_transform_changed()
	{ }