Set the initial default blend method to "By Layer Default"
[synfig.git] / synfig-studio / src / synfigapp / inputdevice.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file inputdevice.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "inputdevice.h"
33 #include "settings.h"
34 #include <cstdio>
35 #include <ETL/stringf>
36 #include "main.h"
37
38 #include "general.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace synfigapp;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 class DeviceSettings : public Settings
54 {
55         InputDevice* input_device;
56 public:
57         DeviceSettings(InputDevice* input_device):
58                 input_device(input_device) { }
59
60
61         virtual bool get_value(const synfig::String& key, synfig::String& value)const
62         {
63                 if(key=="state")
64                 {
65                         value=input_device->get_state();
66                         return true;
67                 }
68                 if(key=="bline_width")
69                 {
70                         value=strprintf("%s",input_device->get_bline_width().get_string().c_str());
71                         return true;
72                 }
73                 if(key=="opacity")
74                 {
75                         value=strprintf("%f",(float)input_device->get_opacity());
76                         return true;
77                 }
78                 if(key=="blend_method")
79                 {
80                         value=strprintf("%i",(int)input_device->get_blend_method());
81                         return true;
82                 }
83                 if(key=="outline_color")
84                 {
85                         Color c(input_device->get_outline_color());
86                         value=strprintf("%f %f %f %f",(float)c.get_r(),(float)c.get_g(),(float)c.get_b(),(float)c.get_a());
87
88                         return true;
89                 }
90                 if(key=="fill_color")
91                 {
92                         Color c(input_device->get_fill_color());
93                         value=strprintf("%f %f %f %f",(float)c.get_r(),(float)c.get_g(),(float)c.get_b(),(float)c.get_a());
94
95                         return true;
96                 }
97                 if(key=="mode")
98                 {
99                         get_mode_value(value);
100                         return true;
101                 }
102                 if(key=="axes")
103                 {
104                         get_axes_value(value);
105                         return true;
106                 }
107                 if(key=="keys")
108                 {
109                         get_keys_value(value);
110                         return true;
111                 }
112
113                 return Settings::get_value(key, value);
114         }
115
116         void get_mode_value(synfig::String & value) const
117         {
118                 if (input_device->get_mode() == InputDevice::MODE_SCREEN)
119                         value = "screen";
120                 else if (input_device->get_mode() == InputDevice::MODE_WINDOW)
121                         value = "window";
122                 else
123                         value = "disabled";
124         }
125
126         void get_axes_value(synfig::String & value) const
127         {
128                 vector<InputDevice::AxisUse> axes = input_device->get_axes();
129                 value = strprintf("%u", axes.size());
130                 vector<InputDevice::AxisUse>::const_iterator itr;
131                 for (itr = axes.begin(); itr != axes.end(); itr++)
132                         value += strprintf(" %u", (unsigned int) *itr);
133         }
134
135         void get_keys_value(synfig::String & value) const
136         {
137                 vector<InputDevice::DeviceKey> keys = input_device->get_keys();
138                 value = strprintf("%u", keys.size());
139                 vector<InputDevice::DeviceKey>::const_iterator itr;
140                 for (itr = keys.begin(); itr != keys.end(); itr++)
141                         value += strprintf(" %u %u", itr->keyval, itr->modifiers);
142         }
143
144         virtual bool set_value(const synfig::String& key,const synfig::String& value)
145         {
146                 if(key=="state")
147                 {
148                         input_device->set_state(value);
149                         return true;
150                 }
151                 if(key=="bline_width")
152                 {
153                         input_device->set_bline_width(synfig::Distance(value));
154                         return true;
155                 }
156                 if(key=="opacity")
157                 {
158                         input_device->set_opacity(atof(value.c_str()));
159                         return true;
160                 }
161                 if(key=="blend_method")
162                 {
163                         input_device->set_blend_method(Color::BlendMethod(atoi(value.c_str())));
164                         return true;
165                 }
166                 if(key=="outline_color")
167                 {
168                         float r=0,g=0,b=0,a=1;
169                         if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
170                                 return false;
171                         input_device->set_outline_color(synfig::Color(r,g,b,a));
172                         return true;
173                 }
174                 if(key=="fill_color")
175                 {
176                         float r=0,g=0,b=0,a=1;
177                         if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
178                                 return false;
179                         input_device->set_fill_color(synfig::Color(r,g,b,a));
180                         return true;
181                 }
182                 if(key=="mode")
183                 {
184                         set_mode_value(value);
185                         return true;
186                 }
187                 if(key=="axes")
188                 {
189                         set_axes_value(value);
190                         return true;
191                 }
192                 if(key=="keys")
193                 {
194                         set_keys_value(value);
195                         return true;
196                 }
197
198                 return Settings::set_value(key, value);
199         }
200
201         void set_mode_value(const synfig::String & value)
202         {
203                 InputDevice::Mode mode;
204                 if (value == "screen")
205                         mode = InputDevice::MODE_SCREEN;
206                 else if (value == "window")
207                         mode = InputDevice::MODE_WINDOW;
208                 else
209                         mode = InputDevice::MODE_DISABLED;
210
211                 input_device->set_mode(mode);
212         }
213
214         void set_axes_value(const synfig::String & value)
215         {
216                 std::vector<InputDevice::AxisUse> axes;
217
218                 unsigned pos = value.find(' ', 0);
219                 if (pos < value.size()) {
220                         int num_axes = atoi(value.substr(0, pos).c_str());
221                         axes.resize(num_axes);
222
223                         for (int axis = 0; axis < num_axes; axis++) {
224                                 int last = pos;
225                                 pos = value.find(' ', pos + 1);
226                                 axes[axis] = InputDevice::AxisUse(atoi(value.substr(last, pos).c_str()));
227                         }
228                 }
229
230                 input_device->set_axes(axes);
231         }
232
233         void set_keys_value(const synfig::String & value)
234         {
235                 std::vector<InputDevice::DeviceKey> keys;
236
237                 unsigned pos = value.find(' ', 0);
238                 if (pos < value.size()) {
239                         int num_keys = atoi(value.substr(0, pos).c_str());
240                         keys.resize(num_keys);
241
242                         for (int key = 0; key < num_keys; key++) {
243                                 int last = pos;
244                                 pos = value.find(' ', pos + 1);
245                                 keys[key].keyval = (unsigned int) atol(value.substr(last, pos).c_str());
246                                 last = pos;
247                                 pos = value.find(' ', pos + 1);
248                                 keys[key].modifiers = (unsigned int) atol(value.substr(last, pos).c_str());
249                         }
250                 }
251
252                 input_device->set_keys(keys);
253         }
254
255         virtual KeyList get_key_list()const
256         {
257                 KeyList ret(Settings::get_key_list());
258                 ret.push_back("outline_color");
259                 ret.push_back("fill_color");
260                 ret.push_back("state");
261                 ret.push_back("bline_width");
262                 ret.push_back("blend_method");
263                 ret.push_back("opacity");
264                 ret.push_back("mode");
265                 ret.push_back("axes");
266                 ret.push_back("keys");
267                 return ret;
268         }
269 };
270
271 /* === P R O C E D U R E S ================================================= */
272
273 /* === M E T H O D S ======================================================= */
274
275 InputDevice::InputDevice(const synfig::String id_, Type type_):
276         id_(id_),
277         type_(type_),
278         state_((type_==TYPE_PEN)?"draw":"normal"),
279         outline_color_(Color::black()),
280         fill_color_(Color::white()),
281         bline_width_(Distance(1,Distance::SYSTEM_POINTS)),
282         opacity_(1.0f),
283         blend_method_(Color::BLEND_BY_LAYER),
284         mode_(MODE_DISABLED)
285 {
286         device_settings=new DeviceSettings(this);
287         Main::settings().add_domain(device_settings,"input_device."+id_);
288 }
289
290 InputDevice::~InputDevice()
291 {
292         Main::settings().remove_domain("input_device."+id_);
293         delete device_settings;
294 }
295
296 Settings&
297 InputDevice::settings()
298 {
299         return *device_settings;
300 }
301
302 const Settings&
303 InputDevice::settings()const
304 {
305         return *device_settings;
306 }