Buttons.cpp 11 KB

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