1 /* === S Y N F I G ========================================================= */
2 /*! \file inputdevice.cpp
3 ** \brief Template 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 "inputdevice.h"
35 #include <ETL/stringf>
42 /* === U S I N G =========================================================== */
46 using namespace synfig;
47 using namespace synfigapp;
49 /* === M A C R O S ========================================================= */
51 /* === G L O B A L S ======================================================= */
53 class DeviceSettings : public Settings
55 InputDevice* input_device;
57 DeviceSettings(InputDevice* input_device):
58 input_device(input_device) { }
61 virtual bool get_value(const synfig::String& key, synfig::String& value)const
65 value=input_device->get_state();
68 if(key=="bline_width")
70 value=strprintf("%s",input_device->get_bline_width().get_string().c_str());
75 value=strprintf("%f",(float)input_device->get_opacity());
78 if(key=="blend_method")
80 value=strprintf("%i",(int)input_device->get_blend_method());
83 if(key=="outline_color")
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());
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());
99 get_mode_value(value);
104 get_axes_value(value);
109 get_keys_value(value);
113 return Settings::get_value(key, value);
116 void get_mode_value(synfig::String & value) const
118 if (input_device->get_mode() == InputDevice::MODE_SCREEN)
120 else if (input_device->get_mode() == InputDevice::MODE_WINDOW)
126 void get_axes_value(synfig::String & value) const
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);
135 void get_keys_value(synfig::String & value) const
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);
144 virtual bool set_value(const synfig::String& key,const synfig::String& value)
148 input_device->set_state(value);
151 if(key=="bline_width")
153 input_device->set_bline_width(synfig::Distance(value));
158 input_device->set_opacity(atof(value.c_str()));
161 if(key=="blend_method")
163 input_device->set_blend_method(Color::BlendMethod(atoi(value.c_str())));
166 if(key=="outline_color")
168 float r=0,g=0,b=0,a=1;
169 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
171 input_device->set_outline_color(synfig::Color(r,g,b,a));
174 if(key=="fill_color")
176 float r=0,g=0,b=0,a=1;
177 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
179 input_device->set_fill_color(synfig::Color(r,g,b,a));
184 set_mode_value(value);
189 set_axes_value(value);
194 set_keys_value(value);
198 return Settings::set_value(key, value);
201 void set_mode_value(const synfig::String & value)
203 InputDevice::Mode mode;
204 if (value == "screen")
205 mode = InputDevice::MODE_SCREEN;
206 else if (value == "window")
207 mode = InputDevice::MODE_WINDOW;
209 mode = InputDevice::MODE_DISABLED;
211 input_device->set_mode(mode);
214 void set_axes_value(const synfig::String & value)
216 std::vector<InputDevice::AxisUse> axes;
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);
223 for (int axis = 0; axis < num_axes; axis++) {
225 pos = value.find(' ', pos + 1);
226 axes[axis] = InputDevice::AxisUse(atoi(value.substr(last, pos).c_str()));
230 input_device->set_axes(axes);
233 void set_keys_value(const synfig::String & value)
235 std::vector<InputDevice::DeviceKey> keys;
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);
242 for (int key = 0; key < num_keys; key++) {
244 pos = value.find(' ', pos + 1);
245 keys[key].keyval = (unsigned int) atol(value.substr(last, pos).c_str());
247 pos = value.find(' ', pos + 1);
248 keys[key].modifiers = (unsigned int) atol(value.substr(last, pos).c_str());
252 input_device->set_keys(keys);
255 virtual KeyList get_key_list()const
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");
271 /* === P R O C E D U R E S ================================================= */
273 /* === M E T H O D S ======================================================= */
275 InputDevice::InputDevice(const synfig::String id_, 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)),
283 blend_method_(Color::BLEND_COMPOSITE),
286 device_settings=new DeviceSettings(this);
287 Main::settings().add_domain(device_settings,"input_device."+id_);
290 InputDevice::~InputDevice()
292 Main::settings().remove_domain("input_device."+id_);
293 delete device_settings;
297 InputDevice::settings()
299 return *device_settings;
303 InputDevice::settings()const
305 return *device_settings;