Add parameter static option for rest of layers.
[synfig.git] / synfig-core / src / modules / lyr_std / xorpattern.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file xorpattern.cpp
3 **      \brief Implementation of the "XOR Pattern" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007-2008 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,N_("XOR Pattern"));
55 SYNFIG_LAYER_SET_CATEGORY(XORPattern,N_("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_COMPOSITE),
65         origin(0.125,0.125),
66         size(0.25,0.25)
67 {
68         Layer::Vocab voc(get_param_vocab());
69         Layer::fill_static(voc);
70 }
71
72 bool
73 XORPattern::set_param(const String & param, const ValueBase &value)
74 {
75         IMPORT(origin);
76         IMPORT(size);
77
78         IMPORT_AS(origin,"pos");
79
80         return Layer_Composite::set_param(param,value);
81 }
82
83 ValueBase
84 XORPattern::get_param(const String & param)const
85 {
86         EXPORT(origin);
87         EXPORT(size);
88
89         EXPORT_NAME();
90         EXPORT_VERSION();
91
92         return Layer_Composite::get_param(param);
93 }
94
95 Color
96 XORPattern::get_color(Context context, const Point &point)const
97 {
98         if(get_amount()==0.0)
99                 return context.get_color(point);
100
101         unsigned int a=(unsigned int)floor((point[0]-origin[0])/size[0]), b=(unsigned int)floor((point[1]-origin[1])/size[1]);
102         unsigned char rindex=(a^b);
103         unsigned char gindex=(a^(~b))*4;
104         unsigned char bindex=~(a^b)*2;
105
106         Color color((Color::value_type)rindex/(Color::value_type)255.0,
107                                 (Color::value_type)gindex/(Color::value_type)255.0,
108                                 (Color::value_type)bindex/(Color::value_type)255.0,
109                                 1.0);
110
111         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
112                 return color;
113         else
114                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
115
116 }
117
118 Layer::Vocab
119 XORPattern::get_param_vocab()const
120 {
121         Layer::Vocab ret(Layer_Composite::get_param_vocab());
122
123         ret.push_back(ParamDesc("origin")
124                 .set_local_name(_("Origin"))
125         );
126         ret.push_back(ParamDesc("size")
127                 .set_local_name(_("Size"))
128                 .set_origin("origin")
129         );
130
131         return ret;
132 }
133
134 synfig::Layer::Handle
135 XORPattern::hit_check(synfig::Context context, const synfig::Point &getpos)const
136 {
137         // if we have a zero amount
138         if(get_amount()==0.0)
139                 // then the click passes down to our context
140                 return context.hit_check(getpos);
141
142         synfig::Layer::Handle tmp;
143         // if we are behind the context, and the click hits something in the context
144         if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(getpos)))
145                 // then return the thing it hit in the context
146                 return tmp;
147
148         // if we're using an 'onto' blend method and the click missed the context
149         if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(getpos)))
150                 // then it misses everything
151                 return 0;
152
153         // otherwise the click hit us, since we're the size of the whole plane
154         return const_cast<XORPattern*>(this);
155 }