Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc2 / src / modules / lyr_std / xorpattern.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file xorpattern.cpp
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "xorpattern.h"
36
37 #include <synfig/string.h>
38 #include <synfig/time.h>
39 #include <synfig/context.h>
40 #include <synfig/paramdesc.h>
41 #include <synfig/renddesc.h>
42 #include <synfig/surface.h>
43 #include <synfig/value.h>
44 #include <synfig/valuenode.h>
45
46 #endif
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 SYNFIG_LAYER_INIT(XORPattern);
53 SYNFIG_LAYER_SET_NAME(XORPattern,"xor_pattern");
54 SYNFIG_LAYER_SET_LOCAL_NAME(XORPattern,_("XOR Pattern"));
55 SYNFIG_LAYER_SET_CATEGORY(XORPattern,_("Other"));
56 SYNFIG_LAYER_SET_VERSION(XORPattern,"0.1");
57 SYNFIG_LAYER_SET_CVS_ID(XORPattern,"$Id$");
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 XORPattern::XORPattern():
64         Layer_Composite (1.0,Color::BLEND_STRAIGHT),
65         pos(0.125,0.125),
66         size(0.25,0.25)
67 {
68 }
69
70 bool
71 XORPattern::set_param(const String & param, const ValueBase &value)
72 {
73         IMPORT(pos);
74         IMPORT(size);
75
76         return Layer_Composite::set_param(param,value);
77 }
78
79 ValueBase
80 XORPattern::get_param(const String & param)const
81 {
82         EXPORT(pos);
83         EXPORT(size);
84
85         EXPORT_NAME();
86         EXPORT_VERSION();
87
88         return Layer_Composite::get_param(param);
89 }
90
91 Color
92 XORPattern::get_color(Context context, const Point &point)const
93 {
94         if(get_amount()==0.0)
95                 return context.get_color(point);
96
97         unsigned int a=(unsigned int)floor((point[0]-pos[0])/size[0]), b=(unsigned int)floor((point[1]-pos[1])/size[1]);
98         unsigned char rindex=(a^b);
99         unsigned char gindex=(a^(~b))*4;
100         unsigned char bindex=~(a^b)*2;
101
102         Color color((Color::value_type)rindex/(Color::value_type)255.0,
103                                 (Color::value_type)gindex/(Color::value_type)255.0,
104                                 (Color::value_type)bindex/(Color::value_type)255.0,
105                                 1.0);
106
107         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
108                 return color;
109         else
110                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
111
112 }
113
114 Layer::Vocab
115 XORPattern::get_param_vocab()const
116 {
117         Layer::Vocab ret(Layer_Composite::get_param_vocab());
118
119         ret.push_back(ParamDesc("pos")
120                 .set_local_name(_("Offset"))
121         );
122         ret.push_back(ParamDesc("size")
123                 .set_local_name(_("Size"))
124                 .set_origin("pos")
125         );
126
127         return ret;
128 }
129
130 synfig::Layer::Handle
131 XORPattern::hit_check(synfig::Context context, const synfig::Point &getpos)const
132 {
133         // if we have a zero amount
134         if(get_amount()==0.0)
135                 // then the click passes down to our context
136                 return context.hit_check(getpos);
137
138         synfig::Layer::Handle tmp;
139         // if we are behind the context, and the click hits something in the context
140         if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(getpos)))
141                 // then return the thing it hit in the context
142                 return tmp;
143
144         // if we're using an 'onto' blend method and the click missed the context
145         if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(getpos)))
146                 // then it misses everything
147                 return 0;
148
149         // otherwise the click hit us, since we're the size of the whole plane
150         return const_cast<XORPattern*>(this);
151 }