forked from danilw/nanogui-GLES-wasm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stackedwidget.cpp
57 lines (45 loc) · 1.58 KB
/
stackedwidget.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
/*
nanogui/stackedwidget.cpp -- Widget used to stack widgets on top
of each other. Only the active widget is visible.
The stacked widget was contributed by Stefan Ivanov.
NanoGUI was developed by Wenzel Jakob <[email protected]>.
The widget drawing code is based on the NanoVG demo application
by Mikko Mononen.
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE.txt file.
*/
#include <nanogui/stackedwidget.h>
NAMESPACE_BEGIN(nanogui)
StackedWidget::StackedWidget(nanogui::Widget *parent)
: Widget(parent) { }
void StackedWidget::setSelectedIndex(int index) {
assert(index < childCount());
if (mSelectedIndex >= 0)
mChildren[mSelectedIndex]->setVisible(false);
mSelectedIndex = index;
mChildren[mSelectedIndex]->setVisible(true);
}
int StackedWidget::selectedIndex() const {
return mSelectedIndex;
}
void StackedWidget::performLayout(NVGcontext *ctx) {
for (auto child : mChildren) {
child->setPosition(Vector2i::Zero());
child->setSize(mSize);
child->performLayout(ctx);
}
}
Vector2i StackedWidget::preferredSize(NVGcontext *ctx) const {
Vector2i size = Vector2i::Zero();
for (auto child : mChildren)
size = size.cwiseMax(child->preferredSize(ctx));
return size;
}
void StackedWidget::addChild(int index, Widget *widget) {
if (mSelectedIndex >= 0)
mChildren[mSelectedIndex]->setVisible(false);
Widget::addChild(index, widget);
widget->setVisible(true);
setSelectedIndex(index);
}
NAMESPACE_END(nanogui)