Difference between revisions of "Moving any Window"
(Fixed the initial movement of the window) |
|||
Line 95: | Line 95: | ||
mMovingWindow = windowEventArgs.window; | mMovingWindow = windowEventArgs.window; | ||
mMovingEvent = System::getSingleton().getGUISheet()->subscribeEvent(CEGUI::Window::EventMouseMove, Event::Subscriber(&DemoSample::onWindowMove, this)); | mMovingEvent = System::getSingleton().getGUISheet()->subscribeEvent(CEGUI::Window::EventMouseMove, Event::Subscriber(&DemoSample::onWindowMove, this)); | ||
+ | mMousePosInWindow = CoordConverter::screenToWindow(*mMovingWindow, MouseCursor::getSingleton().getPosition()); | ||
} | } | ||
return false; // Do not propagate this event to other subscribers | return false; // Do not propagate this event to other subscribers | ||
Line 116: | Line 117: | ||
const MouseEventArgs mouseEventArgs = static_cast<const MouseEventArgs&>(pEventArgs); | const MouseEventArgs mouseEventArgs = static_cast<const MouseEventArgs&>(pEventArgs); | ||
Vector2 localMousePos = CoordConverter::screenToWindow(*mMovingWindow, mouseEventArgs.position); | Vector2 localMousePos = CoordConverter::screenToWindow(*mMovingWindow, mouseEventArgs.position); | ||
− | UVector2 | + | UVector2 mouseMoveOffset(cegui_absdim(localMousePos.d_x), cegui_absdim(localMousePos.d_y)); |
− | + | UVector2 mouseOffsetInWindow(cegui_absdim(mMousePosInWindow.d_x), cegui_absdim(mMousePosInWindow.d_y)); | |
+ | mMovingWindow->setPosition(mMovingWindow->getPosition() + mouseMoveOffset - mouseOffsetInWindow); | ||
return true; | return true; | ||
} | } | ||
Line 124: | Line 126: | ||
CEGUI::Window* mMovingWindow; | CEGUI::Window* mMovingWindow; | ||
CEGUI::Event::Connection mMovingEvent; | CEGUI::Event::Connection mMovingEvent; | ||
+ | CEGUI::Vector2 mMousePosInWindow; | ||
int mClickCount; | int mClickCount; | ||
}; | }; |
Revision as of 16:37, 13 September 2007
Introduction
This bit of code shows how you can play with the Cegui events to move any window, not just FrameWindows.
The current implementation listens for a left mouse click. When a CEGUI::Window* is clicked it is flagged as being a "moving window". The GUI Sheet then subscribes to mouse move events, and upon a mouse movement the "moving window"'s position is changed. Left clicking again releases the window.
There are two problems with this implementation. I tried to use a right-click as the trigger but could not. There may be a problem with the way mouse clicks are injected or with my code. The second problem is that the CEGUI::Window* jumps when initially moved. That's become I'm not taking into account the offset of the mouse within that window.
Files
MovingAnyWindow.h
<cpp/>
- ifndef _00MovingAnyWindow_h_
- define _00MovingAnyWindow_h_
- include "CEGuiSample.h"
- include "CEGUI.h"
class DemoSample : public CEGuiSample { public:
// method to initialise the sample windows and events. bool initialiseSample()
{ using namespace CEGUI; try { // Retrieve the window manager WindowManager& winMgr = WindowManager::getSingleton();
// Load the TaharezLook scheme and set up the default mouse cursor and font Scheme* currentScheme = SchemeManager::getSingleton().loadScheme("TaharezLook.scheme"); System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow"); FontManager::getSingleton().createFont("Commonwealth-10.font");
// Set the GUI Sheet Window* sheet = winMgr.createWindow("DefaultWindow", "root_wnd"); System::getSingleton().setGUISheet(sheet);
// Initially we are not moving any window mMovingWindow = 0;
// We have not yet clicked on the button mClickCount = 0;
// Create a button PushButton* pushButton = static_cast<PushButton*>(winMgr.createWindow("TaharezLook/Button", "Pushbutton")); sheet->addChildWindow(pushButton); pushButton->setPosition(UVector2(cegui_reldim(0.81f), cegui_reldim( 0.32f))); pushButton->setSize(UVector2(cegui_reldim(0.15f), cegui_reldim( 0.2f))); pushButton->setText("Click"); pushButton->subscribeEvent(CEGUI::PushButton::EventClicked, Event::Subscriber(&DemoSample::onRightClick, this)); pushButton->subscribeEvent(CEGUI::PushButton::EventClicked, Event::Subscriber(&DemoSample::onLeftClick, this));
// Create a multicolum list MultiColumnList* mcl = static_cast<MultiColumnList*>(winMgr.createWindow("TaharezLook/MultiColumnList", "MultiColumnList")); sheet->addChildWindow(mcl); mcl->setPosition(UVector2(cegui_reldim(0.01f), cegui_reldim( 0.1f))); mcl->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim( 0.8f))); mcl->subscribeEvent(CEGUI::MultiColumnList::EventMouseClick, Event::Subscriber(&DemoSample::onRightClick, this)); } catch(Exception &e) { #if defined( __WIN32__ ) || defined( _WIN32 ) MessageBox(NULL, e.getMessage().c_str(), "Error initializing the demo", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "Error initializing the demo:" << e.getMessage().c_str() << "\n"; #endif }
return true; }
// method to perform any required cleanup operations. void cleanupSample(void)
{ }
bool onRightClick(const CEGUI::EventArgs& pEventArgs) { using namespace CEGUI; const MouseEventArgs mouseEventArgs = static_cast<const MouseEventArgs&>(pEventArgs); // Check if right click is injected properly because this never works if(true) // if(mouseEventArgs.button == CEGUI::RightButton) { if(mMovingWindow) { // We were moving a window, stop doing so on a second right click mMovingEvent->disconnect(); mMovingWindow = 0; } else { // Start moving a window when right clicking const WindowEventArgs windowEventArgs = static_cast<const WindowEventArgs&>(pEventArgs); mMovingWindow = windowEventArgs.window; mMovingEvent = System::getSingleton().getGUISheet()->subscribeEvent(CEGUI::Window::EventMouseMove, Event::Subscriber(&DemoSample::onWindowMove, this)); mMousePosInWindow = CoordConverter::screenToWindow(*mMovingWindow, MouseCursor::getSingleton().getPosition()); } return false; // Do not propagate this event to other subscribers } return true; }
bool onLeftClick(const CEGUI::EventArgs& pEventArgs) { // This is to check that the event is not propagated using namespace CEGUI; const WindowEventArgs windowEventArgs = static_cast<const WindowEventArgs&>(pEventArgs); ++mClickCount; windowEventArgs.window->setText("Click " + CEGUI::PropertyHelper::intToString(mClickCount)); return true; }
bool onWindowMove(const CEGUI::EventArgs& pEventArgs) { using namespace CEGUI; const MouseEventArgs mouseEventArgs = static_cast<const MouseEventArgs&>(pEventArgs); Vector2 localMousePos = CoordConverter::screenToWindow(*mMovingWindow, mouseEventArgs.position);
UVector2 mouseMoveOffset(cegui_absdim(localMousePos.d_x), cegui_absdim(localMousePos.d_y));
UVector2 mouseOffsetInWindow(cegui_absdim(mMousePosInWindow.d_x), cegui_absdim(mMousePosInWindow.d_y)); mMovingWindow->setPosition(mMovingWindow->getPosition() + mouseMoveOffset - mouseOffsetInWindow); return true; }
private: CEGUI::Window* mMovingWindow; CEGUI::Event::Connection mMovingEvent; CEGUI::Vector2 mMousePosInWindow; int mClickCount; };
- endif // _00MovingAnyWindow_h_
main.cpp
<cpp/>
- if defined( __WIN32__ ) || defined( _WIN32 )
#define WIN32_LEAN_AND_MEAN #define NOMINMAX #include "windows.h"
- endif
- include "MovingAnyWindow.h"
- if defined( __WIN32__ ) || defined( _WIN32 )
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
- else
int main(int argc, char *argv[])
- endif
{
DemoSample app; return app.run();
}