xref: /haiku/src/add-ons/screen_savers/glife/GLifeConfig.cpp (revision f2762b2d5658b0b6c9ff62997a1485194cc77e7c)
1 /*
2  * Copyright 2012, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  * 		Aaron Hill <serac@hillvisions.com>
7  *		Alexander von Gluck <kallisti5@unixzen.com>
8  */
9 
10 
11 #include "GLifeConfig.h"
12 
13 #include <Catalog.h>
14 #include <GroupLayoutBuilder.h>
15 #include <Slider.h>
16 #include <stdio.h>
17 #include <String.h>
18 #include <StringView.h>
19 #include <View.h>
20 
21 #include "GLifeState.h"
22 
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "GLife ScreenSaver"
25 
26 
27 // ------------------------------------------------------
28 //  GLifeConfig Class Constructor Definition
29 GLifeConfig::GLifeConfig(BRect frame, GLifeState* pglsState)
30 	:
31 	BView(frame, "", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
32 	m_pglsState(pglsState)
33 {
34 	SetLayout(new BGroupLayout(B_HORIZONTAL));
35 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
36 
37 	// Info text
38 	BStringView* name = new BStringView(frame, B_EMPTY_STRING,
39 		B_TRANSLATE("OpenGL \"Game of Life\""), B_FOLLOW_LEFT);
40 	BStringView* author = new BStringView(frame, B_EMPTY_STRING,
41 		B_TRANSLATE("by Aaron Hill"), B_FOLLOW_LEFT);
42 
43 	// Sliders
44 	fGridDelay = new BSlider(frame, "GridDelay",
45 		B_TRANSLATE("Grid Life Delay: "),
46 		new BMessage(kGridDelay),
47 		0, 4, B_BLOCK_THUMB);
48 
49 	fGridDelay->SetHashMarks(B_HASH_MARKS_BOTTOM);
50 	fGridDelay->SetLimitLabels(B_TRANSLATE("None"), B_TRANSLATE_COMMENT("4x",
51 			"This is a factor: the x represents 'times'"));
52 	fGridDelay->SetValue(pglsState->GridDelay());
53 	fGridDelay->SetHashMarkCount(5);
54 
55 	fGridBorder = new BSlider(frame, "GridBorder",
56 		B_TRANSLATE("Grid Border: "),
57 		new BMessage(kGridBorder),
58 		0, 10, B_BLOCK_THUMB);
59 
60 	fGridBorder->SetHashMarks(B_HASH_MARKS_BOTTOM);
61 	fGridBorder->SetLimitLabels("0", "10");
62 	fGridBorder->SetValue(pglsState->GridBorder());
63 	fGridBorder->SetHashMarkCount(11);
64 
65 	fGridWidth = new BSlider(frame, "GridWidth",
66 		B_TRANSLATE("Grid Width: "),
67 		new BMessage(kGridWidth),
68 		10, 100, B_BLOCK_THUMB);
69 
70 	fGridWidth->SetHashMarks(B_HASH_MARKS_BOTTOM);
71 	//fGridWidth->SetLimitLabels("10", "100");
72 	fGridWidth->SetValue(pglsState->GridWidth());
73 	fGridWidth->SetHashMarkCount(10);
74 
75 	fGridHeight = new BSlider(frame, "GridHeight",
76 		B_TRANSLATE("Grid Height: "),
77 		new BMessage(kGridHeight),
78 		10, 100, B_BLOCK_THUMB);
79 
80 	fGridHeight->SetHashMarks(B_HASH_MARKS_BOTTOM);
81 	//fGridHeight->SetLimitLabels("10", "100");
82 	fGridHeight->SetValue(pglsState->GridHeight());
83 	fGridHeight->SetHashMarkCount(10);
84 
85 	AddChild(BGroupLayoutBuilder(B_VERTICAL, B_USE_DEFAULT_SPACING)
86 		.Add(BGroupLayoutBuilder(B_VERTICAL, 0)
87 			.Add(name)
88 			.Add(author)
89 		)
90 		.AddGlue()
91 		.Add(fGridDelay)
92 		.Add(fGridBorder)
93 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
94 			.Add(fGridWidth)
95 			.Add(fGridHeight)
96 		)
97 		.SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING,
98 			B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING)
99 	);
100 
101 	// Do our first label update
102 	_UpdateLabels();
103 }
104 
105 
106 // ------------------------------------------------------
107 //  GLifeConfig Class AttachedToWindow Definition
108 void
109 GLifeConfig::AttachedToWindow(void)
110 {
111 	fGridWidth->SetTarget(this);
112 	fGridHeight->SetTarget(this);
113 	fGridBorder->SetTarget(this);
114 	fGridDelay->SetTarget(this);
115 
116 #ifdef _USE_ASYNCHRONOUS
117 
118 	m_uiWindowFlags = Window()->Flags();
119 	Window()->SetFlags(m_uiWindowFlags | B_ASYNCHRONOUS_CONTROLS);
120 
121 #endif
122 }
123 
124 
125 void
126 GLifeConfig::_UpdateLabels()
127 {
128 	BString newLabel;
129 	newLabel.SetToFormat(B_TRANSLATE("Grid Width: %li"), fGridWidth->Value());
130 	fGridWidth->SetLabel(newLabel);
131 
132 	newLabel.SetToFormat(B_TRANSLATE("Grid Height: %li"), fGridHeight->Value());
133 	fGridHeight->SetLabel(newLabel);
134 
135 	newLabel.SetToFormat(B_TRANSLATE("Grid Border: %li"), fGridBorder->Value());
136 	fGridBorder->SetLabel(newLabel);
137 
138 	BString delay;
139 	if (fGridDelay->Value() <= 0)
140 		delay = B_TRANSLATE("none");
141 	else {
142 		BString format = (B_TRANSLATE_CONTEXT("Grid Life Delay: <number>x",
143 			"This is a factor: the x represents 'times'"));
144 		format.ReplaceAll("<number>", B_PRId32);
145 		format.SetToFormat(format.String(), fGridDelay->Value());
146 	}
147 	fGridDelay->SetLabel(delay);
148 }
149 
150 
151 // ------------------------------------------------------
152 //  GLifeConfig Class MessageReceived Definition
153 void
154 GLifeConfig::MessageReceived(BMessage* pbmMessage)
155 {
156 	switch(pbmMessage->what) {
157 		case kGridWidth:
158 			m_pglsState->GridWidth() = fGridWidth->Value();
159 			break;
160 		case kGridHeight:
161 			m_pglsState->GridHeight() = fGridHeight->Value();
162 			break;
163 		case kGridBorder:
164 			m_pglsState->GridBorder() = fGridBorder->Value();
165 			break;
166 		case kGridDelay:
167 			m_pglsState->GridDelay() = fGridDelay->Value();
168 			break;
169 		default:
170 			BView::MessageReceived(pbmMessage);
171 			return;
172 	}
173 	_UpdateLabels();
174 }
175