| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474 |
- #include "BsGUIManager.h"
- #include "BsGUIWidget.h"
- #include "BsGUIElement.h"
- #include "BsImageSprite.h"
- #include "BsSpriteTexture.h"
- #include "CmTime.h"
- #include "CmSceneObject.h"
- #include "CmMaterial.h"
- #include "CmMeshData.h"
- #include "CmVertexDataDesc.h"
- #include "CmMesh.h"
- #include "CmUtil.h"
- #include "CmRenderWindowManager.h"
- #include "CmPlatform.h"
- #include "CmRectI.h"
- #include "CmApplication.h"
- #include "CmException.h"
- #include "CmInput.h"
- #include "CmPass.h"
- #include "CmDebug.h"
- #include "CmRenderQueue.h"
- #include "BsGUIInputCaret.h"
- #include "BsGUIInputSelection.h"
- #include "BsGUIListBox.h"
- #include "BsGUIButton.h"
- #include "BsGUIDropDownBox.h"
- #include "BsGUIContextMenu.h"
- #include "BsDragAndDropManager.h"
- #include "BsGUIDropDownBoxManager.h"
- #include "BsGUIContextMenu.h"
- #include "CmProfiler.h"
- #include "CmMeshHeap.h"
- #include "CmTransientMesh.h"
- #include "BsVirtualInput.h"
- #include "BsCursor.h"
- using namespace CamelotFramework;
- using namespace std::placeholders;
- namespace BansheeEngine
- {
- struct GUIGroupElement
- {
- GUIGroupElement()
- { }
- GUIGroupElement(GUIElement* _element, UINT32 _renderElement)
- :element(_element), renderElement(_renderElement)
- { }
- GUIElement* element;
- UINT32 renderElement;
- };
- struct GUIMaterialGroup
- {
- GUIMaterialInfo matInfo;
- UINT32 numQuads;
- UINT32 depth;
- RectI bounds;
- Vector<GUIGroupElement>::type elements;
- };
- const UINT32 GUIManager::DRAG_DISTANCE = 3;
- const UINT32 GUIManager::MESH_HEAP_INITIAL_NUM_VERTS = 16384;
- const UINT32 GUIManager::MESH_HEAP_INITIAL_NUM_INDICES = 49152;
- GUIManager::GUIManager()
- :mSeparateMeshesByWidget(true), mActiveMouseButton(GUIMouseButton::Left),
- mCaretBlinkInterval(0.5f), mCaretLastBlinkTime(0.0f), mCaretColor(1.0f, 0.6588f, 0.0f), mIsCaretOn(false),
- mTextSelectionColor(1.0f, 0.6588f, 0.0f), mInputCaret(nullptr), mInputSelection(nullptr), mDragState(DragState::NoDrag),
- mActiveCursor(CursorType::Arrow)
- {
- mOnCursorMovedConn = gInput().onCursorMoved.connect(std::bind(&GUIManager::onCursorMoved, this, _1));
- mOnCursorPressedConn = gInput().onCursorPressed.connect(std::bind(&GUIManager::onCursorPressed, this, _1));
- mOnCursorReleasedConn = gInput().onCursorReleased.connect(std::bind(&GUIManager::onCursorReleased, this, _1));
- mOnCursorDoubleClick = gInput().onDoubleClick.connect(std::bind(&GUIManager::onCursorDoubleClick, this, _1));
- mOnTextInputConn = gInput().onCharInput.connect(std::bind(&GUIManager::onTextInput, this, _1));
- mOnInputCommandConn = gInput().onInputCommand.connect(std::bind(&GUIManager::onInputCommandEntered, this, _1));
- mOnVirtualButtonDown = VirtualInput::instance().onButtonDown.connect(std::bind(&GUIManager::onVirtualButtonDown, this, _1));
- mWindowGainedFocusConn = RenderWindowManager::instance().onFocusGained.connect(std::bind(&GUIManager::onWindowFocusGained, this, _1));
- mWindowLostFocusConn = RenderWindowManager::instance().onFocusLost.connect(std::bind(&GUIManager::onWindowFocusLost, this, _1));
- mMouseLeftWindowConn = Platform::onMouseLeftWindow.connect(std::bind(&GUIManager::onMouseLeftWindow, this, _1));
- mInputCaret = cm_new<GUIInputCaret, PoolAlloc>();
- mInputSelection = cm_new<GUIInputSelection, PoolAlloc>();
- DragAndDropManager::startUp(cm_new<DragAndDropManager>());
- mDragEndedConn = DragAndDropManager::instance().onDragEnded.connect(std::bind(&GUIManager::onMouseDragEnded, this, _1));
- GUIDropDownBoxManager::startUp(cm_new<GUIDropDownBoxManager>());
- mVertexDesc = cm_shared_ptr<VertexDataDesc>();
- mVertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
- mVertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
- mMeshHeap = MeshHeap::create(MESH_HEAP_INITIAL_NUM_VERTS, MESH_HEAP_INITIAL_NUM_INDICES, mVertexDesc);
- // Need to defer this call because I want to make sure all managers are initialized first
- deferredCall(std::bind(&GUIManager::updateCaretTexture, this));
- deferredCall(std::bind(&GUIManager::updateTextSelectionTexture, this));
- }
- GUIManager::~GUIManager()
- {
- GUIDropDownBoxManager::shutDown();
- DragAndDropManager::shutDown();
- // Make a copy of widgets, since destroying them will remove them from mWidgets and
- // we can't iterate over an array thats getting modified
- Vector<WidgetInfo>::type widgetCopy = mWidgets;
- for(auto& widget : widgetCopy)
- widget.widget->destroy();
- mOnCursorPressedConn.disconnect();
- mOnCursorReleasedConn.disconnect();
- mOnCursorMovedConn.disconnect();
- mOnCursorDoubleClick.disconnect();
- mOnTextInputConn.disconnect();
- mOnInputCommandConn.disconnect();
- mOnVirtualButtonDown.disconnect();
- mDragEndedConn.disconnect();
- mWindowGainedFocusConn.disconnect();
- mWindowLostFocusConn.disconnect();
- mMouseLeftWindowConn.disconnect();
- cm_delete<PoolAlloc>(mInputCaret);
- cm_delete<PoolAlloc>(mInputSelection);
- }
- void GUIManager::registerWidget(GUIWidget* widget)
- {
- mWidgets.push_back(WidgetInfo(widget));
- const Viewport* renderTarget = widget->getTarget();
- auto findIter = mCachedGUIData.find(renderTarget);
- if(findIter == end(mCachedGUIData))
- mCachedGUIData[renderTarget] = GUIRenderData();
- GUIRenderData& windowData = mCachedGUIData[renderTarget];
- windowData.widgets.push_back(widget);
- windowData.isDirty = true;
- }
- void GUIManager::unregisterWidget(GUIWidget* widget)
- {
- {
- auto findIter = std::find_if(begin(mWidgets), end(mWidgets), [=] (const WidgetInfo& x) { return x.widget == widget; } );
- if(findIter != mWidgets.end())
- mWidgets.erase(findIter);
- }
- const Viewport* renderTarget = widget->getTarget();
- GUIRenderData& renderData = mCachedGUIData[renderTarget];
- {
- auto findIter = std::find(begin(renderData.widgets), end(renderData.widgets), widget);
-
- if(findIter != end(renderData.widgets))
- renderData.widgets.erase(findIter);
- }
- if(renderData.widgets.size() == 0)
- {
- mCachedGUIData.erase(renderTarget);
- }
- else
- renderData.isDirty = true;
- }
- void GUIManager::update()
- {
- DragAndDropManager::instance().update();
- // Update layouts
- gProfiler().beginSample("UpdateLayout");
- for(auto& widgetInfo : mWidgets)
- {
- widgetInfo.widget->_updateLayout();
- }
- gProfiler().endSample("UpdateLayout");
- // Blink caret
- float curTime = gTime().getTime();
- if((curTime - mCaretLastBlinkTime) >= mCaretBlinkInterval)
- {
- mCaretLastBlinkTime = curTime;
- mIsCaretOn = !mIsCaretOn;
- mCommandEvent = GUICommandEvent();
- mCommandEvent.setType(GUICommandEventType::Redraw);
- for(auto& elementInfo : mElementsInFocus)
- {
- sendCommandEvent(elementInfo.widget, elementInfo.element, mCommandEvent);
- }
- }
- PROFILE_CALL(updateMeshes(), "UpdateMeshes");
- mNewElementsUnderCursor.clear();
- for(auto& elementInfo : mElementsUnderCursor)
- {
- if(!elementInfo.element->_isDestroyed())
- mNewElementsUnderCursor.push_back(elementInfo);
- }
- mElementsUnderCursor.swap(mNewElementsUnderCursor);
- mNewActiveElements.clear();
- for(auto& elementInfo : mActiveElements)
- {
- if(!elementInfo.element->_isDestroyed())
- mNewActiveElements.push_back(elementInfo);
- }
- mActiveElements.swap(mNewActiveElements);
- mNewElementsInFocus.clear();
- for(auto& elementInfo : mElementsInFocus)
- {
- if(!elementInfo.element->_isDestroyed())
- mNewElementsInFocus.push_back(elementInfo);
- }
- mElementsInFocus.swap(mNewElementsInFocus);
- for(auto& focusElementInfo : mForcedFocusElements)
- {
- if(focusElementInfo.element->_isDestroyed())
- continue;
- if(focusElementInfo.focus)
- {
- auto iterFind = std::find_if(mElementsInFocus.begin(), mElementsInFocus.end(),
- [&](const ElementInfo& x) { return x.element == focusElementInfo.element; });
- if(iterFind == mElementsInFocus.end())
- {
- mElementsInFocus.push_back(ElementInfo(focusElementInfo.element, focusElementInfo.element->_getParentWidget()));
- mCommandEvent = GUICommandEvent();
- mCommandEvent.setType(GUICommandEventType::FocusGained);
- sendCommandEvent(focusElementInfo.element->_getParentWidget(), focusElementInfo.element, mCommandEvent);
- }
- }
- else
- {
- mNewElementsInFocus.clear();
- for(auto& elementInfo : mElementsInFocus)
- {
- if(elementInfo.element == focusElementInfo.element)
- {
- mCommandEvent = GUICommandEvent();
- mCommandEvent.setType(GUICommandEventType::FocusLost);
- sendCommandEvent(elementInfo.widget, elementInfo.element, mCommandEvent);
- }
- else
- mNewElementsInFocus.push_back(elementInfo);
- }
- mElementsInFocus.swap(mNewElementsInFocus);
- }
- }
- mForcedFocusElements.clear();
- processDestroyQueue();
- }
- void GUIManager::render(ViewportPtr& target, CM::RenderQueue& renderQueue) const
- {
- auto findIter = mCachedGUIData.find(target.get());
- if(findIter == mCachedGUIData.end())
- return;
- const GUIRenderData& renderData = findIter->second;
- // Render the meshes
- if(mSeparateMeshesByWidget)
- {
- // TODO - Possible optimization. I currently divide by width/height inside the shader, while it
- // might be more optimal to just scale the mesh as the resolution changes?
- float invViewportWidth = 1.0f / (target->getWidth() * 0.5f);
- float invViewportHeight = 1.0f / (target->getHeight() * 0.5f);
- UINT32 meshIdx = 0;
- for(auto& mesh : renderData.cachedMeshes)
- {
- GUIMaterialInfo materialInfo = renderData.cachedMaterials[meshIdx];
- GUIWidget* widget = renderData.cachedWidgetsPerMesh[meshIdx];
- if(materialInfo.material == nullptr || !materialInfo.material.isLoaded())
- {
- meshIdx++;
- continue;
- }
- if(mesh == nullptr)
- {
- meshIdx++;
- continue;
- }
- materialInfo.invViewportWidth.set(invViewportWidth);
- materialInfo.invViewportHeight.set(invViewportHeight);
- materialInfo.worldTransform.set(widget->SO()->getWorldTfrm());
- renderQueue.add(materialInfo.material.getInternalPtr(), mesh, 0, Vector3::ZERO);
- meshIdx++;
- }
- }
- else
- {
- // TODO: I want to avoid separating meshes by widget in the future. On DX11 and GL I can set up a shader
- // that accepts multiple world transforms (one for each widget). Then I can add some instance information to vertices
- // and render elements using multiple different transforms with a single call.
- // Separating meshes can then be used as a compatibility mode for DX9
- CM_EXCEPT(NotImplementedException, "Not implemented");
- }
- }
- void GUIManager::updateMeshes()
- {
- for(auto& cachedMeshData : mCachedGUIData)
- {
- GUIRenderData& renderData = cachedMeshData.second;
- gProfiler().beginSample("UM_A");
- // Check if anything is dirty. If nothing is we can skip the update
- bool isDirty = renderData.isDirty;
- renderData.isDirty = false;
- for(auto& widget : renderData.widgets)
- {
- if(widget->isDirty(true))
- {
- isDirty = true;
- }
- }
- gProfiler().endSample("UM_A");
- if(!isDirty)
- continue;
- // Make a list of all GUI elements, sorted from farthest to nearest (highest depth to lowest)
- auto elemComp = [](const GUIGroupElement& a, const GUIGroupElement& b)
- {
- UINT32 aDepth = a.element->_getRenderElementDepth(a.renderElement);
- UINT32 bDepth = b.element->_getRenderElementDepth(b.renderElement);
- // Compare pointers just to differentiate between two elements with the same depth, their order doesn't really matter, but std::set
- // requires all elements to be unique
- return (aDepth > bDepth) ||
- (aDepth == bDepth && a.element > b.element) ||
- (aDepth == bDepth && a.element == b.element && a.renderElement > b.renderElement);
- };
- Set<GUIGroupElement, std::function<bool(const GUIGroupElement&, const GUIGroupElement&)>>::type allElements(elemComp);
- for(auto& widget : renderData.widgets)
- {
- const Vector<GUIElement*>::type& elements = widget->getElements();
- for(auto& element : elements)
- {
- if(element->_isDisabled())
- continue;
- UINT32 numRenderElems = element->getNumRenderElements();
- for(UINT32 i = 0; i < numRenderElems; i++)
- {
- allElements.insert(GUIGroupElement(element, i));
- }
- }
- }
- // Group the elements in such a way so that we end up with a smallest amount of
- // meshes, without breaking back to front rendering order
- UnorderedMap<UINT64, Vector<GUIMaterialGroup>::type>::type materialGroups;
- for(auto& elem : allElements)
- {
- GUIElement* guiElem = elem.element;
- UINT32 renderElemIdx = elem.renderElement;
- UINT32 elemDepth = guiElem->_getRenderElementDepth(renderElemIdx);
- RectI tfrmedBounds = guiElem->_getClippedBounds();
- tfrmedBounds.transform(guiElem->_getParentWidget()->SO()->getWorldTfrm());
- const GUIMaterialInfo& matInfo = guiElem->getMaterial(renderElemIdx);
- UINT64 materialId = matInfo.material->getInternalID(); // TODO - I group based on material ID. So if two widgets used exact copies of the same material
- // this system won't detect it. Find a better way of determining material similarity?
- // If this is a new material, add a new list of groups
- auto findIterMaterial = materialGroups.find(materialId);
- if(findIterMaterial == end(materialGroups))
- materialGroups[materialId] = Vector<GUIMaterialGroup>::type();
- // Try to find a group this material will fit in:
- // - Group that has a depth value same or one below elements depth will always be a match
- // - Otherwise, we search higher depth values as well, but we only use them if no elements in between those depth values
- // overlap the current elements bounds.
- Vector<GUIMaterialGroup>::type& allGroups = materialGroups[materialId];
- GUIMaterialGroup* foundGroup = nullptr;
- for(auto groupIter = allGroups.rbegin(); groupIter != allGroups.rend(); ++groupIter)
- {
- // If we separate meshes by widget, ignore any groups with widget parents other than mine
- if(mSeparateMeshesByWidget)
- {
- if(groupIter->elements.size() > 0)
- {
- GUIElement* otherElem = groupIter->elements.begin()->element; // We only need to check the first element
- if(otherElem->_getParentWidget() != guiElem->_getParentWidget())
- continue;
- }
- }
- GUIMaterialGroup& group = *groupIter;
- if(group.depth == elemDepth || group.depth == (elemDepth - 1))
- {
- foundGroup = &group;
- break;
- }
- else
- {
- UINT32 startDepth = elemDepth;
- UINT32 endDepth = group.depth;
- RectI potentialGroupBounds = group.bounds;
- potentialGroupBounds.encapsulate(tfrmedBounds);
- bool foundOverlap = false;
- for(auto& material : materialGroups)
- {
- for(auto& matGroup : material.second)
- {
- if(&matGroup == &group)
- continue;
- if(matGroup.depth > startDepth && matGroup.depth < endDepth)
- {
- if(matGroup.bounds.overlaps(potentialGroupBounds))
- {
- foundOverlap = true;
- break;
- }
- }
- }
- }
- if(!foundOverlap)
- {
- foundGroup = &group;
- break;
- }
- }
- }
- if(foundGroup == nullptr)
- {
- allGroups.push_back(GUIMaterialGroup());
- foundGroup = &allGroups[allGroups.size() - 1];
- foundGroup->depth = elemDepth;
- foundGroup->bounds = tfrmedBounds;
- foundGroup->elements.push_back(GUIGroupElement(guiElem, renderElemIdx));
- foundGroup->matInfo = matInfo;
- foundGroup->numQuads = guiElem->getNumQuads(renderElemIdx);
- }
- else
- {
- foundGroup->bounds.encapsulate(tfrmedBounds);
- foundGroup->elements.push_back(GUIGroupElement(guiElem, renderElemIdx));
- foundGroup->depth = std::min(foundGroup->depth, elemDepth);
- foundGroup->numQuads += guiElem->getNumQuads(renderElemIdx);
- }
- }
- // Make a list of all GUI elements, sorted from farthest to nearest (highest depth to lowest)
- auto groupComp = [](GUIMaterialGroup* a, GUIMaterialGroup* b)
- {
- return (a->depth > b->depth) || (a->depth == b->depth && a > b);
- // Compare pointers just to differentiate between two elements with the same depth, their order doesn't really matter, but std::set
- // requires all elements to be unique
- };
- Set<GUIMaterialGroup*, std::function<bool(GUIMaterialGroup*, GUIMaterialGroup*)>>::type sortedGroups(groupComp);
- for(auto& material : materialGroups)
- {
- for(auto& group : material.second)
- {
- sortedGroups.insert(&group);
- }
- }
- UINT32 numMeshes = (UINT32)sortedGroups.size();
- UINT32 oldNumMeshes = (UINT32)renderData.cachedMeshes.size();
- if(numMeshes < oldNumMeshes)
- {
- renderData.cachedMeshes.resize(numMeshes);
- }
- renderData.cachedMaterials.resize(numMeshes);
- if(mSeparateMeshesByWidget)
- renderData.cachedWidgetsPerMesh.resize(numMeshes);
- // Fill buffers for each group and update their meshes
- UINT32 groupIdx = 0;
- for(auto& group : sortedGroups)
- {
- renderData.cachedMaterials[groupIdx] = group->matInfo;
- if(mSeparateMeshesByWidget)
- {
- if(group->elements.size() == 0)
- renderData.cachedWidgetsPerMesh[groupIdx] = nullptr;
- else
- {
- GUIElement* elem = group->elements.begin()->element;
- renderData.cachedWidgetsPerMesh[groupIdx] = elem->_getParentWidget();
- }
- }
- MeshDataPtr meshData = cm_shared_ptr<MeshData, PoolAlloc>(group->numQuads * 4, group->numQuads * 6, mVertexDesc);
- UINT8* vertices = meshData->getElementData(VES_POSITION);
- UINT8* uvs = meshData->getElementData(VES_TEXCOORD);
- UINT32* indices = meshData->getIndices32();
- UINT32 vertexStride = meshData->getVertexDesc()->getVertexStride();
- UINT32 indexStride = meshData->getIndexElementSize();
- UINT32 quadOffset = 0;
- for(auto& matElement : group->elements)
- {
- matElement.element->fillBuffer(vertices, uvs, indices, quadOffset, group->numQuads, vertexStride, indexStride, matElement.renderElement);
- UINT32 numQuads = matElement.element->getNumQuads(matElement.renderElement);
- UINT32 indexStart = quadOffset * 6;
- UINT32 indexEnd = indexStart + numQuads * 6;
- UINT32 vertOffset = quadOffset * 4;
- for(UINT32 i = indexStart; i < indexEnd; i++)
- indices[i] += vertOffset;
- quadOffset += numQuads;
- }
- if(groupIdx < (UINT32)renderData.cachedMeshes.size())
- {
- mMeshHeap->dealloc(renderData.cachedMeshes[groupIdx]);
- renderData.cachedMeshes[groupIdx] = mMeshHeap->alloc(meshData);
- }
- else
- {
- renderData.cachedMeshes.push_back(mMeshHeap->alloc(meshData));
- }
- groupIdx++;
- }
- }
- }
- void GUIManager::updateCaretTexture()
- {
- if(mCaretTexture == nullptr)
- {
- HTexture newTex = Texture::create(TEX_TYPE_2D, 1, 1, 0, PF_R8G8B8A8);
- newTex->synchronize(); // TODO - Required due to a bug in allocateSubresourceBuffer
- mCaretTexture = SpriteTexture::create(newTex);
- }
- const HTexture& tex = mCaretTexture->getTexture();
- UINT32 subresourceIdx = tex->mapToSubresourceIdx(0, 0);
- PixelDataPtr data = tex->allocateSubresourceBuffer(subresourceIdx);
- data->setColorAt(mCaretColor, 0, 0);
- gCoreAccessor().writeSubresource(tex.getInternalPtr(), tex->mapToSubresourceIdx(0, 0), data);
- }
- void GUIManager::updateTextSelectionTexture()
- {
- if(mTextSelectionTexture == nullptr)
- {
- HTexture newTex = Texture::create(TEX_TYPE_2D, 1, 1, 0, PF_R8G8B8A8);
- newTex->synchronize(); // TODO - Required due to a bug in allocateSubresourceBuffer
- mTextSelectionTexture = SpriteTexture::create(newTex);
- }
- const HTexture& tex = mTextSelectionTexture->getTexture();
- UINT32 subresourceIdx = tex->mapToSubresourceIdx(0, 0);
- PixelDataPtr data = tex->allocateSubresourceBuffer(subresourceIdx);
- data->setColorAt(mTextSelectionColor, 0, 0);
- gCoreAccessor().writeSubresource(tex.getInternalPtr(), tex->mapToSubresourceIdx(0, 0), data);
- }
- bool GUIManager::onMouseDragEnded(const CM::PositionalInputEvent& event)
- {
- GUIMouseButton guiButton = buttonToGUIButton(event.button);
- if(DragAndDropManager::instance().isDragInProgress() && guiButton == GUIMouseButton::Left)
- {
- for(auto& elementInfo : mElementsUnderCursor)
- {
- Vector2I localPos;
- if(elementInfo.widget != nullptr)
- localPos = getWidgetRelativePos(*elementInfo.widget, event.screenPos);
- bool acceptDrop = true;
- if(DragAndDropManager::instance().needsValidDropTarget())
- {
- acceptDrop = elementInfo.element->_acceptDragAndDrop(localPos, DragAndDropManager::instance().getDragTypeId());
- }
- if(acceptDrop)
- {
- mMouseEvent.setDragAndDropDroppedData(localPos, DragAndDropManager::instance().getDragTypeId(), DragAndDropManager::instance().getDragData());
- bool processed = sendMouseEvent(elementInfo.widget, elementInfo.element, mMouseEvent);
- if(processed)
- return true;
- }
- }
- }
- return false;
- }
- void GUIManager::onCursorMoved(const PositionalInputEvent& event)
- {
- if(event.isUsed())
- return;
- bool buttonStates[(int)GUIMouseButton::Count];
- buttonStates[0] = event.buttonStates[0];
- buttonStates[1] = event.buttonStates[1];
- buttonStates[2] = event.buttonStates[2];
- if(findElementUnderCursor(event.screenPos, buttonStates, event.shift, event.control, event.alt))
- event.markAsUsed();
- if(mDragState == DragState::HeldWithoutDrag)
- {
- UINT32 dist = mLastCursorClickPos.manhattanDist(event.screenPos);
- if(dist > DRAG_DISTANCE)
- {
- for(auto& activeElement : mActiveElements)
- {
- Vector2I localPos = getWidgetRelativePos(*activeElement.widget, event.screenPos);
- mMouseEvent.setMouseDragStartData(localPos);
- if(sendMouseEvent(activeElement.widget, activeElement.element, mMouseEvent))
- event.markAsUsed();
- }
- mDragState = DragState::Dragging;
- }
- }
- // If mouse is being held down send MouseDrag events
- if(mDragState == DragState::Dragging)
- {
- for(auto& activeElement : mActiveElements)
- {
- if(mLastCursorScreenPos != event.screenPos)
- {
- Vector2I localPos = getWidgetRelativePos(*activeElement.widget, event.screenPos);
- mMouseEvent.setMouseDragData(localPos, localPos - mLastCursorScreenPos);
- if(sendMouseEvent(activeElement.widget, activeElement.element, mMouseEvent))
- event.markAsUsed();
- }
- }
- mLastCursorScreenPos = event.screenPos;
- // Also if drag is in progress send DragAndDrop events
- if(DragAndDropManager::instance().isDragInProgress())
- {
- bool acceptDrop = true;
- for(auto& elementInfo : mElementsUnderCursor)
- {
- Vector2I localPos = getWidgetRelativePos(*elementInfo.widget, event.screenPos);
- acceptDrop = true;
- if(DragAndDropManager::instance().needsValidDropTarget())
- {
- acceptDrop = elementInfo.element->_acceptDragAndDrop(localPos, DragAndDropManager::instance().getDragTypeId());
- }
- if(acceptDrop)
- {
- mMouseEvent.setDragAndDropDraggedData(localPos, DragAndDropManager::instance().getDragTypeId(), DragAndDropManager::instance().getDragData());
- if(sendMouseEvent(elementInfo.widget, elementInfo.element, mMouseEvent))
- {
- event.markAsUsed();
- break;
- }
- }
- }
- if(acceptDrop)
- {
- if(mActiveCursor != CursorType::ArrowDrag)
- {
- Cursor::instance().setCursor(CursorType::ArrowDrag);
- mActiveCursor = CursorType::ArrowDrag;
- }
- }
- else
- {
- if(mActiveCursor != CursorType::Deny)
- {
- Cursor::instance().setCursor(CursorType::Deny);
- mActiveCursor = CursorType::Deny;
- }
- }
- }
- }
- else // Otherwise, send MouseMove events if we are hovering over any element
- {
- if(mLastCursorScreenPos != event.screenPos)
- {
- bool moveProcessed = false;
- bool hasCustomCursor = false;
- for(auto& elementInfo : mElementsUnderCursor)
- {
- Vector2I localPos = getWidgetRelativePos(*elementInfo.widget, event.screenPos);
- if(!moveProcessed)
- {
- // Send MouseMove event
- mMouseEvent.setMouseMoveData(localPos);
- moveProcessed = sendMouseEvent(elementInfo.widget, elementInfo.element, mMouseEvent);
- if(moveProcessed)
- {
- event.markAsUsed();
- break;
- }
- }
- if(!hasCustomCursor)
- {
- CursorType newCursor = CursorType::Arrow;
- if(elementInfo.element->_hasCustomCursor(localPos, newCursor))
- {
- if(newCursor != mActiveCursor)
- {
- Cursor::instance().setCursor(newCursor);
- mActiveCursor = newCursor;
- }
- hasCustomCursor = true;
- }
- }
- if(moveProcessed && hasCustomCursor)
- break;
- }
- if(!hasCustomCursor)
- {
- if(mActiveCursor != CursorType::Arrow)
- {
- Cursor::instance().setCursor(CursorType::Arrow);
- mActiveCursor = CursorType::Arrow;
- }
- }
- }
- mLastCursorScreenPos = event.screenPos;
- if(Math::abs(event.mouseWheelScrollAmount) > 0.00001f)
- {
- for(auto& elementInfo : mElementsUnderCursor)
- {
- mMouseEvent.setMouseWheelScrollData(event.mouseWheelScrollAmount);
- if(sendMouseEvent(elementInfo.widget, elementInfo.element, mMouseEvent))
- {
- event.markAsUsed();
- break;
- }
- }
- }
- }
- }
- void GUIManager::onCursorReleased(const CM::PositionalInputEvent& event)
- {
- if(event.isUsed())
- return;
- bool buttonStates[(int)GUIMouseButton::Count];
- buttonStates[0] = event.buttonStates[0];
- buttonStates[1] = event.buttonStates[1];
- buttonStates[2] = event.buttonStates[2];
- if(findElementUnderCursor(event.screenPos, buttonStates, event.shift, event.control, event.alt))
- event.markAsUsed();
- mMouseEvent = GUIMouseEvent(buttonStates, event.shift, event.control, event.alt);
- GUIMouseButton guiButton = buttonToGUIButton(event.button);
- // Send MouseUp event only if we are over the active element (we don't want to accidentally trigger other elements).
- // And only activate when a button that originally caused the active state is released, otherwise ignore it.
- if(mActiveMouseButton == guiButton)
- {
- for(auto& elementInfo : mElementsUnderCursor)
- {
- auto iterFind2 = std::find_if(mActiveElements.begin(), mActiveElements.end(),
- [&](const ElementInfo& x) { return x.element == elementInfo.element; });
- if(iterFind2 != mActiveElements.end())
- {
- Vector2I localPos = getWidgetRelativePos(*elementInfo.widget, event.screenPos);
- mMouseEvent.setMouseUpData(localPos, guiButton);
- if(sendMouseEvent(elementInfo.widget, elementInfo.element, mMouseEvent))
- {
- event.markAsUsed();
- break;
- }
- }
- }
- }
- // Send DragEnd event to whichever element is active
- bool acceptEndDrag = (mDragState == DragState::Dragging || mDragState == DragState::HeldWithoutDrag) && mActiveMouseButton == guiButton &&
- (guiButton == GUIMouseButton::Left);
- if(acceptEndDrag)
- {
- if(mDragState == DragState::Dragging)
- {
- for(auto& activeElement : mActiveElements)
- {
- Vector2I localPos = getWidgetRelativePos(*activeElement.widget, event.screenPos);
- mMouseEvent.setMouseDragEndData(localPos);
- if(sendMouseEvent(activeElement.widget, activeElement.element, mMouseEvent))
- event.markAsUsed();
- }
- }
- mDragState = DragState::NoDrag;
- }
- if(mActiveMouseButton == guiButton)
- {
- mActiveElements.clear();
- mActiveMouseButton = GUIMouseButton::Left;
- }
- if(mActiveCursor != CursorType::Arrow)
- {
- Cursor::instance().setCursor(CursorType::Arrow);
- mActiveCursor = CursorType::Arrow;
- }
- }
- void GUIManager::onCursorPressed(const CM::PositionalInputEvent& event)
- {
- if(event.isUsed())
- return;
- bool buttonStates[(int)GUIMouseButton::Count];
- buttonStates[0] = event.buttonStates[0];
- buttonStates[1] = event.buttonStates[1];
- buttonStates[2] = event.buttonStates[2];
- if(findElementUnderCursor(event.screenPos, buttonStates, event.shift, event.control, event.alt))
- event.markAsUsed();
- mMouseEvent = GUIMouseEvent(buttonStates, event.shift, event.control, event.alt);
- GUIMouseButton guiButton = buttonToGUIButton(event.button);
- // We only check for mouse down if mouse isn't already being held down, and we are hovering over an element
- if(mActiveElements.size() == 0)
- {
- mNewActiveElements.clear();
- for(auto& elementInfo : mElementsUnderCursor)
- {
- Vector2I localPos = getWidgetRelativePos(*elementInfo.widget, event.screenPos);
- mMouseEvent.setMouseDownData(localPos, guiButton);
- bool processed = sendMouseEvent(elementInfo.widget, elementInfo.element, mMouseEvent);
- if(guiButton == GUIMouseButton::Left)
- {
- mDragState = DragState::HeldWithoutDrag;
- mLastCursorClickPos = event.screenPos;
- }
- mNewActiveElements.push_back(ElementInfo(elementInfo.element, elementInfo.widget));
- mActiveMouseButton = guiButton;
- if(processed)
- {
- event.markAsUsed();
- break;
- }
- }
- mActiveElements.swap(mNewActiveElements);
- }
- mNewElementsInFocus.clear();
- mCommandEvent = GUICommandEvent();
-
- // Determine elements that gained focus
- mCommandEvent.setType(GUICommandEventType::FocusGained);
- for(auto& elementInfo : mElementsUnderCursor)
- {
- mNewElementsInFocus.push_back(elementInfo);
- auto iterFind = std::find_if(begin(mElementsInFocus), end(mElementsInFocus),
- [=] (const ElementInfo& x) { return x.element == elementInfo.element; });
- if(iterFind == mElementsInFocus.end())
- {
- sendCommandEvent(elementInfo.widget, elementInfo.element, mCommandEvent);
- }
- }
- // Determine elements that lost focus
- mCommandEvent.setType(GUICommandEventType::FocusLost);
- for(auto& elementInfo : mElementsInFocus)
- {
- auto iterFind = std::find_if(begin(mNewElementsInFocus), end(mNewElementsInFocus),
- [=] (const ElementInfo& x) { return x.element == elementInfo.element; });
- if(iterFind == mNewElementsInFocus.end())
- {
- sendCommandEvent(elementInfo.widget, elementInfo.element, mCommandEvent);
- }
- }
- if(mElementsUnderCursor.size() > 0)
- event.markAsUsed();
- mElementsInFocus.swap(mNewElementsInFocus);
- // If right click try to open context menu
- if(buttonStates[2] == true)
- {
- for(auto& elementInfo : mElementsUnderCursor)
- {
- GUIContextMenu* menu = elementInfo.element->getContextMenu();
- if(menu != nullptr)
- {
- const RenderWindow* window = getWidgetWindow(*elementInfo.widget);
- Vector2I windowPos = window->screenToWindowPos(event.screenPos);
- menu->open(windowPos, *elementInfo.widget);
- event.markAsUsed();
- break;
- }
- }
- }
- }
- void GUIManager::onCursorDoubleClick(const CM::PositionalInputEvent& event)
- {
- if(event.isUsed())
- return;
- bool buttonStates[(int)GUIMouseButton::Count];
- buttonStates[0] = event.buttonStates[0];
- buttonStates[1] = event.buttonStates[1];
- buttonStates[2] = event.buttonStates[2];
- if(findElementUnderCursor(event.screenPos, buttonStates, event.shift, event.control, event.alt))
- event.markAsUsed();
- mMouseEvent = GUIMouseEvent(buttonStates, event.shift, event.control, event.alt);
- GUIMouseButton guiButton = buttonToGUIButton(event.button);
- // We only check for mouse down if we are hovering over an element
- for(auto& elementInfo : mElementsUnderCursor)
- {
- Vector2I localPos = getWidgetRelativePos(*elementInfo.widget, event.screenPos);
- mMouseEvent.setMouseDoubleClickData(localPos, guiButton);
- if(sendMouseEvent(elementInfo.widget, elementInfo.element, mMouseEvent))
- {
- event.markAsUsed();
- break;
- }
- }
- }
- void GUIManager::onInputCommandEntered(CM::InputCommandType commandType)
- {
- if(mElementsInFocus.size() == 0)
- return;
- mCommandEvent = GUICommandEvent();
- switch(commandType)
- {
- case InputCommandType::Backspace:
- mCommandEvent.setType(GUICommandEventType::Backspace);
- break;
- case InputCommandType::Delete:
- mCommandEvent.setType(GUICommandEventType::Delete);
- break;
- case InputCommandType::Return:
- mCommandEvent.setType(GUICommandEventType::Return);
- break;
- case InputCommandType::Escape:
- mCommandEvent.setType(GUICommandEventType::Escape);
- break;
- case InputCommandType::CursorMoveLeft:
- mCommandEvent.setType(GUICommandEventType::MoveLeft);
- break;
- case InputCommandType::CursorMoveRight:
- mCommandEvent.setType(GUICommandEventType::MoveRight);
- break;
- case InputCommandType::CursorMoveUp:
- mCommandEvent.setType(GUICommandEventType::MoveUp);
- break;
- case InputCommandType::CursorMoveDown:
- mCommandEvent.setType(GUICommandEventType::MoveDown);
- break;
- case InputCommandType::SelectLeft:
- mCommandEvent.setType(GUICommandEventType::SelectLeft);
- break;
- case InputCommandType::SelectRight:
- mCommandEvent.setType(GUICommandEventType::SelectRight);
- break;
- case InputCommandType::SelectUp:
- mCommandEvent.setType(GUICommandEventType::SelectUp);
- break;
- case InputCommandType::SelectDown:
- mCommandEvent.setType(GUICommandEventType::SelectDown);
- break;
- }
- for(auto& elementInfo : mElementsInFocus)
- {
- sendCommandEvent(elementInfo.widget, elementInfo.element, mCommandEvent);
- }
- }
- void GUIManager::onVirtualButtonDown(const VirtualButton& button)
- {
- mVirtualButtonEvent.setButton(button);
-
- for(auto& elementInFocus : mElementsInFocus)
- {
- bool processed = sendVirtualButtonEvent(elementInFocus.widget, elementInFocus.element, mVirtualButtonEvent);
- if(processed)
- break;
- }
- }
- bool GUIManager::findElementUnderCursor(const CM::Vector2I& cursorScreenPos, bool buttonStates[3], bool shift, bool control, bool alt)
- {
- Vector<const RenderWindow*>::type widgetWindows;
- for(auto& widgetInfo : mWidgets)
- widgetWindows.push_back(getWidgetWindow(*widgetInfo.widget));
- #if CM_DEBUG_MODE
- // Checks if all referenced windows actually exist
- Vector<RenderWindow*>::type activeWindows = RenderWindowManager::instance().getRenderWindows();
- for(auto& window : widgetWindows)
- {
- if(window == nullptr)
- continue;
- auto iterFind = std::find(begin(activeWindows), end(activeWindows), window);
- if(iterFind == activeWindows.end())
- {
- CM_EXCEPT(InternalErrorException, "GUI manager has a reference to a window that doesn't exist. \
- Please detach all GUIWidgets from windows before destroying a window.");
- }
- }
- #endif
- mNewElementsUnderCursor.clear();
- const RenderWindow* windowUnderCursor = nullptr;
- UnorderedSet<const RenderWindow*>::type uniqueWindows;
- for(auto& window : widgetWindows)
- {
- if(window == nullptr)
- continue;
- uniqueWindows.insert(window);
- }
- for(auto& window : uniqueWindows)
- {
- if(Platform::isPointOverWindow(*window, cursorScreenPos))
- {
- windowUnderCursor = window;
- break;
- }
- }
- if(windowUnderCursor != nullptr)
- {
- Vector2I windowPos = windowUnderCursor->screenToWindowPos(cursorScreenPos);
- Vector4 vecWindowPos((float)windowPos.x, (float)windowPos.y, 0.0f, 1.0f);
- UINT32 widgetIdx = 0;
- for(auto& widgetInfo : mWidgets)
- {
- if(widgetWindows[widgetIdx] == nullptr)
- {
- widgetIdx++;
- continue;
- }
- GUIWidget* widget = widgetInfo.widget;
- if(widgetWindows[widgetIdx] == windowUnderCursor && widget->inBounds(windowToBridgedCoords(*widget, windowPos)))
- {
- const Vector<GUIElement*>::type& elements = widget->getElements();
- Vector2I localPos = getWidgetRelativePos(*widget, cursorScreenPos);
- // Elements with lowest depth (most to the front) get handled first
- for(auto iter = elements.begin(); iter != elements.end(); ++iter)
- {
- GUIElement* element = *iter;
- if(!element->_isDisabled() && element->_isInBounds(localPos))
- {
- mNewElementsUnderCursor.push_back(ElementInfo(element, widget));
- }
- }
- }
- widgetIdx++;
- }
- }
- std::sort(mNewElementsUnderCursor.begin(), mNewElementsUnderCursor.end(),
- [](const ElementInfo& a, const ElementInfo& b)
- {
- return a.element->_getDepth() < b.element->_getDepth();
- });
- // Send MouseOut and MouseOver events
- bool eventProcessed = false;
- for(auto& elementInfo : mElementsUnderCursor)
- {
- GUIElement* element = elementInfo.element;
- GUIWidget* widget = elementInfo.widget;
- auto iterFind = std::find_if(mNewElementsUnderCursor.begin(), mNewElementsUnderCursor.end(),
- [=] (const ElementInfo& x) { return x.element == element; });
- if(iterFind == mNewElementsUnderCursor.end())
- {
- auto iterFind2 = std::find_if(mActiveElements.begin(), mActiveElements.end(),
- [=](const ElementInfo& x) { return x.element == element; });
- // Send MouseOut event
- if(mActiveElements.size() == 0 || iterFind2 != mActiveElements.end())
- {
- Vector2I curLocalPos = getWidgetRelativePos(*widget, cursorScreenPos);
- mMouseEvent.setMouseOutData(curLocalPos);
- if(sendMouseEvent(widget, element, mMouseEvent))
- eventProcessed = true;
- }
- }
- }
- for(auto& elementInfo : mNewElementsUnderCursor)
- {
- GUIElement* element = elementInfo.element;
- GUIWidget* widget = elementInfo.widget;
- auto iterFind = std::find_if(begin(mElementsUnderCursor), end(mElementsUnderCursor),
- [=] (const ElementInfo& x) { return x.element == element; });
- if(iterFind == mElementsUnderCursor.end())
- {
- auto iterFind2 = std::find_if(mActiveElements.begin(), mActiveElements.end(),
- [&](const ElementInfo& x) { return x.element == element; });
- // Send MouseOver event
- if(mActiveElements.size() == 0 || iterFind2 != mActiveElements.end())
- {
- Vector2I localPos;
- if(widget != nullptr)
- localPos = getWidgetRelativePos(*widget, cursorScreenPos);
- mMouseEvent = GUIMouseEvent(buttonStates, shift, control, alt);
- mMouseEvent.setMouseOverData(localPos);
- if(sendMouseEvent(widget, element, mMouseEvent))
- eventProcessed = true;
- }
- }
- }
- mElementsUnderCursor.swap(mNewElementsUnderCursor);
- return eventProcessed;
- }
- void GUIManager::onTextInput(const CM::TextInputEvent& event)
- {
- mTextInputEvent = GUITextInputEvent();
- mTextInputEvent.setData(event.textChar);
- for(auto& elementInFocus : mElementsInFocus)
- {
- if(sendTextInputEvent(elementInFocus.widget, elementInFocus.element, mTextInputEvent))
- event.markAsUsed();
- }
- }
- void GUIManager::onWindowFocusGained(RenderWindow& win)
- {
- for(auto& widgetInfo : mWidgets)
- {
- GUIWidget* widget = widgetInfo.widget;
- if(getWidgetWindow(*widget) == &win)
- widget->ownerWindowFocusChanged();
- }
- }
- void GUIManager::onWindowFocusLost(RenderWindow& win)
- {
- for(auto& widgetInfo : mWidgets)
- {
- GUIWidget* widget = widgetInfo.widget;
- if(getWidgetWindow(*widget) == &win)
- widget->ownerWindowFocusChanged();
- }
- mNewElementsInFocus.clear();
- for(auto& focusedElement : mElementsInFocus)
- {
- if(getWidgetWindow(*focusedElement.widget) == &win)
- {
- mCommandEvent = GUICommandEvent();
- mCommandEvent.setType(GUICommandEventType::FocusLost);
- sendCommandEvent(focusedElement.widget, focusedElement.element, mCommandEvent);
- }
- else
- mNewElementsInFocus.push_back(focusedElement);
- }
- mElementsInFocus.swap(mNewElementsInFocus);
- }
- // We stop getting mouse move events once it leaves the window, so make sure
- // nothing stays in hover state
- void GUIManager::onMouseLeftWindow(CM::RenderWindow* win)
- {
- bool buttonStates[3];
- buttonStates[0] = false;
- buttonStates[1] = false;
- buttonStates[2] = false;
- mNewElementsUnderCursor.clear();
- for(auto& elementInfo : mElementsUnderCursor)
- {
- GUIElement* element = elementInfo.element;
- GUIWidget* widget = elementInfo.widget;
- if(widget->getTarget()->getTarget().get() != win)
- {
- mNewElementsUnderCursor.push_back(elementInfo);
- continue;
- }
- auto iterFind = std::find_if(mActiveElements.begin(), mActiveElements.end(),
- [&](const ElementInfo& x) { return x.element == element; });
- // Send MouseOut event
- if(mActiveElements.size() == 0 || iterFind != mActiveElements.end())
- {
- Vector2I curLocalPos = getWidgetRelativePos(*widget, Vector2I());
- mMouseEvent.setMouseOutData(curLocalPos);
- sendMouseEvent(widget, element, mMouseEvent);
- }
- }
- mElementsUnderCursor.swap(mNewElementsUnderCursor);
- }
- void GUIManager::queueForDestroy(GUIElement* element)
- {
- mScheduledForDestruction.push(element);
- }
- void GUIManager::setFocus(GUIElement* element, bool focus)
- {
- ElementFocusInfo efi;
- efi.element = element;
- efi.focus = focus;
- mForcedFocusElements.push_back(efi);
- }
- void GUIManager::processDestroyQueue()
- {
- // Need two loops and a temporary since element destructors may themselves
- // queue other elements for destruction
- while(!mScheduledForDestruction.empty())
- {
- CM::Stack<GUIElement*>::type toDestroy = mScheduledForDestruction;
- mScheduledForDestruction = CM::Stack<GUIElement*>::type();
- while(!toDestroy.empty())
- {
- cm_delete<PoolAlloc>(toDestroy.top());
- toDestroy.pop();
- }
- }
- }
- void GUIManager::setInputBridge(const CM::RenderTexture* renderTex, const GUIElement* element)
- {
- if(element == nullptr)
- mInputBridge.erase(renderTex);
- else
- mInputBridge[renderTex] = element;
- }
- GUIMouseButton GUIManager::buttonToGUIButton(PositionalInputEventButton cursorButton) const
- {
- if(cursorButton == PositionalInputEventButton::Left)
- return GUIMouseButton::Left;
- else if(cursorButton == PositionalInputEventButton::Middle)
- return GUIMouseButton::Middle;
- else if(cursorButton == PositionalInputEventButton::Right)
- return GUIMouseButton::Right;
- CM_EXCEPT(InvalidParametersException, "Provided button is not a GUI supported mouse button.");
- }
- Vector2I GUIManager::getWidgetRelativePos(const GUIWidget& widget, const Vector2I& screenPos) const
- {
- const RenderWindow* window = getWidgetWindow(widget);
- if(window == nullptr)
- return Vector2I();
- Vector2I windowPos = window->screenToWindowPos(screenPos);
- windowPos = windowToBridgedCoords(widget, windowPos);
- const Matrix4& worldTfrm = widget.SO()->getWorldTfrm();
- Vector4 vecLocalPos = worldTfrm.inverse().multiply3x4(Vector4((float)windowPos.x, (float)windowPos.y, 0.0f, 1.0f));
- Vector2I curLocalPos(Math::roundToInt(vecLocalPos.x), Math::roundToInt(vecLocalPos.y));
- return curLocalPos;
- }
- Vector2I GUIManager::windowToBridgedCoords(const GUIWidget& widget, const Vector2I& windowPos) const
- {
- // This cast might not be valid (the render target could be a window), but we only really need to cast
- // so that mInputBridge map allows us to search through it - we don't access anything unless the target is bridged
- // (in which case we know it is a RenderTexture)
- const RenderTexture* renderTexture = static_cast<const RenderTexture*>(widget.getTarget()->getTarget().get());
- auto iterFind = mInputBridge.find(renderTexture);
- if(iterFind != mInputBridge.end()) // Widget input is bridged, which means we need to transform the coordinates
- {
- const GUIElement* bridgeElement = iterFind->second;
- const Matrix4& worldTfrm = bridgeElement->_getParentWidget()->SO()->getWorldTfrm();
- Vector4 vecLocalPos = worldTfrm.inverse().multiply3x4(Vector4((float)windowPos.x, (float)windowPos.y, 0.0f, 1.0f));
- RectI bridgeBounds = bridgeElement->getBounds();
- // Find coordinates relative to the bridge element
- float x = vecLocalPos.x - (float)bridgeBounds.x;
- float y = vecLocalPos.y - (float)bridgeBounds.y;
- float scaleX = renderTexture->getWidth() / (float)bridgeBounds.width;
- float scaleY = renderTexture->getHeight() / (float)bridgeBounds.height;
- return Vector2I(Math::roundToInt(x * scaleX), Math::roundToInt(y * scaleY));
- }
- return windowPos;
- }
- const CM::RenderWindow* GUIManager::getWidgetWindow(const GUIWidget& widget) const
- {
- // This cast might not be valid (the render target could be a window), but we only really need to cast
- // so that mInputBridge map allows us to search through it - we don't access anything unless the target is bridged
- // (in which case we know it is a RenderTexture)
- const RenderTexture* renderTexture = static_cast<const RenderTexture*>(widget.getTarget()->getTarget().get());
- auto iterFind = mInputBridge.find(renderTexture);
- if(iterFind != mInputBridge.end())
- {
- GUIWidget* parentWidget = iterFind->second->_getParentWidget();
- if(parentWidget != &widget)
- {
- return getWidgetWindow(*parentWidget);
- }
- }
- RenderTargetPtr renderTarget = widget.getTarget()->getTarget();
- Vector<RenderWindow*>::type renderWindows = RenderWindowManager::instance().getRenderWindows();
- auto iterFindWin = std::find(renderWindows.begin(), renderWindows.end(), renderTarget.get());
- if(iterFindWin != renderWindows.end())
- return static_cast<RenderWindow*>(renderTarget.get());
- return nullptr;
- }
- bool GUIManager::sendMouseEvent(GUIWidget* widget, GUIElement* element, const GUIMouseEvent& event)
- {
- return widget->_mouseEvent(element, event);
- }
- bool GUIManager::sendTextInputEvent(GUIWidget* widget, GUIElement* element, const GUITextInputEvent& event)
- {
- return widget->_textInputEvent(element, event);
- }
- bool GUIManager::sendCommandEvent(GUIWidget* widget, GUIElement* element, const GUICommandEvent& event)
- {
- return widget->_commandEvent(element, event);
- }
- bool GUIManager::sendVirtualButtonEvent(GUIWidget* widget, GUIElement* element, const GUIVirtualButtonEvent& event)
- {
- return widget->_virtualButtonEvent(element, event);
- }
- GUIManager& gGUIManager()
- {
- return GUIManager::instance();
- }
- }
|