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