2013/yue/89449093.yue

415 lines
11 KiB
Plaintext

import "macros" as { $ }
$load $FILE
-- This script manages context switches in the backpack (Gear to Wardrobe, etc.) and player state changes. Also manages global functions across different tabs (currently only search)
return if game.CoreGui.Version < 7
-- peace out if we aren't using the right client
-- basic functions
waitForChild = (instance, name) ->
until instance\FindFirstChild name
instance.ChildAdded\wait!
instance\FindFirstChild name
waitForProperty = (instance, property) ->
until instance[property]
instance.Changed\wait!
-- don't do anything if we are in an empty game
waitForChild game, "Players"
if #game.Players\GetChildren! < 1
game.Players.ChildAdded\wait!
-- make sure everything is loaded in before we do anything
-- get our local player
waitForProperty game.Players, "LocalPlayer"
------------------------ Locals ------------------------------
backpack = script.Parent
waitForChild backpack, "Gear"
screen = script.Parent.Parent
assert screen\IsA "ScreenGui"
waitForChild backpack, "Tabs"
waitForChild backpack.Tabs, "CloseButton"
closeButton = backpack.Tabs.CloseButton
waitForChild backpack.Tabs, "InventoryButton"
inventoryButton = backpack.Tabs.InventoryButton
local wardrobeButton
if game.CoreGui.Version >= 8
waitForChild backpack.Tabs, "WardrobeButton"
wardrobeButton = backpack.Tabs.WardrobeButton
waitForChild backpack.Parent, "ControlFrame"
backpackButton = waitForChild backpack.Parent.ControlFrame, "BackpackButton"
currentTab = "gear"
searchFrame = waitForChild backpack, "SearchFrame"
waitForChild backpack.SearchFrame, "SearchBoxFrame"
searchBox = waitForChild backpack.SearchFrame.SearchBoxFrame, "SearchBox"
searchButton = waitForChild backpack.SearchFrame, "SearchButton"
resetButton = waitForChild backpack.SearchFrame, "ResetButton"
robloxGui = waitForChild Game.CoreGui, "RobloxGui"
currentLoadout = waitForChild robloxGui, "CurrentLoadout"
loadoutBackground = waitForChild currentLoadout, "Background"
canToggle = true
readyForNextEvent = true
backpackIsOpen = false
active = true
disabledByDeveloper = false
local humanoidDiedCon
guiTweenSpeed = 0.25 -- how quickly we open/close the backpack
searchDefaultText = "Search..."
tilde = "~"
backquote = "`"
backpackSize = UDim2.new 0, 600, 0, 400
if robloxGui.AbsoluteSize.Y <= 320
backpackSize = UDim2.new 0, 200, 0, 140
------------------------ End Locals ---------------------------
---------------------------------------- Public Event Setup ----------------------------------------
createPublicEvent = (eventName) ->
assert eventName, "eventName is nil"
assert "#{eventName}", "eventName is not a string"
with Instance.new "BindableEvent"
.Name = "#{eventName}"
.Parent = script
createPublicFunction = (funcName, invokeFunc) ->
assert funcName, "funcName is nil"
assert "#{funcName}", "funcName is not a string"
assert invokeFunc, "invokeFunc is nil"
assert type(invokeFunc) == "function", "invokeFunc should be of type 'function'"
with Instance.new "BindableFunction"
.Name = "#{funcName}"
.OnInvoke = invokeFunc
.Parent = script
-- Events
resizeEvent = createPublicEvent "ResizeEvent"
backpackOpenEvent = createPublicEvent "BackpackOpenEvent"
backpackCloseEvent = createPublicEvent "BackpackCloseEvent"
tabClickedEvent = createPublicEvent "TabClickedEvent"
searchRequestedEvent = createPublicEvent "SearchRequestedEvent"
---------------------------------------- End Public Event Setup ----------------------------------------
--------------------------- Internal Functions ----------------------------------------
resetSearchBoxGui = ->
resetButton.Visible = false
searchBox.Text = searchDefaultText
resetSearch = ->
resetSearchBoxGui!
searchRequestedEvent\Fire!
deactivateBackpack = ->
backpack.Visible = false
active = false
initHumanoidDiedConnections = ->
if humanoidDiedCon
humanoidDiedCon\disconnect!
waitForProperty game.Players.LocalPlayer, "Character"
waitForChild game.Players.LocalPlayer.Character, "Humanoid"
humanoidDiedCon = game.Players.LocalPlayer.Character.Humanoid.Died\connect deactivateBackpack
hideBackpack = ->
backpackIsOpen = false
readyForNextEvent = false
backpackButton.Selected = false
resetSearch!
backpackCloseEvent\Fire currentTab
backpack.Tabs.Visible = false
searchFrame.Visible = false
backpack\TweenSizeAndPosition(
UDim2.new(0, backpackSize.X.Offset, 0, 0),
UDim2.new(0.5, -backpackSize.X.Offset / 2, 1, -85),
Enum.EasingDirection.Out,
Enum.EasingStyle.Quad,
guiTweenSpeed,
true,
->
game.GuiService\RemoveCenterDialog backpack
backpack.Visible = false
backpackButton.Selected = false
)
delay guiTweenSpeed, ->
game.GuiService\RemoveCenterDialog backpack
backpack.Visible = false
backpackButton.Selected = false
readyForNextEvent = true
canToggle = true
showBackpack = ->
game.GuiService\AddCenterDialog(backpack, Enum.CenterDialogType.PlayerInitiatedDialog, ->
backpack.Visible = true
backpackButton.Selected = true
->
backpack.Visible = false
backpackButton.Selected = false
)
backpack.Visible = true
backpackButton.Selected = true
backpack\TweenSizeAndPosition(
backpackSize,
UDim2.new(0.5, -backpackSize.X.Offset / 2, 1, -backpackSize.Y.Offset - 88),
Enum.EasingDirection.Out,
Enum.EasingStyle.Quad,
guiTweenSpeed,
true
)
delay guiTweenSpeed, ->
backpack.Tabs.Visible = false
searchFrame.Visible = true
backpackOpenEvent\Fire currentTab
canToggle = true
readyForNextEvent = true
backpackButton.Image = "http://www.roblox.com/asset/?id=97644093"
backpackButton.Position = UDim2.new 0.5, -60, 1, -backpackSize.Y.Offset - 103
toggleBackpack = ->
return if not game.Players.LocalPlayer
return if not game.Players.LocalPlayer["Character"]
return if not canToggle
return if not readyForNextEvent
readyForNextEvent = false
canToggle = false
backpackIsOpen = not backpackIsOpen
if backpackIsOpen
with loadoutBackground
.Image = "http://www.roblox.com/asset/?id=97623721"
.Position = UDim2.new -0.03, 0, -0.17, 0
.Size = UDim2.new 1.05, 0, 1.25, 0
.ZIndex = 2.0
.Visible = true
showBackpack!
else
backpackButton.Position = UDim2.new 0.5, -60, 1, -44
loadoutBackground.Visible = false
backpackButton.Selected = false
backpackButton.Image = "http://www.roblox.com/asset/?id=97617958"
loadoutBackground.Image = "http://www.roblox.com/asset/?id=96536002"
loadoutBackground.Position = UDim2.new -0.1, 0, -0.1, 0
loadoutBackground.Size = UDim2.new 1.2, 0, 1.2, 0
hideBackpack!
clChildren = currentLoadout\GetChildren!
for i = 1, #clChildren
if clChildren[i] and clChildren[i]\IsA "Frame"
frame = clChildren[i]
if #frame\GetChildren! > 0
backpackButton.Position = UDim2.new 0.5, -60, 1, -108
backpackButton.Visible = true
loadoutBackground.Visible = true
if frame\GetChildren![1]\IsA "ImageButton"
imgButton = frame\GetChildren![1]
imgButton.Active = true
imgButton.Draggable = false
activateBackpack = ->
initHumanoidDiedConnections!
active = true
backpack.Visible = backpackIsOpen
if backpackIsOpen
toggleBackpack!
closeBackpack = ->
if backpackIsOpen
toggleBackpack!
setSelected = (tab) ->
assert tab
with tab
assert \IsA "TextButton"
.BackgroundColor3 = Color3.new 1, 1, 1
.TextColor3 = Color3.new 0, 0, 0
.Selected = true
.ZIndex = 3
setUnselected = (tab) ->
assert tab
with tab
assert \IsA "TextButton"
.BackgroundColor3 = Color3.new 0, 0, 0
.TextColor3 = Color3.new 1, 1, 1
.Selected = false
.ZIndex = 1
updateTabGui = (selectedTab) ->
assert selectedTab
if selectedTab == "gear"
setSelected inventoryButton
setUnselected wardrobeButton
elseif selectedTab == "wardrobe"
setSelected wardrobeButton
setUnselected inventoryButton
mouseLeaveTab = (button) ->
assert button
assert button\IsA "TextButton"
return if button.Selected
button.BackgroundColor3 = Color3.new 0, 0, 0
mouseOverTab = (button) ->
assert button
assert button\IsA "TextButton"
return if button.Selected
button.BackgroundColor3 = Color3.new 39 / 255, 39 / 255, 39 / 255
newTabClicked = (tabName) ->
assert tabName
tabName = string.lower tabName
currentTab = tabName
updateTabGui tabName
tabClickedEvent\Fire tabName
resetSearch!
trim = (s) -> s\gsub "^%s*(.-)%s*$", "%1"
-- splitByWhitespace = (text) ->
-- return if type(text) ~= "string"
-- terms = {}
-- for token in string.gmatch text, "[^%s]+"
-- if string.len(token) > 0
-- table.insert terms, token
-- terms
doSearch = ->
searchText = searchBox.Text
if searchText == ""
resetSearch!
return
searchText = trim searchText
resetButton.Visible = true
-- termTable = splitByWhitespace(searchText)
searchRequestedEvent\Fire searchText -- todo: replace this with termtable when table passing is possible
backpackReady = ->
readyForNextEvent = true
coreGuiChanged = (coreGuiType, enabled) ->
if coreGuiType == Enum.CoreGuiType.Backpack or coreGuiType == Enum.CoreGuiType.All
active = enabled
disabledByDeveloper = not enabled
with game\GetService "GuiService"
if disabledByDeveloper
try
\RemoveKey tilde
\RemoveKey backquote
else
\AddKey tilde
\AddKey backquote
resetSearch!
searchFrame.Visible = enabled and backpackIsOpen
currentLoadout.Visible = enabled
backpack.Visible = enabled
backpackButton.Visible = enabled
--------------------------- End Internal Functions -------------------------------------
------------------------------ Public Functions Setup -------------------------------------
createPublicFunction "CloseBackpack", hideBackpack
createPublicFunction "BackpackReady", backpackReady
------------------------------ End Public Functions Setup ---------------------------------
------------------------ Connections/Script Main -------------------------------------------
try
coreGuiChanged Enum.CoreGuiType.Backpack, Game.StarterGui\GetCoreGuiEnabled Enum.CoreGuiType.Backpack
Game.StarterGui.CoreGuiChangedSignal\connect coreGuiChanged
with inventoryButton
.MouseButton1Click\connect -> newTabClicked "gear"
.MouseEnter\connect -> mouseOverTab inventoryButton
.MouseLeave\connect -> mouseLeaveTab inventoryButton
if game.CoreGui.Version >= 8
with wardrobeButton
.MouseButton1Click\connect -> newTabClicked "wardrobe"
.MouseEnter\connect -> mouseOverTab wardrobeButton
.MouseLeave\connect -> mouseLeaveTab wardrobeButton
closeButton.MouseButton1Click\connect closeBackpack
screen.Changed\connect (prop) ->
if prop == "AbsoluteSize"
resizeEvent\Fire screen.AbsoluteSize
-- GuiService key setup
with game\GetService "GuiService"
\AddKey tilde
\AddKey backquote
.KeyPressed\connect (key) ->
return if not active or disabledByDeveloper
if key == tilde or key == backquote
toggleBackpack!
backpackButton.MouseButton1Click\connect ->
return if not active or disabledByDeveloper
toggleBackpack!
if game.Players.LocalPlayer["Character"]
activateBackpack!
game.Players.LocalPlayer.CharacterAdded\connect activateBackpack
-- search functions
searchBox.FocusLost\connect (enterPressed) ->
if enterPressed or searchBox.Text ~= ""
doSearch!
elseif searchBox.Text == ""
resetSearch!
searchButton.MouseButton1Click\connect doSearch
resetButton.MouseButton1Click\connect resetSearch
if searchFrame and robloxGui.AbsoluteSize.Y <= 320
searchFrame.RobloxLocked = false
searchFrame\Destroy!