Selectable.cpp 1.4 KB

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