tile_editor.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  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 "tile_editor.hpp"
  17. #include <ClanLib/Display/display.h>
  18. #include <ClanLib/Display/keys.h>
  19. #include <ClanLib/Display/sprite.h>
  20. #include <ClanLib/Display/mouse.h>
  21. #include "tile.hpp"
  22. TileEditor::TileEditor(int x, int y, int w, int h, CL_Component* parent)
  23. : CL_Component(CL_Rect(CL_Rect(CL_Point(x, y),
  24. CL_Size(w, h))), // FIXME: make this editable via script
  25. parent)
  26. {
  27. tile = 0;
  28. slots.connect(sig_paint(), this, &TileEditor::draw);
  29. slots.connect(sig_mouse_move(), this, &TileEditor::mouse_move);
  30. slots.connect(sig_mouse_down(), this, &TileEditor::mouse_down);
  31. slots.connect(sig_mouse_up (), this, &TileEditor::mouse_up);
  32. }
  33. TileEditor::~TileEditor()
  34. {
  35. }
  36. void
  37. TileEditor::draw()
  38. {
  39. CL_Display::push_translate(get_screen_x(), get_screen_x());
  40. //no_tile.draw(0, 0);
  41. CL_Display::fill_rect(CL_Rect(0, 0, 32, 32), CL_Color(155, 0, 155));
  42. if (tile)
  43. {
  44. tile->get_sprite().draw(0, 0);
  45. CL_Display::flush();
  46. for(int tile_y = 0; tile_y < 8; ++tile_y)
  47. for(int tile_x = 0; tile_x < 8; ++tile_x)
  48. {
  49. if (tile->get_col(tile_x, tile_y))
  50. {
  51. CL_Display::fill_rect(CL_Rect(tile_x*16, tile_y*16,
  52. tile_x*16 + 16, tile_y*16 + 16),
  53. CL_Color(255, 0, 0, 128));
  54. }
  55. }
  56. CL_Display::flush();
  57. if (has_mouse_over())
  58. {
  59. CL_Display::fill_rect(CL_Rect(CL_Point(int(mouse_pos.x)/16 * 16,
  60. int(mouse_pos.y)/16 * 16),
  61. CL_Size(16, 16)),
  62. CL_Color(255, 255, 255, 128));
  63. }
  64. }
  65. else
  66. {
  67. }
  68. CL_Display::pop_modelview();
  69. }
  70. void
  71. TileEditor::mouse_move(const CL_InputEvent& event)
  72. {
  73. mouse_pos = event.mouse_pos;
  74. if (CL_Mouse::get_keycode(CL_MOUSE_LEFT))
  75. paint(event.mouse_pos, true);
  76. else if (CL_Mouse::get_keycode(CL_MOUSE_RIGHT))
  77. paint(event.mouse_pos, false);
  78. }
  79. void
  80. TileEditor::mouse_down(const CL_InputEvent& event)
  81. {
  82. if (tile)
  83. {
  84. switch (event.id)
  85. {
  86. case CL_MOUSE_LEFT:
  87. paint(event.mouse_pos, true);
  88. break;
  89. case CL_MOUSE_RIGHT:
  90. paint(event.mouse_pos, false);
  91. break;
  92. }
  93. }
  94. }
  95. void
  96. TileEditor::paint(CL_Point pos, bool val)
  97. {
  98. if (tile)
  99. {
  100. int x = int(pos.x) / 16;
  101. int y = int(pos.y) / 16;
  102. if (x >= 0 && y >= 0
  103. && x < 8 && y < 8)
  104. {
  105. tile->set_col(x, y, val);
  106. }
  107. }
  108. }
  109. void
  110. TileEditor::mouse_up(const CL_InputEvent& event)
  111. {
  112. }
  113. void
  114. TileEditor::set_tile(Tile* t)
  115. {
  116. tile = t;
  117. }
  118. /* EOF */