CGUITextureCacheBrowser.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt / Gaz Davidson
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CGUITextureCacheBrowser.h"
  5. #include "IGUIEnvironment.h"
  6. #include "IGUIButton.h"
  7. #include "IGUISkin.h"
  8. #include "IGUIFont.h"
  9. #include "IVideoDriver.h"
  10. namespace irr
  11. {
  12. namespace gui
  13. {
  14. CGUITextureCacheBrowser::CGUITextureCacheBrowser(IGUIEnvironment* environment, s32 id, IGUIElement *parent)
  15. : IGUIWindow(environment, parent, id, core::rect<s32>(0,0,300,200)),
  16. CloseButton(0), Panel(0), SelectedTexture(-1), Dragging(false), IsDraggable(true)
  17. {
  18. #ifdef _DEBUG
  19. setDebugName("CGUITextureCacheBrowser");
  20. #endif
  21. IGUISkin* skin = 0;
  22. IGUISpriteBank* sprites = 0;
  23. video::SColor color(255,255,255,255);
  24. if (environment)
  25. skin = environment->getSkin();
  26. s32 buttonw = 15;
  27. if (skin)
  28. {
  29. buttonw = skin->getSize(EGDS_WINDOW_BUTTON_WIDTH);
  30. sprites = skin->getSpriteBank();
  31. color = skin->getColor(EGDC_WINDOW_SYMBOL);
  32. }
  33. s32 posx = RelativeRect.getWidth() - buttonw - 4;
  34. CloseButton = Environment->addButton(core::rect<s32>(posx, 3, posx + buttonw, 3 + buttonw), this, -1,
  35. L"", skin ? skin->getDefaultText(EGDT_WINDOW_CLOSE) : L"Close" );
  36. CloseButton->setSubElement(true);
  37. CloseButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
  38. if (sprites)
  39. {
  40. CloseButton->setSpriteBank(sprites);
  41. CloseButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_WINDOW_CLOSE), color);
  42. CloseButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_WINDOW_CLOSE), color);
  43. }
  44. CloseButton->grab();
  45. // window title
  46. Text = L"Texture Browser";
  47. // panel element
  48. Panel = new CGUIPanel(environment, this);
  49. Panel->setRelativePosition( core::rect<s32>(1, buttonw + 5, 151, RelativeRect.getHeight() - 1));
  50. Panel->setAlignment(EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
  51. Panel->setBorder(true);
  52. Panel->setSubElement(true);
  53. // some buttons
  54. // add images from texture cache
  55. updateImageList();
  56. }
  57. CGUITextureCacheBrowser::~CGUITextureCacheBrowser()
  58. {
  59. if (CloseButton)
  60. CloseButton->drop();
  61. if (Panel)
  62. Panel->drop();
  63. // drop images
  64. u32 i;
  65. for (i=0; i<Images.size(); ++i)
  66. {
  67. Images[i]->drop();
  68. Images[i]->remove();
  69. }
  70. Images.clear();
  71. }
  72. void CGUITextureCacheBrowser::updateImageList()
  73. {
  74. if (!Panel)
  75. return;
  76. video::IVideoDriver* Driver = Environment->getVideoDriver();
  77. // clear images
  78. u32 i;
  79. for (i=0; i<Images.size(); ++i)
  80. {
  81. Images[i]->drop();
  82. Images[i]->remove();
  83. }
  84. Images.clear();
  85. u32 count = (u32)Driver->getTextureCount();
  86. s32 h = Panel->getClientArea().getWidth()-10;
  87. s32 hw = h/2;
  88. core::rect<s32> pos(Panel->getClientArea().getCenter().X - Panel->getAbsolutePosition().UpperLeftCorner.X - hw, 5,
  89. Panel->getClientArea().getCenter().X - Panel->getAbsolutePosition().UpperLeftCorner.X + hw, h+5);
  90. core::position2di moveDist(0, h+5);
  91. for (u32 i=0; i<count; ++i)
  92. {
  93. core::stringw details;
  94. video::ITexture* tex = Driver->getTextureByIndex(i);
  95. details = L"File name: ";
  96. details += tex->getName();
  97. details += L"\nFormat: ";
  98. video::ECOLOR_FORMAT cf = tex->getColorFormat();
  99. bool alpha = false;
  100. switch (cf)
  101. {
  102. case video::ECF_A1R5G5B5:
  103. details += L"A1R5G5B5 (16-bit with 1-bit alpha channel)\n";
  104. alpha = true;
  105. break;
  106. case video::ECF_R5G6B5:
  107. details += L"R5G6B5 (16-bit, no alpha channel)\n";
  108. break;
  109. case video::ECF_R8G8B8:
  110. details += L"R8G8B8 (16-bit, no alpha channel)\n";
  111. break;
  112. case video::ECF_A8R8G8B8:
  113. details += L"R8G8B8 (32-bit with 8-bit alpha channel)\n";
  114. alpha = true;
  115. break;
  116. default:
  117. details += L"Unknown\n";
  118. }
  119. core::dimension2du osize = tex->getOriginalSize();
  120. core::dimension2du size = tex->getOriginalSize();
  121. details += "Size: ";
  122. details += size.Width;
  123. details += "x";
  124. details += size.Height;
  125. if (osize != size)
  126. {
  127. details += "\nOriginal Size: ";
  128. details += osize.Width;
  129. details += "x";
  130. details += osize.Height;
  131. }
  132. details += L"\nMip-maps: ";
  133. if (tex->hasMipMaps())
  134. details += L"Yes\n";
  135. else
  136. details += L"No\n";
  137. IGUIImage* img = Environment->addImage(tex, core::position2di(1,1), alpha, Panel, i);
  138. img->grab();
  139. Images.push_back(img);
  140. img->setRelativePosition(pos);
  141. img->setToolTipText(details.c_str());
  142. img->setScaleImage(true);
  143. img->setColor( SelectedTexture == (s32)i ? video::SColor(255,255,255,255) : video::SColor(128,128,128,128) );
  144. pos = pos + moveDist;
  145. }
  146. }
  147. void CGUITextureCacheBrowser::updateAbsolutePosition()
  148. {
  149. IGUIWindow::updateAbsolutePosition();
  150. updateImageList();
  151. }
  152. //! called if an event happened.
  153. bool CGUITextureCacheBrowser::OnEvent(const SEvent &event)
  154. {
  155. switch(event.EventType)
  156. {
  157. case EET_GUI_EVENT:
  158. if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
  159. {
  160. if (event.GUIEvent.Caller == (IGUIElement*)this)
  161. Dragging = false;
  162. return true;
  163. }
  164. else
  165. if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
  166. {
  167. if (event.GUIEvent.Caller == CloseButton)
  168. {
  169. remove();
  170. return true;
  171. }
  172. }
  173. break;
  174. case EET_MOUSE_INPUT_EVENT:
  175. switch(event.MouseInput.Event)
  176. {
  177. case EMIE_LMOUSE_PRESSED_DOWN:
  178. DragStart.X = event.MouseInput.X;
  179. DragStart.Y = event.MouseInput.Y;
  180. if (getElementFromPoint(DragStart) == this)
  181. {
  182. if (!Environment->hasFocus(this))
  183. {
  184. Dragging = IsDraggable;
  185. //Environment->setFocus(this);
  186. if (Parent)
  187. Parent->bringToFront(this);
  188. }
  189. return true;
  190. }
  191. else
  192. {
  193. if (Panel->getAbsolutePosition().isPointInside(DragStart))
  194. {
  195. // select an image
  196. IGUIElement* el = Panel->getElementFromPoint(DragStart);
  197. if (el && el != Panel)
  198. {
  199. if (el->getType() == EGUIET_IMAGE)
  200. {
  201. setSelected(el->getID());
  202. }
  203. }
  204. else
  205. {
  206. setSelected();
  207. }
  208. }
  209. }
  210. break;
  211. case EMIE_LMOUSE_LEFT_UP:
  212. Dragging = false;
  213. //Environment->removeFocus(this);
  214. return true;
  215. case EMIE_MOUSE_MOVED:
  216. if (Dragging)
  217. {
  218. // gui window should not be dragged outside its parent
  219. if (Parent)
  220. if (event.MouseInput.X < Parent->getAbsolutePosition().UpperLeftCorner.X +1 ||
  221. event.MouseInput.Y < Parent->getAbsolutePosition().UpperLeftCorner.Y +1 ||
  222. event.MouseInput.X > Parent->getAbsolutePosition().LowerRightCorner.X -1 ||
  223. event.MouseInput.Y > Parent->getAbsolutePosition().LowerRightCorner.Y -1)
  224. return true;
  225. move(core::position2d<s32>(event.MouseInput.X - DragStart.X, event.MouseInput.Y - DragStart.Y));
  226. DragStart.X = event.MouseInput.X;
  227. DragStart.Y = event.MouseInput.Y;
  228. return true;
  229. }
  230. break;
  231. default:
  232. break;
  233. }
  234. default:
  235. break;
  236. }
  237. return Parent ? Parent->OnEvent(event) : false;
  238. }
  239. void CGUITextureCacheBrowser::setSelected(s32 index)
  240. {
  241. SelectedTexture = index;
  242. updateImageList();
  243. printf("Texture %d selected\n", index);
  244. }
  245. void CGUITextureCacheBrowser::draw()
  246. {
  247. if (!IsVisible)
  248. return;
  249. IGUISkin* skin = Environment->getSkin();
  250. core::rect<s32> rect = AbsoluteRect;
  251. core::rect<s32> *cl = &AbsoluteClippingRect;
  252. // draw body fast
  253. rect = skin->draw3DWindowBackground(this, true, skin->getColor(EGDC_ACTIVE_BORDER),
  254. AbsoluteRect, &AbsoluteClippingRect);
  255. // draw window text
  256. if (Text.size())
  257. {
  258. rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
  259. rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y);
  260. rect.LowerRightCorner.X -= skin->getSize(EGDS_WINDOW_BUTTON_WIDTH) + 5;
  261. IGUIFont* font = skin->getFont();
  262. if (font)
  263. font->draw(Text.c_str(), rect, skin->getColor(EGDC_ACTIVE_CAPTION), false, true, cl);
  264. }
  265. IGUIElement::draw();
  266. }
  267. bool CGUITextureCacheBrowser::isDraggable() const
  268. {
  269. return IsDraggable;
  270. }
  271. void CGUITextureCacheBrowser::setDraggable(bool draggable)
  272. {
  273. IsDraggable = draggable;
  274. if (Dragging && !IsDraggable)
  275. Dragging = false;
  276. }
  277. //! Returns the rectangle of the drawable area (without border, without titlebar and without scrollbars)
  278. core::rect<s32> CGUITextureCacheBrowser::getClientRect() const
  279. {
  280. return core::recti();
  281. }
  282. } // namespace gui
  283. } // namespace irr