History: Mouse Cursor without using a GUI Toolkit for 1.4.4 (Eihort)
Source of version: 4 (current)
Copy to clipboard
Here is a simple class for enabling mouse cursor in 1.4.4 (Eihort) without using any GUI toolkit.
Based on ((How to show the Mouse Cursor W/O CEGUI))
Initial Construction:
{CODE(wrap="1", colors="c++")} mMouseCursor = new MouseCursor();
mMouseCursor->setImage("entis.png");
mMouseCursor->setVisible(true);
mMouseCursor->setWindowDimensions(win->getWidth(), win->getHeight());
{CODE}
Whenever mouse position changed, update it:
{CODE(wrap="1", colors="c++")} mMouseCursor->updatePosition(mMouse->getMouseState().X.abs, mMouse->getMouseState().Y.abs);
{CODE}
Whenever window size changed update it:
{CODE(wrap="1", colors="c++")} mMouseCursor->setWindowDimensions(rw->getWidth(), rw->getHeight());
{CODE}
Sample program:
{ATTACH(id=>26, icon=>1, showdesc=>1, dls=>1)} {ATTACH}
!!MouseCursor.h
{CODE(wrap="1", colors="c++")} #include <OgrePanelOverlayElement.h>
using namespace Ogre;
class MouseCursor
{
private:
Overlay* mGuiOverlay;
OverlayContainer* mCursorContainer;
TexturePtr mTexture;
MaterialPtr mMaterial;
Real mWindowWidth;
Real mWindowHeight;
public:
MouseCursor(): mGuiOverlay(0), mCursorContainer(0)
{
mMaterial = MaterialManager::getSingleton().create("MouseCursor/default", "General");
mCursorContainer = (OverlayContainer*) OverlayManager::getSingletonPtr()->createOverlayElement("Panel", "MouseCursor");
mCursorContainer->setMaterialName(mMaterial->getName());
mCursorContainer->setPosition(0, 0);
mGuiOverlay = OverlayManager::getSingletonPtr()->create("MouseCursor");
mGuiOverlay->setZOrder(649);
mGuiOverlay->add2D(mCursorContainer);
mGuiOverlay->show();
}
~MouseCursor(void) {}
void setImage(const String& filename)
{
mTexture = TextureManager::getSingleton().load(filename, "General");
TextureUnitState *pTexState;
if(mMaterial->getTechnique(0)->getPass(0)->getNumTextureUnitStates())
{
pTexState = mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0);
}
else
{
pTexState = mMaterial->getTechnique(0)->getPass(0)->createTextureUnitState( mTexture->getName() );
}
pTexState->setTextureAddressingMode(TextureUnitState::TAM_CLAMP);
mMaterial->getTechnique(0)->getPass(0)->setSceneBlending(SBT_TRANSPARENT_ALPHA);
}
void setWindowDimensions(unsigned int width, unsigned int height) {
mWindowWidth = (width > 0) ? width : 1;
mWindowHeight = (height > 0) ? height : 1;
mCursorContainer->setWidth(mTexture->getWidth() / mWindowWidth);
mCursorContainer->setHeight(mTexture->getHeight() / mWindowHeight);
}
void setVisible(bool visible)
{
if(visible) {
mCursorContainer->show();
} else {
mCursorContainer->hide();
}
}
void updatePosition(int x, int y)
{
Real rx = x / mWindowWidth;
Real ry = y / mWindowHeight;
mCursorContainer->setPosition(clamp(rx, 0.0f, 1.0f), clamp(ry, 0.0f, 1.0f));
}
Real clamp(Real a, Real min, Real max) {
if (a < min) {
return min;
}
if (a > max) {
return max;
}
return a;
}
};
{CODE}
For any discussion or questions use [http://www.ogre3d.org/phpBB2/viewtopic.php?p=246179#246179|forum page].