From f60b3f7b8e39193323d2e246ae710b26640dd08c Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Tue, 16 Dec 2014 23:15:55 -0500 Subject: [PATCH] Picture controls now calculate their bounding rect on load --- osx/dialogxml/dialog.cpp | 41 +++++++++++++++++++-------------- osx/dialogxml/pict.cpp | 49 ++++++---------------------------------- osx/dialogxml/pict.h | 13 ++++++----- 3 files changed, 38 insertions(+), 65 deletions(-) diff --git a/osx/dialogxml/dialog.cpp b/osx/dialogxml/dialog.cpp index e1f4bf2d..24d64632 100644 --- a/osx/dialogxml/dialog.cpp +++ b/osx/dialogxml/dialog.cpp @@ -49,6 +49,7 @@ template<> pair cDialog::parse(Element& who /*pict*/){ std::pair p; Iterator attr; std::string name; + ePicType type; bool wide = false, tall = false, custom = false; bool foundTop = false, foundLeft = false, foundType = false, foundNum = false; // required attributes RECT frame; @@ -62,38 +63,43 @@ template<> pair cDialog::parse(Element& who /*pict*/){ std::string val; foundType = true; attr->GetValue(&val); - pic_num_t wasPic = p.second->getPicNum(); if(val == "blank"){ - p.second->setPict(-1, PIC_TER); + p.second->setPict(cPict::BLANK, PIC_TER); + foundNum = true; + continue; }else if(val == "ter") - p.second->setPict(wasPic, PIC_TER); + type = PIC_TER; else if(val == "teranim") - p.second->setPict(wasPic, PIC_TER_ANIM); + type = PIC_TER_ANIM; else if(val == "monst") - p.second->setPict(wasPic, PIC_MONST); + type = PIC_MONST; else if(val == "dlog") - p.second->setPict(wasPic, PIC_DLOG); + type = PIC_DLOG; else if(val == "talk") - p.second->setPict(wasPic, PIC_TALK); + type = PIC_TALK; else if(val == "scen") - p.second->setPict(wasPic, PIC_SCEN); + type = PIC_SCEN; else if(val == "item") - p.second->setPict(wasPic, PIC_ITEM); + type = PIC_ITEM; else if(val == "pc") - p.second->setPict(wasPic, PIC_PC); + type = PIC_PC; else if(val == "field") - p.second->setPict(wasPic, PIC_FIELD); + type = PIC_FIELD; else if(val == "boom") - p.second->setPict(wasPic, PIC_BOOM); + type = PIC_BOOM; else if(val == "missile") - p.second->setPict(wasPic, PIC_MISSILE); + type = PIC_MISSILE; else if(val == "full") - p.second->setPict(wasPic, PIC_FULL); + type = PIC_FULL; else if(val == "map") - p.second->setPict(wasPic, PIC_TER_MAP); + type = PIC_TER_MAP; else if(val == "status") - p.second->setPict(wasPic, PIC_STATUS); + type = PIC_STATUS; else throw xBadVal("pict",name,val,attr->Row(),attr->Column(),fname); + if(foundNum) { + pic_num_t wasPic = p.second->getPicNum(); + p.second->setPict(wasPic, type); + } }else if(name == "custom"){ std::string val; attr->GetValue(&val); @@ -117,7 +123,8 @@ template<> pair cDialog::parse(Element& who /*pict*/){ }else if(name == "num"){ pic_num_t newPic; attr->GetValue(&newPic), foundNum = true; - p.second->setPict(newPic); + if(foundType) p.second->setPict(newPic, type); + else p.second->setPict(newPic); }else if(name == "top"){ attr->GetValue(&frame.top), foundTop = true; }else if(name == "left"){ diff --git a/osx/dialogxml/pict.cpp b/osx/dialogxml/pict.cpp index be7e22a1..e5668cd1 100644 --- a/osx/dialogxml/pict.cpp +++ b/osx/dialogxml/pict.cpp @@ -17,6 +17,7 @@ extern sf::Texture bg_gworld; extern cCustomGraphics spec_scen_g; +const pic_num_t cPict::BLANK = std::numeric_limits::max(); void cPict::init(){ drawPict()[PIC_TER] = &cPict::drawPresetTer; @@ -109,51 +110,15 @@ sf::Color cPict::getColour() throw(xUnsupportedProp) { void cPict::setPict(pic_num_t num, ePicType type){ picNum = num; picType = type; - switch(picType + PIC_PRESET){ - 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_MONST: - if(picNum < 1000) { - if(m_pic_index[num].x == 2) picType += PIC_WIDE; - if(m_pic_index[num].y == 2) picType += PIC_TALL; - } - // Intentional fallthrough - default: - frame.right = frame.left + 28; - frame.bottom = frame.top + 36; - break; + if(picType == PIC_MONST && picNum < 1000) { + if(m_pic_index[num].x == 2) picType += PIC_WIDE; + if(m_pic_index[num].y == 2) picType += PIC_TALL; } - if(picNum >= 1000) { + if(picType != PIC_FULL && picNum >= 1000) { picNum -= 1000; picType += PIC_CUSTOM; } + recalcRect(); } void cPict::setPict(pic_num_t num) { @@ -599,7 +564,7 @@ void cPict::draw(){ tileImage(*inWindow,rect,bg[parent->getBg()]); return; } - if(picNum < 0) { // Just fill with black + if(picNum == BLANK) { // Just fill with black fill_rect(*inWindow, rect, sf::Color::Black); return; } diff --git a/osx/dialogxml/pict.h b/osx/dialogxml/pict.h index 54612c86..bb8ab54b 100644 --- a/osx/dialogxml/pict.h +++ b/osx/dialogxml/pict.h @@ -117,16 +117,14 @@ public: sf::Color getColour() throw(xUnsupportedProp); /// @copydoc setPict(pic_num_t) /// @param type The type of the new icon - /// @note If you change to a type with a different bounding rect, - /// you will need to separately update the bounding rect. - /// (The bounding rect is mostly ignored when drawing, so if the icon is opaque, the control is not clickable, - /// and there is no frame, you can usually safely skip this step.) + /// @note Calling this function automatically adjusts the bounding rect so that the picture fits perfectly. + /// It does not change the control's position. /// /// This function applies a few automatic adjustments to its input: /// /// - If type is PIC_MONST, it automatically looks up the chosen icon to determine /// whether it should apply the tall or wide modifiers. - /// - If num is 1000 or greater, it automatically subtracts 1000 and applies the custom modifier. + /// - If num is 1000 or greater and type is not PIC_FULL, it automatically subtracts 1000 and applies the custom modifier. void setPict(pic_num_t num, ePicType type); /// Set the pict's icon. /// @param num The new icon index. @@ -158,12 +156,15 @@ public: /// @param type_g The type of icon to draw. /// @param framed Whether to draw a frame around the icon. static void drawAt(sf::RenderWindow& win, RECT dest, pic_num_t which_g, ePicType type_g, bool framed); + /// A convenience constant that can be passed as the pic number to setPict(pic_num_t num). + /// It sets the icon to nothing, showing as just black. + static const pic_num_t BLANK; cPict& operator=(cPict& other) = delete; cPict(cPict& other) = delete; private: static std::shared_ptr getSheet(eSheetType type, size_t n = 0); static short animFrame; - short picNum; + pic_num_t picNum; ePicType picType; bool clickable, drawFramed; void drawPresetTer(short num, RECT to_rect);