Create CPopupContentType::Parse and add private to its child members.

This commit is contained in:
Joris 2012-11-13 16:17:27 +01:00
parent 4cb696e9a2
commit fc5ce4aff3
2 changed files with 142 additions and 105 deletions

View file

@ -477,17 +477,23 @@ public:
/// Get the content's height
virtual int GetHeight(const ButtonAction &button, int *Costs) const = 0;
virtual void Parse(lua_State *l) = 0;
static CPopupContentType *ParsePopupContent(lua_State *l);
public:
int PosX; /// X position to draw.
int PosY; /// Y position to draw.
int MarginX; /// Left and right margin width.
int MarginY; /// Upper and lower margin height.
int MinWidth; /// Minimal width covered by content type.
int MinHeight; /// Minimal height covered by content type.
bool Wrap; /// If true, the next content will be placed on the next "line".
protected:
std::string TextColor; /// Color used for plain text in content.
std::string HighlightColor; /// Color used for highlighted letters.
public:
PopupConditionPanel *Condition; /// Condition to show the content; if NULL, no condition.
};
@ -508,6 +514,9 @@ public:
virtual int GetWidth(const ButtonAction &button, int *Costs) const;
virtual int GetHeight(const ButtonAction &button, int *Costs) const;
virtual void Parse(lua_State *l);
private:
int InfoType; /// Type of information to show.
unsigned int MaxWidth; /// Maximum width of multilined information.
CFont *Font; /// Font to use.
@ -524,6 +533,9 @@ public:
virtual int GetWidth(const ButtonAction &button, int *Costs) const;
virtual int GetHeight(const ButtonAction &button, int *Costs) const;
virtual void Parse(lua_State *l);
private:
std::string Text; /// Text to display
unsigned int MaxWidth; /// Maximum width of multilined text.
CFont *Font; /// Font to use.
@ -540,6 +552,9 @@ public:
virtual int GetWidth(const ButtonAction &button, int *Costs) const;
virtual int GetHeight(const ButtonAction &button, int *Costs) const;
virtual void Parse(lua_State *l);
private:
CFont *Font; /// Font to use.
char Centered; /// if true, center the display.
};
@ -555,6 +570,9 @@ public:
virtual int GetWidth(const ButtonAction &button, int *Costs) const;
virtual int GetHeight(const ButtonAction &button, int *Costs) const;
virtual void Parse(lua_State *l);
private:
IntColor Color; /// Color used for line.
unsigned int Width; /// line height
unsigned int Height; /// line height
@ -574,6 +592,9 @@ public:
virtual int GetWidth(const ButtonAction &button, int *Costs) const;
virtual int GetHeight(const ButtonAction &button, int *Costs) const;
virtual void Parse(lua_State *l);
private:
StringDesc *Text; /// Text to display.
CFont *Font; /// Font to use.
char Centered; /// if true, center the display.

View file

@ -865,7 +865,118 @@ static PopupConditionPanel *ParsePopupConditions(lua_State *l)
return condition;
}
static CPopupContentType *CclParsePopupContent(lua_State *l)
/* virtual*/ void CPopupContentTypeButtonInfo::Parse(lua_State *l)
{
Assert(lua_istable(l, -1));
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
const char *key = LuaToString(l, -2);
if (!strcmp(key, "InfoType")) {
std::string temp(LuaToString(l, -1));
if (temp == "Hint") {
this->InfoType = PopupButtonInfo_Hint;
} else if (temp == "Description") {
this->InfoType = PopupButtonInfo_Description;
} else if (temp == "Dependencies") {
this->InfoType = PopupButtonInfo_Dependencies;
}
} else if (!strcmp(key, "MaxWidth")) {
this->MaxWidth = LuaToNumber(l, -1);
} else if (!strcmp(key, "Font")) {
this->Font = CFont::Get(LuaToString(l, -1));
} else {
LuaError(l, "'%s' invalid for method 'Name' in DefinePopups" _C_ key);
}
}
}
/* virtual*/ void CPopupContentTypeText::Parse(lua_State *l)
{
Assert(lua_istable(l, -1));
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
const char *key = LuaToString(l, -2);
if (!strcmp(key, "Text")) {
this->Text = LuaToString(l, -1);
} else if (!strcmp(key, "MaxWidth")) {
this->MaxWidth = LuaToNumber(l, -1);
} else if (!strcmp(key, "Font")) {
this->Font = CFont::Get(LuaToString(l, -1));
} else {
LuaError(l, "'%s' invalid for method 'Text' in DefinePopups" _C_ key);
}
}
}
/* virtual*/ void CPopupContentTypeCosts::Parse(lua_State *l)
{
Assert(lua_istable(l, -1) || lua_isnil(l, -1));
if (!lua_isnil(l, -1)) {
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
const char *key = LuaToString(l, -2);
if (!strcmp(key, "Font")) {
this->Font = CFont::Get(LuaToString(l, -1));
} else if (!strcmp(key, "Centered")) {
this->Centered = LuaToBoolean(l, -1);
} else {
LuaError(l, "'%s' invalid for method 'Costs' in DefinePopups" _C_ key);
}
}
}
}
/* virtual*/ void CPopupContentTypeLine::Parse(lua_State *l)
{
Assert(lua_istable(l, -1) || lua_isnil(l, -1));
if (!lua_isnil(l, -1)) {
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
const char *key = LuaToString(l, -2);
if (!strcmp(key, "Width")) {
this->Width = LuaToNumber(l, -1);
} else if (!strcmp(key, "Height")) {
this->Height = LuaToNumber(l, -1);
} else if (!strcmp(key, "Color")) {
this->Color = LuaToNumber(l, -1);
} else {
LuaError(l, "'%s' invalid for method 'Costs' in DefinePopups" _C_ key);
}
}
}
}
/* virtual*/ void CPopupContentTypeVariable::Parse(lua_State *l)
{
Assert(lua_istable(l, -1) || lua_isstring(l, -1));
if (lua_isstring(l, -1)) {
this->Text = CclParseStringDesc(l);
lua_pushnil(l); // ParseStringDesc eat token
} else {
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
const char *key = LuaToString(l, -2);
if (!strcmp(key, "Text")) {
this->Text = CclParseStringDesc(l);
lua_pushnil(l); // ParseStringDesc eat token
} else if (!strcmp(key, "Font")) {
this->Font = CFont::Get(LuaToString(l, -1));
} else if (!strcmp(key, "Centered")) {
this->Centered = LuaToBoolean(l, -1);
} else if (!strcmp(key, "Variable")) {
const char *const name = LuaToString(l, -1);
this->Index = UnitTypeVar.VariableNameLookup[name];
if (this->Index == -1) {
LuaError(l, "unknown variable '%s'" _C_ LuaToString(l, -1));
}
} else {
LuaError(l, "'%s' invalid for method 'Text' in DefinePopups" _C_ key);
}
}
}
}
/* static */ CPopupContentType *CPopupContentType::ParsePopupContent(lua_State *l)
{
Assert(lua_istable(l, -1));
@ -905,114 +1016,19 @@ static CPopupContentType *CclParsePopupContent(lua_State *l)
lua_rawgeti(l, -2, 2); // Method data
key = LuaToString(l, -2);
if (!strcmp(key, "ButtonInfo")) {
CPopupContentTypeButtonInfo *contentbtype = new CPopupContentTypeButtonInfo;
Assert(lua_istable(l, -1));
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
key = LuaToString(l, -2);
if (!strcmp(key, "InfoType")) {
std::string temp(LuaToString(l, -1));
if (temp == "Hint") {
contentbtype->InfoType = PopupButtonInfo_Hint;
} else if (temp == "Description") {
contentbtype->InfoType = PopupButtonInfo_Description;
} else if (temp == "Dependencies") {
contentbtype->InfoType = PopupButtonInfo_Dependencies;
}
} else if (!strcmp(key, "MaxWidth")) {
contentbtype->MaxWidth = LuaToNumber(l, -1);
} else if (!strcmp(key, "Font")) {
contentbtype->Font = CFont::Get(LuaToString(l, -1));
} else {
LuaError(l, "'%s' invalid for method 'Name' in DefinePopups" _C_ key);
}
}
content = contentbtype;
content = new CPopupContentTypeButtonInfo;
} else if (!strcmp(key, "Text")) {
CPopupContentTypeText *contenttext = new CPopupContentTypeText;
Assert(lua_istable(l, -1));
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
key = LuaToString(l, -2);
if (!strcmp(key, "Text")) {
contenttext->Text = LuaToString(l, -1);
} else if (!strcmp(key, "MaxWidth")) {
contenttext->MaxWidth = LuaToNumber(l, -1);
} else if (!strcmp(key, "Font")) {
contenttext->Font = CFont::Get(LuaToString(l, -1));
} else {
LuaError(l, "'%s' invalid for method 'Text' in DefinePopups" _C_ key);
}
}
content = contenttext;
content = new CPopupContentTypeText;
} else if (!strcmp(key, "Costs")) {
CPopupContentTypeCosts *contentcosts = new CPopupContentTypeCosts;
Assert(lua_istable(l, -1) || lua_isnil(l, -1));
if (!lua_isnil(l, -1)) {
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
key = LuaToString(l, -2);
if (!strcmp(key, "Font")) {
contentcosts->Font = CFont::Get(LuaToString(l, -1));
} else if (!strcmp(key, "Centered")) {
contentcosts->Centered = LuaToBoolean(l, -1);
} else {
LuaError(l, "'%s' invalid for method 'Costs' in DefinePopups" _C_ key);
}
}
}
content = contentcosts;
content = new CPopupContentTypeCosts;
} else if (!strcmp(key, "Line")) {
CPopupContentTypeLine *contentline = new CPopupContentTypeLine;
Assert(lua_istable(l, -1) || lua_isnil(l, -1));
if (!lua_isnil(l, -1)) {
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
key = LuaToString(l, -2);
if (!strcmp(key, "Width")) {
contentline->Width = LuaToNumber(l, -1);
} else if (!strcmp(key, "Height")) {
contentline->Height = LuaToNumber(l, -1);
} else if (!strcmp(key, "Color")) {
contentline->Color = LuaToNumber(l, -1);
} else {
LuaError(l, "'%s' invalid for method 'Costs' in DefinePopups" _C_ key);
}
}
}
content = contentline;
content = new CPopupContentTypeLine;
} else if (!strcmp(key, "Variable")) {
CPopupContentTypeVariable *contenttext = new CPopupContentTypeVariable;
Assert(lua_istable(l, -1) || lua_isstring(l, -1));
if (lua_isstring(l, -1)) {
contenttext->Text = CclParseStringDesc(l);
lua_pushnil(l); // ParseStringDesc eat token
} else {
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) {
key = LuaToString(l, -2);
if (!strcmp(key, "Text")) {
contenttext->Text = CclParseStringDesc(l);
lua_pushnil(l); // ParseStringDesc eat token
} else if (!strcmp(key, "Font")) {
contenttext->Font = CFont::Get(LuaToString(l, -1));
} else if (!strcmp(key, "Centered")) {
contenttext->Centered = LuaToBoolean(l, -1);
} else if (!strcmp(key, "Variable")) {
const char *const name = LuaToString(l, -1);
contenttext->Index = UnitTypeVar.VariableNameLookup[name];
if (contenttext->Index == -1) {
LuaError(l, "unknown variable '%s'" _C_ LuaToString(l, -1));
}
} else {
LuaError(l, "'%s' invalid for method 'Text' in DefinePopups" _C_ key);
}
}
}
content = contenttext;
content = new CPopupContentTypeLine;
} else {
LuaError(l, "Invalid drawing method '%s' in DefinePopups" _C_ key);
}
content->Parse(l);
lua_pop(l, 2); // Pop Variable Name and Method
} else if (!strcmp(key, "Condition")) {
condition = ParsePopupConditions(l);
@ -1070,7 +1086,7 @@ static int CclDefinePopup(lua_State *l)
Assert(lua_istable(l, -1));
for (size_t j = 0; j < lua_rawlen(l, -1); j++, lua_pop(l, 1)) {
lua_rawgeti(l, -1, j + 1);
popup->Contents.push_back(CclParsePopupContent(l));
popup->Contents.push_back(CPopupContentType::ParsePopupContent(l));
}
} else {
LuaError(l, "'%s' invalid for DefinePopups" _C_ key);