menu.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <ClanLib/Display/display.h>
  17. #include <ClanLib/Display/sprite.h>
  18. #include "fonts.hpp"
  19. #include "box.hpp"
  20. #include "menu.hpp"
  21. class MenuItem;
  22. class MenuImpl
  23. {
  24. public:
  25. Menu* parent;
  26. std::vector<CL_Slot> slots;
  27. typedef std::vector<MenuItem*> Items;
  28. Items items;
  29. int current_item;
  30. int width;
  31. int height;
  32. MenuImpl() {
  33. current_item = -1;
  34. }
  35. void draw();
  36. void recalc_size();
  37. int get_width();
  38. int get_height();
  39. void on_mouse_move(const CL_InputEvent& event);
  40. void on_mouse_down(const CL_InputEvent& event);
  41. };
  42. class MenuItem
  43. {
  44. protected:
  45. MenuImpl* parent;
  46. CL_Signal_v0 on_clicked;
  47. public:
  48. MenuItem(MenuImpl* parent_)
  49. : parent(parent_) {}
  50. virtual ~MenuItem() {}
  51. virtual void draw(int x, int y, bool active) =0;
  52. virtual int get_width() =0;
  53. virtual int get_height() =0;
  54. CL_Signal_v0& sig_clicked() { return on_clicked; }
  55. };
  56. class SeparatorMenuItem : public MenuItem
  57. {
  58. public:
  59. SeparatorMenuItem(MenuImpl* parent_)
  60. : MenuItem(parent_)
  61. {}
  62. virtual ~SeparatorMenuItem() {}
  63. void draw(int x, int y, bool active)
  64. {
  65. CL_Display::fill_rect(CL_Rect(CL_Point(x, y), CL_Size(parent->get_width()-7, 2)),
  66. CL_Color(150, 150, 150));
  67. CL_Display::fill_rect(CL_Rect(CL_Point(x, y+1), CL_Size(parent->get_width()-7, 1)),
  68. CL_Color(255, 255, 255));
  69. }
  70. int get_width() { return 10; }
  71. int get_height() { return 2; }
  72. };
  73. class TextMenuItem : public MenuItem
  74. {
  75. private:
  76. CL_Sprite sprite;
  77. std::string text;
  78. public:
  79. TextMenuItem(const CL_Sprite& sprite_, const std::string& text_, MenuImpl* parent_)
  80. : MenuItem(parent_),
  81. sprite(sprite_),
  82. text(text_)
  83. {
  84. if (sprite)
  85. sprite.set_alignment(origin_center);
  86. }
  87. virtual ~TextMenuItem() {}
  88. void draw(int x, int y, bool active) {
  89. if (active)
  90. CL_Display::fill_rect(CL_Rect(CL_Point(x, y-2), CL_Size(parent->get_width() - 7, 18)),
  91. CL_Color(255, 255, 255));
  92. if (sprite)
  93. {
  94. sprite.draw(x+10, y+7);
  95. }
  96. Fonts::verdana11.draw(x+24, y, text);
  97. }
  98. int get_width() { return Fonts::verdana11.bounding_rect(0, 0, text).get_width() + 16; }
  99. int get_height() { return Fonts::verdana11.get_height(); }
  100. };
  101. Menu::Menu(const CL_Point& pos, CL_Component* parent)
  102. : CL_Component(CL_Rect(pos, CL_Size(1,1)), parent),
  103. impl(new MenuImpl())
  104. {
  105. impl->parent = this;
  106. impl->width = 1;
  107. impl->height = 1;
  108. impl->slots.push_back(sig_paint().connect(impl.get(), &MenuImpl::draw));
  109. impl->slots.push_back(sig_mouse_move().connect(impl.get(), &MenuImpl::on_mouse_move));
  110. impl->slots.push_back(sig_mouse_down().connect(impl.get(), &MenuImpl::on_mouse_down));
  111. show(false);
  112. }
  113. Menu::~Menu()
  114. {
  115. clear();
  116. }
  117. void
  118. Menu::clear()
  119. {
  120. for(MenuImpl::Items::iterator i = impl->items.begin(); i != impl->items.end(); ++i)
  121. delete *i;
  122. impl->items.clear();
  123. }
  124. MenuItemHandle
  125. Menu::add_separator()
  126. {
  127. impl->items.push_back(new SeparatorMenuItem(impl.get()));
  128. impl->recalc_size();
  129. return impl->items.size()-1;
  130. }
  131. MenuItemHandle
  132. Menu::add_item(const std::string& name)
  133. {
  134. impl->items.push_back(new TextMenuItem(CL_Sprite(), name, impl.get()));
  135. impl->recalc_size();
  136. return impl->items.size()-1;
  137. }
  138. MenuItemHandle
  139. Menu::add_item(const CL_Sprite& sprite, const std::string& name)
  140. {
  141. impl->items.push_back(new TextMenuItem(sprite, name, impl.get()));
  142. impl->recalc_size();
  143. return impl->items.size()-1;
  144. }
  145. MenuItemHandle
  146. Menu::add_submenu(const std::string& name, const Menu& submenu)
  147. {
  148. impl->recalc_size();
  149. return -1;
  150. }
  151. void
  152. MenuImpl::recalc_size()
  153. {
  154. height = 0;
  155. width = 0;
  156. for(Items::iterator i = items.begin(); i != items.end(); ++i)
  157. width = std::max(width, (*i)->get_width());
  158. for(Items::iterator i = items.begin(); i != items.end(); ++i)
  159. height += (*i)->get_height() + 6;
  160. width += 12 + 24;
  161. height += 8;
  162. parent->set_size(width, height);
  163. }
  164. void
  165. MenuImpl::draw()
  166. {
  167. CL_Display::push_translate(parent->get_screen_x(), parent->get_screen_y());
  168. Box::draw_window(CL_Rect(CL_Point(0, 0),
  169. CL_Size(parent->get_width(),
  170. parent->get_height())));
  171. int x_pos = 3;
  172. int y_pos = 6;
  173. for(int i = 0; i < int(items.size()); ++i)
  174. {
  175. if (i == current_item)
  176. items[i]->draw(x_pos, y_pos, true);
  177. else
  178. items[i]->draw(x_pos, y_pos, false);
  179. y_pos += items[i]->get_height() + 6;
  180. }
  181. CL_Display::pop_modelview();
  182. }
  183. int
  184. MenuImpl::get_width()
  185. {
  186. return width;
  187. }
  188. int
  189. MenuImpl::get_height()
  190. {
  191. return height;
  192. }
  193. void
  194. MenuImpl::on_mouse_down(const CL_InputEvent& event)
  195. {
  196. if (current_item != -1)
  197. {
  198. items[current_item]->sig_clicked()();
  199. }
  200. parent->release_mouse();
  201. parent->show(false);
  202. }
  203. void
  204. MenuImpl::on_mouse_move(const CL_InputEvent& event)
  205. {
  206. if (parent->has_mouse_over())
  207. {
  208. int y_pos = 6;
  209. for(int i = 0; i < int(items.size()); ++i)
  210. {
  211. y_pos += items[i]->get_height() + 6;
  212. if (y_pos > event.mouse_pos.y)
  213. {
  214. current_item = i;
  215. return;
  216. }
  217. }
  218. current_item = -1;
  219. }
  220. else
  221. {
  222. current_item = -1;
  223. }
  224. }
  225. CL_Signal_v0&
  226. Menu::sig_clicked(MenuItemHandle item)
  227. {
  228. return impl->items[item]->sig_clicked();
  229. }
  230. void
  231. Menu::run()
  232. {
  233. // FIXME: Make menu act sane on == 0 items
  234. if (impl->items.size() > 0)
  235. {
  236. show(true);
  237. capture_mouse();
  238. raise();
  239. }
  240. }
  241. /* EOF */