Skip to content

Commit

Permalink
libdeng2|Widgets: Refining the layout rules implementation
Browse files Browse the repository at this point in the history
The Rule class hierarchy provides tools for specifying dynamic
mathematical expressions (e.g., for widget layout). The implementation
is still rather rough, though.

Todo for later: Rules are rather powerful, but there is also need for
a (higher-level?) mechanism where parent widgets can lay out child
widgets according to some principle (list, grid, etc.). In practice,
it can build on the Rules. In any case, manually constructing complex
rules is a bit arduous.
  • Loading branch information
skyjake committed Jan 22, 2013
1 parent 61d6c7c commit 10ebd80
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 160 deletions.
6 changes: 4 additions & 2 deletions doomsday/libdeng2/include/de/widgets/operatorrule.h
Expand Up @@ -48,14 +48,16 @@ class OperatorRule : public Rule
/**
* The operator rule takes ownership of the operands that have no parent.
*/
explicit OperatorRule(Operator op, Rule *unary, QObject *parent = 0);
explicit OperatorRule(Operator op, Rule *unaryOwn, QObject *parent = 0);

explicit OperatorRule(Operator op, Rule const *left, Rule const *right, QObject *parent = 0);
explicit OperatorRule(Operator op, Rule const *left, Rule *rightOwn, QObject *parent = 0);
explicit OperatorRule(Operator op, Rule *leftOwn, Rule const *right, QObject *parent = 0);

/**
* The operator rule takes ownership of the operands that have no parent.
*/
explicit OperatorRule(Operator op, Rule *left, Rule *right, QObject *parent = 0);
explicit OperatorRule(Operator op, Rule *leftOwn, Rule *rightOwn, QObject *parent = 0);

protected:
void update();
Expand Down
49 changes: 31 additions & 18 deletions doomsday/libdeng2/include/de/widgets/rectanglerule.h
Expand Up @@ -52,7 +52,19 @@ class RectangleRule : public Rule

public:
explicit RectangleRule(QObject *parent = 0);

/**
* Constructs a rectangle rule with individual rules defining the placement
* of the rectangle. Ownership of the input rules is not claimed.
*
* @param left Rule for the left coordinate.
* @param top Rule for the top coordinate.
* @param right Rule for the right coordinate.
* @param bottom Rule for the bottom coordinate.
* @param parent Parent object.
*/
explicit RectangleRule(Rule const *left, Rule const *top, Rule const *right, Rule const *bottom, QObject *parent = 0);

explicit RectangleRule(RectangleRule const *rect, QObject *parent = 0);

// Output rules.
Expand All @@ -66,9 +78,18 @@ class RectangleRule : public Rule
* previously been defined, the old one is destroyed first.
*
* @param inputRule Input rule to set.
* @param rule RectangleRule takes ownership.
* @param ruleOwn RectangleRule claims ownership (if rule has no parent yet).
*/
RectangleRule &setInput(InputRule inputRule, Rule *ruleOwn);

/**
* Sets one of the input rules of the rectangle. If the particular rule has
* previously been defined, the old one is destroyed first.
*
* @param inputRule Input rule to set.
* @param rule RectangleRule does not take ownership.
*/
void setRule(InputRule inputRule, Rule* rule);
RectangleRule &setInput(InputRule inputRule, Rule const *rule);

/**
* Returns an input rule.
Expand Down Expand Up @@ -97,6 +118,12 @@ class RectangleRule : public Rule
*/
Rectanglef rect() const;

/**
* Returns the current rectangle as defined by the input rules.
* Values are floored to integers.
*/
Rectanglei recti() const;

public slots:
void currentTimeChanged();

Expand All @@ -106,22 +133,8 @@ public slots:
void dependencyReplaced(Rule const *oldRule, Rule const *newRule);

private:
Rule const **ruleRef(InputRule rule);

DerivedRule *_left;
DerivedRule *_top;
DerivedRule *_right;
DerivedRule *_bottom;

AnimationVector2 _normalizedAnchorPoint;
Rule const *_anchorXRule;
Rule const *_anchorYRule;
Rule const *_leftRule;
Rule const *_topRule;
Rule const *_rightRule;
Rule const *_bottomRule;
Rule const *_widthRule;
Rule const *_heightRule;
struct Instance;
Instance *d;
};

} // namespace de
Expand Down
6 changes: 5 additions & 1 deletion doomsday/libdeng2/include/de/widgets/rootwidget.h
Expand Up @@ -25,20 +25,24 @@

namespace de {

class Rule;

class RootWidget : public Widget
{
public:
RootWidget();
~RootWidget();

Vector2i viewSize() const;
Rule const *viewWidth() const;
Rule const *viewHeight() const;

/**
* Sets the size of the view. All widgets in the tree are notified.
*
* @param size View size.
*/
void setViewSize(Vector2i const &size);
virtual void setViewSize(Vector2i const &viewSize);

void initialize();
void draw();
Expand Down
16 changes: 9 additions & 7 deletions doomsday/libdeng2/include/de/widgets/rule.h
Expand Up @@ -49,6 +49,12 @@ class Rule : public QObject

float value() const;

/**
* Transfers this rule's dependencies to @a newRule. The dependent rules
* are updated accordingly. Afterwards, this rule has no more dependencies.
*/
void transferDependencies(Rule *toRule);

/**
* Updates the rule with a valid value. Derived classes must call
* setValue() in their implementation of this method, because it sets the
Expand All @@ -58,12 +64,6 @@ class Rule : public QObject
*/
virtual void update();

/**
* Replaces this rule with @a newRule. The dependent rules are updated
* accordingly. Afterwards, this rule has no more dependencies.
*/
void replace(Rule *newRule);

protected:
/**
* Links rules together. This rule will depend on @a dependency.
Expand All @@ -80,8 +80,10 @@ class Rule : public QObject
* Takes ownership of a rule.
*
* @param child Rule whose ownership should be claimed.
*
* @return @c true, if ownership was taken.
*/
void claim(Rule *child);
bool claim(Rule *child);

/**
* Called to notify that the dependency @a oldRule has been replaced with
Expand Down
36 changes: 29 additions & 7 deletions doomsday/libdeng2/src/widgets/operatorrule.cpp
Expand Up @@ -30,12 +30,12 @@ OperatorRule::OperatorRule(Operator op, Rule const *unary, QObject *parent)
dependsOn(_leftOperand);
}

OperatorRule::OperatorRule(Operator op, Rule *unary, QObject *parent)
: Rule(parent), _operator(op), _leftOperand(unary), _rightOperand(0)
OperatorRule::OperatorRule(Operator op, Rule *unaryOwn, QObject *parent)
: Rule(parent), _operator(op), _leftOperand(unaryOwn), _rightOperand(0)
{
DENG2_ASSERT(_leftOperand != 0);

claim(unary);
claim(unaryOwn);
dependsOn(_leftOperand);
}

Expand All @@ -49,14 +49,36 @@ OperatorRule::OperatorRule(Operator op, Rule const *left, Rule const *right, QOb
dependsOn(_rightOperand);
}

OperatorRule::OperatorRule(Operator op, Rule *left, Rule *right, QObject *parent)
: Rule(parent), _operator(op), _leftOperand(left), _rightOperand(right)
OperatorRule::OperatorRule(Operator op, Rule const *left, Rule *rightOwn, QObject *parent)
: Rule(parent), _operator(op), _leftOperand(left), _rightOperand(rightOwn)
{
DENG2_ASSERT(_leftOperand != 0);
DENG2_ASSERT(_rightOperand != 0);

claim(rightOwn);
dependsOn(_leftOperand);
dependsOn(_rightOperand);
}

OperatorRule::OperatorRule(Operator op, Rule *leftOwn, Rule const *right, QObject *parent)
: Rule(parent), _operator(op), _leftOperand(leftOwn), _rightOperand(right)
{
DENG2_ASSERT(_leftOperand != 0);
DENG2_ASSERT(_rightOperand != 0);

claim(leftOwn);
dependsOn(_leftOperand);
dependsOn(_rightOperand);
}

OperatorRule::OperatorRule(Operator op, Rule *leftOwn, Rule *rightOwn, QObject *parent)
: Rule(parent), _operator(op), _leftOperand(leftOwn), _rightOperand(rightOwn)
{
DENG2_ASSERT(_leftOperand != 0);
DENG2_ASSERT(_rightOperand != 0);

claim(left);
claim(right);
claim(leftOwn);
claim(rightOwn);
dependsOn(_leftOperand);
dependsOn(_rightOperand);
}
Expand Down

0 comments on commit 10ebd80

Please sign in to comment.