9e74eadaf3d5ec4a901a76f8a5624ea33bcffb26
[synfig.git] / synfig-core / trunk / 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 **
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 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "xorpattern.h"
35
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44
45 #endif
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_LAYER_INIT(XORPattern);
52 SYNFIG_LAYER_SET_NAME(XORPattern,"xor_pattern");
53 SYNFIG_LAYER_SET_LOCAL_NAME(XORPattern,_("XOR Pattern"));
54 SYNFIG_LAYER_SET_CATEGORY(XORPattern,_("Other"));
55 SYNFIG_LAYER_SET_VERSION(XORPattern,"0.1");
56 SYNFIG_LAYER_SET_CVS_ID(XORPattern,"$Id$");
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 XORPattern::XORPattern():
63         Layer_Composite (1.0,Color::BLEND_STRAIGHT),
64         pos(0.125,0.125),
65         size(0.25,0.25)
66 {
67 }
68
69 bool
70 XORPattern::set_param(const String & param, const ValueBase &value)
71 {
72         IMPORT(pos);
73         IMPORT(size);
74
75         return Layer_Composite::set_param(param,value);
76 }
77
78 ValueBase
79 XORPattern::get_param(const String & param)const
80 {
81         EXPORT(pos);
82         EXPORT(size);
83
84         EXPORT_NAME();
85         EXPORT_VERSION();
86
87         return Layer_Composite::get_param(param);
88 }
89
90 Color
91 XORPattern::get_color(Context context, const Point &point)const
92 {
93         if(get_amount()==0.0)
94                 return context.get_color(point);
95
96         unsigned int a=(unsigned int)floor((point[0]-pos[0])/size[0]), b=(unsigned int)floor((point[1]-pos[1])/size[1]);
97         unsigned char rindex=(a^b);
98         unsigned char gindex=(a^(~b))*4;
99         unsigned char bindex=~(a^b)*2;
100
101         Color color((Color::value_type)rindex/(Color::value_type)255.0,
102                                 (Color::value_type)gindex/(Color::value_type)255.0,
103                                 (Color::value_type)bindex/(Color::value_type)255.0,
104                                 1.0);
105
106         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
107                 return color;
108         else
109                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
110
111 }
112
113 Layer::Vocab
114 XORPattern::get_param_vocab()const
115 {
116         Layer::Vocab ret(Layer_Composite::get_param_vocab());
117
118         ret.push_back(ParamDesc("pos")
119                 .set_local_name(_("Offset"))
120         );
121         ret.push_back(ParamDesc("size")
122                 .set_local_name(_("Size"))
123                 .set_origin("pos")
124         );
125
126         return ret;
127 }
128
129 synfig::Layer::Handle
130 XORPattern::hit_check(synfig::Context context, const synfig::Point &getpos)const
131 {
132         // if we have a zero amount
133         if(get_amount()==0.0)
134                 // then the click passes down to our context
135                 return context.hit_check(getpos);
136
137         synfig::Layer::Handle tmp;
138         // if we are behind the context, and the click hits something in the context
139         if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(getpos)))
140                 // then return the thing it hit in the context
141                 return tmp;
142
143         // if we're using an 'onto' blend method and the click missed the context
144         if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(getpos)))
145                 // then it misses everything
146                 return 0;
147
148         // otherwise the click hit us, since we're the size of the whole plane
149         return const_cast<XORPattern*>(this);
150 }