libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CanvasView.hpp
Go to the documentation of this file.
1 #ifndef CANVASVIEW_HPP
2 #define CANVASVIEW_HPP
3 /*
4 This file is part of AlgAudio.
5 
6 AlgAudio, Copyright (C) 2015 CeTA - Audiovisual Technology Center
7 
8 AlgAudio is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12 
13 AlgAudio is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with AlgAudio. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include <queue>
23 #include "UI/UIContainer.hpp"
24 #include "ModuleUI/ModuleGUI.hpp"
25 #include "Canvas.hpp"
26 #include "Alertable.hpp"
27 
28 namespace AlgAudio{
29 
38 public:
40  static std::shared_ptr<CanvasView> CreateEmpty(std::shared_ptr<Window> parent);
41 
42  virtual void CustomDraw(DrawContext& c);
43  virtual bool CustomMousePress(bool,MouseButton,Point2D);
44  virtual void CustomMouseEnter(Point2D);
45  virtual void CustomMouseLeave(Point2D);
46  virtual void OnKeyboard(KeyData);
47 
48  virtual void Clear() override {}
49  virtual Point2D GetChildPos(std::shared_ptr<UIWidget>) const override;
50 
52  virtual void CustomMouseMotion(Point2D from_abs,Point2D to_abs) override;
59  void MouseMotionOverCanvasPlane(Point2D from_rel, Point2D to_rel);
60 
65  LateReturn<> AddModule(std::string id, Point2D position);
66 
69  void RemoveSelected();
70 
73  void CenterView();
74 
76 
78  void IncreaseZoom();
79  void DecreaseZoom();
80  void SetZoom(float level);
82 
87  void SwitchTopLevelCanvas(std::shared_ptr<Canvas> canvas, std::string name);
88 
91  std::shared_ptr<Canvas> GetCurrentCanvas() { if(canvas_stack.size() == 0) return nullptr; else return canvas_stack.back().first; }
94  std::shared_ptr<Canvas> GetTopCanvas() { if(canvas_stack.size() == 0) return nullptr; else return canvas_stack.front().first; }
95 
98  void EnterCanvas(std::shared_ptr<Canvas> canvas, std::string name="");
101  void ExitCanvas();
104  std::vector<std::string> GetCanvasStackPath();
108 
109 private:
110  CanvasView(std::shared_ptr<Window> parent);
111 
114  std::deque<std::pair<std::shared_ptr<Canvas>, std::string>> canvas_stack;
115 
117  std::vector<std::shared_ptr<ModuleGUI>> module_guis;
118 
119  void CreateModuleGUIs();
120  void ResetUI();
121 
122  // Used to determine which module was clicked knowing the click position.
123  int InWhich(Point2D relative_pos);
124  int CurveStrengthFuncA(Point2D a, Point2D b);
125  int CurveStrengthFuncB(Point2D a, Point2D b);
126  // These flags are set according to the mouse buttons state.
127  bool lmb_down = false;
128  bool mmb_down = false;
129 
132  enum MouseDownMode{
133  ModeNone,
134  ModeModuleBody,
135  ModeCaptured,
136  ModeInlet,
137  ModeOutlet,
138  ModeSlider,
139  ModeSliderInlet,
140  ModeSliderRelativeOutlet,
141  ModeSliderAbsoluteOutlet,
142  };
143  MouseDownMode mouse_down_mode;
145  UIWidget::ID mouse_down_elem_widgetid;
147  std::string mouse_down_elem_paramid;
149  Point2D mouse_down_position, drag_position;
152  int mouse_down_id = -1;
153 
157  std::list<std::pair<std::shared_ptr<ModuleGUI>, Point2D>> selection;
158  void ClearSelection();
159  void SelectSingle(std::shared_ptr<ModuleGUI>);
160  void AddToSelection(std::shared_ptr<ModuleGUI>);
161 
171  inline Point2D_<float> PositionRelToAbs(Point2D_<float> rel){ return (rel - view_position)*view_zoom + current_size/2; }
172  inline Point2D_<float> PositionAbsToRel(Point2D_<float> abs){ return view_position + (abs - current_size/2)/view_zoom; }
173 
175  bool drag_in_progress = false;
176  bool view_move_in_progress = false;
178  enum DragMode{
179  DragModeMove,
180  DragModeConnectAudioFromInlet,
181  DragModeConnectAudioFromOutlet,
182  DragModeConnectDataFromInlet,
183  DragModeConnectDataFromRelativeOutlet,
184  DragModeConnectDataFromAbsoluteOutlet,
185  DragModeSlider,
186  DragModeBBSelect,
187  };
188  DragMode drag_mode;
191  void FinalizeAudioConnectingDrag(int inlet_module_id, UIWidget::ID inlet_id, int outlet_module_id, UIWidget::ID outlet_id);
192  void FinalizeDataConnectingDrag(int inlet_module_id, UIWidget::ID inlet_slider_id, int outlet_module_id, UIWidget::ID outlet_slider_id, Canvas::DataConnectionMode);
193 
195  Point2D mmb_down_pos_abs;
198  Point2D view_move_start_view_position;
199 
201  void StopDrag();
202 
205  enum class PotentialWireMode{
206  None,
207  New,
208  Remove,
209  };
210  enum class PotentialWireType{
211  Audio,
212  DataRelative,
213  DataAbsolute,
214  };
217  PotentialWireMode potential_wire = PotentialWireMode::None;
218  PotentialWireType potential_wire_type;
219  std::pair<std::pair<int,std::string>, std::pair<int,std::string>> potential_wire_connection;
220  // Animated fadeout of potential wire.
221  Subscription fadeout_anim;
222  PotentialWireMode fadeout_wire = PotentialWireMode::None;
223  PotentialWireType fadeout_wire_type;
224  std::pair<std::pair<std::weak_ptr<ModuleGUI>,std::string>, std::pair<std::weak_ptr<ModuleGUI>,std::string>> fadeout_wire_connection;
225  void FadeoutWireStart(PotentialWireMode m);
226  void FadeoutWireStep(float delta);
227  float fadeout_phase;
228 
229  // Flags remebering held keys.
230  bool shift_held = false, ctrl_held = false, alt_held = false;
231 
233  Point2D view_position = Point2D(0,0);
234  float view_zoom = 1.0;
235 };
236 
237 } // namespace AlgAudio
238 
239 #endif //CANVASVIEW_HPP
void MouseMotionOverCanvasPlane(Point2D from_rel, Point2D to_rel)
Definition: DrawContext.hpp:65
Signal on_canvas_stack_path_changed
Definition: CanvasView.hpp:107
std::weak_ptr< UIWidget > parent
Definition: UIWidget.hpp:85
virtual void CustomMouseMotion(Point2D from_abs, Point2D to_abs) override
Point2D_< int > Point2D
Definition: Utilities.hpp:35
virtual void Clear() override
Definition: CanvasView.hpp:48
Size2D current_size
Definition: UIWidget.hpp:174
virtual Point2D GetChildPos(std::shared_ptr< UIWidget >) const override
MouseButton
Definition: Utilities.hpp:190
std::vector< std::string > GetCanvasStackPath()
void SwitchTopLevelCanvas(std::shared_ptr< Canvas > canvas, std::string name)
void SetZoom(float level)
DataConnectionMode
Definition: Canvas.hpp:129
void EnterCanvas(std::shared_ptr< Canvas > canvas, std::string name="")
virtual void CustomDraw(DrawContext &c)
Definition: CanvasView.hpp:37
virtual void OnKeyboard(KeyData)
Definition: UIContainer.hpp:60
Definition: Alertable.hpp:26
Definition: UIWidget.hpp:137
virtual bool CustomMousePress(bool, MouseButton, Point2D)
std::shared_ptr< Canvas > GetCurrentCanvas()
Definition: CanvasView.hpp:91
virtual void CustomMouseEnter(Point2D)
virtual void CustomMouseLeave(Point2D)
std::shared_ptr< Canvas > GetTopCanvas()
Definition: CanvasView.hpp:94
static std::shared_ptr< CanvasView > CreateEmpty(std::shared_ptr< Window > parent)
Definition: Signal.hpp:145
LateReturn AddModule(std::string id, Point2D position)
Definition: LateReturn.hpp:35
Definition: Utilities.hpp:157