Commit 268fb35c authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add the element interface and stack

parent 854c9a63
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -8,15 +8,24 @@
 * <http://www.gnu.org/licenses/>.
 */
#pragma once
#include <stack>
#include "element.h"
#include "elements/plot/plot_config.h"
#include "graphics/layer.h"

namespace signaltk {

struct Context {
  PlotConfig plot_config;
  std::stack<ElementRef> elements;
  Layer frame;
};

template <typename T>
Status stack_head(Context* ctx, T** head);

template <typename T>
Status stack_head(const Context& ctx, T const** head);

} // namespace signaltk

#include "context_impl.h"
+35 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "signaltk" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * libstx is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#pragma once

namespace signaltk {

template <typename T>
Status stack_head(Context* ctx, T** head) {
  if (ctx->elements.empty()) {
    return Status::ERROR_INVALID_ELEM;
  }

  *head = static_cast<T*>(ctx->elements.top().get());
  return OK;
}

template <typename T>
Status stack_head(const Context& ctx, T const** head) {
  if (ctx.elements.empty()) {
    return Status::ERROR_INVALID_ELEM;
  }

  *head = static_cast<const T*>(ctx.elements.top().get());
  return OK;
}

} // namespace signaltk

elements/element.h

0 → 100644
+24 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "signaltk" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * libstx is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#pragma once
#include <memory>

namespace signaltk {

struct Element {

  virtual ~Element() = default;

};

using ElementRef = std::unique_ptr<Element>;

} // namespace signaltk
+25 −5
Original line number Diff line number Diff line
@@ -23,6 +23,11 @@ AxisDefinition::AxisDefinition() :
    tick_length_rem(kDefaultTickLengthREM) {}

Status plot_axis_add(Context* ctx, AxisPosition pos) {
  PlotConfig* elem;
  if (auto rc = stack_head(ctx, &elem); rc) {
    return rc;
  }

  auto axis_config = std::make_unique<AxisDefinition>();
  axis_config->position = pos;

@@ -41,18 +46,28 @@ Status plot_axis_add(Context* ctx, AxisPosition pos) {
      break;
  }

  ctx->plot_config.axes.emplace_back(std::move(axis_config));
  elem->axes.emplace_back(std::move(axis_config));
  return OK;
}

Status plot_axis_addtick(Context* ctx, float offset) {
  auto& axis_config = ctx->plot_config.axes.back();
  PlotConfig* elem;
  if (auto rc = stack_head(ctx, &elem); rc) {
    return rc;
  }

  auto& axis_config = elem->axes.back();
  axis_config->ticks.emplace_back(offset);
  return OK;
}

Status plot_axis_addlabel(Context* ctx, float offset, const char* label) {
  auto& axis_config = ctx->plot_config.axes.back();
  PlotConfig* elem;
  if (auto rc = stack_head(ctx, &elem); rc) {
    return rc;
  }

  auto& axis_config = elem->axes.back();
  axis_config->labels.emplace_back(offset, label);
  return OK;
}
@@ -168,10 +183,15 @@ Status plot_render_axis_horizontal(
}

Status plot_render_axis(Context* ctx, int i) {
  assert(i < ctx->plot_config.axes.size());
  PlotConfig* elem;
  if (auto rc = stack_head(ctx, &elem); rc) {
    return rc;
  }

  assert(i < elem->axes.size());

  int padding = 80;
  const auto& axis = ctx->plot_config.axes[i];
  const auto& axis = elem->axes[i];

  Status rc;
  switch (axis->position) {
+4 −1
Original line number Diff line number Diff line
@@ -11,11 +11,14 @@
#include "signaltk.h"
#include "axes.h"
#include "plot_domain.h"
#include "elements/element.h"
#include <memory>

namespace signaltk {

struct PlotConfig {
struct PlotConfig : public Element {
  static constexpr const char* ID = "plot";

  PlotDomain x_domain;
  double x_min;
  double x_max;
Loading