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