direction.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SuperTux
  2. // Copyright (C) 2008 Ryan Flegel <rflegel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <vector>
  17. #include "supertux/direction.hpp"
  18. #include "editor/object_option.hpp"
  19. #include "util/gettext.hpp"
  20. #include "util/log.hpp"
  21. std::ostream& operator<<(std::ostream& o, const Direction& dir)
  22. {
  23. return o << dir_to_string(dir);
  24. }
  25. std::string
  26. dir_to_string(const Direction& dir)
  27. {
  28. switch (dir)
  29. {
  30. case Direction::NONE:
  31. return "none";
  32. case Direction::LEFT:
  33. return "left";
  34. case Direction::RIGHT:
  35. return "right";
  36. case Direction::UP:
  37. return "up";
  38. case Direction::DOWN:
  39. return "down";
  40. default:
  41. if (dir != Direction::AUTO)
  42. {
  43. // Display a warning when an invalid direction has been provided.
  44. log_warning << "Unknown direction \"" << dir << "\". Switching to \"auto\"." << std::endl;
  45. }
  46. return "auto";
  47. }
  48. }
  49. std::string
  50. dir_to_translated_string(const Direction& dir)
  51. {
  52. switch (dir)
  53. {
  54. case Direction::NONE:
  55. return _("none");
  56. case Direction::LEFT:
  57. return _("left");
  58. case Direction::RIGHT:
  59. return _("right");
  60. case Direction::UP:
  61. return _("up");
  62. case Direction::DOWN:
  63. return _("down");
  64. default:
  65. if (dir != Direction::AUTO)
  66. {
  67. // Display a warning when an invalid direction has been provided.
  68. log_warning << "Unknown direction \"" << dir << "\". Switching to \"auto\"." << std::endl;
  69. }
  70. return _("auto");
  71. }
  72. }
  73. Direction
  74. string_to_dir(const std::string& dir_str)
  75. {
  76. if (dir_str == "none")
  77. return Direction::NONE;
  78. else if (dir_str == "left")
  79. return Direction::LEFT;
  80. else if (dir_str == "right")
  81. return Direction::RIGHT;
  82. else if (dir_str == "up")
  83. return Direction::UP;
  84. else if (dir_str == "down")
  85. return Direction::DOWN;
  86. else
  87. {
  88. if (dir_str != "auto")
  89. {
  90. // Display a warning when an invalid direction has been provided.
  91. log_warning << "Unknown direction \"" << dir_str << "\". Switching to \"auto\"." << std::endl;
  92. }
  93. return Direction::AUTO;
  94. }
  95. }
  96. Direction
  97. invert_dir(const Direction& dir)
  98. {
  99. switch (dir)
  100. {
  101. case Direction::RIGHT:
  102. return Direction::LEFT;
  103. case Direction::LEFT:
  104. return Direction::RIGHT;
  105. case Direction::DOWN:
  106. return Direction::UP;
  107. case Direction::UP:
  108. return Direction::DOWN;
  109. default:
  110. return Direction::NONE;
  111. }
  112. }
  113. /* EOF */