Skip to content

Commit

Permalink
fix this lib
Browse files Browse the repository at this point in the history
fix work of gif
add example to Readme.md
add path to lint.yaml
create normal test
  • Loading branch information
Egor00f committed Feb 13, 2025
1 parent bbb3edb commit fa22f35
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 32 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/Lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ name: Lint

on:
push:

paths:
- 'src/**'
- 'tests/**'
pull_request:
branches:
- main
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,37 @@

library for creating gifs

## Example

```
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local giflib = require(ReplicatedStorage.shared.giflib)
local imageLabel = Instance.new("ImageLabel")
imageLabel.Parent = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
imageLabel.Size = UDim2.fromScale(1, 1)
local mygif = giflib.newGif(
imageLabel,
{
giflib.newImage("85510906103514", 0.08),
giflib.newImage("108084812514916", 0.08),
giflib.newImage("110413056991989", 0.08),
giflib.newImage("138519472508007", 0.08),
giflib.newImage("76126708840626", 0.08),
giflib.newImage("105799496264978", 0.08),
giflib.newImage("126677371037972", 0.08),
giflib.newImage("110695448399045", 0.08),
giflib.newImage("88405074787964", 0.08),
giflib.newImage("82234753210135", 0.08),
giflib.newImage("126380322008457", 0.08),
giflib.newImage("73999504428848", 1)
},
true
)
mygif:StartAnimation()
```
150 changes: 129 additions & 21 deletions src/giflib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ local algorithm = require(ReplicatedStorage.Packages.stdlib).algorithm
local giflib = {}

export type GifImage = {
Image: string,

Image: ImageLabel,

--[[
delay on this image
]]
Time: number
}

--[[
Gif struct
]]
export type Gif = {
--[[
Список кадров в гифке
Expand All @@ -19,7 +27,14 @@ export type Gif = {
GifImage
},

ImageLabel: ImageLabel,
--[[
Gif surface
]]
ImageLabel: Frame,

--[[
Current frame
]]
Frame: number,

--[[
Expand All @@ -28,14 +43,46 @@ export type Gif = {
AnimationRunning: boolean,

--[[
Гифка зацикленна
Гифка зацикленна.
if true animation is will be looped.
]]
LoopAnimation: boolean,

--[[
Images already preloaded
]]
IsLoaded: boolean,

--[[
Start Gif animation
]]
StartAnimation: (self: Gif)->any,

--[[
Stop gif animation
]]
StopAnimation: (self: Gif)->any,

--[[
Reset animation.
if animation runnning now, run gif from first image
]]
ResetAnimation: (self: Gif)->any,

--[[
Destroy gif
]]
Destroy: (self: Gif)->any,
--[[
Preload all images
]]
Preload: (self: Gif)->any,

--[[
Add image
]]
AddImage: (self: Gif, image: GifImage)->any,

--[[
Показывает что анимация завершилась
Expand All @@ -44,60 +91,96 @@ export type Gif = {
ComplitedEvent: BindableEvent
}

function _Destroy(self: Gif)
function giflib.Destroy(self: Gif)
self.ComplitedEvent:Destroy()
end

function _Preload(self: Gif)
function giflib.Preload(self: Gif)
ContentProvider:PreloadAsync(algorithm.copy_by_prop(self.Images, "Image"))
self.IsLoaded = true
end

function _StartAnimation(self: Gif)
function giflib.StartAnimation(self: Gif)

if not self.IsLoaded then
self:Preload()
end

self.AnimationRunning = true

task.spawn(function()
self.AnimationRunning = true

while not self.AnimationRunning do
self.ImageLabel.Image = self.Images[self.Frame].Image
while self.AnimationRunning and #self.Images >= self.Frame do
local GifImage = self.Images[self.Frame]

GifImage.Image.Visible = true

if #self.Images == self.Frame then
if #self.Images + 1 <= self.Frame then
break
else
self.Frame += 1
end

task.wait(self.Images[self.Frame].Time)
task.wait(GifImage.Time)
GifImage.Image.Visible = false
end

self.AnimationRunning = false

self.ComplitedEvent:Fire()
end)
end

function _StopAnimation(self: Gif)
function giflib.StopAnimation(self: Gif)
self.AnimationRunning = false
end

function _ResetAnimation(self: Gif)
function giflib.ResetAnimation(self: Gif)
self.Frame = 1
if not self.AnimationRunning then
self:StartAnimation()
end
end

function giflib.AddImage(self: Gif, image: GifImage)
self.IsLoaded = false

self.Images[#self.Images + 1] = image
end

function giflib.newGif(imageLabel: ImageLabel, images: {GifImage}, loopAnimation: boolean?): Gif

--[[
Gif constructor
`Label` - то на чем отображается гифка
`images` - list of `GifImage`s
`loopAnimation` - if true animation is will be looped
]]
function giflib.newGif(Label: Frame, images: {GifImage}, loopAnimation: boolean?): Gif

local _ComplitedEvent = Instance.new("BindableEvent")

local self: Gif = {
ImageLabel = imageLabel,
ImageLabel = Label,
Images = images,
Frame = 1,
AnimationRunning = false,
Complited = _ComplitedEvent.Event,
ComplitedEvent = _ComplitedEvent,
LoopAnimation = loopAnimation or false,
Destroy = _Destroy,
StartAnimation = _StartAnimation,
StopAnimation = _StopAnimation,
ResetAnimation = _ResetAnimation,
IsLoaded = false,
Destroy = giflib.Destroy,
StartAnimation = giflib.StartAnimation,
StopAnimation = giflib.StopAnimation,
ResetAnimation = giflib.ResetAnimation,
Preload = giflib.Preload,
AddImage = giflib.AddImage
}

for _, v in pairs(self.Images) do
v.Image.Parent = Label
end

self.Complited:Connect(function()
if self.LoopAnimation then
self:ResetAnimation()
Expand All @@ -109,5 +192,30 @@ function giflib.newGif(imageLabel: ImageLabel, images: {GifImage}, loopAnimation
return self
end

--[[
Gif image constructor
you can do not use funct fror creating `GifImage`
]]
function giflib.newImage(id: string, t: number): GifImage
local function AddProtocolIfINeeded(): string
if not id:find("http://www.roblox.com/asset/?id=") then
id = "http://www.roblox.com/asset/?id=" .. id
end

return id
end

local img: GifImage = {
Image = Instance.new("ImageLabel"),
Time = t
}

img.Image.Image = AddProtocolIfINeeded()
img.Image.Size = UDim2.fromScale(1, 1)

return img
end


return giflib
16 changes: 14 additions & 2 deletions tests.project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"$className": "DataModel",
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"shared": {
"shared": {
"$path": "src"
},
"Packages": {
Expand All @@ -17,6 +17,18 @@
"$className": "StarterPlayerScripts",
"$path": "tests"
}
},
"StarterGui": {
"$className": "StarterGui",
"ScreenGui": {
"$className": "ScreenGui"
}
},
"Workspace": {
"$className": "Workspace",
"$properties": {
"Gravity": 0
}
}
}
}
}
26 changes: 19 additions & 7 deletions tests/test.client.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local giflib = require(ReplicatedStorage.shared.giflib)

local imageLabel = Instance.new("ImageLabel")

local imgLabel = Instance.new("ImageLabel")
imageLabel.Parent = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
imageLabel.Size = UDim2.fromScale(1, 1)

local mygif = giflib.newGif(
imgLabel,
imageLabel,
{
{Image = "img1", Time = 0.08},
{Image = "img2", Time = 0.08},
{Image = "img3", Time = 0.08}

}
giflib.newImage("85510906103514", 0.08),
giflib.newImage("108084812514916", 0.08),
giflib.newImage("110413056991989", 0.08),
giflib.newImage("138519472508007", 0.08),
giflib.newImage("76126708840626", 0.08),
giflib.newImage("105799496264978", 0.08),
giflib.newImage("126677371037972", 0.08),
giflib.newImage("110695448399045", 0.08),
giflib.newImage("88405074787964", 0.08),
giflib.newImage("82234753210135", 0.08),
giflib.newImage("126380322008457", 0.08),
giflib.newImage("73999504428848", 1)
},
true
)

mygif:StartAnimation()
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "egor00f/giflib"

description = "Library for creating gifs"

version = "0.1.1"
version = "0.1.2"

registry = "https://github.com/UpliftGames/wally-index"

Expand Down

0 comments on commit fa22f35

Please sign in to comment.