Skip to content

Commit

Permalink
Start factoring out the Gui interface to igui.h.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 6, 2017
1 parent 16d2277 commit 7b950b9
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 63 deletions.
83 changes: 83 additions & 0 deletions include/igui.h
@@ -0,0 +1,83 @@
#pragma once

#include <vector>
#include <string>
#include "imodule.h"

namespace gui
{

class Gui;
typedef std::shared_ptr<Gui> GuiPtr;

enum GuiType
{
NOT_LOADED_YET, // no attempt to load the GUI has been made
UNDETERMINED, // not checked yet for type
ONE_SIDED_READABLE, // 1-sided
TWO_SIDED_READABLE, // 2-sided
NO_READABLE, // not a readable
IMPORT_FAILURE, // failed to load
FILE_NOT_FOUND, // file doesn't exist
};

/**
* greebo: Interface managing the idTech4 GUI files,
* keeping track of all the loaded GUIs,
* parsing the .gui files on demand.
*/
class IGuiManager :
public RegisterableModule
{
public:
typedef std::vector<std::string> StringList;

// A visitor class used to traverse all known GUIs by path
class Visitor
{
public:
virtual ~Visitor() {}

virtual void visit(const std::string& guiPath, const GuiType& guiType) = 0;
};

public:
virtual ~IGuiManager() {}

// Gets a GUI from the given VFS path, parsing it on demand
// Returns NULL if the GUI couldn't be found or loaded.
virtual GuiPtr getGui(const std::string& guiPath) = 0;

// Returns the number of known GUIs (or GUI paths)
virtual std::size_t getNumGuis() = 0;

// Traverse all known GUIs using the given Visitor
virtual void foreachGui(Visitor& visitor) = 0;

// Returns the GUI appearance type for the given GUI path
virtual GuiType getGuiType(const std::string& guiPath) = 0;

// Reload the gui
virtual void reloadGui(const std::string& guiPath) = 0;

// Returns the _errorList for use in a GUI.
virtual const StringList& getErrorList() = 0;

// Clears out the GUIs and reloads them
virtual void reloadGuis() = 0;
};

}

const char* const MODULE_GUIMANAGER("GuiManager");

// Application-wide Accessor to the global GUI manager
inline gui::IGuiManager& GlobalGuiManager()
{
// Cache the reference locally
static gui::IGuiManager& _manager(
*std::static_pointer_cast<gui::IGuiManager>(
module::GlobalModuleRegistry().getModule(MODULE_GUIMANAGER))
);
return _manager;
}
2 changes: 1 addition & 1 deletion plugins/dm.gui/GuiSelector.cpp
Expand Up @@ -112,7 +112,7 @@ void GuiSelector::fillTrees()
wxutil::VFSTreePopulator popTwo(_twoSidedStore);

ReadablePopulator walker(popOne, popTwo);
gui::GuiManager::Instance().foreachGui(walker);
GlobalGuiManager().foreachGui(walker);

popOne.forEachNode(*this);
popTwo.forEachNode(*this);
Expand Down
4 changes: 2 additions & 2 deletions plugins/dm.gui/ReadableEditorDialog.cpp
Expand Up @@ -1151,7 +1151,7 @@ void ReadableEditorDialog::checkGuiLayout()

std::string msg;

gui::GuiType type = gui::GuiManager::Instance().getGuiType(guiName);
gui::GuiType type = GlobalGuiManager().getGuiType(guiName);

switch (type)
{
Expand Down Expand Up @@ -1264,7 +1264,7 @@ void ReadableEditorDialog::showXdImportSummary()

void ReadableEditorDialog::showGuiImportSummary()
{
XData::StringList errors = gui::GuiManager::Instance().getErrorList();
XData::StringList errors = GlobalGuiManager().getErrorList();
if (errors.empty())
{
wxutil::Messagebox::ShowError(_("No import summary available. Browse Gui Definitions first."), this);
Expand Down
4 changes: 2 additions & 2 deletions plugins/dm.gui/ReadablePopulator.h
Expand Up @@ -34,7 +34,7 @@ class ReadablePopulator :
_popTwo(popTwo),
_progress(_("Analysing Guis")),
_count(0),
_numGuis(gui::GuiManager::Instance().getNumGuis()),
_numGuis(GlobalGuiManager().getNumGuis()),
_evLimiter(50)
{}

Expand All @@ -51,7 +51,7 @@ class ReadablePopulator :
gui::GuiType type;
if (guiType == gui::NOT_LOADED_YET || guiType == gui::UNDETERMINED)
{
type = gui::GuiManager::Instance().getGuiType(guiPath);
type = GlobalGuiManager().getGuiType(guiPath);
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions plugins/dm.gui/ReadableReloader.h
Expand Up @@ -31,7 +31,7 @@ class ReadableReloader :
_count(0),
_evLimiter(50)
{
_numGuis = gui::GuiManager::Instance().getNumGuis();
_numGuis = GlobalGuiManager().getNumGuis();
}

void visit(const std::string& guiPath, const gui::GuiType& guiType)
Expand All @@ -46,18 +46,18 @@ class ReadableReloader :

if (guiType != gui::NOT_LOADED_YET)
{
gui::GuiManager::Instance().reloadGui(guiPath);
GlobalGuiManager().reloadGui(guiPath);
}
}

static void run(const cmd::ArgumentList& args)
{
try
{
gui::GuiManager::Instance().reloadGuis();
GlobalGuiManager().reloadGuis();

ReadableReloader reloader;
gui::GuiManager::Instance().foreachGui(reloader);
GlobalGuiManager().foreachGui(reloader);
}
catch (wxutil::ModalProgressDialog::OperationAbortedException&)
{}
Expand Down
31 changes: 28 additions & 3 deletions plugins/dm.gui/gui/GuiManager.cpp
Expand Up @@ -192,10 +192,35 @@ GuiPtr GuiManager::loadGui(const std::string& guiPath)
}
}

GuiManager& GuiManager::Instance()
const std::string& GuiManager::getName() const
{
static GuiManager _instance;
return _instance;
static std::string _name(MODULE_GUIMANAGER);
return _name;
}

const StringSet& GuiManager::getDependencies() const
{
static StringSet _dependencies;

if (_dependencies.empty())
{
_dependencies.insert(MODULE_VIRTUALFILESYSTEM);
}

return _dependencies;
}

void GuiManager::initialiseModule(const ApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;

// Search the VFS for GUIs
init();
}

void GuiManager::shutdownModule()
{
clear();
}

} // namespace gui
62 changes: 20 additions & 42 deletions plugins/dm.gui/gui/GuiManager.h
@@ -1,11 +1,10 @@
#pragma once

#include "igui.h"
#include "util/Noncopyable.h"
#include <memory>
#include <map>
#include "ifilesystem.h"
#include "string/string.h"
#include <vector>
#include "ThreadedDefLoader.h"

namespace gui
Expand All @@ -17,17 +16,6 @@ namespace
const std::string GUI_EXT("gui");
}

enum GuiType
{
NOT_LOADED_YET, // no attempt to load the GUI has been made
UNDETERMINED, // not checked yet for type
ONE_SIDED_READABLE, // 1-sided
TWO_SIDED_READABLE, // 2-sided
NO_READABLE, // not a readable
IMPORT_FAILURE, // failed to load
FILE_NOT_FOUND, // file doesn't exist
};

class Gui;
typedef std::shared_ptr<Gui> GuiPtr;

Expand All @@ -36,20 +24,9 @@ typedef std::shared_ptr<Gui> GuiPtr;
* including parsing the .gui files on demand.
*/
class GuiManager :
public util::Noncopyable
public IGuiManager,
public util::Noncopyable
{
public:
typedef std::vector<std::string> StringList;

// A visitor class used to traverse all known GUIs by path
class Visitor
{
public:
virtual ~Visitor() {}

virtual void visit(const std::string& guiPath, const GuiType& guiType) = 0;
};

private:
struct GuiInfo
{
Expand Down Expand Up @@ -78,40 +55,41 @@ class GuiManager :
// A List of all the errors occuring lastly.
StringList _errorList;

GuiManager();

public:
GuiManager();

// Gets a GUI from the given VFS path, parsing it on demand
// Returns NULL if the GUI couldn't be found or loaded.
GuiPtr getGui(const std::string& guiPath);
GuiPtr getGui(const std::string& guiPath) override;

// Returns the number of known GUIs (or GUI paths)
std::size_t getNumGuis();
std::size_t getNumGuis() override;

// Traverse all known GUIs using the given Visitor
void foreachGui(Visitor& visitor);
void foreachGui(Visitor& visitor) override;

// Returns the GUI appearance type for the given GUI path
GuiType getGuiType(const std::string& guiPath);
GuiType getGuiType(const std::string& guiPath) override;

// Reload the gui
void reloadGui(const std::string& guiPath);
void reloadGui(const std::string& guiPath) override;

// Returns the _errorList for use in a GUI.
const StringList& getErrorList() { return _errorList; }

// Provides access to the singleton
static GuiManager& Instance();

void init();
const StringList& getErrorList() override { return _errorList; }

// Clears out the GUIs and reloads them
void reloadGuis();
void reloadGuis() override;

// Clears all internal objects
void clear();
// RegisterableModule
const std::string& GuiManager::getName() const override;
const StringSet& GuiManager::getDependencies() const override;
void initialiseModule(const ApplicationContext& ctx) override;
void shutdownModule() override;

private:
void init();
void clear();

// Searches the VFS for all available GUI definitions
void findGuis();

Expand Down
2 changes: 1 addition & 1 deletion plugins/dm.gui/gui/GuiView.h
Expand Up @@ -36,7 +36,7 @@ class GuiView :
// Sets the GUI to render (by VFS path)
void setGui(const std::string& gui)
{
setGui(GuiManager::Instance().getGui(gui));
setGui(GlobalGuiManager().getGui(gui));
}

// Sets the GUI to render (can be NULL to clear this view)
Expand Down
9 changes: 1 addition & 8 deletions plugins/dm.gui/plugin.cpp
Expand Up @@ -76,9 +76,6 @@ class GuiModule :
sigc::mem_fun(this, &GuiModule::onRadiantStartup)
);

// Search the VFS for GUIs
gui::GuiManager::Instance().init();

// Create the Readable Editor Preferences
constructPreferences();
}
Expand Down Expand Up @@ -119,11 +116,6 @@ class GuiModule :

page.appendPathEntry(_("Custom Folder"), ui::RKEY_READABLES_CUSTOM_FOLDER, true);
}

void shutdownModule()
{
gui::GuiManager::Instance().clear();
}
};
typedef std::shared_ptr<GuiModule> GuiModulePtr;

Expand All @@ -132,4 +124,5 @@ extern "C" void DARKRADIANT_DLLEXPORT RegisterModule(IModuleRegistry& registry)
module::performDefaultInitialisation(registry);

registry.registerModule(GuiModulePtr(new GuiModule));
registry.registerModule(std::make_shared<gui::GuiManager>());
}
1 change: 1 addition & 0 deletions tools/msvc/include.vcxproj
Expand Up @@ -133,6 +133,7 @@
<ClInclude Include="..\..\include\igrid.h" />
<ClInclude Include="..\..\include\igroupdialog.h" />
<ClInclude Include="..\..\include\igroupnode.h" />
<ClInclude Include="..\..\include\igui.h" />
<ClInclude Include="..\..\include\iimage.h" />
<ClInclude Include="..\..\include\iinteractiveview.h" />
<ClInclude Include="..\..\include\ilayer.h" />
Expand Down

0 comments on commit 7b950b9

Please sign in to comment.