1/*
2 * Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
3 * Copyright 2014-2016 Haiku, Inc. All rights reserved.
4 *
5 * Distributed under the terms of the MIT license.
6 *
7 * Authors:
8 *		Tri-Edge AI
9 *		John Scipione, jscipione@gmail.com
10 */
11
12
13#include "ConfigView.h"
14
15#include <LayoutBuilder.h>
16#include <ListView.h>
17#include <ScrollView.h>
18#include <Slider.h>
19#include <StringView.h>
20#include <View.h>
21
22#include "ColorItem.h"
23#include "Gravity.h"
24#include "RainbowItem.h"
25
26
27static const int32 kMsgSize = 'size';
28static const int32 kMsgShade = 'shad';
29
30
31ConfigView::ConfigView(BRect frame, Gravity* parent)
32	:
33	BView(frame, B_EMPTY_STRING, B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
34	fParent(parent),
35	fTitleString(new BStringView(B_EMPTY_STRING, "OpenGL Gravity Effect")),
36	fAuthorString(new BStringView(B_EMPTY_STRING, "by Tri-Edge AI")),
37	fCountSlider(new BSlider(B_EMPTY_STRING, "Particle Count: ",
38		new BMessage(kMsgSize), 0, 4, B_HORIZONTAL, B_BLOCK_THUMB,
39		B_NAVIGABLE | B_WILL_DRAW)),
40	fShadeString(new BStringView(B_EMPTY_STRING, "Shade: ")),
41	fShadeList(new BListView(B_EMPTY_STRING, B_SINGLE_SELECTION_LIST,
42		B_WILL_DRAW | B_NAVIGABLE))
43{
44	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
45
46	fShadeList->SetSelectionMessage(new BMessage(kMsgShade));
47	fShadeList->AddItem(new ColorItem("Red", (rgb_color){ 255, 65, 54 }));
48	fShadeList->AddItem(new ColorItem("Green", (rgb_color){ 46, 204, 64 }));
49	fShadeList->AddItem(new ColorItem("Blue", (rgb_color){ 0, 116, 217 }));
50	fShadeList->AddItem(new ColorItem("Orange", (rgb_color){ 255, 133, 27 }));
51	fShadeList->AddItem(new ColorItem("Purple", (rgb_color){ 177, 13, 201 }));
52	fShadeList->AddItem(new ColorItem("White", (rgb_color){ 255, 255, 255 }));
53	fShadeList->AddItem(new RainbowItem("Rainbow"));
54
55	fShadeScroll = new BScrollView(B_EMPTY_STRING, fShadeList,
56		B_WILL_DRAW | B_FRAME_EVENTS, false, true);
57
58	fCountSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
59	fCountSlider->SetHashMarkCount(5);
60	fCountSlider->SetLimitLabels("128", "2048");
61	fCountSlider->SetModificationMessage(new BMessage(kMsgSize));
62	fCountSlider->SetValue(parent->Config.ParticleCount);
63
64	BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
65		.AddGroup(B_VERTICAL, 0)
66			.Add(fTitleString)
67			.Add(fAuthorString)
68			.End()
69		.Add(fShadeString)
70		.Add(fShadeScroll)
71		.Add(fCountSlider)
72		.SetInsets(B_USE_DEFAULT_SPACING)
73		.End();
74}
75
76
77void
78ConfigView::AllAttached()
79{
80	// fixup scroll bar range
81	fShadeScroll->ScrollBar(B_VERTICAL)->SetRange(0.0f,
82		fShadeList->ItemFrame(fShadeList->CountItems()).bottom
83			- fShadeList->ItemFrame((int32)0).top);
84
85	// select the shade
86	fShadeList->Select(fParent->Config.ShadeID);
87}
88
89
90void
91ConfigView::AttachedToWindow()
92{
93	fShadeList->SetTarget(this);
94	fCountSlider->SetTarget(this);
95}
96
97
98void
99ConfigView::MessageReceived(BMessage* message)
100{
101	switch (message->what) {
102		case kMsgSize:
103			fParent->Config.ParticleCount = fCountSlider->Value();
104			break;
105
106		case kMsgShade:
107			fParent->Config.ShadeID = fShadeList->CurrentSelection();
108			break;
109
110		default:
111			BView::MessageReceived(message);
112	}
113}
114