Buttons.cpp 10 KB

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