Use a controller to move Drag&Drop widget with the mouse cursor

deque
scrawl 11 years ago
parent a18cec7c8e
commit 715d357f24

@ -28,6 +28,16 @@
namespace MWGui namespace MWGui
{ {
DragAndDrop::DragAndDrop()
: mDraggedWidget(NULL)
, mDraggedCount(0)
, mSourceModel(NULL)
, mSourceView(NULL)
, mSourceSortModel(NULL)
, mIsOnDragAndDrop(NULL)
{
}
void DragAndDrop::startDrag (int index, SortFilterItemModel* sortModel, ItemModel* sourceModel, ItemView* sourceView, int count) void DragAndDrop::startDrag (int index, SortFilterItemModel* sortModel, ItemModel* sourceModel, ItemView* sourceView, int count)
{ {
mItem = sourceModel->getItem(index); mItem = sourceModel->getItem(index);
@ -36,7 +46,6 @@ namespace MWGui
mSourceView = sourceView; mSourceView = sourceView;
mSourceSortModel = sortModel; mSourceSortModel = sortModel;
mIsOnDragAndDrop = true; mIsOnDragAndDrop = true;
mDragAndDropWidget->setVisible(true);
// If picking up an item that isn't from the player's inventory, the item gets added to player inventory backend // If picking up an item that isn't from the player's inventory, the item gets added to player inventory backend
// immediately, even though it's still floating beneath the mouse cursor. A bit counterintuitive, // immediately, even though it's still floating beneath the mouse cursor. A bit counterintuitive,
@ -73,8 +82,13 @@ namespace MWGui
mSourceSortModel->addDragItem(mItem.mBase, count); mSourceSortModel->addDragItem(mItem.mBase, count);
} }
ItemWidget* baseWidget = mDragAndDropWidget->createWidget<ItemWidget> ItemWidget* baseWidget = MyGUI::Gui::getInstance().createWidget<ItemWidget>("MW_ItemIcon", 0, 0, 42, 42, MyGUI::Align::Default, "DragAndDrop");
("MW_ItemIcon", MyGUI::IntCoord(0, 0, 42, 42), MyGUI::Align::Default);
Controllers::ControllerFollowMouse* controller =
MyGUI::ControllerManager::getInstance().createItem(Controllers::ControllerFollowMouse::getClassTypeName())
->castType<Controllers::ControllerFollowMouse>();
MyGUI::ControllerManager::getInstance().addItem(baseWidget, controller);
mDraggedWidget = baseWidget; mDraggedWidget = baseWidget;
baseWidget->setItem(mItem.mBase); baseWidget->setItem(mItem.mBase);
baseWidget->setNeedMouseFocus(false); baseWidget->setNeedMouseFocus(false);
@ -99,8 +113,6 @@ namespace MWGui
std::string sound = mItem.mBase.getClass().getDownSoundId(mItem.mBase); std::string sound = mItem.mBase.getClass().getDownSoundId(mItem.mBase);
MWBase::Environment::get().getSoundManager()->playSound (sound, 1.0, 1.0); MWBase::Environment::get().getSoundManager()->playSound (sound, 1.0, 1.0);
mDragAndDropWidget->setVisible(false);
// If item is dropped where it was taken from, we don't need to do anything - // If item is dropped where it was taken from, we don't need to do anything -
// otherwise, do the transfer // otherwise, do the transfer
if (targetModel != mSourceModel) if (targetModel != mSourceModel)

@ -33,13 +33,14 @@ namespace MWGui
public: public:
bool mIsOnDragAndDrop; bool mIsOnDragAndDrop;
MyGUI::Widget* mDraggedWidget; MyGUI::Widget* mDraggedWidget;
MyGUI::Widget* mDragAndDropWidget;
ItemModel* mSourceModel; ItemModel* mSourceModel;
ItemView* mSourceView; ItemView* mSourceView;
SortFilterItemModel* mSourceSortModel; SortFilterItemModel* mSourceSortModel;
ItemStack mItem; ItemStack mItem;
int mDraggedCount; int mDraggedCount;
DragAndDrop();
void startDrag (int index, SortFilterItemModel* sortModel, ItemModel* sourceModel, ItemView* sourceView, int count); void startDrag (int index, SortFilterItemModel* sortModel, ItemModel* sourceModel, ItemView* sourceView, int count);
void drop (ItemModel* targetModel, ItemView* targetView); void drop (ItemModel* targetModel, ItemView* targetView);

@ -1,5 +1,7 @@
#include "controllers.hpp" #include "controllers.hpp"
#include <MyGUI_InputManager.h>
namespace MWGui namespace MWGui
{ {
namespace Controllers namespace Controllers
@ -50,5 +52,17 @@ namespace MWGui
{ {
} }
// -------------------------------------------------------------
void ControllerFollowMouse::prepareItem(MyGUI::Widget *_widget)
{
}
bool ControllerFollowMouse::addTime(MyGUI::Widget *_widget, float _time)
{
_widget->setPosition(MyGUI::InputManager::getInstance().getMousePosition());
return true;
}
} }
} }

@ -40,6 +40,17 @@ namespace MWGui
bool mEnabled; bool mEnabled;
float mTimeLeft; float mTimeLeft;
}; };
/// Automatically positions a widget below the mouse cursor.
class ControllerFollowMouse :
public MyGUI::ControllerItem
{
MYGUI_RTTI_DERIVED( ControllerFollowMouse )
private:
bool addTime(MyGUI::Widget* _widget, float _time);
void prepareItem(MyGUI::Widget* _widget);
};
} }
} }

@ -173,6 +173,7 @@ namespace MWGui
ItemWidget::registerComponents(); ItemWidget::registerComponents();
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Controllers::ControllerRepeatClick>("Controller"); MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Controllers::ControllerRepeatClick>("Controller");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Controllers::ControllerFollowMouse>("Controller");
MyGUI::FactoryManager::getInstance().registerFactory<ResourceImageSetPointerFix>("Resource", "ResourceImageSetPointer"); MyGUI::FactoryManager::getInstance().registerFactory<ResourceImageSetPointerFix>("Resource", "ResourceImageSetPointer");
MyGUI::ResourceManager::getInstance().load("core.xml"); MyGUI::ResourceManager::getInstance().load("core.xml");
@ -219,13 +220,7 @@ namespace MWGui
int w = MyGUI::RenderManager::getInstance().getViewSize().width; int w = MyGUI::RenderManager::getInstance().getViewSize().width;
int h = MyGUI::RenderManager::getInstance().getViewSize().height; int h = MyGUI::RenderManager::getInstance().getViewSize().height;
MyGUI::Widget* dragAndDropWidget = mGui->createWidgetT("Widget","",0,0,w,h,MyGUI::Align::Default,"DragAndDrop","DragAndDropWidget");
dragAndDropWidget->setVisible(false);
mDragAndDrop = new DragAndDrop(); mDragAndDrop = new DragAndDrop();
mDragAndDrop->mIsOnDragAndDrop = false;
mDragAndDrop->mDraggedWidget = 0;
mDragAndDrop->mDragAndDropWidget = dragAndDropWidget;
mRecharge = new Recharge(); mRecharge = new Recharge();
mMenu = new MainMenu(w,h); mMenu = new MainMenu(w,h);
@ -1048,7 +1043,6 @@ namespace MWGui
mBookWindow->center(); mBookWindow->center();
mQuickKeysMenu->center(); mQuickKeysMenu->center();
mSpellBuyingWindow->center(); mSpellBuyingWindow->center();
mDragAndDrop->mDragAndDropWidget->setSize(MyGUI::IntSize(x, y));
mInputBlocker->setSize(MyGUI::IntSize(x,y)); mInputBlocker->setSize(MyGUI::IntSize(x,y));
} }

Loading…
Cancel
Save