|
48 | 48 | -- ##################################################################### |
49 | 49 | -- [[ Tween Configuration Tables ]] |
50 | 50 | -- ##################################################################### |
51 | | - |
52 | 51 | local validTweenTypes = {"Alpha", "Angle", "X", "Y", "Color", "Zoom", "Scale"} |
53 | 52 | local tweenAltNames = { |
54 | 53 | Rotate = "Angle", |
|
69 | 68 | -- ##################################################################### |
70 | 69 | -- [[ Custom Tween Implementation: Scale ]] |
71 | 70 | -- ##################################################################### |
72 | | - |
73 | 71 | local activeTweens = {} |
74 | 72 | local tweenUpdateRate = 1 / 60 -- 60 FPS update rate |
75 | 73 |
|
76 | | --- Custom implementation of scale tweening using runTimer |
77 | | -local function doTweenScale(tag, object, toValue, duration, easeType) |
78 | | - -- Get current scale values |
79 | | - local startScaleX = getProperty(object .. ".scale.x") |
80 | | - local startScaleY = getProperty(object .. ".scale.y") |
81 | | - |
82 | | - if startScaleX == nil or startScaleY == nil then |
83 | | - debugPrint('Tween Error: Object "' .. object .. '" does not exist or has no scale property') |
84 | | - return false |
85 | | - end |
86 | | - |
87 | | - -- Store tween data |
88 | | - activeTweens[tag] = { |
89 | | - object = object, |
90 | | - startScaleX = startScaleX, |
91 | | - startScaleY = startScaleY, |
92 | | - targetScale = toValue, |
93 | | - duration = duration, |
94 | | - easeType = easeType, |
95 | | - elapsed = 0, |
96 | | - isActive = true |
97 | | - } |
98 | | - |
99 | | - -- Start the update timer |
100 | | - runTimer("tween_scale_" .. tag, tweenUpdateRate, 0) |
101 | | - return true |
102 | | -end |
103 | | - |
104 | | --- Update function for scale tweens |
105 | | -local function updateScaleTween(tag, elapsed) |
106 | | - local tween = activeTweens[tag] |
107 | | - if not tween or not tween.isActive then |
108 | | - return false |
109 | | - end |
110 | | - |
111 | | - tween.elapsed = tween.elapsed + elapsed |
112 | | - local progress = math.min(tween.elapsed / tween.duration, 1) |
113 | | - local easedProgress = applyEasing(progress, tween.easeType) |
114 | | - local currentScale = tween.startScaleX + (tween.targetScale - tween.startScaleX) * easedProgress |
115 | | - |
116 | | - setProperty(tween.object .. ".scale.x", currentScale) |
117 | | - setProperty(tween.object .. ".scale.y", currentScale) |
118 | | - |
119 | | - if progress >= 1 then |
120 | | - tween.isActive = false |
121 | | - cancelTimer("tween_scale_" .. tag) |
122 | | - |
123 | | - if onTweenCompleted then |
124 | | - onTweenCompleted(tag) |
125 | | - end |
126 | | - |
127 | | - return false |
128 | | - end |
129 | | - |
130 | | - return true |
131 | | -end |
132 | | - |
133 | 74 | -- Custom implementation of easing functions for the custom scale tween |
134 | 75 | -- Mimics easing functions found in FlxEase |
135 | 76 | local easingFunctions = {} |
@@ -281,11 +222,66 @@ local function applyEasing(t, easeType) |
281 | 222 | return easingFunc and easingFunc(t) or t |
282 | 223 | end |
283 | 224 |
|
| 225 | +-- Custom implementation of scale tweening using runTimer |
| 226 | +local function doTweenScale(tag, object, toValue, duration, easeType) |
| 227 | + -- Get current scale values |
| 228 | + local startScaleX = getProperty(object .. ".scale.x") |
| 229 | + local startScaleY = getProperty(object .. ".scale.y") |
| 230 | + |
| 231 | + if startScaleX == nil or startScaleY == nil then |
| 232 | + debugPrint('Tween Error: Object "' .. object .. '" does not exist or has no scale property') |
| 233 | + return false |
| 234 | + end |
| 235 | + |
| 236 | + -- Store tween data |
| 237 | + activeTweens[tag] = { |
| 238 | + object = object, |
| 239 | + startScaleX = startScaleX, |
| 240 | + startScaleY = startScaleY, |
| 241 | + targetScale = toValue, |
| 242 | + duration = duration, |
| 243 | + easeType = easeType, |
| 244 | + elapsed = 0, |
| 245 | + isActive = true |
| 246 | + } |
| 247 | + |
| 248 | + -- Start the update timer |
| 249 | + runTimer("tween_scale_" .. tag, tweenUpdateRate, 0) |
| 250 | + return true |
| 251 | +end |
| 252 | + |
| 253 | +-- Update function for scale tweens |
| 254 | +local function updateScaleTween(tag, elapsed) |
| 255 | + local tween = activeTweens[tag] |
| 256 | + if not tween or not tween.isActive then |
| 257 | + return false |
| 258 | + end |
| 259 | + |
| 260 | + tween.elapsed = tween.elapsed + elapsed |
| 261 | + local progress = math.min(tween.elapsed / tween.duration, 1) |
| 262 | + local easedProgress = applyEasing(progress, tween.easeType) |
| 263 | + local currentScale = tween.startScaleX + (tween.targetScale - tween.startScaleX) * easedProgress |
| 264 | + |
| 265 | + setProperty(tween.object .. ".scale.x", currentScale) |
| 266 | + setProperty(tween.object .. ".scale.y", currentScale) |
| 267 | + |
| 268 | + if progress >= 1 then |
| 269 | + tween.isActive = false |
| 270 | + cancelTimer("tween_scale_" .. tag) |
| 271 | + |
| 272 | + if onTweenCompleted then |
| 273 | + onTweenCompleted(tag) |
| 274 | + end |
| 275 | + |
| 276 | + return false |
| 277 | + end |
| 278 | + |
| 279 | + return true |
| 280 | +end |
284 | 281 |
|
285 | 282 | -- ##################################################################### |
286 | 283 | -- [[ Event Functions ]] |
287 | 284 | -- ##################################################################### |
288 | | - |
289 | 285 | -- Parses the tween type, tag, and object from the input string. |
290 | 286 | local function parseTweenNames(value) |
291 | 287 | local cleanValue = value:gsub(" ", "") |
|
355 | 351 | -- ##################################################################### |
356 | 352 | -- [[ Bind our local functions to Psych Engine events ]] |
357 | 353 | -- ##################################################################### |
358 | | - |
359 | 354 | function onEvent(name, value1, value2) |
360 | 355 | if name == "Tween" then |
361 | 356 | local tweenType, tag, object = parseTweenNames(value1) |
|
0 commit comments