💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.5 KiB

  1. #include "toolbar.hpp"
  2. #include <algorithm>
  3. Toolbar::Toolbar():
  4. toolbarTopTexture(nullptr),
  5. toolbarBottomTexture(nullptr),
  6. toolbarMiddleTexture(nullptr),
  7. buttonRaisedTexture(nullptr),
  8. buttonDepressedTexture(nullptr),
  9. depressedButtonIndex(0)
  10. {
  11. toolbarContainer.addChild(&toolbarTopImage);
  12. toolbarContainer.addChild(&toolbarBottomImage);
  13. toolbarContainer.addChild(&toolbarMiddleImage);
  14. }
  15. void Toolbar::setToolbarTopTexture(Texture* texture)
  16. {
  17. toolbarTopTexture = texture;
  18. toolbarTopImage.setTexture(toolbarTopTexture);
  19. }
  20. void Toolbar::setToolbarBottomTexture(Texture* texture)
  21. {
  22. toolbarBottomTexture = texture;
  23. toolbarBottomImage.setTexture(toolbarBottomTexture);
  24. }
  25. void Toolbar::setToolbarMiddleTexture(Texture* texture)
  26. {
  27. toolbarMiddleTexture = texture;
  28. toolbarMiddleImage.setTexture(toolbarMiddleTexture);
  29. }
  30. void Toolbar::setButtonRaisedTexture(Texture* texture)
  31. {
  32. buttonRaisedTexture = texture;
  33. }
  34. void Toolbar::setButtonDepressedTexture(Texture* texture)
  35. {
  36. buttonDepressedTexture = texture;
  37. }
  38. void Toolbar::resize()
  39. {
  40. int toolbarWidth = toolbarMiddleTexture->getWidth();
  41. int toolbarHeight = toolbarTopTexture->getHeight() + toolbarBottomTexture->getHeight() + toolbarMiddleTexture->getHeight() * std::max<int>(0, (int)buttons.size() - 1);
  42. float borderSpacing = 8.0f;
  43. float buttonOffsetY = ((toolbarTopTexture->getHeight() + toolbarBottomTexture->getHeight()) - buttonRaisedTexture->getHeight()) / 2;
  44. // Resize toolbar
  45. toolbarContainer.setAnchor(Vector2(0.0f, 0.5f));
  46. toolbarContainer.setDimensions(Vector2(toolbarWidth, toolbarHeight));
  47. toolbarContainer.setTranslation(Vector2(borderSpacing, 0.0f));
  48. toolbarTopImage.setAnchor(Vector2(0.0f, 0.0f));
  49. toolbarTopImage.setDimensions(Vector2(toolbarTopTexture->getWidth(), toolbarTopTexture->getHeight()));
  50. toolbarTopImage.setTranslation(Vector2(0.0f, 0.0f));
  51. toolbarBottomImage.setAnchor(Vector2(0.0f, 1.0f));
  52. toolbarBottomImage.setDimensions(Vector2(toolbarBottomTexture->getWidth(), toolbarBottomTexture->getHeight()));
  53. toolbarBottomImage.setTranslation(Vector2(0.0f, 0.0f));
  54. toolbarMiddleImage.setAnchor(Vector2(0.0f, 0.5f));
  55. toolbarMiddleImage.setDimensions(Vector2(toolbarMiddleTexture->getWidth(), toolbarMiddleTexture->getHeight() * std::max<int>(0, (int)buttons.size() - 1)));
  56. toolbarMiddleImage.setTranslation(Vector2(0.0f, 0.0f));
  57. // Resize buttons and icons
  58. for (std::size_t i = 0; i < buttons.size(); ++i)
  59. {
  60. UIImage* button = buttons[i];
  61. button->setAnchor(Vector2(0.5f, 0.0f));
  62. button->setDimensions(Vector2(buttonRaisedTexture->getWidth(), buttonRaisedTexture->getHeight()));
  63. button->setTranslation(Vector2(0.0f, buttonOffsetY + i * toolbarMiddleTexture->getHeight()));
  64. UIImage* icon = icons[i];
  65. icon->setAnchor(Vector2(0.5f, 0.5f));
  66. icon->setDimensions(Vector2(icon->getTexture()->getWidth(), icon->getTexture()->getHeight()));
  67. icon->setTranslation(Vector2(0.0f, 0.0f));
  68. }
  69. }
  70. void Toolbar::addButton(Texture* iconTexture, std::function<void()> pressCallback, std::function<void()> releaseCallback)
  71. {
  72. if (depressedButtonIndex == buttons.size())
  73. {
  74. ++depressedButtonIndex;
  75. }
  76. // Allocate new button and icon
  77. UIImage* button = new UIImage();
  78. button->setTexture(buttonRaisedTexture);
  79. buttons.push_back(button);
  80. UIImage* icon = new UIImage();
  81. icon->setTexture(iconTexture);
  82. icon->setTintColor(Vector4(1.0f, 1.0f, 1.0f, 0.30f));
  83. icons.push_back(icon);
  84. // Add button to toolbar
  85. toolbarContainer.addChild(button);
  86. // Add icon to button
  87. button->addChild(icon);
  88. // Setup callbacks
  89. std::size_t buttonIndex = buttons.size() - 1;
  90. //button->setMouseOverCallback(std::bind(&Toolbar::selectMenuItem, this, buttonIndex));
  91. //button->setMouseMovedCallback(std::bind(&Toolbar::selectMenuItem, this, buttonIndex));
  92. button->setMousePressedCallback(std::bind(&Toolbar::pressButton, this, buttonIndex));
  93. pressCallbacks.push_back(pressCallback);
  94. releaseCallbacks.push_back(releaseCallback);
  95. }
  96. void Toolbar::pressButton(std::size_t index)
  97. {
  98. releaseButton(depressedButtonIndex);
  99. if (index == depressedButtonIndex)
  100. {
  101. depressedButtonIndex = buttons.size();
  102. }
  103. else
  104. {
  105. depressedButtonIndex = index;
  106. buttons[index]->setTexture(buttonDepressedTexture);
  107. //icons[index]->setTranslation(Vector2(2.0f, 2.0f));
  108. icons[index]->setTintColor(Vector4(1.0f, 1.0f, 1.0f, 1.0f));
  109. pressCallbacks[index]();
  110. }
  111. }
  112. void Toolbar::releaseButton(std::size_t index)
  113. {
  114. if (index < buttons.size())
  115. {
  116. buttons[index]->setTexture(buttonRaisedTexture);
  117. //icons[index]->setTranslation(Vector2(0.0f, 0.0f));
  118. icons[index]->setTintColor(Vector4(1.0f, 1.0f, 1.0f, 0.30f));
  119. releaseCallbacks[index]();
  120. }
  121. }