UISelectItem.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "UISelectItem.h"
  23. using namespace tb;
  24. namespace Atomic
  25. {
  26. UISelectItem::UISelectItem(Context* context, const String &str, const String &id, const String &skinImage) : Object(context)
  27. , subSource_(nullptr)
  28. {
  29. SetID(id);
  30. SetString(str);
  31. SetSkinImage(skinImage);
  32. }
  33. UISelectItem::~UISelectItem()
  34. {
  35. }
  36. void UISelectItem::SetID(const String& id)
  37. {
  38. id_ = TBID(id.CString());
  39. }
  40. void UISelectItem::SetSkinImage(const String& skinImage)
  41. {
  42. skinImage_ = TBID(skinImage.CString());
  43. }
  44. void UISelectItem::SetSubSource(UISelectItemSource *subSource)
  45. {
  46. subSource_ = subSource;
  47. }
  48. tb::TBGenericStringItem* UISelectItem::GetTBItem()
  49. {
  50. tb::TBGenericStringItem* item;
  51. if (!subSource_)
  52. {
  53. item = new tb::TBGenericStringItem(str_.CString(), id_);
  54. if (skinImage_)
  55. item->SetSkinImage(skinImage_);
  56. }
  57. else
  58. {
  59. item = new tb::TBGenericStringItem(str_.CString(), subSource_->GetTBItemSource());
  60. }
  61. return item;
  62. }
  63. // Source
  64. UISelectItemSource::UISelectItemSource(Context* context) : Object(context)
  65. {
  66. }
  67. UISelectItemSource::~UISelectItemSource()
  68. {
  69. }
  70. void UISelectItemSource::RemoveItemWithId(const String& id)
  71. {
  72. tb::TBID test = TBID(id.CString());
  73. for (List<SharedPtr<UISelectItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  74. {
  75. if ((*itr)->GetID() == test) {
  76. items_.Erase(itr);
  77. break;
  78. }
  79. }
  80. }
  81. void UISelectItemSource::RemoveItemWithStr(const String& str)
  82. {
  83. for (List<SharedPtr<UISelectItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  84. {
  85. if ((*itr)->GetStr() == str) {
  86. items_.Erase(itr);
  87. break;
  88. }
  89. }
  90. }
  91. const String& UISelectItemSource::GetItemStr(int index)
  92. {
  93. int nn = 0;
  94. for (List<SharedPtr<UISelectItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  95. {
  96. if ( nn == index) return (*itr)->GetStr();
  97. nn++;
  98. }
  99. return ( String::EMPTY );
  100. }
  101. TBSelectItemSource *UISelectItemSource::GetTBItemSource()
  102. {
  103. // caller's responsibility to clean up
  104. TBGenericStringItemSource* src = new TBGenericStringItemSource();
  105. for (List<SharedPtr<UISelectItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++)
  106. {
  107. tb::TBGenericStringItem* tbitem = (*itr)->GetTBItem();
  108. src->AddItem(tbitem);
  109. }
  110. return src;
  111. }
  112. }