Blame c++/perspective/src/mainwindow.cpp

538bb7
#include <iostream></iostream>
538bb7
#include <utility></utility>
538bb7
538bb7
#include <gdkmm pixbuf.h=""></gdkmm>
538bb7
#include <gtkmm hvbox.h=""></gtkmm>
538bb7
#include <gtkmm image.h=""></gtkmm>
538bb7
538bb7
#include "mainwindow.h"
538bb7
538bb7
538bb7
MainWindow::MainWindow():
a1942e
	src_rect0   (new View::Point( Vector2(-1, -1) )),
a1942e
	src_rect1   (new View::Point( Vector2( 1,  1) )),
a1942e
	dst_rect0   (new View::Point( Vector2(-1, -1) )),
a1942e
	dst_rect1   (new View::Point( Vector2( 1, -1) )),
a1942e
	dst_rect2   (new View::Point( Vector2( 1,  1) )),
a1942e
	dst_rect3   (new View::Point( Vector2(-1,  1) )),
a1942e
	dst_bounds0 (new View::Point( Vector2(-2, -2) )),
a1942e
	dst_bounds1 (new View::Point( Vector2( 2,  2) ))
538bb7
{
a1942e
	std::cout << "generator seed: " << generator.seed() << std::endl;
a1942e
	set_default_size(750, 500);
538bb7
	
538bb7
	Gtk::HBox *hbox = manage(new Gtk::HBox());
538bb7
	hbox->set_homogeneous(true);
538bb7
	
a1942e
	src_view.transform.scale(50, 50);
538bb7
	src_view.points.push_back(src_rect0);
538bb7
	src_view.points.push_back(src_rect1);
538bb7
	src_view.signal_point_motion.connect(
538bb7
		sigc::mem_fun(*this, &MainWindow::on_point_motion) );
538bb7
	src_view.signal_point_changed.connect(
538bb7
		sigc::mem_fun(*this, &MainWindow::on_point_changed) );
a1942e
	src_view.signal_transform_changed.connect(
a1942e
		sigc::bind(
a1942e
			sigc::mem_fun(*this, &MainWindow::on_view_transform_changed),
a1942e
			&src_view ));
538bb7
	src_view.signal_draw_view.connect(
538bb7
		sigc::mem_fun(*this, &MainWindow::on_draw_src_view) );
538bb7
	hbox->add(src_view);
538bb7
	
a1942e
	dst_view.transform.scale(50, 50);
538bb7
	dst_view.points.push_back(dst_rect0);
538bb7
	dst_view.points.push_back(dst_rect1);
538bb7
	dst_view.points.push_back(dst_rect2);
538bb7
	dst_view.points.push_back(dst_rect3);
538bb7
	dst_view.points.push_back(dst_bounds0);
538bb7
	dst_view.points.push_back(dst_bounds1);
538bb7
	dst_view.signal_point_motion.connect(
538bb7
		sigc::mem_fun(*this, &MainWindow::on_point_motion) );
538bb7
	dst_view.signal_point_changed.connect(
538bb7
		sigc::mem_fun(*this, &MainWindow::on_point_changed) );
a1942e
	src_view.signal_transform_changed.connect(
a1942e
		sigc::bind(
a1942e
			sigc::mem_fun(*this, &MainWindow::on_view_transform_changed),
a1942e
			&dst_view ));
538bb7
	dst_view.signal_draw_view.connect(
538bb7
		sigc::mem_fun(*this, &MainWindow::on_draw_dst_view) );
538bb7
	hbox->add(dst_view);
538bb7
	
a1942e
	//hbox->add(*manage(generator_demo()));
538bb7
	
538bb7
	add(*hbox);
538bb7
	show_all();
a1942e
	
a1942e
	update_view_surface();
538bb7
}
538bb7
538bb7
Gtk::Widget*
538bb7
MainWindow::generator_demo() {
538bb7
	std::cout << "generator demo: generate" << std::endl;
538bb7
	DataSurface surface(256, 256);
538bb7
	generator.generate(surface, Vector2(0, 0), Vector2(16, 16));
538bb7
	
538bb7
	std::cout << "generator demo: convert" << std::endl;
538bb7
	Glib::RefPtr<gdk::pixbuf> pixbuf = Gdk::Pixbuf::create(</gdk::pixbuf>
538bb7
		Gdk::COLORSPACE_RGB, true, 8, surface.width(), surface.height() );
538bb7
	guint8 *pixel = pixbuf->get_pixels();
538bb7
	for(int r = 0; r < surface.height(); ++r) {
538bb7
		Color *row = surface[r];
538bb7
		for(int c = 0; c < surface.width(); ++c) {
538bb7
			const Color &color = row[c];
538bb7
			*(pixel++) = (guint8)(color.r*255.9);
538bb7
			*(pixel++) = (guint8)(color.g*255.9);
538bb7
			*(pixel++) = (guint8)(color.b*255.9);
538bb7
			*(pixel++) = (guint8)(color.a*255.9);
538bb7
		}
538bb7
	}
538bb7
538bb7
	return new Gtk::Image(pixbuf);
538bb7
}
538bb7
538bb7
void
a1942e
MainWindow::update_view_surface() {
a1942e
	Matrix4 transform = src_view.transform.inverted();
a1942e
	int w = src_view.get_width();
a1942e
	int h = src_view.get_height();
a1942e
	
a1942e
	Vector2 rect0(transform * Vector4(0, 0, 0, 1));
a1942e
	Vector2 rect1(transform * Vector4(w, h, 0, 1));
a1942e
	
a1942e
	if (w <= 0 || h <= 0) {
a1942e
		view_surface.clear();
a1942e
	} else
a1942e
	if ( !view_surface
a1942e
	  || view_surface->get_width() != w
a1942e
	  || view_surface->get_height() != h
a1942e
	  || view_surface_rect0 != rect0
a1942e
	  || view_surface_rect1 != rect1 )
a1942e
	{
a1942e
		DataSurface surface(w, h);
a1942e
		generator.generate(surface, rect0, rect1);
a1942e
		view_surface = surface.to_cairo_surface();
a1942e
		view_surface_rect0 = rect0;
a1942e
		view_surface_rect1 = rect1;
a1942e
	}
a1942e
}
a1942e
a1942e
void
538bb7
MainWindow::on_point_motion(const View::PointPtr &/*point*/) {
538bb7
	queue_draw();
538bb7
}
538bb7
538bb7
void
538bb7
MainWindow::on_point_changed(const View::PointPtr &/*point*/) {
538bb7
}
538bb7
538bb7
void
a1942e
MainWindow::on_view_transform_changed(View *view) {
a1942e
	if (view == &src_view) {
a1942e
		update_view_surface();
a1942e
		view->queue_draw();
a1942e
	}
a1942e
}
a1942e
a1942e
void
538bb7
MainWindow::on_draw_src_view(const Cairo::RefPtr<cairo::context> &context) {</cairo::context>
538bb7
	const Real ps = src_view.get_pixel_size();
538bb7
538bb7
	context->save();
a1942e
a1942e
	if ( !view_surface
a1942e
	  || src_view.get_width() != view_surface->get_width()
a1942e
	  || src_view.get_height() != view_surface->get_height() )
a1942e
		update_view_surface();
a1942e
	if (view_surface) {
a1942e
		context->save();
a1942e
		src_view.back_transform_context(context);
a1942e
		context->set_source(view_surface, 0, 0);
a1942e
		context->paint();
a1942e
		context->restore();
a1942e
	}
538bb7
	
538bb7
	context->move_to(src_rect0->position.x, src_rect0->position.y);
538bb7
	context->line_to(src_rect0->position.x, src_rect1->position.y);
538bb7
	context->line_to(src_rect1->position.x, src_rect1->position.y);
538bb7
	context->line_to(src_rect1->position.x, src_rect0->position.y);
538bb7
	context->close_path();
538bb7
	context->set_line_width(ps);
538bb7
	context->set_source_rgba(1, 1, 0, 0.5);
538bb7
	context->stroke();
538bb7
	
538bb7
	context->restore();
538bb7
}
538bb7
538bb7
void
538bb7
MainWindow::on_draw_dst_view(const Cairo::RefPtr<cairo::context> &context) {</cairo::context>
538bb7
	const Real ps = src_view.get_pixel_size();
538bb7
538bb7
	context->save();
538bb7
	
538bb7
	context->move_to(dst_rect0->position.x, dst_rect0->position.y);
538bb7
	context->line_to(dst_rect1->position.x, dst_rect1->position.y);
538bb7
	context->line_to(dst_rect2->position.x, dst_rect2->position.y);
538bb7
	context->line_to(dst_rect3->position.x, dst_rect3->position.y);
538bb7
	context->close_path();
538bb7
	context->set_line_width(ps);
538bb7
	context->set_source_rgba(1, 1, 0, 0.5);
538bb7
	context->stroke();
538bb7
538bb7
	context->move_to(dst_bounds0->position.x, dst_bounds0->position.y);
538bb7
	context->line_to(dst_bounds0->position.x, dst_bounds1->position.y);
538bb7
	context->line_to(dst_bounds1->position.x, dst_bounds1->position.y);
538bb7
	context->line_to(dst_bounds1->position.x, dst_bounds0->position.y);
538bb7
	context->close_path();
538bb7
	context->set_line_width(ps);
538bb7
	context->set_source_rgba(1, 0, 0, 0.5);
538bb7
	context->stroke();
538bb7
538bb7
	context->restore();
538bb7
}