123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- // Copyright (C) 2002-2012 Nikolaus Gebhardt / Gaz Davidson
- // This file is part of the "Irrlicht Engine".
- // For conditions of distribution and use, see copyright notice in irrlicht.h
- #include "CGUITextureCacheBrowser.h"
- #include "IGUIEnvironment.h"
- #include "IGUIButton.h"
- #include "IGUISkin.h"
- #include "IGUIFont.h"
- #include "IVideoDriver.h"
- namespace irr
- {
- namespace gui
- {
- CGUITextureCacheBrowser::CGUITextureCacheBrowser(IGUIEnvironment* environment, s32 id, IGUIElement *parent)
- : IGUIWindow(environment, parent, id, core::rect<s32>(0,0,300,200)),
- CloseButton(0), Panel(0), SelectedTexture(-1), Dragging(false), IsDraggable(true)
- {
- #ifdef _DEBUG
- setDebugName("CGUITextureCacheBrowser");
- #endif
- IGUISkin* skin = 0;
- IGUISpriteBank* sprites = 0;
- video::SColor color(255,255,255,255);
- if (environment)
- skin = environment->getSkin();
- s32 buttonw = 15;
- if (skin)
- {
- buttonw = skin->getSize(EGDS_WINDOW_BUTTON_WIDTH);
- sprites = skin->getSpriteBank();
- color = skin->getColor(EGDC_WINDOW_SYMBOL);
- }
- s32 posx = RelativeRect.getWidth() - buttonw - 4;
- CloseButton = Environment->addButton(core::rect<s32>(posx, 3, posx + buttonw, 3 + buttonw), this, -1,
- L"", skin ? skin->getDefaultText(EGDT_WINDOW_CLOSE) : L"Close" );
- CloseButton->setSubElement(true);
- CloseButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
- if (sprites)
- {
- CloseButton->setSpriteBank(sprites);
- CloseButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_WINDOW_CLOSE), color);
- CloseButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_WINDOW_CLOSE), color);
- }
- CloseButton->grab();
- // window title
- Text = L"Texture Browser";
- // panel element
- Panel = new CGUIPanel(environment, this);
- Panel->setRelativePosition( core::rect<s32>(1, buttonw + 5, 151, RelativeRect.getHeight() - 1));
- Panel->setAlignment(EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
- Panel->setBorder(true);
- Panel->setSubElement(true);
- // some buttons
- // add images from texture cache
- updateImageList();
- }
- CGUITextureCacheBrowser::~CGUITextureCacheBrowser()
- {
- if (CloseButton)
- CloseButton->drop();
- if (Panel)
- Panel->drop();
- // drop images
- u32 i;
- for (i=0; i<Images.size(); ++i)
- {
- Images[i]->drop();
- Images[i]->remove();
- }
- Images.clear();
- }
- void CGUITextureCacheBrowser::updateImageList()
- {
- if (!Panel)
- return;
- video::IVideoDriver* Driver = Environment->getVideoDriver();
- // clear images
- u32 i;
- for (i=0; i<Images.size(); ++i)
- {
- Images[i]->drop();
- Images[i]->remove();
- }
- Images.clear();
- u32 count = (u32)Driver->getTextureCount();
- s32 h = Panel->getClientArea().getWidth()-10;
- s32 hw = h/2;
- core::rect<s32> pos(Panel->getClientArea().getCenter().X - Panel->getAbsolutePosition().UpperLeftCorner.X - hw, 5,
- Panel->getClientArea().getCenter().X - Panel->getAbsolutePosition().UpperLeftCorner.X + hw, h+5);
- core::position2di moveDist(0, h+5);
- for (u32 i=0; i<count; ++i)
- {
- core::stringw details;
- video::ITexture* tex = Driver->getTextureByIndex(i);
- details = L"File name: ";
- details += tex->getName();
- details += L"\nFormat: ";
- video::ECOLOR_FORMAT cf = tex->getColorFormat();
- bool alpha = false;
- switch (cf)
- {
- case video::ECF_A1R5G5B5:
- details += L"A1R5G5B5 (16-bit with 1-bit alpha channel)\n";
- alpha = true;
- break;
- case video::ECF_R5G6B5:
- details += L"R5G6B5 (16-bit, no alpha channel)\n";
- break;
- case video::ECF_R8G8B8:
- details += L"R8G8B8 (16-bit, no alpha channel)\n";
- break;
- case video::ECF_A8R8G8B8:
- details += L"R8G8B8 (32-bit with 8-bit alpha channel)\n";
- alpha = true;
- break;
- default:
- details += L"Unknown\n";
- }
- core::dimension2du osize = tex->getOriginalSize();
- core::dimension2du size = tex->getOriginalSize();
- details += "Size: ";
- details += size.Width;
- details += "x";
- details += size.Height;
- if (osize != size)
- {
- details += "\nOriginal Size: ";
- details += osize.Width;
- details += "x";
- details += osize.Height;
- }
- details += L"\nMip-maps: ";
- if (tex->hasMipMaps())
- details += L"Yes\n";
- else
- details += L"No\n";
- IGUIImage* img = Environment->addImage(tex, core::position2di(1,1), alpha, Panel, i);
- img->grab();
- Images.push_back(img);
- img->setRelativePosition(pos);
- img->setToolTipText(details.c_str());
- img->setScaleImage(true);
- img->setColor( SelectedTexture == (s32)i ? video::SColor(255,255,255,255) : video::SColor(128,128,128,128) );
- pos = pos + moveDist;
- }
- }
- void CGUITextureCacheBrowser::updateAbsolutePosition()
- {
- IGUIWindow::updateAbsolutePosition();
- updateImageList();
- }
- //! called if an event happened.
- bool CGUITextureCacheBrowser::OnEvent(const SEvent &event)
- {
- switch(event.EventType)
- {
- case EET_GUI_EVENT:
- if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
- {
- if (event.GUIEvent.Caller == (IGUIElement*)this)
- Dragging = false;
- return true;
- }
- else
- if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
- {
- if (event.GUIEvent.Caller == CloseButton)
- {
- remove();
- return true;
- }
- }
- break;
- case EET_MOUSE_INPUT_EVENT:
- switch(event.MouseInput.Event)
- {
- case EMIE_LMOUSE_PRESSED_DOWN:
- DragStart.X = event.MouseInput.X;
- DragStart.Y = event.MouseInput.Y;
- if (getElementFromPoint(DragStart) == this)
- {
- if (!Environment->hasFocus(this))
- {
- Dragging = IsDraggable;
- //Environment->setFocus(this);
- if (Parent)
- Parent->bringToFront(this);
- }
- return true;
- }
- else
- {
- if (Panel->getAbsolutePosition().isPointInside(DragStart))
- {
- // select an image
- IGUIElement* el = Panel->getElementFromPoint(DragStart);
- if (el && el != Panel)
- {
- if (el->getType() == EGUIET_IMAGE)
- {
- setSelected(el->getID());
- }
- }
- else
- {
- setSelected();
- }
- }
- }
- break;
- case EMIE_LMOUSE_LEFT_UP:
- Dragging = false;
- //Environment->removeFocus(this);
- return true;
- case EMIE_MOUSE_MOVED:
- if (Dragging)
- {
- // gui window should not be dragged outside its parent
- if (Parent)
- if (event.MouseInput.X < Parent->getAbsolutePosition().UpperLeftCorner.X +1 ||
- event.MouseInput.Y < Parent->getAbsolutePosition().UpperLeftCorner.Y +1 ||
- event.MouseInput.X > Parent->getAbsolutePosition().LowerRightCorner.X -1 ||
- event.MouseInput.Y > Parent->getAbsolutePosition().LowerRightCorner.Y -1)
- return true;
- move(core::position2d<s32>(event.MouseInput.X - DragStart.X, event.MouseInput.Y - DragStart.Y));
- DragStart.X = event.MouseInput.X;
- DragStart.Y = event.MouseInput.Y;
- return true;
- }
- break;
- default:
- break;
- }
- default:
- break;
- }
- return Parent ? Parent->OnEvent(event) : false;
- }
- void CGUITextureCacheBrowser::setSelected(s32 index)
- {
- SelectedTexture = index;
- updateImageList();
- printf("Texture %d selected\n", index);
- }
- void CGUITextureCacheBrowser::draw()
- {
- if (!IsVisible)
- return;
- IGUISkin* skin = Environment->getSkin();
- core::rect<s32> rect = AbsoluteRect;
- core::rect<s32> *cl = &AbsoluteClippingRect;
- // draw body fast
- rect = skin->draw3DWindowBackground(this, true, skin->getColor(EGDC_ACTIVE_BORDER),
- AbsoluteRect, &AbsoluteClippingRect);
- // draw window text
- if (Text.size())
- {
- rect.UpperLeftCorner.X += skin->getSize(EGDS_TEXT_DISTANCE_X);
- rect.UpperLeftCorner.Y += skin->getSize(EGDS_TEXT_DISTANCE_Y);
- rect.LowerRightCorner.X -= skin->getSize(EGDS_WINDOW_BUTTON_WIDTH) + 5;
- IGUIFont* font = skin->getFont();
- if (font)
- font->draw(Text.c_str(), rect, skin->getColor(EGDC_ACTIVE_CAPTION), false, true, cl);
- }
- IGUIElement::draw();
- }
- bool CGUITextureCacheBrowser::isDraggable() const
- {
- return IsDraggable;
- }
- void CGUITextureCacheBrowser::setDraggable(bool draggable)
- {
- IsDraggable = draggable;
- if (Dragging && !IsDraggable)
- Dragging = false;
- }
- //! Returns the rectangle of the drawable area (without border, without titlebar and without scrollbars)
- core::rect<s32> CGUITextureCacheBrowser::getClientRect() const
- {
- return core::recti();
- }
- } // namespace gui
- } // namespace irr
|