1 /*---------------------------------------------------------------------------*
2 
3   Copyright (C) Nintendo.  All rights reserved.
4 
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law.  They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10 
11  *---------------------------------------------------------------------------*/
12 
13     // Creates a window with tabs
TabWindow(WindowManager * manager,const char * name,CVec2 position,float width,bool canKill)14 TabWindow::TabWindow(WindowManager* manager, const char* name, CVec2 position, float width, bool canKill)
15     : Window(manager, name, position, CVec2(width, WINDOW_HANDLE_HEIGHT), CVec2(0, 0), canKill), tabWindowSelected(0), tabWindowOver(-1)
16 {
17 
18 }
19 
20     // Draws the tab window
Draw()21 void TabWindow::Draw()
22 {
23         // At this point, we better have some tabs!
24     ASSERT(tabWindowList.size() && "Must have at least one tab!");
25 
26     float temp = this->offY;
27     this->offY = 0;
28 
29         // We can't be over the one that's selected!
30     if (tabWindowSelected == tabWindowOver)
31         tabWindowOver = -1;
32 
33     float x = -GetWidth() / 2 + GetWidth() / tabWindowList.size() / 2;
34     float y = -height / 2;
35 
36     float spacing = GetWidth() / tabWindowList.size();
37 
38     float buttonWidth = GetWidth() / tabWindowList.size();
39     float buttonHeight = height;
40     float textHeight = buttonHeight * 0.8;
41 
42     for (int i = 0; i < tabWindowList.size(); ++i)
43     {
44         if (tabWindowSelected != i)
45             menu->DrawBox(CVec3(x, y, 0.99), CVec2(buttonWidth, buttonHeight), CVec4(0.2, 0.2, 0.2));
46         else
47             menu->DrawQuad(CVec3(x, y, 0.99), CVec2(buttonWidth, buttonHeight), CVec4(0, 0, 0, 0));
48 
49         if (tabWindowOver == i)
50             menu->DrawBox(CVec3(x, y, 0.9901), CVec2(buttonWidth, buttonHeight),  CVec4(0.3, 0.3, 0.3));
51 
52         menu->DrawTextCenter(tabWindowList[i]->name, CVec3(x, y, 0.9902), CVec2(buttonWidth, textHeight),  CVec4(1.0, 1.0, 1.0));
53 
54         x += spacing;
55     }
56 
57     AttachWindow(tabWindowList[tabWindowSelected]);
58 
59     this->offY = temp;
60 }
61 
62     // Anything special the window updates beforehand
PreUpdate(bool canUpdate)63 bool TabWindow::PreUpdate(bool canUpdate)
64 {
65     this->offY = 0;
66 
67     if (canUpdate)
68     {
69         float height = this->GetHeight() - this->height;
70         scrollBar->count = this->GetMaxHeight() - this->height;
71         scrollBar->max = height;
72         scrollBar->x = (this->GetWidth() - SCROLLBAR_WIDTH) / 2;
73         scrollBar->y = -height / 2 - this->height;
74         scrollBar->height = height;
75         canUpdate = scrollBar->Update();
76     }
77     else
78         scrollBar->Reset();
79 
80         // At this point, we better have some tabs!
81     ASSERT(tabWindowList.size() && "Must have at least one tab!");
82 
83     tabWindowOver = -1;
84 
85         // The cursor is over one of the buttons
86     if (canUpdate && menu->CursorColliding(0, -height / 2, GetWidth(), height))
87     {
88             // Figure out which one!
89         tabWindowOver = (menu->CursorX() - this->GetLeft()) / this->GetWidth() * tabWindowList.size();
90         canUpdate = false;
91 
92         if (menu->PositiveTriggered())
93         {
94             tabWindowSelected = tabWindowOver;
95             scrollBar->pos = 0;
96             scrollBar->wasMax = false;
97         }
98     }
99 
100     AttachWindow(tabWindowList[tabWindowSelected]);
101 
102     this->offY = scrollBar->pos;
103 
104     return canUpdate;
105 }
106 
107     // Anything special the window draws beforehand
PreDraw()108 void TabWindow::PreDraw()
109 {
110     float temp = this->offY;
111     this->offY = 0;
112 
113     float height = this->GetHeight() - this->height;
114     scrollBar->count = this->GetMaxHeight() - this->height;
115     scrollBar->max = height;
116     scrollBar->x = (this->GetWidth() - SCROLLBAR_WIDTH) / 2;
117     scrollBar->y = -height / 2 - this->height;
118     scrollBar->height = height;
119     scrollBar->Draw();
120 
121     this->offY = temp;
122 }
123 
124     // Adds a window to the list of tabs
AddWindow(Window * window)125 void TabWindow::AddWindow(Window* window)
126 {
127     tabWindowList.push_back(window);
128 }
129 
130     // Removes a window from the list of tabs
RemoveWindow(Window * window)131 void TabWindow::RemoveWindow(Window* window)
132 {
133     int remove = 0;
134     for (std::vector<Window*>::iterator iter = tabWindowList.begin(); iter != tabWindowList.end(); ++iter, ++remove)
135     {
136         if (*iter == window)
137         {
138                 // Make sure the selected number is updated
139             if (remove < tabWindowSelected || (tabWindowSelected == tabWindowList.size() - 1 && tabWindowSelected > 0))
140                 tabWindowSelected --;
141 
142                 // Just in case, remove ALL instances of the window in the tab list
143             tabWindowList.erase(iter);
144             RemoveWindow(window);
145             return;
146         }
147     }
148 }
149