Browse Source

Move SVG_DPI and mm2px() to svg.hpp.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
6ffea7d25d
4 changed files with 41 additions and 41 deletions
  1. +1
    -24
      include/app/common.hpp
  2. +14
    -14
      include/componentlibrary.hpp
  3. +24
    -0
      include/svg.hpp
  4. +2
    -3
      src/svg.cpp

+ 1
- 24
include/app/common.hpp View File

@@ -2,8 +2,8 @@
#include <jansson.h> #include <jansson.h>


#include <common.hpp> #include <common.hpp>
#include <math.hpp>
#include <ui/common.hpp> #include <ui/common.hpp>
#include <svg.hpp> // for mm2px(), etc




namespace rack { namespace rack {
@@ -14,29 +14,6 @@ namespace rack {
namespace app { namespace app {




static const float SVG_DPI = 75.f;
static const float MM_PER_IN = 25.4f;


/** Converts inch measurements to pixels */
inline float in2px(float in) {
return in * SVG_DPI;
}

inline math::Vec in2px(math::Vec in) {
return in.mult(SVG_DPI);
}

/** Converts millimeter measurements to pixels */
inline float mm2px(float mm) {
return mm * (SVG_DPI / MM_PER_IN);
}

inline math::Vec mm2px(math::Vec mm) {
return mm.mult(SVG_DPI / MM_PER_IN);
}


// A 1HPx3U module should be 15x380 pixels. Thus the width of a module should be a factor of 15. // A 1HPx3U module should be 15x380 pixels. Thus the width of a module should be a factor of 15.
static const float RACK_GRID_WIDTH = 15; static const float RACK_GRID_WIDTH = 15;
static const float RACK_GRID_HEIGHT = 380; static const float RACK_GRID_HEIGHT = 380;


+ 14
- 14
include/componentlibrary.hpp View File

@@ -133,7 +133,7 @@ typedef TRedGreenBlueLight<> RedGreenBlueLight;
template <typename TBase> template <typename TBase>
struct LargeLight : TBase { struct LargeLight : TBase {
LargeLight() { LargeLight() {
this->box.size = app::mm2px(math::Vec(5.179, 5.179));
this->box.size = mm2px(math::Vec(5.179, 5.179));
} }
}; };


@@ -141,7 +141,7 @@ struct LargeLight : TBase {
template <typename TBase> template <typename TBase>
struct MediumLight : TBase { struct MediumLight : TBase {
MediumLight() { MediumLight() {
this->box.size = app::mm2px(math::Vec(3.176, 3.176));
this->box.size = mm2px(math::Vec(3.176, 3.176));
} }
}; };


@@ -149,7 +149,7 @@ struct MediumLight : TBase {
template <typename TBase> template <typename TBase>
struct SmallLight : TBase { struct SmallLight : TBase {
SmallLight() { SmallLight() {
this->box.size = app::mm2px(math::Vec(2.176, 2.176));
this->box.size = mm2px(math::Vec(2.176, 2.176));
} }
}; };


@@ -157,7 +157,7 @@ struct SmallLight : TBase {
template <typename TBase> template <typename TBase>
struct TinyLight : TBase { struct TinyLight : TBase {
TinyLight() { TinyLight() {
this->box.size = app::mm2px(math::Vec(1.088, 1.088));
this->box.size = mm2px(math::Vec(1.088, 1.088));
} }
}; };


@@ -193,18 +193,18 @@ template <typename TBase>
struct LEDBezelLight : TBase { struct LEDBezelLight : TBase {
LEDBezelLight() { LEDBezelLight() {
this->bgColor = color::BLACK_TRANSPARENT; this->bgColor = color::BLACK_TRANSPARENT;
this->box.size = app::mm2px(math::Vec(6.0, 6.0));
this->box.size = mm2px(math::Vec(6.0, 6.0));
} }
}; };


/** A light to displayed over PB61303. Must add a color by subclassing or templating. /** A light to displayed over PB61303. Must add a color by subclassing or templating.
Don't add this as a child of the PB61303 itself. Instead, just place it over it as a sibling in the scene graph, offset by app::mm2px(math::Vec(0.5, 0.5)).
Don't add this as a child of the PB61303 itself. Instead, just place it over it as a sibling in the scene graph, offset by mm2px(math::Vec(0.5, 0.5)).
*/ */
template <typename TBase> template <typename TBase>
struct PB61303Light : TBase { struct PB61303Light : TBase {
PB61303Light() { PB61303Light() {
this->bgColor = color::BLACK_TRANSPARENT; this->bgColor = color::BLACK_TRANSPARENT;
this->box.size = app::mm2px(math::Vec(9.0, 9.0));
this->box.size = mm2px(math::Vec(9.0, 9.0));
} }
}; };


@@ -529,8 +529,8 @@ struct BefacoSlidePot : app::SvgSlider {


struct LEDSlider : app::SvgSlider { struct LEDSlider : app::SvgSlider {
LEDSlider() { LEDSlider() {
maxHandlePos = app::mm2px(math::Vec(0.738, 0.738).plus(math::Vec(2, 0)));
minHandlePos = app::mm2px(math::Vec(0.738, 22.078).plus(math::Vec(2, 0)));
maxHandlePos = mm2px(math::Vec(0.738, 0.738).plus(math::Vec(2, 0)));
minHandlePos = mm2px(math::Vec(0.738, 22.078).plus(math::Vec(2, 0)));
setBackgroundSvg(Svg::load(asset::system("res/ComponentLibrary/LEDSlider.svg"))); setBackgroundSvg(Svg::load(asset::system("res/ComponentLibrary/LEDSlider.svg")));
} }
}; };
@@ -569,8 +569,8 @@ struct LEDSliderWhite : LEDSlider {
struct LEDSliderHorizontal : app::SvgSlider { struct LEDSliderHorizontal : app::SvgSlider {
LEDSliderHorizontal() { LEDSliderHorizontal() {
horizontal = true; horizontal = true;
maxHandlePos = app::mm2px(math::Vec(22.078, 0.738).plus(math::Vec(0, 2)));
minHandlePos = app::mm2px(math::Vec(0.738, 0.738).plus(math::Vec(0, 2)));
maxHandlePos = mm2px(math::Vec(22.078, 0.738).plus(math::Vec(0, 2)));
minHandlePos = mm2px(math::Vec(0.738, 0.738).plus(math::Vec(0, 2)));
setBackgroundSvg(Svg::load(asset::system("res/ComponentLibrary/LEDSliderHorizontal.svg"))); setBackgroundSvg(Svg::load(asset::system("res/ComponentLibrary/LEDSliderHorizontal.svg")));
} }
}; };
@@ -602,7 +602,7 @@ template <typename TLightBase = RedLight>
struct LEDLightSlider : LightSlider<LEDSlider, TLightBase> { struct LEDLightSlider : LightSlider<LEDSlider, TLightBase> {
LEDLightSlider() { LEDLightSlider() {
this->setHandleSvg(Svg::load(asset::system("res/ComponentLibrary/LEDSliderHandle.svg"))); this->setHandleSvg(Svg::load(asset::system("res/ComponentLibrary/LEDSliderHandle.svg")));
this->light->box.size = app::mm2px(math::Vec(1.524, 3.276));
this->light->box.size = mm2px(math::Vec(1.524, 3.276));
} }
}; };


@@ -610,7 +610,7 @@ template <typename TLightBase = RedLight>
struct LEDLightSliderHorizontal : LightSlider<LEDSliderHorizontal, TLightBase> { struct LEDLightSliderHorizontal : LightSlider<LEDSliderHorizontal, TLightBase> {
LEDLightSliderHorizontal() { LEDLightSliderHorizontal() {
this->setHandleSvg(Svg::load(asset::system("res/ComponentLibrary/LEDSliderHorizontalHandle.svg"))); this->setHandleSvg(Svg::load(asset::system("res/ComponentLibrary/LEDSliderHorizontalHandle.svg")));
this->light->box.size = app::mm2px(math::Vec(3.276, 1.524));
this->light->box.size = mm2px(math::Vec(3.276, 1.524));
} }
}; };


@@ -768,7 +768,7 @@ struct ScrewBlack : app::SvgScrew {
struct SegmentDisplay : widget::Widget { struct SegmentDisplay : widget::Widget {
int lightsLen = 0; int lightsLen = 0;
bool vertical = false; bool vertical = false;
float margin = app::mm2px(0.5);
float margin = mm2px(0.5);


void draw(const DrawArgs& args) override { void draw(const DrawArgs& args) override {
// Background // Background


+ 24
- 0
include/svg.hpp View File

@@ -5,11 +5,35 @@
#include <nanosvg.h> #include <nanosvg.h>


#include <common.hpp> #include <common.hpp>
#include <math.hpp>




namespace rack { namespace rack {




static const float SVG_DPI = 75.f;
static const float MM_PER_IN = 25.4f;


/** Converts inch measurements to pixels */
inline float in2px(float in) {
return in * SVG_DPI;
}

inline math::Vec in2px(math::Vec in) {
return in.mult(SVG_DPI);
}

/** Converts millimeter measurements to pixels */
inline float mm2px(float mm) {
return mm * (SVG_DPI / MM_PER_IN);
}

inline math::Vec mm2px(math::Vec mm) {
return mm.mult(SVG_DPI / MM_PER_IN);
}


struct Svg { struct Svg {
NSVGimage* handle = NULL; NSVGimage* handle = NULL;




+ 2
- 3
src/svg.cpp View File

@@ -1,7 +1,6 @@
#include <svg.hpp> #include <svg.hpp>
#include <map> #include <map>
#include <math.hpp> #include <math.hpp>
#include <app/common.hpp> // for app::SVG_DPI




// #define DEBUG_ONLY(x) x // #define DEBUG_ONLY(x) x
@@ -18,7 +17,7 @@ Svg::~Svg() {




void Svg::loadFile(const std::string& filename) { void Svg::loadFile(const std::string& filename) {
handle = nsvgParseFromFile(filename.c_str(), "px", app::SVG_DPI);
handle = nsvgParseFromFile(filename.c_str(), "px", SVG_DPI);
if (handle) { if (handle) {
INFO("Loaded SVG %s", filename.c_str()); INFO("Loaded SVG %s", filename.c_str());
} }
@@ -31,7 +30,7 @@ void Svg::loadFile(const std::string& filename) {
void Svg::loadString(const std::string& str) { void Svg::loadString(const std::string& str) {
// nsvgParse modifies the input string // nsvgParse modifies the input string
std::string strCopy = str; std::string strCopy = str;
handle = nsvgParse(&strCopy[0], "px", app::SVG_DPI);
handle = nsvgParse(&strCopy[0], "px", SVG_DPI);
if (handle) { if (handle) {
INFO("Loaded SVG"); INFO("Loaded SVG");
} }


Loading…
Cancel
Save