From: <tz...@us...> - 2006-10-24 03:50:52
|
Revision: 1929 http://svn.sourceforge.net/freeorion/revision/?rev=1929&view=rev Author: tzlaine Date: 2006-10-23 20:50:43 -0700 (Mon, 23 Oct 2006) Log Message: ----------- Generalized the recent commit that allowed random selection of nebula graphics based on the ones available in the nebula graphics directory. Now selection of random graphics or modulo-N graphics is available, chosen from whatever set is available in the applicable directory. Modified Paths: -------------- trunk/FreeOrion/UI/ClientUI.cpp trunk/FreeOrion/UI/ClientUI.h trunk/FreeOrion/UI/MapWnd.cpp trunk/FreeOrion/UI/SidePanel.cpp trunk/FreeOrion/UI/SystemIcon.cpp Added Paths: ----------- trunk/FreeOrion/default/data/art/stars/blackhole1.png trunk/FreeOrion/default/data/art/stars/blackhole2.png Removed Paths: ------------- trunk/FreeOrion/default/data/art/stars/black1.png trunk/FreeOrion/default/data/art/stars/black2.png Modified: trunk/FreeOrion/UI/ClientUI.cpp =================================================================== --- trunk/FreeOrion/UI/ClientUI.cpp 2006-10-24 03:20:25 UTC (rev 1928) +++ trunk/FreeOrion/UI/ClientUI.cpp 2006-10-24 03:50:43 UTC (rev 1929) @@ -27,8 +27,10 @@ #include <log4cpp/PatternLayout.hh> #include <log4cpp/FileAppender.hh> +#include <boost/algorithm/string/predicate.hpp> +#include <boost/filesystem/exception.hpp> +#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/operations.hpp> -#include <boost/filesystem/fstream.hpp> #include <boost/spirit.hpp> #include <string> @@ -96,7 +98,20 @@ GG::Clr ClientUI::TechWndProgressBarBackground() { return GetOptionsDB().Get<StreamableColor>("UI.tech-progress-background").ToClr(); } GG::Clr ClientUI::TechWndProgressBar() { return GetOptionsDB().Get<StreamableColor>("UI.tech-progress").ToClr(); } +std::map<StarType, std::string>& ClientUI::StarTypeFilePrefixes() +{ + static std::map<StarType, std::string> prefixes; + prefixes[STAR_BLUE] = "blue"; + prefixes[STAR_WHITE] = "white"; + prefixes[STAR_YELLOW] = "yellow"; + prefixes[STAR_ORANGE] = "orange"; + prefixes[STAR_RED] = "red"; + prefixes[STAR_NEUTRON] = "neutron"; + prefixes[STAR_BLACK] = "blackhole"; + return prefixes; +} + // private static members log4cpp::Category& ClientUI::s_logger(log4cpp::Category::getRoot()); ClientUI* ClientUI::s_the_UI = 0; @@ -379,6 +394,19 @@ #endif } +boost::shared_ptr<GG::Texture> ClientUI::GetRandomTexture(const boost::filesystem::path& dir, const std::string& prefix) +{ + TexturesAndDist prefixed_textures_and_dist = PrefixedTexturesAndDist(dir, prefix); + return prefixed_textures_and_dist.first[(*prefixed_textures_and_dist.second)()]; +} + +boost::shared_ptr<GG::Texture> ClientUI::GetModuloTexture(const boost::filesystem::path& dir, const std::string& prefix, int n) +{ + assert(0 <= n); + TexturesAndDist prefixed_textures_and_dist = PrefixedTexturesAndDist(dir, prefix); + return prefixed_textures_and_dist.first[n % prefixed_textures_and_dist.first.size()]; +} + ///////////////////////////////////////////////////// //Screen Functions/////////////////////////////////// @@ -556,49 +584,6 @@ } } -boost::shared_ptr<GG::Texture> -ClientUI::GetNumberedTexture(const std::string& dir_name, const std::map<int, std::string>& types_to_names, - int type, int hash_key) -{ - using boost::lexical_cast; - using std::string; - - static std::map<int, std::pair<string, int> > image_names; - - if (image_names.empty()) { - for (std::map<int, std::string>::const_iterator it = types_to_names.begin(); it != types_to_names.end(); ++it) { - image_names[it->first].first = it->second; - } - - fs::path star_dir(ClientUI::ArtDir() / dir_name); - fs::directory_iterator end_it; - for (fs::directory_iterator it(star_dir); it != end_it; ++it) { - if (!fs::is_directory(*it)) { - if (it->leaf().find("blue") == 0) { - ++image_names[STAR_BLUE].second; - } else if (it->leaf().find("white") == 0) { - ++image_names[STAR_WHITE].second; - } else if (it->leaf().find("yellow") == 0) { - ++image_names[STAR_YELLOW].second; - } else if (it->leaf().find("orange") == 0) { - ++image_names[STAR_ORANGE].second; - } else if (it->leaf().find("red") == 0) { - ++image_names[STAR_RED].second; - } else if (it->leaf().find("neutron") == 0) { - ++image_names[STAR_NEUTRON].second; - } else if (it->leaf().find("black") == 0) { - ++image_names[STAR_BLACK].second; - } - } - } - } - - int star_variant = image_names[type].second ? (hash_key % image_names[type].second) : 0; - fs::path path = ClientUI::ArtDir() / "stars" / - (image_names[type].first + lexical_cast<string>(star_variant + 1) + ".png"); - return GetTexture(path); -} - //////////////////////////////////////////////////// void ClientUI::HideAllWindows() { @@ -612,6 +597,32 @@ #endif } +ClientUI::TexturesAndDist ClientUI::PrefixedTexturesAndDist(const boost::filesystem::path& dir, const std::string& prefix) +{ + namespace fs = boost::filesystem; + assert(fs::is_directory(dir)); + const std::string KEY = dir.native_directory_string() + "/" + prefix; + PrefixedTextures::iterator prefixed_textures_it = m_prefixed_textures.find(KEY); + if (prefixed_textures_it == m_prefixed_textures.end()) { + prefixed_textures_it = m_prefixed_textures.insert(std::make_pair(KEY, TexturesAndDist())).first; + std::vector<boost::shared_ptr<GG::Texture> >& textures = prefixed_textures_it->second.first; + boost::shared_ptr<SmallIntDistType>& rand_int = prefixed_textures_it->second.second; + fs::directory_iterator end_it; + for (fs::directory_iterator it(dir); it != end_it; ++it) { + try { + if (fs::exists(*it) && !fs::is_directory(*it) && boost::algorithm::starts_with(it->leaf(), prefix)) + textures.push_back(ClientUI::GetTexture(*it)); + } catch (const fs::filesystem_error& e) { + // ignore files for which permission is denied, and rethrow other exceptions + if (e.error() != fs::security_error) + throw; + } + } + rand_int.reset(new SmallIntDistType(SmallIntDist(0, textures.size() - 1))); + } + return prefixed_textures_it->second; +} + TempUISoundDisabler::TempUISoundDisabler() : m_was_enabled(GetOptionsDB().Get<bool>("UI.sound.enabled")) { Modified: trunk/FreeOrion/UI/ClientUI.h =================================================================== --- trunk/FreeOrion/UI/ClientUI.h 2006-10-24 03:20:25 UTC (rev 1928) +++ trunk/FreeOrion/UI/ClientUI.h 2006-10-24 03:50:43 UTC (rev 1929) @@ -2,9 +2,9 @@ #ifndef _ClientUI_h_ #define _ClientUI_h_ -#ifndef _SitRepEntry_h_ +#include "../universe/Enums.h" #include "../util/SitRepEntry.h" -#endif +#include "../util/Random.h" #ifndef _GG_Wnd_h_ #include <GG/Wnd.h> @@ -115,6 +115,13 @@ bool ZoomToEncyclopediaEntry(const std::string& str); //!< Opens the encyclodedia screen and presents the entry for the given item void ZoomToSystem(System* system); //!< Zooms to a particular system on the galaxy map void ZoomToFleet(Fleet* fleet); //!< Zooms to a particular fleet on the galaxy map and opens the fleet window + + /** Loads a texture at random from the set of files starting with \a prefix in directory \a dir. */ + boost::shared_ptr<GG::Texture> GetRandomTexture(const boost::filesystem::path& dir, const std::string& prefix); + + /** Loads texture \a n % N from the set of files starting with \a prefix in directory \a dir, where N is the number + of files found in \a dir with prefix \a prefix. */ + boost::shared_ptr<GG::Texture> GetModuloTexture(const boost::filesystem::path& dir, const std::string& prefix, int n); //!@} static ClientUI* GetClientUI() {return s_the_UI;} //!< returns a pointer to the singleton ClientUI class @@ -125,24 +132,12 @@ static void LogMessage(const std::string& msg); //!<sends a message to the logger - static void GenerateSitRepText( SitRepEntry *p_sit_rep ); ///< generates a SitRep string from it's XML data. + static void GenerateSitRepText(SitRepEntry *p_sit_rep); ///< generates a SitRep string from it's XML data. /** Loads the requested texture from file \a name; mipmap textures are generated if \a mipmap is true load default missing.png if name isn't found. */ static boost::shared_ptr<GG::Texture> GetTexture(const boost::filesystem::path& path, bool mipmap = false); - /** Loads and returns one of a set of numbered textures. This is supposed to be used to retrieve textures that are numbered, e.g. the star textures - blue1.png, blue2,png, ..., yellow1.png, yellow2.png, etc. It is assumed that all such files are numbered starting with 1, not 0. \a dir_name - is the name of the directory in which the images are found, relative to ClientUI::ArtDir(). - \a types_to_names is a map of object types to their base filenames, e.g. (System::BLUE --> "blue"), (PT_SWAMP --> "swamp"), etc. \a type is - the type of object for which you want a texture, which is used to look up the name in \a types_to_names. \a hash_key is used to pick the numer - from [1, N] of the texture to be used. This number is usually the ID() of the UniverseObject that the texture represents on-screen. This is - used so that the texture used to represent the object is arbitrary, but is always the same, even after a save-reload cycle. \see SystemIcon.cpp - ... for an example of how to use this function. */ - static boost::shared_ptr<GG::Texture> GetNumberedTexture(const std::string& dir_name, const std::map<int, std::string>& types_to_names, - int type, int hash_key); - - //! \name Static Config Data //!@{ static boost::filesystem::path ArtDir(); //!< directory holding artwork static boost::filesystem::path SoundDir(); //!< directory holding sound and music @@ -200,6 +195,8 @@ static GG::Clr UnresearchableTechTextAndBorderColor(); static GG::Clr TechWndProgressBarBackground(); static GG::Clr TechWndProgressBar(); + + static std::map<StarType, std::string>& StarTypeFilePrefixes(); //!@} private: @@ -207,6 +204,10 @@ void SwitchState(State state); //!< switch current state to \a state, free's last state window and create the one for the new state + typedef std::pair<std::vector<boost::shared_ptr<GG::Texture> >, boost::shared_ptr<SmallIntDistType> > TexturesAndDist; + typedef std::map<std::string, TexturesAndDist> PrefixedTextures; + TexturesAndDist PrefixedTexturesAndDist(const boost::filesystem::path& dir, const std::string& prefix); + State m_state; //!< represents the screen currently being displayed IntroScreen* m_intro_screen; //!< the intro (and main menu) screen first showed when the game starts up @@ -216,6 +217,8 @@ int m_previously_shown_system; //!< the ID of the system that was shown in the sidepanel before the last call to HideAllWindows() + PrefixedTextures m_prefixed_textures; + static log4cpp::Category& s_logger; //!< log4cpp logging category static ClientUI* s_the_UI; //!< pointer to the one and only ClientUI object }; Modified: trunk/FreeOrion/UI/MapWnd.cpp =================================================================== --- trunk/FreeOrion/UI/MapWnd.cpp 2006-10-24 03:20:25 UTC (rev 1928) +++ trunk/FreeOrion/UI/MapWnd.cpp 2006-10-24 03:50:43 UTC (rev 1929) @@ -27,10 +27,6 @@ #include <GG/DrawUtil.h> #include <GG/MultiEdit.h> -#include <boost/algorithm/string/predicate.hpp> -#include <boost/filesystem/operations.hpp> -#include <boost/filesystem/exception.hpp> - #include <vector> #include <deque> @@ -80,29 +76,6 @@ return true; } #endif - - boost::shared_ptr<GG::Texture> RandomNebula() - { - static std::vector<boost::shared_ptr<GG::Texture> > nebulae; - static boost::shared_ptr<SmallIntDistType> rand_int; - if (nebulae.empty()) { - namespace fs = boost::filesystem; - fs::directory_iterator end_it; - for (fs::directory_iterator it(ClientUI::ArtDir()); it != end_it; ++it) { - try { - if (fs::exists(*it) && !fs::is_directory(*it) && boost::algorithm::starts_with(it->leaf(), "nebula")) - nebulae.push_back(ClientUI::GetTexture(*it)); - } catch (const fs::filesystem_error& e) { - // ignore files for which permission is denied, and rethrow other exceptions - if (e.error() != fs::security_error) - throw; - } - } - rand_int.reset(new SmallIntDistType(SmallIntDist(0, nebulae.size() - 1))); - } - assert(2 <= nebulae.size()); - return nebulae[(*rand_int)()]; - } } @@ -604,7 +577,7 @@ m_nebula_centers.resize(num_nebulae); SmallIntDistType universe_placement = SmallIntDist(0, static_cast<int>(Universe::UniverseWidth())); for (int i = 0; i < num_nebulae; ++i) { - m_nebulae[i] = RandomNebula(); + m_nebulae[i] = ClientUI::GetClientUI()->GetRandomTexture(ClientUI::ArtDir(), "nebula"); m_nebula_centers[i] = GG::Pt(universe_placement(), universe_placement()); } } Modified: trunk/FreeOrion/UI/SidePanel.cpp =================================================================== --- trunk/FreeOrion/UI/SidePanel.cpp 2006-10-24 03:20:25 UTC (rev 1928) +++ trunk/FreeOrion/UI/SidePanel.cpp 2006-10-24 03:50:43 UTC (rev 1929) @@ -1477,41 +1477,23 @@ int drop_height = std::min(TEXT_ROW_HEIGHT * system_names_in_droplist, MAX_DROPLIST_DROP_HEIGHT) + TOTAL_LISTBOX_MARGIN; m_system_name->SetDropHeight(drop_height); - for (int i = 0; i < m_system_name->NumRows(); i++) + for (int i = 0; i < m_system_name->NumRows(); i++) { if(select_row == &m_system_name->GetRow(i)) { m_system_name->Select(i); break; } - - std::vector<boost::shared_ptr<GG::Texture> > textures; - boost::shared_ptr<GG::Texture> graphic; - - boost::filesystem::path star_image = ClientUI::ArtDir() / "stars_sidepanel"; - std::string star_filename; - switch (s_system->Star()) - { - case STAR_BLUE : star_filename = "blue0" ; break; - case STAR_WHITE : star_filename = "white0" ; break; - case STAR_YELLOW : star_filename = "yellow0" ; break; - case STAR_ORANGE : star_filename = "orange0" ; break; - case STAR_RED : star_filename = "red0" ; break; - case STAR_NEUTRON : star_filename = "neutron0" ; break; - case STAR_BLACK : star_filename = "blackhole0"; break; - default : star_filename = "white0" ; break; } - star_filename += lexical_cast<std::string>(s_system->ID() % 2) + ".png"; - star_image /= star_filename; - std::cout << star_image.native_file_string() << std::endl; - graphic = ClientUI::GetTexture(star_image); - + boost::shared_ptr<GG::Texture> graphic = ClientUI::GetClientUI()->GetModuloTexture(ClientUI::ArtDir() / "stars_sidepanel", ClientUI::StarTypeFilePrefixes()[s_system->Star()], s_system->ID()); + std::vector<boost::shared_ptr<GG::Texture> > textures; textures.push_back(graphic); int star_dim = (Width()*4)/5; m_star_graphic = new GG::DynamicGraphic(Width()-(star_dim*2)/3,-(star_dim*1)/3,star_dim,star_dim,true,textures[0]->DefaultWidth(),textures[0]->DefaultHeight(),0,textures, GG::GR_FITGRAPHIC | GG::GR_PROPSCALE); - AttachChild(m_star_graphic);MoveChildDown(m_star_graphic); + AttachChild(m_star_graphic); + MoveChildDown(m_star_graphic); // TODO: add fleet icons std::pair<System::const_orbit_iterator, System::const_orbit_iterator> range = s_system->non_orbit_range(); @@ -1523,8 +1505,7 @@ std::vector<const Planet*> plt_vec = s_system->FindObjects<Planet>(); m_planet_panel_container->SetPlanets(plt_vec, s_system->Star()); - for(unsigned int i = 0; i < plt_vec.size(); i++) - { + for(unsigned int i = 0; i < plt_vec.size(); i++) { GG::Connect(plt_vec[i]->StateChangedSignal, &SidePanel::PlanetsChanged, this); GG::Connect(plt_vec[i]->ResourceCenterChangedSignal, &SidePanel::PlanetsChanged, this); } Modified: trunk/FreeOrion/UI/SystemIcon.cpp =================================================================== --- trunk/FreeOrion/UI/SystemIcon.cpp 2006-10-24 03:20:25 UTC (rev 1928) +++ trunk/FreeOrion/UI/SystemIcon.cpp 2006-10-24 03:50:43 UTC (rev 1929) @@ -19,21 +19,6 @@ #include <boost/lexical_cast.hpp> -namespace { - const std::map<int, std::string>& StarTypesNames() - { - static std::map<int, std::string> star_type_names; - star_type_names[STAR_BLUE] = "blue"; - star_type_names[STAR_WHITE] = "white"; - star_type_names[STAR_YELLOW] = "yellow"; - star_type_names[STAR_ORANGE] = "orange"; - star_type_names[STAR_RED] = "red"; - star_type_names[STAR_NEUTRON] = "neutron"; - star_type_names[STAR_BLACK] = "black"; - return star_type_names; - } -} - //////////////////////////////////////////////// // OwnerColoredSystemName //////////////////////////////////////////////// @@ -84,9 +69,9 @@ m_system(*GetUniverse().Object<const System>(id)), m_static_graphic(0), m_selection_indicator(0), + m_selected(false), m_name(0), - m_default_star_color(GG::CLR_WHITE), - m_selected(false) + m_default_star_color(GG::CLR_WHITE) { Connect(m_system.StateChangedSignal, &SystemIcon::Refresh, this); @@ -100,7 +85,7 @@ static_cast<int>(ul.y + ClientUI::SystemIconSize() * zoom + 0.5))); // static graphic - boost::shared_ptr<GG::Texture> graphic = ClientUI::GetNumberedTexture("stars", StarTypesNames(), m_system.Star(), m_system.ID()); + boost::shared_ptr<GG::Texture> graphic = ClientUI::GetClientUI()->GetModuloTexture(ClientUI::ArtDir() / "stars", ClientUI::StarTypeFilePrefixes()[m_system.Star()], m_system.ID()); m_static_graphic = new GG::StaticGraphic(0, 0, Width(), Height(), graphic, GG::GR_FITGRAPHIC); AdjustBrightness(m_default_star_color, 0.80); m_static_graphic->SetColor(m_default_star_color); @@ -108,7 +93,7 @@ // selection indicator graphic boost::shared_ptr<GG::Texture> selection_texture = ClientUI::GetTexture(ClientUI::ArtDir() / "misc" / "system_selection.png"); - double size = ClientUI::SystemSelectionIndicatorSize()*Width(); + int size = static_cast<int>(ClientUI::SystemSelectionIndicatorSize() * Width()); m_selection_indicator = new GG::StaticGraphic(0, 0, size, size, selection_texture, GG::GR_FITGRAPHIC); AttachChild(m_selection_indicator); m_selection_indicator->Hide(); @@ -147,8 +132,8 @@ m_static_graphic->SizeMove(GG::Pt(0, 0), lr - ul); if (m_selection_indicator && m_selected) { - double size = ClientUI::SystemSelectionIndicatorSize()*Width(); - GG::Pt ind_ul = GG::Pt((Width() - size)*0.5, (Height() - size)*0.5); + int size = static_cast<int>(ClientUI::SystemSelectionIndicatorSize() * Width()); + GG::Pt ind_ul = GG::Pt((Width() - size) / 2, (Height() - size) / 2); GG::Pt ind_lr = ind_ul + GG::Pt(size, size); m_selection_indicator->SizeMove(ind_ul, ind_lr); m_selection_indicator->Show(); @@ -205,8 +190,8 @@ m_selected = selected; if (m_selected) { - double size = ClientUI::SystemSelectionIndicatorSize()*Width(); - GG::Pt ind_ul = GG::Pt((Width() - size)*0.5, (Height() - size)*0.5); + int size = static_cast<int>(ClientUI::SystemSelectionIndicatorSize() * Width()); + GG::Pt ind_ul = GG::Pt((Width() - size) / 2, (Height() - size) / 2); GG::Pt ind_lr = ind_ul + GG::Pt(size, size); m_selection_indicator->SizeMove(ind_ul, ind_lr); m_selection_indicator->Show(); Deleted: trunk/FreeOrion/default/data/art/stars/black1.png =================================================================== (Binary files differ) Deleted: trunk/FreeOrion/default/data/art/stars/black2.png =================================================================== (Binary files differ) Copied: trunk/FreeOrion/default/data/art/stars/blackhole1.png (from rev 1925, trunk/FreeOrion/default/data/art/stars/black1.png) =================================================================== (Binary files differ) Copied: trunk/FreeOrion/default/data/art/stars/blackhole2.png (from rev 1925, trunk/FreeOrion/default/data/art/stars/black2.png) =================================================================== (Binary files differ) |