| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2012 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "CheckBox.h"
- #include "Context.h"
- #include "CoreEvents.h"
- #include "Cursor.h"
- #include "DropDownList.h"
- #include "FileSelector.h"
- #include "Font.h"
- #include "Graphics.h"
- #include "GraphicsEvents.h"
- #include "InputEvents.h"
- #include "LineEdit.h"
- #include "ListView.h"
- #include "Log.h"
- #include "Matrix3x4.h"
- #include "Profiler.h"
- #include "ResourceCache.h"
- #include "ScrollBar.h"
- #include "Shader.h"
- #include "ShaderVariation.h"
- #include "Slider.h"
- #include "Text.h"
- #include "Texture2D.h"
- #include "UI.h"
- #include "UIEvents.h"
- #include "VertexBuffer.h"
- #include "Window.h"
- #include "Sort.h"
- #include "DebugNew.h"
- OBJECTTYPESTATIC(UI);
- UI::UI(Context* context) :
- Object(context),
- mouseButtons_(0),
- qualifiers_(0),
- initialized_(false)
- {
- SubscribeToEvent(E_SCREENMODE, HANDLER(UI, HandleScreenMode));
- SubscribeToEvent(E_MOUSEMOVE, HANDLER(UI, HandleMouseMove));
- SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UI, HandleMouseButtonDown));
- SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UI, HandleMouseButtonUp));
- SubscribeToEvent(E_MOUSEWHEEL, HANDLER(UI, HandleMouseWheel));
- SubscribeToEvent(E_KEYDOWN, HANDLER(UI, HandleKeyDown));
- SubscribeToEvent(E_CHAR, HANDLER(UI, HandleChar));
- SubscribeToEvent(E_POSTUPDATE, HANDLER(UI, HandlePostUpdate));
- SubscribeToEvent(E_RENDERUPDATE, HANDLER(UI, HandleRenderUpdate));
-
- // Try to initialize right now, but skip if screen mode is not yet set
- Initialize();
- }
- UI::~UI()
- {
- }
- void UI::SetCursor(Cursor* cursor)
- {
- if (!rootElement_)
- return;
-
- // Remove old cursor (if any) and set new
- if (cursor_)
- {
- rootElement_->RemoveChild(cursor_);
- cursor_.Reset();
- }
- if (cursor)
- {
- rootElement_->AddChild(cursor);
- cursor_ = cursor;
-
- IntVector2 pos = cursor_->GetPosition();
- const IntVector2& rootSize = rootElement_->GetSize();
- pos.x_ = Clamp(pos.x_, 0, rootSize.x_ - 1);
- pos.y_ = Clamp(pos.y_, 0, rootSize.y_ - 1);
- cursor_->SetPosition(pos);
- }
- }
- void UI::SetFocusElement(UIElement* element)
- {
- if (!rootElement_)
- return;
-
- using namespace FocusChanged;
-
- VariantMap eventData;
- eventData[P_CLICKEDELEMENT] = (void*)element;
-
- if (element)
- {
- // Return if already has focus
- if (focusElement_ == element)
- return;
-
- // Search for an element in the hierarchy that can alter focus. If none found, exit
- element = GetFocusableElement(element);
- if (!element)
- return;
- }
-
- // Remove focus from the old element
- if (focusElement_)
- {
- UIElement* oldFocusElement = focusElement_;
- focusElement_.Reset();
-
- VariantMap focusEventData;
- focusEventData[Defocused::P_ELEMENT] = oldFocusElement;
- oldFocusElement->SendEvent(E_DEFOCUSED, focusEventData);
- }
-
- // Then set focus to the new
- if (element && element->GetFocusMode() >= FM_FOCUSABLE)
- {
- focusElement_ = element;
-
- VariantMap focusEventData;
- focusEventData[Focused::P_ELEMENT] = element;
- element->SendEvent(E_FOCUSED, focusEventData);
- }
-
- eventData[P_ELEMENT] = (void*)element;
- SendEvent(E_FOCUSCHANGED, eventData);
- }
- void UI::Clear()
- {
- if (!rootElement_)
- return;
-
- rootElement_->RemoveAllChildren();
- if (cursor_)
- rootElement_->AddChild(cursor_);
- }
- void UI::Update(float timeStep)
- {
- if (!rootElement_)
- return;
-
- PROFILE(UpdateUI);
-
- if (cursor_ && cursor_->IsVisible())
- {
- IntVector2 pos = cursor_->GetPosition();
- WeakPtr<UIElement> element(GetElementAt(pos));
-
- bool dragSource = dragElement_ && (dragElement_->GetDragDropMode() & DD_SOURCE) != 0;
- bool dragTarget = element && (element->GetDragDropMode() & DD_TARGET) != 0;
- bool dragDropTest = dragSource && dragTarget && element != dragElement_;
-
- // Hover effect
- // If a drag is going on, transmit hover only to the element being dragged, unless it's a drop target
- if (element)
- {
- if (!dragElement_ || dragElement_ == element || dragDropTest)
- element->OnHover(element->ScreenToElement(pos), pos, mouseButtons_, qualifiers_, cursor_);
- }
-
- // Drag and drop test
- if (dragDropTest)
- {
- bool accept = element->OnDragDropTest(dragElement_);
- if (accept)
- {
- using namespace DragDropTest;
-
- VariantMap eventData;
- eventData[P_SOURCE] = (void*)dragElement_.Get();
- eventData[P_TARGET] = (void*)element.Get();
- eventData[P_ACCEPT] = accept;
- SendEvent(E_DRAGDROPTEST, eventData);
- accept = eventData[P_ACCEPT].GetBool();
- }
-
- cursor_->SetShape(accept ? CS_ACCEPTDROP : CS_REJECTDROP);
- }
- else if (dragSource)
- cursor_->SetShape(dragElement_ == element ? CS_ACCEPTDROP : CS_REJECTDROP);
- }
-
- Update(timeStep, rootElement_);
- }
- void UI::RenderUpdate()
- {
- if (!rootElement_ || !graphics_)
- return;
-
- PROFILE(GetUIBatches);
-
- // Get batches & quads from the UI elements
- batches_.Clear();
- quads_.Clear();
- const IntVector2& rootSize = rootElement_->GetSize();
- GetBatches(rootElement_, IntRect(0, 0, rootSize.x_, rootSize.y_));
-
- // If no drag, reset cursor shape for next frame
- if (cursor_ && !dragElement_)
- cursor_->SetShape(CS_NORMAL);
- }
- void UI::Render()
- {
- PROFILE(RenderUI);
-
- if (!graphics_ || graphics_->IsDeviceLost() || !quads_.Size())
- return;
-
- // Update quad geometry into the vertex buffer
- unsigned numVertices = quads_.Size() * 6;
- // Resize the vertex buffer if too small or much too large
- if (vertexBuffer_->GetVertexCount() < numVertices || vertexBuffer_->GetVertexCount() > numVertices * 2)
- vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
-
- unsigned vertexSize = vertexBuffer_->GetVertexSize();
- void* lockedData = vertexBuffer_->Lock(0, numVertices, LOCK_DISCARD);
- if (!lockedData)
- return;
-
- for (unsigned i = 0; i < batches_.Size(); ++i)
- batches_[i].UpdateGeometry(graphics_, ((unsigned char*)lockedData) + batches_[i].quadStart_ * vertexSize * 6);
-
- vertexBuffer_->Unlock();
-
- Vector2 scale(2.0f, -2.0f);
- Vector2 offset(-1.0f, 1.0f);
-
- Matrix4 projection(Matrix4::IDENTITY);
- projection.m00_ = scale.x_;
- projection.m03_ = offset.x_;
- projection.m11_ = scale.y_;
- projection.m13_ = offset.y_;
- projection.m22_ = 1.0f;
- projection.m23_ = 0.0f;
- projection.m33_ = 1.0f;
-
- graphics_->ClearParameterSources();
- graphics_->SetAlphaTest(false);
- graphics_->SetCullMode(CULL_CCW);
- graphics_->SetDepthTest(CMP_ALWAYS);
- graphics_->SetDepthWrite(false);
- graphics_->SetFillMode(FILL_SOLID);
- graphics_->SetStencilTest(false);
- graphics_->ResetRenderTargets();
-
- ShaderVariation* ps = 0;
- ShaderVariation* vs = 0;
-
- unsigned alphaFormat = Graphics::GetAlphaFormat();
-
- for (unsigned i = 0; i < batches_.Size(); ++i)
- {
- const UIBatch& batch = batches_[i];
- if (!batch.quadCount_)
- continue;
-
- if (!batch.texture_)
- {
- ps = noTexturePS_;
- vs = noTextureVS_;
- }
- else
- {
- // If texture contains only an alpha channel, use alpha shader (for fonts)
- vs = diffTextureVS_;
-
- if (batch.texture_->GetFormat() == alphaFormat)
- ps = alphaTexturePS_;
- else
- ps = diffTexturePS_;
- }
-
- graphics_->SetShaders(vs, ps);
- if (graphics_->NeedParameterUpdate(VSP_MODEL, this))
- graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
- if (graphics_->NeedParameterUpdate(VSP_VIEWPROJ, this))
- graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
- if (graphics_->NeedParameterUpdate(PSP_MATDIFFCOLOR, this))
- graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
-
- // Use alpha test if not alpha blending
- if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
- graphics_->SetAlphaTest(true, CMP_GREATEREQUAL, 0.5f);
- else
- graphics_->SetAlphaTest(false);
-
- graphics_->SetBlendMode(batch.blendMode_);
- graphics_->SetScissorTest(true, batch.scissor_);
- graphics_->SetTexture(0, batch.texture_);
- graphics_->SetVertexBuffer(vertexBuffer_);
- graphics_->Draw(TRIANGLE_LIST, batch.quadStart_ * 6, batch.quadCount_ * 6);
- }
- }
- SharedPtr<UIElement> UI::LoadLayout(XMLFile* file, XMLFile* styleFile)
- {
- PROFILE(LoadUILayout);
-
- SharedPtr<UIElement> root;
-
- if (!file)
- {
- LOGERROR("Null UI layout XML file");
- return root;
- }
-
- LOGDEBUG("Loading UI layout " + file->GetName());
-
- XMLElement rootElem = file->GetRoot("element");
- if (!rootElem)
- {
- LOGERROR("No root UI element in " + file->GetName());
- return root;
- }
-
- String type = rootElem.GetAttribute("type");
- if (type.Empty())
- type = "UIElement";
-
- root = DynamicCast<UIElement>(context_->CreateObject(ShortStringHash(type)));
- if (!root)
- {
- LOGERROR("Could not create UI element " + type);
- return root;
- }
- root->SetName(rootElem.GetAttribute("name"));
-
- String styleName = rootElem.HasAttribute("style") ? rootElem.GetAttribute("style") : rootElem.GetAttribute("type");
- // First set the base style from the style file if exists, then apply UI layout overrides
- if (styleFile)
- root->SetStyle(styleFile, styleName);
- root->SetStyle(rootElem);
-
- // Load rest of the elements recursively
- LoadLayout(root, rootElem, styleFile);
-
- return root;
- }
- void UI::SetClipBoardText(const String& text)
- {
- clipBoard_ = text;
- }
- UIElement* UI::GetElementAt(const IntVector2& position, bool activeOnly)
- {
- if (!rootElement_)
- return 0;
-
- UIElement* result = 0;
- GetElementAt(result, rootElement_, position, activeOnly);
- return result;
- }
- UIElement* UI::GetElementAt(int x, int y, bool activeOnly)
- {
- return GetElementAt(IntVector2(x, y), activeOnly);
- }
- UIElement* UI::GetFocusElement() const
- {
- return focusElement_;
- }
- UIElement* UI::GetFrontElement() const
- {
- if (!rootElement_)
- return 0;
-
- const Vector<SharedPtr<UIElement> >& rootChildren = rootElement_->GetChildren();
- int maxPriority = M_MIN_INT;
- UIElement* front = 0;
-
- for (unsigned i = 0; i < rootChildren.Size(); ++i)
- {
- // Do not take into account input-disabled elements, hidden elements or those that are always in the front
- if (!rootChildren[i]->IsActive() || !rootChildren[i]->IsVisible() || !rootChildren[i]->GetBringToBack())
- continue;
-
- int priority = rootChildren[i]->GetPriority();
- if (priority > maxPriority)
- {
- maxPriority = priority;
- front = rootChildren[i];
- }
- }
-
- return front;
- }
- IntVector2 UI::GetCursorPosition()
- {
- if (!cursor_)
- return IntVector2::ZERO;
- else
- return cursor_->GetPosition();
- }
- void UI::Initialize()
- {
- Graphics* graphics = GetSubsystem<Graphics>();
- ResourceCache* cache = GetSubsystem<ResourceCache>();
-
- if (!graphics || !graphics->IsInitialized() || !cache)
- return;
-
- PROFILE(InitUI);
-
- graphics_ = graphics;
- cache_ = cache;
-
- rootElement_ = new UIElement(context_);
- rootElement_->SetSize(graphics->GetWidth(), graphics->GetHeight());
-
- #ifdef USE_OPENGL
- Shader* basicVS = cache->GetResource<Shader>("Shaders/GLSL/Basic.vert");
- Shader* basicPS = cache->GetResource<Shader>("Shaders/GLSL/Basic.frag");
- #else
- Shader* basicVS = cache->GetResource<Shader>("Shaders/SM2/Basic.vs2");
- Shader* basicPS = cache->GetResource<Shader>("Shaders/SM2/Basic.ps2");
- #endif
-
- if (basicVS && basicPS)
- {
- noTextureVS_ = basicVS->GetVariation("VCol");
- diffTextureVS_ = basicVS->GetVariation("DiffVCol");
- noTexturePS_ = basicPS->GetVariation("VCol");
- diffTexturePS_ = basicPS->GetVariation("DiffVCol");
- alphaTexturePS_ = basicPS->GetVariation("AlphaVCol");
- }
-
- vertexBuffer_ = new VertexBuffer(context_);
-
- LOGINFO("Initialized user interface");
- initialized_ = true;
- }
- void UI::Update(float timeStep, UIElement* element)
- {
- element->Update(timeStep);
-
- const Vector<SharedPtr<UIElement> >& children = element->GetChildren();
- for (Vector<SharedPtr<UIElement> >::ConstIterator i = children.Begin(); i != children.End(); ++i)
- Update(timeStep, *i);
- }
- void UI::GetBatches(UIElement* element, IntRect currentScissor)
- {
- // Set clipping scissor for child elements. No need to draw if zero size
- element->AdjustScissor(currentScissor);
- if (currentScissor.left_ == currentScissor.right_ || currentScissor.top_ == currentScissor.bottom_)
- return;
-
- element->SortChildren();
- const Vector<SharedPtr<UIElement> >& children = element->GetChildren();
- if (children.Empty())
- return;
-
- // For non-root elements draw all children of same priority before recursing into their children: assumption is that they have
- // same renderstate
- Vector<SharedPtr<UIElement> >::ConstIterator i = children.Begin();
- if (element != rootElement_)
- {
- Vector<SharedPtr<UIElement> >::ConstIterator j = i;
- int currentPriority = children.Front()->GetPriority();
- while (i != children.End())
- {
- while (j != children.End() && (*j)->GetPriority() == currentPriority)
- {
- if (IsVisible(*j, currentScissor))
- (*j)->GetBatches(batches_, quads_, currentScissor);
- ++j;
- }
- // Now recurse into the children
- while (i != j)
- {
- if (IsVisible(*i, currentScissor))
- GetBatches(*i, currentScissor);
- ++i;
- }
- if (i != children.End())
- currentPriority = (*i)->GetPriority();
- }
- }
- // On the root level draw each element and its children immediately after to avoid artifacts
- else
- {
- while (i != children.End())
- {
- if ((*i)->IsVisible())
- {
- if (IsVisible(*i, currentScissor))
- (*i)->GetBatches(batches_, quads_, currentScissor);
- GetBatches(*i, currentScissor);
- }
- ++i;
- }
- }
- }
- bool UI::IsVisible(UIElement* element, const IntRect& currentScissor)
- {
- // First check element's visibility
- if (!element->IsVisible())
- return false;
- // Then check element dimensions against the scissor rectangle
- const IntVector2& screenPos = element->GetScreenPosition();
- if (screenPos.x_ >= currentScissor.right_ || screenPos.x_ + element->GetWidth() <= currentScissor.left_ ||
- screenPos.y_ >= currentScissor.bottom_ || screenPos.y_ + element->GetHeight() <= currentScissor.top_)
- return false;
- else
- return true;
- }
- void UI::GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool activeOnly)
- {
- if (!current)
- return;
-
- current->SortChildren();
- const Vector<SharedPtr<UIElement> >& children = current->GetChildren();
- LayoutMode parentLayoutMode = current->GetLayoutMode();
-
- for (Vector<SharedPtr<UIElement> >::ConstIterator i = children.Begin(); i != children.End(); ++i)
- {
- UIElement* element = *i;
- bool hasChildren = element->GetNumChildren() > 0;
-
- if (element != cursor_.Get() && element->IsVisible())
- {
- if (element->IsInside(position, true))
- {
- // Store the current result, then recurse into its children. Because children
- // are sorted from lowest to highest priority, the topmost match should remain
- if (element->IsActive() || !activeOnly)
- result = element;
-
- if (hasChildren)
- GetElementAt(result, element, position, activeOnly);
- // Layout optimization: if the element has no children, can break out after the first match
- else if (parentLayoutMode != LM_FREE)
- break;
- }
- else
- {
- if (hasChildren)
- {
- if (element->IsInsideCombined(position, true))
- GetElementAt(result, element, position, activeOnly);
- }
- // Layout optimization: if position is much beyond the visible screen, check how many elements we can skip,
- // or if we already passed all visible elements
- else if (parentLayoutMode != LM_FREE)
- {
- if (i == children.Begin())
- {
- int screenPos = (parentLayoutMode == LM_HORIZONTAL) ? element->GetScreenPosition().x_ :
- element->GetScreenPosition().y_;
- int layoutMinSize = current->GetLayoutMinSize();
-
- if (screenPos < 0 && layoutMinSize > 0)
- {
- unsigned toSkip = -screenPos / layoutMinSize;
- if (toSkip > 0)
- {
- i += (toSkip - 1);
- if (i >= children.End())
- break;
- }
- }
- }
- else if (parentLayoutMode == LM_HORIZONTAL)
- {
- if (element->GetScreenPosition().x_ >= rootElement_->GetSize().x_)
- break;
- }
- else if (parentLayoutMode == LM_VERTICAL)
- {
- if (element->GetScreenPosition().y_ >= rootElement_->GetSize().y_)
- break;
- }
- }
- }
- }
- }
- }
- UIElement* UI::GetFocusableElement(UIElement* element)
- {
- while (element)
- {
- if (element->GetFocusMode() != FM_NOTFOCUSABLE)
- break;
- element = element->GetParent();
- }
- return element;
- }
- void UI::LoadLayout(UIElement* current, const XMLElement& elem, XMLFile* styleFile)
- {
- XMLElement childElem = elem.GetChild("element");
- while (childElem)
- {
- // Create element
- String type = childElem.GetAttribute("type");
- if (type.Empty())
- type = "UIElement";
-
- SharedPtr<UIElement> child = DynamicCast<UIElement>(context_->CreateObject(ShortStringHash(type)));
- if (!child)
- {
- LOGERROR("Could not create UI element " + type);
- childElem = childElem.GetNext("element");
- continue;
- }
- child->SetName(childElem.GetAttribute("name"));
-
- // Add to the hierarchy
- current->AddChild(child);
-
- // First set the base style from the style file if exists, then apply UI layout overrides
- String styleName = childElem.HasAttribute("style") ? childElem.GetAttribute("style") : childElem.GetAttribute("type");
- if (styleFile)
- child->SetStyle(styleFile, styleName);
- child->SetStyle(childElem);
-
- // Load the children recursively
- LoadLayout(child, childElem, styleFile);
-
- childElem = childElem.GetNext("element");
- }
- }
- void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
- {
- using namespace ScreenMode;
-
- if (!initialized_)
- Initialize();
- else
- rootElement_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
- }
- void UI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
- {
- using namespace MouseMove;
-
- mouseButtons_ = eventData[P_BUTTONS].GetInt();
- qualifiers_ = eventData[P_QUALIFIERS].GetInt();
-
- if (cursor_)
- {
- const IntVector2& rootSize = rootElement_->GetSize();
-
- // Move cursor only when visible
- if (cursor_->IsVisible())
- {
- IntVector2 pos = cursor_->GetPosition();
- pos.x_ += eventData[P_DX].GetInt();
- pos.y_ += eventData[P_DY].GetInt();
- pos.x_ = Clamp(pos.x_, 0, rootSize.x_ - 1);
- pos.y_ = Clamp(pos.y_, 0, rootSize.y_ - 1);
- cursor_->SetPosition(pos);
- }
-
- if (dragElement_ && mouseButtons_)
- {
- IntVector2 pos = cursor_->GetPosition();
- if (dragElement_->IsActive() && dragElement_->IsVisible())
- dragElement_->OnDragMove(dragElement_->ScreenToElement(pos), pos, mouseButtons_, qualifiers_, cursor_);
- else
- {
- dragElement_->OnDragEnd(dragElement_->ScreenToElement(pos), pos, cursor_);
- dragElement_.Reset();
- }
- }
- }
- }
- void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
- {
- mouseButtons_ = eventData[MouseButtonDown::P_BUTTONS].GetInt();
- qualifiers_ = eventData[MouseButtonDown::P_QUALIFIERS].GetInt();
- int button = eventData[MouseButtonDown::P_BUTTON].GetInt();
-
- if (cursor_ && cursor_->IsVisible())
- {
- IntVector2 pos = cursor_->GetPosition();
- WeakPtr<UIElement> element(GetElementAt(pos));
-
- if (element)
- {
- // Handle focusing & bringing to front
- if (button == MOUSEB_LEFT)
- {
- SetFocusElement(element);
- element->BringToFront();
- }
-
- // Handle click
- element->OnClick(element->ScreenToElement(pos), pos, mouseButtons_, qualifiers_, cursor_);
-
- // Handle start of drag. OnClick() may have caused destruction of the element, so check the pointer again
- if (element && !dragElement_ && mouseButtons_ == MOUSEB_LEFT)
- {
- dragElement_ = element;
- element->OnDragStart(element->ScreenToElement(pos), pos, mouseButtons_, qualifiers_, cursor_);
- }
- }
- else
- {
- // If clicked over no element, or a disabled element, lose focus
- SetFocusElement(0);
- }
-
- using namespace UIMouseClick;
-
- VariantMap eventData;
- eventData[UIMouseClick::P_ELEMENT] = (void*)element.Get();
- eventData[UIMouseClick::P_X] = pos.x_;
- eventData[UIMouseClick::P_Y] = pos.y_;
- eventData[UIMouseClick::P_BUTTON] = button;
- eventData[UIMouseClick::P_BUTTONS] = mouseButtons_;
- eventData[UIMouseClick::P_QUALIFIERS] = qualifiers_;
- SendEvent(E_UIMOUSECLICK, eventData);
- }
- }
- void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
- {
- using namespace MouseButtonUp;
-
- mouseButtons_ = eventData[P_BUTTONS].GetInt();
- qualifiers_ = eventData[P_QUALIFIERS].GetInt();
-
- if (cursor_ && (cursor_->IsVisible())|| (dragElement_))
- {
- IntVector2 pos = cursor_->GetPosition();
-
- if (dragElement_ && !mouseButtons_)
- {
- if (dragElement_->IsActive() && dragElement_->IsVisible())
- {
- dragElement_->OnDragEnd(dragElement_->ScreenToElement(pos), pos, cursor_);
-
- // Drag and drop finish
- bool dragSource = dragElement_ && (dragElement_->GetDragDropMode() & DD_SOURCE) != 0;
- if (dragSource)
- {
- WeakPtr<UIElement> target(GetElementAt(pos));
- bool dragTarget = target && (target->GetDragDropMode() & DD_TARGET) != 0;
- bool dragDropFinish = dragSource && dragTarget && target != dragElement_;
-
- if (dragDropFinish)
- {
- bool accept = target->OnDragDropFinish(dragElement_);
-
- // OnDragDropFinish() may have caused destruction of the elements, so check the pointers again
- if (accept && dragElement_ && target)
- {
- using namespace DragDropFinish;
-
- VariantMap eventData;
- eventData[P_SOURCE] = (void*)dragElement_.Get();
- eventData[P_TARGET] = (void*)target.Get();
- eventData[P_ACCEPT] = accept;
- SendEvent(E_DRAGDROPFINISH, eventData);
- }
- }
- }
- }
-
- dragElement_.Reset();
- }
- }
- }
- void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
- {
- using namespace MouseWheel;
-
- mouseButtons_ = eventData[P_BUTTONS].GetInt();
- qualifiers_ = eventData[P_QUALIFIERS].GetInt();
- int delta = eventData[P_WHEEL].GetInt();
-
- UIElement* element = GetFocusElement();
- if (element)
- element->OnWheel(delta, mouseButtons_, qualifiers_);
- else
- {
- // If no element has actual focus, get the element at cursor
- if (cursor_)
- {
- IntVector2 pos = cursor_->GetPosition();
- UIElement* element = GetElementAt(pos);
- // If the element itself is not focusable, search for a focusable parent
- element = GetFocusableElement(element);
- if (element && element->GetFocusMode() >= FM_FOCUSABLE)
- element->OnWheel(delta, mouseButtons_, qualifiers_);
- }
- }
- }
- void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- using namespace KeyDown;
-
- mouseButtons_ = eventData[P_BUTTONS].GetInt();
- qualifiers_ = eventData[P_QUALIFIERS].GetInt();
- int key = eventData[P_KEY].GetInt();
-
- UIElement* element = GetFocusElement();
- if (element)
- {
- // Switch focus between focusable elements in the same top level window
- if (key == KEY_TAB)
- {
- UIElement* topLevel = element->GetParent();
- while (topLevel && topLevel->GetParent() != rootElement_)
- topLevel = topLevel->GetParent();
- if (topLevel)
- {
- topLevel->GetChildren(tempElements_, true);
- for (PODVector<UIElement*>::Iterator i = tempElements_.Begin(); i != tempElements_.End();)
- {
- if ((*i)->GetFocusMode() < FM_FOCUSABLE)
- i = tempElements_.Erase(i);
- else
- ++i;
- }
- for (unsigned i = 0; i < tempElements_.Size(); ++i)
- {
- if (tempElements_[i] == element)
- {
- UIElement* next = tempElements_[(i + 1) % tempElements_.Size()];
- SetFocusElement(next);
- return;
- }
- }
- }
- }
- // Defocus the element
- else if (key == KEY_ESC && element->GetFocusMode() == FM_FOCUSABLE_DEFOCUSABLE)
- element->SetFocus(false);
- // If none of the special keys, pass the key to the focused element
- else
- element->OnKey(key, mouseButtons_, qualifiers_);
- }
- }
- void UI::HandleChar(StringHash eventType, VariantMap& eventData)
- {
- using namespace Char;
-
- mouseButtons_ = eventData[P_BUTTONS].GetInt();
- qualifiers_ = eventData[P_QUALIFIERS].GetInt();
-
- UIElement* element = GetFocusElement();
- if (element)
- element->OnChar(eventData[P_CHAR].GetInt(), mouseButtons_, qualifiers_);
- }
- void UI::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
- {
- if (initialized_)
- {
- using namespace PostUpdate;
-
- Update(eventData[P_TIMESTEP].GetFloat());
- }
- }
- void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
- {
- if (initialized_)
- RenderUpdate();
- }
- void RegisterUILibrary(Context* context)
- {
- Font::RegisterObject(context);
-
- UIElement::RegisterObject(context);
- BorderImage::RegisterObject(context);
- Button::RegisterObject(context);
- CheckBox::RegisterObject(context);
- Cursor::RegisterObject(context);
- Text::RegisterObject(context);
- Window::RegisterObject(context);
- LineEdit::RegisterObject(context);
- Slider::RegisterObject(context);
- ScrollBar::RegisterObject(context);
- ScrollView::RegisterObject(context);
- ListView::RegisterObject(context);
- Menu::RegisterObject(context);
- DropDownList::RegisterObject(context);
- FileSelector::RegisterObject(context);
- }
|