Skip to content

Commit d6ab49c

Browse files
committed
Store viewer runtime info in the layout instead of the node.
1 parent e30d767 commit d6ab49c

4 files changed

Lines changed: 49 additions & 138 deletions

File tree

Sources/BuiltInNodes/BI_UINodeLayouts.cpp

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,6 @@ NUIE::EventHandlerResult HeaderWithSlotsAndSwitchLayout::HandleMouseClick ( Basi
187187
return NUIE::EventHandlerResult::EventNotHandled;
188188
}
189189

190-
HeaderWithSlotsAndMultilineTextLayout::ClickHandler::ClickHandler ()
191-
{
192-
193-
}
194-
195-
HeaderWithSlotsAndMultilineTextLayout::ClickHandler::~ClickHandler ()
196-
{
197-
198-
}
199-
200190
HeaderWithSlotsAndMultilineTextLayout::HeaderWithSlotsAndMultilineTextLayout ( const std::string& leftButtonId,
201191
const std::wstring& leftButtonText,
202192
const std::string& rightButtonId,
@@ -206,8 +196,8 @@ HeaderWithSlotsAndMultilineTextLayout::HeaderWithSlotsAndMultilineTextLayout ( c
206196
leftButtonText (leftButtonText),
207197
rightButtonId (rightButtonId),
208198
rightButtonText (rightButtonText),
209-
storedPageCount (0),
210-
storedCurrentPage (0)
199+
pageCount (0),
200+
currentPage (1)
211201
{
212202
}
213203

@@ -216,30 +206,48 @@ void HeaderWithSlotsAndMultilineTextLayout::AddPanels ( const BasicUINode& uiNod
216206
NUIE::NodePanelDrawer& drawer) const
217207
{
218208
std::vector<std::wstring> texts;
219-
size_t allTextCount = 0;
220209
size_t textsPerPage = 0;
221-
size_t pageCount = 0;
222-
size_t currentPage = 0;
210+
GetTextInfo (uiNode, env.GetStringConverter (), texts, textsPerPage);
211+
212+
size_t textCount = texts.size ();
213+
pageCount = textCount / textsPerPage;
214+
if (textCount % textsPerPage != 0) {
215+
pageCount = pageCount + 1;
216+
}
217+
if (currentPage > pageCount) {
218+
currentPage = pageCount;
219+
}
223220

224-
GetTextInfo (uiNode, env.GetStringConverter (), texts, allTextCount, textsPerPage, pageCount, currentPage);
221+
std::vector<std::wstring> visibleTexts;
222+
if (!texts.empty ()) {
223+
textCount = texts.size ();
224+
for (size_t i = 0; i < textsPerPage; ++i) {
225+
size_t textIndex = (currentPage - 1) * textsPerPage + i;
226+
if (textIndex < texts.size ()) {
227+
visibleTexts.push_back (texts[textIndex]);
228+
}
229+
}
230+
} else {
231+
textCount = 1;
232+
pageCount = 1;
233+
currentPage = 1;
234+
visibleTexts.push_back (NE::LocalizeString (L"<empty>"));
235+
}
225236

226237
HeaderBasedLayout::AddPanels (uiNode, env, drawer);
227238
drawer.AddPanel (NUIE::NodeUIPanelPtr (new NodeUISlotPanel (uiNode, env)));
228-
drawer.AddPanel (NUIE::NodeUIPanelPtr (new NodeUIMultiLineTextPanel (texts, allTextCount, textsPerPage, env)));
229-
if (allTextCount > textsPerPage) {
230-
drawer.AddPanel (NUIE::NodeUIPanelPtr (new NodeUILeftRightButtonsPanel (leftButtonId, leftButtonText, rightButtonId, rightButtonText, std::to_wstring (currentPage) + L" / " + std::to_wstring (pageCount) + L" (" + std::to_wstring (allTextCount) + L")", env)));
239+
drawer.AddPanel (NUIE::NodeUIPanelPtr (new NodeUIMultiLineTextPanel (visibleTexts, textCount, textsPerPage, env)));
240+
if (textCount > textsPerPage) {
241+
drawer.AddPanel (NUIE::NodeUIPanelPtr (new NodeUILeftRightButtonsPanel (leftButtonId, leftButtonText, rightButtonId, rightButtonText, std::to_wstring (currentPage) + L" / " + std::to_wstring (pageCount) + L" (" + std::to_wstring (textCount) + L")", env)));
231242
}
232-
233-
storedCurrentPage = currentPage;
234-
storedPageCount = pageCount;
235243
}
236244

237245
NUIE::EventHandlerResult HeaderWithSlotsAndMultilineTextLayout::HandleMouseClick ( BasicUINode& uiNode,
238246
NUIE::NodeUIEnvironment& env,
239247
const NUIE::ModifierKeys&,
240248
NUIE::MouseButton mouseButton,
241249
const NUIE::Point& position,
242-
NUIE::UINodeCommandInterface& commandInterface) const
250+
NUIE::UINodeCommandInterface&) const
243251
{
244252
if (mouseButton != NUIE::MouseButton::Left) {
245253
return NUIE::EventHandlerResult::EventNotHandled;
@@ -249,27 +257,20 @@ NUIE::EventHandlerResult HeaderWithSlotsAndMultilineTextLayout::HandleMouseClick
249257
return NUIE::EventHandlerResult::EventNotHandled;
250258
}
251259

252-
std::shared_ptr<ClickHandler> clickHandler = GetClickHandler (uiNode);
253260
NUIE::Rect minusButtonRect = uiNode.GetSpecialRect (env, leftButtonId);
254261
NUIE::Rect plusButtonRect = uiNode.GetSpecialRect (env, rightButtonId);
255262

256263
if (minusButtonRect.Contains (position)) {
257-
commandInterface.RunUndoableCommand ([&] () {
258-
size_t newCurrentPage = storedCurrentPage - 1;
259-
if (newCurrentPage == 0) {
260-
newCurrentPage = storedPageCount;
261-
}
262-
clickHandler->SetCurrentPage (newCurrentPage);
263-
});
264+
currentPage = currentPage - 1;
265+
if (currentPage == 0) {
266+
currentPage = pageCount;
267+
}
264268
return NUIE::EventHandlerResult::EventHandled;
265269
} else if (plusButtonRect.Contains (position)) {
266-
commandInterface.RunUndoableCommand ([&] () {
267-
size_t newCurrentPage = storedCurrentPage + 1;
268-
if (newCurrentPage > storedPageCount) {
269-
newCurrentPage = 1;
270-
}
271-
clickHandler->SetCurrentPage (newCurrentPage);
272-
});
270+
currentPage = currentPage + 1;
271+
if (currentPage > pageCount) {
272+
currentPage = 1;
273+
}
273274
return NUIE::EventHandlerResult::EventHandled;
274275
}
275276
return NUIE::EventHandlerResult::EventNotHandled;

Sources/BuiltInNodes/BI_UINodeLayouts.hpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,6 @@ class HeaderWithSlotsAndSwitchLayout : public HeaderBasedLayout
141141
class HeaderWithSlotsAndMultilineTextLayout : public HeaderBasedLayout
142142
{
143143
public:
144-
class ClickHandler
145-
{
146-
public:
147-
ClickHandler ();
148-
virtual ~ClickHandler ();
149-
150-
virtual void SetCurrentPage (size_t currentPage) = 0;
151-
};
152-
153144
HeaderWithSlotsAndMultilineTextLayout ( const std::string& leftButtonId,
154145
const std::wstring& leftButtonText,
155146
const std::string& rightButtonId,
@@ -170,21 +161,17 @@ class HeaderWithSlotsAndMultilineTextLayout : public HeaderBasedLayout
170161
virtual void GetTextInfo ( const BasicUINode& uiNode,
171162
const NE::StringConverter& stringConverter,
172163
std::vector<std::wstring>& texts,
173-
size_t& textCount,
174-
size_t& textsPerPage,
175-
size_t& pageCount,
176-
size_t& currentPage) const = 0;
177-
178-
virtual std::shared_ptr<ClickHandler> GetClickHandler (BasicUINode& uiNode) const = 0;
164+
size_t& textsPerPage) const = 0;
179165

180166
private:
181167
std::string leftButtonId;
182168
std::wstring leftButtonText;
183169
std::string rightButtonId;
184170
std::wstring rightButtonText;
185171

186-
mutable size_t storedPageCount;
187-
mutable size_t storedCurrentPage;
172+
protected:
173+
mutable size_t pageCount;
174+
mutable size_t currentPage;
188175
};
189176

190177
}

Sources/BuiltInNodes/BI_ViewerUINodes.cpp

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -92,74 +92,23 @@ MultiLineViewerNode::Layout::Layout ( const std::string& leftButtonId,
9292
void MultiLineViewerNode::Layout::GetTextInfo (const BasicUINode& uiNode,
9393
const NE::StringConverter& stringConverter,
9494
std::vector<std::wstring>& texts,
95-
size_t& textCount,
96-
size_t& textsPerPage,
97-
size_t& pageCount,
98-
size_t& currentPage) const
95+
size_t& textsPerPage) const
9996
{
100-
std::vector<std::wstring> nodeTexts;
10197
if (uiNode.HasCalculatedValue ()) {
10298
NE::ValueConstPtr nodeValue = uiNode.GetCalculatedValue ();
10399
if (nodeValue != nullptr) {
104100
NE::FlatEnumerate (nodeValue, [&] (const NE::ValueConstPtr& value) {
105101
if (value != nullptr) {
106-
nodeTexts.push_back (value->ToString (stringConverter));
102+
texts.push_back (value->ToString (stringConverter));
107103
} else {
108-
nodeTexts.push_back (NE::LocalizeString (L"<empty>"));
104+
texts.push_back (NE::LocalizeString (L"<empty>"));
109105
}
110106
});
111107
}
112108
}
113109

114110
const MultiLineViewerNode* viewerNode = dynamic_cast<const MultiLineViewerNode*> (&uiNode);
115111
textsPerPage = viewerNode->GetTextsPerPage ();
116-
currentPage = viewerNode->GetCurrentPage ();
117-
118-
if (!nodeTexts.empty ()) {
119-
textCount = nodeTexts.size ();
120-
pageCount = textCount / textsPerPage;
121-
if (textCount % textsPerPage != 0) {
122-
pageCount = pageCount + 1;
123-
}
124-
if (currentPage > pageCount) {
125-
currentPage = pageCount;
126-
}
127-
for (size_t i = 0; i < textsPerPage; ++i) {
128-
size_t textIndex = (currentPage - 1) * textsPerPage + i;
129-
if (textIndex < nodeTexts.size ()) {
130-
texts.push_back (nodeTexts[textIndex]);
131-
}
132-
}
133-
} else {
134-
textCount = 1;
135-
pageCount = 1;
136-
currentPage = 1;
137-
texts.push_back (NE::LocalizeString (L"<empty>"));
138-
}
139-
140-
viewerNode->ValidateCurrentPage (currentPage);
141-
}
142-
143-
std::shared_ptr<HeaderWithSlotsAndMultilineTextLayout::ClickHandler> MultiLineViewerNode::Layout::GetClickHandler (BasicUINode& uiNode) const
144-
{
145-
class ClickHandler : public HeaderWithSlotsAndMultilineTextLayout::ClickHandler
146-
{
147-
public:
148-
ClickHandler (MultiLineViewerNode* node) :
149-
node (node)
150-
{
151-
}
152-
153-
virtual void SetCurrentPage (size_t currentPage) override
154-
{
155-
node->SetCurrentPage (currentPage);
156-
}
157-
158-
private:
159-
MultiLineViewerNode* node;
160-
};
161-
162-
return std::shared_ptr<HeaderWithSlotsAndMultilineTextLayout::ClickHandler> (new ClickHandler (dynamic_cast<MultiLineViewerNode*> (&uiNode)));
163112
}
164113

165114
MultiLineViewerNode::MultiLineViewerNode () :
@@ -170,8 +119,7 @@ MultiLineViewerNode::MultiLineViewerNode () :
170119

171120
MultiLineViewerNode::MultiLineViewerNode (const NE::String& name, const NUIE::Point& position, size_t textsPerPage) :
172121
BasicUINode (name, position, NUIE::InvalidIconId, UINodeLayoutPtr (new Layout ("minus", NE::LocalizeString (L"<"), "plus", NE::LocalizeString (L">")))),
173-
textsPerPage (textsPerPage),
174-
currentPage (1)
122+
textsPerPage (textsPerPage)
175123
{
176124

177125
}
@@ -251,19 +199,4 @@ void MultiLineViewerNode::SetTextsPerPage (size_t newTextsPerPage)
251199
textsPerPage = newTextsPerPage;
252200
}
253201

254-
size_t MultiLineViewerNode::GetCurrentPage () const
255-
{
256-
return currentPage;
257-
}
258-
259-
void MultiLineViewerNode::SetCurrentPage (size_t newCurrentPage)
260-
{
261-
currentPage = newCurrentPage;
262-
}
263-
264-
void MultiLineViewerNode::ValidateCurrentPage (size_t correctCurrentPage) const
265-
{
266-
currentPage = correctCurrentPage;
267-
}
268-
269202
}

Sources/BuiltInNodes/BI_ViewerUINodes.hpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ class MultiLineViewerNode : public BasicUINode
5151
virtual void GetTextInfo ( const BasicUINode& uiNode,
5252
const NE::StringConverter& stringConverter,
5353
std::vector<std::wstring>& texts,
54-
size_t& textCount,
55-
size_t& textsPerPage,
56-
size_t& pageCount,
57-
size_t& currentPage) const override;
58-
59-
virtual std::shared_ptr<HeaderWithSlotsAndMultilineTextLayout::ClickHandler> GetClickHandler (BasicUINode& uiNode) const override;
54+
size_t& textsPerPage) const override;
6055
};
6156

6257
MultiLineViewerNode ();
@@ -75,13 +70,8 @@ class MultiLineViewerNode : public BasicUINode
7570
size_t GetTextsPerPage () const;
7671
void SetTextsPerPage (size_t newTextsPerPage);
7772

78-
size_t GetCurrentPage () const;
79-
void SetCurrentPage (size_t newCurrentPage);
80-
void ValidateCurrentPage (size_t correctCurrentPage) const;
81-
8273
private:
83-
size_t textsPerPage;
84-
mutable size_t currentPage;
74+
size_t textsPerPage;
8575
};
8676

8777
}

0 commit comments

Comments
 (0)