MyGUI_DDContainer.cpp
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_DDContainer.h"
#include "MyGUI_InputManager.h"
#include "MyGUI_LayerManager.h"
namespace MyGUI
{
void DDContainer::onMouseButtonPressed(int _left, int _top, MouseButton _id)
{
// смещение внутри виджета, куда кликнули мышкой
mClickInWidget = InputManager::getInstance().getLastPressedPosition(MouseButton::Left) - getAbsolutePosition();
mouseButtonPressed(_id);
Base::onMouseButtonPressed(_left, _top, _id);
}
void DDContainer::onMouseButtonReleased(int _left, int _top, MouseButton _id)
{
mouseButtonReleased(_id);
Base::onMouseButtonReleased(_left, _top, _id);
}
void DDContainer::onMouseDrag(int _left, int _top, MouseButton _id)
{
mouseDrag(_id);
Base::onMouseDrag(_left, _top, _id);
}
void DDContainer::mouseButtonPressed(MouseButton _id)
{
if (MouseButton::Left == _id)
{
// сбрасываем инфу для дропа
mDropResult = false;
mOldDrop = nullptr;
mDropInfo.reset();
mReseiverContainer = nullptr;
// сбрасываем, чтобы обновился дропный виджет
mStartDrop = false;
}
// если нажата другая клавиша и был дроп то сбрасываем
else
{
endDrop(true);
}
}
void DDContainer::mouseButtonReleased(MouseButton _id)
{
if (MouseButton::Left == _id)
{
endDrop(false);
}
}
void DDContainer::mouseDrag(MouseButton _id)
{
if (MouseButton::Left != _id)
return;
// нужно ли обновить данные
bool update = false;
// первый раз дропаем елемент
if (!mStartDrop && mDropSenderIndex != ITEM_NONE)
{
mStartDrop = true;
mNeedDrop = false;
update = true;
// запрос на нужность дропа по индексу
mDropInfo.set(this, mDropSenderIndex, nullptr, ITEM_NONE);
mReseiverContainer = nullptr;
eventStartDrag(this, mDropInfo, mNeedDrop);
if (mNeedDrop)
{
eventChangeDDState(this, DDItemState::Start);
}
else
{
// сбрасываем фокус мыши (не обязательно)
InputManager::getInstance().resetMouseCaptureWidget();
}
}
// дроп не нужен
if (!mNeedDrop)
{
return;
}
// делаем запрос, над кем наша мыша
const IntPoint& point = InputManager::getInstance().getMousePosition();
Widget* item = LayerManager::getInstance().getWidgetFromPoint(point.left, point.top);
updateDropItems();
// если равно, значит уже спрашивали
if (mOldDrop == item)
return;
mOldDrop = item;
// сбрасываем старую подсветку
if (mReseiverContainer)
mReseiverContainer->_setContainerItemInfo(mDropInfo.receiver_index, false, false);
mDropResult = false;
mReseiverContainer = nullptr;
Widget* receiver = nullptr;
// есть виджет под нами
if (item)
{
// делаем запрос на индекс по произвольному виджету
receiver = item->_getContainer();
// работаем только с контейнерами
if (receiver && receiver->isType<DDContainer>())
{
// подписываемся на информацию о валидности дропа
mReseiverContainer = static_cast<DDContainer*>(receiver);
mReseiverContainer->_eventInvalideContainer.clear();
mReseiverContainer->_eventInvalideContainer += newDelegate(this, &DDContainer::notifyInvalideDrop);
// делаем запрос на возможность дропа
size_t receiver_index = receiver->_getItemIndex(item);
mDropInfo.set(this, mDropSenderIndex, mReseiverContainer, receiver_index);
eventRequestDrop(this, mDropInfo, mDropResult);
// устанавливаем новую подсветку
mReseiverContainer->_setContainerItemInfo(mDropInfo.receiver_index, true, mDropResult);
}
else
{
mDropInfo.set(this, mDropSenderIndex, nullptr, ITEM_NONE);
}
}
// нет виджета под нами
else
{
mDropInfo.set(this, mDropSenderIndex, nullptr, ITEM_NONE);
}
DDItemState state;
DDWidgetState data(mDropSenderIndex);
data.update = update;
if (receiver == nullptr)
{
data.accept = false;
data.refuse = false;
state = DDItemState::Miss;
}
else if (mDropResult)
{
data.accept = true;
data.refuse = false;
state = DDItemState::Accept;
}
else
{
data.accept = false;
data.refuse = true;
state = DDItemState::Refuse;
}
updateDropItemsState(data);
eventChangeDDState(this, state);
}
void DDContainer::endDrop(bool _reset)
{
if (mStartDrop)
{
removeDropItems();
// сбрасываем старую подсветку
if (mReseiverContainer)
mReseiverContainer->_setContainerItemInfo(mDropInfo.receiver_index, false, false);
if (_reset)
mDropResult = false;
eventDropResult(this, mDropInfo, mDropResult);
eventChangeDDState(this, DDItemState::End);
// сбрасываем инфу для дропа
mStartDrop = false;
mDropResult = false;
mNeedDrop = false;
mOldDrop = nullptr;
mDropInfo.reset();
mReseiverContainer = nullptr;
mDropSenderIndex = ITEM_NONE;
}
}
void DDContainer::removeDropItems()
{
mDropItem = nullptr;
}
void DDContainer::updateDropItems()
{
if (mDropItem == nullptr)
{
requestDragWidgetInfo(this, mDropItem, mDropDimension);
}
const IntPoint& point = InputManager::getInstance().getMousePositionByLayer();
if (mDropItem)
{
mDropItem->setCoord(
point.left - mClickInWidget.left + mDropDimension.left,
point.top - mClickInWidget.top + mDropDimension.top,
mDropDimension.width,
mDropDimension.height);
mDropItem->setVisible(true);
}
}
void DDContainer::updateDropItemsState(const DDWidgetState& _state)
{
eventUpdateDropState(this, mDropItem, _state);
}
void DDContainer::notifyInvalideDrop(DDContainer* _sender)
{
mouseDrag(MouseButton::Left);
}
void DDContainer::setPropertyOverride(std::string_view _key, std::string_view _value)
{
if (_key == "NeedDragDrop")
setNeedDragDrop(utility::parseValue<bool>(_value));
else
{
Base::setPropertyOverride(_key, _value);
return;
}
eventChangeProperty(this, _key, _value);
}
void DDContainer::setNeedDragDrop(bool _value)
{
mNeedDragDrop = _value;
}
bool DDContainer::getNeedDragDrop() const
{
return mNeedDragDrop;
}
void DDContainer::_setContainerItemInfo(size_t _index, bool _set, bool _accept)
{
}
void DDContainer::resetDrag()
{
_resetContainer(false);
}
} // namespace MyGUI
MyGUI_DDContainer.h
MyGUI_InputManager.h
MyGUI_LayerManager.h
MyGUI_Precompiled.h
MyGUI::DDContainer
widget description should be here.
Definition MyGUI_DDContainer.h:31
MyGUI::DDContainer::mDropInfo
DDItemInfo mDropInfo
Definition MyGUI_DDContainer.h:129
MyGUI::DDContainer::onMouseDrag
void onMouseDrag(int _left, int _top, MouseButton _id) override
Definition MyGUI_DDContainer.cpp:32
MyGUI::DDContainer::eventRequestDrop
EventHandle_DDContainerPtrCDDItemInfoRefBoolRef eventRequestDrop
Definition MyGUI_DDContainer.h:59
MyGUI::DDContainer::notifyInvalideDrop
virtual void notifyInvalideDrop(DDContainer *_sender)
Definition MyGUI_DDContainer.cpp:240
MyGUI::DDContainer::eventStartDrag
EventHandle_DDContainerPtrCDDItemInfoRefBoolRef eventStartDrag
Definition MyGUI_DDContainer.h:51
MyGUI::DDContainer::_setContainerItemInfo
virtual void _setContainerItemInfo(size_t _index, bool _set, bool _accept)
Definition MyGUI_DDContainer.cpp:270
MyGUI::DDContainer::mNeedDragDrop
bool mNeedDragDrop
Definition MyGUI_DDContainer.h:140
MyGUI::DDContainer::mOldDrop
Widget * mOldDrop
Definition MyGUI_DDContainer.h:127
MyGUI::DDContainer::requestDragWidgetInfo
EventHandle_EventHandle_DDContainerPtrWidgetPtrRefIntCoordRef requestDragWidgetInfo
Definition MyGUI_DDContainer.h:82
MyGUI::DDContainer::mReseiverContainer
DDContainer * mReseiverContainer
Definition MyGUI_DDContainer.h:142
MyGUI::DDContainer::getNeedDragDrop
bool getNeedDragDrop() const
Definition MyGUI_DDContainer.cpp:265
MyGUI::DDContainer::onMouseButtonPressed
void onMouseButtonPressed(int _left, int _top, MouseButton _id) override
Definition MyGUI_DDContainer.cpp:15
MyGUI::DDContainer::mDropSenderIndex
size_t mDropSenderIndex
Definition MyGUI_DDContainer.h:131
MyGUI::DDContainer::setPropertyOverride
void setPropertyOverride(std::string_view _key, std::string_view _value) override
Definition MyGUI_DDContainer.cpp:245
MyGUI::DDContainer::mouseButtonPressed
void mouseButtonPressed(MouseButton _id)
Definition MyGUI_DDContainer.cpp:39
MyGUI::DDContainer::updateDropItems
virtual void updateDropItems()
Definition MyGUI_DDContainer.cpp:215
MyGUI::DDContainer::mStartDrop
bool mStartDrop
Definition MyGUI_DDContainer.h:125
MyGUI::DDContainer::eventChangeDDState
EventHandle_EventHandle_DDContainerPtrDDItemState eventChangeDDState
Definition MyGUI_DDContainer.h:74
MyGUI::DDContainer::onMouseButtonReleased
void onMouseButtonReleased(int _left, int _top, MouseButton _id) override
Definition MyGUI_DDContainer.cpp:25
MyGUI::DDContainer::mClickInWidget
IntPoint mClickInWidget
Definition MyGUI_DDContainer.h:137
MyGUI::DDContainer::mouseButtonReleased
void mouseButtonReleased(MouseButton _id)
Definition MyGUI_DDContainer.cpp:59
MyGUI::DDContainer::mDropResult
bool mDropResult
Definition MyGUI_DDContainer.h:123
MyGUI::DDContainer::mDropItem
Widget * mDropItem
Definition MyGUI_DDContainer.h:134
MyGUI::DDContainer::removeDropItems
virtual void removeDropItems()
Definition MyGUI_DDContainer.cpp:210
MyGUI::DDContainer::mNeedDrop
bool mNeedDrop
Definition MyGUI_DDContainer.h:124
MyGUI::DDContainer::setNeedDragDrop
void setNeedDragDrop(bool _value)
Definition MyGUI_DDContainer.cpp:260
MyGUI::DDContainer::updateDropItemsState
virtual void updateDropItemsState(const DDWidgetState &_state)
Definition MyGUI_DDContainer.cpp:235
MyGUI::DDContainer::eventDropResult
EventHandle_DDContainerPtrCDDItemInfoRefBool eventDropResult
Definition MyGUI_DDContainer.h:67
MyGUI::DDContainer::mDropDimension
IntCoord mDropDimension
Definition MyGUI_DDContainer.h:135
MyGUI::DDContainer::resetDrag
void resetDrag()
Definition MyGUI_DDContainer.cpp:274
MyGUI::DDContainer::mouseDrag
void mouseDrag(MouseButton _id)
Definition MyGUI_DDContainer.cpp:67
MyGUI::DDContainer::endDrop
void endDrop(bool _reset)
Definition MyGUI_DDContainer.cpp:184
MyGUI::DDContainer::eventUpdateDropState
delegates::MultiDelegate< DDContainer *, Widget *, const DDWidgetState & > eventUpdateDropState
Definition MyGUI_DDContainer.h:101
MyGUI::ICroppedRectangle::getAbsolutePosition
const IntPoint & getAbsolutePosition() const
Definition MyGUI_ICroppedRectangle.h:62
MyGUI::InputManager::getLastPressedPosition
const IntPoint & getLastPressedPosition(MouseButton _id) const
Definition MyGUI_InputManager.cpp:648
MyGUI::InputManager::getMousePosition
const IntPoint & getMousePosition() const
Definition MyGUI_InputManager.cpp:657
MyGUI::InputManager::getInstance
static InputManager & getInstance()
MyGUI::InputManager::resetMouseCaptureWidget
void resetMouseCaptureWidget()
Definition MyGUI_InputManager.cpp:687
MyGUI::InputManager::getMousePositionByLayer
IntPoint getMousePositionByLayer() const
Definition MyGUI_InputManager.cpp:606
MyGUI::LayerManager::getInstance
static LayerManager & getInstance()
MyGUI::LayerManager::getWidgetFromPoint
Widget * getWidgetFromPoint(int _left, int _top) const
Definition MyGUI_LayerManager.cpp:211
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:40
MyGUI::Widget::isType
virtual bool isType(const std::type_info &_type) const override
Definition MyGUI_Widget.h:44
MyGUI::Widget::eventChangeProperty
EventHandle_WidgetStringString eventChangeProperty
Definition MyGUI_Widget.h:324
MyGUI::Widget::_resetContainer
virtual void _resetContainer(bool _update)
Definition MyGUI_Widget.cpp:1092
MyGUI::Widget::_getContainer
Widget * _getContainer() const
Definition MyGUI_Widget.cpp:1392
MyGUI::Widget::_getItemIndex
virtual size_t _getItemIndex(Widget *_item) const
Definition MyGUI_Widget.cpp:1382
MyGUI::utility::parseValue
T parseValue(std::string_view _value)
Definition MyGUI_StringUtility.h:60
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::IntPoint
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:27
MyGUI::ITEM_NONE
constexpr size_t ITEM_NONE
Definition MyGUI_Macros.h:19
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100
MyGUI::DDItemState
Definition MyGUI_DDItemInfo.h:17
MyGUI::DDItemState::End
@ End
Definition MyGUI_DDItemInfo.h:22
MyGUI::DDItemState::Accept
@ Accept
Definition MyGUI_DDItemInfo.h:24
MyGUI::DDItemState::Start
@ Start
Definition MyGUI_DDItemInfo.h:21
MyGUI::DDItemState::Refuse
@ Refuse
Definition MyGUI_DDItemInfo.h:25
MyGUI::DDItemState::Miss
@ Miss
Definition MyGUI_DDItemInfo.h:23
MyGUI::DDWidgetState
Definition MyGUI_DDItemInfo.h:100
MyGUI::DDWidgetState::update
bool update
Definition MyGUI_DDItemInfo.h:109
MyGUI::DDWidgetState::accept
bool accept
Definition MyGUI_DDItemInfo.h:111
MyGUI::DDWidgetState::refuse
bool refuse
Definition MyGUI_DDItemInfo.h:113
MyGUI::MouseButton
Definition MyGUI_MouseButton.h:16
MyGUI::MouseButton::Left
@ Left
Definition MyGUI_MouseButton.h:21
MyGUI::types::TPoint::left
T left
Definition MyGUI_TPoint.h:19
MyGUI::types::TPoint::top
T top
Definition MyGUI_TPoint.h:20