Commit d89bbe2c authored by Paul Asmuth's avatar Paul Asmuth
Browse files

control draw and layout order using an (internal) z-index

parent 686a6855
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -5,6 +5,14 @@ scale-y-min: 0;
scale-y-max: 30;
scale-x-padding: 0.6;

bars {
  xs: csv(tests/testdata/city_temperatures_london.csv, month);
  ys: csv(tests/testdata/city_temperatures_london.csv, temperature);
  labels: csv(tests/testdata/city_temperatures_london.csv, temperature_str);
  size: 2.6em;
  color: #4572a7;
}

axis {
  position: left;
  layout: linear(5);
@@ -27,10 +35,3 @@ axis {
  labels: csv(tests/testdata/city_temperatures_london.csv, month_name);
}
bars {
  xs: csv(tests/testdata/city_temperatures_london.csv, month);
  ys: csv(tests/testdata/city_temperatures_london.csv, temperature);
  labels: csv(tests/testdata/city_temperatures_london.csv, temperature_str);
  size: 2.6em;
  color: #4572a7;
}
+17 −13
Original line number Diff line number Diff line
@@ -118,17 +118,25 @@ ReturnCode layout_element(
ReturnCode layout_elements(
    const Layer& layer,
    const Rectangle& parent_bbox,
    const std::vector<ElementRef>& elements,
    std::vector<LayoutInfo>* element_layouts) {
    std::vector<ElementPlacement>* elements) {
  /* sort elements by z-index */
  std::stable_sort(
      elements->begin(),
      elements->end(),
      [] (const ElementPlacement& a, const ElementPlacement& b) {
        return a.element->z_index() < b.element->z_index();
      });

  /* compute bounding boxes */
  LayoutState layout_state;
  layout_state.content_box = parent_bbox;

  for (const auto& e : elements) {
  for (auto& e : *elements) {
    double bbox_w = 0.0;
    double bbox_h = 0.0;

    if (auto rc =
          e->layout(
          e.element->layout(
              layer,
              layout_state.content_box.w,
              layout_state.content_box.h,
@@ -138,25 +146,21 @@ ReturnCode layout_elements(
      return rc;
    }

    LayoutInfo l;
    l.bounding_box = parent_bbox;

    if (auto rc =
          layout_element(
              e->layout_settings(),
              e.element->layout_settings(),
              bbox_w,
              bbox_h,
              &layout_state,
              &l.content_box);
              &e.layout.content_box);
          !rc.isSuccess()) {
      return rc;
    }

    element_layouts->emplace_back(l);
  }

  for (auto& l : *element_layouts) {
    l.inner_box = layout_state.content_box;
  for (auto& e : *elements) {
    e.layout.bounding_box = parent_bbox;
    e.layout.inner_box = layout_state.content_box;
  }

  return OK;
+6 −2
Original line number Diff line number Diff line
@@ -90,6 +90,11 @@ struct LayoutInfo {

};

struct ElementPlacement {
  ElementRef element;
  LayoutInfo layout;
};

ReturnCode layout_element(
    const LayoutSettings& config,
    double bbox_w,
@@ -100,8 +105,7 @@ ReturnCode layout_element(
ReturnCode layout_elements(
    const Layer& layer,
    const Rectangle& parent_bbox,
    const std::vector<ElementRef>& elements,
    std::vector<LayoutInfo>* element_layouts);
    std::vector<ElementPlacement>* elements);

} // namespace plotfx

source/element.cc

0 → 100644
+49 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "plotfx" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "element.h"
#include "core/layout.h"

namespace plotfx {

int32_t Element::z_index() const {
  auto layout = layout_settings();

  switch (layout.position) {
    case Position::RELATIVE:
      return 1;
    case Position::TOP:
      return 0;
  }

  return -1;
}

} // namespace plotfx
+3 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include "plist/plist.h"
#include "utils/return_code.h"
#include "core/format.h"
#include "graphics/geometry.h"

namespace plotfx {
struct Layer;
@@ -79,6 +80,7 @@ using ElementLayoutAsFn = std::function<ReturnCode (
struct Element {
  virtual ~Element() = default;
  virtual const LayoutSettings& layout_settings() const = 0;
  int32_t z_index() const;
  ElementLayoutFn layout;
  ElementDrawFn draw;
};
@@ -90,6 +92,7 @@ struct ElementInstance : public Element {
  const LayoutSettings& layout_settings() const override {
    return config.layout;
  }

};

using ElementRef = std::shared_ptr<Element>;
Loading