xref: /haiku/src/apps/haikudepot/util/RatingUtils.cpp (revision 4b3c808edaaf626e7ee6cb0ee9d21dc96b173ee6)
19883929bSAndrew Lindesay /*
266ee6532SAndrew Lindesay  * Copyright 2020-2024, Andrew Lindesay <apl@lindesay.co.nz>.
39883929bSAndrew Lindesay  * All rights reserved. Distributed under the terms of the MIT License.
49883929bSAndrew Lindesay  */
59883929bSAndrew Lindesay 
69883929bSAndrew Lindesay #include <View.h>
79883929bSAndrew Lindesay 
89883929bSAndrew Lindesay 
99883929bSAndrew Lindesay #include "HaikuDepotConstants.h"
10c3cad236SAndrew Lindesay #include "Logger.h"
119883929bSAndrew Lindesay #include "RatingUtils.h"
1266ee6532SAndrew Lindesay #include "SharedIcons.h"
139883929bSAndrew Lindesay 
149883929bSAndrew Lindesay 
RatingStarsMetrics(BSize starSize)15c3cad236SAndrew Lindesay RatingStarsMetrics::RatingStarsMetrics(BSize starSize)
16c3cad236SAndrew Lindesay 	:
17c3cad236SAndrew Lindesay 	fStarSize(starSize)
18c3cad236SAndrew Lindesay {
19c3cad236SAndrew Lindesay }
20c3cad236SAndrew Lindesay 
21c3cad236SAndrew Lindesay 
22c3cad236SAndrew Lindesay const BSize
StarSize() const23c3cad236SAndrew Lindesay RatingStarsMetrics::StarSize() const
24c3cad236SAndrew Lindesay {
25c3cad236SAndrew Lindesay 	return fStarSize;
26c3cad236SAndrew Lindesay }
27c3cad236SAndrew Lindesay 
28c3cad236SAndrew Lindesay 
29c3cad236SAndrew Lindesay float
SpacingBetweenStars() const30c3cad236SAndrew Lindesay RatingStarsMetrics::SpacingBetweenStars() const
31c3cad236SAndrew Lindesay {
32c3cad236SAndrew Lindesay 	return 2.0 * fStarSize.Width() / 16.0;
33c3cad236SAndrew Lindesay }
34c3cad236SAndrew Lindesay 
35c3cad236SAndrew Lindesay 
36c3cad236SAndrew Lindesay const BPoint
LocationOfStarAtIndex(int index) const37c3cad236SAndrew Lindesay RatingStarsMetrics::LocationOfStarAtIndex(int index) const
38c3cad236SAndrew Lindesay {
39c3cad236SAndrew Lindesay 	float indexf = static_cast<float>(index);
40c3cad236SAndrew Lindesay 	return BPoint(indexf * (fStarSize.Width() + SpacingBetweenStars()), 0.0);
41c3cad236SAndrew Lindesay }
42c3cad236SAndrew Lindesay 
43c3cad236SAndrew Lindesay 
44c3cad236SAndrew Lindesay const BSize
Size() const45c3cad236SAndrew Lindesay RatingStarsMetrics::Size() const
46c3cad236SAndrew Lindesay {
47c3cad236SAndrew Lindesay 	return BSize((fStarSize.Width() * 5) + (SpacingBetweenStars() * 4), fStarSize.Height());
48c3cad236SAndrew Lindesay }
49c3cad236SAndrew Lindesay 
50c3cad236SAndrew Lindesay 
519883929bSAndrew Lindesay /*static*/ void
Draw(BView * target,BPoint at,float value)529883929bSAndrew Lindesay RatingUtils::Draw(BView* target, BPoint at, float value)
539883929bSAndrew Lindesay {
549883929bSAndrew Lindesay 	const BBitmap* star;
559883929bSAndrew Lindesay 
569883929bSAndrew Lindesay 	if (value < RATING_MIN)
5766ee6532SAndrew Lindesay 		star = SharedIcons::IconStarGrey16Scaled()->Bitmap();
589883929bSAndrew Lindesay 	else
5966ee6532SAndrew Lindesay 		star = SharedIcons::IconStarBlue16Scaled()->Bitmap();
609883929bSAndrew Lindesay 
619883929bSAndrew Lindesay 	if (star == NULL) {
629883929bSAndrew Lindesay 		debugger("no star icon found in application resources.");
639883929bSAndrew Lindesay 		return;
649883929bSAndrew Lindesay 	}
659883929bSAndrew Lindesay 
669883929bSAndrew Lindesay 	Draw(target, at, value, star);
679883929bSAndrew Lindesay }
689883929bSAndrew Lindesay 
699883929bSAndrew Lindesay 
709883929bSAndrew Lindesay /*static*/ void
Draw(BView * target,BPoint at,float value,const BBitmap * star)719883929bSAndrew Lindesay RatingUtils::Draw(BView* target, BPoint at, float value,
729883929bSAndrew Lindesay 	const BBitmap* star)
739883929bSAndrew Lindesay {
749883929bSAndrew Lindesay 	if (star == NULL) {
75c3cad236SAndrew Lindesay 		HDFATAL("no star icon found in application resources.");
769883929bSAndrew Lindesay 		return;
779883929bSAndrew Lindesay 	}
789883929bSAndrew Lindesay 
79c3cad236SAndrew Lindesay 	RatingStarsMetrics metrics(star->Bounds().Size());
80c3cad236SAndrew Lindesay 	BRect rect(at, metrics.Size());
81c3cad236SAndrew Lindesay 
82c3cad236SAndrew Lindesay 	target->FillRect(rect, B_SOLID_LOW);
83c3cad236SAndrew Lindesay 		// a rectangle covering the whole area of the stars
84c3cad236SAndrew Lindesay 
859883929bSAndrew Lindesay 	target->SetDrawingMode(B_OP_OVER);
869883929bSAndrew Lindesay 
87c3cad236SAndrew Lindesay 	for (int i = 0; i < 5; i++)
88c3cad236SAndrew Lindesay 		target->DrawBitmap(star, rect.LeftTop() + metrics.LocationOfStarAtIndex(i));
899883929bSAndrew Lindesay 
909883929bSAndrew Lindesay 	if (value >= RATING_MIN && value < 5.0f) {
919883929bSAndrew Lindesay 		target->SetDrawingMode(B_OP_OVER);
92c3cad236SAndrew Lindesay 		BRect shadeOverRect = rect;
93c3cad236SAndrew Lindesay 		shadeOverRect.left = ceilf(rect.right - (1.0 - (value / 5.0f)) * rect.Width());
949883929bSAndrew Lindesay 
959883929bSAndrew Lindesay 		rgb_color color = target->LowColor();
969883929bSAndrew Lindesay 		color.alpha = 190;
979883929bSAndrew Lindesay 		target->SetHighColor(color);
989883929bSAndrew Lindesay 
999883929bSAndrew Lindesay 		target->SetDrawingMode(B_OP_ALPHA);
100c3cad236SAndrew Lindesay 		target->FillRect(shadeOverRect, B_SOLID_HIGH);
1019883929bSAndrew Lindesay 	}
1029883929bSAndrew Lindesay }
103*4b3c808eSAndrew Lindesay 
104*4b3c808eSAndrew Lindesay 
105*4b3c808eSAndrew Lindesay /*!	With the `userRatingInfo` provided, does it make sense for the application
106*4b3c808eSAndrew Lindesay 	to attempt to download the user ratings? If it looks like there are none
107*4b3c808eSAndrew Lindesay 	then it's making no sense and if it has already downloaded some then it
108*4b3c808eSAndrew Lindesay 	also does not make any sense.
109*4b3c808eSAndrew Lindesay */
110*4b3c808eSAndrew Lindesay /*static*/ bool
ShouldTryPopulateUserRatings(UserRatingInfoRef userRatingInfo)111*4b3c808eSAndrew Lindesay RatingUtils::ShouldTryPopulateUserRatings(UserRatingInfoRef userRatingInfo)
112*4b3c808eSAndrew Lindesay {
113*4b3c808eSAndrew Lindesay 	if (!userRatingInfo.IsSet())
114*4b3c808eSAndrew Lindesay 		return true;
115*4b3c808eSAndrew Lindesay 
116*4b3c808eSAndrew Lindesay 	UserRatingSummaryRef summary = userRatingInfo->Summary();
117*4b3c808eSAndrew Lindesay 
118*4b3c808eSAndrew Lindesay 	if (!summary.IsSet())
119*4b3c808eSAndrew Lindesay 		return true;
120*4b3c808eSAndrew Lindesay 
121*4b3c808eSAndrew Lindesay 	if (summary->RatingCount() == 0)
122*4b3c808eSAndrew Lindesay 		return false;
123*4b3c808eSAndrew Lindesay 
124*4b3c808eSAndrew Lindesay 	return !userRatingInfo->UserRatingsPopulated();
125*4b3c808eSAndrew Lindesay }
126