Blob Blame Raw
#ifndef VIEW_H
#define VIEW_H


#include <vector>

#include <gtkmm/drawingarea.h>

#include "common.h"
#include "surface.h"
#include "matrix.h"


class View: public Gtk::DrawingArea {
public:
	class Point: public Shared {
	public:
		Vector2 position;
		Color color;
		Real radius;
		bool selected;
		
		inline explicit Point(
			const Vector2 &position = Vector2(),
			const Color &color = Color(1, 1, 0, 1),
			Real radius = 5.0,
			bool selected = false
		):
			position(position),
			color(color),
			radius(radius),
			selected(selected)
		{ }
		
		virtual void draw(
			const Cairo::RefPtr<Cairo::Context>& context,
			View &view );
	};

	typedef RefPtr<Point> PointPtr;
	typedef std::vector<PointPtr> PointList;

public:
	Matrix transform;
	PointList points;

private:
	bool dragging;
	PointPtr selected_point;
	Vector2 selected_point_offset;
	Vector2 mouse_position; // relative to center of widget

public:
	View();
	virtual ~View();

	sigc::signal<void, const Cairo::RefPtr<Cairo::Context>&> signal_draw_view;
	sigc::signal<void, const PointPtr&> signal_point_motion;
	sigc::signal<void, const PointPtr&> signal_point_changed;
	sigc::signal<void> signal_transform_changed;
	
	void point_motion(const PointPtr &point)
		{ signal_point_motion(point); }
	void point_changed(const PointPtr &point)
		{ point_motion(point); signal_point_changed(point); }

	Real get_pixel_size() const;
	Pair2 get_bounds() const;
	
	Matrix transform_to_pixels() const;
	Matrix transform_from_pixels() const;
	
protected:
	bool on_draw(const Cairo::RefPtr<Cairo::Context> &context) override;
	bool on_motion_notify_event(GdkEventMotion *event) override;
	bool on_button_press_event(GdkEventButton *event) override;
	bool on_button_release_event(GdkEventButton *event) override;

	virtual void on_draw_view(const Cairo::RefPtr<Cairo::Context> &context);
	virtual void on_point_motion(const PointPtr &point);
	virtual void on_point_changed(const PointPtr &point);
	virtual void on_transform_changed();
};


#endif