CBattleConsole.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "StdInc.h"
  2. #include "CBattleConsole.h"
  3. #include "../SDL_Extensions.h"
  4. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt(""), whoSetAlter(0)
  5. {
  6. }
  7. CBattleConsole::~CBattleConsole()
  8. {
  9. texts.clear();
  10. }
  11. void CBattleConsole::show(SDL_Surface * to)
  12. {
  13. if(ingcAlter.size())
  14. {
  15. CSDL_Ext::printAtMiddleWB(ingcAlter, pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, zwykly, to);
  16. }
  17. else if(alterTxt.size())
  18. {
  19. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, zwykly, to);
  20. }
  21. else if(texts.size())
  22. {
  23. if(texts.size()==1)
  24. {
  25. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, zwykly, to);
  26. }
  27. else
  28. {
  29. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, zwykly, to);
  30. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 27, FONT_SMALL, 80, zwykly, to);
  31. }
  32. }
  33. }
  34. bool CBattleConsole::addText(const std::string & text)
  35. {
  36. if(text.size()>70)
  37. return false; //text too long!
  38. int firstInToken = 0;
  39. for(size_t i = 0; i < text.size(); ++i) //tokenize
  40. {
  41. if(text[i] == 10)
  42. {
  43. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  44. firstInToken = i+1;
  45. }
  46. }
  47. texts.push_back( text.substr(firstInToken, text.size()) );
  48. lastShown = texts.size()-1;
  49. return true;
  50. }
  51. void CBattleConsole::eraseText(ui32 pos)
  52. {
  53. if(pos < texts.size())
  54. {
  55. texts.erase(texts.begin() + pos);
  56. if(lastShown == texts.size())
  57. --lastShown;
  58. }
  59. }
  60. void CBattleConsole::changeTextAt(const std::string & text, ui32 pos)
  61. {
  62. if(pos >= texts.size()) //no such pos
  63. return;
  64. texts[pos] = text;
  65. }
  66. void CBattleConsole::scrollUp(ui32 by)
  67. {
  68. if(lastShown > static_cast<int>(by))
  69. lastShown -= by;
  70. }
  71. void CBattleConsole::scrollDown(ui32 by)
  72. {
  73. if(lastShown + by < texts.size())
  74. lastShown += by;
  75. }