1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/* -------------------------------------------------------
Copyright (c) 2011 Alberto G. Salguero (alberto.salguero (at) uca.es)
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions of
the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------- */
#include "ICSInputControlSystem.h"
#define B1(t) (t*t)
#define B2(t) (2*t*(1-t))
#define B3(t) ((1-t)*(1-t))
namespace ICS
{
Channel::Channel(int number, float initialValue
, float bezierMidPointY, float bezierMidPointX, float symmetricAt, float bezierStep)
: mNumber(number)
, mValue(initialValue)
, mSymmetricAt(symmetricAt)
, mBezierStep(bezierStep)
, mEnabled(true)
{
mBezierMidPoint.x = bezierMidPointX;
mBezierMidPoint.y = bezierMidPointY;
setBezierFunction(bezierMidPointY, bezierMidPointX, symmetricAt, bezierStep);
}
void Channel::setEnabled(bool enabled)
{
mEnabled = enabled;
}
float Channel::getValue()
{
if(mValue == 0 || mValue == 1)
{
return mValue;
}
assert (!mBezierFunction.empty());
BezierFunction::iterator it = mBezierFunction.begin();
//size_t size_minus_1 = mBezierFunction.size() - 1;
BezierFunction::iterator last = mBezierFunction.end();
--last;
for ( ; it != last ; )
{
BezierPoint left = (*it);
BezierPoint right = (*(++it));
if( (left.x <= mValue) && (right.x > mValue) )
{
float val = left.y - (left.x - mValue) * (left.y - right.y) / (left.x - right.x);
return std::max<float>(0.0,std::min<float>(1.0, val));
}
}
return -1;
}
void Channel::setValue(float value)
{
float previousValue = this->getValue();
mValue = value;
if(previousValue != value && mEnabled)
{
notifyListeners(previousValue);
}
}
void Channel::notifyListeners(float previousValue)
{
std::list<ChannelListener*>::iterator pos = mListeners.begin();
while (pos != mListeners.end())
{
(*pos)->channelChanged((Channel*)this, this->getValue(), previousValue);
++pos;
}
}
void Channel::addControl(Control* control, Channel::ChannelDirection dir, float percentage)
{
ControlChannelBinderItem ccBinderItem;
ccBinderItem.control = control;
ccBinderItem.direction = dir;
ccBinderItem.percentage = percentage;
mAttachedControls.push_back(ccBinderItem);
}
Channel::ControlChannelBinderItem Channel::getAttachedControlBinding(Control* control)
{
for(std::vector<ControlChannelBinderItem>::iterator it = mAttachedControls.begin() ;
it != mAttachedControls.end() ; it++)
{
if((*it).control == control)
{
return (*it);
}
}
ControlChannelBinderItem nullBinderItem;
nullBinderItem.control = NULL;
nullBinderItem.direction = Channel/*::ChannelDirection*/::DIRECT;
nullBinderItem.percentage = 0;
return nullBinderItem;
}
void Channel::update()
{
if(this->getControlsCount() == 1)
{
ControlChannelBinderItem ccBinderItem = mAttachedControls.back();
float diff = ccBinderItem.control->getValue() - ccBinderItem.control->getInitialValue();
if(ccBinderItem.direction == ICS::Channel::DIRECT)
{
this->setValue(ccBinderItem.control->getInitialValue() + (ccBinderItem.percentage * diff));
}
else
{
this->setValue(ccBinderItem.control->getInitialValue() - (ccBinderItem.percentage * diff));
}
}
else
{
float val = 0;
std::vector<ControlChannelBinderItem>::const_iterator it;
for(it=mAttachedControls.begin(); it!=mAttachedControls.end(); ++it)
{
ControlChannelBinderItem ccBinderItem = (*it);
float diff = ccBinderItem.control->getValue() - ccBinderItem.control->getInitialValue();
if(ccBinderItem.direction == ICS::Channel::DIRECT)
{
val += (ccBinderItem.percentage * diff);
}
else
{
val -= (ccBinderItem.percentage * diff);
}
}
if(mAttachedControls.size() > 0)
{
this->setValue(mAttachedControls.begin()->control->getInitialValue() + val);
}
}
}
void Channel::setBezierFunction(float bezierMidPointY, float bezierMidPointX, float symmetricAt, float bezierStep)
{
mBezierMidPoint.x = bezierMidPointX;
mBezierMidPoint.y = bezierMidPointY;
mBezierStep = bezierStep;
mSymmetricAt = symmetricAt;
mBezierFunction.clear();
BezierPoint start;
start.x = 0;
start.y = 0;
BezierPoint end;
end.x = 1;
end.y = 1;
mBezierFunction.push_front(end);
FilterInterval interval;
interval.startX = start.x;
interval.startY = start.y;
interval.midX = mBezierMidPoint.x;
interval.midY = mBezierMidPoint.y;
interval.endX = end.x;
interval.endY = end.y;
interval.step = bezierStep;
mIntervals.push_back(interval);
if(!(mBezierMidPoint.x == 0.5 && mBezierMidPoint.y == 0.5))
{
float t = mBezierStep;
while(t < 1)
{
BezierPoint p;
p.x = start.x * B1(t) + mBezierMidPoint.x * B2(t) + end.x * B3(t);
p.y = start.y * B1(t) + mBezierMidPoint.y * B2(t) + end.y * B3(t);
mBezierFunction.push_front(p);
t += mBezierStep;
}
}
mBezierFunction.push_front(start);
}
void Channel::addBezierInterval(float startX, float startY, float midX, float midY
, float endX, float endY, float step)
{
FilterInterval interval;
interval.startX = startX;
interval.startY = startY;
interval.midX = midX;
interval.midY = midY;
interval.endX = endX;
interval.endY = endY;
interval.step = step;
mIntervals.push_back(interval);
float t = 0;
while(t <= 1)
{
BezierPoint p;
p.x = startX * B1(t) + midX * B2(t) + endX * B3(t);
p.y = startY * B1(t) + midY * B2(t) + endY * B3(t);
BezierFunction::iterator it = mBezierFunction.begin();
while( it != mBezierFunction.end() )
{
BezierPoint left = (*it);
BezierPoint right;
++it;
if( it != mBezierFunction.end() )
{
right = (*it);
}
else
{
right.x = endX;
right.y = endY;
}
if(p.x > left.x && p.x < right.x)
{
mBezierFunction.insert(it, p);
break;
}
}
t += 1.0f / ((endX-startX)/step);
}
}
}
|