💿🐜 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.

145 lines
4.5 KiB

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