xref: /haiku/src/add-ons/screen_savers/debugnow/DebugNow.cpp (revision c90684742e7361651849be4116d0e5de3a817194)
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, leavengood@gmail.com
7  */
8 
9 
10 #include <Font.h>
11 #include <ScreenSaver.h>
12 #include <StringView.h>
13 #include <View.h>
14 
15 #include <BuildScreenSaverDefaultSettingsView.h>
16 
17 
18 const rgb_color kMediumBlue = {0, 0, 100};
19 const rgb_color kWhite = {255, 255, 255};
20 
21 
22 // Inspired by the classic BeOS BuyNow screensaver, of course
23 class DebugNow : public BScreenSaver
24 {
25 	public:
26 							DebugNow(BMessage *archive, image_id);
27 		void				Draw(BView *view, int32 frame);
28 		void				StartConfig(BView *view);
29 		status_t			StartSaver(BView *view, bool preview);
30 
31 	private:
32 		const char* 		fLine1;
33 		const char*			fLine2;
34 		BPoint				fLine1Start;
35 		BPoint				fLine2Start;
36 		escapement_delta	fDelta;
37 };
38 
39 
40 BScreenSaver* instantiate_screen_saver(BMessage *msg, image_id image)
41 {
42 	return new DebugNow(msg, image);
43 }
44 
45 
46 DebugNow::DebugNow(BMessage *archive, image_id id)
47 	:
48 	BScreenSaver(archive, id),
49 	fLine1("DEBUG"),
50 	fLine2("NOW")
51 {
52 }
53 
54 
55 void
56 DebugNow::StartConfig(BView *view)
57 {
58 	BPrivate::BuildScreenSaverDefaultSettingsView(view, "DEBUG NOW",
59 		"by Ryan Leavengood");
60 }
61 
62 
63 status_t
64 DebugNow::StartSaver(BView *view, bool preview)
65 {
66 	float viewWidth = view->Bounds().Width();
67 	float viewHeight = view->Bounds().Height();
68 
69 	BFont font;
70 	view->GetFont(&font);
71 	font.SetSize(viewHeight / 2.5);
72 	view->SetFont(&font);
73 
74 	fDelta.nonspace = 0;
75 	fDelta.space = 0;
76 	BRect stringRect;
77 	font.GetBoundingBoxesForStrings(&fLine1, 1, B_SCREEN_METRIC, &fDelta,
78 		&stringRect);
79 	float y = ((viewHeight - (stringRect.Height() * 2 + viewHeight / 10)) / 2)
80 		+ stringRect.Height();
81 	fLine1Start.Set(((viewWidth - stringRect.Width()) / 2) - stringRect.left, y);
82 	font.GetBoundingBoxesForStrings(&fLine2, 1, B_SCREEN_METRIC, &fDelta,
83 		&stringRect);
84 	fLine2Start.Set(((viewWidth - stringRect.Width()) / 2) - stringRect.left,
85 		y + stringRect.Height() + viewHeight / 10);
86 
87 	// Set tick size to 500,000 microseconds = 0.5 second
88 	SetTickSize(500000);
89 
90 	return B_OK;
91 }
92 
93 
94 void
95 DebugNow::Draw(BView *view, int32 frame)
96 {
97 	// On first frame set the low color to make the text rendering correct
98 	if (frame == 0)
99 		view->SetLowColor(kMediumBlue);
100 
101 	// Draw the background color every frame
102 	view->SetHighColor(kMediumBlue);
103 	view->FillRect(view->Bounds());
104 
105 	// Draw the text every other frame to make the it blink
106 	if (frame % 2 == 1) {
107 		view->SetHighColor(kWhite);
108 		view->DrawString(fLine1, fLine1Start, &fDelta);
109 		view->DrawString(fLine2, fLine2Start, &fDelta);
110 	}
111 }
112 
113