Gorgon Game Engine
Dimension.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Geometry/Margin.h"
4 #include "../Geometry/Size.h"
5 #include "../Geometry/Bounds.h"
6 
7 namespace Gorgon { namespace UI {
9  class Dimension {
10  public:
11 
16  enum Unit {
19 
23 
27 
30 
35  EM,
36 
37  //todo add line height
38  };
39 
41  Dimension(int value = 0, Unit unit = Pixel) : value(value), unit(unit) {/* implicit */
42  }
43 
45  int operator ()(int parentwidth, int emwidth = 10) const {
46  return Calculate(parentwidth, emwidth);
47  }
48 
50  int Calculate(int parentwidth, int emwidth = 10) const {
51  switch(unit) {
52  case Percent:
53  return int(std::round((double)value * parentwidth / 100));
54  case MilliPixel:
55  return int(std::round((double)value / 1000));
56  case BasisPoint:
57  return int(std::round((double)value * parentwidth / 10000));
58  case EM:
59  return int(std::round(value * emwidth / 100));
60  case Pixel:
61  default:
62  return value;
63  }
64  }
65 
67  float CalculateFloat(float parentwidth, float emwidth = 10) const {
68  switch(unit) {
69  case Percent:
70  return (float)value * parentwidth / 100.f;
71  case BasisPoint:
72  return (float)value * parentwidth / 10000.f;
73  case MilliPixel:
74  return (float)value / 1000;
75  case EM:
76  return (float)value * emwidth / 100.f;
77  case Pixel:
78  default:
79  return (float)value;
80  }
81  }
82 
84  bool IsRelative() const {
85  return unit == Percent || unit == BasisPoint;
86  }
87 
90  int GetValue() const {
91  return value;
92  }
93 
95  Unit GetUnit() const {
96  return unit;
97  }
98 
100  void Set(int value) {
101  this->value = value;
102  }
103 
105  void Set(int value, Unit unit) {
106  this->value = value;
107  this->unit = unit;
108  }
109 
110  private:
111  int value;
112  Unit unit;
113  };
114 
117 
120 
123 
125  inline Geometry::Point Convert(const Point &p, const Geometry::Size &parent, int emwidth = 10) {
126  return {p.X(parent.Width, emwidth), p.Y(parent.Height, emwidth)};
127  }
128 
130  inline Geometry::Size Convert(const Size &s, const Geometry::Size &parent, int emwidth = 10) {
131  return {s.Width(parent.Width, emwidth), s.Height(parent.Height, emwidth)};
132  }
133 
135  inline Geometry::Margin Convert(const Margin &m, const Geometry::Size &parent, int emwidth = 10) {
136  return {m.Left(parent.Width, emwidth), m.Top(parent.Height, emwidth), m.Right(parent.Width, emwidth), m.Bottom(parent.Height, emwidth), };
137  }
138 
139 } }
Gorgon::UI::Dimension
Dimension data for components. Allows relative position and sizing.
Definition: Dimension.h:9
Gorgon::UI::Dimension::GetUnit
Unit GetUnit() const
Returns the unit of the dimension.
Definition: Dimension.h:95
Gorgon::UI::Dimension::operator()
int operator()(int parentwidth, int emwidth=10) const
Returns the calculated dimension in pixels.
Definition: Dimension.h:45
Gorgon::UI::Dimension::Set
void Set(int value)
Changes the value of the dimension without modifying the units.
Definition: Dimension.h:100
Gorgon::Geometry::basic_Margin::Left
T_ Left
Left margin.
Definition: Margin.h:167
Gorgon::UI::Convert
Geometry::Point Convert(const Point &p, const Geometry::Size &parent, int emwidth=10)
Converts a dimension based point to pixel based point.
Definition: Dimension.h:125
Gorgon::Geometry::basic_Point::X
T_ X
X coordinate.
Definition: Point.h:368
Gorgon::Geometry::basic_Size::Height
T_ Height
Height of this size object.
Definition: Size.h:261
Gorgon::UI::Dimension::Pixel
@ Pixel
Fixed pixel based dimensions.
Definition: Dimension.h:18
Gorgon::UI::Dimension::Set
void Set(int value, Unit unit)
Changes the value and unit of the dimension.
Definition: Dimension.h:105
Gorgon::Geometry::basic_Margin::Top
T_ Top
Top margin.
Definition: Margin.h:170
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size< Dimension >
Gorgon::UI::Dimension::GetValue
int GetValue() const
Returns the value of the dimension, should not be considered as pixels.
Definition: Dimension.h:90
Gorgon::UI::Dimension::IsRelative
bool IsRelative() const
Returns if the dimension is relative to the parentwidth.
Definition: Dimension.h:84
Gorgon::UI::Dimension::Calculate
int Calculate(int parentwidth, int emwidth=10) const
Returns the calculated dimension in pixels.
Definition: Dimension.h:50
Gorgon::Geometry::basic_Margin::Bottom
T_ Bottom
Bottom margin.
Definition: Margin.h:176
Gorgon::Geometry::basic_Point< Dimension >
Gorgon::UI::Dimension::BasisPoint
@ BasisPoint
Dimension will be relative to the parent and given in 1/10000.
Definition: Dimension.h:29
Gorgon::UI::Dimension::CalculateFloat
float CalculateFloat(float parentwidth, float emwidth=10) const
Returns the calculated dimension in pixels.
Definition: Dimension.h:67
Gorgon::Geometry::basic_Margin::Right
T_ Right
Right margin.
Definition: Margin.h:173
Gorgon::Geometry::basic_Point::Y
T_ Y
Y coordinate.
Definition: Point.h:371
Gorgon::Geometry::basic_Size::Width
T_ Width
Width of this size object.
Definition: Size.h:258
Gorgon::UI::Dimension::EM
@ EM
Dimension will be relative to the text size, given value is the percent of the width of an EM dash.
Definition: Dimension.h:35
Gorgon::Geometry::basic_Margin< Dimension >
Gorgon::UI::Dimension::Dimension
Dimension(int value=0, Unit unit=Pixel)
Constructs a new dimension or type casts integer to dimension
Definition: Dimension.h:41
Gorgon::UI::Dimension::MilliPixel
@ MilliPixel
1/1000th of a pixel, there are only few places that this will be used.
Definition: Dimension.h:26
Gorgon::UI::Dimension::Unit
Unit
Unit for dimensions.
Definition: Dimension.h:16
Gorgon::UI::Dimension::Percent
@ Percent
Dimension will be relative to the parent and given in percent.
Definition: Dimension.h:22