1 | /* === S Y N F I G ========================================================= */ |
---|
2 | /*! \file state_zoom.cpp |
---|
3 | ** \brief Zoom Tool Implementation File |
---|
4 | ** |
---|
5 | ** $Id$ |
---|
6 | ** |
---|
7 | ** \legal |
---|
8 | ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley |
---|
9 | ** Copyright (c) 2008 Chris Moore |
---|
10 | ** |
---|
11 | ** This package is free software; you can redistribute it and/or |
---|
12 | ** modify it under the terms of the GNU General Public License as |
---|
13 | ** published by the Free Software Foundation; either version 2 of |
---|
14 | ** the License, or (at your option) any later version. |
---|
15 | ** |
---|
16 | ** This package is distributed in the hope that it will be useful, |
---|
17 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | ** General Public License for more details. |
---|
20 | ** \endlegal |
---|
21 | */ |
---|
22 | /* ========================================================================= */ |
---|
23 | |
---|
24 | /* === H E A D E R S ======================================================= */ |
---|
25 | |
---|
26 | #ifdef USING_PCH |
---|
27 | # include "pch.h" |
---|
28 | #else |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | # include <config.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <sigc++/signal.h> |
---|
34 | #include <sigc++/object.h> |
---|
35 | |
---|
36 | #include <ETL/handle> |
---|
37 | #include <synfig/vector.h> |
---|
38 | |
---|
39 | |
---|
40 | #include "state_zoom.h" |
---|
41 | #include "state_normal.h" |
---|
42 | #include "event_mouse.h" |
---|
43 | #include "canvasview.h" |
---|
44 | #include "workarea.h" |
---|
45 | #include "app.h" |
---|
46 | #include "docks/dock_toolbox.h" |
---|
47 | #include <synfigapp/main.h> |
---|
48 | |
---|
49 | #include "general.h" |
---|
50 | |
---|
51 | #endif |
---|
52 | |
---|
53 | /* === U S I N G =========================================================== */ |
---|
54 | |
---|
55 | using namespace std; |
---|
56 | using namespace etl; |
---|
57 | using namespace synfig; |
---|
58 | using namespace studio; |
---|
59 | |
---|
60 | /* === M A C R O S ========================================================= */ |
---|
61 | |
---|
62 | /* === G L O B A L S ======================================================= */ |
---|
63 | StateZoom studio::state_zoom; |
---|
64 | |
---|
65 | const float ZOOMFACTOR = 1.25f; |
---|
66 | |
---|
67 | /* === C L A S S E S & S T R U C T S ======================================= */ |
---|
68 | |
---|
69 | class studio::StateZoom_Context : public sigc::trackable |
---|
70 | { |
---|
71 | etl::handle<CanvasView> canvas_view_; |
---|
72 | CanvasView::IsWorking is_working; |
---|
73 | |
---|
74 | WorkArea::PushState push_state; |
---|
75 | |
---|
76 | Point p1,p2; |
---|
77 | |
---|
78 | public: |
---|
79 | |
---|
80 | //events |
---|
81 | Smach::event_result event_stop_handler(const Smach::event& x); |
---|
82 | Smach::event_result event_refresh_handler(const Smach::event& x); |
---|
83 | Smach::event_result event_mouse_click_handler(const Smach::event& x); |
---|
84 | |
---|
85 | //constructor destructor |
---|
86 | StateZoom_Context(CanvasView* canvas_view); |
---|
87 | ~StateZoom_Context(); |
---|
88 | |
---|
89 | //Canvas interaction |
---|
90 | const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;} |
---|
91 | etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();} |
---|
92 | synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();} |
---|
93 | WorkArea * get_work_area()const{return canvas_view_->get_work_area();} |
---|
94 | |
---|
95 | //void zoom(const Point& p1, const Point& p2); |
---|
96 | |
---|
97 | }; // END of class StateZoom_Context |
---|
98 | |
---|
99 | /* === M E T H O D S ======================================================= */ |
---|
100 | |
---|
101 | StateZoom::StateZoom(): |
---|
102 | Smach::state<StateZoom_Context>("zoom") |
---|
103 | { |
---|
104 | insert(event_def(EVENT_STOP,&StateZoom_Context::event_stop_handler)); |
---|
105 | insert(event_def(EVENT_REFRESH,&StateZoom_Context::event_refresh_handler)); |
---|
106 | insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateZoom_Context::event_mouse_click_handler)); |
---|
107 | //insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateZoom_Context::event_mouse_click_handler)); |
---|
108 | insert(event_def(EVENT_WORKAREA_BOX,&StateZoom_Context::event_mouse_click_handler)); |
---|
109 | //insert(event_def(EVENT_WORKAREA_BUTTON_CLICK,&StateZoom_Context::event_mouse_click_handler)); |
---|
110 | insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateZoom_Context::event_mouse_click_handler)); |
---|
111 | } |
---|
112 | |
---|
113 | StateZoom::~StateZoom() |
---|
114 | { |
---|
115 | } |
---|
116 | |
---|
117 | StateZoom_Context::StateZoom_Context(CanvasView* canvas_view): |
---|
118 | canvas_view_(canvas_view), |
---|
119 | is_working(*canvas_view), |
---|
120 | push_state(get_work_area()) |
---|
121 | { |
---|
122 | // Turn off layer clicking |
---|
123 | get_work_area()->set_allow_layer_clicks(false); |
---|
124 | |
---|
125 | // Hide all ducks |
---|
126 | get_work_area()->set_type_mask(Duck::TYPE_NONE); |
---|
127 | get_canvas_view()->toggle_duck_mask(Duck::TYPE_NONE); |
---|
128 | |
---|
129 | get_work_area()->set_cursor(Gdk::CROSSHAIR); |
---|
130 | |
---|
131 | App::dock_toolbox->refresh(); |
---|
132 | } |
---|
133 | |
---|
134 | StateZoom_Context::~StateZoom_Context() |
---|
135 | { |
---|
136 | // Refresh the work area |
---|
137 | get_work_area()->queue_draw(); |
---|
138 | |
---|
139 | App::dock_toolbox->refresh(); |
---|
140 | } |
---|
141 | |
---|
142 | Smach::event_result |
---|
143 | StateZoom_Context::event_stop_handler(const Smach::event& /*x*/) |
---|
144 | { |
---|
145 | //throw Smach::egress_exception(); |
---|
146 | throw &state_normal; |
---|
147 | return Smach::RESULT_OK; |
---|
148 | } |
---|
149 | |
---|
150 | Smach::event_result |
---|
151 | StateZoom_Context::event_refresh_handler(const Smach::event& /*x*/) |
---|
152 | { |
---|
153 | return Smach::RESULT_ACCEPT; |
---|
154 | } |
---|
155 | |
---|
156 | Smach::event_result |
---|
157 | StateZoom_Context::event_mouse_click_handler(const Smach::event& x) |
---|
158 | { |
---|
159 | if(x.key==EVENT_WORKAREA_BOX) |
---|
160 | { |
---|
161 | const EventBox& event(*reinterpret_cast<const EventBox*>(&x)); |
---|
162 | |
---|
163 | if(event.button==BUTTON_LEFT) |
---|
164 | { |
---|
165 | Point tl = get_work_area()->get_window_tl(); |
---|
166 | Point br = get_work_area()->get_window_br(); |
---|
167 | Vector window_span = br - tl, window_middle = (br+tl)/2; |
---|
168 | Vector box_span = event.p2 - event.p1, box_middle = (event.p1+event.p2)/2; |
---|
169 | Point newpos; |
---|
170 | float zoom; |
---|
171 | |
---|
172 | if(event.modifier & Gdk::CONTROL_MASK) //zoom out... |
---|
173 | { |
---|
174 | if (window_span[0] == 0 || window_span[1] == 0) zoom = 1; |
---|
175 | else zoom = max(abs(box_span[0]/window_span[0]), abs(box_span[1]/window_span[1])); |
---|
176 | |
---|
177 | // focus_point is -1 times the real position for some reason... |
---|
178 | // center the window so the old contents fill the drawn box |
---|
179 | newpos = -((window_middle - box_middle)/zoom + window_middle); |
---|
180 | } |
---|
181 | else // zoom in |
---|
182 | { |
---|
183 | if (box_span[0] == 0 || box_span[1] == 0) zoom = 1; |
---|
184 | else zoom = min(abs(window_span[0]/box_span[0]), abs(window_span[1]/box_span[1])); |
---|
185 | |
---|
186 | // center the window at the center of the box |
---|
187 | newpos = -(-get_work_area()->get_focus_point() + (box_middle - window_middle)); |
---|
188 | } |
---|
189 | |
---|
190 | get_work_area()->set_focus_point(newpos); |
---|
191 | get_work_area()->set_zoom(get_work_area()->get_zoom()*zoom); |
---|
192 | |
---|
193 | return Smach::RESULT_ACCEPT; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | if(x.key==EVENT_WORKAREA_MOUSE_BUTTON_UP) |
---|
198 | { |
---|
199 | const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x)); |
---|
200 | |
---|
201 | if(event.button==BUTTON_LEFT) |
---|
202 | { |
---|
203 | Point evpos; |
---|
204 | |
---|
205 | //make the event pos be in the same space... |
---|
206 | // The weird ass inverted center normalized space... |
---|
207 | { |
---|
208 | const Point realcenter = (get_work_area()->get_window_tl() + get_work_area()->get_window_br())/2; |
---|
209 | evpos = -(event.pos - realcenter) + get_work_area()->get_focus_point(); |
---|
210 | } |
---|
211 | |
---|
212 | /* Zooming: |
---|
213 | focus point must zoom about the point evpos... |
---|
214 | |
---|
215 | trans about an origin not 0: |
---|
216 | p' = A(p - o) + o |
---|
217 | */ |
---|
218 | |
---|
219 | Vector v = get_work_area()->get_focus_point() - evpos; |
---|
220 | |
---|
221 | if(event.modifier & Gdk::CONTROL_MASK) //zoom out... |
---|
222 | { |
---|
223 | v*=ZOOMFACTOR; |
---|
224 | //get_work_area()->zoom_out(); |
---|
225 | get_work_area()->set_focus_point(evpos + v); |
---|
226 | get_work_area()->set_zoom(get_work_area()->get_zoom()/ZOOMFACTOR); |
---|
227 | }else //zoom in |
---|
228 | { |
---|
229 | v/=ZOOMFACTOR; |
---|
230 | //get_work_area()->zoom_in(); |
---|
231 | get_work_area()->set_focus_point(evpos + v); |
---|
232 | get_work_area()->set_zoom(get_work_area()->get_zoom()*ZOOMFACTOR); |
---|
233 | } |
---|
234 | |
---|
235 | return Smach::RESULT_ACCEPT; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | return Smach::RESULT_OK; |
---|
240 | } |
---|