Leaves: Don't save the control, already have value.

I was looking at the Leaves screensaver for reference and I noticed
this peculiarity. It saves a pointer to each slider control
just so that it can later read their values, but, the values are
already provided in the message in the be:value property, so just
use that instead.
This commit is contained in:
John Scipione
2013-09-02 02:53:50 -04:00
parent 51b735b342
commit 01f623b00f
2 changed files with 45 additions and 36 deletions
+43 -29
View File
@@ -1,11 +1,13 @@
/* /*
* Copyright 2010-2011, Haiku, Inc. All Rights Reserved. * Copyright 2010-2013, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT license. * Distributed under the terms of the MIT license.
* *
* Authors: * Authors:
* Deyan Genovski, [email protected] * Deyan Genovski, [email protected]
* Geoffry Song, [email protected] * Geoffry Song, [email protected]
*/ */
#include "Leaves.h" #include "Leaves.h"
#include <stdlib.h> #include <stdlib.h>
@@ -60,6 +62,7 @@ static const int kMinimumDropRate = 2, kMaximumDropRate = 50;
static const int kMinimumLeafSize = 50, kMaximumLeafSize = 1000; static const int kMinimumLeafSize = 50, kMaximumLeafSize = 1000;
static const int kMaximumSizeVariation = 200; static const int kMaximumSizeVariation = 200;
// message constants
enum { enum {
MSG_SET_DROP_RATE = 'drop', MSG_SET_DROP_RATE = 'drop',
MSG_SET_LEAF_SIZE = 'size', MSG_SET_LEAF_SIZE = 'size',
@@ -77,9 +80,6 @@ instantiate_screen_saver(BMessage* msg, image_id image)
Leaves::Leaves(BMessage* archive, image_id id) Leaves::Leaves(BMessage* archive, image_id id)
: :
BScreenSaver(archive, id), BScreenSaver(archive, id),
fDropRateSlider(NULL),
fLeafSizeSlider(NULL),
fSizeVariationSlider(NULL),
fDropRate(10), fDropRate(10),
fLeafSize(150), fLeafSize(150),
fSizeVariation(50) fSizeVariation(50)
@@ -102,35 +102,35 @@ Leaves::StartConfig(BView* view)
bounds.InsetBy(10, 10); bounds.InsetBy(10, 10);
BRect frame(0, 0, bounds.Width(), 20); BRect frame(0, 0, bounds.Width(), 20);
fDropRateSlider = new BSlider(frame, "drop rate", BSlider* dropRateSlider = new BSlider(frame, "drop rate",
B_TRANSLATE("Drop rate:"), new BMessage(MSG_SET_DROP_RATE), B_TRANSLATE("Drop rate:"), new BMessage(MSG_SET_DROP_RATE),
kMinimumDropRate, kMaximumDropRate, B_BLOCK_THUMB, kMinimumDropRate, kMaximumDropRate, B_BLOCK_THUMB,
B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM); B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM);
fDropRateSlider->SetValue(fDropRate); dropRateSlider->SetValue(fDropRate);
fDropRateSlider->ResizeToPreferred(); dropRateSlider->ResizeToPreferred();
bounds.bottom -= fDropRateSlider->Bounds().Height() * 1.5; bounds.bottom -= dropRateSlider->Bounds().Height() * 1.5;
fDropRateSlider->MoveTo(bounds.LeftBottom()); dropRateSlider->MoveTo(bounds.LeftBottom());
view->AddChild(fDropRateSlider); view->AddChild(dropRateSlider);
fLeafSizeSlider = new BSlider(frame, "leaf size", BSlider* leafSizeSlider = new BSlider(frame, "leaf size",
B_TRANSLATE("Leaf size:"), new BMessage(MSG_SET_LEAF_SIZE), B_TRANSLATE("Leaf size:"), new BMessage(MSG_SET_LEAF_SIZE),
kMinimumLeafSize, kMaximumLeafSize, B_BLOCK_THUMB, kMinimumLeafSize, kMaximumLeafSize, B_BLOCK_THUMB,
B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM); B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM);
fLeafSizeSlider->SetValue(fLeafSize); leafSizeSlider->SetValue(fLeafSize);
fLeafSizeSlider->ResizeToPreferred(); leafSizeSlider->ResizeToPreferred();
bounds.bottom -= fLeafSizeSlider->Bounds().Height() * 1.5; bounds.bottom -= leafSizeSlider->Bounds().Height() * 1.5;
fLeafSizeSlider->MoveTo(bounds.LeftBottom()); leafSizeSlider->MoveTo(bounds.LeftBottom());
view->AddChild(fLeafSizeSlider); view->AddChild(leafSizeSlider);
fSizeVariationSlider = new BSlider(frame, "variation", BSlider* sizeVariationSlider = new BSlider(frame, "variation",
B_TRANSLATE("Size variation:"), new BMessage(MSG_SET_SIZE_VARIATION), B_TRANSLATE("Size variation:"), new BMessage(MSG_SET_SIZE_VARIATION),
0, kMaximumSizeVariation, B_BLOCK_THUMB, 0, kMaximumSizeVariation, B_BLOCK_THUMB,
B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM); B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM);
fSizeVariationSlider->SetValue(fSizeVariation); sizeVariationSlider->SetValue(fSizeVariation);
fSizeVariationSlider->ResizeToPreferred(); sizeVariationSlider->ResizeToPreferred();
bounds.bottom -= fSizeVariationSlider->Bounds().Height() * 1.5; bounds.bottom -= sizeVariationSlider->Bounds().Height() * 1.5;
fSizeVariationSlider->MoveTo(bounds.LeftBottom()); sizeVariationSlider->MoveTo(bounds.LeftBottom());
view->AddChild(fSizeVariationSlider); view->AddChild(sizeVariationSlider);
BTextView* textView = new BTextView(bounds, B_EMPTY_STRING, BTextView* textView = new BTextView(bounds, B_EMPTY_STRING,
bounds.OffsetToCopy(0., 0.), B_FOLLOW_ALL, B_WILL_DRAW); bounds.OffsetToCopy(0., 0.), B_FOLLOW_ALL, B_WILL_DRAW);
@@ -150,9 +150,9 @@ Leaves::StartConfig(BView* view)
BWindow* window = view->Window(); BWindow* window = view->Window();
if (window) window->AddHandler(this); if (window) window->AddHandler(this);
fDropRateSlider->SetTarget(this); dropRateSlider->SetTarget(this);
fLeafSizeSlider->SetTarget(this); leafSizeSlider->SetTarget(this);
fSizeVariationSlider->SetTarget(this); sizeVariationSlider->SetTarget(this);
} }
@@ -187,14 +187,29 @@ Leaves::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case MSG_SET_DROP_RATE: case MSG_SET_DROP_RATE:
fDropRate = fDropRateSlider->Value(); {
int32 dropRate;
if (message->FindInt32("be:value", &dropRate) == B_OK)
fDropRate = dropRate;
break; break;
}
case MSG_SET_LEAF_SIZE: case MSG_SET_LEAF_SIZE:
fLeafSize = fLeafSizeSlider->Value(); {
int32 leafSize;
if (message->FindInt32("be:value", &leafSize) == B_OK)
fLeafSize = leafSize;
break; break;
}
case MSG_SET_SIZE_VARIATION: case MSG_SET_SIZE_VARIATION:
fSizeVariation = fSizeVariationSlider->Value(); {
int32 sizeVariation;
if (message->FindInt32("be:value", &sizeVariation) == B_OK)
fSizeVariation = sizeVariation;
break; break;
}
default: default:
BHandler::MessageReceived(message); BHandler::MessageReceived(message);
} }
@@ -253,4 +268,3 @@ Leaves::_RandomPoint(const BRect& bound)
return BPoint(drand48() * (bound.right - bound.left) + bound.left, return BPoint(drand48() * (bound.right - bound.left) + bound.left,
drand48() * (bound.bottom - bound.top) + bound.top); drand48() * (bound.bottom - bound.top) + bound.top);
} }
+2 -7
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2011, Haiku, Inc. All Rights Reserved. * Copyright 2010-2013, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT license. * Distributed under the terms of the MIT license.
* *
* Authors: * Authors:
@@ -14,8 +14,6 @@
#include <ScreenSaver.h> #include <ScreenSaver.h>
class BSlider;
class Leaves : public BScreenSaver, public BHandler { class Leaves : public BScreenSaver, public BHandler {
public: public:
Leaves(BMessage* archive, image_id id); Leaves(BMessage* archive, image_id id);
@@ -28,9 +26,6 @@ public:
virtual void MessageReceived(BMessage* message); virtual void MessageReceived(BMessage* message);
private: private:
BSlider* fDropRateSlider;
BSlider* fLeafSizeSlider;
BSlider* fSizeVariationSlider;
int32 fDropRate; int32 fDropRate;
int32 fLeafSize; int32 fLeafSize;
int32 fSizeVariation; int32 fSizeVariation;
@@ -38,5 +33,5 @@ private:
inline BPoint _RandomPoint(const BRect& bound); inline BPoint _RandomPoint(const BRect& bound);
}; };
#endif // _LEAVES_H_
#endif // _LEAVES_H_