Dialog XML definitions are now loaded thru the resource manager

This commit is contained in:
2023-01-07 11:59:42 -05:00
parent c3dd886783
commit e3d6a4748e
25 changed files with 222 additions and 108 deletions

View File

@@ -10,8 +10,9 @@
#include <functional>
#include "dialogxml/widgets/control.hpp"
#include "fileio/resmgr/res_dialog.hpp"
cChoiceDlog::cChoiceDlog(std::string file, std::vector<std::string> buttons, cDialog* p) : dlg(file, p) {
cChoiceDlog::cChoiceDlog(std::string file, std::vector<std::string> buttons, cDialog* p) : dlg(*ResMgr::dialogs.get(file), p) {
using namespace std::placeholders;
std::vector<std::string>::iterator iter = buttons.begin();
while(iter != buttons.end()){

View File

@@ -10,6 +10,7 @@
#include <stdexcept>
#include "dialog.hpp"
#include "gfx/tiling.hpp" // for bg
#include "fileio/resmgr/res_dialog.hpp"
#include "sounds.hpp"
#include "dialogxml/widgets/pict.hpp"
#include "dialogxml/widgets/button.hpp"
@@ -171,20 +172,17 @@ cKey cControl::parseKey(string what){
cDialog::cDialog(cDialog* p) : parent(p) {}
cDialog::cDialog(std::string path, cDialog* p) : parent(p) {
loadFromFile(path + ".xml");
cDialog::cDialog(const DialogDefn& file, cDialog* p) : parent(p) {
loadFromFile(file);
}
extern fs::path progDir;
void cDialog::loadFromFile(std::string path){
void cDialog::loadFromFile(const DialogDefn& file){
static const cKey enterKey = {true, key_enter};
bg = defaultBackground;
fname = path;
fs::path cPath = progDir/"data"/"dialogs"/path;
fname = file.id;
try{
TiXmlBase::SetCondenseWhiteSpace(false);
Document xml(cPath.string().c_str());
xml.LoadFile();
const Document& xml = file.defn;
Iterator<Attribute> attr;
Iterator<Element> node;

View File

@@ -29,6 +29,7 @@
class cControl;
class cTextField;
struct DialogDefn;
/// Specifies the relative position of a control's labelling text.
enum eLabelPos {
@@ -62,7 +63,7 @@ class cDialog {
std::string currentFocus;
cDialog* parent;
std::string generateRandomString();
void loadFromFile(std::string path);
void loadFromFile(const DialogDefn& file);
template<typename Iter> void handleTabOrder(std::string& itemHit, Iter begin, Iter end);
std::vector<std::pair<std::string,cTextField*>> tabOrder;
static cDialog* topWindow; // Tracks the frontmost dialog.
@@ -80,7 +81,7 @@ public:
/// Creates a new dialog, loading its definition from a file.
/// @param path The name of the file to load. It must be in the game's dialogs directory.
/// @param p Optionally, a parent dialog.
explicit cDialog(std::string path, cDialog* p = nullptr); // cd_create_dialog
explicit cDialog(const DialogDefn& file, cDialog* p = nullptr); // cd_create_dialog
~cDialog(); // cd_kill_dialog
/// Add a new control to the dialog.
/// @param what A pointer to the control, which should already be constructed.

View File

@@ -11,10 +11,15 @@
#include <sstream>
#include <boost/lexical_cast.hpp>
#include "dialogxml/widgets/pict.hpp"
#include "fileio/resmgr/res_dialog.hpp"
static DialogDefn& loadDefn() {
return *ResMgr::dialogs.get("choose-pict");
}
cPictChoice::cPictChoice(const std::vector<pic_num_t>& pics,ePicType t,cDialog* parent) : cPictChoice(pics.begin(), pics.end(), t, parent) {}
cPictChoice::cPictChoice(const std::vector<std::pair<pic_num_t,ePicType>>& pics,cDialog* parent) : dlg("choose-pict",parent) {
cPictChoice::cPictChoice(const std::vector<std::pair<pic_num_t,ePicType>>& pics,cDialog* parent) : dlg(loadDefn(),parent) {
picts = pics;
attachHandlers();
}
@@ -24,14 +29,14 @@ cPictChoice::cPictChoice(
std::vector<pic_num_t>::const_iterator end,
ePicType t,
cDialog* parent
) : dlg("choose-pict",parent) {
) : dlg(loadDefn(),parent) {
for(auto iter = begin; iter != end; iter++) {
picts.push_back({*iter,t});
}
attachHandlers();
}
cPictChoice::cPictChoice(pic_num_t first, pic_num_t last, ePicType t, cDialog* parent) : dlg("choose-pict",parent) {
cPictChoice::cPictChoice(pic_num_t first, pic_num_t last, ePicType t, cDialog* parent) : dlg(loadDefn(),parent) {
for(pic_num_t i = first; i <= last; i++) {
picts.push_back({i,t});
}

View File

@@ -13,11 +13,17 @@
#include <boost/lexical_cast.hpp>
#include "fileio/resmgr/res_dialog.hpp"
static DialogDefn& loadDefn() {
return *ResMgr::dialogs.get("choose-string");
}
cStringChoice::cStringChoice(
std::vector<std::string>& strs,
std::string title,
cDialog* parent
) : dlg("choose-string",parent) {
) : dlg(loadDefn(),parent) {
if(!title.empty()) dlg["title"].setText(title);
strings = strs;
attachHandlers();
@@ -28,7 +34,7 @@ cStringChoice::cStringChoice(
std::vector<std::string>::iterator end,
std::string title,
cDialog* parent
) : dlg("choose-string",parent) {
) : dlg(loadDefn(),parent) {
if(!title.empty()) dlg["title"].setText(title);
copy(begin,end,std::inserter(strings, strings.begin()));
attachHandlers();

View File

@@ -11,18 +11,19 @@
#include <sstream>
#include "tools/winutil.hpp"
#include "dialogxml/widgets/pict.hpp"
#include "fileio/resmgr/res_dialog.hpp"
std::string cStrDlog::getFileName(short n_strs, ePicType type, bool hasTitle){
DialogDefn& cStrDlog::getDefn(short n_strs, ePicType type, bool hasTitle){
std::ostringstream sout;
sout << n_strs << "str";
if(hasTitle) sout << "-title";
if(type == PIC_DLOG_LG || type == PIC_CUSTOM_DLOG_LG || type == PIC_SCEN_LG)
sout << "-lg";
return sout.str();
return *ResMgr::dialogs.get(sout.str());
}
cStrDlog::cStrDlog(std::string str1,std::string str2,std::string title,pic_num_t pic,ePicType t,cDialog* parent)
: dlg(cStrDlog::getFileName((str1 != "") + (str2 != ""), t, title != ""), parent), type(t) {
: dlg(cStrDlog::getDefn((str1 != "") + (str2 != ""), t, title != ""), parent), type(t) {
using namespace std::placeholders;
cPict& pic_ctrl = dynamic_cast<cPict&>(dlg["pict"]);
pic_ctrl.setPict(pic, type);

View File

@@ -20,7 +20,7 @@ typedef std::function<void(cDialog&)> record_callback_t;
/// A simple dialog with one or two long strings, an optional title, and an optional record button.
class cStrDlog {
static std::string getFileName(short n_strs, ePicType type, bool hasTitle);
static struct DialogDefn& getDefn(short n_strs, ePicType type, bool hasTitle);
cDialog dlg;
short sound = -1;
record_callback_t rec_f;