1 /* === S Y N F I G ========================================================= */
2 /*! \file state_zoom.cpp
3 ** \brief Zoom Tool Implementation File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <sigc++/signal.h>
33 #include <sigc++/object.h>
36 #include <synfig/vector.h>
39 #include "state_zoom.h"
40 #include "event_mouse.h"
41 #include "canvasview.h"
45 #include <synfigapp/main.h>
51 /* === U S I N G =========================================================== */
55 using namespace synfig;
56 using namespace studio;
58 /* === M A C R O S ========================================================= */
60 /* === G L O B A L S ======================================================= */
61 StateZoom studio::state_zoom;
63 const float ZOOMFACTOR = 1.25f;
65 /* === C L A S S E S & S T R U C T S ======================================= */
67 class studio::StateZoom_Context : public sigc::trackable
69 etl::handle<CanvasView> canvas_view_;
70 CanvasView::IsWorking is_working;
74 bool prev_workarea_layer_status_;
79 Smach::event_result event_stop_handler(const Smach::event& x);
80 Smach::event_result event_refresh_handler(const Smach::event& x);
81 Smach::event_result event_mouse_click_handler(const Smach::event& x);
83 //constructor destructor
84 StateZoom_Context(CanvasView* canvas_view);
88 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
89 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
90 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
91 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
93 //void zoom(const Point& p1, const Point& p2);
95 }; // END of class StateZoom_Context
97 /* === M E T H O D S ======================================================= */
99 StateZoom::StateZoom():
100 Smach::state<StateZoom_Context>("zoom")
102 insert(event_def(EVENT_STOP,&StateZoom_Context::event_stop_handler));
103 insert(event_def(EVENT_REFRESH,&StateZoom_Context::event_refresh_handler));
104 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateZoom_Context::event_mouse_click_handler));
105 //insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateZoom_Context::event_mouse_click_handler));
106 insert(event_def(EVENT_WORKAREA_BOX,&StateZoom_Context::event_mouse_click_handler));
107 //insert(event_def(EVENT_WORKAREA_BUTTON_CLICK,&StateZoom_Context::event_mouse_click_handler));
108 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateZoom_Context::event_mouse_click_handler));
111 StateZoom::~StateZoom()
115 StateZoom_Context::StateZoom_Context(CanvasView* canvas_view):
116 canvas_view_(canvas_view),
117 is_working(*canvas_view),
118 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks())
120 // Turn off layer clicking
121 get_work_area()->set_allow_layer_clicks(false);
123 // clear out the ducks
124 get_work_area()->clear_ducks(); //???
126 // Refresh the work area
127 get_work_area()->queue_draw();
129 // Hide the tables if they are showing
130 //prev_table_status=get_canvas_view()->tables_are_visible();
131 //if(prev_table_status)get_canvas_view()->hide_tables();
133 // Disable the time bar
134 //get_canvas_view()->set_sensitive_timebar(false);
137 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateZoom_Context::on_user_click));
138 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
140 App::toolbox->refresh();
143 StateZoom_Context::~StateZoom_Context()
145 // Restore layer clicking
146 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
147 get_canvas_view()->work_area->reset_cursor();
149 // Enable the time bar
150 //get_canvas_view()->set_sensitive_timebar(true);
152 // Bring back the tables if they were out before
153 //if(prev_table_status)get_canvas_view()->show_tables();
155 // Refresh the work area
156 get_work_area()->queue_draw();
158 App::toolbox->refresh();
160 get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
164 StateZoom_Context::event_stop_handler(const Smach::event& /*x*/)
166 throw Smach::egress_exception();
170 StateZoom_Context::event_refresh_handler(const Smach::event& /*x*/)
172 return Smach::RESULT_ACCEPT;
176 StateZoom_Context::event_mouse_click_handler(const Smach::event& x)
178 if(x.key==EVENT_WORKAREA_BOX)
180 const EventBox& event(*reinterpret_cast<const EventBox*>(&x));
182 if(event.button==BUTTON_LEFT)
184 //respond to event box...
187 //Center the new position at the center of the box
189 //OH MY GOD HACK - the space is -1* and offset (by the value of the center of the canvas)...
192 const Point evcenter = (event.p1+event.p2)/2;
193 const Point realcenter = (get_work_area()->get_window_tl() + get_work_area()->get_window_br())/2;
194 newpos = -(evcenter - realcenter) + get_work_area()->get_focus_point();
197 //The zoom will be whatever the required factor to convert current box size to desired box size
198 Point tl = get_work_area()->get_window_tl();
199 Point br = get_work_area()->get_window_br();
201 Vector span = br - tl;
202 Vector v = event.p2 - event.p1;
204 //get the minimum zoom as long as it's greater than 1...
205 v[0] = abs(v[0])/abs(span[0]);
206 v[1] = abs(v[1])/abs(span[1]);
208 float zdiv = max(v[0],v[1]);
209 if(zdiv < 1 && zdiv > 0) //must be zoomable
211 get_work_area()->set_focus_point(newpos);
212 get_work_area()->set_zoom(get_work_area()->get_zoom()/zdiv);
215 return Smach::RESULT_ACCEPT;
219 if(x.key==EVENT_WORKAREA_MOUSE_BUTTON_UP)
221 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
223 if(event.button==BUTTON_LEFT)
227 //make the event pos be in the same space...
228 // The weird ass inverted center normalized space...
230 const Point realcenter = (get_work_area()->get_window_tl() + get_work_area()->get_window_br())/2;
231 evpos = -(event.pos - realcenter) + get_work_area()->get_focus_point();
235 focus point must zoom about the point evpos...
237 trans about an origin not 0:
241 Vector v = get_work_area()->get_focus_point() - evpos;
243 if(event.modifier & Gdk::CONTROL_MASK) //zoom out...
246 //get_work_area()->zoom_out();
247 get_work_area()->set_focus_point(evpos + v);
248 get_work_area()->set_zoom(get_work_area()->get_zoom()/ZOOMFACTOR);
252 //get_work_area()->zoom_in();
253 get_work_area()->set_focus_point(evpos + v);
254 get_work_area()->set_zoom(get_work_area()->get_zoom()*ZOOMFACTOR);
257 return Smach::RESULT_ACCEPT;
261 return Smach::RESULT_OK;