xref: /haiku/src/add-ons/screen_savers/spider/SpiderView.cpp (revision 837d7501ef1d85b5205e1eadcd2e7a229ff7818e)
1 /*
2  * Copyright 2007-2014 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus, superstippi@gmx.de
7  */
8 
9 
10 #include "SpiderView.h"
11 
12 #include <Catalog.h>
13 #include <ColorMenuItem.h>
14 #include <ControlLook.h>
15 #include <MenuField.h>
16 #include <Message.h>
17 #include <PopUpMenu.h>
18 #include <Slider.h>
19 #include <StringView.h>
20 
21 #include "SpiderSaver.h"
22 
23 
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "SpiderView"
26 
27 
28 static const uint32 kMsgQueueNumber		= 'qunm';
29 static const uint32 kMsgPolyNumber		= 'plnm';
30 static const uint32 kMsgQueueDepth		= 'qudp';
31 static const uint32 kMsgColor			= 'colr';
32 
33 
34 //	#pragma - SpiderView
35 
36 
SpiderView(BRect frame,SpiderSaver * saver,uint32 queueNumber,uint32 maxPolyPoints,uint32 maxQueueDepth,uint32 color)37 SpiderView::SpiderView(BRect frame, SpiderSaver* saver, uint32 queueNumber,
38 	uint32 maxPolyPoints, uint32 maxQueueDepth, uint32 color)
39 	:
40 	BView(frame, "spider view", B_FOLLOW_NONE, B_WILL_DRAW | B_PULSE_NEEDED),
41 	fSaver(saver)
42 {
43 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
44 
45 	frame.OffsetTo(0.0, 0.0);
46 	frame.InsetBy(10.0, 5.0);
47 
48 	float viewHeight = floorf(frame.Height() / 5.0);
49 
50 	// title stuff
51 	font_height fh;
52 	be_bold_font->GetHeight(&fh);
53 	float fontHeight = fh.ascent + fh.descent + 5.0;
54 	frame.bottom = frame.top + fontHeight;
55 	BStringView* title = new BStringView(frame, B_EMPTY_STRING,
56 		B_TRANSLATE("Spider by stippi"));
57 	title->SetFont(be_bold_font);
58 	AddChild(title);
59 
60 	be_plain_font->GetHeight(&fh);
61 	fontHeight = fh.ascent + fh.descent + 5.0;
62 	frame.top = frame.bottom;
63 	frame.bottom = frame.top + fontHeight;
64 	title = new BStringView(frame, B_EMPTY_STRING,
65 		B_TRANSLATE("for  bonefish"));
66 	BFont font(be_plain_font);
67 	font.SetShear(110.0);
68 	title->SetFont(&font);
69 	title->SetAlignment(B_ALIGN_CENTER);
70 	AddChild(title);
71 
72 	// controls
73 	frame.top = 10.0f;
74 	frame.bottom = frame.top + viewHeight;
75 
76 	// queue slider
77 	frame.OffsetBy(0.0f, viewHeight);
78 	fQueueNumberSlider = new BSlider(frame, "queue number",
79 		B_TRANSLATE("Max. polygon count"), new BMessage(kMsgQueueNumber),
80 		1, MAX_QUEUE_NUMBER);
81 	fQueueNumberSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
82 	fQueueNumberSlider->SetHashMarkCount((MAX_QUEUE_NUMBER - 1) / 2 + 1);
83 	fQueueNumberSlider->SetValue(queueNumber);
84 	AddChild(fQueueNumberSlider);
85 
86 	// poly sliders
87 	frame.OffsetBy(0.0f, viewHeight);
88 	fPolyNumberSlider = new BSlider(frame, "poly points",
89 		B_TRANSLATE("Max. points per polygon"), new BMessage(kMsgPolyNumber),
90 		MIN_POLY_POINTS, MAX_POLY_POINTS);
91 	fPolyNumberSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
92 	fPolyNumberSlider->SetHashMarkCount(MAX_POLY_POINTS - MIN_POLY_POINTS + 1);
93 	fPolyNumberSlider->SetValue(maxPolyPoints);
94 	AddChild(fPolyNumberSlider);
95 
96 	// queue depth slider
97 	frame.OffsetBy(0.0f, viewHeight);
98 	fQueueDepthSlider = new BSlider(frame, "queue depth",
99 		B_TRANSLATE("Trail depth"), new BMessage(kMsgQueueDepth),
100 		MIN_QUEUE_DEPTH, MAX_QUEUE_DEPTH);
101 	fQueueDepthSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
102 	fQueueDepthSlider->SetHashMarkCount(
103 		(MAX_QUEUE_DEPTH - MIN_QUEUE_DEPTH) / 4 + 1);
104 	fQueueDepthSlider->SetValue(maxQueueDepth);
105 	AddChild(fQueueDepthSlider);
106 
107 	// color menu field
108 	BPopUpMenu* menu = new BPopUpMenu(B_TRANSLATE("Color"));
109 	BColorMenuItem* item;
110 	BMessage* message;
111 
112 	// red
113 	message = new BMessage(kMsgColor);
114 	message->AddInt32("color", RED);
115 	item = new BColorMenuItem(B_TRANSLATE("Red"), message,
116 		(rgb_color){ 255, 65, 54 });
117 	item->SetMarked(color == RED);
118 	menu->AddItem(item);
119 
120 	// green
121 	message = new BMessage(kMsgColor);
122 	message->AddInt32("color", GREEN);
123 	item = new BColorMenuItem(B_TRANSLATE("Green"), message,
124 		(rgb_color){ 46, 204, 64 });
125 	item->SetMarked(color == GREEN);
126 	menu->AddItem(item);
127 
128 	// blue
129 	message = new BMessage(kMsgColor);
130 	message->AddInt32("color", BLUE);
131 	item = new BColorMenuItem(B_TRANSLATE("Blue"), message,
132 		(rgb_color){ 0, 116, 217 });
133 	item->SetMarked(color == BLUE);
134 	menu->AddItem(item);
135 
136 	// yellow
137 	message = new BMessage(kMsgColor);
138 	message->AddInt32("color", YELLOW);
139 	item = new BColorMenuItem(B_TRANSLATE("Yellow"), message,
140 		(rgb_color){ 255, 220, 0 });
141 	item->SetMarked(color == YELLOW);
142 	menu->AddItem(item);
143 
144 	// purple
145 	message = new BMessage(kMsgColor);
146 	message->AddInt32("color", PURPLE);
147 	item = new BColorMenuItem(B_TRANSLATE("Purple"), message,
148 		(rgb_color){ 177, 13, 201 });
149 	item->SetMarked(color == PURPLE);
150 	menu->AddItem(item);
151 
152 	// cyan
153 	message = new BMessage(kMsgColor);
154 	message->AddInt32("color", CYAN);
155 	item = new BColorMenuItem(B_TRANSLATE("Cyan"), message,
156 		(rgb_color){ 127, 219, 255 });
157 	item->SetMarked(color == CYAN);
158 	menu->AddItem(item);
159 
160 	// gray
161 	message = new BMessage(kMsgColor);
162 	message->AddInt32("color", GRAY);
163 	item = new BColorMenuItem(B_TRANSLATE("Gray"), message,
164 		(rgb_color){ 170, 170, 170 });
165 	item->SetMarked(color == GRAY);
166 	menu->AddItem(item);
167 
168 	frame.OffsetBy(0.0f, viewHeight);
169 	fColorMenuField = new BMenuField( frame, "color", B_TRANSLATE("Color"),
170 		menu);
171 	fColorMenuField->SetDivider(fColorMenuField->StringWidth(
172 		B_TRANSLATE("Color")) + be_control_look->DefaultItemSpacing());
173 	AddChild(fColorMenuField);
174 }
175 
176 
~SpiderView()177 SpiderView::~SpiderView()
178 {
179 }
180 
181 
182 void
AttachedToWindow()183 SpiderView::AttachedToWindow()
184 {
185 	fQueueNumberSlider->SetTarget(this);
186 	fPolyNumberSlider->SetTarget(this);
187 	fQueueDepthSlider->SetTarget(this);
188 	fColorMenuField->Menu()->SetTargetForItems(this);
189 }
190 
191 
192 void
MessageReceived(BMessage * message)193 SpiderView::MessageReceived(BMessage* message)
194 {
195 	switch (message->what) {
196 		case kMsgQueueNumber:
197 			fSaver->SetQueueNumber(fQueueNumberSlider->Value());
198 			break;
199 
200 		case kMsgPolyNumber:
201 			fSaver->SetPolyPoints(fPolyNumberSlider->Value());
202 			break;
203 
204 		case kMsgQueueDepth:
205 			fSaver->SetQueueDepth(fQueueDepthSlider->Value());
206 			break;
207 
208 		case kMsgColor: {
209 			uint32 color;
210 			if (message->FindInt32("color", (int32*)&color) == B_OK)
211 				fSaver->SetColor(color);
212 
213 			break;
214 		}
215 
216 		default:
217 			BView::MessageReceived(message);
218 	}
219 }
220