@@ -10,6 +10,26 @@ craft certain items in OldSchool RuneScape.
1010{$INCLUDE_ONCE WaspLib/osrs.simba}
1111
1212type
13+
14+ (*
15+ ## ERSMakeQuantity
16+ ```pascal
17+ ERSMakeQuantity = enum(ONE, FIVE, TEN, CUSTOM, X, ALL);
18+ ```
19+ Enum representing the make quantity buttons.
20+ *)
21+ ERSMakeQuantity = enum(ONE, FIVE, TEN, CUSTOM, X, ALL);
22+
23+ (*
24+ ## TRSMakeQuantityButton
25+ Helper record used to cache make menu buttons.
26+ *)
27+ TRSMakeQuantityButton = record
28+ ButtonType: ERSMakeQuantity;
29+ Button: TRSButton;
30+ QuantityUpText: String;
31+ end;
32+
1333(*
1434## TRSMakeItem
1535Helper record used to cache make menu items.
@@ -18,19 +38,22 @@ Helper record used to cache make menu items.
1838 Item: String;
1939 Index, Quantity: Integer;
2040 end;
21-
2241(*
2342## TRSMake
2443Main record used to interact with the {ref}`Make` menu.
2544*)
2645 TRSMake = record
2746 Items: array of TRSMakeItem;
47+ DynamicQuantityButtons: array of TRSMakeQuantityButton;
2848 QuantityButtons: TBoxArray;
2949 SelectedQuantity: Integer;
30- {%codetools on}
50+ QuantityArea: TBox;
51+ {%codetools off}
3152 _IsOpenHelper, _ItemsHelper: TBox;
3253 {%codetools on}
54+
3355 const TEXT_COLOR: TColor = $203040;
56+ const BORDER_COLOR: TColorTolerance = [$080707, 5, EColorSpace.RGB, [1.000, 1.000, 1.000]];
3457 end;
3558
3659(*
@@ -53,11 +76,12 @@ begin
5376
5477 Self.QuantityButtons := TBoxArray.Create([X2-228, Y1+15], 6, 1, 29, 29, [6,0]).Reversed();
5578 Self.QuantityButtons[0].X2 += 5;
79+ Self.QuantityArea := Self.QuantityButtons.Merge();
5680
5781 Self._ItemsHelper.X1 := X1 + 8;
5882 Self._ItemsHelper.Y1 := Y1 + 51;
5983 Self._ItemsHelper.X2 := X2 - 8;
60- Self._ItemsHelper.Y2 := Y2 - 19 ;
84+ Self._ItemsHelper.Y2 := Y2 - 14 ;
6185 end;
6286end;
6387
96120 Result := SleepUntil(Self.IsOpen(), interval, time);
97121end;
98122
99-
100123(*
101124## Make.GetItemBoxes
102125```pascal
117140 tpa: TPointArray;
118141 b: TBox;
119142begin
120- tpa := Target.FindColor($080707, 5 , Self._ItemsHelper);
143+ tpa := Target.FindColor(Self.BORDER_COLOR , Self._ItemsHelper);
121144 for tpa in tpa.Cluster(1.5) do
122145 begin
123146 b := tpa.Bounds();
@@ -126,6 +149,190 @@ begin
126149 end;
127150end;
128151
152+ (*
153+ ## Make.GetQuantityBoxes
154+ ```pascal
155+ function TRSMake.GetQuantityBoxes(): TBoxArray;
156+ ```
157+ Finds quantity button boundaries and returns them as a TBoxArray.
158+
159+ You have 2 ways of getting the quantity boxes, a static one:
160+ ```pascal
161+ ShowOnTarget(Make.QuantityButtons);
162+ ```
163+ ```{figure} ../../images/make_static_quantity_boxes.png
164+ The make static boxes.
165+ ```
166+
167+ And a dynamic one:
168+ ```pascal
169+ ShowOnTarget(Make.GetQuantityBoxes());
170+ ```
171+ ```{figure} ../../images/make_quantity_boxes.png
172+ The make "dynamic" boxes.
173+ ```
174+
175+ There are use cases for both, internally, `Make.GetQuantityBoxes` is usually used.
176+ *)
177+ function TRSMake.GetQuantityBoxes(): TBoxArray;
178+ var
179+ tpa: TPointArray;
180+ b: TBox;
181+ begin
182+ tpa := Target.FindColor(Self.BORDER_COLOR, Self.QuantityArea);
183+ for tpa in tpa.Cluster(1.5) do
184+ begin
185+ b := tpa.Bounds();
186+ if InRange(b.Height, 20, 40) and InRange(b.Width, 20, 40) then
187+ Result += b;
188+ end;
189+ Result := Result.SortByX(True);
190+ end;
191+
192+ (*
193+ ## Make.CreateDynamicQuantityButtons
194+ ```pascal
195+ function TRSMake.CreateDynamicQuantityButtons(): array [ERSMakeQuantity] of TRSButton;
196+ ```
197+ Returns the visible quantity buttons as a array of TRSMakeQuantityButton.
198+
199+ Example:
200+ ```pascal
201+ Writeln (Make.CreateDynamicQuantityButtons());
202+ ```
203+
204+ ```
205+ *)
206+
207+ function TRSMake.CreateDynamicQuantityButtons(): array of TRSMakeQuantityButton;
208+ var
209+ boxes: TBoxArray;
210+ i: Integer;
211+ quantityStr: String;
212+ btn: TRSMakeQuantityButton;
213+ begin
214+ boxes := Self.GetQuantityBoxes();
215+ SetLength(Result, Length(boxes));
216+
217+ for i := 0 to High(boxes) do
218+ begin
219+ quantityStr := OCR.Recognize(boxes[i], RSFonts.PLAIN_11, [Self.TEXT_COLOR, RSFonts.WHITE], 0);
220+
221+ btn.ButtonType := ERSMakeQuantity.String2Quantity(quantityStr);
222+ btn.Button.Bounds := boxes[i];
223+ btn.Button.EnabledColors := [[RSFonts.WHITE, 0]];
224+
225+ if quantityStr.Equals('X', True) then
226+ btn.QuantityUpText := 'Other'
227+ else
228+ btn.QuantityUpText := quantityStr;
229+
230+ Result[i] := btn;
231+ end;
232+ end;
233+
234+ (*
235+ ### ERSMakeQuantity.String2Quantity
236+ ```pascal
237+ function ERSMakeQuantity.String2Quantity(quantity: String): ERSMakeQuantity; static;
238+ ```
239+ Internal helper function to convert a string quantity into a ERSMakeQuantity.
240+
241+ Example:
242+ ```pascal
243+ WriteLn ERSMakeQuantity.String2Quantity('5');
244+ WriteLn ERSMakeQuantity.String2Quantity('All');
245+ ```
246+ *)
247+
248+ function ERSMakeQuantity.String2Quantity(quantity: String): ERSMakeQuantity; static;
249+ begin
250+ case quantity of
251+ '1' : Result := ERSMakeQuantity.ONE;
252+ '5' : Result := ERSMakeQuantity.FIVE;
253+ '10' : Result := ERSMakeQuantity.TEN;
254+ 'All': Result := ERSMakeQuantity.ALL;
255+ 'X' : Result := ERSMakeQuantity.X;
256+ else Result := ERSMakeQuantity.CUSTOM;
257+ end;
258+ end;
259+
260+ (*
261+ ## Make.FindDynamicQuantityButtons
262+ ```pascal
263+ function TRSMake.FindDynamicQuantityButtons(): Boolean;
264+ ```
265+ Updates and caches the dynamic quantity buttons. Only works when the interface is open and no hints are open.
266+ Returns True if successfully updated.
267+
268+ Example:
269+ ```pascal
270+ if Make.FindDynamicQuantityButtons() then
271+ WriteLn('Cached ', Length(Make.FindDynamicQuantityButtons), ' quantity buttons');
272+ ```
273+ *)
274+ function TRSMake.FindDynamicQuantityButtons(): Boolean;
275+ begin
276+ Result := False;
277+ if not Self.IsOpen() then
278+ Exit;
279+
280+ Self.DynamicQuantityButtons := Self.CreateDynamicQuantityButtons();
281+ end;
282+
283+ (*
284+ ### Make.GetCurrentQuantityButton
285+ ```pascal
286+ function TRSMake.GetCurrentQuantityButton(): Integer;
287+ ```
288+ Get the current active make quantity button.
289+
290+ Example:
291+ ```pascal
292+ current := Make.GetCurrentQuantityButton();
293+ WritLn current;
294+ ShowOnTarget(current.Button.Bounds);
295+ ```
296+ *)
297+ function TRSMake.GetCurrentQuantityButton(): TRSMakeQuantityButton;
298+ var
299+ i: Integer;
300+ begin
301+ Result := [];
302+ for i := 0 to High(Self.DynamicQuantityButtons) do
303+ if Self.DynamicQuantityButtons[i].Button.Enabled then
304+ Exit(Self.DynamicQuantityButtons[i]);
305+ end;
306+
307+ (*
308+ ### Make.GetCurrentQuantity
309+ ```pascal
310+ function TRSMake.GetCurrentQuantity(): Integer;
311+ ```
312+ Get the current active make quantity as an integer.
313+ If All button is current then returns -1.
314+
315+ Example:
316+ ```pascal
317+ current := Make.GetCurrentQuantityButton();
318+ WritLn current;
319+ ShowOnTarget(current.Button.Bounds);
320+ ```
321+ *)
322+ function TRSMake.GetCurrentQuantity(): Integer;
323+ var
324+ current: TRSMakeQuantityButton;
325+ begin
326+ current := Self.GetCurrentQuantityButton();
327+
328+ if current.Button.Bounds.Area = 0 then
329+ raise GetDebugLn('Make', 'Failed to get current quantity button');
330+
331+ if current.QuantityUpText.Equals('All', True) then
332+ Exit(-1);
333+
334+ Result := StrToInt(current.QuantityUpText);
335+ end;
129336
130337(*
131338## Make.HasHint
@@ -310,7 +517,6 @@ begin
310517 Result := Target.HasColor(RSFonts.WHITE, 0, 1, Self.QuantityButtons[idx]);
311518end;
312519
313-
314520(*
315521### Make.SetQuantity
316522```pascal
@@ -438,7 +644,6 @@ begin
438644 end;
439645end;
440646
441-
442647procedure TRSMake.Draw(img: TImage);
443648begin
444649 if not Self.IsOpen() then Exit;
@@ -447,6 +652,8 @@ begin
447652 img.DrawBox(Chat.Bounds);
448653 img.DrawColor := $FFFFFF;
449654 img.DrawBoxArray(Self.QuantityButtons, False);
655+ img.DrawColor := $0000FF;
656+ img.DrawBoxArray(Self.GetQuantityBoxes(), False);
450657 img.DrawColor := $00FF00;
451658 img.DrawBoxArray(Self.GetItemBoxes(), False);
452659 img.DrawColor := $FF0000;
0 commit comments