1 /*
2  * client/Menu.cpp
3  *
4  * This file is part of Leges Motus, a networked, 2D shooter set in zero gravity.
5  *
6  * Copyright 2009-2010 Andrew Ayer, Nathan Partlan, Jeffrey Pfau
7  *
8  * Leges Motus is free and open source software.  You may redistribute it and/or
9  * modify it under the terms of version 2, or (at your option) version 3, of the
10  * GNU General Public License (GPL), as published by the Free Software Foundation.
11  *
12  * Leges Motus is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14  * PARTICULAR PURPOSE.  See the full text of the GNU General Public License for
15  * further detail.
16  *
17  * For a full copy of the GNU General Public License, please see the COPYING file
18  * in the root of the source code tree.  You may also retrieve a copy from
19  * <http://www.gnu.org/licenses/gpl-2.0.txt>, or request a copy by writing to the
20  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307  USA
22  *
23  */
24 
25 #include "Menu.hpp"
26 
27 using namespace LM;
28 using namespace std;
29 
Menu()30 Menu::Menu() {
31 	m_over = NULL;
32 	m_pressed = NULL;
33 }
34 
~Menu()35 Menu::~Menu() {
36 	for (vector<MenuItem*>::iterator iter = m_menu_items.begin(); iter != m_menu_items.end(); ++iter) {
37 		m_group.take_graphic((*iter)->get_graphic());
38 		delete *iter;
39 	}
40 }
41 
add_item_internal(MenuItem * item)42 void Menu::add_item_internal(MenuItem* item) {
43 	if (item == NULL) {
44 		return;
45 	}
46 	m_menu_items.push_back(item);
47 	m_group.give_graphic(item->get_graphic());
48 }
49 
remove_item_internal(MenuItem * item)50 void Menu::remove_item_internal(MenuItem* item) {
51 	if (item == NULL) {
52 		return;
53 	}
54 	// TODO delete menu item here?
55 	for (vector<MenuItem*>::iterator iter = m_menu_items.begin(); iter != m_menu_items.end();) {
56 		if (*iter == item) {
57 			iter = m_menu_items.erase(iter);
58 		} else {
59 			++iter;
60 		}
61 	}
62 	m_group.take_graphic(item->get_graphic());
63 }
64 
get_graphic_group()65 GraphicGroup* Menu::get_graphic_group() {
66 	return &m_group;
67 }
68 
mouse_motion_event(const SDL_MouseMotionEvent & event)69 MenuItem* Menu::mouse_motion_event(const SDL_MouseMotionEvent& event) {
70 	MenuItem* seen_item = item_at_position(event.x, event.y);
71 	if (seen_item != m_over) {
72 		if (m_over != NULL && m_over->get_state() == MenuItem::HOVER) {
73 			m_over->set_state(MenuItem::NORMAL);
74 			mouseout(m_over, event.x, event.y);
75 		}
76 		if (seen_item != NULL && seen_item->get_state() == MenuItem::NORMAL) {
77 			seen_item->set_state(MenuItem::HOVER);
78 			mouseover(seen_item, event.x, event.y);
79 		}
80 		m_over = seen_item;
81 	}
82 
83 	if (seen_item && seen_item->disabled()) {
84 		return NULL;
85 	}
86 
87 	return seen_item;
88 }
89 
mouse_button_event(const SDL_MouseButtonEvent & event)90 MenuItem* Menu::mouse_button_event(const SDL_MouseButtonEvent& event) {
91 	if (event.button != SDL_BUTTON_LEFT) {
92 		return NULL;
93 	}
94 	if (event.state == SDL_PRESSED) {
95 		m_pressed = item_at_position(event.x, event.y);
96 		if (m_pressed != NULL && m_pressed->get_state() == MenuItem::HOVER) {
97 			m_pressed->set_state(MenuItem::CLICKED);
98 		}
99 		mousedown(m_pressed, event.x, event.y);
100 	} else {
101 		if (m_pressed != NULL && m_pressed->get_state() == MenuItem::CLICKED) {
102 			if (item_at_position(event.x, event.y) == m_pressed) {
103 				m_pressed->set_state(MenuItem::HOVER);
104 			} else {
105 				m_pressed->set_state(MenuItem::NORMAL);
106 			}
107 		}
108 		mouseup(m_pressed, event.x, event.y);
109 	}
110 
111 	if (m_pressed && m_pressed->disabled()) {
112 		return NULL;
113 	}
114 
115 	return m_pressed;
116 }
117