Skip to content

Commit

Permalink
Refactor|libgui|libdeng2: Derived rule, font, color and image banks f…
Browse files Browse the repository at this point in the history
…rom InfoBank
  • Loading branch information
skyjake committed May 14, 2013
1 parent 131cf76 commit ad5cb8c
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 150 deletions.
14 changes: 5 additions & 9 deletions doomsday/libdeng2/include/de/widgets/rulebank.h
Expand Up @@ -23,30 +23,25 @@
#ifndef LIBGUI_RULEBANK_H
#define LIBGUI_RULEBANK_H

#include "../Bank"
#include "../InfoBank"
#include "../ConstantRule"

namespace de {

class File;
class Record;

/**
* Bank of Rules where each is identified by a Path.
*/
class RuleBank : public Bank
class RuleBank : public InfoBank
{
public:
RuleBank();

/**
* Creates a number of rules based on information in an Info document.
*
* @param source Info source containing rule definitions.
*/
void addFromInfo(String const &source);

/**
* Creates a number of rules based on information in an Info document.
* The contents of the file are parsed first.
*
* @param source File with Info source containing rule definitions.
*/
Expand All @@ -62,6 +57,7 @@ class RuleBank : public Bank
Rule const &rule(Path const &path) const;

protected:
virtual ISource *newSourceFromInfo(String const &id);
virtual IData *loadFromSource(ISource &source);

private:
Expand Down
49 changes: 14 additions & 35 deletions doomsday/libdeng2/src/widgets/rulebank.cpp
Expand Up @@ -26,15 +26,15 @@ DENG2_PIMPL(RuleBank)
{
struct RuleSource : public ISource
{
Instance *d;
InfoBank &bank;
String id;

RuleSource(Instance *inst, String const &ruleId) : d(inst), id(ruleId) {}
Time modifiedAt() const { return d->modTime; }
RuleSource(InfoBank &b, String const &ruleId) : bank(b), id(ruleId) {}
Time modifiedAt() const { return bank.sourceModifiedAt(); }

Rule *load() const
{
return refless(new ConstantRule(d->info[id].value().asNumber()));
return refless(new ConstantRule(bank[id].value().asNumber()));
}
};

Expand All @@ -45,51 +45,30 @@ DENG2_PIMPL(RuleBank)
RuleData(Rule *r) : rule(holdRef(r)) {}
~RuleData() { releaseRef(rule); }

duint sizeInMemory() const
{
return 0; // we don't count
}
duint sizeInMemory() const { return 0; /* we don't count */ }
};

Time modTime;
ScriptedInfo info;

Instance(Public *i) : Base(i)
{}
Instance(Public *i) : Base(i) {}
};

RuleBank::RuleBank()
: Bank(DisableHotStorage), d(new Instance(this))
RuleBank::RuleBank() : InfoBank(DisableHotStorage), d(new Instance(this))
{}

void RuleBank::addFromInfo(String const &source)
void RuleBank::addFromInfo(File const &file)
{
LOG_AS("RuleBank");
try
{
d->modTime = Time();
d->info.parse(source);

foreach(String fn, d->info.allBlocksOfType("rule"))
{
add(fn, new Instance::RuleSource(d, fn));
}
}
catch(Error const &er)
{
LOG_WARNING("Failed to read Info source:\n") << er.asText();
}
parse(file);
InfoBank::addFromInfo("rule");
}

void RuleBank::addFromInfo(File const &file)
Rule const &RuleBank::rule(Path const &path) const
{
addFromInfo(String::fromUtf8(Block(file)));
d->modTime = file.status().modifiedAt;
return *static_cast<Instance::RuleData &>(data(path)).rule;
}

Rule const &RuleBank::rule(Path const &path) const
Bank::ISource *RuleBank::newSourceFromInfo(String const &id)
{
return *static_cast<Instance::RuleData &>(data(path)).rule;
return new Instance::RuleSource(*this, id);
}

Bank::IData *RuleBank::loadFromSource(ISource &source)
Expand Down
15 changes: 5 additions & 10 deletions doomsday/libgui/include/de/gui/colorbank.h
Expand Up @@ -19,7 +19,7 @@
#ifndef LIBGUI_COLORBANK_H
#define LIBGUI_COLORBANK_H

#include <de/Bank>
#include <de/InfoBank>
#include <de/File>
#include <de/Vector>

Expand All @@ -28,7 +28,7 @@ namespace de {
/**
* Bank of colors where each color is identified by a Path.
*/
class ColorBank : public Bank
class ColorBank : public InfoBank
{
public:
typedef Vector4ub Color;
Expand All @@ -39,13 +39,7 @@ class ColorBank : public Bank

/**
* Creates a number of colors based on information in an Info document.
*
* @param source Info source containing color definitions.
*/
void addFromInfo(String const &source);

/**
* Creates a number of colors based on information in an Info document.
* The file is parsed first.
*
* @param source File with Info source containing color definitions.
*/
Expand All @@ -65,11 +59,12 @@ class ColorBank : public Bank
*
* @param path Identifier of the color.
*
* @return Vector with the color values (0...1).
* @return Vector with the floating-point color values (0...1).
*/
Colorf colorf(Path const &path) const;

protected:
virtual ISource *newSourceFromInfo(String const &id);
virtual IData *loadFromSource(ISource &source);

private:
Expand Down
13 changes: 4 additions & 9 deletions doomsday/libgui/include/de/gui/fontbank.h
Expand Up @@ -19,7 +19,7 @@
#ifndef LIBGUI_FONTBANK_H
#define LIBGUI_FONTBANK_H

#include <de/Bank>
#include <de/InfoBank>
#include <de/File>
#include "../Font"
#include "libgui.h"
Expand All @@ -29,20 +29,14 @@ namespace de {
/**
* Bank containing fonts.
*/
class LIBGUI_PUBLIC FontBank : public Bank
class LIBGUI_PUBLIC FontBank : public InfoBank
{
public:
FontBank();

/**
* Creates a number of fonts based on information in an Info document.
*
* @param source Info source containing font definitions.
*/
void addFromInfo(String const &source);

/**
* Creates a number of fonts based on information in an Info document.
* The file is parsed first.
*
* @param source File with Info source containing font definitions.
*/
Expand All @@ -58,6 +52,7 @@ class LIBGUI_PUBLIC FontBank : public Bank
Font const &font(Path const &path) const;

protected:
virtual ISource *newSourceFromInfo(String const &id);
virtual IData *loadFromSource(ISource &source);

private:
Expand Down
7 changes: 3 additions & 4 deletions doomsday/libgui/include/de/gui/imagebank.h
Expand Up @@ -22,7 +22,7 @@
#include "libgui.h"
#include "../Image"

#include <de/Bank>
#include <de/InfoBank>

namespace de {

Expand All @@ -33,7 +33,7 @@ class File;
*
* @ingroup data
*/
class LIBGUI_PUBLIC ImageBank : public Bank
class LIBGUI_PUBLIC ImageBank : public InfoBank
{
public:
/**
Expand All @@ -48,13 +48,12 @@ class LIBGUI_PUBLIC ImageBank : public Bank
ImageBank(Flags const &flags = BackgroundThread | DisableHotStorage);

void add(Path const &path, String const &imageFilePath);

void addFromInfo(String const &source, String const &relativeToPath = "");
void addFromInfo(File const &file);

Image &image(Path const &path) const;

protected:
ISource *newSourceFromInfo(String const &id);
IData *loadFromSource(ISource &source);
IData *newData();

Expand Down
44 changes: 13 additions & 31 deletions doomsday/libgui/src/colorbank.cpp
Expand Up @@ -18,23 +18,22 @@

#include "de/ColorBank"
#include <de/ArrayValue>
#include <de/ScriptedInfo>

namespace de {

DENG2_PIMPL(ColorBank)
{
struct ColorSource : public ISource
{
Instance *d;
ColorBank &bank;
String id;

ColorSource(Instance *inst, String const &colorId) : d(inst), id(colorId) {}
Time modifiedAt() const { return d->modTime; }
ColorSource(ColorBank &b, String const &colorId) : bank(b), id(colorId) {}
Time modifiedAt() const { return bank.sourceModifiedAt(); }

Vector4d load() const
{
ArrayValue const &colorDef = d->info[id].value<ArrayValue>();
ArrayValue const &colorDef = bank[id].value<ArrayValue>();

// Alpha component is optional.
ddouble alpha = 1.0;
Expand All @@ -58,40 +57,18 @@ DENG2_PIMPL(ColorBank)
duint sizeInMemory() const { return 0; /* we don't count */ }
};

Time modTime;
ScriptedInfo info;

Instance(Public *i) : Base(i)
{}
};

ColorBank::ColorBank()
: Bank(DisableHotStorage), d(new Instance(this))
ColorBank::ColorBank() : InfoBank(DisableHotStorage), d(new Instance(this))
{}

void ColorBank::addFromInfo(String const &source)
{
LOG_AS("ColorBank");
try
{
d->modTime = Time();
d->info.parse(source);

foreach(String fn, d->info.allBlocksOfType("color"))
{
add(fn, new Instance::ColorSource(d, fn));
}
}
catch(Error const &er)
{
LOG_WARNING("Failed to read Info source:\n") << er.asText();
}
}

void ColorBank::addFromInfo(File const &file)
{
addFromInfo(String::fromUtf8(Block(file)));
d->modTime = file.status().modifiedAt;
LOG_AS("ColorBank");
parse(file);
InfoBank::addFromInfo("color");
}

ColorBank::Color ColorBank::color(Path const &path) const
Expand All @@ -108,6 +85,11 @@ ColorBank::Colorf ColorBank::colorf(Path const &path) const
return Colorf(float(clamped.x), float(clamped.y), float(clamped.z), float(clamped.w));
}

Bank::ISource *ColorBank::newSourceFromInfo(String const &id)
{
return new Instance::ColorSource(*this, id);
}

Bank::IData *ColorBank::loadFromSource(ISource &source)
{
return new Instance::ColorData(static_cast<Instance::ColorSource &>(source).load());
Expand Down

0 comments on commit ad5cb8c

Please sign in to comment.