soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SLayoutSize.cpp
1#include "souistd.h"
2#include "layout/SLayoutSize.h"
3#include <interface/slayout-i.h>
4#pragma warning(push)
5#pragma warning(disable : 4985) // disable the warning message during the include
6#include <math.h> // this is where I would normally get the warning message
7#pragma warning(pop)
8
9SNSBEGIN
10static const wchar_t *kUnitMap[] = {
11 L"px",
12 L"dp",
13 L"dip",
14 L"sp",
15};
16
18 : fSize(_fSize)
19 , unit(defUnit)
20{
21}
22
23SLayoutSize::SLayoutSize(float _fSize, Unit _unit)
24 : fSize(_fSize)
25 , unit(_unit)
26{
27}
28
29static int fround(float v)
30{
31 return (int)floor(v + 0.5f);
32}
33
34bool SLayoutSize::fequal(float a, float b)
35{
36 return fabs(a - b) < 0.00000001f;
37}
38
39bool SLayoutSize::valueEqual(float value)
40{
41 return fequal(fSize, value);
42}
43
45{
46 // copy from stringstream.
47 if (isWrapContent())
48 return L"-1";
49 else if (isMatchParent())
50 return L"-2";
51 return SStringW().Format(L"%.*g%s", 8, fSize, kUnitMap[unit]);
52}
53
55{
56 return fequal(fSize, SIZE_MATCH_PARENT);
57}
58
60{
61 fSize = SIZE_MATCH_PARENT;
62}
63
65{
66 return fequal(fSize, SIZE_WRAP_CONTENT);
67}
68
70{
71 fSize = SIZE_WRAP_CONTENT;
72}
73
74void SLayoutSize::setSize(float fSize, Unit unit)
75{
76 this->fSize = fSize;
77 this->unit = unit;
78}
79
81{
82 return fSize >= (float)(int)SIZE_SPEC;
83}
84
85int SLayoutSize::toPixelSize(int scale) const
86{
87 if (!isValid())
88 return 0;
89 if (isMatchParent())
90 return SIZE_MATCH_PARENT;
91 else if (isWrapContent())
92 return SIZE_WRAP_CONTENT;
93 else if (unit == px)
94 return (int)fSize;
95 else // if(unit == dp || unit == dip || unit= sp)
96 return (int)fround(fSize * scale / 100);
97}
98
100{
101 fSize = SIZE_UNDEF;
102}
103
105{
106 return !fequal(fSize, SIZE_UNDEF);
107}
108
110{
111 return fequal(fSize, 0.0f);
112}
113
115{
116 if (strSize.IsEmpty())
117 return;
118 int cUnitSize = 0;
119 for (int i = strSize.GetLength() - 1; i >= 0; i--)
120 {
121 wchar_t c = strSize[i];
122 if ((c >= 'a' && c < 'z') || (c >= 'A' && c <= 'z'))
123 {
124 cUnitSize++;
125 }
126 else
127 {
128 break;
129 }
130 }
131 if (cUnitSize > 0)
132 {
133 SStringW strUnit = strSize.Right(cUnitSize);
134 unit = unitFromString(strUnit);
135 if (unit == unknow)
136 unit = defUnit;
137 fSize = (float)_wtof(strSize.Left(strSize.GetLength() - cUnitSize));
138 }
139 else
140 {
141 unit = defUnit;
142 fSize = (float)_wtof(strSize);
143 }
144}
145
146//只复制数值,不复制方向
148{
149 fSize = src.fSize;
150 unit = src.unit;
151 return *this;
152}
153
155{
156 SLayoutSize ret;
157 ret.parseString(strSize);
158 return ret;
159}
160
162{
163 Unit ret = unknow;
164 SStringW strUnit2 = strUnit;
165 strUnit2.MakeLower();
166 for (int i = 0; i < ARRAYSIZE(kUnitMap); i++)
167 {
168 if (strUnit2.Compare(kUnitMap[i]) == 0)
169 {
170 ret = (Unit)i;
171 break;
172 }
173 }
174 return ret;
175}
176
177SLayoutSize::Unit SLayoutSize::defUnit = SLayoutSize::px;
178SNSEND
SLayoutSize & operator=(const SLayoutSize &src)
赋值运算符重载
bool valueEqual(float value)
比较大小值是否相等
void setMatchParent()
设置为匹配父容器大小
void setWrapContent()
设置为包裹内容大小
static bool fequal(float a, float b)
比较两个浮点数是否相等
void parseString(const SStringW &strSize)
从字符串解析大小
void setInvalid()
设置为无效大小
bool isWrapContent() const
检查是否为包裹内容大小
SLayoutSize(float fSize=0.0f)
默认构造函数
bool isMatchParent() const
检查是否为匹配父容器大小
static SLayoutSize::Unit unitFromString(const SStringW &strUnit)
从字符串解析单位
int toPixelSize(int scale) const
将大小转换为像素值
void setSize(float fSize, Unit unit)
设置指定大小
SStringW toString() const
将大小转换为字符串表示
bool isValid() const
检查是否为有效大小
static SLayoutSize fromString(const SStringW &strSize)
从字符串创建大小对象
Unit
布局大小单位枚举
Definition SLayoutSize.h:17
bool isSpecifiedSize() const
检查是否为指定大小
bool isZero() const
检查大小是否为零
A class representing an ASCII string.
Definition sstringw.h:96
BOOL IsEmpty() SCONST
Checks if the string is empty.
int Compare(const wchar_t *psz) SCONST
Compares the string with another string.
Definition sstringw.cpp:934
BOOL __cdecl Format(HINSTANCE hInst, UINT nFormatID,...)
Formats a string using a format string and variable arguments.
Definition sstringw.cpp:490
SStringW & MakeLower()
Converts the string to lowercase.
Definition sstringw.cpp:869
SStringW Right(int nCount) const
Extracts the rightmost part of the string.
Definition sstringw.cpp:892
int GetLength() SCONST
Retrieves the length of the string.
SStringW Left(int nCount) const
Extracts the leftmost part of the string.
Definition sstringw.cpp:879