Advertisement
DanielHarty

Untitled

Feb 7th, 2022
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.93 KB | None | 0 0
  1. local RunService = game:GetService("RunService")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local Players = game:GetService("Players")
  4. local GuiService = game:GetService("GuiService")
  5. local UserInputService = game:GetService("UserInputService")
  6.  
  7. local Button = require(script.Parent.Button)
  8. local hf = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("HelperFunctions"))
  9.  
  10. local uiEvent = ReplicatedStorage:WaitForChild("UI"):WaitForChild("UIEvent")
  11.  
  12. local NINTHS = {
  13.     {"Authleft", "Authcenter", "Authright"},
  14.     {"Leftcenter", "Centrist", "Rightcenter"},
  15.     {"Libleft", "Libcenter", "Libright"}
  16. }
  17.  
  18. local FlairGui = {}
  19. FlairGui.__index = FlairGui
  20.  
  21. function FlairGui.new(flairGui)
  22.     local self = {}
  23.     setmetatable(self, FlairGui)
  24.    
  25.     -- State
  26.     self.owner = Players.LocalPlayer
  27.     self.isChoosingFlair = false
  28.    
  29.     -- Managed gui element instances and connections
  30.     self.flairGui = flairGui
  31.  
  32.    
  33.     self.editFlairButton = self.flairGui.EditFlairButton
  34.     self.editFlairButton.Activated:Connect(function()
  35.         self:onEditFlairButtonActivated()
  36.     end)
  37.    
  38.     self.flairChooserBackground = self.flairGui.FlairChooserBackground
  39.     self.flairChooserBackground.Visible = false
  40.    
  41.     self.flairChooser = self.flairChooserBackground.FlairChooser
  42.    
  43.     self.flairChooser.MouseMoved:Connect(function(mouseX, mouseY)
  44.         self:onMouseMovedInsideFlairChooser(mouseX, mouseY)
  45.     end)
  46.    
  47.     self.flairChooser.MouseButton1Down:Connect(function(mouseX, mouseY)
  48.         self:onFlairChooserActivated(mouseX, mouseY)
  49.     end)
  50.    
  51.     -- Compass
  52.     self.compass = self.flairChooser.Compass
  53.     self.circle = self.compass.Circle  
  54.    
  55.     -- Slider
  56.     self.slider = self.flairChooser.Slider
  57.     self.bar = self.slider.Bar
  58.    
  59.     -- Submit button
  60.     self.submitButton = Button.new(
  61.         self.flairGui.FlairChooserBackground.SubmitButton,
  62.         Color3.fromRGB(0, 223, 0),
  63.         Color3.fromRGB(0, 255, 0),
  64.         Color3.fromRGB(0, 200, 0)
  65.     )
  66.     self.submitButton.mouseButton1Down:Connect(function()
  67.         self:submit()
  68.     end)
  69.    
  70.     -- RemoveFlair button
  71.     self.removeFlairButton = Button.new(
  72.         self.flairGui.FlairChooserBackground.RemoveFlairButton,
  73.         Color3.fromRGB(184, 0, 0),
  74.         Color3.fromRGB(220, 0, 0),
  75.         Color3.fromRGB(160, 0, 0)
  76.     )
  77.     self.removeFlairButton.mouseButton1Down:Connect(function()
  78.         self:removeFlair()
  79.     end)
  80.    
  81.     -- EnterIdeologyTextbox
  82.     self.ideologyTextbox = self.flairChooserBackground.EnterIdeology.Frame.IdeologyTextBox
  83.     self.ideologyTextbox.FocusLost:Connect(function()
  84.         self.selectedIdeology = self.ideologyTextbox.Text
  85.     end)
  86.    
  87.     self:setupUIEventConn()
  88.    
  89.     self:init()
  90.    
  91.     print("[FLAIR GUI] Client initialized FlairGui:", self)
  92.  
  93.     return self
  94. end
  95.  
  96. function FlairGui:init()
  97.     -- start closed
  98.     self:setFlairChooserOpen(false)
  99.    
  100.     -- init ideology
  101.     self.selectedIdeology = self.flairGui.Init.Ideology.Value
  102.    
  103.     -- init stats
  104.     self.selectedPercentCulturallyConservative = self.flairGui.Init.PercentCulturallyConservative.Value
  105.     self.selectedPercentEconomicallyRight = self.flairGui.Init.PercentEconomicallyRight.Value
  106.     self.selectedPercentLibertarian = self.flairGui.Init.PercentLibertarian.Value
  107.    
  108.     -- init character
  109.     local character = self.owner.Character or self.owner.CharacterAdded:Wait()
  110.     local humanoid = character:WaitForChild("Humanoid")
  111.     -- Close on death
  112.     humanoid.Died:Connect(function()
  113.         self:setFlairChooserOpen(false)
  114.     end)
  115.    
  116.     self:update()
  117. end
  118.  
  119. function FlairGui:update()
  120.     -- update selected ideo
  121.     if self.selectedIdeology == "Undecided" then
  122.         self.ideologyTextbox.Text = "Enter Ideology (optional)"
  123.     else
  124.         self.ideologyTextbox.Text = self.selectedIdeology
  125.     end
  126.    
  127.     -- update compass circle
  128.     local circleMappedX = hf.map(self.selectedPercentEconomicallyRight, 0,100, 0,1)
  129.     local circleMappedY = hf.map(self.selectedPercentLibertarian, 0,100, 0,1)
  130.     self.circle.Position = UDim2.new(circleMappedX, 0, circleMappedY, 0)
  131.  
  132.     -- update slider bar
  133.     local mappedY = hf.map(self.selectedPercentCulturallyConservative, 0,100, 0,1)
  134.     self.bar.Position = UDim2.new(.5, 0, mappedY, 0)
  135.    
  136.     if self.selectedNinth ~= "Undecided" then
  137.         -- update selected ninth
  138.         local y = math.floor(hf.map(self.selectedPercentLibertarian, 0,100, 1,3))
  139.         local x = math.floor(hf.map(self.selectedPercentEconomicallyRight, 0,100, 1,3))
  140.  
  141.         self.selectedNinth = NINTHS[y][x]
  142.     end
  143. end
  144.  
  145. function FlairGui:setupUIEventConn()
  146.     self.uiEventConn = uiEvent.OnClientEvent:Connect(function(params)
  147.         if params.battleStarted and self.isChoosingFlair then
  148.             self:setFlairChooserOpen(false)
  149.         end
  150.     end)
  151. end
  152.  
  153. function FlairGui:setFlairChooserOpen(isOpen)
  154.     self.flairChooserBackground.Visible = isOpen
  155.     self.isChoosingFlair = isOpen
  156. end
  157.  
  158. function FlairGui:onEditFlairButtonActivated()
  159.     if self.isChoosingFlair then
  160.         self:setFlairChooserOpen(false)
  161.     else
  162.         self:setFlairChooserOpen(true)
  163.     end
  164. end
  165.  
  166. function FlairGui:onFlairChooserActivated(mouseX, mouseY)
  167.     if hf.pointInElement(mouseX, mouseY, self.compass) then
  168.         --print("clicked compass")
  169.         self.focus = self.compass
  170.         self:onCompassInputBegan(mouseX, mouseY)
  171.        
  172.     elseif hf.pointInElement(mouseX, mouseY, self.slider) then
  173.         --print("clicked slider")
  174.         self.focus = self.slider
  175.         self:onSliderInputBegan(mouseX, mouseY)
  176.     else
  177.         --print("clicked nothing")
  178.         self.focus = nil
  179.     end
  180. end
  181.  
  182. function FlairGui:onMouseMovedInsideFlairChooser(mouseX, mouseY)
  183.     --print(mouseX..","..mouseY)
  184.    
  185.     local flairChooserPosition = self.flairChooser.AbsolutePosition
  186.     local flairChooserSize = self.flairChooser.AbsoluteSize
  187.  
  188.     local relativeX = mouseX - flairChooserPosition.X
  189.     local relativeY = (mouseY - flairChooserPosition.Y) - GuiService:GetGuiInset().Y
  190.  
  191.     --print("relative x:", math.floor(relativeX))
  192.     --print("relative y:", math.floor(relativeY))
  193.    
  194.     if self.focus == self.compass then
  195.         self:onCompassInputBegan(mouseX, mouseY)
  196.     elseif self.focus == self.slider then
  197.         self:onSliderInputBegan(mouseX, mouseY)
  198.     end
  199.    
  200. end
  201.  
  202. function FlairGui:onCompassInputBegan(mouseX, mouseY)
  203.  
  204.     if not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then return end
  205.  
  206.     --print("compass clicked at ("..mouseX..","..mouseY..")")
  207.  
  208.     local compassPosition = self.compass.AbsolutePosition
  209.     local compassSize = self.compass.AbsoluteSize
  210.  
  211.     local relativeX = mouseX - compassPosition.X
  212.     local relativeY = (mouseY - compassPosition.Y) - GuiService:GetGuiInset().Y
  213.  
  214.     --print("relative x:", math.floor(relativeX))
  215.     --print("relative y:", math.floor(relativeY))
  216.    
  217.     local clampedX = math.clamp(relativeX, 0, compassSize.X)
  218.     local clampedY = math.clamp(relativeY, 0, compassSize.Y)
  219.    
  220.     --print("clamped x:", clampedX)
  221.     --print("clamped y:", clampedY)
  222.  
  223.     local mappedX = hf.map(clampedX, 0, compassSize.X, 0, 1)
  224.     local mappedY = hf.map(clampedY, 0, compassSize.Y, 0, 1)
  225.  
  226.     --print("mapped x:", mappedX)
  227.     --print("mapped y:", mappedY)
  228.  
  229.     self.circle.Position = UDim2.new(mappedX, 0, mappedY, 0)
  230.  
  231.     local x = math.clamp( math.floor(mappedX * 3) + 1, 1, 3)
  232.     local y = math.clamp( math.floor(mappedY * 3) + 1, 1, 3)
  233.  
  234.  
  235.     --print("x:", x)
  236.     --print("y:", y)
  237.  
  238.     if self.selectedNinth ~= NINTHS[y][x] then
  239.         self.selectedNinth = NINTHS[y][x]
  240.         --print("selected ninth:", self.selectedNinth)
  241.     end
  242.    
  243.     local remappedX = hf.map(mappedX, 0, 1, 0, 100)
  244.     local percentEconomicallyRight = math.floor(remappedX)
  245.     if self.selectedPercentEconomicallyRight ~= percentEconomicallyRight then
  246.         self.selectedPercentEconomicallyRight = percentEconomicallyRight
  247.         --print("selected percent economically right: ".."%"..self.selectedPercentEconomicallyRight.." economically right")
  248.     end
  249.    
  250.     local remappedY = hf.map(mappedY, 0, 1, 0, 100)
  251.     local percentLibertarian = math.floor(remappedY)
  252.     if self.selectedPercentLibertarian ~= percentLibertarian then
  253.         self.selectedPercentLibertarian = percentLibertarian
  254.         --print("selected percent libertarian: ".."%"..self.selectedPercentLibertarian)
  255.     end
  256.    
  257. end
  258.  
  259. function FlairGui:onSliderInputBegan(mouseX, mouseY)
  260.  
  261.     if not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then return end
  262.  
  263.     --print("slider clicked at ("..mouseX..","..mouseY..")")
  264.  
  265.     local sliderPosition = self.slider.AbsolutePosition
  266.     local sliderSize = self.slider.AbsoluteSize
  267.  
  268.     local relativeY = (mouseY - sliderPosition.Y) - GuiService:GetGuiInset().Y
  269.     --print("relative y:", math.floor(relativeY))
  270.    
  271.     local clampedY = math.clamp(relativeY, 0, sliderSize.Y)
  272.     --print("clamped y:", clampedY)
  273.  
  274.     local mappedY = hf.map(clampedY, 0, sliderSize.Y, 0, 1)
  275.     --print("mapped y:", mappedY)
  276.  
  277.     self.bar.Position = UDim2.new(.5, 0, mappedY, 0)
  278.    
  279.     local remappedY = hf.map(mappedY, 0, 1, 0, 100)
  280.    
  281.     local percentConvervative = math.floor(remappedY)
  282.    
  283.     if self.selectedPercentCulturallyConservative ~= percentConvervative then
  284.         self.selectedPercentCulturallyConservative = percentConvervative
  285.         print("selected percent culturally conservative: ".."%"..self.selectedPercentCulturallyConservative.." conservative")
  286.     end
  287. end
  288.  
  289. function FlairGui:removeFlair()
  290.     self.selectedIdeology = "Undecided"
  291.     self.selectedNinth = "Undecided"
  292.     self.selectedPercentCulturallyConservative = 50
  293.     self.selectedPercentEconomicallyRight = 50
  294.     self.selectedPercentLibertarian = 50
  295.    
  296.     self:update()
  297.    
  298.     uiEvent:FireServer({
  299.         resetFlair = true
  300.     })
  301.    
  302.     self:setFlairChooserOpen(false)
  303.    
  304. end
  305.  
  306. function FlairGui:submit()
  307.     print("------------")
  308.     print("[FLAIR GUI] Submitting:")
  309.     print("  selected ideology: "..self.selectedIdeology)
  310.     print("  selected ninth: "..self.selectedNinth)
  311.     print("  %"..self.selectedPercentCulturallyConservative.." culturally conservative")
  312.     print("  %"..self.selectedPercentEconomicallyRight.." economically right")
  313.     print("  %"..self.selectedPercentLibertarian.." libertarian")
  314.     print("------------")
  315.     uiEvent:FireServer({
  316.         submitFlair = {
  317.             ideology = self.selectedIdeology,
  318.             percentCulturallyConservative = self.selectedPercentCulturallyConservative,
  319.             percentEconomicallyRight = self.selectedPercentEconomicallyRight,
  320.             percentLibertarian = self.selectedPercentLibertarian
  321.         }
  322.     })
  323.    
  324.     self:setFlairChooserOpen(false)
  325.    
  326. end
  327.  
  328. return FlairGui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement