xref: /haiku/src/apps/haikudepot/util/RatingUtils.cpp (revision b8a45b3a2df2379b4301bf3bd5949b9a105be4ba)
1 /*
2  * Copyright 2020-2024, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include <View.h>
7 
8 
9 #include "HaikuDepotConstants.h"
10 #include "RatingUtils.h"
11 #include "SharedIcons.h"
12 
13 
14 /*static*/ void
15 RatingUtils::Draw(BView* target, BPoint at, float value)
16 {
17 	const BBitmap* star;
18 
19 	if (value < RATING_MIN)
20 		star = SharedIcons::IconStarGrey16Scaled()->Bitmap();
21 	else
22 		star = SharedIcons::IconStarBlue16Scaled()->Bitmap();
23 
24 	if (star == NULL) {
25 		debugger("no star icon found in application resources.");
26 		return;
27 	}
28 
29 	Draw(target, at, value, star);
30 }
31 
32 
33 /*static*/ void
34 RatingUtils::Draw(BView* target, BPoint at, float value,
35 	const BBitmap* star)
36 {
37 	BRect rect = BOUNDS_RATING;
38 	rect.OffsetBy(at);
39 
40 		// a rectangle covering the whole area of the stars
41 	target->FillRect(rect, B_SOLID_LOW);
42 
43 	if (star == NULL) {
44 		debugger("no star icon found in application resources.");
45 		return;
46 	}
47 
48 	target->SetDrawingMode(B_OP_OVER);
49 
50 	float x = 0;
51 	for (int i = 0; i < 5; i++) {
52 		target->DrawBitmap(star, at + BPoint(x, 0));
53 		x += SIZE_RATING_STAR + WIDTH_RATING_STAR_SPACING;
54 	}
55 
56 	if (value >= RATING_MIN && value < 5.0f) {
57 		target->SetDrawingMode(B_OP_OVER);
58 
59 		rect = BOUNDS_RATING;
60 		rect.right = x - 2;
61 		rect.left = ceilf(rect.left + (value / 5.0f) * rect.Width());
62 		rect.OffsetBy(at);
63 
64 		rgb_color color = target->LowColor();
65 		color.alpha = 190;
66 		target->SetHighColor(color);
67 
68 		target->SetDrawingMode(B_OP_ALPHA);
69 		target->FillRect(rect, B_SOLID_HIGH);
70 	}
71 }