my log
[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: inputdevice.cpp,v 1.2 2005/01/12 04:08:32 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "inputdevice.h"
32 #include "settings.h"
33 #include <cstdio>
34 #include <ETL/stringf>
35 #include "main.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace synfigapp;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 class DeviceSettings : public Settings
51 {
52         InputDevice* input_device;
53 public:
54         DeviceSettings(InputDevice* input_device):
55                 input_device(input_device) { }
56                 
57
58         virtual bool get_value(const synfig::String& key, synfig::String& value)const
59         {
60                 if(key=="state")
61                 {
62                         value=input_device->get_state();
63                         return true;
64                 }
65                 if(key=="bline_width")
66                 {
67                         value=strprintf("%s",input_device->get_bline_width().get_string().c_str());
68                         return true;
69                 }
70                 if(key=="opacity")
71                 {
72                         value=strprintf("%f",(float)input_device->get_opacity());
73                         return true;
74                 }
75                 if(key=="blend_method")
76                 {
77                         value=strprintf("%i",(int)input_device->get_blend_method());
78                         return true;
79                 }
80                 if(key=="color")
81                 {
82                         Color c(input_device->get_foreground_color());
83                         value=strprintf("%f %f %f %f",(float)c.get_r(),(float)c.get_g(),(float)c.get_b(),(float)c.get_a());
84
85                         return true;
86                 }
87                 
88                 return Settings::get_value(key, value);
89         }
90         
91         virtual bool set_value(const synfig::String& key,const synfig::String& value)
92         {
93                         DEBUGPOINT();
94                 if(key=="state")
95                 {
96                         input_device->set_state(value);
97                         return true;
98                 }
99                 if(key=="bline_width")
100                 {
101                         input_device->set_bline_width(synfig::Distance(value));
102                         return true;
103                 }
104                 if(key=="opacity")
105                 {
106                         input_device->set_opacity(atof(value.c_str()));
107                         return true;
108                 }
109                 if(key=="blend_method")
110                 {
111                         input_device->set_blend_method(Color::BlendMethod(atoi(value.c_str())));
112                         return true;
113                 }
114                 if(key=="color")
115                 {
116                         float r=0,g=0,b=0,a=1;
117                         if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
118                                 return false;
119                         input_device->set_foreground_color(synfig::Color(r,g,b,a));
120                         return true;
121                 }
122                 
123                 return Settings::set_value(key, value);
124         }
125         
126         virtual KeyList get_key_list()const
127         {
128                 KeyList ret(Settings::get_key_list());
129                 ret.push_back("color");
130                 ret.push_back("state");
131                 ret.push_back("bline_width");
132                 ret.push_back("blend_method");
133                 ret.push_back("opacity");
134                 return ret;
135         }
136 };
137
138 /* === P R O C E D U R E S ================================================= */
139
140 /* === M E T H O D S ======================================================= */
141
142 InputDevice::InputDevice(const synfig::String id_, Type type_):
143         id_(id_),
144         type_(type_),
145         state_((type_==TYPE_PEN)?"sketch":"normal"),
146         foreground_color_(Color::black()),
147         background_color_(Color::white()),
148         bline_width_(Distance(1,Distance::SYSTEM_POINTS)),
149         opacity_(1.0f),
150         blend_method_(Color::BLEND_COMPOSITE)
151 {
152         switch(type_)
153         {
154                 case TYPE_MOUSE:
155                         state_="normal";
156                         break;
157
158                 case TYPE_PEN:
159                         state_="draw";
160                         break;
161
162                 case TYPE_ERASER:
163                         state_="normal";
164                         break;
165
166                 case TYPE_CURSOR:
167                         state_="normal";
168                         break;
169
170                 default:
171                         state_="normal";
172                         break;
173         }
174         
175         device_settings=new DeviceSettings(this);
176         Main::settings().add_domain(device_settings,"input_device."+id_);
177 }
178
179 InputDevice::~InputDevice()
180 {
181         Main::settings().remove_domain("input_device."+id_);    
182         delete device_settings;
183 }
184
185 Settings&
186 InputDevice::settings()
187 {
188         return *device_settings;
189 }
190
191 const Settings&
192 InputDevice::settings()const
193 {
194         return *device_settings;
195 }