Skip to content

Commit

Permalink
Shell|libdeng2: Working on rules
Browse files Browse the repository at this point in the history
Todo: Rule dependencies and ownership need to be reference-counted.
  • Loading branch information
skyjake committed Jan 22, 2013
1 parent 4f46885 commit bc4bab3
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 22 deletions.
2 changes: 1 addition & 1 deletion doomsday/libdeng2/include/de/widgets/rectanglerule.h
Expand Up @@ -113,7 +113,7 @@ class RectangleRule : public Rule
* (1, 1) to the bottom right.
* @param transition Transition time for the change.
*/
void setAnchorPoint(Vector2f const &normalizedPoint, Time::Delta const &transition = 0);
void setAnchorPoint(Vector2f const &normalizedPoint, TimeDelta const &transition = 0);

/**
* Returns the current rectangle as defined by the input rules.
Expand Down
8 changes: 7 additions & 1 deletion doomsday/libdeng2/include/de/widgets/rule.h
Expand Up @@ -64,15 +64,19 @@ class Rule : public QObject
*/
virtual void update();

bool isValid() const;

protected:
/**
* Links rules together. This rule will depend on @a dependency.
*/
void dependsOn(Rule const *dependency);

void independentOf(Rule const *dependency);

void addDependent(Rule *rule);
void removeDependent(Rule *rule);
void setValue(float value, bool markValid = true);
void setValue(float value);

float cachedValue() const;

Expand All @@ -91,6 +95,8 @@ class Rule : public QObject
*/
virtual void dependencyReplaced(Rule const *oldRule, Rule const *newRule);

void invalidateSilently();

public slots:
void invalidate();

Expand Down
15 changes: 13 additions & 2 deletions doomsday/libdeng2/include/de/widgets/scalarrule.h
Expand Up @@ -37,7 +37,11 @@ class ScalarRule : public Rule
public:
explicit ScalarRule(float initialValue, QObject *parent = 0);

void set(float value, Time::Delta transition = 0);
void set(float target, TimeDelta transition = 0);

void set(Rule const *target, TimeDelta transition = 0);

void set(Rule *targetOwn, TimeDelta transition = 0);

/**
* Read-only access to the scalar animation.
Expand All @@ -48,10 +52,17 @@ class ScalarRule : public Rule

protected:
void update();
void dependencyReplaced(Rule const *oldRule, Rule const *newRule);

void dismissTargetRule();

protected slots:
void timeChanged();

private:
Animation _animation;
Time _validAt;
Rule const *_rule;
bool _ruleOwned;
};

} // namespace de
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libdeng2/src/widgets/rectanglerule.cpp
Expand Up @@ -246,7 +246,7 @@ Rule const *RectangleRule::inputRule(InputRule inputRule)
return d->ruleRef(inputRule).rule.constPtr;
}

void RectangleRule::setAnchorPoint(Vector2f const &normalizedPoint, Time::Delta const &transition)
void RectangleRule::setAnchorPoint(Vector2f const &normalizedPoint, TimeDelta const &transition)
{
d->normalizedAnchorPoint.setValue(normalizedPoint, transition);
invalidate();
Expand Down
29 changes: 22 additions & 7 deletions doomsday/libdeng2/src/widgets/rule.cpp
Expand Up @@ -45,6 +45,8 @@ float Rule::value() const
// Force an update.
const_cast<Rule *>(this)->update();
}
DENG2_ASSERT(_isValid);

return _value;
}

Expand All @@ -54,24 +56,30 @@ void Rule::update()
_isValid = true;
}

bool Rule::isValid() const
{
return _isValid;
}

void Rule::dependencyReplaced(Rule const *, Rule const *)
{
// No dependencies.
}

void Rule::invalidateSilently()
{
_isValid = false;
}

float Rule::cachedValue() const
{
return _value;
}

void Rule::setValue(float v, bool markValid)
void Rule::setValue(float v)
{
_value = v;

if(markValid)
{
_isValid = true;
}
_isValid = true;
}

void Rule::transferDependencies(Rule *toRule)
Expand All @@ -98,6 +106,13 @@ void Rule::dependsOn(Rule const *dependency)
const_cast<Rule *>(dependency)->addDependent(this);
}

void Rule::independentOf(Rule const *dependency)
{
DENG2_ASSERT(dependency != 0);

const_cast<Rule *>(dependency)->removeDependent(this);
}

void Rule::addDependent(Rule *rule)
{
DENG2_ASSERT(!_dependentRules.contains(rule));
Expand All @@ -122,7 +137,7 @@ void Rule::invalidate()
{
if(_isValid)
{
_isValid = false;
invalidateSilently();
emit valueInvalidated();
}
}
Expand Down
65 changes: 57 additions & 8 deletions doomsday/libdeng2/src/widgets/scalarrule.cpp
Expand Up @@ -18,27 +18,76 @@
*/

#include "de/ScalarRule"
#include "de/Clock"
#include <QDebug>

namespace de {

ScalarRule::ScalarRule(float initialValue, QObject *parent)
: Rule(initialValue, parent), _animation(initialValue)
: Rule(initialValue, parent), _animation(initialValue), _rule(0), _ruleOwned(false)
{}

void ScalarRule::set(float value, de::Time::Delta transition)
void ScalarRule::set(float target, de::TimeDelta transition)
{
_animation.setValue(value, transition);
dismissTargetRule();

connect(&_animation.clock(), SIGNAL(timeChanged()), this, SLOT(timeChanged()));
_animation.setValue(target, transition);
invalidate();
}

void ScalarRule::set(Rule const *target, TimeDelta transition)
{
set(target->value(), transition);

_ruleOwned = false;
_rule = target;
dependsOn(_rule);
}

void ScalarRule::set(Rule *targetOwn, TimeDelta transition)
{
set(targetOwn->value(), transition);

_ruleOwned = claim(targetOwn);
_rule = targetOwn;
dependsOn(_rule);
}

void ScalarRule::update()
{
// Avoid repeated evaluation.
Time const now = Animation::currentTime();
if(_validAt != now)
setValue(_animation);
}

void ScalarRule::dependencyReplaced(Rule const *oldRule, Rule const *newRule)
{
if(oldRule == _rule)
{
dismissTargetRule();
oldRule = newRule;
}
}

void ScalarRule::dismissTargetRule()
{
if(_rule)
{
independentOf(_rule);
if(_ruleOwned) delete _rule;
_rule = 0;
}
_ruleOwned = false;
}

void ScalarRule::timeChanged()
{
if(!_animation.done() || cachedValue() != _animation.value())
{
invalidate();
}
if(_animation.done())
{
setValue(_animation, _animation.done());
_validAt = now;
disconnect(this, SLOT(timeChanged()));
}
}

Expand Down
2 changes: 2 additions & 0 deletions doomsday/tools/shell/shell-text/src/cursesapp.cpp
Expand Up @@ -186,6 +186,8 @@ struct CursesApp::Instance
qDebug() << "Got key" << QString("0x%1").arg(key, 0, 16).toAscii().constData();
}
}

rootWidget->draw();
}

void windowWasResized() // called from signal handler
Expand Down
2 changes: 2 additions & 0 deletions doomsday/tools/shell/shell-text/src/cursestextcanvas.cpp
Expand Up @@ -62,6 +62,8 @@ void CursesTextCanvas::show()
}
}

wmove(_window, 0, 0);

// Mark everything clean.
TextCanvas::show();

Expand Down
1 change: 0 additions & 1 deletion doomsday/tools/shell/shell-text/src/logwidget.cpp
Expand Up @@ -17,7 +17,6 @@
*/

#include "logwidget.h"
#include <QDebug>

struct LogWidget::Instance
{
Expand Down
5 changes: 4 additions & 1 deletion doomsday/tools/shell/shell-text/src/shellapp.cpp
Expand Up @@ -30,8 +30,11 @@ struct ShellApp::Instance
{
logWidget = new LogWidget;

ScalarRule *anim = new ScalarRule(0);
anim->set(5, 2);

logWidget->rule()
.setInput(RectangleRule::Left, new ConstantRule(0))
.setInput(RectangleRule::Left, anim)
.setInput(RectangleRule::Top, new ConstantRule(0))
.setInput(RectangleRule::Width, self.rootWidget().viewWidth())
.setInput(RectangleRule::Height, self.rootWidget().viewHeight());
Expand Down

0 comments on commit bc4bab3

Please sign in to comment.