Skip to content

Commit

Permalink
libdeng2|Widgets: Added IndirectRule
Browse files Browse the repository at this point in the history
Indirect rules are useful when others need to depend one a rule that
may be replaced at any time.
  • Loading branch information
skyjake committed Aug 18, 2013
1 parent 0724ded commit 9189fd6
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
1 change: 1 addition & 0 deletions doomsday/libdeng2/include/de/IndirectRule
@@ -0,0 +1 @@
#include "widgets/indirectrule.h"
59 changes: 59 additions & 0 deletions doomsday/libdeng2/include/de/widgets/indirectrule.h
@@ -0,0 +1,59 @@
/** @file indirectrule.h Indirect rule.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_INDIRECTRULE_H
#define LIBDENG2_INDIRECTRULE_H

#include "../Rule"

namespace de {

/**
* Rule that gets its value indirectly from another rule. The value of an
* indirect rule cannot be set directly.
*
* Indirect rules are useful when others need to depend one a rule that may
* change dynamically. Anyone relying on the indirect rule will be duly
* notified of changes in the source of the indirect rule, without having to
* change anything in the existing rule relationships.
*/
class IndirectRule : public Rule
{
public:
IndirectRule();
~IndirectRule();

/**
* Sets the source rule whose value this indirect rule will reflect.
*
* @param rule Source rule.
*/
void setSource(Rule const &rule);

void update();

Rule const &source() const;
String description() const;

private:
Rule const *_source;
};

} // namespace de

#endif // LIBDENG2_INDIRECTRULE_H
3 changes: 2 additions & 1 deletion doomsday/libdeng2/include/de/widgets/rules.h
Expand Up @@ -20,6 +20,7 @@
#include "../Rule"
#include "../ConstantRule"
#include "../DelegateRule"
#include "../ScalarRule"
#include "../IndirectRule"
#include "../OperatorRule"
#include "../ScalarRule"
#include "../RuleRectangle"
61 changes: 61 additions & 0 deletions doomsday/libdeng2/src/widgets/indirectrule.cpp
@@ -0,0 +1,61 @@
/** @file indirectule.cpp Indirect rule.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "de/IndirectRule"

namespace de {

IndirectRule::IndirectRule() : _source(0)
{}

IndirectRule::~IndirectRule()
{
independentOf(_source);
}

void IndirectRule::setSource(Rule const &rule)
{
if(_source)
{
independentOf(_source);
}
dependsOn(_source = &rule);

invalidate();
}

void IndirectRule::update()
{
if(_source)
{
setValue(_source->value());
}
}

Rule const &IndirectRule::source() const
{
DENG2_ASSERT(_source != 0);
return *_source;
}

String IndirectRule::description() const
{
return String("Indirect => ") + source().description();
}

} // namespace de
3 changes: 3 additions & 0 deletions doomsday/libdeng2/widgets.pri
Expand Up @@ -4,6 +4,7 @@ HEADERS += \
include/de/AnimationVector \
include/de/DelegateRule \
include/de/ConstantRule \
include/de/IndirectRule \
include/de/OperatorRule \
include/de/RuleBank \
include/de/RuleRectangle \
Expand All @@ -17,6 +18,7 @@ HEADERS += \
include/de/widgets/animationvector.h \
include/de/widgets/constantrule.h \
include/de/widgets/delegaterule.h \
include/de/widgets/indirectrule.h \
include/de/widgets/operatorrule.h \
include/de/widgets/rulerectangle.h \
include/de/widgets/rootwidget.h \
Expand All @@ -31,6 +33,7 @@ SOURCES += \
src/widgets/animation.cpp \
src/widgets/constantrule.cpp \
src/widgets/delegaterule.cpp \
src/widgets/indirectrule.cpp \
src/widgets/operatorrule.cpp \
src/widgets/rulerectangle.cpp \
src/widgets/rootwidget.cpp \
Expand Down

0 comments on commit 9189fd6

Please sign in to comment.