]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CGUIToolBar.cpp
Remove core::list and replace uses with std::list (#105)
[irrlicht.git] / source / Irrlicht / CGUIToolBar.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #include "CGUIToolBar.h"\r
6 #ifdef _IRR_COMPILE_WITH_GUI_\r
7 \r
8 #include "IGUISkin.h"\r
9 #include "IGUIEnvironment.h"\r
10 #include "IVideoDriver.h"\r
11 #include "IGUIButton.h"\r
12 #include "IGUIFont.h"\r
13 #include "CGUIButton.h"\r
14 \r
15 namespace irr\r
16 {\r
17 namespace gui\r
18 {\r
19 \r
20 //! constructor\r
21 CGUIToolBar::CGUIToolBar(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)\r
22 :IGUIToolBar(environment, parent, id, rectangle), ButtonX(5)\r
23 {\r
24         #ifdef _DEBUG\r
25         setDebugName("CGUIToolBar");\r
26         #endif\r
27 \r
28         // calculate position and find other menubars\r
29         s32 y = 0;\r
30         s32 parentwidth = 100;\r
31 \r
32         if (parent)\r
33         {\r
34                 parentwidth = Parent->getAbsolutePosition().getWidth();\r
35                 s32 parentheight = Parent->getAbsolutePosition().getHeight();\r
36 \r
37                 for (const auto& e : parent->getChildren())\r
38                 {\r
39                         if (    e->hasType(EGUIET_CONTEXT_MENU) \r
40                                 ||      e->hasType(EGUIET_MENU) \r
41                                 ||      e->hasType(EGUIET_TOOL_BAR)  )\r
42                         {\r
43                                 core::rect<s32> r = e->getAbsolutePosition();\r
44                                 if (r.UpperLeftCorner.X == 0 && r.UpperLeftCorner.Y <= y &&\r
45                                         r.LowerRightCorner.X == parentwidth \r
46                                         && parentheight > r.LowerRightCorner.Y  )\r
47                                         y = r.LowerRightCorner.Y;\r
48                         }\r
49                         else\r
50                         {\r
51                                 e->getType();\r
52                         }\r
53                 }\r
54         }\r
55 \r
56         core::rect<s32> rr;\r
57         rr.UpperLeftCorner.X = 0;\r
58         rr.UpperLeftCorner.Y = y;\r
59         s32 height = Environment->getSkin()->getSize ( EGDS_MENU_HEIGHT );\r
60 \r
61         /*IGUISkin* skin = Environment->getSkin();\r
62         IGUIFont* font = skin->getFont();\r
63         if (font)\r
64         {\r
65                 s32 t = font->getDimension(L"A").Height + 5;\r
66                 if (t > height)\r
67                         height = t;\r
68         }*/\r
69 \r
70         rr.LowerRightCorner.X = parentwidth;\r
71         rr.LowerRightCorner.Y = rr.UpperLeftCorner.Y + height;\r
72         setRelativePosition(rr);\r
73 }\r
74 \r
75 \r
76 //! called if an event happened.\r
77 bool CGUIToolBar::OnEvent(const SEvent& event)\r
78 {\r
79         if (isEnabled())\r
80         {\r
81                 if (event.EventType == EET_MOUSE_INPUT_EVENT &&\r
82                         event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)\r
83                 {\r
84                         if (AbsoluteClippingRect.isPointInside(core::position2di(event.MouseInput.X, event.MouseInput.Y)))\r
85                                 return true;\r
86                 }\r
87         }\r
88 \r
89         return IGUIElement::OnEvent(event);\r
90 }\r
91 \r
92 \r
93 //! draws the element and its children\r
94 void CGUIToolBar::draw()\r
95 {\r
96         if (!IsVisible)\r
97                 return;\r
98 \r
99         IGUISkin* skin = Environment->getSkin();\r
100         if (!skin)\r
101                 return;\r
102 \r
103         core::rect<s32> rect = AbsoluteRect;\r
104         core::rect<s32>* clip = &AbsoluteClippingRect;\r
105 \r
106         // draw frame\r
107         skin->draw3DToolBar(this, rect, clip);\r
108 \r
109         IGUIElement::draw();\r
110 }\r
111 \r
112 \r
113 //! Updates the absolute position.\r
114 void CGUIToolBar::updateAbsolutePosition()\r
115 {\r
116         if (Parent)\r
117         {\r
118                 DesiredRect.UpperLeftCorner.X = 0;\r
119                 DesiredRect.LowerRightCorner.X = Parent->getAbsolutePosition().getWidth();\r
120         }\r
121 \r
122         IGUIElement::updateAbsolutePosition();\r
123 }\r
124 \r
125 \r
126 //! Adds a button to the tool bar\r
127 IGUIButton* CGUIToolBar::addButton(s32 id, const wchar_t* text,const wchar_t* tooltiptext,\r
128         video::ITexture* img, video::ITexture* pressed, bool isPushButton,\r
129         bool useAlphaChannel)\r
130 {\r
131         ButtonX += 3;\r
132 \r
133         core::rect<s32> rectangle(ButtonX,2,ButtonX+1,3);\r
134         if ( img )\r
135         {\r
136                 const core::dimension2du &size = img->getOriginalSize();\r
137                 rectangle.LowerRightCorner.X = rectangle.UpperLeftCorner.X + size.Width + 8;\r
138                 rectangle.LowerRightCorner.Y = rectangle.UpperLeftCorner.Y + size.Height + 6;\r
139         }\r
140 \r
141         if ( text )\r
142         {\r
143                 IGUISkin* skin = Environment->getSkin();\r
144                 IGUIFont * font = skin->getFont(EGDF_BUTTON);\r
145                 if ( font )\r
146                 {\r
147                         core::dimension2d<u32> dim = font->getDimension(text);\r
148                         if ( (s32)dim.Width > rectangle.getWidth() )\r
149                                 rectangle.LowerRightCorner.X = rectangle.UpperLeftCorner.X + dim.Width + 8;\r
150                         if ( (s32)dim.Height > rectangle.getHeight() )\r
151                                 rectangle.LowerRightCorner.Y = rectangle.UpperLeftCorner.Y + dim.Height + 6;\r
152                 }\r
153         }\r
154 \r
155         ButtonX += rectangle.getWidth();\r
156 \r
157         IGUIButton* button = new CGUIButton(Environment, this, id, rectangle);\r
158         button->drop();\r
159 \r
160         if (text)\r
161                 button->setText(text);\r
162 \r
163         if (tooltiptext)\r
164                 button->setToolTipText(tooltiptext);\r
165 \r
166         if (img)\r
167                 button->setImage(img);\r
168 \r
169         if (pressed)\r
170                 button->setPressedImage(pressed);\r
171 \r
172         if (isPushButton)\r
173                 button->setIsPushButton(isPushButton);\r
174 \r
175         if (useAlphaChannel)\r
176                 button->setUseAlphaChannel(useAlphaChannel);\r
177 \r
178         return button;\r
179 }\r
180 \r
181 } // end namespace gui\r
182 } // end namespace irr\r
183 \r
184 #endif // _IRR_COMPILE_WITH_GUI_\r
185 \r