]> git.lizzy.rs Git - minetest.git/blob - src/guiPauseMenu.cpp
676c145f25a10163e39e8925d0a631dd7753c366
[minetest.git] / src / guiPauseMenu.cpp
1 /*\r
2 Minetest-c55\r
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>\r
4 Original author Kabak Dmitry <userdima@gmail.com>, contributed under\r
5 the minetest contributor agreement.\r
6 \r
7 This program is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 This program is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License along\r
18 with this program; if not, write to the Free Software Foundation, Inc.,\r
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
20 */\r
21 \r
22 \r
23 #include "guiPauseMenu.h"\r
24 \r
25 void guiPauseMenu::scaleGui() // this function scales gui from the size stored in file to screen size\r
26 {\r
27         core::dimension2du screen=dev->getVideoDriver()->getScreenSize();\r
28         core::vector2di real=root->getAbsolutePosition().LowerRightCorner; // determine gui size stored in file (which is size of my menu root node)\r
29         float factorX=(float)screen.Width/(float)real.X;\r
30         float factorY=(float)screen.Height/(float)real.Y;\r
31         scaleGui(guienv->getRootGUIElement(),factorX,factorY);\r
32 }\r
33 void guiPauseMenu::scaleGui(gui::IGUIElement *node,float factorX,float factorY) // recursive set scale\r
34 {\r
35         if((node->getParent() && node->getParent()->getID()==255) || node->getID()==255) // modify only menu's elements\r
36         {\r
37                 int lx,rx,ly,ry;\r
38                 lx=(float)node->getRelativePosition().UpperLeftCorner.X*factorX;\r
39                 ly=(float)node->getRelativePosition().UpperLeftCorner.Y*factorY;\r
40                 rx=(float)node->getRelativePosition().LowerRightCorner.X*factorX;\r
41                 ry=(float)node->getRelativePosition().LowerRightCorner.Y*factorY;\r
42                 node->setRelativePosition(core::recti(lx,ly,rx,ry));\r
43         }\r
44 \r
45         core::list<gui::IGUIElement*>::ConstIterator it = node->getChildren().begin();\r
46         for(; it != node->getChildren().end(); ++it)\r
47                 scaleGui((*it),factorX,factorY);\r
48 }\r
49 \r
50 bool guiPauseMenu::loadMenu()\r
51 {\r
52         guienv->loadGUI("../data/pauseMenu.gui");\r
53 \r
54         root=(gui::IGUIStaticText*)guienv->getRootGUIElement()->getElementFromId(255,true);\r
55         if(!root) // if there is no my root node then menu file not found or corrupted\r
56                 return false;\r
57 \r
58         scaleGui(); // scale gui to our screen size\r
59 \r
60         root->setVisible(false); // hide our menu\r
61         // make it transparent\r
62         //root->setBackgroundColor(video::SColor(100,128,100,128));\r
63         root->setBackgroundColor(video::SColor(140,0,0,0));\r
64 \r
65         return true;\r
66 }\r
67 \r
68 guiPauseMenu::guiPauseMenu(IrrlichtDevice *device, IEventReceiver *recv) : dev(device), oldRecv(recv)\r
69 {\r
70         if(!dev)\r
71                 return;\r
72         guienv=dev->getGUIEnvironment();\r
73 \r
74         if (!loadMenu())\r
75                 return;\r
76 \r
77         device->setEventReceiver(this); // now WE are the input receiver! ahhaha! \r
78 }\r
79 \r
80 bool guiPauseMenu::OnEvent(const SEvent& event)\r
81 {\r
82         if(!dev->isWindowFocused())\r
83                 setVisible(true);\r
84 \r
85         bool ret=false;\r
86         if(oldRecv && !isVisible()) // call master if we have it and if we are inactive\r
87                 ret=oldRecv->OnEvent(event);\r
88 \r
89         if(ret==true)\r
90                 return true; // if the master receiver does the work\r
91 \r
92         if(event.EventType==EET_KEY_INPUT_EVENT)\r
93         {\r
94                 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)\r
95                 {\r
96                         setVisible(!isVisible());\r
97                 }\r
98         }\r
99         if(event.EventType==EET_GUI_EVENT)\r
100         {\r
101                 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)\r
102                 {\r
103                         switch(event.GUIEvent.Caller->getID())\r
104                         {\r
105                         case 256: // continue\r
106                                 setVisible(false);\r
107                                 break;\r
108                         case 257: // exit\r
109                                 dev->closeDevice();\r
110                                 break;\r
111                         }\r
112                 }\r
113         }\r
114 \r
115         return false;\r
116 }\r
117 \r
118 guiPauseMenu::~guiPauseMenu(void)\r
119 {\r
120 }\r