Buttons.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Buttons.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Buttons.h"
  12. #include "Images.h"
  13. #include "TextControls.h"
  14. #include "../CMusicHandler.h"
  15. #include "../CGameInfo.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../battle/BattleInterface.h"
  18. #include "../battle/BattleInterfaceClasses.h"
  19. #include "../gui/CGuiHandler.h"
  20. #include "../gui/MouseButton.h"
  21. #include "../gui/Shortcut.h"
  22. #include "../windows/InfoWindows.h"
  23. #include "../render/CAnimation.h"
  24. #include "../render/Canvas.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. void CButton::update()
  28. {
  29. if (overlay)
  30. {
  31. Point targetPos = Rect::createCentered( pos, overlay->pos.dimensions()).topLeft();
  32. if (state == PRESSED)
  33. overlay->moveTo(targetPos + Point(1,1));
  34. else
  35. overlay->moveTo(targetPos);
  36. }
  37. int newPos = stateToIndex[int(state)];
  38. if(animateLonelyFrame)
  39. {
  40. if(state == PRESSED)
  41. image->moveBy(Point(1,1));
  42. else
  43. image->moveBy(Point(-1,-1));
  44. }
  45. if (newPos < 0)
  46. newPos = 0;
  47. if (state == HIGHLIGHTED && image->size() < 4)
  48. newPos = (int)image->size()-1;
  49. image->setFrame(newPos);
  50. if (isActive())
  51. redraw();
  52. }
  53. void CButton::setBorderColor(std::optional<SDL_Color> borderColor)
  54. {
  55. setBorderColor(borderColor, borderColor, borderColor, borderColor);
  56. }
  57. void CButton::setBorderColor(std::optional<SDL_Color> normalBorderColor,
  58. std::optional<SDL_Color> pressedBorderColor,
  59. std::optional<SDL_Color> blockedBorderColor,
  60. std::optional<SDL_Color> highlightedBorderColor)
  61. {
  62. stateToBorderColor[NORMAL] = normalBorderColor;
  63. stateToBorderColor[PRESSED] = pressedBorderColor;
  64. stateToBorderColor[BLOCKED] = blockedBorderColor;
  65. stateToBorderColor[HIGHLIGHTED] = highlightedBorderColor;
  66. update();
  67. }
  68. void CButton::addCallback(std::function<void()> callback)
  69. {
  70. this->callback += callback;
  71. }
  72. void CButton::addTextOverlay(const std::string & Text, EFonts font, SDL_Color color)
  73. {
  74. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  75. addOverlay(std::make_shared<CLabel>(pos.w/2, pos.h/2, font, ETextAlignment::CENTER, color, Text));
  76. update();
  77. }
  78. void CButton::addOverlay(std::shared_ptr<CIntObject> newOverlay)
  79. {
  80. overlay = newOverlay;
  81. if(overlay)
  82. {
  83. addChild(newOverlay.get());
  84. Point targetPos = Rect::createCentered( pos, overlay->pos.dimensions()).topLeft();
  85. overlay->moveTo(targetPos);
  86. }
  87. update();
  88. }
  89. void CButton::addImage(std::string filename)
  90. {
  91. imageNames.push_back(filename);
  92. }
  93. void CButton::addHoverText(ButtonState state, std::string text)
  94. {
  95. hoverTexts[state] = text;
  96. }
  97. void CButton::setImageOrder(int state1, int state2, int state3, int state4)
  98. {
  99. stateToIndex[0] = state1;
  100. stateToIndex[1] = state2;
  101. stateToIndex[2] = state3;
  102. stateToIndex[3] = state4;
  103. update();
  104. }
  105. void CButton::setAnimateLonelyFrame(bool agreement)
  106. {
  107. animateLonelyFrame = agreement;
  108. }
  109. void CButton::setState(ButtonState newState)
  110. {
  111. if (state == newState)
  112. return;
  113. state = newState;
  114. update();
  115. }
  116. CButton::ButtonState CButton::getState()
  117. {
  118. return state;
  119. }
  120. bool CButton::isBlocked()
  121. {
  122. return state == BLOCKED;
  123. }
  124. bool CButton::isHighlighted()
  125. {
  126. return state == HIGHLIGHTED;
  127. }
  128. void CButton::block(bool on)
  129. {
  130. if(on || state == BLOCKED) //dont change button state if unblock requested, but it's not blocked
  131. setState(on ? BLOCKED : NORMAL);
  132. }
  133. void CButton::onButtonClicked()
  134. {
  135. // debug logging to figure out pressed button (and as result - player actions) in case of crash
  136. logAnim->trace("Button clicked at %dx%d", pos.x, pos.y);
  137. CIntObject * parent = this->parent;
  138. std::string prefix = "Parent is";
  139. while (parent)
  140. {
  141. logAnim->trace("%s%s at %dx%d", prefix, typeid(*parent).name(), parent->pos.x, parent->pos.y);
  142. parent = parent->parent;
  143. prefix = '\t' + prefix;
  144. }
  145. callback();
  146. }
  147. void CButton::clickLeft(tribool down, bool previousState)
  148. {
  149. if(isBlocked())
  150. return;
  151. if (down)
  152. {
  153. if (getState() != PRESSED)
  154. {
  155. if (!soundDisabled)
  156. CCS->soundh->playSound(soundBase::button);
  157. setState(PRESSED);
  158. if (actOnDown)
  159. onButtonClicked();
  160. }
  161. }
  162. else
  163. {
  164. if (getState() == PRESSED)
  165. {
  166. if(hoverable && isHovered())
  167. setState(HIGHLIGHTED);
  168. else
  169. setState(NORMAL);
  170. if (!actOnDown && previousState && (down == false))
  171. onButtonClicked();
  172. }
  173. }
  174. }
  175. void CButton::clickRight(tribool down, bool previousState)
  176. {
  177. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  178. CRClickPopup::createAndPush(helpBox);
  179. }
  180. void CButton::hover (bool on)
  181. {
  182. if(hoverable && !isBlocked())
  183. {
  184. if(on)
  185. setState(HIGHLIGHTED);
  186. else
  187. setState(NORMAL);
  188. }
  189. /*if(pressedL && on) // WTF is this? When this is used?
  190. setState(PRESSED);*/
  191. std::string name = hoverTexts[getState()].empty()
  192. ? hoverTexts[0]
  193. : hoverTexts[getState()];
  194. if(!name.empty() && !isBlocked()) //if there is no name, there is nothing to display also
  195. {
  196. if (on)
  197. GH.statusbar()->write(name);
  198. else
  199. GH.statusbar()->clearIfMatching(name);
  200. }
  201. }
  202. CButton::CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help, CFunctionList<void()> Callback, EShortcut key, bool playerColoredButton):
  203. CKeyShortcut(key),
  204. callback(Callback)
  205. {
  206. defActions = 255-DISPOSE;
  207. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  208. stateToIndex[0] = 0;
  209. stateToIndex[1] = 1;
  210. stateToIndex[2] = 2;
  211. stateToIndex[3] = 3;
  212. state=NORMAL;
  213. currentImage = -1;
  214. hoverable = actOnDown = soundDisabled = false;
  215. hoverTexts[0] = help.first;
  216. helpBox=help.second;
  217. pos.x += position.x;
  218. pos.y += position.y;
  219. if (!defName.empty())
  220. {
  221. imageNames.push_back(defName);
  222. setIndex(0);
  223. if (playerColoredButton)
  224. image->playerColored(LOCPLINT->playerID);
  225. }
  226. }
  227. void CButton::setIndex(size_t index)
  228. {
  229. if (index == currentImage || index>=imageNames.size())
  230. return;
  231. currentImage = index;
  232. auto anim = std::make_shared<CAnimation>(imageNames[index]);
  233. setImage(anim);
  234. }
  235. void CButton::setImage(std::shared_ptr<CAnimation> anim, int animFlags)
  236. {
  237. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  238. image = std::make_shared<CAnimImage>(anim, getState(), 0, 0, 0, animFlags);
  239. pos = image->pos;
  240. }
  241. void CButton::setPlayerColor(PlayerColor player)
  242. {
  243. if (image && image->isPlayerColored())
  244. image->playerColored(player);
  245. }
  246. void CButton::showAll(Canvas & to)
  247. {
  248. CIntObject::showAll(to);
  249. auto borderColor = stateToBorderColor[getState()];
  250. if (borderColor)
  251. to.drawBorder(Rect::createAround(pos, 1), *borderColor);
  252. }
  253. std::pair<std::string, std::string> CButton::tooltip()
  254. {
  255. return std::pair<std::string, std::string>();
  256. }
  257. std::pair<std::string, std::string> CButton::tooltipLocalized(const std::string & key)
  258. {
  259. return std::make_pair(
  260. CGI->generaltexth->translate(key, "hover"),
  261. CGI->generaltexth->translate(key, "help")
  262. );
  263. }
  264. std::pair<std::string, std::string> CButton::tooltip(const std::string & hover, const std::string & help)
  265. {
  266. return std::make_pair(hover, help);
  267. }
  268. CToggleBase::CToggleBase(CFunctionList<void (bool)> callback):
  269. callback(callback),
  270. selected(false),
  271. allowDeselection(true)
  272. {
  273. }
  274. CToggleBase::~CToggleBase() = default;
  275. void CToggleBase::doSelect(bool on)
  276. {
  277. // for overrides
  278. }
  279. void CToggleBase::setEnabled(bool enabled)
  280. {
  281. // for overrides
  282. }
  283. void CToggleBase::setSelected(bool on)
  284. {
  285. bool changed = (on != selected);
  286. selected = on;
  287. doSelect(on);
  288. if (changed)
  289. callback(on);
  290. }
  291. bool CToggleBase::canActivate()
  292. {
  293. if (selected && !allowDeselection)
  294. return false;
  295. return true;
  296. }
  297. void CToggleBase::addCallback(std::function<void(bool)> function)
  298. {
  299. callback += function;
  300. }
  301. CToggleButton::CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  302. CFunctionList<void(bool)> callback, EShortcut key, bool playerColoredButton):
  303. CButton(position, defName, help, 0, key, playerColoredButton),
  304. CToggleBase(callback)
  305. {
  306. allowDeselection = true;
  307. }
  308. void CToggleButton::doSelect(bool on)
  309. {
  310. if (on)
  311. {
  312. setState(HIGHLIGHTED);
  313. }
  314. else
  315. {
  316. setState(NORMAL);
  317. }
  318. }
  319. void CToggleButton::setEnabled(bool enabled)
  320. {
  321. setState(enabled ? NORMAL : BLOCKED);
  322. }
  323. void CToggleButton::clickLeft(tribool down, bool previousState)
  324. {
  325. // force refresh
  326. hover(false);
  327. hover(true);
  328. if(isBlocked())
  329. return;
  330. if (down && canActivate())
  331. {
  332. CCS->soundh->playSound(soundBase::button);
  333. setState(PRESSED);
  334. }
  335. if(previousState)//mouse up
  336. {
  337. if(down == false && getState() == PRESSED && canActivate())
  338. {
  339. onButtonClicked();
  340. setSelected(!selected);
  341. }
  342. else
  343. doSelect(selected); // restore
  344. }
  345. }
  346. void CToggleGroup::addCallback(std::function<void(int)> callback)
  347. {
  348. onChange += callback;
  349. }
  350. void CToggleGroup::resetCallback()
  351. {
  352. onChange.clear();
  353. }
  354. void CToggleGroup::addToggle(int identifier, std::shared_ptr<CToggleBase> button)
  355. {
  356. if(auto intObj = std::dynamic_pointer_cast<CIntObject>(button)) // hack-ish workagound to avoid diamond problem with inheritance
  357. {
  358. addChild(intObj.get());
  359. }
  360. button->addCallback([=] (bool on) { if (on) selectionChanged(identifier);});
  361. button->allowDeselection = false;
  362. if(buttons.count(identifier)>0)
  363. logAnim->error("Duplicated toggle button id %d", identifier);
  364. buttons[identifier] = button;
  365. }
  366. CToggleGroup::CToggleGroup(const CFunctionList<void(int)> &OnChange)
  367. : onChange(OnChange), selectedID(-2)
  368. {
  369. }
  370. void CToggleGroup::setSelected(int id)
  371. {
  372. selectionChanged(id);
  373. }
  374. void CToggleGroup::setSelectedOnly(int id)
  375. {
  376. for(auto it = buttons.begin(); it != buttons.end(); it++)
  377. {
  378. int buttonId = it->first;
  379. buttons[buttonId]->setEnabled(buttonId == id);
  380. }
  381. selectionChanged(id);
  382. }
  383. void CToggleGroup::selectionChanged(int to)
  384. {
  385. if (to == selectedID)
  386. return;
  387. int oldSelection = selectedID;
  388. selectedID = to;
  389. if (buttons.count(oldSelection))
  390. buttons[oldSelection]->setSelected(false);
  391. if (buttons.count(to))
  392. buttons[to]->setSelected(true);
  393. onChange(to);
  394. redraw();
  395. }
  396. int CToggleGroup::getSelected() const
  397. {
  398. return selectedID;
  399. }