Skip to content

Commit 8c4f977

Browse files
authored
Merge pull request #139 from jacz24/trsmake
Added documentation and fail safes
2 parents 5a42957 + e6b8f6e commit 8c4f977

File tree

1 file changed

+65
-31
lines changed

1 file changed

+65
-31
lines changed

osrs/interfaces/chat/make.simba

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ begin
180180
end;
181181

182182
(*
183-
## Make.CreateDynamicQuantityButtons
183+
## Make.GetQuantityButtons()
184184
```pascal
185-
function TRSMake.CreateDynamicQuantityButtons(): array of TRSMakeQuantityButton;
185+
function TRSMake.GetQuantityButtons(): array of TRSMakeQuantityButton;
186186
```
187187
Returns the visible quantity buttons as a array of TRSMakeQuantityButton.
188188

189189
Example:
190190
```pascal
191-
WriteLn Make.CreateDynamicQuantityButtons();
191+
WriteLn Make.GetQuantityButtons();
192192
```
193193
*)
194194
function TRSMake.GetQuantityButtons(): array of TRSMakeQuantityButton;
@@ -221,11 +221,22 @@ end;
221221
```pascal
222222
function TRSMake.FindQuantityButtons(): array of TRSMakeQuantityButton;
223223
```
224-
TODO..
224+
Attempts to find and return all quantity buttons in the Make interface.
225+
This is a safe wrapper around `GetQuantityButtons()` that first verifies
226+
the Make interface is open before attempting to locate the buttons.
227+
228+
Returns an empty array if the Make interface is not currently open.
225229

226230
Example:
227231
```pascal
228-
TODO
232+
var
233+
buttons: array of TRSMakeQuantityButton;
234+
i: Integer;
235+
begin
236+
buttons := Make.FindQuantityButtons();
237+
for i := 0 to High(buttons) do
238+
WriteLn('Button ', i, ' bounds: ', buttons[i].Button.Bounds);
239+
end;
229240
```
230241
*)
231242
function TRSMake.FindQuantityButtons(): array of TRSMakeQuantityButton;
@@ -255,6 +266,9 @@ var
255266
i: Integer;
256267
begin
257268
btns := Self.FindQuantityButtons();
269+
if Length(btns) = 0 then
270+
Exit;
271+
258272
for i := 0 to High(btns) do
259273
if btns[i].Button.Enabled() then
260274
Exit(btns[i]);
@@ -280,6 +294,8 @@ var
280294
current: TRSMakeQuantityButton;
281295
begin
282296
current := Self.GetCurrentQuantityButton();
297+
if current = [] then
298+
Exit;
283299

284300
if current.Button.Bounds.Area = 0 then
285301
raise GetDebugLn('Make', 'Failed to get current quantity button');
@@ -411,27 +427,38 @@ are visible which you can do easily through the next methods.
411427
*)
412428

413429
(*
414-
### Make.IndexOfQuantity
430+
### Make.FindQuantityButton
415431
```pascal
416-
function TRSMake.IndexOfQuantity(amount: Integer): Integer;
432+
function TRSMake.FindQuantityButton(amount: Integer): TRSMakeQuantityButton;
417433
```
418-
Finds the index of a quantity button by the specified `amount`.
419-
If the `amount` we are looking for, the custom quantity button is returned,
420-
otherwise the result is `-1`.
434+
Finds and returns the TRSMakeQuantityButton matching the specified `amount`.
435+
436+
For standard quantities (1, 5, 10, All), returns the corresponding button directly.
437+
For custom amounts, prioritizes a button already set to that value, otherwise
438+
returns the "Other" (X) button which will prompt for manual entry.
439+
440+
Returns an empty record if no matching button is found or the Make interface is not open.
441+
442+
Note: Automatically closes any tooltip that may be covering the quantity buttons.
421443

422444
Example:
423445
```pascal
424-
WriteLn Make.IndexOfQuantity(Make.QUANTITY_ALL);
446+
var
447+
btn: TRSMakeQuantityButton;
448+
begin
449+
btn := Make.FindQuantityButton(5);
450+
if btn <> [] then
451+
WriteLn('Found button: ', btn);
452+
end;
425453
```
426454
*)
427-
function TRSMake.IndexOfQuantity(amount: Integer): Integer;
455+
function TRSMake.FindQuantityButton(amount: Integer): TRSMakeQuantityButton;
428456
var
429457
hintPt: TPoint;
430-
i: Integer;
458+
idx: Integer;
431459
quantity: ERSItemQuantity;
460+
quantitybtns: array of TRSMakeQuantityButton;
432461
begin
433-
if amount <= 0 then Exit(0);
434-
435462
if Self.HasHint() then
436463
begin
437464
hintPt := Self.GetHintBox().TopRight;
@@ -441,19 +468,25 @@ begin
441468
raise GetDebugLn('Make', 'Failed to close the tooltip which is covering the quanitty buttons');
442469
end;
443470

471+
quantitybtns := Self.FindQuantityButtons();
444472
quantity := ERSItemQuantity.Integer2Quantity(amount);
445473

446-
if quantity = ERSItemQuantity.CUSTOM then
474+
for idx := 0 to High(quantitybtns) do
447475
begin
448-
if amount = OCR.RecognizeNumber(Self.QuantityButtonBoxes[2], RSFonts.PLAIN_11, [Self.TEXT_COLOR, RSFonts.WHITE], 0) then
449-
Exit(2);
450-
Exit(1);
476+
if quantitybtns[idx].Quantity = quantity then
477+
begin
478+
if quantity = ERSItemQuantity.CUSTOM then
479+
begin
480+
if quantitybtns[idx].UpText = IntToStr(amount) then
481+
Exit(quantitybtns[idx]);
482+
if quantitybtns[idx].UpText = 'Other' then
483+
Exit(quantitybtns[idx]);
484+
end
485+
else
486+
Exit(quantitybtns[idx]);
487+
end;
451488
end;
452-
453-
for i := 1 to High(Self.QuantityButtonBoxes) do
454-
if amount = OCR.RecognizeNumber(Self.QuantityButtonBoxes[i], RSFonts.PLAIN_11, [Self.TEXT_COLOR, RSFonts.WHITE], 0) then
455-
Exit(i);
456-
Result := -1;
489+
Result := [];
457490
end;
458491

459492
(*
@@ -487,18 +520,19 @@ WriteLn Make.SetQuantity(Make.QUANTITY_ALL);
487520
*)
488521
function TRSMake.SetQuantity(amount: Integer): Boolean;
489522
var
490-
idx: Integer;
523+
quantityButton: TRSMakeQuantityButton;
491524
done: Boolean;
492525
begin
493-
idx := Self.IndexOfQuantity(amount);
494-
if idx < 0 then
526+
quantityButton := Self.FindQuantityButton(amount);
527+
if quantityButton = [] then
495528
raise GetDebugLn('Make', 'Quantity button for "' + ToStr(amount) + '" is not available.');
496529

497-
if Self.IsQuantitySelected(idx) then Exit(True);
530+
if quantityButton.Button.Enabled() then Exit(True);
531+
532+
quantityButton.Button.Click(EMouseButton.LEFT);
498533

499-
Mouse.Click(Self.QuantityButtonBoxes[idx], EMouseButton.LEFT);
500534
Result := SleepUntil(
501-
(done := Self.IsQuantitySelected(idx)) or
535+
(done := quantityButton.Button.Enabled()) or
502536
Chat.FindQuery('Enter amount', True), RandomMode(100, 50, 1500), 600
503537
);
504538
if done then Exit;
@@ -531,7 +565,7 @@ begin
531565
boxes[index].Y1 := boxes[index].Y2 + 1;
532566
boxes[index].Y2 += 15;
533567

534-
if OCR.Locate(boxes[index], 'Space', [Self.TEXT_COLOR], 0, RSFonts.PLAIN_11) < 0.95 then
568+
if OCR.Locate(boxes[index], 'Space', [Self.TEXT_COLOR], 0, RSFonts.PLAIN_11) >= 0.95 then
535569
Keyboard.KeyPress(EKeyCode.SPACE)
536570
else
537571
Keyboard.KeyPress(EKeyCode(Ord(EKeyCode.NUM_1) + index));

0 commit comments

Comments
 (0)