Files
oboe/osx/dialogxml/dialog.cpp
Celtic Minstrel c413d292a9 Tear out most of the legacy code in the game - see below for details
(PC Editor and Scenario Editor are unaffected by this commit.)

Things removed:
- All references to Carbon and QuickDraw are gone.
- No more Resource Manager; the game no longer relies on old resource files at all
- The old dialog framework (from dlogtool.c, functions usually prefixed with cd_) is no longer used.
- Files that weren't being compiled
- Boost libraries that are now in the C++ library (function, shared_ptr)
- Obsolete build settings

Replacement dependencies:
- Boost Filesystem replaces references to things like FSSpec
- SFML replaces all the QuickDraw code and most window management
- Cocoa replaces AppleEvent management, menu management, and some window management
- I wrote a resource manager interface to mimick the important aspects of the behaviour of the Mac Resource Manager
- I had to rewrite some functions that QuickDraw provided natively, such as clipping regions; not all of these are tested

Things added:
- Every referenced dialog has been converted from a DITL resource into the new XML-based dialog format.
- All referenced STR# resources have been extracted into simple text files
- Now compiles against Mac OSX 10.7 SDK and libc++
- The first item in the Help menu opens the docs on Sylae's website
- It seems all the constants for soundtool vanished somewhere, so I added them back from the original Mac source

Other changes:
- Updated to XCode 4(?) project format
- Added the xcschemes created by XCode 4; I'm not sure how important these are, but they seem like they might be important
- Added document on converting dialogs to the XML format.
- Make string formatting mismatches into errors instead of warnings
- Disable error limit
- Graphics sheets that previously used masking now have alpha transparency
- Converted all graphics sheets to 8-bit PNG
- Trimmed white border from intro image
- Converted dialogs in the resource file have their resource name set to the name of the XML file of the converted version
- Referenced string resources in the resource file have their resource name set to the name of the text file of the extracted version
- Add the black-and-white patterns from the PAT resources to the pixpats.png; at least one of them is used somewhere in the game
- Recreated the menu.xib as a Cocoa xib file instead of a Carbon xib file
- Disable GNU C++ extensions; maybe this'll make it easier to compile with cl.exe later
- Add marks to the enormous handle_action function to make it easier to navigate\
- A build step to validate the XML dialogs using xmllint (doesn't quite work properly yet but does at least catch dialogs that are not well-formed)- Fix a lot of warnings about assigning string constants to non-const char pointers
- Fixed the file and application icons (which had somehow become corrupted at some point)
- Lots of additional functions in the custom location and rectangle classes, including implicit conversion to and from SFML rects and vectors; also they now store coordinates as int instead of char
- A new enum for encounter note types
- Much tweaking of the encounter note recording mechanisms
- To ease porting, I added a simple function that converts from classic Mac ticks (about 1/60 of a second) to the SFML time type
- Python script to convert STR# resources to txt files, replacing newlines with vertical bars
- Extracted the Mac font (Dungeon Bold) from the resource file and also added the Windows font (MaidenWord)

XML Dialog Framework changes:
- Remove the static initialization object in favour of manually calling cDialog::init()
- {set,get}Format() no longer used for colour; there's a dedicated {set,get}Colour() instead
- draw() methods unprotected in the control classes so that controls can be drawn in the main window
- There's no longer a friend relationship between the dialogs and the controls
- Fixed buttons duplicating the "depressed" boolean
- Buttons now properly offset the label for tiny and push buttons, and for LEDs
- Buttons no longer assume that either none or both of "width" and "height" are given in the XML
- Add {get,set}BtnType() to cButton
- cLedGroup now overrides handleClick(), which has also been made virtual; this was necessary for LEDs within a group to properly hilite while being clicked
- Add addChoice() to cLedGroup to insert additional LED choices
- Moved the key enums and cKey to a separate file
- Add a method to get a control's attached key
- Add methods to get and set a control's rect and position
- Controls can now directly take a window as a parent rather than a dialog
- Add addLabelFor() method to cDialog which adds a static text control as a label for another control
- Remove hack for storing a dialog result of arbitrary type in favour of boost::any
- Add method to get default text colour for a dialog
- Add method to get a dialog's rect
- Add method to add the same event handler to multiple controls in a dialog
- Add concept of default button to dialogs
- Add enum for text field type (currently either number or text)
- Implement the text field without use of native controls, including somewhat decent text input and a flashing insertion point
- cPict no longer stores references to every sheet; it fetches them as needed from the resource manager
- The many draw functions in cPict are no longer static, since they need to access the window containing the pict (before they could only be static due to QuickDraw's global state)
- Add setPict() without a type argument to change the graphic without changing the type, which is a common operation
- Add a scrollbar control; the specifics aren't implemented yet
- Change signature of the record callback for cStrDlog; it will no longer be passed the strings
- Publicize the no-button constructor of cChoiceDlog; it'll assume "okay" is the only button
- Add operator-> to cPictChoice for accessing the underlying dialog
- Add constructor to cPictChoice that takes a starting and ending pic num
- Remove err parameter from giveError
- Many more keys handled, plus support for catching copy, paste, cut, and select all keyboard shortcuts
- Text input fields take priority, overriding any other keyboard shortcuts in the dialog, but they never catch help (F1), escape, or enter
- Some changes to the format itself:
-> keys go in the "def-key" attribute, but modifiers go in "key-mod"
-> "clickable" is no longer a recognized attribute
-> "title" is now a recognized text size (18pt, even larger than "large" at 12pt)
-> "defbtn" attribute on the root element
2014-04-14 13:52:01 -04:00

1312 lines
38 KiB
C++

/*
* dialog.cpp
* BoE
*
* Created by Celtic Minstrel on 11/05/09.
*
*/
#include <cmath>
#include <stdexcept>
#include <boost/type_traits/is_pointer.hpp>
#include "dialog.h"
#include "graphtool.h"
#include "soundtool.h"
using namespace std;
using namespace ticpp;
#include "pict.h"
#include "button.h"
#include "field.h"
#include "message.h"
#include "winutil.h"
#include "mathutil.h"
// TODO: Would be nice if I could avoid depending on mainPtr
extern sf::RenderWindow mainPtr;
extern sf::Texture bg_gworld;
extern bool play_sounds;
const short cDialog::BG_DARK = 5, cDialog::BG_LIGHT = 16;
static std::string generateRandomString(){
// Not bothering to seed, because it doesn't actually matter if it's truly random.
// Though, this will be called after srand() is called in main() anyway.
int n_chars = rand() % 100;
std::string s = "$";
while(n_chars > 0){
s += char(rand() % 96) + ' '; // was 223 ...
n_chars--;
}
return s;
}
template<> pair<string,cPict*> cDialog::parse(Element& who /*pict*/){
std::pair<std::string,cPict*> p;
Iterator<Attribute> attr;
std::string name;
bool wide = false, tall = false, custom = false;
bool foundTop = false, foundLeft = false, foundType = false, foundNum = false; // required attributes
RECT frame;
int width = 0, height = 0;
p.second = new cPict(*this);
for(attr = attr.begin(&who); attr != attr.end(); attr++){
attr->GetName(&name);
if(name == "name")
attr->GetValue(&p.first);
else if(name == "type"){
std::string val;
foundType = true;
attr->GetValue(&val);
pic_num_t wasPic = p.second->getPicNum();
if(val == "blank"){
p.second->setPict(-1, PIC_TER);
}else if(val == "ter")
p.second->setPict(wasPic, PIC_TER);
else if(val == "teranim")
p.second->setPict(wasPic, PIC_TER_ANIM);
else if(val == "monst")
p.second->setPict(wasPic, PIC_MONST);
else if(val == "dlog")
p.second->setPict(wasPic, PIC_DLOG);
else if(val == "talk")
p.second->setPict(wasPic, PIC_TALK);
else if(val == "scen")
p.second->setPict(wasPic, PIC_SCEN);
else if(val == "item")
p.second->setPict(wasPic, PIC_ITEM);
else if(val == "pc")
p.second->setPict(wasPic, PIC_PC);
else if(val == "field")
p.second->setPict(wasPic, PIC_FIELD);
else if(val == "boom")
p.second->setPict(wasPic, PIC_BOOM);
else if(val == "missile")
p.second->setPict(wasPic, PIC_MISSILE);
else if(val == "full")
p.second->setPict(wasPic, PIC_FULL);
else if(val == "map")
p.second->setPict(wasPic, PIC_TER_MAP);
else if(val == "status")
p.second->setPict(wasPic, PIC_STATUS);
else throw xBadVal("pict",name,val,attr->Row(),attr->Column());
}else if(name == "custom"){
std::string val;
attr->GetValue(&val);
if(val == "true") custom = true;
}else if(name == "size"){
std::string val;
attr->GetValue(&val);
if(val == "wide") wide = true;
else if(val == "tall") tall = true;
else if(val == "large") wide = tall = true;
else throw xBadVal("pict",name,val,attr->Row(),attr->Column());
}else if(name == "def-key"){
std::string val;
attr->GetValue(&val);
// TODO: The modifiers are now in key-mod, so this needs to be updated
try{
p.second->attachKey(parseKey(val));
}catch(int){
throw xBadVal("pict",name,val,attr->Row(),attr->Column());
}
}else if(name == "num"){
pic_num_t newPic;
attr->GetValue(&newPic), foundNum = true;
p.second->setPict(newPic);
}else if(name == "top"){
attr->GetValue(&frame.top), foundTop = true;
}else if(name == "left"){
attr->GetValue(&frame.left), foundLeft = true;
}else if(name == "width"){
attr->GetValue(&width);
}else if(name == "height"){
attr->GetValue(&height);
}else throw xBadAttr("pict",name,attr->Row(),attr->Column());
}
if(!foundType) throw xMissingAttr("pict","type",who.Row(),who.Column());
if(!foundNum) throw xMissingAttr("pict","num",who.Row(),who.Column());
if(!foundTop) throw xMissingAttr("pict","top",who.Row(),who.Column());
if(!foundLeft) throw xMissingAttr("pict","left",who.Row(),who.Column());
if(wide || tall) {
pic_num_t wasPic = p.second->getPicNum();
if(wide && !tall && p.second->getPicType() == PIC_MONST) p.second->setPict(wasPic, PIC_MONST_WIDE);
else if(!wide && tall && p.second->getPicType() == PIC_MONST) p.second->setPict(wasPic, PIC_MONST_TALL);
else if(wide && tall){
if(p.second->getPicType() == PIC_MONST) p.second->setPict(wasPic, PIC_MONST_LG);
else if(p.second->getPicType() == PIC_SCEN) p.second->setPict(wasPic, PIC_SCEN_LG);
else if(p.second->getPicType() == PIC_DLOG) p.second->setPict(wasPic, PIC_DLOG_LG);
}
}
frame.right = frame.left;
frame.bottom = frame.top;
if(width > 0 || height > 0 || p.second->getPicType() == PIC_FULL){
frame.right = frame.left + width;
frame.bottom = frame.top + height;
}else switch(p.second->getPicType()){
case PIC_DLOG:
frame.right = frame.left + 36;
frame.bottom = frame.top + 36;
break;
case PIC_DLOG_LG:
frame.right = frame.left + 72;
frame.bottom = frame.top + 72;
break;
case PIC_SCEN:
case PIC_TALK:
frame.right = frame.left + 32;
frame.bottom = frame.top + 32;
break;
case PIC_SCEN_LG:
frame.right = frame.left + 64;
frame.bottom = frame.top + 64;
break;
case PIC_MISSILE:
frame.right = frame.left + 18;
frame.bottom = frame.top + 18;
break;
case PIC_TER_MAP:
frame.right = frame.left + 24;
frame.bottom = frame.top + 24;
break;
case PIC_STATUS:
frame.right = frame.left + 12;
frame.bottom = frame.top + 12;
break;
case PIC_FULL:
// TODO: Do some handling here to determine the sheet to use, and perhaps to load and set it.
break;
default:
frame.right = frame.left + 28;
frame.bottom = frame.top + 36;
break;
}
p.second->setBounds(frame);
if(custom) {
pic_num_t wasPic = p.second->getPicNum();
p.second->setPict(wasPic, p.second->getPicType() + PIC_CUSTOM);
}
if(p.first == ""){
do{
p.first = generateRandomString();
}while(controls.find(p.first) != controls.end());
}
return p;
}
template<> pair<string,cTextMsg*> cDialog::parse(Element& who /*text*/){
pair<string,cTextMsg*> p;
Iterator<Attribute> attr;
Iterator<Node> node;
string name;
int width = 0, height = 0;
bool foundTop = false, foundLeft = false; // top and left are required attributes
RECT frame;
p.second = new cTextMsg(this);
if(bg == BG_DARK) p.second->setColour(sf::Color::White);
for(attr = attr.begin(&who); attr != attr.end(); attr++){
attr->GetName(&name);
if(name == "name")
attr->GetValue(&p.first);
else if(name == "framed"){
std::string val;
attr->GetValue(&val);
if(val == "true") p.second->setFormat(TXT_FRAME, true);
}else if(name == "font"){
std::string val;
attr->GetValue(&val);
if(val == "dungeon")
p.second->setFormat(TXT_FONT, DUNGEON);
else if(val == "geneva")
p.second->setFormat(TXT_FONT, GENEVA);
else if(val == "silom")
p.second->setFormat(TXT_FONT, SILOM);
else if(val == "maidenword")
p.second->setFormat(TXT_FONT, MAIDENWORD);
else throw xBadVal("text",name,val,attr->Row(),attr->Column());
}else if(name == "size"){
std::string val;
attr->GetValue(&val);
if(val == "large")
p.second->setFormat(TXT_SIZE, 12);
else if(val == "small")
p.second->setFormat(TXT_SIZE, 10);
else if(val == "title")
p.second->setFormat(TXT_SIZE, 18);
else throw xBadVal("text",name,val,attr->Row(),attr->Column());
}else if(name == "color" || name == "colour"){
std::string val;
attr->GetValue(&val);
sf::Color clr;
try{
clr = parseColor(val);
}catch(int){
throw xBadVal("text",name,val,attr->Row(),attr->Column());
}
p.second->setColour(clr);
}else if(name == "def-key"){
std::string val;
attr->GetValue(&val);
try{
p.second->attachKey(parseKey(val));
}catch(int){
throw xBadVal("text",name,val,attr->Row(),attr->Column());
}
}else if(name == "top"){
attr->GetValue(&frame.top), foundTop = true;
}else if(name == "left"){
attr->GetValue(&frame.left), foundLeft = true;
}else if(name == "width"){
attr->GetValue(&width);
}else if(name == "height"){
attr->GetValue(&height);
// }else if(name == "fromlist"){
// attr->GetValue(&p.second->fromList);
}else throw xBadAttr("pict",name,attr->Row(),attr->Column());
}
if(!foundTop) throw xMissingAttr("text","top",who.Row(),who.Column());
if(!foundLeft) throw xMissingAttr("text","left",who.Row(),who.Column());
frame.right = frame.left + width;
frame.bottom = frame.top + height;
p.second->setBounds(frame);
string content;
for(node = node.begin(&who); node != node.end(); node++){
string val;
int type = node->Type();
node->GetValue(&val);
// TODO: Strip out tabs and newlines
// TODO: De-magic the | character
if(type == TiXmlNode::ELEMENT && val == "br") content += '|'; // because vertical bar is replaced by a newline when drawing strings
else if(type == TiXmlNode::TEXT) content += val;
else{
val = '<' + val + '>';
throw xBadVal("text","<content>",content + val,node->Row(),node->Column());
}
}
p.second->setText(content);
if(p.first == ""){
do{
p.first = generateRandomString();
}while(controls.find(p.first) != controls.end());
}
return p;
}
/**
* Parses an HTML-style colour string, recognizing three-digit hex, six-digit hex, and HTML colour names.
*/
sf::Color cDialog::parseColor(string what){
sf::Color clr;
if(what[0] == '#'){
unsigned int r,g,b;
if(sscanf(what.c_str(),"#%2x%2x%2x",&r,&g,&b) < 3)
if(sscanf(what.c_str(),"#%1x%1x%1x",&r,&g,&b) < 3)
throw -1;
clr.r = r, clr.g = g, clr.b = b;
}else if(what == "black")
clr.r = 0x00, clr.g = 0x00, clr.b = 0x00;
else if(what == "red")
clr.r = 0xFF, clr.g = 0x00, clr.b = 0x00;
else if(what == "lime")
clr.r = 0x00, clr.g = 0xFF, clr.b = 0x00;
else if(what == "blue")
clr.r = 0x00, clr.g = 0x00, clr.b = 0xFF;
else if(what == "yellow")
clr.r = 0xFF, clr.g = 0xFF, clr.b = 0x00;
else if(what == "aqua")
clr.r = 0x00, clr.g = 0xFF, clr.b = 0xFF;
else if(what == "fuchsia")
clr.r = 0xFF, clr.g = 0x00, clr.b = 0xFF;
else if(what == "white")
clr.r = 0xFF, clr.g = 0xFF, clr.b = 0xFF;
else if(what == "gray" || what == "grey")
clr.r = 0x80, clr.g = 0x80, clr.b = 0x80;
else if(what == "maroon")
clr.r = 0x80, clr.g = 0x00, clr.b = 0x00;
else if(what == "green")
clr.r = 0x00, clr.g = 0x80, clr.b = 0x00;
else if(what == "navy")
clr.r = 0x00, clr.g = 0x00, clr.b = 0x80;
else if(what == "olive")
clr.r = 0x80, clr.g = 0x80, clr.b = 0x00;
else if(what == "teal")
clr.r = 0x00, clr.g = 0x80, clr.b = 0x80;
else if(what == "purple")
clr.r = 0x80, clr.g = 0x00, clr.b = 0x80;
else if(what == "silver")
clr.r = 0xC0, clr.g = 0xC0, clr.b = 0xC0;
else throw -1;
return clr;
}
template<> pair<string,cButton*> cDialog::parse(Element& who /*button*/){
pair<string,cButton*> p;
Iterator<Attribute> attr;
Iterator<Node> node;
string name;
int width = 0, height = 0;
bool foundType = false, foundTop = false, foundLeft = false; // required attributes
bool foundKey = false;
std::string keyMod, keyMain;
int keyModRow, keyModCol, keyMainRow, keyMainCol;
RECT frame;
p.second = new cButton(this);
for(attr = attr.begin(&who); attr != attr.end(); attr++){
attr->GetName(&name);
if(name == "name")
attr->GetValue(&p.first);
else if(name == "wrap"){
std::string val;
attr->GetValue(&val);
if(val == "true") p.second->setFormat(TXT_WRAP, true);
}else if(name == "type"){
std::string val;
foundType = true;
attr->GetValue(&val);
if(val == "small")
p.second->setBtnType(BTN_SM);
else if(val == "regular")
p.second->setBtnType(BTN_REG);
else if(val == "large")
p.second->setBtnType(BTN_LG);
else if(val == "help")
p.second->setBtnType(BTN_HELP);
else if(val == "left")
p.second->setBtnType(BTN_LEFT);
else if(val == "right")
p.second->setBtnType(BTN_RIGHT);
else if(val == "up")
p.second->setBtnType(BTN_UP);
else if(val == "down")
p.second->setBtnType(BTN_DOWN);
else if(val == "tiny")
p.second->setBtnType(BTN_TINY);
else if(val == "done")
p.second->setBtnType(BTN_DONE);
else if(val == "tall")
p.second->setBtnType(BTN_TALL);
else if(val == "trait")
p.second->setBtnType(BTN_TRAIT);
else if(val == "push")
p.second->setBtnType(BTN_PUSH);
}else if(name == "def-key"){
attr->GetValue(&keyMain);
foundKey = true;
keyMainRow = attr->Row();
keyMainCol = attr->Column();
}else if(name == "key-mod"){
attr->GetValue(&keyMod);
foundKey = true;
keyModRow = attr->Row();
keyModCol = attr->Column();
// }else if(name == "fromlist"){
// attr->GetValue(&p.second->fromList);
}else if(name == "top"){
attr->GetValue(&frame.top), foundTop = true;
}else if(name == "left"){
attr->GetValue(&frame.left), foundLeft = true;
}else if(name == "width"){
attr->GetValue(&width);
}else if(name == "height"){
attr->GetValue(&height);
}else throw xBadAttr("button",name,attr->Row(),attr->Column());
}
if(bg == BG_DARK && p.second->getBtnType() == BTN_TINY) p.second->setColour(sf::Color::White);
if(!foundType) throw xMissingAttr("button","type",who.Row(),who.Column());
if(!foundTop) throw xMissingAttr("button","top",who.Row(),who.Column());
if(!foundLeft) throw xMissingAttr("button","left",who.Row(),who.Column());
if(foundKey) {
cKey theKey;
try{
theKey = parseKey(keyMod + " " + keyMain);
}catch(int){
try {
theKey = parseKey(keyMain);
}catch(int){
throw xBadVal("button","def-key",keyMain,keyMainRow,keyMainCol);
}
throw xBadVal("button","key-mod",keyMod,keyModRow,keyModCol);
}
p.second->attachKey(theKey);
}
if(width > 0 || height > 0) {
// TODO: What if width is set but height isn't?
frame.right = frame.left + width;
frame.bottom = frame.top + height;
}else switch(p.second->getBtnType()){
case BTN_SM:
frame.right = frame.left + 23;
frame.bottom = frame.top + 23;
break;
case BTN_LG:
frame.right = frame.left + 102;
frame.bottom = frame.top + 23;
break;
case BTN_HELP:
frame.right = frame.left + 16;
frame.bottom = frame.top + 13;
break;
case BTN_TINY:
case BTN_LED: // this should never happen
frame.right = frame.left + 14;
frame.bottom = frame.top + 10;
break;
case BTN_TALL:
case BTN_TRAIT:
frame.right = frame.left + 63;
frame.bottom = frame.top + 40;
break;
case BTN_PUSH:
frame.right = frame.left + 30;
frame.bottom = frame.top + 30;
break;
default:
frame.right = frame.left + 63;
frame.bottom = frame.top + 23;
}
p.second->setBounds(frame);
string content;
for(node = node.begin(&who); node != node.end(); node++){
string val;
int type = node->Type();
node->GetValue(&val);
if(type == TiXmlNode::ELEMENT && val == "key"){
// TODO: There's surely a better way to do this
if(content.length() > 0) throw xBadVal("button","<content>",content + val,node->Row(),node->Column());
// p.second->labelWithKey = true;
}else if(type == TiXmlNode::TEXT) content += val;
else{
val = '<' + val + '>';
throw xBadVal("text","<content>",val,node->Row(),node->Column());
}
}
p.second->setText(content);
if(p.first == ""){
do{
p.first = generateRandomString();
}while(controls.find(p.first) != controls.end());
}
return p;
}
cKey cDialog::parseKey(string what){
cKey key;
key.spec = false;
key.c = 0;
key.mod = mod_none;
if(what == "none") return key;
istringstream sin(what);
string parts[4];
sin >> parts[0] >> parts[1] >> parts[2] >> parts[3];
for(int i = 0; i < 4; i++){
if(parts[i] == "ctrl") key.mod += mod_ctrl;
else if(parts[i] == "alt") key.mod += mod_alt;
else if(parts[i] == "shift") key.mod += mod_shift;
else if(parts[i] == "left") {
key.spec = true;
key.k = key_left;
break;
}else if(parts[i] == "right") {
key.spec = true;
key.k = key_right;
break;
}else if(parts[i] == "up") {
key.spec = true;
key.k = key_up;
break;
}else if(parts[i] == "down") {
key.spec = true;
key.k = key_down;
break;
}else if(parts[i] == "esc") {
key.spec = true;
key.k = key_esc;
break;
}else if(parts[i] == "enter" || parts[i] == "return") {
key.spec = true;
key.k = key_enter;
break;
}else if(parts[i] == "tab") {
key.spec = true;
key.k = key_tab;
break;
}else if(parts[i] == "help") {
key.spec = true;
key.k = key_help;
break;
}else if(parts[i] == "space") {
key.c = ' ';
break;
}else if(parts[i].length() == 1) {
key.c = parts[i][0];
break;
}else throw -1;
}
return key;
}
template<> pair<string,cLed*> cDialog::parse(Element& who /*LED*/){
pair<string,cLed*> p;
Iterator<Attribute> attr;
Iterator<Node> node;
string name;
int width = 0, height = 0;
bool foundTop = false, foundLeft = false; // requireds
RECT frame;
p.second = new cLed(this);
if(bg == BG_DARK) p.second->setColour(sf::Color::White);
for(attr = attr.begin(&who); attr != attr.end(); attr++){
attr->GetName(&name);
if(name == "name")
attr->GetValue(&p.first);
else if(name == "state"){
std::string val;
attr->GetValue(&val);
if(val == "red") p.second->setState(led_red);
else if(val == "green") p.second->setState(led_green);
else if(val == "off") p.second->setState(led_off);
else throw xBadVal("led",name,val,attr->Row(),attr->Column());
// }else if(name == "fromlist"){
// attr->GetValue(&p.second->fromList);
}else if(name == "font"){
std::string val;
attr->GetValue(&val);
if(val == "dungeon")
p.second->setFormat(TXT_FONT, DUNGEON);
else if(val == "geneva")
p.second->setFormat(TXT_FONT, GENEVA);
else if(val == "silom")
p.second->setFormat(TXT_FONT, SILOM);
else if(val == "maidenword")
p.second->setFormat(TXT_FONT, MAIDENWORD);
else throw xBadVal("text",name,val,attr->Row(),attr->Column());
}else if(name == "size"){
std::string val;
attr->GetValue(&val);
if(val == "large")
p.second->setFormat(TXT_SIZE, 12);
else if(val == "small")
p.second->setFormat(TXT_SIZE, 10);
if(val == "title")
p.second->setFormat(TXT_SIZE, 18);
else throw xBadVal("text",name,val,attr->Row(),attr->Column());
}else if(name == "color" || name == "colour"){
std::string val;
attr->GetValue(&val);
sf::Color clr;
try{
clr = parseColor(val);
}catch(int){
throw xBadVal("text",name,val,attr->Row(),attr->Column());
}
p.second->setColour(clr);
}else if(name == "top"){
attr->GetValue(&frame.top), foundTop = true;
}else if(name == "left"){
attr->GetValue(&frame.left), foundLeft = true;
}else if(name == "width"){
attr->GetValue(&width);
}else if(name == "height"){
attr->GetValue(&height);
}else throw xBadAttr("button",name,attr->Row(),attr->Column());
}
if(!foundTop) throw xMissingAttr("led","top",who.Row(),who.Column());
if(!foundLeft) throw xMissingAttr("led","left",who.Row(),who.Column());
if(width > 0) {
frame.right = frame.left + width;
}else{
frame.right = frame.left + 14;
}
if(height > 0) {
frame.bottom = frame.top + height;
}else{
frame.bottom = frame.top + 10;
}
p.second->setBounds(frame);
string content;
for(node = node.begin(&who); node != node.end(); node++){
string val;
int type = node->Type();
node->GetValue(&val);
if(type == TiXmlNode::TEXT) content += val;
else{
val = '<' + val + '>';
throw xBadVal("text","<content>",content + val,node->Row(),node->Column());
}
}
p.second->setText(content);
if(p.first == ""){
do{
p.first = generateRandomString();
}while(controls.find(p.first) != controls.end());
}
return p;
}
template<> pair<string,cLedGroup*> cDialog::parse(Element& who /*group*/){
pair<string,cLedGroup*> p;
Iterator<Attribute> attr;
Iterator<Element> node;
string name;
p.second = new cLedGroup(this);
for(attr = attr.begin(&who); attr != attr.end(); attr++){
attr->GetName(&name);
if(name == "name")
attr->GetValue(&p.first);
// else if(name == "fromlist")
// attr->GetValue(&p.second->fromList);
else throw xBadAttr("button",name,attr->Row(),attr->Column());
}
string content;
for(node = node.begin(&who); node != node.end(); node++){
string val;
int type = node->Type();
node->GetValue(&val);
if(type == TiXmlNode::ELEMENT && val == "led"){
auto led = parse<cLed>(*node);
p.second->addChoice(led.second, led.first);
}else{
val = '<' + val + '>';
throw xBadVal("text","<content>", content + val,node->Row(),node->Column());
}
}
p.second->setText(content);
p.second->recalcRect();
if(p.first == ""){
do{
p.first = generateRandomString();
}while(controls.find(p.first) != controls.end());
}
return p;
}
template<> pair<string,cTextField*> cDialog::parse(Element& who /*field*/){
pair<string,cTextField*> p;
Iterator<Attribute> attr;
Iterator<Node> node;
string name;
int width = 0, height = 0;
bool foundTop = false, foundLeft = false; // requireds
RECT frame;
p.second = new cTextField(this);
for(attr = attr.begin(&who); attr != attr.end(); attr++){
attr->GetName(&name);
if(name == "name")
attr->GetValue(&p.first);
else if(name == "type"){
std::string val;
attr->GetValue(&val);
if(val == "num")
p.second->setInputType(FLD_NUM);
else if(val == "text")
p.second->setInputType(FLD_TEXT);
else throw xBadVal("field",name,val,attr->Row(),attr->Column());
}else if(name == "top"){
attr->GetValue(&frame.top), foundTop = true;
}else if(name == "left"){
attr->GetValue(&frame.left), foundLeft = true;
}else if(name == "width"){
attr->GetValue(&width);
}else if(name == "height"){
attr->GetValue(&height);
}else throw xBadAttr("button",name,attr->Row(),attr->Column());
}
if(!foundTop) throw xMissingAttr("field","top",attr->Row(),attr->Column());
if(!foundLeft) throw xMissingAttr("field","left",attr->Row(),attr->Column());
frame.right = frame.left + width;
frame.bottom = frame.top + height;
p.second->setBounds(frame);
if(p.first == ""){
do{
p.first = generateRandomString();
}while(controls.find(p.first) != controls.end());
}
return p;
}
cDialog::cDialog(cDialog* p) : parent(p) {}
cDialog::cDialog(std::string path) : parent(NULL) {
loadFromFile(path);
}
cDialog::cDialog(std::string path, cDialog* p) : parent(p) {
loadFromFile(path);
}
extern fs::path progDir;
void cDialog::loadFromFile(std::string path){
bg = BG_DARK; // default is dark background
fs::path cPath = progDir/"data"/"dialogs"/path;
try{
printf("Loading dialog from: %s\n", cPath.c_str());
Document xml(cPath.c_str());
xml.LoadFile();
Iterator<Attribute> attr;
Iterator<Element> node;
string type, name, val;
xml.FirstChildElement()->GetValue(&type);
if(type != "dialog") throw xBadNode(type,xml.FirstChildElement()->Row(),xml.FirstChildElement()->Column());
for(attr = attr.begin(xml.FirstChildElement()); attr != attr.end(); attr++){
attr->GetName(&name);
attr->GetValue(&val);
if(name == "skin"){
if(val == "light") bg = BG_LIGHT;
else if(val == "dark") bg = BG_DARK;
else{
istringstream sin(val);
sin >> bg;
if(sin.fail()) throw xBadVal(type,name,val,attr->Row(),attr->Column());
}
}else if(name == "fore"){
sf::Color clr;
try{
clr = parseColor(val);
}catch(int){
throw xBadVal("text",name,val,attr->Row(),attr->Column());
}
defTextClr = clr;
} else if(name == "defbtn") {
defaultButton = val;
}else if(name != "debug")
throw xBadAttr(type,name,attr->Row(),attr->Column());
}
for(node = node.begin(xml.FirstChildElement()); node != node.end(); node++){
node->GetValue(&type);
// Yes, I'm using insert instead of [] to add elements to the map.
// In this situation, it's actually easier that way; the reason being, the
// map key is obtained from the name attribute of each element.
if(type == "field")
controls.insert(parse<cTextField>(*node));
else if(type == "text")
controls.insert(parse<cTextMsg>(*node));
else if(type == "pict")
controls.insert(parse<cPict>(*node));
else if(type == "button")
controls.insert(parse<cButton>(*node));
else if(type == "led")
controls.insert(parse<cLed>(*node));
else if(type == "group")
controls.insert(parse<cLedGroup>(*node));
else throw xBadNode(type,node->Row(),node->Column());
}
} catch(Exception& x){ // XML processing exception
printf("%s",x.what());
exit(1);
} catch(xBadVal& x){ // Invalid value for an attribute
printf("%s",x.what());
exit(1);
} catch(xBadAttr& x){ // Invalid attribute for an element
printf("%s",x.what());
exit(1);
} catch(xBadNode& x){ // Invalid element
printf("%s",x.what());
exit(1);
} catch(xMissingAttr& x){ // Invalid element
printf("%s",x.what());
exit(1);
}
dialogNotToast = true;
if(bg == BG_DARK) defTextClr = sf::Color::White;
// now calculate window rect
winRect = RECT();
recalcRect();
ctrlIter iter = controls.begin();
currentFocus = NULL;
while(iter != controls.end()){
if(typeid(iter->second) == typeid(cTextField*)){
cTextField* fld = (cTextField*) iter->second;
if(currentFocus == NULL) currentFocus = fld;
}
iter++;
}
}
void cDialog::recalcRect(){
ctrlIter iter = controls.begin();
while(iter != controls.end()){
printf("%s \"%s\"\n",typeid(*(iter->second)).name(),iter->first.c_str());
RECT frame = iter->second->getBounds();
if(frame.right > winRect.right)
winRect.right = frame.right;
if(frame.bottom > winRect.bottom)
winRect.bottom = frame.bottom;
iter++;
}
winRect.right += 6;
winRect.bottom += 6;
}
void cDialog::init(){
cControl::init();
cButton::init();
cLed::init();
cPict::init();
}
cDialog::~cDialog(){
ctrlIter iter = controls.begin();
while(iter != controls.end()){
delete iter->second;
iter++;
}
win.close();
}
bool cDialog::add(cControl* what, RECT ctrl_frame, std::string key){
// First make sure the key is not already present.
// If it is, we can't add the control, so return false.
if(controls.find(key) != controls.end()) return false;
what->setBounds(ctrl_frame);
controls.insert(std::make_pair(key,what));
return true;
}
bool cDialog::remove(std::string key){
ctrlIter ctrl = controls.find(key);
if(ctrl == controls.end()) return false;
delete ctrl->second;
controls.erase(ctrl);
return true;
}
extern char keyToChar(sf::Keyboard::Key key, bool isShift);
void cDialog::run(){
using kb = sf::Keyboard;
kb::Key k;
cKey key;
sf::Event currentEvent;
std::string itemHit = "";
dialogNotToast = true;
// Focus the first text field, if there is one
for(auto ctrl : controls) {
if(ctrl.second->getType() == CTRL_FIELD) {
ctrl.second->triggerFocusHandler(*this, ctrl.first, false);
break;
}
}
win.create(sf::VideoMode(winRect.width(), winRect.height()), "Dialog", sf::Style::Titlebar);
win.setActive();
win.setVisible(true);
makeFrontWindow(win);
draw();
ModalSession dlog(win);
while(dialogNotToast){
draw();
if(!win.pollEvent(currentEvent)) continue;
location where;
switch(currentEvent.type){
case sf::Event::KeyPressed:
k = currentEvent.key.code;
switch(k){
case kb::Up:
key.spec = true;
key.k = key_up;
break;
case kb::Right:
key.spec = true;
key.k = key_right;
break;
case kb::Left:
key.spec = true;
key.k = key_left;
break;
case kb::Down:
key.spec = true;
key.k = key_down;
break;
case kb::Escape:
key.spec = true;
key.k = key_esc;
break;
case kb::Return: // TODO: Also enter (keypad)
key.spec = true;
key.k = key_enter;
break;
case kb::BackSpace:
key.spec = true;
key.k = key_bsp;
break;
case kb::Delete:
key.spec = true;
key.k = key_del;
break;
case kb::Tab:
key.spec = true;
key.k = key_tab;
break;
case kb::F1:
key.spec = true;
key.k = key_help;
break;
case kb::Home:
key.spec = true;
key.k = key_home;
break;
case kb::End:
key.spec = true;
key.k = key_end;
break;
case kb::PageUp:
key.spec = true;
key.k = key_pgup;
break;
case kb::PageDown:
key.spec = true;
key.k = key_pgdn;
break;
// TODO: Add cases for key_tab and key_help and others
case kb::LShift:
case kb::RShift:
case kb::LAlt:
case kb::RAlt:
case kb::LControl:
case kb::RControl:
case kb::LSystem:
case kb::RSystem:
continue;
default:
// TODO: Should probably only support system or control depending on OS
key.spec = false;
if(currentEvent.key.system || currentEvent.key.control) {
if(k == kb::C) {
key.spec = true;
key.k = key_copy;
} else if(k == kb::X) {
key.spec = true;
key.k = key_cut;
} else if(k == kb::V) {
key.spec = true;
key.k = key_paste;
} else if(k == kb::A) {
key.spec = true;
key.k = key_selectall;
}
if(key.spec) break;
}
key.c = keyToChar(k, currentEvent.key.shift);
break;
}
key.mod = mod_none;
if(currentEvent.key.control || currentEvent.key.system) {
if(key.spec){
if(key.k == key_left) key.k = key_home;
else if(key.k == key_right) key.k = key_end;
else if(key.k == key_up) key.k = key_pgup;
else if(key.k == key_down) key.k = key_pgdn;
else if(key.k == key_copy || key.k == key_cut);
else if(key.k == key_paste || key.k == key_selectall);
else key.mod += mod_ctrl;
}else key.mod += mod_ctrl;
}
if(currentEvent.key.shift) key.mod += mod_shift;
if(currentEvent.key.alt) key.mod += mod_alt;
itemHit = process_keystroke(key); // TODO: This should be a separate check from the fields thing?
if(itemHit.empty()) break;
where = controls[itemHit]->getBounds().centre();
if(controls[itemHit]->getType() == CTRL_FIELD){
if(key.spec && key.k == key_tab){
// TODO: Tabbing through fields, and trigger focus events.
ctrlIter cur = controls.find(itemHit), iter;
if(!cur->second->triggerFocusHandler(*this,itemHit,true)) break;
iter = std::next(cur);
while(iter != cur){
if(typeid(iter->second) == typeid(cTextField*)){
if(iter->second->triggerFocusHandler(*this,iter->first,false)){
currentFocus = (cTextField*) iter->second;
itemHit = "";
break;
}
}
iter++;
if(iter == controls.end()) iter = controls.begin();
}
if(iter == cur) // no focus change occured!
; // TODO: Surely something should happen here?
} else if(!key.spec || key.k != key_enter || mod_contains(key.mod, mod_alt)) {
dynamic_cast<cTextField*>(controls[itemHit])->handleInput(key);
itemHit = "";
}
}
break;
case sf::Event::MouseButtonPressed:
key.mod = mod_none;
if(kb::isKeyPressed(kb::LControl)) key.mod += mod_ctrl;
if(kb::isKeyPressed(kb::RControl)) key.mod += mod_ctrl;
if(kb::isKeyPressed(kb::LSystem)) key.mod += mod_ctrl;
if(kb::isKeyPressed(kb::RSystem)) key.mod += mod_ctrl;
if(kb::isKeyPressed(kb::LAlt)) key.mod += mod_alt;
if(kb::isKeyPressed(kb::RAlt)) key.mod += mod_alt;
if(kb::isKeyPressed(kb::LShift)) key.mod += mod_shift;
if(kb::isKeyPressed(kb::RShift)) key.mod += mod_shift;
where = {currentEvent.mouseButton.x, currentEvent.mouseButton.y};
itemHit = process_click(where, key.mod);
break;
}
if(itemHit.empty()) continue;;
ctrlIter ctrl = controls.find(itemHit);
// TODO: Should it do something with the boolean return value?
if(ctrl != controls.end()) ctrl->second->triggerClickHandler(*this,itemHit,key.mod,where);
itemHit.clear();
}
win.setVisible(false);
sf::RenderWindow* parentWin = &(parent ? parent->win : mainPtr);
while(parentWin->pollEvent(currentEvent));
}
void cDialog::setBg(short n){
bg = n;
}
void cDialog::setDefTextClr(sf::Color clr){
defTextClr = clr;
}
sf::Color cDialog::getDefTextClr() {
return defTextClr;
}
bool cDialog::toast(){
dialogNotToast = false;
return true;
}
void cDialog::attachClickHandlers(click_callback_t handler, std::vector<std::string> controls) {
cDialog& me = *this;
for(std::string control : controls) {
me[control].attachClickHandler(handler);
}
}
void cDialog::attachFocusHandlers(focus_callback_t handler, std::vector<std::string> controls) {
cDialog& me = *this;
for(std::string control : controls) {
me[control].attachFocusHandler(handler);
}
}
bool cDialog::addLabelFor(std::string key, std::string label, eLabelPos where, short offset, bool bold) {
cControl& ctrl = this->getControl(key);
key += "-label";
RECT labelRect = ctrl.getBounds();
switch(where) {
case LABEL_LEFT:
labelRect.right = labelRect.left;
labelRect.left -= 2 * offset;
break;
case LABEL_ABOVE:
labelRect.bottom = labelRect.top;
labelRect.top -= 2 * offset;
break;
case LABEL_RIGHT:
labelRect.left = labelRect.right;
labelRect.right += 2 * offset;
break;
case LABEL_BELOW:
labelRect.top = labelRect.bottom;
labelRect.bottom += 2 * offset;
break;
}
if(labelRect.height() < 14){
int expand = (14 - labelRect.height()) / 2 + 1;
labelRect.inset(0, -expand);
} else labelRect.offset(0, labelRect.height() / 6);
cControl* labelCtrl;
try {
labelCtrl = &this->getControl(key);
} catch(std::invalid_argument x) {
labelCtrl = new cTextMsg(this);
}
labelCtrl->setText(label);
labelCtrl->setFormat(TXT_FONT, bold ? SILOM : GENEVA);
// TODO: Do we need to set colour to white if background is dark?
labelCtrl->setColour(ctrl.getColour());
add(labelCtrl, labelRect, key);
}
std::string cDialog::process_keystroke(cKey keyHit){
unsigned long dummy;
ctrlIter iter = controls.begin();
while(iter != controls.end()){
if(iter->second->getType() == CTRL_FIELD && iter->second->isVisible() && dynamic_cast<cTextField*>(iter->second)->hasFocus()) {
if(!keyHit.spec || (keyHit.k != key_esc && keyHit.k != key_help))
return iter->first;
}
if(iter->second->isVisible() && iter->second->isClickable() && iter->second->getAttachedKey() == keyHit){
iter->second->setActive(true);
draw();
if (play_sounds) {
if(typeid(iter->second) == typeid(cLed*))
play_sound(34);
else play_sound(37);
sf::sleep(time_in_ticks(6));
}
else sf::sleep(time_in_ticks(14));
iter->second->setActive(false);
draw();
sf::sleep(sf::milliseconds(8));
return iter->first;
}
iter++;
}
// If you get an escape and it isn't processed, make it an enter.
if(keyHit.spec && keyHit.k == key_esc){
keyHit.k = key_enter;
return process_keystroke(keyHit);
}
// If nothing was hit and the key was enter, return the default button (if any)
if(keyHit.spec && keyHit.k == key_enter)
return defaultButton;
return "";
}
std::string cDialog::process_click(location where, eKeyMod mods){
ctrlIter iter = controls.begin();
while(iter != controls.end()){
if(iter->second->isVisible() && iter->second->isClickable() && where.in(iter->second->getBounds())){
if(iter->second->handleClick())
return iter->first;
else return "";
}
iter++;
}
return "";
}
xBadNode::xBadNode(std::string t, int r, int c) throw() :
type(t),
row(r),
col(c),
msg(NULL) {}
const char* xBadNode::what() throw() {
if(msg == NULL){
char* s = new (nothrow) char[100];
if(s == NULL){
printf("Allocation of memory for error message failed, bailing out...");
abort();
}
sprintf(s,"XML Parse Error: Unknown element %s encountered (line %d, column %d).",type.c_str(),row,col);
msg = s;
}
return msg;
}
xBadNode::~xBadNode() throw(){
if(msg != NULL) delete msg;
}
xBadAttr::xBadAttr(std::string t, std::string n, int r, int c) throw() :
type(t),
name(n),
row(r),
col(c),
msg(NULL) {}
const char* xBadAttr::what() throw() {
if(msg == NULL){
char* s = new (nothrow) char[100];
if(s == NULL){
printf("Allocation of memory for error message failed, bailing out...");
abort();
}
sprintf(s,"XML Parse Error: Unknown attribute %s encountered on element %s (line %d, column %d).",name.c_str(),type.c_str(),row,col);
msg = s;
}
return msg;
}
xBadAttr::~xBadAttr() throw(){
if(msg != NULL) delete msg;
}
xMissingAttr::xMissingAttr(std::string t, std::string n, int r, int c) throw() :
type(t),
name(n),
row(r),
col(c),
msg(NULL) {}
const char* xMissingAttr::what() throw() {
if(msg == NULL){
char* s = new (nothrow) char[100];
if(s == NULL){
printf("Allocation of memory for error message failed, bailing out...");
abort();
}
sprintf(s,"XML Parse Error: Required attribute %s missing on element %s (line %d, column %d).",name.c_str(),type.c_str(),row,col);
msg = s;
}
return msg;
}
xMissingAttr::~xMissingAttr() throw(){
if(msg != NULL) delete msg;
}
xBadVal::xBadVal(std::string t, std::string n, std::string v, int r, int c) throw() :
type(t),
name(n),
val(v),
row(r),
col(c),
msg(NULL) {}
const char* xBadVal::what() throw() {
if(msg == NULL){
char* s = new (nothrow) char[100];
if(s == NULL){
printf("Allocation of memory for error message failed, bailing out...");
abort();
}
sprintf(s,"XML Parse Error: Invalid value %s for attribute %s encountered on element %s (line %d, column %d).",val.c_str(),name.c_str(),type.c_str(),row,col);
msg = s;
}
return msg;
}
xBadVal::~xBadVal() throw(){
if(msg != NULL) delete msg;
}
void cDialog::draw(){
win.setActive();
tileImage(win,winRect,bg_gworld,::bg[bg]);
ctrlIter iter = controls.begin();
while(iter != controls.end()){
iter->second->draw();
iter++;
}
win.display();
}
cControl& cDialog::operator[](std::string id){
return getControl(id);
}
cControl& cDialog::getControl(std::string id) {
ctrlIter iter = controls.find(id);
if(iter != controls.end()) return *(iter->second);
iter = controls.begin();
while(iter != controls.end()){
if(iter->second->getType() == CTRL_GROUP){
try{
cLedGroup* tmp = (cLedGroup*) (iter->second);
return tmp->operator[](id);
}catch(std::invalid_argument) {}
}else if(iter->second->getType() == CTRL_STACK){ // TODO: Implement stacks
// try{
// cStack* tmp = (cStack*) (iter->second);
// return tmp->operator[](id);
// }catch(std::invalid_argument) {}
}
iter++;
}
throw std::invalid_argument(id + " does not exist in the dialog.");
}