xref: /haiku/src/add-ons/screen_savers/glife/GLifeConfig.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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 	SetViewColor(ui_color(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 	char newLabel[64];
129 	snprintf(newLabel, sizeof(newLabel), B_TRANSLATE("Grid Width: %li"),
130 		fGridWidth->Value());
131 	fGridWidth->SetLabel(newLabel);
132 	snprintf(newLabel, sizeof(newLabel), B_TRANSLATE("Grid Height: %li"),
133 		fGridHeight->Value());
134 	fGridHeight->SetLabel(newLabel);
135 	snprintf(newLabel, sizeof(newLabel), B_TRANSLATE("Grid Border: %li"),
136 		fGridBorder->Value());
137 	fGridBorder->SetLabel(newLabel);
138 
139 	char delay[16];
140 	if (fGridDelay->Value() <= 0)
141 		sprintf(delay, B_TRANSLATE("none"));
142 	else {
143 		sprintf(delay, "%" B_PRId32, fGridDelay->Value());
144 		sprintf(delay, B_TRANSLATE_COMMENT("%sx",
145 				"This is a factor: the x represents 'times'"), delay);
146 	}
147 	snprintf(newLabel, sizeof(newLabel), B_TRANSLATE("Grid Life Delay: %s"),
148 		delay);
149 	fGridDelay->SetLabel(newLabel);
150 }
151 
152 
153 // ------------------------------------------------------
154 //  GLifeConfig Class MessageReceived Definition
155 void
156 GLifeConfig::MessageReceived(BMessage* pbmMessage)
157 {
158 	switch(pbmMessage->what) {
159 		case kGridWidth:
160 			m_pglsState->GridWidth() = fGridWidth->Value();
161 			break;
162 		case kGridHeight:
163 			m_pglsState->GridHeight() = fGridHeight->Value();
164 			break;
165 		case kGridBorder:
166 			m_pglsState->GridBorder() = fGridBorder->Value();
167 			break;
168 		case kGridDelay:
169 			m_pglsState->GridDelay() = fGridDelay->Value();
170 			break;
171 		default:
172 			BView::MessageReceived(pbmMessage);
173 			return;
174 	}
175 	_UpdateLabels();
176 }
177