Buttons.cpp 9.8 KB

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