@quenty/timedtween 1.0.1-canary.402.a911cda.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## 1.0.1-canary.402.a911cda.0 (2023-08-23)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add timed tween package ([122659c](https://github.com/Quenty/NevermoreEngine/commit/122659c7a9faa24a6282b7063199d519f08e9860))
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2014-2023 James Onnen (Quenty)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ ## TimedTween
2
+
3
+ <div align="center">
4
+ <a href="http://quenty.github.io/NevermoreEngine/">
5
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
6
+ </a>
7
+ <a href="https://discord.gg/mhtGUS8">
8
+ <img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
9
+ </a>
10
+ <a href="https://github.com/Quenty/NevermoreEngine/actions">
11
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
12
+ </a>
13
+ </div>
14
+
15
+ Linear timed tweening model
16
+
17
+ <div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/TimedTweenUtils">View docs →</a></div>
18
+
19
+ ## Installation
20
+
21
+ ```
22
+ npm install @quenty/timedtween --save
23
+ ```
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "timedtween",
3
+ "tree": {
4
+ "$path": "src"
5
+ }
6
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@quenty/timedtween",
3
+ "version": "1.0.1-canary.402.a911cda.0",
4
+ "description": "Linear timed tweening model",
5
+ "keywords": [
6
+ "Roblox",
7
+ "Nevermore",
8
+ "Lua",
9
+ "timedtween"
10
+ ],
11
+ "bugs": {
12
+ "url": "https://github.com/Quenty/NevermoreEngine/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/Quenty/NevermoreEngine.git",
17
+ "directory": "src/timedtween/"
18
+ },
19
+ "funding": {
20
+ "type": "patreon",
21
+ "url": "https://www.patreon.com/quenty"
22
+ },
23
+ "license": "MIT",
24
+ "contributors": [
25
+ "Quenty"
26
+ ],
27
+ "dependencies": {
28
+ "@quenty/basicpane": "7.18.1-canary.402.a911cda.0",
29
+ "@quenty/blend": "6.26.1-canary.402.a911cda.0",
30
+ "@quenty/loader": "6.2.2-canary.402.a911cda.0",
31
+ "@quenty/math": "2.4.1-canary.402.a911cda.0",
32
+ "@quenty/promise": "6.7.1-canary.402.a911cda.0",
33
+ "@quenty/promisemaid": "1.2.1-canary.402.a911cda.0",
34
+ "@quenty/rx": "7.14.1-canary.402.a911cda.0",
35
+ "@quenty/steputils": "3.2.0",
36
+ "@quenty/valueobject": "7.21.1-canary.402.a911cda.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "gitHead": "a911cdaf4f1039b599528cec17b027f4660e4fd8"
42
+ }
@@ -0,0 +1,158 @@
1
+ --[=[
2
+ @class TimedTween
3
+ ]=]
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local RunService = game:GetService("RunService")
8
+
9
+ local BasicPane = require("BasicPane")
10
+ local ValueObject = require("ValueObject")
11
+ local Math = require("Math")
12
+ local StepUtils = require("StepUtils")
13
+ local Observable = require("Observable")
14
+ local Maid = require("Maid")
15
+ local Promise = require("Promise")
16
+
17
+ local TimedTween = setmetatable({}, BasicPane)
18
+ TimedTween.ClassName = "TimedTween"
19
+ TimedTween.__index = TimedTween
20
+
21
+ function TimedTween.new(transitionTime)
22
+ local self = setmetatable(BasicPane.new(), TimedTween)
23
+
24
+ self._transitionTime = self._maid:Add(ValueObject.new(0.15, "number"))
25
+ self._state = self._maid:Add(ValueObject.new({
26
+ p0 = 0;
27
+ p1 = 0;
28
+ t0 = 0;
29
+ t1 = 0;
30
+ }))
31
+
32
+ if transitionTime then
33
+ self:SetTransitionTime(transitionTime)
34
+ end
35
+
36
+ self._maid:GiveTask(self._transitionTime.Changed:Connect(function()
37
+ self:_updateState()
38
+ end))
39
+
40
+ self._maid:GiveTask(self.VisibleChanged:Connect(function()
41
+ self:_updateState()
42
+ end))
43
+ self:_updateState()
44
+
45
+ return self
46
+ end
47
+
48
+ function TimedTween:SetTransitionTime(transitionTime)
49
+ return self._transitionTime:Mount(transitionTime)
50
+ end
51
+
52
+ function TimedTween:ObserveRenderStepped()
53
+ return self:ObserveOnSignal(RunService.RenderStepped)
54
+ end
55
+
56
+ function TimedTween:ObserveOnSignal(signal)
57
+ return Observable.new(function(sub)
58
+ local maid = Maid.new()
59
+
60
+ local startAnimate, stopAnimate = StepUtils.bindToSignal(signal, function()
61
+ local state = self:_computeState(os.clock())
62
+ sub:Fire(state.p)
63
+ return state.rtime > 0
64
+ end)
65
+
66
+ maid:GiveTask(stopAnimate)
67
+ maid:GiveTask(self._state.Changed:Connect(startAnimate))
68
+ startAnimate()
69
+
70
+ return maid
71
+ end)
72
+ end
73
+
74
+ function TimedTween:Observe()
75
+ return self:ObserveOnSignal(RunService.RenderStepped)
76
+ end
77
+
78
+ function TimedTween:PromiseFinished()
79
+ local initState = self:_computeState(os.clock())
80
+ if initState.rtime <= 0 then
81
+ return Promise.resolved()
82
+ end
83
+
84
+ local maid = Maid.new()
85
+ local promise = Promise.new()
86
+ maid:GiveTask(promise)
87
+
88
+ maid:GiveTask(self._state:Observe():Subscribe(function()
89
+ local state = self:_computeState(os.clock())
90
+ if state.rtime <= 0 then
91
+ promise:Resolve()
92
+ return
93
+ end
94
+
95
+ maid._scheduled = task.delay(state.rtime, function()
96
+ promise:Resolve()
97
+ end)
98
+ end))
99
+
100
+ self._maid[promise] = maid
101
+
102
+ promise:Finally(function()
103
+ self._maid[promise] = nil
104
+ end)
105
+
106
+ maid:GiveTask(function()
107
+ self._maid[promise] = nil
108
+ end)
109
+ return promise
110
+ end
111
+
112
+ function TimedTween:_updateState()
113
+ local transitionTime = self._transitionTime.Value
114
+ local target = self:IsVisible() and 1 or 0;
115
+
116
+ local now = os.clock()
117
+ local computed = self:_computeState(now)
118
+ local p0 = computed.p
119
+
120
+ local remainingDist = target - p0
121
+
122
+ self._state.Value = {
123
+ p0 = p0;
124
+ p1 = target;
125
+ t0 = now;
126
+ t1 = now + Math.map(math.abs(remainingDist), 0, 1, 0, transitionTime);
127
+ }
128
+ end
129
+
130
+ function TimedTween:_computeState(now)
131
+ local state = self._state.Value
132
+ local p
133
+
134
+ local duration = math.max(0, state.t1 - state.t0)
135
+ if duration == 0 then
136
+ p = state.p1
137
+ else
138
+ p = Math.map(math.clamp(now, state.t0, state.t1), state.t0, state.t1, state.p0, state.p1)
139
+ end
140
+
141
+ local rtime = math.abs(state.p1 - p)*duration
142
+
143
+ local v
144
+ if rtime > 0 and duration > 0 then
145
+ v = (state.p1 - state.p0)/duration
146
+ else
147
+ v = 0
148
+ end
149
+
150
+ return {
151
+ p = p;
152
+ v = v;
153
+ rtime = rtime;
154
+ }
155
+ end
156
+
157
+
158
+ return TimedTween
@@ -0,0 +1,34 @@
1
+ --[[
2
+ @class TimedTween.story
3
+ ]]
4
+
5
+ local require = require(game:GetService("ServerScriptService"):FindFirstChild("LoaderUtils", true).Parent).load(script)
6
+
7
+ local Maid = require("Maid")
8
+ local TimedTween = require("TimedTween")
9
+ local Blend = require("Blend")
10
+
11
+ return function(target)
12
+ local maid = Maid.new()
13
+
14
+ local timedTween = TimedTween.new(0.3)
15
+ maid:GiveTask(timedTween)
16
+
17
+
18
+ maid:GiveTask(Blend.mount(target, {
19
+ Blend.New "TextButton" {
20
+ Size = UDim2.fromScale(1, 1);
21
+ BackgroundTransparency = Blend.Computed(timedTween:Observe(), function(position)
22
+ return 1 - position
23
+ end);
24
+
25
+ [Blend.OnEvent "Activated"] = function()
26
+ timedTween:Toggle()
27
+ end;
28
+ };
29
+ }));
30
+
31
+ return function()
32
+ maid:DoCleaning()
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "TimedTweenTest",
3
+ "tree": {
4
+ "$className": "DataModel",
5
+ "ServerScriptService": {
6
+ "timedtween": {
7
+ "$path": ".."
8
+ }
9
+ }
10
+ }
11
+ }