-
Notifications
You must be signed in to change notification settings - Fork 3
/
overallstate.cpp
209 lines (198 loc) · 5.53 KB
/
overallstate.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "overallstate.h"
#include "countdownclock.h"
#include "globals.h"
#include <set>
bool OverallState::RotationReqd(VGfloat width, VGfloat height)
{
if(m_bLandscape)
return width < height;
else
return width > height;
}
bool OverallState::Landscape()
{
return m_bLandscape;
}
bool OverallState::ScreenSaver()
{
return m_bScreenSaver;
}
void OverallState::UpdateFromMessage(const std::shared_ptr<ClockMsg_SetGlobal> &pMsg)
{
UpdateFromMessage(*pMsg);
}
void OverallState::UpdateFromMessage(const ClockMsg_SetGlobal &message)
{
m_bLandscape = message.bLandscape;
m_bScreenSaver = message.bScreenSaver;
}
void OverallState::SetLandscape(bool bLandscape)
{
m_bLandscape = bLandscape;
}
bool OverallState::HandleClockMessages(std::queue<std::shared_ptr<ClockMsg> > &msgs, struct timeval & tvCur)
{
bool bSizeChanged = false;
while(!msgs.empty())
{
auto pMsg = msgs.front();
msgs.pop();
if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetGlobal>(pMsg))
{
UpdateFromMessage(castCmd);
continue;
}
int regionIndex = 0;
bool bMaxRegion = false;
#define UPDATE_CHECK(target, source) { auto tmp = (source); bSizeChanged = bSizeChanged || ((target) != tmp); (target) = tmp;}
if(auto regionCmd = std::dynamic_pointer_cast<ClockMsg_Region>(pMsg))
{
if(regionCmd->bHasRegionIndex)
regionIndex = regionCmd->regionIndex;
else
{
if(UpdateRegionCount(1))
{
bSizeChanged = true;
}
bMaxRegion = true;
}
}
if(!Regions[regionIndex])
{
Regions[regionIndex] = std::make_shared<RegionState>();
}
std::shared_ptr<RegionState> pRS = Regions[regionIndex];
if(bMaxRegion && (pRS->x() != 0.0f || pRS->y() != 1.0f || pRS->width() != 1.0f || pRS->height() != 1.0f))
{
bSizeChanged = true;
pRS->UpdateFromMessage(std::make_shared<ClockMsg_SetLocation>(std::make_shared<int>(regionIndex), "SETLOCATION:0:0:1:1"));
}
if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_ClearImages>(pMsg))
{
Images.clear();
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_StoreImage>(pMsg))
{
if(!Images[castCmd->name].IsSameSource(castCmd->pSourceBlob))
Images[castCmd->name] = ScalingImage(castCmd->pParsedImage, castCmd->pSourceBlob);
}
else if(auto castCmd = std::dynamic_pointer_cast<ResizedImage>(pMsg))
{
Images[castCmd->Name].UpdateFromResize(castCmd);
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetRegionCount>(pMsg))
{
if(UpdateRegionCount(castCmd->iCount))
{
bSizeChanged = true;
}
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetSize>(pMsg))
{
UPDATE_CHECK(pRS->TD.nRows, castCmd->iRows)
UPDATE_CHECK(pRS->TD.nCols_default, castCmd->iCols)
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetLocation>(pMsg))
{
if(pRS->UpdateFromMessage(castCmd))
{
bSizeChanged = true;
}
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetRow>(pMsg))
{
UPDATE_CHECK(pRS->TD.nCols[castCmd->iRow], castCmd->iCols);
}
else if(auto indCmd = std::dynamic_pointer_cast<ClockMsg_SetIndicator>(pMsg))
{
std::shared_ptr<TallyState> pNewState;
int row = indCmd->iRow;
int col = indCmd->iCol;
auto pOldState = pRS->TD.displays[row][col];
if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetCountdown>(pMsg))
{
pNewState = std::make_shared<CountdownClock>(castCmd);
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetTally>(pMsg))
{
pNewState = std::make_shared<SimpleTallyState>(castCmd, pOldState);
}
if(!pNewState || !pOldState)
bSizeChanged = true;
else if(!bSizeChanged)
{
auto textOld = pOldState->Text(tvCur);
auto textNew = pNewState->Text(tvCur);
bool bMonoOld = pOldState->IsMonoSpaced();
bool bMonoNew = pNewState->IsMonoSpaced();
if(bMonoOld != bMonoNew)
bSizeChanged = true;
else if((bool)textOld != (bool)textNew)
bSizeChanged = true;
else if(textOld && textNew)
{
if(bMonoNew)
{
if(textOld->size() != textNew->size())
bSizeChanged = true;
}
else if(*textOld != *textNew)
bSizeChanged = true;
}
}
pRS->TD.displays[row][col] = pNewState;
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetLabel>(pMsg))
{
int row = castCmd->iRow;
int col = castCmd->iCol;
if(pRS->TD.displays[row][col])
{
auto pNew = pRS->TD.displays[row][col]->SetLabel(castCmd->sText);
//Returns an empty pointer if the label is the same (ie no change required)
if(pNew)
{
bSizeChanged = true;
pRS->TD.displays[row][col] = pNew;
}
}
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetProfile>(pMsg))
{
//No size calculation depends upon the setting of this string, so just store it.
pRS->TD.sProfName = castCmd->sProfile;
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetLayout>(pMsg))
{
pRS->UpdateFromMessage(castCmd);
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetFontSizeZones>(pMsg))
{
bSizeChanged |= pRS->UpdateFromMessage(castCmd);
}
else
{
fprintf(stderr, "IMPOSSIBLE HAPPENED!! Received unknown command in message queue: %s\n",typeid(pMsg.get()).name());
}
}
return bSizeChanged;
}
bool OverallState::UpdateRegionCount(int newCount)
{
std::set<int> removeIndices;
for(const auto & item: Regions)
{
if(item.first >= newCount || item.first < 0)
removeIndices.insert(item.first);
}
if(!removeIndices.empty())
{
for(int index: removeIndices)
{
Regions.erase(index);
}
return true;
}
return false;
}