ofxOui - UI Components for OpenFrameworks

ofxOuiGraph.hpp
Go to the documentation of this file.
1 /*
2  * ofxOuiGraph.hpp
3  *
4  * Graph view area
5  *
6  * This code has been authored by Jules Francoise <jfrancoi@sfu.ca>
7  * during his postdoc contract at Simon Fraser University (MovingStories
8  * project).
9  * See:
10  * http://julesfrancoise.com/
11  * http://movingstories.ca/
12  *
13  * Copyright (C) 2016 Jules Francoise.
14  *
15  * This Source Code Form is subject to the terms of the Mozilla Public
16  * License, v. 2.0. If a copy of the MPL was not distributed with this
17  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
18  */
19 
20 #ifndef ofxOuiGraph_hpp
21 #define ofxOuiGraph_hpp
22 
23 #include "ofMain.h"
24 #include "ofxOuiTextBox.hpp"
25 
26 namespace ofxOui {
27 
32 class Graph {
33  public:
34  class Ruler {
35  public:
36  enum class Layout { Horizontal, Vertical };
37 
38  Ruler(Layout layout_ = Layout::Horizontal) : layout(layout_) {}
39  void update();
40  void draw();
41 
42  float x;
43  float y;
44  float width;
45  float height;
46 
47  float min;
48  float max;
49 
51 
52  protected:
53  vector<float> ticks_minor;
54  vector<float> ticks_major;
55  vector<TextBox> ticks_labels;
56  };
57 
61  struct Bounds {
62  float x_min = 0.;
63  float x_max = 1.;
64  float y_min = 0.;
65  float y_max = 1.;
66  };
67 
72  class BoundsChangedEvent : public ofEventArgs {
73  public:
78  BoundsChangedEvent(Graph* sender_);
79 
84  };
85 
89  Graph();
90 
94  ~Graph();
95 
96  void drawRuler();
97  void setupViewport();
98  void restoreViewport();
99 
108  template <typename T, typename args, class ListenerClass>
109  void onBoundsChanged(T* owner,
110  void (ListenerClass::*listenerMethod)(args)) {
111  using namespace std::placeholders;
112  if (owner)
114  std::bind(listenerMethod, owner, _1);
115  else
117  }
118 
124  bool insideViewArea(ofVec2f const& pointAsPixels) const;
125 
131  bool insideGraphArea(ofVec2f const& pointAsCoord) const;
132 
138  ofVec2f coord2pix(ofVec2f const& pointAsCoord) const;
139 
145  ofVec2f pix2coord(ofVec2f const& pointAsPixels) const;
146 
152  ofVec2f scale2view(ofVec2f const& scaleAsCoord) const;
153 
159  ofVec2f scale2graph(ofVec2f const& scaleAsPixels) const;
160 
164  ofRectangle view;
165 
170 
171  bool iso_zoom = true;
172  std::pair<bool, Ruler> horizontal_ruler = {true, {}};
173  std::pair<bool, Ruler> vertical_ruler = {true, {Ruler::Layout::Vertical}};
174 
175  protected:
176  void mouseScrolled(ofMouseEventArgs& e);
177  std::function<void(Graph::BoundsChangedEvent&)>
179 
180  bool viewport_valid = false;
181 };
182 }
183 
184 #endif
ofVec2f scale2graph(ofVec2f const &scaleAsPixels) const
Scale pixel units to graph units.
Definition: ofxOuiGraph.cpp:196
vector< TextBox > ticks_labels
Definition: ofxOuiGraph.hpp:55
Definition: ofxOuiGraph.hpp:34
std::function< void(Graph::BoundsChangedEvent &)> bounds_changed_event_callback_
Definition: ofxOuiGraph.hpp:178
ofVec2f pix2coord(ofVec2f const &pointAsPixels) const
Convert pixel coordinates to graph coordinates.
Definition: ofxOuiGraph.cpp:179
float y
Definition: ofxOuiGraph.hpp:43
float max
Definition: ofxOuiGraph.hpp:48
Ruler(Layout layout_=Layout::Horizontal)
Definition: ofxOuiGraph.hpp:38
ofVec2f scale2view(ofVec2f const &scaleAsCoord) const
Scale graph units to pixel units.
Definition: ofxOuiGraph.cpp:190
float width
Definition: ofxOuiGraph.hpp:44
ofRectangle view
View area rectangle (in pixels)
Definition: ofxOuiGraph.hpp:164
void draw()
Definition: ofxOuiGraph.cpp:72
void drawRuler()
Definition: ofxOuiGraph.cpp:105
Layout
Definition: ofxOuiGraph.hpp:36
Graph * sender
pointer to the sender component
Definition: ofxOuiGraph.hpp:83
void setupViewport()
Definition: ofxOuiGraph.cpp:128
void mouseScrolled(ofMouseEventArgs &e)
Definition: ofxOuiGraph.cpp:202
std::pair< bool, Ruler > vertical_ruler
Definition: ofxOuiGraph.hpp:173
vector< float > ticks_major
Definition: ofxOuiGraph.hpp:54
vector< float > ticks_minor
Definition: ofxOuiGraph.hpp:53
Definition: ofxOuiColormap.hpp:25
float min
Definition: ofxOuiGraph.hpp:47
bool insideViewArea(ofVec2f const &pointAsPixels) const
Checks if a point is in the view area.
Definition: ofxOuiGraph.cpp:154
float x
Definition: ofxOuiGraph.hpp:42
bool viewport_valid
Definition: ofxOuiGraph.hpp:180
Event arguments for pan/zoom events resulting in bounds changes of the graph area.
Definition: ofxOuiGraph.hpp:72
Graph()
Default Constructor.
Definition: ofxOuiGraph.cpp:95
bool insideGraphArea(ofVec2f const &pointAsCoord) const
Checks if a point is in the graph area.
Definition: ofxOuiGraph.cpp:161
void update()
Definition: ofxOuiGraph.cpp:22
void restoreViewport()
Definition: ofxOuiGraph.cpp:148
Bounds graph_area
Bounds of the graph space.
Definition: ofxOuiGraph.hpp:169
ofVec2f coord2pix(ofVec2f const &pointAsCoord) const
Convert graph coordinates to pixel coordinates.
Definition: ofxOuiGraph.cpp:168
~Graph()
Destructor.
Definition: ofxOuiGraph.cpp:100
Bounds of the graph space.
Definition: ofxOuiGraph.hpp:61
void onBoundsChanged(T *owner, void(ListenerClass::*listenerMethod)(args))
Set callback function for bounds changed events.
Definition: ofxOuiGraph.hpp:109
std::pair< bool, Ruler > horizontal_ruler
Definition: ofxOuiGraph.hpp:172
Layout layout
Definition: ofxOuiGraph.hpp:50
float height
Definition: ofxOuiGraph.hpp:45
2D Graph Area (with virtual coordinate system)
Definition: ofxOuiGraph.hpp:32
bool iso_zoom
Definition: ofxOuiGraph.hpp:171