Advertisement
TaylorsRus

Client (Fusion example)

Apr 3rd, 2024
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.84 KB | Gaming | 0 0
  1. --// I always use GetService as it's the most reliable way to fetch a service.
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local InputService = game:GetService("UserInputService")
  4. local Lighting = game:GetService("Lighting")
  5. local Players = game:GetService("Players")
  6.  
  7. --// Get the player objects early in the initialization progress as it is important.
  8. local Player = Players.LocalPlayer
  9. local Camera = workspace.CurrentCamera
  10. local PlayerGui = Player:WaitForChild("PlayerGui")
  11.  
  12. --// Get all folders, before all sub-folders or objects of folders,and modules.
  13. local Packages = ReplicatedStorage:WaitForChild("Packages")
  14. local Interface = ReplicatedStorage:WaitForChild("Interface")
  15. local Sounds = ReplicatedStorage:WaitForChild("Sounds")
  16.  
  17. --// I always use WaitForChild in an un-defined scope, as it ensures it will always be given time to load.
  18. --// Load modules before objects.
  19. local Fusion = require(Packages:WaitForChild("Fusion"))
  20.  
  21. local LootInterface = Interface:WaitForChild("LootInterface")
  22. local PlantingFrame = LootInterface:WaitForChild("PlantingFrame")
  23.  
  24. local ScrollingFrame = PlantingFrame:WaitForChild("Frame"):WaitForChild("ScrollingFrame")
  25. local CloseButton = PlantingFrame:WaitForChild("CloseButton")
  26.  
  27. local Tooltip = LootInterface:WaitForChild("Tooltip")
  28.  
  29. local Blur = Lighting:WaitForChild("Blur")
  30.  
  31. local OpenSound = Sounds:WaitForChild("Open")
  32. local HoverSound = Sounds:WaitForChild("Hover")
  33. local ClickSound = Sounds:WaitForChild("Click")
  34.  
  35. --// Initializing Fusion keywords.
  36. local Value, Computed, Hydrate = Fusion.Value, Fusion.Computed, Fusion.Hydrate
  37. local New, Children, OnEvent, OnChange = Fusion.New, Fusion.Children, Fusion.OnEvent, Fusion.OnChange
  38. local Tween, Spring = Fusion.Tween, Fusion.Spring
  39.  
  40. local ACTIVATION_KEY = Enum.KeyCode.E
  41.  
  42. local SPRING_SPEED = 25
  43. local SPRING_DAMPING = .5
  44.  
  45. --// This value is an important listener as it controls all functionality for opening and closing the GUI.
  46. local InterfaceEnabled = Value(false)
  47.  
  48. local InterfaceEnabledComputation = Computed(function()
  49.     local InterfaceEnabled = InterfaceEnabled:get()
  50.    
  51.     if InterfaceEnabled then
  52.         OpenSound:Play()
  53.     end
  54.    
  55.     return InterfaceEnabled
  56. end)
  57.  
  58. --// Bootstrap for opening and closing the GUI (InputService)
  59. local function InputBegan(InputObject, GameProcessedEvent)
  60.     local InputKey = InputObject.KeyCode
  61.     if GameProcessedEvent or InputKey ~= ACTIVATION_KEY then
  62.         return
  63.     end
  64.  
  65.     InterfaceEnabled:set(not InterfaceEnabled:get())
  66. end
  67.  
  68. --// This runs on MouseButton1Click on the CloseButton Instance
  69. local function CloseButtonClicked()
  70.     InterfaceEnabled:set(false)
  71. end
  72.  
  73. --// This runs every frame while the mouse is in a Cell
  74. local function UpdateTooltipPosition(MouseInside)
  75.     print(MouseInside:get())
  76.     while MouseInside:get() do task.wait()
  77.         local ScreenCenter = Camera.ViewportSize / 2
  78.         local MousePosition = InputService:GetMouseLocation()
  79.         local MouseInTopHalf = (MousePosition.Y - ScreenCenter.Y) < 0
  80.  
  81.         local TooltipAnchorY = MouseInTopHalf and .05 or .95
  82.  
  83.         Tooltip.AnchorPoint = Vector2.new(.95, TooltipAnchorY)
  84.         Tooltip.Position = UDim2.fromOffset(MousePosition.X, MousePosition.Y)
  85.     end
  86. end
  87.  
  88. --// "Cell" is a keyword for a child of a "Grid" (UIGridLayout)
  89. local function InitializeCell(Cell)
  90.     local ImageButton = Cell:WaitForChild("ImageButton")
  91.     local UIScale = nil
  92.     local MouseInside = Value(false)   
  93.    
  94.     local function ScaleComputation()
  95.         return InterfaceEnabled:get() and (MouseInside:get() and .9 or 1) or 0
  96.     end
  97.  
  98.     local function MouseInsideComputation()
  99.         if MouseInside:get() then
  100.             HoverSound:Play()
  101.         end
  102.  
  103.         task.spawn(UpdateTooltipPosition, MouseInside)
  104.         return MouseInside:get()
  105.     end
  106.    
  107.     local function CellMouseEnter()
  108.         MouseInside:set(true)
  109.     end
  110.     local function CellMouseLeave()
  111.         MouseInside:set(false)
  112.     end
  113.    
  114.     local function CellMouseButton1Click()
  115.         ClickSound:Play()
  116.     end
  117.        
  118.     UIScale = New "UIScale" {
  119.         Parent = Cell,
  120.         Scale = Spring(Computed(ScaleComputation), SPRING_SPEED, SPRING_DAMPING),
  121.     }
  122.    
  123.     Hydrate(ImageButton) {
  124.         [OnEvent "MouseEnter"] = CellMouseEnter,
  125.         [OnEvent "MouseLeave"] = CellMouseLeave,
  126.         [OnEvent "MouseButton1Click"] = CellMouseButton1Click
  127.     }  
  128.     Hydrate(Tooltip) {
  129.         Visible = MouseInsideComputation,
  130.         Position = UDim2.new(0, 0, 0 ,0)
  131.     }  
  132.     Computed(MouseInsideComputation)
  133. end
  134.  
  135. --// This is a self contained function to reduce indentation when looping through children.
  136. local function InitializeScrollingGrid()
  137.     for _, Cell in ipairs(ScrollingFrame:GetChildren()) do
  138.         if not Cell:IsA("Frame") then
  139.             continue
  140.         end
  141.        
  142.         InitializeCell(Cell)
  143.     end
  144. end
  145.  
  146. Hydrate(LootInterface) {
  147.     --// Order of creation should replicate Instance.new()
  148.     --// ex; Parent is set last.
  149.     Enabled = InterfaceEnabledComputation,
  150.     Parent = PlayerGui,
  151.    
  152.     [Children] = {
  153.         PlantingFrame = Hydrate(PlantingFrame) {
  154.             [Children] = {     
  155.                 New "UIScale" {
  156.                     Scale =  Spring(Computed(function()
  157.                         return InterfaceEnabled:get() and 1.2 or 1
  158.                     end), SPRING_SPEED, SPRING_DAMPING),
  159.                 },         
  160.                 Hydrate(CloseButton) {
  161.                     [OnEvent "MouseButton1Click"] = CloseButtonClicked
  162.                 },
  163.            
  164.                 Computed(InitializeScrollingGrid)  
  165.             }
  166.         },             
  167.     }
  168. }
  169.  
  170. --// We init the Blur completely separately, becuase it is not in any way a descendent of LootInterface
  171. Hydrate(Blur) {
  172.     Enabled = InterfaceEnabledComputation,
  173.     Size = Spring(Computed(function()
  174.         return InterfaceEnabled:get() and 24 or 0
  175.     end), SPRING_SPEED, SPRING_DAMPING)
  176. }
  177.  
  178. --// Note, this was made with the functional programming paradigm in mind.
  179.  
  180. InputService.InputBegan:Connect(InputBegan)
  181.  
  182. --[[ // Order of operations for Fusion bootstrapping is
  183.     1. Create
  184.     2. Hydrate
  185.     3. Compute
  186.    
  187.     Order of operations for a Fusion instances' properites and events is:
  188.    
  189.     1. Most essential to least essential properties
  190.     2. Parent to object
  191.     3. Initilaize blocks "[]" such as "Children"
  192. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement