UIMenubar.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <Atomic/UI/TBUI.h>
  6. #include <Atomic/IO/Log.h>
  7. #include "UIMenubar.h"
  8. using namespace tb;
  9. namespace tb {
  10. // THIS MUST MATCH TBSimpleLayoutItemWidget in tb_select_item.cpp
  11. class TBSimpleLayoutItemWidget : public TBLayout, private TBWidgetListener
  12. {
  13. public:
  14. TBSimpleLayoutItemWidget(TBID image, TBSelectItemSource *source, const char *str);
  15. ~TBSimpleLayoutItemWidget();
  16. virtual bool OnEvent(const TBWidgetEvent &ev);
  17. private:
  18. TBSelectItemSource *m_source;
  19. TBTextField m_textfield;
  20. TBSkinImage m_image;
  21. TBSkinImage m_image_arrow;
  22. TBMenuWindow *m_menu; ///< Points to the submenu window if opened
  23. virtual void OnWidgetDelete(TBWidget *widget);
  24. void OpenSubMenu();
  25. void CloseSubMenu();
  26. };
  27. }
  28. namespace AtomicEditor
  29. {
  30. // == AdvancedItemWidget ======================================================
  31. MenubarItemWidget::MenubarItemWidget(MenubarItem *item, MenubarItemSource *source,
  32. TBSelectItemViewer *source_viewer, int index)
  33. : m_source(source)
  34. , m_source_viewer(source_viewer)
  35. , m_index(index)
  36. {
  37. SetSkinBg(TBIDC("TBSelectItem"));
  38. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  39. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  40. SetPaintOverflowFadeout(false);
  41. TBWidget* root = GetContentRoot();
  42. TBFontDescription fd;
  43. fd.SetID(TBIDC("Vera"));
  44. fd.SetSize(12);
  45. TBTextField* text = new TBTextField();
  46. text->SetIgnoreInput(true);
  47. text->SetText(item->str);
  48. text->SetFontDescription(fd);
  49. root->AddChild(text);
  50. if (item->shortcut_.Length())
  51. {
  52. TBWidget* spacer = new TBWidget();
  53. spacer->SetIgnoreInput(true);
  54. spacer->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  55. root->AddChild(spacer);
  56. TBTextField* shortcut = new TBTextField();
  57. shortcut->SetIgnoreInput(true);
  58. shortcut->SetText(item->shortcut_.CString());
  59. shortcut->SetFontDescription(fd);
  60. shortcut->SetGravity(WIDGET_GRAVITY_RIGHT);
  61. root->AddChild(shortcut);
  62. }
  63. }
  64. bool MenubarItemWidget::OnEvent(const TBWidgetEvent &ev)
  65. {
  66. if (m_source && ev.type == EVENT_TYPE_CLICK && ev.target == this)
  67. {
  68. return false;
  69. }
  70. return false;
  71. }
  72. // == AdvancedItemSource ======================================================
  73. bool MenubarItemSource::Filter(int index, const char *filter)
  74. {
  75. return true;
  76. }
  77. TBWidget *MenubarItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  78. {
  79. const char *string = GetItemString(index);
  80. TBSelectItemSource *sub_source = GetItemSubSource(index);
  81. TBID image = GetItemImage(index);
  82. if (sub_source || image)
  83. {
  84. if (TBSimpleLayoutItemWidget *itemwidget = new TBSimpleLayoutItemWidget(image, sub_source, string))
  85. {
  86. itemwidget->SetID(GetItem(index)->id);
  87. return itemwidget;
  88. }
  89. }
  90. else if (string && *string == '-')
  91. {
  92. if (TBSeparator *separator = new TBSeparator)
  93. {
  94. separator->SetGravity(WIDGET_GRAVITY_ALL);
  95. separator->SetSkinBg(TBIDC("AESeparator"));
  96. return separator;
  97. }
  98. }
  99. else if (TBLayout *layout = new MenubarItemWidget(GetItem(index), this, viewer, index))
  100. {
  101. layout->SetID(GetItem(index)->id);
  102. return layout;
  103. }
  104. return NULL;
  105. }
  106. }