11(*
22# GameTabs
3- Methods to interact with the gametab buttons
3+ Methods to interact with the gametab buttons.
4+
5+ By default it will attempt to use "F-Keys" to open gametabs.
6+ You can turn this off by setting `TRSGameTabs.KeybindsEnabled` to `False`.
7+
8+ The way keybindings work in SRLT is that it will try a random "F-Key" everytime
9+ you want to open a gametab. Because it uses a random "F-Key", it will probably
10+ not open the gametab you want but whatever opens, gets cached and mapped to the
11+ random "F-Key" used.
12+
13+ Next time you want to open a gametab that has already been mapped, it will use
14+ the correct "F-Key".
415*)
516{$DEFINE SRLT_GAMETABS_INCLUDED}
617{$INCLUDE_ONCE SRLT/osrs.simba}
718
19+ {.$DEFINE SRLT_KEYBINDS_DEBUG}
20+
821type
922(*
1023(ERSGameTab)=
@@ -14,9 +27,8 @@ ERSGameTab = enum(NONE, COMBAT, STATS, ACHIEVEMENTS, INVENTORY, EQUIPMENT, PRAYE
1427```
1528*)
1629 ERSGameTab = enum(
17- NONE,
1830 COMBAT, STATS, ACHIEVEMENTS, INVENTORY, EQUIPMENT, PRAYER, MAGIC,
19- CLAN, FRIENDS, ACCOUNT, LOGOUT, OPTIONS, EMOTES, MUSIC
31+ CLAN, FRIENDS, ACCOUNT, LOGOUT, OPTIONS, EMOTES, MUSIC, NONE
2032 );
2133
2234(*
@@ -26,9 +38,10 @@ Main record responsible for handling the gametabs.
2638*)
2739 TRSGameTabs = record
2840 Bounds: TBox;
29- {%codetools off}
3041 Tabs: TBoxArray;
31- {%codetools on}
42+ Keybinds: array [ERSGameTab] of EKeyCode;
43+ KeybindsEnabled: Boolean;
44+ AvailableKeys: EKeycodeArray;
3245 const ActiveColor: TColorTolerance = [$14194A, 1.235, EColorSpace.HSL, [1.631, 1.084, 0.286]];
3346 end;
3447
@@ -37,7 +50,9 @@ Main record responsible for handling the gametabs.
3750```pascal
3851procedure TRSGameTabs.SetupInterface();
3952```
40- Internal method automatically called for you responsible for setting up coordinates.
53+ Internal method responsible for setting up coordinates for the {ref}`TRSGameTabs`.
54+
55+ This is automatically called for you on the {ref}`GameTabs variable`.
4156*)
4257procedure TRSGameTabs.SetupInterface();
4358var
89104 if Target.Width >= 948 then
90105 begin
91106 Self.Tabs := TBoxArray.Create(Self.Bounds.BottomLeft.Offset(0,-35), 14, 1, 30, 33, [3, 0]);
92- for tab := ERSGameTab.MUSIC downto ERSGameTab.FRIENDS - 1 do
93- Self.Tabs[tab-1 ] := Self.Tabs[tab-2 ];
107+ for tab := ERSGameTab.MUSIC downto ERSGameTab.FRIENDS do
108+ Self.Tabs[tab] := Self.Tabs[tab-1 ];
94109 end
95110 else // Two rows
96111 Self.Tabs := TBoxArray.Create(Self.Bounds.BottomLeft.Offset(0,-70), 7, 2, 30, 33, [3, 3]);
@@ -105,24 +120,6 @@ begin
105120 end;
106121end;
107122
108-
109- (*
110- ## GameTabs.Get
111- ```pascal
112- function TRSGameTabs.Get(tab: ERSGameTab): TBox;
113- ```
114- Get a GameTab button bounds.
115-
116- Example:
117- ```pascal
118- ShowOnClient(GameTabs.Get(ERSGameTab.INVENTORY));
119- ```
120- *)
121- function TRSGameTabs.Get(tab: ERSGameTab): TBox;
122- begin
123- Result := Self.Tabs[tab-1];
124- end;
125-
126123(*
127124## GameTabs.GetCurrent
128125```pascal
@@ -142,9 +139,10 @@ begin
142139 if (RSClient.Mode = ERSMode.RESIZABLE_MODERN) and (Target.CountColor(8639715, 0, Self.Tabs[ERSGameTab.LOGOUT-1]) > 0) then
143140 Exit(ERSGameTab.LOGOUT);
144141
145- for tab := ERSGameTab.COMBAT to High( ERSGameTab) do
146- if (Target.CountColor(Self.ActiveColor, Self.Tabs[tab-1 ]) > 50) then
142+ for tab := ERSGameTab.COMBAT to ERSGameTab.MUSIC do
143+ if (Target.CountColor(Self.ActiveColor, Self.Tabs[tab]) > 50) then
147144 Exit(tab);
145+ Result := ERSGameTab.NONE;
148146end;
149147
150148
@@ -184,25 +182,90 @@ begin
184182 Result := SleepUntil(Self.IsOpen(tab), interval, time);
185183end;
186184
185+
186+ (*
187+ ## GameTabs.FKeyOpen
188+ ```pascal
189+ function TRSGameTabs.FKeyOpen(tab: ERSGameTab): Boolean;
190+ ```
191+ Attempts to open the specified `tab` gametab with an FKey.
192+ This works by randomly using FKeys that haven't been tried yet and slowly
193+ caching which gametabs they open. At first most times this is used it won't
194+ open the `tab` specified, but as more EKeyCode/ERSGameTab pairs get mapped
195+ this starts getting them right.
196+
197+ Example:
198+ ```pascal
199+ WriteLn GameTabs.FKeyOpen(ERSGameTab.INVENTORY);
200+ ```
201+ *)
202+ function TRSGameTabs.FKeyOpen(tab: ERSGameTab): Boolean;
203+ var
204+ current, new: ERSGameTab;
205+ i: Integer;
206+ switched: Boolean;
207+ begin
208+ {$IFDEF SRLT_KEYBINDS_DEBUG}
209+ for current := ERSGameTab.COMBAT to ERSGameTab.MUSIC do
210+ WriteLn GetDebugLn('GameTabs', 'Keybinds -> ' + ToStr(current) + ' -> ' ToStr(Self.Keybinds[current]));
211+ {$ENDIF}
212+
213+ if Self.Keybinds[tab] <> EKeyCode.UNKNOWN then
214+ begin
215+ Keyboard.PressKey(Self.Keybinds[tab]);
216+ Exit(Self.WaitOpen(tab, 2000));
217+ end;
218+
219+ if Self.AvailableKeys = [] then Exit; //all keys are already setup and there's no match
220+ i := Random(0, High(Self.AvailableKeys));
221+
222+ current := Self.GetCurrent();
223+ Keyboard.PressKey(Self.AvailableKeys[i]);
224+
225+ switched := SleepUntil(current <> (new := Self.GetCurrent()), RandomMode(100, 50, 1500), 600);
226+
227+ if switched then //if tab switched we are sure of this key-gametab pair
228+ begin
229+ Self.Keybinds[new] := Self.AvailableKeys[i];
230+ Delete(Self.AvailableKeys, i, 1);
231+ end
232+ else if Self.Keybinds[new] <> EKeyCode.UNKNOWN then
233+ Delete(Self.AvailableKeys, i, 1); //This key has no pair
234+
235+ Result := new = Tab;
236+ end;
237+
238+
187239(*
188240## GameTabs.Open
189241```pascal
190- function TRSGameTabs.Open(tab: ERSGameTab): Boolean;
242+ function TRSGameTabs.Open(tab: ERSGameTab; fkeyProbability: Single = -1 ): Boolean;
191243```
192244Attempts to open the specified `tab` gametab.
245+ If `TRSGameTabs.KeybindsEnabled` is true, we might attempt to open the tab
246+ with {ref}`TRSGameTabs.FKeyOpen`, subject to `fkeyProbability`.
193247
194248Example:
195249```pascal
196250WriteLn GameTabs.Open(ERSGameTab.INVENTORY);
197251```
198252*)
199- function TRSGameTabs.Open(tab: ERSGameTab): Boolean;
253+ function TRSGameTabs.Open(tab: ERSGameTab; fkeyProbability: Single = -1 ): Boolean;
200254begin
201- if Self.IsOpen(tab) then Exit(True);
255+ if Self.IsOpen(tab) then
256+ Exit(True);
257+
258+ if Self.KeybindsEnabled then
259+ begin
260+ if fkeyProbability = -1 then fkeyProbability := Biometrics.RandomDouble(0.8);
261+
262+ if RandomBoolean(fkeyProbability) and Self.FKeyOpen(tab) then
263+ Exit(True);
264+ end;
202265
203266 for 1 to 3 do
204267 begin
205- Mouse.Click(Self.Tabs[tab-1 ], EMouseButton.LEFT);
268+ Mouse.Click(Self.Tabs[tab], EMouseButton.LEFT);
206269 if Self.WaitOpen(tab, 2000) then Exit(True);
207270 end;
208271end;
@@ -215,4 +278,18 @@ begin
215278end;
216279
217280var
281+ (*
282+ (GameTabs variable)=
283+ ## var GameTabs
284+ Global {ref}`TRSGameTabs` variable.
285+ *)
218286 GameTabs: TRSGameTabs;
287+
288+ begin
289+ GameTabs.KeybindsEnabled := True;
290+ GameTabs.AvailableKeys := [
291+ EKeyCode.ESCAPE, EKeyCode.F1, EKeyCode.F2, EKeyCode.F3, EKeyCode.F4,
292+ EKeyCode.F5, EKeyCode.F6, EKeyCode.F7, EKeyCode.F8, EKeyCode.F9,
293+ EKeyCode.F10, EKeyCode.F11, EKeyCode.F12
294+ ];
295+ end;
0 commit comments