1 /* 2 * Copyright 2020, 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 12 13 BReference<SharedBitmap> RatingUtils::sStarBlueBitmap 14 = BReference<SharedBitmap>(new SharedBitmap(RSRC_STAR_BLUE)); 15 BReference<SharedBitmap> RatingUtils::sStarGrayBitmap 16 = BReference<SharedBitmap>(new SharedBitmap(RSRC_STAR_GREY)); 17 18 19 /*static*/ void 20 RatingUtils::Draw(BView* target, BPoint at, float value) 21 { 22 const BBitmap* star; 23 24 if (value < RATING_MIN) 25 star = sStarGrayBitmap->Bitmap(BITMAP_SIZE_16); 26 else 27 star = sStarBlueBitmap->Bitmap(BITMAP_SIZE_16); 28 29 if (star == NULL) { 30 debugger("no star icon found in application resources."); 31 return; 32 } 33 34 Draw(target, at, value, star); 35 } 36 37 38 /*static*/ void 39 RatingUtils::Draw(BView* target, BPoint at, float value, 40 const BBitmap* star) 41 { 42 BRect rect = BOUNDS_RATING; 43 rect.OffsetBy(at); 44 45 // a rectangle covering the whole area of the stars 46 target->FillRect(rect, B_SOLID_LOW); 47 48 if (star == NULL) { 49 debugger("no star icon found in application resources."); 50 return; 51 } 52 53 target->SetDrawingMode(B_OP_OVER); 54 55 float x = 0; 56 for (int i = 0; i < 5; i++) { 57 target->DrawBitmap(star, at + BPoint(x, 0)); 58 x += SIZE_RATING_STAR + WIDTH_RATING_STAR_SPACING; 59 } 60 61 if (value >= RATING_MIN && value < 5.0f) { 62 target->SetDrawingMode(B_OP_OVER); 63 64 rect = BOUNDS_RATING; 65 rect.right = x - 2; 66 rect.left = ceilf(rect.left + (value / 5.0f) * rect.Width()); 67 rect.OffsetBy(at); 68 69 rgb_color color = target->LowColor(); 70 color.alpha = 190; 71 target->SetHighColor(color); 72 73 target->SetDrawingMode(B_OP_ALPHA); 74 target->FillRect(rect, B_SOLID_HIGH); 75 } 76 }