@@ -5107,129 +5107,65 @@ int Game_Interpreter::ManiacBitmask(int value, int mask) const {
5107
5107
5108
5108
return value;
5109
5109
}
5110
- std::vector<double > parseBezier (const std::string& bezierParams) {
5111
- std::vector<double > params;
5112
- std::string temp;
5113
- size_t startPos = bezierParams.find (" (" ) + 1 ;
5114
- size_t endPos = bezierParams.find (" )" );
5115
- std::string valuesString = bezierParams.substr (startPos, endPos - startPos);
5116
5110
5117
- size_t commaPos = valuesString.find (" ," );
5118
- while (commaPos != std::string::npos) {
5119
- temp = valuesString.substr (0 , commaPos);
5120
- params.push_back (std::stod (temp));
5121
- valuesString = valuesString.substr (commaPos + 1 );
5122
- commaPos = valuesString.find (" ," );
5123
- }
5124
- // Push the last value into the vector
5125
- params.push_back (std::stod (valuesString));
5126
-
5127
- return params;
5128
- }
5129
-
5130
- // FIXME: cubicBezier is completely Broken
5131
- // references to how it should work:
5111
+ // references for cubic bezier:
5132
5112
// https://matthewlein.com/tools/ceaser
5133
5113
// https://cubic-bezier.com/
5114
+ double cubicBezier (float t, const double & p0,const double & p1, const double & p2, const double & p3) {
5134
5115
5135
- double cubicBezier (double t, double p0, double p1, double p2, double p3) {
5136
- double u = 1 - t;
5137
- double tt = t * t;
5138
- double uu = u * u;
5139
- double uuu = uu * u;
5140
- double ttt = tt * t;
5116
+ float u = 1 - t;
5117
+ float tt = t * t;
5118
+ float uu = u * u;
5119
+ float uuu = uu * u;
5120
+ float ttt = tt * t;
5141
5121
5142
- double p = uuu * p0; // (1-t)^3
5143
- double q = 3 * uu * t * p1; // 3t(1-t)^2
5144
- double r = 3 * u * tt * p2; // 3(1-t)t^2
5145
- double s = ttt * p3; // t^3
5122
+ // Point2d p = {0,0};
5123
+ // p.x = uuu * 0 + 3 * uu * t * p0 + 3 * u * tt * p2 + ttt * 1;
5124
+ return uuu * 0 + 3 * uu * t * p1 + 3 * u * tt * p3 + ttt * 1 ;
5146
5125
5147
- return p + q + r + s ;
5126
+ // return p.y ;
5148
5127
}
5149
5128
5150
- double getEasedT (const std::string& easingType, double t, double b, double c, double d) {
5151
- if (easingType == " linear" ) {
5152
- return c * t / d + b;
5153
- }
5154
- else if (easingType == " quadIn" ) {
5155
- t /= d;
5156
- return c * t * t + b;
5157
- }
5158
- else if (easingType == " quadOut" ) {
5159
- t /= d;
5160
- return -c * t * (t - 2 ) + b;
5161
- }
5162
- else if (easingType == " quadInOut" ) {
5163
- t /= d / 2 ;
5164
- if (t < 1 ) {
5165
- return c / 2 * t * t + b;
5166
- }
5167
- else {
5168
- t -= 1 ;
5169
- return -c / 2 * (t * (t - 2 ) - 1 ) + b;
5170
- }
5171
- }
5172
- else if (easingType == " cubicIn" ) {
5173
- t /= d;
5174
- return c * t * t * t + b;
5175
- }
5176
- else if (easingType == " cubicOut" ) {
5177
- t = (t / d) - 1 ;
5178
- return c * (t * t * t + 1 ) + b;
5179
- }
5180
- else if (easingType == " cubicInOut" ) {
5181
- t /= d / 2 ;
5182
- if (t < 1 ) {
5183
- return c / 2 * t * t * t + b;
5184
- }
5185
- else {
5186
- t -= 2 ;
5187
- return c / 2 * (t * t * t + 2 ) + b;
5188
- }
5189
- }
5190
- else if (easingType == " sinIn" ) {
5191
- return -c * cos (t / d * (M_PI / 2 )) + c + b;
5192
- }
5193
- else if (easingType == " sinOut" ) {
5194
- return c * sin (t / d * (M_PI / 2 )) + b;
5195
- }
5196
- else if (easingType == " sinInOut" ) {
5197
- return -c / 2 * (cos (M_PI * t / d) - 1 ) + b;
5198
- }
5199
- else if (easingType == " expoIn" ) {
5200
- return c * pow (2 , 10 * (t / d - 1 )) + b;
5201
- }
5202
- else if (easingType == " expoOut" ) {
5203
- return c * (-pow (2 , -10 * t / d) + 1 ) + b;
5204
- }
5205
- else if (easingType == " expoInOut" ) {
5206
- t /= d / 2 ;
5207
- if (t < 1 ) {
5208
- return c / 2 * pow (2 , 10 * (t - 1 )) + b;
5209
- }
5210
- else {
5211
- t -= 1 ;
5212
- return c / 2 * (-pow (2 , -10 * t) + 2 ) + b;
5213
- }
5214
- }
5215
- else if (easingType == " circIn" ) {
5216
- t /= d;
5217
- return -c * (sqrt (1 - t * t) - 1 ) + b;
5218
- }
5219
- else if (easingType == " circOut" ) {
5220
- t = (t / d) - 1 ;
5221
- return c * sqrt (1 - t * t) + b;
5222
- }
5223
- else if (easingType == " circInOut" ) {
5224
- t /= d / 2 ;
5225
- if (t < 1 ) {
5226
- return -c / 2 * (sqrt (1 - t * t) - 1 ) + b;
5227
- }
5228
- else {
5229
- t -= 2 ;
5230
- return c / 2 * (sqrt (1 - t * t) + 1 ) + b;
5231
- }
5232
- }
5129
+ double getEasedTime (const std::string& easingType, double t, double b, double c, double d) {
5130
+ if (easingType == " linear" ) return cubicBezier (t, 0.250 , 0.250 , 0.750 , 0.750 );
5131
+
5132
+ else if (easingType == " ease" ) return cubicBezier (t, 0.250 , 0.100 , 0.250 , 1.000 );
5133
+ else if (easingType == " easeIn" ) return cubicBezier (t, 0.420 , 0.000 , 1.000 , 1.000 );
5134
+ else if (easingType == " easeOut" ) return cubicBezier (t, 0.000 , 0.000 , 0.580 , 1.000 );
5135
+ else if (easingType == " easeInOut" ) return cubicBezier (t, 0.420 , 0.000 , 0.580 , 1.000 );
5136
+
5137
+ else if (easingType == " quadIn" ) return cubicBezier (t, 0.550 , 0.085 , 0.680 , 0.530 );
5138
+ else if (easingType == " quadOut" ) return cubicBezier (t, 0.250 , 0.460 , 0.450 , 0.940 );
5139
+ else if (easingType == " quadInOut" ) return cubicBezier (t, 0.455 , 0.030 , 0.515 , 0.955 );
5140
+
5141
+ else if (easingType == " cubicIn" ) return cubicBezier (t, 0.550 , 0.055 , 0.675 , 0.190 );
5142
+ else if (easingType == " cubicOut" ) return cubicBezier (t, 0.215 , 0.610 , 0.355 , 1.000 );
5143
+ else if (easingType == " cubicInOut" ) return cubicBezier (t, 0.645 , 0.045 , 0.355 , 1.000 );
5144
+
5145
+ else if (easingType == " quartIn" ) return cubicBezier (t, 0.895 , 0.030 , 0.685 , 0.220 );
5146
+ else if (easingType == " quartOut" ) return cubicBezier (t, 0.165 , 0.840 , 0.440 , 1.000 );
5147
+ else if (easingType == " quartInOut" ) return cubicBezier (t, 0.770 , 0.000 , 0.175 , 1.000 );
5148
+
5149
+ else if (easingType == " quintIn" ) return cubicBezier (t, 0.755 , 0.050 , 0.855 , 0.060 );
5150
+ else if (easingType == " quintOut" ) return cubicBezier (t, 0.230 , 1.000 , 0.320 , 1.000 );
5151
+ else if (easingType == " quintInOut" ) return cubicBezier (t, 0.860 , 0.000 , 0.070 , 1.000 );
5152
+
5153
+ else if (easingType == " sineIn" ) return cubicBezier (t, 0.470 , 0.000 , 0.745 , 0.715 );
5154
+ else if (easingType == " sineOut" ) return cubicBezier (t, 0.390 , 0.575 , 0.565 , 1.000 );
5155
+ else if (easingType == " sineInOut" ) return cubicBezier (t, 0.445 , 0.050 , 0.550 , 0.950 );
5156
+
5157
+ else if (easingType == " ExpoIn" ) return cubicBezier (t, 0.950 , 0.050 , 0.795 , 0.035 );
5158
+ else if (easingType == " expoOut" ) return cubicBezier (t, 0.190 , 1.000 , 0.220 , 1.000 );
5159
+ else if (easingType == " expoInOut" ) return cubicBezier (t, 1.000 , 0.000 , 0.000 , 1.000 );
5160
+
5161
+ else if (easingType == " circIn" ) return cubicBezier (t, 0.600 , 0.040 , 0.980 , 0.335 );
5162
+ else if (easingType == " circOut" ) return cubicBezier (t, 0.075 , 0.820 , 0.165 , 1.000 );
5163
+ else if (easingType == " circInOut" ) return cubicBezier (t, 0.785 , 0.135 , 0.150 , 0.860 );
5164
+
5165
+ else if (easingType == " backIn" ) return cubicBezier (t, 0.600 , -0.280 , 0.735 , 0.045 );
5166
+ else if (easingType == " backOut" ) return cubicBezier (t, 0.175 , 0.885 , 0.320 , 1.275 );
5167
+ else if (easingType == " backInOut" ) return cubicBezier (t, 0.680 , -0.550 , 0.265 , 1.550 );
5168
+
5233
5169
else if (easingType == " elasticIn" ) {
5234
5170
if (t == 0 ) {
5235
5171
return b;
@@ -5279,8 +5215,9 @@ double getEasedT(const std::string& easingType, double t, double b, double c, do
5279
5215
double postFix = a * pow (2 , -10 * (t -= 1 )); // this is a fix, again, with post-increment operators
5280
5216
return postFix * sin ((t * d - s) * (2 * M_PI) / p) * 0.5 + c + b;
5281
5217
}
5218
+
5282
5219
else if (easingType == " bounceIn" ) {
5283
- return c - getEasedT (" bounceOut" , d - t, 0 , c, d) + b;
5220
+ return c - getEasedTime (" bounceOut" , d - t, 0 , c, d) + b;
5284
5221
}
5285
5222
else if (easingType == " bounceOut" ) {
5286
5223
if ((t /= d) < (1 / 2.75 )) {
@@ -5301,65 +5238,30 @@ double getEasedT(const std::string& easingType, double t, double b, double c, do
5301
5238
}
5302
5239
else if (easingType == " bounceInOut" ) {
5303
5240
if (t < d / 2 ) {
5304
- return getEasedT (" bounceIn" , t * 2 , 0 , c, d) * 0.5 + b;
5241
+ return getEasedTime (" bounceIn" , t * 2 , 0 , c, d) * 0.5 + b;
5305
5242
}
5306
5243
else {
5307
- return getEasedT (" bounceOut" , t * 2 - d, 0 , c, d) * 0.5 + c * 0.5 + b;
5308
- }
5309
- }
5310
- if (easingType.substr (0 , 6 ) == " bezier" ) {
5311
- std::vector < double > bezierParams = parseBezier (easingType.substr (7 ));
5312
- if (bezierParams.size () == 4 ) {
5313
- return cubicBezier (t / d, bezierParams[0 ], bezierParams[1 ], bezierParams[2 ], bezierParams[3 ]);
5244
+ return getEasedTime (" bounceOut" , t * 2 - d, 0 , c, d) * 0.5 + c * 0.5 + b;
5314
5245
}
5315
5246
}
5316
5247
5317
- return c * t / d + b; // Default to linear easing if the easing type is not recognized
5318
- }
5319
-
5320
- std::vector<double > interpolate (double start, double end, double duration, const std::string& easingTypeAtStart, const std::string& easingTypeAtEnd) {
5321
- std::vector<double > interpolatedValues;
5322
- interpolatedValues.push_back (start);
5248
+ if (easingType.substr (0 , 6 ) == " bezier" ) {
5249
+ std::vector<double > bezierParams;
5323
5250
5324
- // Calculate the number of steps based on the duration
5325
- int numSteps = static_cast < int >(duration); // Convert duration to an integer
5326
- double stepSize = 1.0 / numSteps ;
5251
+ size_t startPos = easingType. find ( " ( " ) + 1 ;
5252
+ size_t endPos = easingType. find ( " ) " );
5253
+ std::string valuesString = easingType. substr (startPos, endPos - startPos) ;
5327
5254
5328
- // Calculate the halfway point
5329
- double halfway = start + (end - start) * 0.5 ;
5255
+ std::istringstream iss (valuesString);
5256
+ double value ;
5330
5257
5331
- if (easingTypeAtEnd == " null" ) {
5332
- // Use easingTypeAtStart for the entire animation
5333
- for (int step = 1 ; step <= numSteps; ++step) {
5334
- double t = step * stepSize;
5335
- double easedT = getEasedT (easingTypeAtStart, t, 0 , 1 , 1 ); // Call getEasedT with appropriate parameters
5336
- double interpolatedValue = start + easedT * (end - start);
5337
- interpolatedValues.push_back (interpolatedValue);
5338
- }
5339
- }
5340
- else {
5341
- // Generate the first half of the interpolation
5342
- for (int step = 1 ; step <= numSteps / 2 ; ++step) {
5343
- double t = step * stepSize;
5344
- double normalizedT = t / 0.5 ; // Normalize the time for the first half
5345
- double easedT = getEasedT (easingTypeAtStart, normalizedT, 0 , 1 , 1 ); // Call getEasedT with appropriate parameters
5346
- double interpolatedValue = start + easedT * (halfway - start);
5347
- interpolatedValues.push_back (interpolatedValue);
5348
- }
5258
+ while (iss >> value) bezierParams.push_back (value), iss.ignore ();
5349
5259
5350
- // Generate the second half of the interpolation
5351
- for (int step = numSteps / 2 + 1 ; step <= numSteps; ++step) {
5352
- double t = step * stepSize;
5353
- double normalizedT = (t - 0.5 ) / 0.5 ; // Normalize the time for the second half
5354
- double easedT = getEasedT (easingTypeAtEnd, normalizedT, 0 , 1 , 1 ); // Call getEasedT with appropriate parameters
5355
- double interpolatedValue = halfway + easedT * (end - halfway);
5356
- interpolatedValues.push_back (interpolatedValue);
5357
- }
5260
+ if (bezierParams.size () == 4 )
5261
+ return cubicBezier (t, bezierParams[0 ], bezierParams[1 ], bezierParams[2 ], bezierParams[3 ]);
5358
5262
}
5359
5263
5360
- interpolatedValues.push_back (end);
5361
-
5362
- return interpolatedValues;
5264
+ return c * t / d + b; // Default to linear easing if the easing type is not recognized
5363
5265
}
5364
5266
5365
5267
bool Game_Interpreter::CommandAnimateVariable (lcf::rpg::EventCommand const & com) {
@@ -5370,42 +5272,67 @@ bool Game_Interpreter::CommandAnimateVariable(lcf::rpg::EventCommand const& com)
5370
5272
int32_t end = ValueOrVariable (com.parameters [4 ], com.parameters [5 ]);
5371
5273
int32_t duration = ValueOrVariable (com.parameters [6 ], com.parameters [7 ]);
5372
5274
5275
+ // Extract easing information
5276
+ std::string easingTypeAtStart = ToString (com.string );
5277
+ std::string easingTypeAtEnd = " null" ;
5278
+
5279
+ std::size_t pos = easingTypeAtStart.find (' /' );
5280
+
5281
+ if (pos != std::string::npos) {
5282
+ easingTypeAtEnd = easingTypeAtStart.substr (pos + 1 );
5283
+ easingTypeAtStart = easingTypeAtStart.substr (0 , pos);
5284
+ }
5285
+
5373
5286
// Prepare animation-related commands
5374
5287
lcf::rpg::EventCommand waitCom;
5375
5288
waitCom.code = int (Cmd::Wait);
5376
5289
5377
5290
lcf::rpg::EventCommand animatedCom;
5378
5291
animatedCom.code = int (Cmd::ControlVars);
5379
5292
std::vector<int32_t > animatedVarParams = { 0 , static_cast <int32_t >(target), 0 , 0 , 0 , static_cast <int32_t >(end) };
5380
- animatedCom.parameters = lcf::DBArray<int32_t >(animatedVarParams.begin (), animatedVarParams.end ());
5381
5293
5382
5294
std::vector<lcf::rpg::EventCommand> cmdList;
5383
5295
5384
- // Extract easing information
5385
- std::string easeStart = ToString (com.string );
5386
- std::string easeEnd = " null" ;
5296
+ int numSteps = static_cast <int >(duration);
5297
+ double stepSize = 1.0 / numSteps;
5387
5298
5388
- std::size_t pos = easeStart.find (' /' );
5299
+ for (int step = 1 ; step <= numSteps; ++step) {
5300
+ double normalizedTime;
5301
+ double currentTime = step * stepSize;
5302
+ double halfway;
5389
5303
5390
- if (pos != std::string::npos) {
5391
- easeEnd = easeStart.substr (pos + 1 );
5392
- easeStart = easeStart.substr (0 , pos);
5393
- }
5304
+ std::string easingType;
5305
+
5306
+ if (easingTypeAtEnd == " null" ) { // use a single interpolation.
5307
+ normalizedTime = currentTime;
5308
+ easingType = easingTypeAtStart;
5309
+ halfway = (step <= numSteps / 2 ) ? end : start;
5310
+ }
5311
+ else {
5312
+ if (step <= numSteps / 2 ) { // use 2 interpolations: start and end.
5313
+ normalizedTime = currentTime / 0.5 ;
5314
+ easingType = easingTypeAtStart;
5315
+ }
5316
+ else {
5317
+ normalizedTime = (currentTime - 0.5 ) / 0.5 ;
5318
+ easingType = easingTypeAtEnd;
5319
+ }
5320
+ halfway = start + 0.5 * (end - start);
5321
+ }
5322
+
5323
+ double easedTime = getEasedTime (easingType, normalizedTime, 0 , 1 , 1 );
5394
5324
5395
- // Insert animation commands
5396
- std::vector<double > interpolatedValues = interpolate (start, end, duration, easeStart, easeEnd);
5325
+ double startValue = (step <= numSteps / 2 ) ? start : halfway;
5326
+ double endValue = (step <= numSteps / 2 ) ? halfway : end;
5327
+ double interpolatedValue = startValue + easedTime * (endValue - startValue);
5397
5328
5398
- // Insert animatedCom and waitCom commands for each interpolated value
5399
- for (int value : interpolatedValues) {
5400
- animatedVarParams.back () = value;
5329
+ animatedVarParams.back () = interpolatedValue;
5401
5330
animatedCom.parameters = lcf::DBArray<int32_t >(animatedVarParams.begin (), animatedVarParams.end ());
5402
- animatedCom.indent = com.indent + 1 ;
5403
5331
5404
5332
cmdList.push_back (animatedCom);
5405
5333
cmdList.push_back (waitCom);
5406
5334
}
5407
5335
5408
- // Update current_command index and return true to indicate success
5409
5336
Push (cmdList, 0 , false );
5410
5337
return true ;
5411
5338
}
0 commit comments