Difference between revisions of "Moving any Window"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
(Introduction)
(Fixed the problem with right-click not activating the move mode)
Line 3: Line 3:
  
 
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.
 
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 is a problem 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.
 
  
 
=== Files ===
 
=== Files ===
Line 48: Line 46:
 
pushButton->setSize(UVector2(cegui_reldim(0.15f), cegui_reldim( 0.2f)));
 
pushButton->setSize(UVector2(cegui_reldim(0.15f), cegui_reldim( 0.2f)));
 
pushButton->setText("Click");
 
pushButton->setText("Click");
pushButton->subscribeEvent(CEGUI::PushButton::EventClicked, Event::Subscriber(&DemoSample::onRightClick, this));
+
pushButton->subscribeEvent(CEGUI::PushButton::EventMouseClick, Event::Subscriber(&DemoSample::onRightClick, this));
 
pushButton->subscribeEvent(CEGUI::PushButton::EventClicked, Event::Subscriber(&DemoSample::onLeftClick, this));
 
pushButton->subscribeEvent(CEGUI::PushButton::EventClicked, Event::Subscriber(&DemoSample::onLeftClick, this));
  
Line 79: Line 77:
 
using namespace CEGUI;
 
using namespace CEGUI;
 
const MouseEventArgs mouseEventArgs = static_cast<const MouseEventArgs&>(pEventArgs);
 
const MouseEventArgs mouseEventArgs = static_cast<const MouseEventArgs&>(pEventArgs);
// Check if right click is injected properly because this never works
+
if(mouseEventArgs.button == CEGUI::RightButton)
if(true) // if(mouseEventArgs.button == CEGUI::RightButton)
+
 
{
 
{
 
if(mMovingWindow)
 
if(mMovingWindow)

Revision as of 16:49, 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.

Files

MovingAnyWindow.h

<cpp/>

  1. ifndef _00MovingAnyWindow_h_
  2. define _00MovingAnyWindow_h_
  1. include "CEGuiSample.h"
  2. 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::EventMouseClick, 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); 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; };

  1. endif // _00MovingAnyWindow_h_

main.cpp

<cpp/>

  1. if defined( __WIN32__ ) || defined( _WIN32 )

#define WIN32_LEAN_AND_MEAN #define NOMINMAX #include "windows.h"

  1. endif
  1. include "MovingAnyWindow.h"


  1. if defined( __WIN32__ ) || defined( _WIN32 )

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)

  1. else

int main(int argc, char *argv[])

  1. endif

{

   DemoSample app;
   return app.run();

}