1/*
2 nanogui/stackedwidget.cpp -- Widget used to stack widgets on top
3 of each other. Only the active widget is visible.
4
5 The stacked widget was contributed by Stefan Ivanov.
6
7 NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>.
8 The widget drawing code is based on the NanoVG demo application
9 by Mikko Mononen.
10
11 All rights reserved. Use of this source code is governed by a
12 BSD-style license that can be found in the LICENSE.txt file.
13*/
14
15#include <nanogui/stackedwidget.h>
16
17NAMESPACE_BEGIN(nanogui)
18
19StackedWidget::StackedWidget(nanogui::Widget *parent)
20 : Widget(parent) { }
21
22void StackedWidget::setSelectedIndex(int index) {
23 assert(index < childCount());
24 if (mSelectedIndex >= 0)
25 mChildren[mSelectedIndex]->setVisible(false);
26 mSelectedIndex = index;
27 mChildren[mSelectedIndex]->setVisible(true);
28}
29
30int StackedWidget::selectedIndex() const {
31 return mSelectedIndex;
32}
33
34void StackedWidget::performLayout(NVGcontext *ctx) {
35 for (auto child : mChildren) {
36 child->setPosition(Vector2i::Zero());
37 child->setSize(mSize);
38 child->performLayout(ctx);
39 }
40}
41
42Vector2i StackedWidget::preferredSize(NVGcontext *ctx) const {
43 Vector2i size = Vector2i::Zero();
44 for (auto child : mChildren)
45 size = size.cwiseMax(child->preferredSize(ctx));
46 return size;
47}
48
49void StackedWidget::addChild(int index, Widget *widget) {
50 if (mSelectedIndex >= 0)
51 mChildren[mSelectedIndex]->setVisible(false);
52 Widget::addChild(index, widget);
53 widget->setVisible(true);
54 setSelectedIndex(index);
55}
56
57NAMESPACE_END(nanogui)
58