Added style reader to encapculate access to style files

This commit is contained in:
kshalnev 2015-06-05 21:18:41 +03:00 committed by Alex Zolotarev
parent 61b0af91c4
commit 68275d8f94
13 changed files with 127 additions and 64 deletions

View file

@ -47,7 +47,6 @@
#define CELL2FEATURE_SORTED_EXT ".c2f.sorted"
#define CELL2FEATURE_TMP_EXT ".c2f.tmp"
#define COUNTRIES_FILE "countries.txt"
#define WORLD_FILE_NAME "World"
@ -62,9 +61,6 @@
#define EXTERNAL_RESOURCES_FILE "external_resources.txt"
#define DRAWING_RULES_LIGHT_BIN_FILE "drules_proto.bin"
#define DRAWING_RULES_DARK_BIN_FILE "drules_proto_dark.bin"
/// How many langs we're supporting on indexing stage
#define MAX_SUPPORTED_LANGUAGES 64

View file

@ -77,11 +77,6 @@ namespace graphics
density = static_cast<EDensity>(FindFirstBySecond(s_density, name, EqualStrings()));
}
string const resourcePath(string const & name, EDensity d)
{
return my::JoinFoldersToPath(string("resources-") + convert(d), name);
}
double visualScale(EDensity density)
{
static double const vs [6] = { 0.75, 1, 1.5, 2, 3, 2.4 };

View file

@ -23,9 +23,6 @@ namespace graphics
/// get density from name
void convert(char const * name, EDensity & density);
/// get resource name for specified density
string const resourcePath(string const & name, EDensity density);
/// get scaling koefficient for specified density
double visualScale(EDensity density);

View file

@ -2,7 +2,7 @@
#include "graphics/opengl/data_traits.hpp"
#include "platform/platform.hpp"
#include "indexer/map_style_reader.hpp"
#include "coding/lodepng_io.hpp"
@ -12,7 +12,7 @@ namespace graphics
{
m2::PointU const GetDimensions(string const & resName, EDensity density)
{
ReaderPtr<Reader> reader = GetPlatform().GetReader(resourcePath(resName, density));
ReaderPtr<Reader> reader = GetStyleReader().GetResourceReader(resName, density);
gil::point2<ptrdiff_t> size = gil::lodepng_read_dimensions(reader);
return m2::PointU(size.x, size.y);
}
@ -27,8 +27,6 @@ namespace graphics
m_resourceName(name),
m_size(0, 0)
{
string resName = graphics::resourcePath(name, density);
try
{
m_size = GetDimensions(name, density);
@ -39,7 +37,7 @@ namespace graphics
(DATA_TRAITS::pixel_t*)&m_data[0],
m_size.x * sizeof(DATA_TRAITS::pixel_t));
ReaderPtr<Reader> reader = GetPlatform().GetReader(resName);
ReaderPtr<Reader> reader = GetStyleReader().GetResourceReader(name, density);
gil::lodepng_read_and_convert_view(reader, v, DATA_TRAITS::color_converter());
}

View file

@ -5,7 +5,7 @@
#include "graphics/image.hpp"
#include "platform/platform.hpp"
#include "indexer/map_style_reader.hpp"
#include "coding/lodepng_io.hpp"
@ -68,7 +68,7 @@ namespace graphics
Texture(string const & fileName, EDensity density) : BaseTexture(GetDimensions(fileName, density))
{
typename Traits::image_t image(width(), height());
ReaderPtr<Reader> reader = GetPlatform().GetReader(resourcePath(fileName, density));
ReaderPtr<Reader> reader = GetStyleReader().GetResourceReader(fileName, density);
gil::lodepng_read_and_convert_image(reader, image, typename Traits::color_converter());
upload(&gil::view(image)(0, 0));
}
@ -194,7 +194,7 @@ namespace graphics
{
lock();
view_t v = view(width(), height());
ReaderPtr<Reader> reader = GetPlatform().GetReader(resourcePath(fileName, density));
ReaderPtr<Reader> reader = GetStyleReader().GetResourceReader(fileName, density);
gil::lodepng_read_and_convert_view(reader, v, typename Traits::color_converter());
upload(&v(0, 0));
unlock();

View file

@ -18,6 +18,8 @@
#include "base/logging.hpp"
#include "base/exception.hpp"
#include "indexer/map_style_reader.hpp"
namespace graphics
{
typedef gl::Texture<DATA_TRAITS, true> TDynamicTexture;
@ -269,7 +271,7 @@ namespace
{
try
{
ReaderPtr<Reader> reader(GetPlatform().GetReader(resourcePath(skinFileName, density)));
ReaderPtr<Reader> reader(GetStyleReader().GetResourceReader(skinFileName, density));
reader.ReadAsString(m_skinBuffer);
size_t i = m_skinBuffer.find("file=\"", 0);

View file

@ -4,6 +4,7 @@
#include "indexer/scales.hpp"
#include "indexer/classificator.hpp"
#include "indexer/drules_include.hpp"
#include "indexer/map_style_reader.hpp"
#include "defines.hpp"
@ -11,7 +12,6 @@
#include "std/iterator_facade.hpp"
#include "platform/platform.hpp"
#include "platform/settings.hpp"
#include "base/logging.hpp"
@ -20,8 +20,6 @@
namespace
{
uint32_t const DEFAULT_BG_COLOR = 0xEEEEDD;
char const * const MAP_STYLE_KEY = "MapStyleKey";
}
namespace drule {
@ -396,20 +394,6 @@ namespace
return backgroundColor;
}
string GetRulesFile(MapStyle mapStyle)
{
switch (mapStyle)
{
case MapStyleLight:
return DRAWING_RULES_LIGHT_BIN_FILE;
case MapStyleDark:
return DRAWING_RULES_DARK_BIN_FILE;
default:
LOG(LWARNING, ("Unknown map style, use light instead"));
return DRAWING_RULES_LIGHT_BIN_FILE;
}
}
}
void RulesHolder::LoadFromBinaryProto(string const & s)
@ -427,25 +411,10 @@ void RulesHolder::LoadFromBinaryProto(string const & s)
void LoadRules()
{
string const rulesFile = GetRulesFile(GetCurrentMapStyle());
string buffer;
ModelReaderPtr(GetPlatform().GetReader(rulesFile)).ReadAsString(buffer);
GetStyleReader().GetDrawingRulesReader().ReadAsString(buffer);
rules().LoadFromBinaryProto(buffer);
}
MapStyle GetCurrentMapStyle()
{
int mapStyle;
if (!Settings::Get(MAP_STYLE_KEY, mapStyle))
mapStyle = MapStyleLight;
return static_cast<MapStyle>(mapStyle);
}
void SetCurrentMapStyle(MapStyle mapStyle)
{
Settings::Set(MAP_STYLE_KEY, static_cast<int>(mapStyle));
}
}

View file

@ -1,8 +1,6 @@
#pragma once
#include "indexer/drawing_rule_def.hpp"
#include "indexer/map_style.hpp"
#include "base/base.hpp"
#include "base/buffer_vector.hpp"
@ -106,7 +104,4 @@ namespace drule
RulesHolder & rules();
void LoadRules();
MapStyle GetCurrentMapStyle();
void SetCurrentMapStyle(MapStyle mapStyle);
}

View file

@ -34,6 +34,7 @@ SOURCES += \
geometry_serialization.cpp \
index.cpp \
index_builder.cpp \
map_style_reader.cpp \
mercator.cpp \
mwm_set.cpp \
mwm_version.cpp \
@ -80,6 +81,7 @@ HEADERS += \
interval_index_builder.hpp \
interval_index_iface.hpp \
map_style.hpp \
map_style_reader.hpp \
mercator.hpp \
mwm_set.hpp \
mwm_version.hpp \

View file

@ -0,0 +1,87 @@
#include "map_style_reader.hpp"
#include "base/logging.hpp"
#include "coding/file_name_utils.hpp"
#include "platform/platform.hpp"
#include "platform/settings.hpp"
#include "std/string.hpp"
namespace
{
char const * const MAP_STYLE_KEY = "MapStyleKey";
string GetDrawingRulesFile(MapStyle mapStyle)
{
switch (mapStyle)
{
case MapStyleLight:
return "drules_proto.bin";
case MapStyleDark:
return "drules_proto_dark.bin";
default:
LOG(LWARNING, ("Unknown map style", mapStyle));
return string();
}
}
string GetDensitySuffix(graphics::EDensity density)
{
char const * const str = graphics::convert(density);
if (nullptr == str)
{
LOG(LERROR, ("Unknown density", density));
return string();
}
return str;
}
string GetStyleSuffix(MapStyle mapStyle)
{
switch (mapStyle)
{
case MapStyleLight:
return "";
case MapStyleDark:
return "_dark";
default:
LOG(LWARNING, ("Unknown map style", mapStyle));
return string();
}
}
} // namespace
void StyleReader::SetCurrentStyle(MapStyle mapStyle)
{
Settings::Set(MAP_STYLE_KEY, static_cast<int>(mapStyle));
}
MapStyle StyleReader::GetCurrentStyle()
{
int mapStyle;
if (!Settings::Get(MAP_STYLE_KEY, mapStyle))
mapStyle = MapStyleLight;
return static_cast<MapStyle>(mapStyle);
}
ReaderPtr<Reader> StyleReader::GetDrawingRulesReader()
{
string const rulesFile = GetDrawingRulesFile(GetCurrentStyle());
return GetPlatform().GetReader(rulesFile);
}
ReaderPtr<Reader> StyleReader::GetResourceReader(string const & file, graphics::EDensity density)
{
string const resourceDir = string("resources-") + GetDensitySuffix(density) + GetStyleSuffix(GetCurrentStyle());
return GetPlatform().GetReader(my::JoinFoldersToPath(resourceDir, file));
}
StyleReader & GetStyleReader()
{
static StyleReader instance;
return instance;
}

View file

@ -0,0 +1,20 @@
#pragma once
#include "coding/reader.hpp"
#include "graphics/defines.hpp"
#include "map_style.hpp"
class StyleReader
{
public:
void SetCurrentStyle(MapStyle mapStyle);
MapStyle GetCurrentStyle();
ReaderPtr<Reader> GetDrawingRulesReader();
ReaderPtr<Reader> GetResourceReader(string const & file, graphics::EDensity density);
};
extern StyleReader & GetStyleReader();

View file

@ -26,11 +26,12 @@
#include "search/result.hpp"
#include "indexer/categories_holder.hpp"
#include "indexer/classificator_loader.hpp"
#include "indexer/drawing_rules.hpp"
#include "indexer/feature.hpp"
#include "indexer/map_style_reader.hpp"
#include "indexer/mwm_version.hpp"
#include "indexer/scales.hpp"
#include "indexer/classificator_loader.hpp"
/// @todo Probably it's better to join this functionality.
//@{
@ -1654,13 +1655,13 @@ void Framework::CreateDrapeEngine(dp::RefPointer<dp::OGLContextFactory> contextF
void Framework::SetMapStyle(MapStyle mapStyle)
{
drule::SetCurrentMapStyle(mapStyle);
GetStyleReader().SetCurrentStyle(mapStyle);
drule::LoadRules();
}
MapStyle Framework::GetMapStyle() const
{
return drule::GetCurrentMapStyle();
return GetStyleReader().GetCurrentStyle();
}
void Framework::SetupMeasurementSystem()

View file

@ -11,6 +11,7 @@
#include "coding/parse_xml.hpp"
#include "indexer/drawing_rules.hpp"
#include "indexer/map_style_reader.hpp"
#include "base/logging.hpp"
@ -167,12 +168,12 @@ SoftwareRenderer::SoftwareRenderer(graphics::GlyphCache::Params const & glyphCac
m_symbolsIndex[symbolName] = rect;
});
ReaderSource<ReaderPtr<Reader>> source(ReaderPtr<Reader>(pl.GetReader(resourcePath("basic.skn", density))));
ReaderSource<ReaderPtr<Reader>> source(ReaderPtr<Reader>(GetStyleReader().GetResourceReader("basic.skn", density)));
if (!ParseXML(source, loader))
LOG(LERROR, ("Error parsing skin"));
ASSERT(!textureFileName.empty(), ());
ReaderPtr<Reader> texReader(pl.GetReader(resourcePath(textureFileName, density)));
ReaderPtr<Reader> texReader(GetStyleReader().GetResourceReader(textureFileName, density));
vector<uint8_t> textureData;
LodePNG::loadFile(textureData, texReader);
VERIFY(LodePNG::decode(m_symbolsSkin, m_skinWidth, m_skinHeight, textureData) == 0, ());