1 /* 2 * Copyright 2007-2009, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ryan Leavengood 7 */ 8 9 10 #include <ScreenSaver.h> 11 #include <View.h> 12 #include <StringView.h> 13 #include <Font.h> 14 15 16 const rgb_color kMediumBlue = {0, 0, 100}; 17 const rgb_color kWhite = {255, 255, 255}; 18 19 20 // Inspired by the classic BeOS screensaver, of course 21 class DebugNow : public BScreenSaver 22 { 23 public: 24 DebugNow(BMessage *archive, image_id); 25 void Draw(BView *view, int32 frame); 26 void StartConfig(BView *view); 27 status_t StartSaver(BView *view, bool preview); 28 29 private: 30 const char *fLine1; 31 const char *fLine2; 32 BPoint fLine1Start; 33 BPoint fLine2Start; 34 }; 35 36 37 BScreenSaver *instantiate_screen_saver(BMessage *msg, image_id image) 38 { 39 return new DebugNow(msg, image); 40 } 41 42 43 DebugNow::DebugNow(BMessage *archive, image_id id) 44 : BScreenSaver(archive, id) 45 , fLine1("DEBUG") 46 , fLine2("NOW") 47 { 48 } 49 50 51 void 52 DebugNow::StartConfig(BView *view) 53 { 54 view->AddChild(new BStringView(BRect(20, 10, 200, 35), "", "DEBUG NOW, " 55 "by Ryan Leavengood")); 56 } 57 58 59 status_t 60 DebugNow::StartSaver(BView *view, bool preview) 61 { 62 float width = view->Bounds().Width(); 63 float height = view->Bounds().Height(); 64 65 BFont font; 66 view->GetFont(&font); 67 font.SetSize(height / 2.5); 68 view->SetFont(&font); 69 70 BRect rect; 71 escapement_delta delta; 72 delta.nonspace = 0; 73 delta.space = 0; 74 // If anyone has suggestions for how to clean this up, speak up 75 font.GetBoundingBoxesForStrings(&fLine1, 1, B_SCREEN_METRIC, &delta, &rect); 76 float y = ((height - (rect.Height() * 2 + height / 10)) / 2) + rect.Height(); 77 fLine1Start.Set((width - rect.Width()) / 2, y); 78 font.GetBoundingBoxesForStrings(&fLine2, 1, B_SCREEN_METRIC, &delta, &rect); 79 fLine2Start.Set((width - rect.Width()) / 2, y + rect.Height() + height / 10); 80 81 return B_OK; 82 } 83 84 85 void 86 DebugNow::Draw(BView *view, int32 frame) 87 { 88 if (frame == 0) { 89 // fill with blue on first frame 90 view->SetLowColor(kMediumBlue); 91 view->FillRect(view->Bounds(), B_SOLID_LOW); 92 93 // Set tick size to 500,000 microseconds = 0.5 second 94 SetTickSize(500000); 95 } else { 96 // Drawing the background color every other frame to make the text blink 97 if (frame % 2 == 1) 98 view->SetHighColor(kWhite); 99 else 100 view->SetHighColor(kMediumBlue); 101 102 view->DrawString(fLine1, fLine1Start); 103 view->DrawString(fLine2, fLine2Start); 104 } 105 } 106 107