1 #ifndef WOLF_GRAPH_DEFINED_H
2 #define WOLF_GRAPH_DEFINED_H
3 
4 #include "src/DistrhoDefines.h"
5 
6 START_NAMESPACE_DISTRHO
7 
8 namespace wolf
9 {
10 /**
11    * The max number of vertices that can be in the graph at the same time.
12    */
13 const int maxVertices = 99;
14 
15 enum CurveType
16 {
17   SingleCurve = 0,
18   DoubleCurve,
19   StairsCurve,
20   WaveCurve
21 };
22 
23 enum WarpType
24 {
25   None = 0,
26   BendPlus,
27   BendMinus,
28   BendPlusMinus,
29   SkewPlus,
30   SkewMinus,
31   SkewPlusMinus
32 };
33 
34 class Graph;
35 
36 class Vertex
37 {
38 public:
39   friend class Graph;
40 
41   float getX();
42   float getY();
43   float getTension() const;
44   CurveType getType() const;
45 
46   void setX(float x);
47   void setY(float y);
48   void setPosition(float x, float y);
49   void setTension(float tension);
50   void setType(CurveType type);
51   void setGraphPtr(Graph *graphPtr);
52 
53 protected:
54   Vertex();
55   Vertex(float posX, float posY, float tension, CurveType type, Graph *graphPtr);
56 
57   float warpCoordinate(const float coordinate, const float warpAmount, const WarpType warpType) const;
58   float unwarpCoordinate(float coordinate, const float warpAmount, const WarpType warpType) const;
59 
60 private:
61   float x;
62   float y;
63   bool xDirty;
64   bool yDirty;
65   float tension;
66   float hWarp;
67   float vWarp;
68   float graphHWarp;
69   float graphVWarp;
70   WarpType graphHType;
71   WarpType graphVType;
72   CurveType type;
73 
74   Graph *graphPtr;
75 };
76 
77 class Graph
78 {
79 public:
80   Graph();
81 
82   void insertVertex(float x, float y, float tension = 0.0f, CurveType type = CurveType::SingleCurve);
83   void removeVertex(int index);
84   Vertex *getVertexAtIndex(int index);
85 
86   void setTensionAtIndex(int index, float tension);
87 
88   /**
89    * Return the number of vertices contained in the graph.
90    */
91   int getVertexCount();
92 
93   static float getOutValue(float input, float tension, float p1x, float p1y, float p2x, float p2y, CurveType type);
94 
95   /**
96    * Get the y value at x in the graph.
97    */
98   float getValueAt(float x);
99 
100   /**
101    * Empty the graph.
102    */
103   void clear();
104 
105   /**
106    * Save the graph into a string.
107    */
108   const char *serialize();
109 
110   bool getBipolarMode();
111   void setBipolarMode(bool bipolarMode);
112 
113   /**
114    * warp getter/setters
115    */
116   //-------------------------------------------
117   void setHorizontalWarpAmount(float warp);
118   float getHorizontalWarpAmount() const;
119 
120   void setVerticalWarpAmount(float warp);
121   float getVerticalWarpAmount() const;
122 
123   void setHorizontalWarpType(WarpType warpType);
124   WarpType getHorizontalWarpType() const;
125 
126   void setVerticalWarpType(WarpType warpType);
127   WarpType getVerticalWarpType() const;
128   //-------------------------------------------
129 
130   /**
131    * Rebuild the graph from a string.
132    */
133   void rebuildFromString(const char *serializedGraph);
134 
135 private:
136   Vertex vertices[maxVertices];
137   int vertexCount;
138 
139   float horizontalWarpAmount;
140   float verticalWarpAmount;
141 
142   WarpType horizontalWarpType;
143   WarpType verticalWarpType;
144 
145   bool bipolarMode;
146 
147   //format: x,y,tension,type;
148   char serializationBuffer[(sizeof(char) * 256 + 4) * maxVertices + 1];
149 };
150 
151 } // namespace wolf
152 
153 END_NAMESPACE_DISTRHO
154 
155 #endif