CClickableHex.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "StdInc.h"
  2. #include "CClickableHex.h"
  3. #include "CBattleInterface.h"
  4. #include "../../lib/BattleState.h"
  5. #include "../CGameInfo.h"
  6. #include "../CPlayerInterface.h"
  7. #include "../../lib/CTownHandler.h"
  8. #include "../Graphics.h"
  9. #include "../../CCallback.h"
  10. #include "../../lib/CGeneralTextHandler.h"
  11. #include "../SDL_Extensions.h"
  12. #include "../GUIClasses.h"
  13. #include "CBattleConsole.h"
  14. #include "../UIFramework/CGuiHandler.h"
  15. SPoint CClickableHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * stack, const CBattleInterface * cbi)
  16. {
  17. SPoint ret(-500, -500); //returned value
  18. if(stack && stack->position < 0) //creatures in turrets
  19. {
  20. switch(stack->position)
  21. {
  22. case -2: //keep
  23. ret = graphics->wallPositions[cbi->siegeH->town->town->typeID][17];
  24. break;
  25. case -3: //lower turret
  26. ret = graphics->wallPositions[cbi->siegeH->town->town->typeID][18];
  27. break;
  28. case -4: //upper turret
  29. ret = graphics->wallPositions[cbi->siegeH->town->town->typeID][19];
  30. break;
  31. }
  32. }
  33. else
  34. {
  35. ret.y = -139 + 42 * (hexNum/GameConstants::BFIELD_WIDTH); //counting y
  36. //counting x
  37. if(attacker)
  38. {
  39. ret.x = -160 + 22 * ( ((hexNum/GameConstants::BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % GameConstants::BFIELD_WIDTH);
  40. }
  41. else
  42. {
  43. ret.x = -219 + 22 * ( ((hexNum/GameConstants::BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % GameConstants::BFIELD_WIDTH);
  44. }
  45. //shifting position for double - hex creatures
  46. if(stack && stack->doubleWide())
  47. {
  48. if(attacker)
  49. {
  50. ret.x -= 44;
  51. }
  52. else
  53. {
  54. ret.x += 45;
  55. }
  56. }
  57. }
  58. //returning
  59. return ret +CPlayerInterface::battleInt->pos;
  60. }
  61. void CClickableHex::activate()
  62. {
  63. activateHover();
  64. activateMouseMove();
  65. activateLClick();
  66. activateRClick();
  67. }
  68. void CClickableHex::deactivate()
  69. {
  70. deactivateHover();
  71. deactivateMouseMove();
  72. deactivateLClick();
  73. deactivateRClick();
  74. }
  75. void CClickableHex::hover(bool on)
  76. {
  77. hovered = on;
  78. //Hoverable::hover(on);
  79. if(!on && setAlterText)
  80. {
  81. myInterface->console->alterTxt = std::string();
  82. setAlterText = false;
  83. }
  84. }
  85. CClickableHex::CClickableHex() : setAlterText(false), myNumber(-1), accessible(true), hovered(false), strictHovered(false), myInterface(NULL)
  86. {
  87. }
  88. void CClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  89. {
  90. if(myInterface->cellShade)
  91. {
  92. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  93. {
  94. strictHovered = false;
  95. }
  96. else //hovered pixel is inside hex
  97. {
  98. strictHovered = true;
  99. }
  100. }
  101. if(hovered && strictHovered) //print attacked creature to console
  102. {
  103. const CStack * attackedStack = myInterface->curInt->cb->battleGetStackByPos(myNumber);
  104. if(myInterface->console->alterTxt.size() == 0 &&attackedStack != NULL &&
  105. attackedStack->owner != myInterface->curInt->playerID &&
  106. attackedStack->alive())
  107. {
  108. char tabh[160];
  109. const std::string & attackedName = attackedStack->count == 1 ? attackedStack->getCreature()->nameSing : attackedStack->getCreature()->namePl;
  110. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  111. myInterface->console->alterTxt = std::string(tabh);
  112. setAlterText = true;
  113. }
  114. }
  115. else if(setAlterText)
  116. {
  117. myInterface->console->alterTxt = std::string();
  118. setAlterText = false;
  119. }
  120. }
  121. void CClickableHex::clickLeft(tribool down, bool previousState)
  122. {
  123. if(!down && hovered && strictHovered) //we've been really clicked!
  124. {
  125. myInterface->hexLclicked(myNumber);
  126. }
  127. }
  128. void CClickableHex::clickRight(tribool down, bool previousState)
  129. {
  130. const CStack * myst = myInterface->curInt->cb->battleGetStackByPos(myNumber); //stack info
  131. if(hovered && strictHovered && myst!=NULL)
  132. {
  133. if(!myst->alive()) return;
  134. if(down)
  135. {
  136. GH.pushInt(createCreWindow(myst));
  137. }
  138. }
  139. }