Buttons.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. state = newState;
  115. update();
  116. }
  117. CButton::ButtonState CButton::getState()
  118. {
  119. return state;
  120. }
  121. bool CButton::isBlocked()
  122. {
  123. return state == BLOCKED;
  124. }
  125. bool CButton::isHighlighted()
  126. {
  127. return state == HIGHLIGHTED;
  128. }
  129. void CButton::block(bool on)
  130. {
  131. if(on || state == BLOCKED) //dont change button state if unblock requested, but it's not blocked
  132. setState(on ? BLOCKED : NORMAL);
  133. }
  134. void CButton::onButtonClicked()
  135. {
  136. // debug logging to figure out pressed button (and as result - player actions) in case of crash
  137. logAnim->trace("Button clicked at %dx%d", pos.x, pos.y);
  138. CIntObject * parent = this->parent;
  139. std::string prefix = "Parent is";
  140. while (parent)
  141. {
  142. logAnim->trace("%s%s at %dx%d", prefix, typeid(*parent).name(), parent->pos.x, parent->pos.y);
  143. parent = parent->parent;
  144. prefix = '\t' + prefix;
  145. }
  146. callback();
  147. }
  148. void CButton::clickPressed(const Point & cursorPosition)
  149. {
  150. if(isBlocked())
  151. return;
  152. if (getState() != PRESSED)
  153. {
  154. if (!soundDisabled)
  155. {
  156. CCS->soundh->playSound(soundBase::button);
  157. GH.input().hapticFeedback();
  158. }
  159. setState(PRESSED);
  160. if (actOnDown)
  161. onButtonClicked();
  162. }
  163. }
  164. void CButton::clickReleased(const Point & cursorPosition)
  165. {
  166. if (getState() == PRESSED)
  167. {
  168. if(hoverable && isHovered())
  169. setState(HIGHLIGHTED);
  170. else
  171. setState(NORMAL);
  172. if (!actOnDown)
  173. onButtonClicked();
  174. }
  175. }
  176. void CButton::clickCancel(const Point & cursorPosition)
  177. {
  178. if (getState() == PRESSED)
  179. {
  180. if(hoverable && isHovered())
  181. setState(HIGHLIGHTED);
  182. else
  183. setState(NORMAL);
  184. }
  185. }
  186. void CButton::showPopupWindow(const Point & cursorPosition)
  187. {
  188. if(helpBox.size()) //there is no point to show window with nothing inside...
  189. CRClickPopup::createAndPush(helpBox);
  190. }
  191. void CButton::hover (bool on)
  192. {
  193. if(hoverable && !isBlocked())
  194. {
  195. if(on)
  196. setState(HIGHLIGHTED);
  197. else
  198. setState(NORMAL);
  199. }
  200. /*if(pressedL && on) // WTF is this? When this is used?
  201. setState(PRESSED);*/
  202. std::string name = hoverTexts[getState()].empty()
  203. ? hoverTexts[0]
  204. : hoverTexts[getState()];
  205. if(!name.empty() && !isBlocked()) //if there is no name, there is nothing to display also
  206. {
  207. if (on)
  208. GH.statusbar()->write(name);
  209. else
  210. GH.statusbar()->clearIfMatching(name);
  211. }
  212. }
  213. CButton::CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help, CFunctionList<void()> Callback, EShortcut key, bool playerColoredButton):
  214. CKeyShortcut(key),
  215. callback(Callback)
  216. {
  217. defActions = 255-DISPOSE;
  218. addUsedEvents(LCLICK | SHOW_POPUP | HOVER | KEYBOARD);
  219. stateToIndex[0] = 0;
  220. stateToIndex[1] = 1;
  221. stateToIndex[2] = 2;
  222. stateToIndex[3] = 3;
  223. state=NORMAL;
  224. currentImage = -1;
  225. hoverable = actOnDown = soundDisabled = false;
  226. hoverTexts[0] = help.first;
  227. helpBox=help.second;
  228. pos.x += position.x;
  229. pos.y += position.y;
  230. if (!defName.empty())
  231. {
  232. imageNames.push_back(defName);
  233. setIndex(0);
  234. if (playerColoredButton)
  235. image->playerColored(LOCPLINT->playerID);
  236. }
  237. }
  238. void CButton::setIndex(size_t index)
  239. {
  240. if (index == currentImage || index>=imageNames.size())
  241. return;
  242. currentImage = index;
  243. auto anim = std::make_shared<CAnimation>(imageNames[index]);
  244. setImage(anim);
  245. }
  246. void CButton::setImage(std::shared_ptr<CAnimation> anim, int animFlags)
  247. {
  248. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  249. image = std::make_shared<CAnimImage>(anim, getState(), 0, 0, 0, animFlags);
  250. pos = image->pos;
  251. }
  252. void CButton::setPlayerColor(PlayerColor player)
  253. {
  254. if (image && image->isPlayerColored())
  255. image->playerColored(player);
  256. }
  257. void CButton::showAll(Canvas & to)
  258. {
  259. CIntObject::showAll(to);
  260. auto borderColor = stateToBorderColor[getState()];
  261. if (borderColor)
  262. to.drawBorder(Rect::createAround(pos, 1), *borderColor);
  263. }
  264. std::pair<std::string, std::string> CButton::tooltip()
  265. {
  266. return std::pair<std::string, std::string>();
  267. }
  268. std::pair<std::string, std::string> CButton::tooltipLocalized(const std::string & key)
  269. {
  270. return std::make_pair(
  271. CGI->generaltexth->translate(key, "hover"),
  272. CGI->generaltexth->translate(key, "help")
  273. );
  274. }
  275. std::pair<std::string, std::string> CButton::tooltip(const std::string & hover, const std::string & help)
  276. {
  277. return std::make_pair(hover, help);
  278. }
  279. CToggleBase::CToggleBase(CFunctionList<void (bool)> callback):
  280. callback(callback),
  281. selected(false),
  282. allowDeselection(true)
  283. {
  284. }
  285. CToggleBase::~CToggleBase() = default;
  286. void CToggleBase::doSelect(bool on)
  287. {
  288. // for overrides
  289. }
  290. void CToggleBase::setEnabled(bool enabled)
  291. {
  292. // for overrides
  293. }
  294. void CToggleBase::setSelectedSilent(bool on)
  295. {
  296. selected = on;
  297. doSelect(on);
  298. }
  299. void CToggleBase::setSelected(bool on)
  300. {
  301. bool changed = (on != selected);
  302. setSelectedSilent(on);
  303. if (changed)
  304. callback(on);
  305. }
  306. bool CToggleBase::canActivate()
  307. {
  308. if (selected && !allowDeselection)
  309. return false;
  310. return true;
  311. }
  312. void CToggleBase::addCallback(std::function<void(bool)> function)
  313. {
  314. callback += function;
  315. }
  316. CToggleButton::CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  317. CFunctionList<void(bool)> callback, EShortcut key, bool playerColoredButton):
  318. CButton(position, defName, help, 0, key, playerColoredButton),
  319. CToggleBase(callback)
  320. {
  321. allowDeselection = true;
  322. }
  323. void CToggleButton::doSelect(bool on)
  324. {
  325. if (on)
  326. {
  327. setState(HIGHLIGHTED);
  328. }
  329. else
  330. {
  331. setState(NORMAL);
  332. }
  333. }
  334. void CToggleButton::setEnabled(bool enabled)
  335. {
  336. setState(enabled ? NORMAL : BLOCKED);
  337. }
  338. void CToggleButton::clickPressed(const Point & cursorPosition)
  339. {
  340. // force refresh
  341. hover(false);
  342. hover(true);
  343. if(isBlocked())
  344. return;
  345. if (canActivate())
  346. {
  347. CCS->soundh->playSound(soundBase::button);
  348. GH.input().hapticFeedback();
  349. setState(PRESSED);
  350. }
  351. }
  352. void CToggleButton::clickReleased(const Point & cursorPosition)
  353. {
  354. // force refresh
  355. hover(false);
  356. hover(true);
  357. if(isBlocked())
  358. return;
  359. if (getState() == PRESSED && canActivate())
  360. {
  361. onButtonClicked();
  362. setSelected(!selected);
  363. }
  364. else
  365. doSelect(selected); // restore
  366. }
  367. void CToggleButton::clickCancel(const Point & cursorPosition)
  368. {
  369. // force refresh
  370. hover(false);
  371. hover(true);
  372. if(isBlocked())
  373. return;
  374. doSelect(selected);
  375. }
  376. void CToggleGroup::addCallback(std::function<void(int)> callback)
  377. {
  378. onChange += callback;
  379. }
  380. void CToggleGroup::resetCallback()
  381. {
  382. onChange.clear();
  383. }
  384. void CToggleGroup::addToggle(int identifier, std::shared_ptr<CToggleBase> button)
  385. {
  386. if(auto intObj = std::dynamic_pointer_cast<CIntObject>(button)) // hack-ish workagound to avoid diamond problem with inheritance
  387. {
  388. addChild(intObj.get());
  389. }
  390. button->addCallback([=] (bool on) { if (on) selectionChanged(identifier);});
  391. button->allowDeselection = false;
  392. if(buttons.count(identifier)>0)
  393. logAnim->error("Duplicated toggle button id %d", identifier);
  394. buttons[identifier] = button;
  395. }
  396. CToggleGroup::CToggleGroup(const CFunctionList<void(int)> &OnChange)
  397. : onChange(OnChange), selectedID(-2)
  398. {
  399. }
  400. void CToggleGroup::setSelected(int id)
  401. {
  402. selectionChanged(id);
  403. }
  404. void CToggleGroup::setSelectedOnly(int id)
  405. {
  406. for(auto it = buttons.begin(); it != buttons.end(); it++)
  407. {
  408. int buttonId = it->first;
  409. buttons[buttonId]->setEnabled(buttonId == id);
  410. }
  411. selectionChanged(id);
  412. }
  413. void CToggleGroup::selectionChanged(int to)
  414. {
  415. if (to == selectedID)
  416. return;
  417. int oldSelection = selectedID;
  418. selectedID = to;
  419. if (buttons.count(oldSelection))
  420. buttons[oldSelection]->setSelected(false);
  421. if (buttons.count(to))
  422. buttons[to]->setSelected(true);
  423. onChange(to);
  424. redraw();
  425. }
  426. int CToggleGroup::getSelected() const
  427. {
  428. return selectedID;
  429. }