@quenty/coreguienabler 6.0.1 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [6.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/coreguienabler@6.0.1...@quenty/coreguienabler@6.1.0) (2023-01-11)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Use CoreGuiEnabler to disable core gui types ([808d020](https://github.com/Quenty/NevermoreEngine/commit/808d020ce49a72e738c7abdc0f918cba40043f46))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [6.0.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/coreguienabler@6.0.0...@quenty/coreguienabler@6.0.1) (2022-11-04)
7
18
 
8
19
  **Note:** Version bump only for package @quenty/coreguienabler
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/coreguienabler",
3
- "version": "6.0.1",
3
+ "version": "6.1.0",
4
4
  "description": "Key based CoreGuiEnabler, singleton Use this class to load/unload CoreGuis / other GUIs, by disabling based upon keys Keys are additive, so if you have more than 1 disabled, it's ok.",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -26,10 +26,11 @@
26
26
  "dependencies": {
27
27
  "@quenty/characterutils": "^6.0.1",
28
28
  "@quenty/loader": "^6.0.1",
29
- "@quenty/maid": "^2.4.0"
29
+ "@quenty/maid": "^2.4.0",
30
+ "@quenty/rx": "^7.4.0"
30
31
  },
31
32
  "publishConfig": {
32
33
  "access": "public"
33
34
  },
34
- "gitHead": "8b9877c26a3fc753409a5114e56717837291279d"
35
+ "gitHead": "db2db00ab4e24a3eab0449b77a00bee6d91f2755"
35
36
  }
@@ -23,6 +23,8 @@ local UserInputService = game:GetService("UserInputService")
23
23
 
24
24
  local CharacterUtils = require("CharacterUtils")
25
25
  local Maid = require("Maid")
26
+ local ObservableSubscriptionTable = require("ObservableSubscriptionTable")
27
+ local Rx = require("Rx")
26
28
 
27
29
  local CoreGuiEnabler = {}
28
30
  CoreGuiEnabler.__index = CoreGuiEnabler
@@ -35,6 +37,9 @@ function CoreGuiEnabler.new()
35
37
 
36
38
  self._states = {}
37
39
 
40
+ self._stateSubs = ObservableSubscriptionTable.new()
41
+ self._maid:GiveTask(self._stateSubs)
42
+
38
43
  self:AddState(Enum.CoreGuiType.Backpack, function(isEnabled)
39
44
  StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, isEnabled)
40
45
  CharacterUtils.unequipTools(Players.LocalPlayer)
@@ -64,48 +69,82 @@ function CoreGuiEnabler.new()
64
69
  end
65
70
 
66
71
  function CoreGuiEnabler:_addStarterGuiState(stateName)
67
- local boolValueName = stateName .. "State"
68
- self[boolValueName] = Instance.new("BoolValue")
69
- self[boolValueName].Value = false
70
-
71
72
  self:AddState(stateName, function(isEnabled)
72
- self[boolValueName].Value = isEnabled
73
73
  local success, err = pcall(function()
74
74
  StarterGui:SetCore(stateName, isEnabled)
75
75
  end)
76
76
  if not success then
77
- warn("Failed to set core", err)
77
+ warn("[CoreGuiEnabler] - Failed to set core", err)
78
78
  end
79
79
  end)
80
80
  end
81
81
 
82
+ --[=[
83
+ Gets the current state
84
+
85
+ @param coreGuiState string | CoreGuiType
86
+ @return boolean
87
+ ]=]
88
+ function CoreGuiEnabler:IsEnabled(coreGuiState)
89
+ local data = self._states[coreGuiState]
90
+ if not data then
91
+ error(string.format("[CoreGuiEnabler] - State '%s' does not exist.", tostring(coreGuiState)))
92
+ end
93
+
94
+ return next(data.disabledBy) == nil
95
+ end
96
+
97
+ --[=[
98
+ Observes the state whenever it changed
99
+ @param coreGuiState string | CoreGuiType
100
+ @return Observable<boolean>
101
+ ]=]
102
+ function CoreGuiEnabler:ObserveIsEnabled(coreGuiState)
103
+ local data = self._states[coreGuiState]
104
+ if not data then
105
+ error(string.format("[CoreGuiEnabler] - State '%s' does not exist.", tostring(coreGuiState)))
106
+ end
107
+
108
+ return self._stateSubs:Observe(coreGuiState)
109
+ :Pipe({
110
+ Rx.startFrom(function()
111
+ return { self:IsEnabled(coreGuiState) }
112
+ end)
113
+ })
114
+ end
115
+
82
116
  --[=[
83
117
  Adds a state that can be disabled or enabled.
84
118
  @param coreGuiState string | CoreGuiType
85
- @param coreGuiStateChangeFunc (isEnabled: boolean)
119
+ @param onChangeCallback (isEnabled: boolean)
86
120
  ]=]
87
- function CoreGuiEnabler:AddState(coreGuiState, coreGuiStateChangeFunc)
88
- assert(type(coreGuiStateChangeFunc) == "function", "must have coreGuiStateChangeFunc as function")
121
+ function CoreGuiEnabler:AddState(coreGuiState, onChangeCallback)
122
+ assert(type(onChangeCallback) == "function", "must have onChangeCallback as function")
89
123
  assert(self._states[coreGuiState] == nil, "state already exists")
90
124
 
91
- local realState = {}
92
- local lastState = true
125
+ self._states[coreGuiState] = {
126
+ lastState = true;
127
+ onChangeCallback = onChangeCallback;
128
+ disabledBy = {};
129
+ }
130
+ end
131
+
132
+ function CoreGuiEnabler:_setDisabledByKey(coreGuiState, key, value)
133
+ assert(key ~= nil, "Bad key")
93
134
 
94
- local function isEnabled()
95
- return next(realState) == nil
135
+ local data = self._states[coreGuiState]
136
+ if not data then
137
+ error(string.format("[CoreGuiEnabler] - State '%s' does not exist.", tostring(coreGuiState)))
96
138
  end
97
139
 
98
- self._states[coreGuiState] = setmetatable({}, {
99
- __newindex = function(_, index, value)
100
- rawset(realState, index, value)
101
-
102
- local newState = isEnabled()
103
- if lastState ~= newState then
104
- lastState = newState
105
- coreGuiStateChangeFunc(newState)
106
- end
107
- end;
108
- })
140
+ data.disabledBy[key] = value
141
+
142
+ local newState = next(data.disabledBy) == nil
143
+ if data.lastState ~= newState then
144
+ data.lastState = newState
145
+ data.onChangeCallback(newState)
146
+ self._stateSubs:Fire(coreGuiState, newState)
147
+ end
109
148
  end
110
149
 
111
150
  --[=[
@@ -115,11 +154,13 @@ end
115
154
  @return function -- Callback function to re-enable the state
116
155
  ]=]
117
156
  function CoreGuiEnabler:Disable(key, coreGuiState)
157
+ assert(key ~= nil, "Bad key")
158
+
118
159
  if not self._states[coreGuiState] then
119
- error(("[CoreGuiEnabler] - State '%s' does not exist."):format(tostring(coreGuiState)))
160
+ error(string.format("[CoreGuiEnabler] - State '%s' does not exist.", tostring(coreGuiState)))
120
161
  end
121
162
 
122
- self._states[coreGuiState][key] = true
163
+ self:_setDisabledByKey(coreGuiState, key, true)
123
164
 
124
165
  return function()
125
166
  self:Enable(key, coreGuiState)
@@ -132,11 +173,13 @@ end
132
173
  @param coreGuiState string | CoreGuiType
133
174
  ]=]
134
175
  function CoreGuiEnabler:Enable(key, coreGuiState)
176
+ assert(key ~= nil, "Bad key")
177
+
135
178
  if not self._states[coreGuiState] then
136
- error(("[CoreGuiEnabler] - State '%s' does not exist."):format(tostring(coreGuiState)))
179
+ error(string.format("[CoreGuiEnabler] - State '%s' does not exist.", tostring(coreGuiState)))
137
180
  end
138
181
 
139
- self._states[coreGuiState][key] = nil
182
+ self:_setDisabledByKey(coreGuiState, key, nil)
140
183
  end
141
184
 
142
185
  return CoreGuiEnabler.new()