UISelectable.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../UI/UISelectable.h"
  6. namespace Urho3D
  7. {
  8. extern const char* UI_CATEGORY;
  9. void UISelectable::RegisterObject(Context* context)
  10. {
  11. context->RegisterFactory<UISelectable>(UI_CATEGORY);
  12. URHO3D_COPY_BASE_ATTRIBUTES(UIElement);
  13. URHO3D_ATTRIBUTE("Selection Color", selectionColor_, Color::TRANSPARENT_BLACK, AM_FILE);
  14. URHO3D_ATTRIBUTE("Hover Color", hoverColor_, Color::TRANSPARENT_BLACK, AM_FILE);
  15. }
  16. void UISelectable::GetBatches(Vector<UIBatch>& batches, Vector<float>& vertexData, const IntRect & currentScissor)
  17. {
  18. // Hovering and/or whole selection batch
  19. if ((hovering_ && hoverColor_.a_ > 0.0) || (selected_ && selectionColor_.a_ > 0.0f))
  20. {
  21. bool both = hovering_ && selected_ && hoverColor_.a_ > 0.0 && selectionColor_.a_ > 0.0f;
  22. UIBatch batch(this, BLEND_ALPHA, currentScissor, nullptr, &vertexData);
  23. batch.SetColor(both ? selectionColor_.Lerp(hoverColor_, 0.5f) :
  24. (selected_ && selectionColor_.a_ > 0.0f ? selectionColor_ : hoverColor_));
  25. batch.AddQuad(0.f, 0.f, (float)GetWidth(), (float)GetHeight(), 0, 0);
  26. UIBatch::AddOrMerge(batch, batches);
  27. }
  28. // Reset hovering for next frame
  29. hovering_ = false;
  30. }
  31. void UISelectable::SetSelectionColor(const Color& color)
  32. {
  33. selectionColor_ = color;
  34. }
  35. void UISelectable::SetHoverColor(const Color& color)
  36. {
  37. hoverColor_ = color;
  38. }
  39. }