1/*
2 * Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
3 * Copyright 2014 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 "Gravity.h"
14
15#include "ConfigView.h"
16#include "GravityView.h"
17
18#include <ScreenSaver.h>
19#include <View.h>
20
21#include <stdlib.h>
22
23
24Gravity::Gravity(BMessage* prefs, image_id imageID)
25	:
26	BScreenSaver(prefs, imageID),
27	fGravityView(NULL),
28	fConfigView(NULL)
29{
30	srand(time(NULL));
31
32	if (prefs->IsEmpty()) {
33		Config.ParticleCount = 1;
34		Config.ShadeID = 2;
35	} else {
36		if (prefs->FindInt32("ParticleCount", &Config.ParticleCount) != B_OK)
37			Config.ParticleCount = 1;
38
39		if (prefs->FindInt32("ShadeID", &Config.ShadeID) != B_OK)
40			Config.ShadeID = 2;
41	}
42}
43
44
45status_t
46Gravity::SaveState(BMessage* prefs) const
47{
48	prefs->AddInt32("ParticleCount", Config.ParticleCount);
49	prefs->AddInt32("ShadeID", Config.ShadeID);
50	return B_OK;
51}
52
53
54void
55Gravity::StartConfig(BView* view)
56{
57	fConfigView = new ConfigView(view->Bounds(), this);
58	view->AddChild(fConfigView);
59}
60
61
62status_t
63Gravity::StartSaver(BView* view, bool preview)
64{
65	SetTickSize((1000 / 20) * 1000);
66		// ~20 FPS
67	fGravityView = new GravityView(view->Bounds(), this);
68	view->AddChild(fGravityView);
69
70	return B_OK;
71}
72
73
74void
75Gravity::StopSaver()
76{
77	if (fGravityView != NULL)
78		fGravityView->EnableDirectMode(false);
79}
80
81
82void
83Gravity::DirectConnected(direct_buffer_info* info)
84{
85	if (fGravityView != NULL) {
86		fGravityView->DirectConnected(info);
87		fGravityView->EnableDirectMode(true);
88	}
89}
90
91
92void
93Gravity::Draw(BView*, int32 frame)
94{
95	fGravityView->DirectDraw();
96}
97