00001 #include "gui.h"
00002 #include "debug.h"
00003
00004 using namespace irr;
00005 using namespace core;
00006 using namespace scene;
00007 using namespace video;
00008 using namespace gui;
00009
00010 extern Debug* dbg;
00011
00012 Gui::Gui(IrrlichtDevice* d, stringc name, rect<s32> position)
00013 {
00014
00015 dbg->log("create Gui");
00016 device = d;
00017 createRootWindow(name, position);
00018 }
00019
00020 Gui::~Gui()
00021 {
00022
00023 for (u32 i=0; i<elements.size(); i++)
00024 {
00025 elements[i]->remove();
00026 }
00027 elements.clear();
00028 window->remove();
00029 dbg->log("Gui deleted");
00030 }
00031
00032 void Gui::addSlider(s32 id, stringw text, s32 initialValue)
00033 {
00034 if (idExists(id))
00035 {
00036 dbg->log("Gui::addSlider() ID already exist", id);
00037 return;
00038 }
00039
00040
00041 stringw tmp(text.c_str());
00042 stringw value(initialValue);
00043 tmp.append(": ");
00044 tmp.append(value);
00045 addText(id - 19339, tmp);
00046
00047
00048 IGUIEnvironment* guienv = device->getGUIEnvironment();
00049 rect<s32> pos = getNextElementPos(20);
00050 pos.UpperLeftCorner.Y -= 8;
00051 pos.LowerRightCorner.Y -= 8;
00052 bool doHorizontal = true;
00053 IGUIElement* parent = window;
00054 elements.push_back(guienv->addScrollBar(doHorizontal, pos, parent, id) );
00055 elements.getLast()->setText(text.c_str());
00056 ((IGUIScrollBar*)elements.getLast())->setPos(initialValue);
00057 }
00058
00059 void Gui::addButton(s32 id, stringw text)
00060 {
00061 if (idExists(id))
00062 {
00063 dbg->log("Gui::addButton() ID already exist", id);
00064 return;
00065 }
00066
00067 IGUIEnvironment* guienv = device->getGUIEnvironment();
00068 rect<s32> pos = getNextElementPos(20);
00069 IGUIElement* parent = window;
00070 elements.push_back(guienv->addButton(pos, parent, id, text.c_str()) );
00071 }
00072
00073 void Gui::addText(s32 id, stringw text)
00074 {
00075 if (idExists(id))
00076 {
00077 dbg->log("Gui::addText() ID already exist", id);
00078 return;
00079 }
00080
00081 IGUIEnvironment* guienv = device->getGUIEnvironment();
00082 rect<s32> pos = getNextElementPos(10);
00083 bool doBorder = true;
00084 bool doWordWrap = true;
00085 IGUIElement* parent = window;
00086 bool doFillBG = false;
00087 elements.push_back(guienv->addStaticText(text.c_str(), pos, doBorder, doWordWrap, parent, id, doFillBG) );
00088 }
00089
00090 rect<s32> Gui::getNextElementPos(s32 height)
00091 {
00092 rect<s32> result;
00093
00094 if (elements.empty())
00095 {
00096 result.UpperLeftCorner.X = 5;
00097 result.UpperLeftCorner.Y = 30;
00098 result.LowerRightCorner.X = window->getRelativePosition().LowerRightCorner.X - window->getRelativePosition().UpperLeftCorner.X - 5;
00099 result.LowerRightCorner.Y = 30 + height;
00100 }
00101 else
00102 {
00103 IGUIElement* tmp = elements.getLast();
00104 result = tmp->getRelativePosition();
00105 result.UpperLeftCorner.X = 5;
00106 result.UpperLeftCorner.Y = result.LowerRightCorner.Y + 10;
00107 result.LowerRightCorner.X = window->getRelativePosition().LowerRightCorner.X - window->getRelativePosition().UpperLeftCorner.X - 5;
00108 result.LowerRightCorner.Y = result.UpperLeftCorner.Y + height;
00109 }
00110
00111
00112 rect<s32> winRect = window->getRelativePosition();
00113 s32 windowTitleHeight = 100;
00114 if (result.LowerRightCorner.Y + windowTitleHeight > winRect.LowerRightCorner.Y)
00115 {
00116 winRect.LowerRightCorner.Y = result.LowerRightCorner.Y + 20;
00117 window->setRelativePosition(winRect);
00118 }
00119 return result;
00120 }
00121
00122 void Gui::createRootWindow(stringc name, rect<s32> position)
00123 {
00124
00125 if (position.LowerRightCorner.Y < position.UpperLeftCorner.Y + 131)
00126 {
00127 position.LowerRightCorner.Y = position.UpperLeftCorner.Y + 130;
00128 }
00129 IGUIEnvironment* guienv = device->getGUIEnvironment();
00130 bool doModal = false;
00131 stringw text(name.c_str());
00132 IGUIElement* parent = guienv->getRootGUIElement();
00133 s32 id = 0;
00134 window = guienv->addWindow(position, doModal, text.c_str(), parent, id);
00135 if (! window)
00136 {
00137 dbg->log("Gui::createRootWindow() cannot create window");
00138 }
00139 window->getCloseButton()->setVisible(false);
00140 }
00141
00142 void Gui::setVisible(bool visible)
00143 {
00144 if (visible)
00145 {
00146 window->setVisible(true);
00147 }
00148 else
00149 {
00150 window->setVisible(false);
00151 }
00152 }
00153
00154 bool Gui::isVisible()
00155 {
00156 return window->isVisible();
00157 }
00158
00159 bool Gui::idExists(s32 id)
00160 {
00161 for (u32 i=0; i<elements.size(); i++)
00162 {
00163 if (elements[i]->getID() == id)
00164 {
00165 return true;
00166 }
00167 }
00168 return false;
00169 }
00170
00171 irr::s32 Gui::getSliderValue(s32 id)
00172 {
00173 s32 result = 0;
00174
00175 for (u32 i=0; i<elements.size(); i++)
00176 {
00177 if (elements[i]->getID() == id)
00178 {
00179 result = ((IGUIScrollBar*)elements[i])->getPos();
00180
00181 stringw tmp( getTextFromID(id).c_str() );
00182 tmp.append(": ");
00183 stringw value(result);
00184 tmp.append(value);
00185 setTextFromID(id - 19339, tmp);
00186 }
00187 }
00188 return result;
00189 }
00190
00191 stringc Gui::getTextFromID(s32 id)
00192 {
00193 stringc result("ID not found");
00194
00195 for (u32 i=0; i<elements.size(); i++)
00196 {
00197 if (elements[i]->getID() == id)
00198 {
00199 result = elements[i]->getText();
00200 }
00201 }
00202 return result;
00203 }
00204
00205 void Gui::setTextFromID(s32 id, stringw text)
00206 {
00207 for (u32 i=0; i<elements.size(); i++)
00208 {
00209 if (elements[i]->getID() == id)
00210 {
00211 elements[i]->setText(text.c_str());
00212 return;
00213 }
00214 }
00215 dbg->log("Gui::setTextFromID() cannot find id", id);
00216 }
00217
00218 void Gui::bringToFront()
00219 {
00220 device->getGUIEnvironment()->getRootGUIElement()->bringToFront(window);
00221 }