Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RunService = game:GetService("RunService")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Players = game:GetService("Players")
- local GuiService = game:GetService("GuiService")
- local UserInputService = game:GetService("UserInputService")
- local Button = require(script.Parent.Button)
- local hf = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("HelperFunctions"))
- local uiEvent = ReplicatedStorage:WaitForChild("UI"):WaitForChild("UIEvent")
- local NINTHS = {
- {"Authleft", "Authcenter", "Authright"},
- {"Leftcenter", "Centrist", "Rightcenter"},
- {"Libleft", "Libcenter", "Libright"}
- }
- local FlairGui = {}
- FlairGui.__index = FlairGui
- function FlairGui.new(flairGui)
- local self = {}
- setmetatable(self, FlairGui)
- -- State
- self.owner = Players.LocalPlayer
- self.isChoosingFlair = false
- -- Managed gui element instances and connections
- self.flairGui = flairGui
- self.editFlairButton = self.flairGui.EditFlairButton
- self.editFlairButton.Activated:Connect(function()
- self:onEditFlairButtonActivated()
- end)
- self.flairChooserBackground = self.flairGui.FlairChooserBackground
- self.flairChooserBackground.Visible = false
- self.flairChooser = self.flairChooserBackground.FlairChooser
- self.flairChooser.MouseMoved:Connect(function(mouseX, mouseY)
- self:onMouseMovedInsideFlairChooser(mouseX, mouseY)
- end)
- self.flairChooser.MouseButton1Down:Connect(function(mouseX, mouseY)
- self:onFlairChooserActivated(mouseX, mouseY)
- end)
- -- Compass
- self.compass = self.flairChooser.Compass
- self.circle = self.compass.Circle
- -- Slider
- self.slider = self.flairChooser.Slider
- self.bar = self.slider.Bar
- -- Submit button
- self.submitButton = Button.new(
- self.flairGui.FlairChooserBackground.SubmitButton,
- Color3.fromRGB(0, 223, 0),
- Color3.fromRGB(0, 255, 0),
- Color3.fromRGB(0, 200, 0)
- )
- self.submitButton.mouseButton1Down:Connect(function()
- self:submit()
- end)
- -- RemoveFlair button
- self.removeFlairButton = Button.new(
- self.flairGui.FlairChooserBackground.RemoveFlairButton,
- Color3.fromRGB(184, 0, 0),
- Color3.fromRGB(220, 0, 0),
- Color3.fromRGB(160, 0, 0)
- )
- self.removeFlairButton.mouseButton1Down:Connect(function()
- self:removeFlair()
- end)
- -- EnterIdeologyTextbox
- self.ideologyTextbox = self.flairChooserBackground.EnterIdeology.Frame.IdeologyTextBox
- self.ideologyTextbox.FocusLost:Connect(function()
- self.selectedIdeology = self.ideologyTextbox.Text
- end)
- self:setupUIEventConn()
- self:init()
- print("[FLAIR GUI] Client initialized FlairGui:", self)
- return self
- end
- function FlairGui:init()
- -- start closed
- self:setFlairChooserOpen(false)
- -- init ideology
- self.selectedIdeology = self.flairGui.Init.Ideology.Value
- -- init stats
- self.selectedPercentCulturallyConservative = self.flairGui.Init.PercentCulturallyConservative.Value
- self.selectedPercentEconomicallyRight = self.flairGui.Init.PercentEconomicallyRight.Value
- self.selectedPercentLibertarian = self.flairGui.Init.PercentLibertarian.Value
- -- init character
- local character = self.owner.Character or self.owner.CharacterAdded:Wait()
- local humanoid = character:WaitForChild("Humanoid")
- -- Close on death
- humanoid.Died:Connect(function()
- self:setFlairChooserOpen(false)
- end)
- self:update()
- end
- function FlairGui:update()
- -- update selected ideo
- if self.selectedIdeology == "Undecided" then
- self.ideologyTextbox.Text = "Enter Ideology (optional)"
- else
- self.ideologyTextbox.Text = self.selectedIdeology
- end
- -- update compass circle
- local circleMappedX = hf.map(self.selectedPercentEconomicallyRight, 0,100, 0,1)
- local circleMappedY = hf.map(self.selectedPercentLibertarian, 0,100, 0,1)
- self.circle.Position = UDim2.new(circleMappedX, 0, circleMappedY, 0)
- -- update slider bar
- local mappedY = hf.map(self.selectedPercentCulturallyConservative, 0,100, 0,1)
- self.bar.Position = UDim2.new(.5, 0, mappedY, 0)
- if self.selectedNinth ~= "Undecided" then
- -- update selected ninth
- local y = math.floor(hf.map(self.selectedPercentLibertarian, 0,100, 1,3))
- local x = math.floor(hf.map(self.selectedPercentEconomicallyRight, 0,100, 1,3))
- self.selectedNinth = NINTHS[y][x]
- end
- end
- function FlairGui:setupUIEventConn()
- self.uiEventConn = uiEvent.OnClientEvent:Connect(function(params)
- if params.battleStarted and self.isChoosingFlair then
- self:setFlairChooserOpen(false)
- end
- end)
- end
- function FlairGui:setFlairChooserOpen(isOpen)
- self.flairChooserBackground.Visible = isOpen
- self.isChoosingFlair = isOpen
- end
- function FlairGui:onEditFlairButtonActivated()
- if self.isChoosingFlair then
- self:setFlairChooserOpen(false)
- else
- self:setFlairChooserOpen(true)
- end
- end
- function FlairGui:onFlairChooserActivated(mouseX, mouseY)
- if hf.pointInElement(mouseX, mouseY, self.compass) then
- --print("clicked compass")
- self.focus = self.compass
- self:onCompassInputBegan(mouseX, mouseY)
- elseif hf.pointInElement(mouseX, mouseY, self.slider) then
- --print("clicked slider")
- self.focus = self.slider
- self:onSliderInputBegan(mouseX, mouseY)
- else
- --print("clicked nothing")
- self.focus = nil
- end
- end
- function FlairGui:onMouseMovedInsideFlairChooser(mouseX, mouseY)
- --print(mouseX..","..mouseY)
- local flairChooserPosition = self.flairChooser.AbsolutePosition
- local flairChooserSize = self.flairChooser.AbsoluteSize
- local relativeX = mouseX - flairChooserPosition.X
- local relativeY = (mouseY - flairChooserPosition.Y) - GuiService:GetGuiInset().Y
- --print("relative x:", math.floor(relativeX))
- --print("relative y:", math.floor(relativeY))
- if self.focus == self.compass then
- self:onCompassInputBegan(mouseX, mouseY)
- elseif self.focus == self.slider then
- self:onSliderInputBegan(mouseX, mouseY)
- end
- end
- function FlairGui:onCompassInputBegan(mouseX, mouseY)
- if not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then return end
- --print("compass clicked at ("..mouseX..","..mouseY..")")
- local compassPosition = self.compass.AbsolutePosition
- local compassSize = self.compass.AbsoluteSize
- local relativeX = mouseX - compassPosition.X
- local relativeY = (mouseY - compassPosition.Y) - GuiService:GetGuiInset().Y
- --print("relative x:", math.floor(relativeX))
- --print("relative y:", math.floor(relativeY))
- local clampedX = math.clamp(relativeX, 0, compassSize.X)
- local clampedY = math.clamp(relativeY, 0, compassSize.Y)
- --print("clamped x:", clampedX)
- --print("clamped y:", clampedY)
- local mappedX = hf.map(clampedX, 0, compassSize.X, 0, 1)
- local mappedY = hf.map(clampedY, 0, compassSize.Y, 0, 1)
- --print("mapped x:", mappedX)
- --print("mapped y:", mappedY)
- self.circle.Position = UDim2.new(mappedX, 0, mappedY, 0)
- local x = math.clamp( math.floor(mappedX * 3) + 1, 1, 3)
- local y = math.clamp( math.floor(mappedY * 3) + 1, 1, 3)
- --print("x:", x)
- --print("y:", y)
- if self.selectedNinth ~= NINTHS[y][x] then
- self.selectedNinth = NINTHS[y][x]
- --print("selected ninth:", self.selectedNinth)
- end
- local remappedX = hf.map(mappedX, 0, 1, 0, 100)
- local percentEconomicallyRight = math.floor(remappedX)
- if self.selectedPercentEconomicallyRight ~= percentEconomicallyRight then
- self.selectedPercentEconomicallyRight = percentEconomicallyRight
- --print("selected percent economically right: ".."%"..self.selectedPercentEconomicallyRight.." economically right")
- end
- local remappedY = hf.map(mappedY, 0, 1, 0, 100)
- local percentLibertarian = math.floor(remappedY)
- if self.selectedPercentLibertarian ~= percentLibertarian then
- self.selectedPercentLibertarian = percentLibertarian
- --print("selected percent libertarian: ".."%"..self.selectedPercentLibertarian)
- end
- end
- function FlairGui:onSliderInputBegan(mouseX, mouseY)
- if not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then return end
- --print("slider clicked at ("..mouseX..","..mouseY..")")
- local sliderPosition = self.slider.AbsolutePosition
- local sliderSize = self.slider.AbsoluteSize
- local relativeY = (mouseY - sliderPosition.Y) - GuiService:GetGuiInset().Y
- --print("relative y:", math.floor(relativeY))
- local clampedY = math.clamp(relativeY, 0, sliderSize.Y)
- --print("clamped y:", clampedY)
- local mappedY = hf.map(clampedY, 0, sliderSize.Y, 0, 1)
- --print("mapped y:", mappedY)
- self.bar.Position = UDim2.new(.5, 0, mappedY, 0)
- local remappedY = hf.map(mappedY, 0, 1, 0, 100)
- local percentConvervative = math.floor(remappedY)
- if self.selectedPercentCulturallyConservative ~= percentConvervative then
- self.selectedPercentCulturallyConservative = percentConvervative
- print("selected percent culturally conservative: ".."%"..self.selectedPercentCulturallyConservative.." conservative")
- end
- end
- function FlairGui:removeFlair()
- self.selectedIdeology = "Undecided"
- self.selectedNinth = "Undecided"
- self.selectedPercentCulturallyConservative = 50
- self.selectedPercentEconomicallyRight = 50
- self.selectedPercentLibertarian = 50
- self:update()
- uiEvent:FireServer({
- resetFlair = true
- })
- self:setFlairChooserOpen(false)
- end
- function FlairGui:submit()
- print("------------")
- print("[FLAIR GUI] Submitting:")
- print(" selected ideology: "..self.selectedIdeology)
- print(" selected ninth: "..self.selectedNinth)
- print(" %"..self.selectedPercentCulturallyConservative.." culturally conservative")
- print(" %"..self.selectedPercentEconomicallyRight.." economically right")
- print(" %"..self.selectedPercentLibertarian.." libertarian")
- print("------------")
- uiEvent:FireServer({
- submitFlair = {
- ideology = self.selectedIdeology,
- percentCulturallyConservative = self.selectedPercentCulturallyConservative,
- percentEconomicallyRight = self.selectedPercentEconomicallyRight,
- percentLibertarian = self.selectedPercentLibertarian
- }
- })
- self:setFlairChooserOpen(false)
- end
- return FlairGui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement