1 /*
2  * client/ChatLog.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 "ChatLog.hpp"
26 #include "ServerBrowser.hpp"
27 #include "GameController.hpp"
28 #include "GameWindow.hpp"
29 #include "TextManager.hpp"
30 #include "common/network.hpp"
31 #include "common/misc.hpp"
32 
33 using namespace LM;
34 using namespace std;
35 
36 const int ChatLog::TEXT_LAYER = -7;
37 const int ChatLog::LINE_SPACING = 15;
38 const int ChatLog::RIGHT_PADDING = 25;
39 
ChatLog(GameController & parent,GameWindow * window,TextManager * textmanager,int screenwidth,int screenheight,Font * standardfont,Font * mediumfont,Font * menufont)40 ChatLog::ChatLog(GameController& parent, GameWindow* window, TextManager* textmanager, int screenwidth,
41 				int screenheight, Font* standardfont, Font* mediumfont, Font* menufont) : m_parent(parent) {
42 	m_window = window;
43 	m_text_manager = textmanager;
44 	m_font = standardfont;
45 	m_medium_font = mediumfont;
46 	m_menu_font = menufont;
47 	m_screen_width = screenwidth;
48 	m_screen_height = screenheight;
49 	m_is_invisible = false;
50 
51 	m_background = new TableBackground(2, m_screen_width * .70);
52 	m_background->set_row_height(0, 43);
53 	m_background->set_row_height(1, m_screen_height * .70 - m_background->get_row_height(0));
54 	m_background->set_priority(-5);
55 	m_background->set_border_color(Color(1,1,1,0.8));
56 	m_background->set_border_width(2);
57 	m_background->set_cell_color(0, Color(0.2,0.1,0.1,0.8));
58 	m_background->set_cell_color(1, Color(0.0,0.0,0.0,0.9));
59 	m_background->set_y(m_screen_height * .15);
60 	m_background->set_x(m_screen_width/2);
61 	m_background->set_border_collapse(true);
62 	m_background->set_corner_radius(20);
63 	m_window->register_graphic(m_background, GameWindow::LAYER_HUD);
64 
65 	m_scrollbar = new ScrollBar();
66 	m_scrollbar->set_priority(-6);
67 	m_scrollbar->set_height(m_background->get_image_height() - m_background->get_row_height(0) - 20);
68 	m_scrollbar->set_x(m_background->get_x() + m_background->get_image_width()/2 - 20);
69 	m_scrollbar->set_y(m_background->get_y() + m_background->get_row_height(0) + 5 + m_scrollbar->get_height()/2);
70 	m_scrollbar->set_section_color(ScrollBar::BUTTONS, Color(0.7,0.2,0.1));
71 	m_scrollbar->set_section_color(ScrollBar::TRACK, Color(0.2,0.1,0.1));
72 	m_scrollbar->set_section_color(ScrollBar::TRACKER, Color(0.2,0.2,0.4));
73 	m_scrollbar->set_scroll_speed(3);
74 
75 	double width = m_background->get_x() - m_background->get_image_width()/2 - m_scrollbar->get_x() - m_scrollbar->get_image_width() + RIGHT_PADDING;
76 	m_scrollarea = new ScrollArea(width, m_background->get_image_height() - m_background->get_row_height(0) - 30, width, 10, NULL, m_scrollbar);
77 	m_scrollarea->set_priority(TEXT_LAYER);
78 	m_scrollarea->get_group()->set_priority(TEXT_LAYER);
79 	m_scrollarea->set_x(m_background->get_x() - RIGHT_PADDING);
80 	m_scrollarea->set_y(m_background->get_y() + m_background->get_row_height(0) + 15);
81 	m_scrollarea->set_center_x(m_scrollarea->get_width()/2);
82 	m_scrollarea->set_center_y(0);
83 
84 	m_window->register_graphic(m_scrollbar, GameWindow::LAYER_HUD);
85 	m_window->register_graphic(m_scrollarea, GameWindow::LAYER_HUD);
86 
87 	m_title = m_text_manager->place_string("Chat Log", m_background->get_x(), m_background->get_y() + 10, TextManager::LEFT, GameWindow::LAYER_HUD, TEXT_LAYER);
88 	m_title->set_x(m_background->get_x() - m_title->get_image_width()/2);
89 
90 	m_scrollbar->scroll(1);
91 }
92 
~ChatLog()93 ChatLog::~ChatLog() {
94 	delete m_background;
95 	delete m_scrollbar;
96 	delete m_scrollarea;
97 }
98 
set_visible(bool visible)99 void ChatLog::set_visible(bool visible) {
100 	m_is_invisible = !visible;
101 
102 	m_background->set_invisible(!visible);
103 	m_title->set_invisible(!visible);
104 	m_scrollbar->set_invisible(!visible);
105 	m_scrollarea->set_invisible(!visible);
106 }
107 
add_message(string message,Color color,Color shadow)108 void ChatLog::add_message(string message, Color color, Color shadow) {
109 	double old_progress = m_scrollbar->get_scroll_progress();
110 	m_text_manager->set_active_color(color);
111 	m_text_manager->set_shadow_color(shadow);
112 	Text* new_message = m_text_manager->place_string(message, m_background->get_x() - m_background->get_image_width()/2 -
113 		m_scrollarea->get_x() + m_scrollarea->get_width()/2 + 10, LINE_SPACING * m_items.size(), TextManager::LEFT, GameWindow::LAYER_HUD);
114 	new_message->set_priority(TEXT_LAYER);
115 	m_text_manager->set_shadow_color(GameController::TEXT_SHADOW);
116 	m_items.push_back(m_scrollarea->get_group()->add_graphic(new_message, message));
117 	m_text_manager->remove_string(new_message);
118 	m_scrollarea->set_content_height(LINE_SPACING * (m_items.size()));
119 	if (old_progress == 1) {
120 		m_scrollbar->scroll(1);
121 	}
122 }
123 
delete_message(int num)124 void ChatLog::delete_message(int num) {
125 	// TODO: stub
126 }
127 
scrollbar_button_event(const SDL_MouseButtonEvent & event)128 void ChatLog::scrollbar_button_event(const SDL_MouseButtonEvent& event) {
129 	m_scrollbar->mouse_button_event(event);
130 }
131 
scrollbar_motion_event(const SDL_MouseMotionEvent & event)132 void ChatLog::scrollbar_motion_event(const SDL_MouseMotionEvent& event) {
133 	m_scrollbar->mouse_motion_event(event);
134 }
135 
autoscroll(double scale)136 void ChatLog::autoscroll(double scale) {
137 	m_scrollbar->autoscroll(scale);
138 }
139 
is_invisible()140 bool ChatLog::is_invisible() {
141 	return m_is_invisible;
142 }
143