forked from grijjy/MvvmStarterKit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGrijjy.Mvvm.ValueConverters.pas
189 lines (157 loc) · 5.58 KB
/
Grijjy.Mvvm.ValueConverters.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
unit Grijjy.Mvvm.ValueConverters;
{ Some standard value converters. }
{$INCLUDE 'Grijjy.inc'}
interface
uses
Grijjy.Mvvm.Types,
Grijjy.Mvvm.Rtti;
type
{ A value converter that subtracts 1 from the source value.
When run in reverse (target-to-source), it adds 1 to the target value. }
TgoMinusOneConverter = class(TgoValueConverter)
public
class function ConvertSourceToTarget(const ASource: TgoValue): TgoValue; override;
class function ConvertTargetToSource(const ATarget: TgoValue): TgoValue; override;
end;
type
{ A value converter that converts a TColor to a (fully opaque) TAlphaColor.
When run in reverse the Alpha value is ignored. }
TgoColorToAlphaColor = class(TgoValueConverter)
public
class function ConvertSourceToTarget(const ASource: TgoValue): TgoValue; override;
class function ConvertTargetToSource(const ATarget: TgoValue): TgoValue; override;
end;
type
{ A value converter that converts a TAlphaColor to a TColor (discarding the
Alpha part). When run in reverse the Alpha value is set to opaque. }
TgoAlphaColorToColor = class(TgoValueConverter)
public
class function ConvertSourceToTarget(const ASource: TgoValue): TgoValue; override;
class function ConvertTargetToSource(const ATarget: TgoValue): TgoValue; override;
end;
type
{ A value converter that converts a TAlphaColor to a TColor for use with the
VCL (ignoring the Alpha part). When run in reverse the Alpha value is set
to opaque. VCL colors have the Red and Blue values swapped. }
TgoAlphaColorToVclColor = class(TgoValueConverter)
public
class function ConvertSourceToTarget(const ASource: TgoValue): TgoValue; override;
class function ConvertTargetToSource(const ATarget: TgoValue): TgoValue; override;
end;
type
{ A value converter that converts a TColor for use with the VCL to a
(fully opaque) TAlphaColor (discarding the Alpha part). When run in reverse
the Alpha value is ignored. VCL colors have the Red and Blue values swapped. }
TgoVclColorToAlphaColor = class(TgoValueConverter)
public
class function ConvertSourceToTarget(const ASource: TgoValue): TgoValue; override;
class function ConvertTargetToSource(const ATarget: TgoValue): TgoValue; override;
end;
implementation
uses
System.SysConst,
System.SysUtils;
function goSwapRB(const AValue: UInt32): Integer;
begin
Result := ((AValue and $000000FF) shl 16)
or ((AValue and $00FF0000) shr 16)
or (AValue and $FF00FF00);
end;
{ TgoMinusOneConverter }
class function TgoMinusOneConverter.ConvertSourceToTarget(
const ASource: TgoValue): TgoValue;
begin
case ASource.ValueType of
TgoValueType.Ordinal:
Result := TgoValue.CreateOrdinal(ASource.AsOrdinal - 1);
TgoValueType.Int64:
Result := TgoValue.CreateInt64(ASource.AsInt64 - 1);
TgoValueType.Float:
Result := TgoValue.CreateFloat(ASource.AsDouble - 1);
else
Result := ASource;
end;
end;
class function TgoMinusOneConverter.ConvertTargetToSource(
const ATarget: TgoValue): TgoValue;
begin
case ATarget.ValueType of
TgoValueType.Ordinal:
Result := TgoValue.CreateOrdinal(ATarget.AsOrdinal + 1);
TgoValueType.Int64:
Result := TgoValue.CreateInt64(ATarget.AsInt64 + 1);
TgoValueType.Float:
Result := TgoValue.CreateFloat(ATarget.AsDouble + 1);
else
Result := ATarget;
end;
end;
{ TgoColorToAlphaColor }
class function TgoColorToAlphaColor.ConvertSourceToTarget(
const ASource: TgoValue): TgoValue;
begin
if (ASource.ValueType = TgoValueType.Ordinal) then
Result := TgoValue.CreateOrdinal(ASource.AsOrdinal or NativeInt($FF000000))
else
Result := ASource;
end;
class function TgoColorToAlphaColor.ConvertTargetToSource(
const ATarget: TgoValue): TgoValue;
begin
if (ATarget.ValueType = TgoValueType.Ordinal) then
Result := TgoValue.CreateOrdinal(ATarget.AsOrdinal and $00FFFFFF)
else
Result := ATarget;
end;
{ TgoAlphaColorToColor }
class function TgoAlphaColorToColor.ConvertSourceToTarget(
const ASource: TgoValue): TgoValue;
begin
if (ASource.ValueType = TgoValueType.Ordinal) then
Result := TgoValue.CreateOrdinal(ASource.AsOrdinal and $00FFFFFF)
else
Result := ASource;
end;
class function TgoAlphaColorToColor.ConvertTargetToSource(
const ATarget: TgoValue): TgoValue;
begin
if (ATarget.ValueType = TgoValueType.Ordinal) then
Result := TgoValue.CreateOrdinal(ATarget.AsOrdinal or NativeInt($FF000000))
else
Result := ATarget;
end;
{ TgoAlphaColorToVclColor }
class function TgoAlphaColorToVclColor.ConvertSourceToTarget(
const ASource: TgoValue): TgoValue;
begin
if (ASource.ValueType = TgoValueType.Ordinal) then
Result := TgoValue.CreateOrdinal(goSwapRB(ASource.AsOrdinal) and $00FFFFFF)
else
Result := ASource;
end;
class function TgoAlphaColorToVclColor.ConvertTargetToSource(
const ATarget: TgoValue): TgoValue;
begin
if (ATarget.ValueType = TgoValueType.Ordinal) then
Result := TgoValue.CreateOrdinal(goSwapRB(ATarget.AsOrdinal) or NativeInt($FF000000))
else
Result := ATarget;
end;
{ TgoVclColorToAlphaColor }
class function TgoVclColorToAlphaColor.ConvertSourceToTarget(
const ASource: TgoValue): TgoValue;
begin
if (ASource.ValueType = TgoValueType.Ordinal) then
Result := TgoValue.CreateOrdinal(goSwapRB(ASource.AsOrdinal) or NativeInt($FF000000))
else
Result := ASource;
end;
class function TgoVclColorToAlphaColor.ConvertTargetToSource(
const ATarget: TgoValue): TgoValue;
begin
if (ATarget.ValueType = TgoValueType.Ordinal) then
Result := TgoValue.CreateOrdinal(goSwapRB(ATarget.AsOrdinal) and $00FFFFFF)
else
Result := ATarget;
end;
end.