Move class state to map
https://github.com/mapbox/mapbox-gl-js/pull/936
This commit is contained in:
@@ -68,9 +68,12 @@ public:
|
||||
void resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height);
|
||||
|
||||
// Styling
|
||||
void setAppliedClasses(const std::vector<std::string> &classes);
|
||||
void toggleClass(const std::string &name);
|
||||
const std::vector<std::string> &getAppliedClasses() const;
|
||||
void addClass(const std::string&);
|
||||
void removeClass(const std::string&);
|
||||
bool hasClass(const std::string&) const;
|
||||
void setClasses(const std::vector<std::string>&);
|
||||
std::vector<std::string> getClasses() const;
|
||||
|
||||
void setDefaultTransitionDuration(uint64_t milliseconds = 0);
|
||||
void setStyleURL(const std::string &url);
|
||||
void setStyleJSON(std::string newStyleJSON, const std::string &base = "");
|
||||
@@ -188,6 +191,7 @@ private:
|
||||
|
||||
std::string styleURL;
|
||||
std::string styleJSON = "";
|
||||
std::vector<std::string> classes;
|
||||
|
||||
bool debug = false;
|
||||
timestamp animationTime = 0;
|
||||
|
||||
Submodule ios/mapbox-gl-cocoa updated: 40b9b7b8e0...2e5bfbdcd5
@@ -184,7 +184,11 @@ void GLFWView::key(GLFWwindow *window, int key, int /*scancode*/, int action, in
|
||||
case GLFW_KEY_R:
|
||||
if (!mods) {
|
||||
view->map->setDefaultTransitionDuration(300);
|
||||
view->map->toggleClass("night");
|
||||
if (view->map->hasClass("night")) {
|
||||
view->map->removeClass("night");
|
||||
} else {
|
||||
view->map->addClass("night");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_N:
|
||||
|
||||
@@ -311,6 +311,7 @@ void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) {
|
||||
style = std::make_shared<Style>();
|
||||
}
|
||||
style->loadJSON((const uint8_t *)styleJSON.c_str());
|
||||
style->cascadeClasses(classes);
|
||||
fileSource.setBase(base);
|
||||
glyphStore->setURL(style->glyph_url);
|
||||
update();
|
||||
@@ -497,23 +498,44 @@ bool Map::getDebug() const {
|
||||
return debug;
|
||||
}
|
||||
|
||||
void Map::setAppliedClasses(const std::vector<std::string> &classes) {
|
||||
style->setAppliedClasses(classes);
|
||||
if (style->hasTransitions()) {
|
||||
update();
|
||||
void Map::addClass(const std::string& klass) {
|
||||
if (hasClass(klass)) return;
|
||||
classes.push_back(klass);
|
||||
if (style) {
|
||||
style->cascadeClasses(classes);
|
||||
if (style->hasTransitions()) {
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Map::toggleClass(const std::string &name) {
|
||||
style->toggleClass(name);
|
||||
if (style->hasTransitions()) {
|
||||
update();
|
||||
void Map::removeClass(const std::string& klass) {
|
||||
if (!hasClass(klass)) return;
|
||||
classes.erase(std::remove(classes.begin(), classes.end(), klass), classes.end());
|
||||
if (style) {
|
||||
style->cascadeClasses(classes);
|
||||
if (style->hasTransitions()) {
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Map::getAppliedClasses() const {
|
||||
return style->getAppliedClasses();
|
||||
void Map::setClasses(const std::vector<std::string>& classes_) {
|
||||
classes = classes_;
|
||||
if (style) {
|
||||
style->cascadeClasses(classes);
|
||||
if (style->hasTransitions()) {
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Map::hasClass(const std::string& klass) const {
|
||||
return std::find(classes.begin(), classes.end(), klass) != classes.end();
|
||||
}
|
||||
|
||||
std::vector<std::string> Map::getClasses() const {
|
||||
return classes;
|
||||
}
|
||||
|
||||
void Map::setDefaultTransitionDuration(uint64_t milliseconds) {
|
||||
|
||||
@@ -47,31 +47,9 @@ void Style::setDefaultTransitionDuration(uint16_t duration_milliseconds) {
|
||||
defaultTransition.duration = duration_milliseconds;
|
||||
}
|
||||
|
||||
const std::vector<std::string> &Style::getAppliedClasses() const {
|
||||
return appliedClasses;
|
||||
}
|
||||
|
||||
void Style::setAppliedClasses(const std::vector<std::string> &class_names) {
|
||||
appliedClasses = class_names;
|
||||
updateClasses();
|
||||
}
|
||||
|
||||
void Style::toggleClass(const std::string &name) {
|
||||
if (name.length()) {
|
||||
auto it = std::find(appliedClasses.begin(), appliedClasses.end(), name);
|
||||
if (it == appliedClasses.end()) {
|
||||
appliedClasses.push_back(name);
|
||||
} else {
|
||||
appliedClasses.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
updateClasses();
|
||||
}
|
||||
|
||||
void Style::updateClasses() {
|
||||
void Style::cascadeClasses(const std::vector<std::string>& classes) {
|
||||
if (layers) {
|
||||
layers->setClasses(appliedClasses, util::now(), defaultTransition);
|
||||
layers->setClasses(classes, util::now(), defaultTransition);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,8 +78,6 @@ void Style::loadJSON(const uint8_t *const data) {
|
||||
layers = parser.getLayers();
|
||||
sprite_url = parser.getSprite();
|
||||
glyph_url = parser.getGlyphURL();
|
||||
|
||||
updateClasses();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ class Style {
|
||||
public:
|
||||
struct exception : std::runtime_error { exception(const char *msg) : std::runtime_error(msg) {} };
|
||||
|
||||
public:
|
||||
Style();
|
||||
~Style();
|
||||
|
||||
@@ -35,31 +34,20 @@ public:
|
||||
void updateProperties(float z, timestamp t);
|
||||
|
||||
void setDefaultTransitionDuration(uint16_t duration_milliseconds = 0);
|
||||
|
||||
void setAppliedClasses(const std::vector<std::string> &classes);
|
||||
const std::vector<std::string> &getAppliedClasses() const;
|
||||
void toggleClass(const std::string &name);
|
||||
|
||||
// Updates the styling information to reflect the current array
|
||||
// of applied classes.
|
||||
void updateClasses();
|
||||
void cascadeClasses(const std::vector<std::string>&);
|
||||
|
||||
bool hasTransitions() const;
|
||||
|
||||
const std::string &getSpriteURL() const;
|
||||
|
||||
public:
|
||||
util::ptr<StyleLayerGroup> layers;
|
||||
std::vector<std::string> appliedClasses;
|
||||
std::string glyph_url;
|
||||
|
||||
private:
|
||||
std::string sprite_url;
|
||||
|
||||
private:
|
||||
PropertyTransition defaultTransition;
|
||||
bool initial_render_complete = false;
|
||||
|
||||
std::unique_ptr<uv::rwlock> mtx;
|
||||
};
|
||||
|
||||
|
||||
@@ -188,8 +188,8 @@ TEST_P(HeadlessTest, render) {
|
||||
CachingHTTPFileSource fileSource(platform::defaultCacheDatabase());
|
||||
Map map(view, fileSource);
|
||||
|
||||
map.setClasses(classes);
|
||||
map.setStyleJSON(style, base_directory);
|
||||
map.setAppliedClasses(classes);
|
||||
|
||||
view.resize(width, height, pixelRatio);
|
||||
map.resize(width, height, pixelRatio);
|
||||
|
||||
Reference in New Issue
Block a user