libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Utilities.hpp
Go to the documentation of this file.
1 #ifndef UTILITIES_HPP
2 #define UTILITIES_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 #include <string>
22 #include <vector>
23 #include <cmath>
24 #include "Signal.hpp"
25 #include "Exception.hpp"
26 
27 struct SDL_Color;
28 struct SDL_KeyboardEvent;
29 
30 namespace AlgAudio{
31 
32 // Forward declarations
33 template <typename T=int>
34 struct Point2D_;
35 struct Rect;
36 
37 typedef Point2D_<int> Point2D;
38 
40 struct Size2D{
41  Size2D(int w = 0, int h = 0) : width(w), height(h) {}
43  bool Fits(const Size2D& other) const{ return (width >= other.width && height >= other.height); }
45  bool IsEmpty() const {return width <= 0 || height <= 0; }
46  int width, height;
47  bool operator==(const Size2D& other) const{ return (width == other.width && height == other.height); }
48  bool operator!=(const Size2D& other) const{ return !(*this == other); }
49  std::string ToString() const {return "{" + std::to_string(width) + ", " + std::to_string(height) + " }"; }
50  Size2D operator+(const Size2D& other) const { return Size2D(width + other.width, height + other.height);}
51  Size2D operator-(const Size2D& other) const { return Size2D(width - other.width, height - other.height);}
52  Size2D operator/(const int& i) const {return Size2D(width/i, height/i);}
53  Point2D ToPoint() const;
54 };
55 
57 template <typename T>
58 struct Point2D_{
59  Point2D_(T x_ = 0, T y_ = 0) : x(x_), y(y_) {}
60  template <typename Q>
61  Point2D_(const Point2D_<Q> &other) : x(other.x), y(other.y) {}
62  T x, y;
63  bool operator==(const Point2D_<T>& other) const{ return (x == other.x && y == other.y); }
64  bool operator!=(const Point2D_<T>& other) const{ return !(*this == other); }
65  std::string ToString() const {return "{" + std::to_string(x) + ", " + std::to_string(y) + " }"; }
66  Point2D_<T> operator+(const Point2D_<T>& other) const { return Point2D_<T>(x + other.x, y + other.y);}
67  Point2D_<T> operator-(const Point2D_<T>& other) const { return Point2D_<T>(x - other.x, y - other.y);}
68  Point2D_<T> operator-() const { return Point2D_<T>(-x,-y);}
69  Point2D_<T> operator/(const T& t) const {return Point2D_<T>(x/t, y/t);}
70  Point2D_<T> operator*(const T& t) const {return Point2D_<T>(x*t, y*t);}
71  Point2D_<T> operator+(const Size2D& other) const { return Point2D_<T>(x + other.width, y + other.height);}
72  Point2D_<T> operator-(const Size2D& other) const { return Point2D_<T>(x - other.width, y - other.height);}
73  Point2D_<T>& operator+=(const Point2D_<T>& other) { x += other.x; y += other.y; return *this;}
74  Point2D_<T>& operator-=(const Point2D_<T>& other) { x -= other.x; y -= other.y; return *this;}
76  bool IsInside(Point2D_<T> r, Size2D s) const { return (x >= r.x) && (x < r.x + s.width) && (y >= r.y) && (y < r.y + s.height);}
78  bool IsInside(const Rect& r) const;
80  static float Distance(Point2D_<T> a, Point2D_<T> b){return sqrt(float((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)));}
81 };
82 inline Point2D Size2D::ToPoint() const{ return Point2D(width,height);}
83 
84 template <typename T>
85 Point2D_<T> operator*(const T& t, const Point2D_<T>& a) {
86  return Point2D_<T>(a.x*t, a.y*t);
87 }
88 
89 // Special case for multiplying and dividing a classic point with a float. To
90 // prevent accuracy loss specialisations below return a float point. Also, if
91 // it was not for these specialisations, multiplying a point by 0.5 would
92 // implicitly convert 0.5 to an int and thus cause a SIGFPE.
93 inline Point2D_<float> operator*(const float& t, const Point2D_<int>& a) {
94  return Point2D_<float>(a.x*t, a.y*t);
95 }
96 inline Point2D_<float> operator*(const Point2D_<int>& a, const float& t) {
97  return Point2D_<float>(a.x*t, a.y*t);
98 }
99 inline Point2D_<float> operator/(const Point2D_<int>& a, const float& t) {
100  return Point2D_<float>(a.x/t, a.y/t);
101 }
102 
104 struct Rect{
105  Point2D a;
106  Point2D b;
107  Rect() {}
110  Rect(Point2D a_, Point2D b_){
111  if(a_.x <= b_.x && a_.y <= b_.y) {a = a_; b = b_;}
112  else if(a_.x >= b_.x && a_.y >= b_.y) {a = b_; b = a_;}
113  else if(a_.x <= b_.x && a_.y >= b_.y) {a = {a_.x,b_.y}; b = {b_.x,a_.y};}
114  else if(a_.x >= b_.x && a_.y <= b_.y) {a = {b_.x,a_.y}; b = {a_.x,b_.y};}
115  }
116  Rect(Point2D a_, Size2D s) : a(a_), b(a_+s) {}
118  inline Size2D Size() const {
119  auto q = b-a;
120  return {q.x,q.y};
121  }
123  Rect MoveOffset(Point2D p) const {return Rect(a+p, b+p);}
125  inline Point2D Center() const {return a + Size()/2;}
127  bool IsFullyInside(const Rect& other){
128  return a.x >= other.a.x && a.y >= other.a.y && b.x <= other.b.x && b.y <= other.b.y;
129  }
130 };
131 
132 template <typename T>
133 bool Point2D_<T>::IsInside(const Rect& r) const{
134  return IsInside(r.a, r.Size());
135 }
136 
137 
138 typedef enum Direction{
143 } Direction;
144 typedef enum HorizAlignment{
149 typedef enum VertAlignment{
153 } VertAlignment;
154 
157 struct KeyData{
160  static void InitKeymap();
161 
162  enum KeyType{
169  };
170  KeyData(const SDL_KeyboardEvent&);
172  KeyData(std::string);
174  bool pressed = true;
176  bool repeat = false;
178  bool shift = false, ctrl = false, alt = false;
182  std::string symbol = "";
184  bool IsPrintable() const {return type == Letter || type == Digit || type == Symbol || type == Text;}
186  bool IsTrig() const {return pressed && !repeat;}
187 };
188 
190 enum class MouseButton{
191  Left,
192  Right,
193  Middle,
194  WheelUp,
195  WheelDown
196 };
197 
199 struct MidiMessage{
200  enum class Type{
201  NoteOn,
202  NoteOff,
203  Control,
204  };
206  unsigned char channel;
207  unsigned char number;
208  unsigned char velocity;
209  unsigned char value;
210 };
211 
214 class Utilities{
215 private:
216  Utilities() = delete; // static class
217 public:
219  static void Wait(int ms);
221  static void WaitOS(int ms);
223  static bool GetFileExists(std::string path);
225  static std::string GetDir(std::string);
227  static std::string GetFilename(std::string);
229  static std::string GetCurrentDir();
232  static const char OSDirSeparator;
235  static const std::string OSLibSuffix;
237  static const std::string OSSCLangBinName;
239  static std::string ConvertUnipathToOSPath(const std::string& unipath);
241  static std::string ConvertOSpathToUniPath(const std::string& ospath);
246  static std::string FindSCLang();
247 
249  static void CopyToClipboard(std::string);
250 
256  static Point2D Align(HorizAlignment, VertAlignment, Size2D inner, Size2D outer);
257 
258  // String operations
260  static std::vector<std::string> SplitString(std::string str, std::string delimiter);
262  static std::string JoinString(std::vector<std::string> str, std::string c);
264  static void Replace(std::string& str, const std::string& from, const std::string& to);
266  static std::string Trim(std::string);
268  static std::string TrimAllLines(std::string);
270  static std::string PrettyFloat(float val);
271 
272  // Other
274  static float mtof(float m);
275 
277  static void NumericLocaleSetUniversal();
279  static void NumericLocaleRestoreUserCustom();
284  };
285 };
286 
287 } // namespace AlgAudio
288 
289 #endif //UTILITIES_HPP
Definition: Utilities.hpp:166
Point2D_< T > & operator-=(const Point2D_< T > &other)
Definition: Utilities.hpp:74
Rect MoveOffset(Point2D p) const
Definition: Utilities.hpp:123
KeyData(const SDL_KeyboardEvent &)
unsigned char velocity
Definition: Utilities.hpp:208
static std::string ConvertOSpathToUniPath(const std::string &ospath)
VertAlignment
Definition: Utilities.hpp:149
Definition: Utilities.hpp:145
Definition: Utilities.hpp:214
static std::vector< std::string > SplitString(std::string str, std::string delimiter)
static std::string GetCurrentDir()
Definition: Utilities.hpp:139
static void CopyToClipboard(std::string)
Definition: Utilities.hpp:104
static const char OSDirSeparator
Definition: Utilities.hpp:232
Definition: Utilities.hpp:281
bool ctrl
Definition: Utilities.hpp:178
static void WaitOS(int ms)
std::string symbol
Definition: Utilities.hpp:182
LocaleDecPoint()
Definition: Utilities.hpp:282
static std::string GetDir(std::string)
Point2D_< int > Point2D
Definition: Utilities.hpp:35
Size2D operator+(const Size2D &other) const
Definition: Utilities.hpp:50
std::string ToString() const
Definition: Utilities.hpp:49
~LocaleDecPoint()
Definition: Utilities.hpp:283
static std::string FindSCLang()
int height
Definition: Utilities.hpp:46
unsigned char channel
Definition: Utilities.hpp:206
bool operator==(const Size2D &other) const
Definition: Utilities.hpp:47
Point2D_< T > operator-() const
Definition: Utilities.hpp:68
Definition: Utilities.hpp:150
MouseButton
Definition: Utilities.hpp:190
Definition: Utilities.hpp:164
Definition: Utilities.hpp:147
Rect(Point2D a_, Point2D b_)
Definition: Utilities.hpp:110
Rect(Point2D a_, Size2D s)
Definition: Utilities.hpp:116
bool operator==(const Point2D_< T > &other) const
Definition: Utilities.hpp:63
static void NumericLocaleSetUniversal()
Point2D_< T > operator/(const T &t) const
Definition: Utilities.hpp:69
Definition: Utilities.hpp:165
static const std::string OSLibSuffix
Definition: Utilities.hpp:235
static const std::string OSSCLangBinName
Definition: Utilities.hpp:237
unsigned char number
Definition: Utilities.hpp:207
static std::string Trim(std::string)
static void Wait(int ms)
static std::string PrettyFloat(float val)
bool Fits(const Size2D &other) const
Definition: Utilities.hpp:43
bool shift
Definition: Utilities.hpp:178
Rect()
Definition: Utilities.hpp:107
Type
Definition: Utilities.hpp:200
Direction
Definition: Utilities.hpp:138
bool operator!=(const Point2D_< T > &other) const
Definition: Utilities.hpp:64
Point2D_(const Point2D_< Q > &other)
Definition: Utilities.hpp:61
Point2D_< T > operator-(const Size2D &other) const
Definition: Utilities.hpp:72
Definition: Utilities.hpp:34
static std::string JoinString(std::vector< std::string > str, std::string c)
int width
Definition: Utilities.hpp:46
Size2D(int w=0, int h=0)
Definition: Utilities.hpp:41
static void Replace(std::string &str, const std::string &from, const std::string &to)
Definition: Utilities.hpp:168
static void InitKeymap()
T x
Definition: Utilities.hpp:62
Size2D operator/(const int &i) const
Definition: Utilities.hpp:52
unsigned char value
Definition: Utilities.hpp:209
Size2D operator-(const Size2D &other) const
Definition: Utilities.hpp:51
static float mtof(float m)
Definition: Utilities.hpp:165
Definition: Alertable.hpp:26
bool IsInside(Point2D_< T > r, Size2D s) const
Definition: Utilities.hpp:76
Definition: Utilities.hpp:164
bool IsFullyInside(const Rect &other)
Definition: Utilities.hpp:127
KeyType type
Definition: Utilities.hpp:180
bool pressed
Definition: Utilities.hpp:174
Point2D_< float > operator/(const Point2D_< int > &a, const float &t)
Definition: Utilities.hpp:99
bool IsPrintable() const
Definition: Utilities.hpp:184
Point2D_< T > operator+(const Point2D_< T > &other) const
Definition: Utilities.hpp:66
static Point2D Align(HorizAlignment, VertAlignment, Size2D inner, Size2D outer)
Definition: Utilities.hpp:151
Definition: Utilities.hpp:167
Point2D_< T > operator*(const T &t, const Point2D_< T > &a)
Definition: Utilities.hpp:85
Definition: Utilities.hpp:142
static float Distance(Point2D_< T > a, Point2D_< T > b)
Definition: Utilities.hpp:80
Definition: Utilities.hpp:164
static bool GetFileExists(std::string path)
Point2D_< T > & operator+=(const Point2D_< T > &other)
Definition: Utilities.hpp:73
Point2D a
Definition: Utilities.hpp:105
std::string ToString() const
Definition: Utilities.hpp:65
Point2D Center() const
Definition: Utilities.hpp:125
Size2D Size() const
Definition: Utilities.hpp:118
static std::string TrimAllLines(std::string)
Definition: Utilities.hpp:140
Definition: Utilities.hpp:163
Point2D_< T > operator-(const Point2D_< T > &other) const
Definition: Utilities.hpp:67
T y
Definition: Utilities.hpp:62
static std::string ConvertUnipathToOSPath(const std::string &unipath)
Definition: Utilities.hpp:141
Point2D ToPoint() const
Definition: Utilities.hpp:82
Definition: Utilities.hpp:146
bool IsTrig() const
Definition: Utilities.hpp:186
bool repeat
Definition: Utilities.hpp:176
Definition: Utilities.hpp:167
Definition: Utilities.hpp:199
Point2D_< T > operator*(const T &t) const
Definition: Utilities.hpp:70
Point2D_(T x_=0, T y_=0)
Definition: Utilities.hpp:59
HorizAlignment
Definition: Utilities.hpp:144
bool operator!=(const Size2D &other) const
Definition: Utilities.hpp:48
static std::string GetFilename(std::string)
KeyType
Definition: Utilities.hpp:162
Definition: Utilities.hpp:167
Definition: Utilities.hpp:152
bool IsEmpty() const
Definition: Utilities.hpp:45
bool alt
Definition: Utilities.hpp:178
Point2D_< T > operator+(const Size2D &other) const
Definition: Utilities.hpp:71
Definition: Utilities.hpp:157
static void NumericLocaleRestoreUserCustom()
Type type
Definition: Utilities.hpp:205
Point2D b
Definition: Utilities.hpp:106
Definition: Utilities.hpp:40
Definition: Utilities.hpp:164