Fixed double initialitation of the field state_ in InputDevice.
[synfig.git] / synfig-studio / trunk / 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=="color")
84                 {
85                         Color c(input_device->get_foreground_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=="bgcolor")
91                 {
92                         Color c(input_device->get_background_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
98                 return Settings::get_value(key, value);
99         }
100
101         virtual bool set_value(const synfig::String& key,const synfig::String& value)
102         {
103                 if(key=="state")
104                 {
105                         input_device->set_state(value);
106                         return true;
107                 }
108                 if(key=="bline_width")
109                 {
110                         input_device->set_bline_width(synfig::Distance(value));
111                         return true;
112                 }
113                 if(key=="opacity")
114                 {
115                         input_device->set_opacity(atof(value.c_str()));
116                         return true;
117                 }
118                 if(key=="blend_method")
119                 {
120                         input_device->set_blend_method(Color::BlendMethod(atoi(value.c_str())));
121                         return true;
122                 }
123                 if(key=="color")
124                 {
125                         float r=0,g=0,b=0,a=1;
126                         if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
127                                 return false;
128                         input_device->set_foreground_color(synfig::Color(r,g,b,a));
129                         return true;
130                 }
131                 if(key=="bgcolor")
132                 {
133                         float r=0,g=0,b=0,a=1;
134                         if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
135                                 return false;
136                         input_device->set_background_color(synfig::Color(r,g,b,a));
137                         return true;
138                 }
139
140                 return Settings::set_value(key, value);
141         }
142
143         virtual KeyList get_key_list()const
144         {
145                 KeyList ret(Settings::get_key_list());
146                 ret.push_back("color");
147                 ret.push_back("bgcolor");
148                 ret.push_back("state");
149                 ret.push_back("bline_width");
150                 ret.push_back("blend_method");
151                 ret.push_back("opacity");
152                 return ret;
153         }
154 };
155
156 /* === P R O C E D U R E S ================================================= */
157
158 /* === M E T H O D S ======================================================= */
159
160 InputDevice::InputDevice(const synfig::String id_, Type type_):
161         id_(id_),
162         type_(type_),
163         state_((type_==TYPE_PEN)?"draw":"normal"),
164         foreground_color_(Color::black()),
165         background_color_(Color::white()),
166         bline_width_(Distance(1,Distance::SYSTEM_POINTS)),
167         opacity_(1.0f),
168         blend_method_(Color::BLEND_COMPOSITE)
169 {
170         device_settings=new DeviceSettings(this);
171         Main::settings().add_domain(device_settings,"input_device."+id_);
172 }
173
174 InputDevice::~InputDevice()
175 {
176         Main::settings().remove_domain("input_device."+id_);
177         delete device_settings;
178 }
179
180 Settings&
181 InputDevice::settings()
182 {
183         return *device_settings;
184 }
185
186 const Settings&
187 InputDevice::settings()const
188 {
189         return *device_settings;
190 }