Buttons.cpp 9.9 KB

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