Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_03 / synfig-studio / src / synfigapp / inputdevice.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file inputdevice.cpp
3 **      \brief Template File
4 **
5 **      $Id: inputdevice.cpp,v 1.2 2005/01/12 04:08:32 darco Exp $
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 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace synfigapp;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 class DeviceSettings : public Settings
52 {
53         InputDevice* input_device;
54 public:
55         DeviceSettings(InputDevice* input_device):
56                 input_device(input_device) { }
57                 
58
59         virtual bool get_value(const synfig::String& key, synfig::String& value)const
60         {
61                 if(key=="state")
62                 {
63                         value=input_device->get_state();
64                         return true;
65                 }
66                 if(key=="bline_width")
67                 {
68                         value=strprintf("%s",input_device->get_bline_width().get_string().c_str());
69                         return true;
70                 }
71                 if(key=="opacity")
72                 {
73                         value=strprintf("%f",(float)input_device->get_opacity());
74                         return true;
75                 }
76                 if(key=="blend_method")
77                 {
78                         value=strprintf("%i",(int)input_device->get_blend_method());
79                         return true;
80                 }
81                 if(key=="color")
82                 {
83                         Color c(input_device->get_foreground_color());
84                         value=strprintf("%f %f %f %f",(float)c.get_r(),(float)c.get_g(),(float)c.get_b(),(float)c.get_a());
85
86                         return true;
87                 }
88                 
89                 return Settings::get_value(key, value);
90         }
91         
92         virtual bool set_value(const synfig::String& key,const synfig::String& value)
93         {
94                         DEBUGPOINT();
95                 if(key=="state")
96                 {
97                         input_device->set_state(value);
98                         return true;
99                 }
100                 if(key=="bline_width")
101                 {
102                         input_device->set_bline_width(synfig::Distance(value));
103                         return true;
104                 }
105                 if(key=="opacity")
106                 {
107                         input_device->set_opacity(atof(value.c_str()));
108                         return true;
109                 }
110                 if(key=="blend_method")
111                 {
112                         input_device->set_blend_method(Color::BlendMethod(atoi(value.c_str())));
113                         return true;
114                 }
115                 if(key=="color")
116                 {
117                         float r=0,g=0,b=0,a=1;
118                         if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
119                                 return false;
120                         input_device->set_foreground_color(synfig::Color(r,g,b,a));
121                         return true;
122                 }
123                 
124                 return Settings::set_value(key, value);
125         }
126         
127         virtual KeyList get_key_list()const
128         {
129                 KeyList ret(Settings::get_key_list());
130                 ret.push_back("color");
131                 ret.push_back("state");
132                 ret.push_back("bline_width");
133                 ret.push_back("blend_method");
134                 ret.push_back("opacity");
135                 return ret;
136         }
137 };
138
139 /* === P R O C E D U R E S ================================================= */
140
141 /* === M E T H O D S ======================================================= */
142
143 InputDevice::InputDevice(const synfig::String id_, Type type_):
144         id_(id_),
145         type_(type_),
146         state_((type_==TYPE_PEN)?"sketch":"normal"),
147         foreground_color_(Color::black()),
148         background_color_(Color::white()),
149         bline_width_(Distance(1,Distance::SYSTEM_POINTS)),
150         opacity_(1.0f),
151         blend_method_(Color::BLEND_COMPOSITE)
152 {
153         switch(type_)
154         {
155                 case TYPE_MOUSE:
156                         state_="normal";
157                         break;
158
159                 case TYPE_PEN:
160                         state_="draw";
161                         break;
162
163                 case TYPE_ERASER:
164                         state_="normal";
165                         break;
166
167                 case TYPE_CURSOR:
168                         state_="normal";
169                         break;
170
171                 default:
172                         state_="normal";
173                         break;
174         }
175         
176         device_settings=new DeviceSettings(this);
177         Main::settings().add_domain(device_settings,"input_device."+id_);
178 }
179
180 InputDevice::~InputDevice()
181 {
182         Main::settings().remove_domain("input_device."+id_);    
183         delete device_settings;
184 }
185
186 Settings&
187 InputDevice::settings()
188 {
189         return *device_settings;
190 }
191
192 const Settings&
193 InputDevice::settings()const
194 {
195         return *device_settings;
196 }