view engine/core/gui/guichan/guichanguimanager.cpp @ 697:ecaa4d98f05f tip

Abstracted the GUI code and refactored the GUIChan-specific code into its own module. * Most of the GUIChan code has been refactored into its own gui/guichan module. However, references to the GuiFont class still persist in the Engine and GuiManager code and these will need further refactoring. * GuiManager is now an abstract base class which specific implementations (e.g. GUIChan) should subclass. * The GUIChan GUI code is now a concrete implementation of GuiManager, most of which is in the new GuiChanGuiManager class. * The GUI code in the Console class has been refactored out of the Console and into the GUIChan module as its own GuiChanConsoleWidget class. The rest of the Console class related to executing commands was left largely unchanged. * Existing client code may need to downcast the GuiManager pointer received from FIFE::Engine::getGuiManager() to GuiChanGuiManager, since not all functionality is represented in the GuiManager abstract base class. Python client code can use the new GuiChanGuiManager.castTo static method for this purpose.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 18 Jun 2011 00:28:40 -1000
parents
children
line wrap: on
line source

/***************************************************************************
 *   Copyright (C) 2005-2008 by the FIFE team                              *
 *   http://www.fifengine.de                                               *
 *   This file is part of FIFE.                                            *
 *                                                                         *
 *   FIFE is free software; you can redistribute it and/or                 *
 *   modify it under the terms of the GNU Lesser General Public            *
 *   License as published by the Free Software Foundation; either          *
 *   version 2.1 of the License, or (at your option) any later version.    *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

// Standard C++ library includes
#include <iostream>
#include <cassert>

// 3rd party library includes
#include <boost/filesystem/convenience.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/tokenizer.hpp>
#include <guichan/sdl/sdlinput.hpp>
#include <guichan/key.hpp>
#include <guichan/focushandler.hpp>
#include <guichan.hpp>

// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "util/base/exception.h"
#include "util/log/logger.h"
#include "video/renderbackend.h"
#include "gui/console.h"
#include "video/fonts/fontbase.h"
#include "video/fonts/truetypefont.h"
#include "video/fonts/subimagefont.h"
#include "video/renderbackend.h"
#include "eventchannel/key/ec_key.h"
#include "eventchannel/key/ec_keyevent.h"
#include "eventchannel/mouse/ec_mouseevent.h"
#include "util/time/timemanager.h"
#include "util/log/logger.h"
#include "util/base/exception.h"

#include "commandline.h"
#include "gui_font.h"
#include "gui_imageloader.h"
#include "guichanguimanager.h"
#ifdef HAVE_OPENGL
#include "opengl/opengl_gui_graphics.h"
#endif
#include "sdl/sdl_gui_graphics.h"
#include "widgets/utf8textbox.h"

namespace FIFE {
	static Logger _log(LM_GUI);

	const unsigned GuiChanConsoleWidget::m_maxOutputRows = 50;

	GuiChanConsoleWidget::GuiChanConsoleWidget() :
		gcn::Container(),
		m_input(new CommandLine()),
		m_output(new gcn::UTF8TextBox()),
		m_outputscrollarea(new gcn::ScrollArea(m_output)),
		m_status(new gcn::Label()),
		m_toolsbutton(new gcn::Button("Tools")) {

		reLayout();
		add(m_outputscrollarea);
		add(m_input);
		add(m_status);
		add(m_toolsbutton);

		setOpaque(true);

		m_input->setCallback(std::bind1st(std::mem_fun(&Console::execute),
				m_console));
		m_prompt = "-- ";

		m_isAttached = false;

		m_fpsTimer.setInterval(500);
		m_fpsTimer.setCallback(
				boost::bind(&GuiChanConsoleWidget::updateCaption, this));

		m_hiding = true;

		m_animationTimer.setInterval(20);
		m_animationTimer.setCallback(
				boost::bind(&GuiChanConsoleWidget::updateAnimation, this));

		m_toolsbutton->addActionListener(this);
		m_toolsbutton->setFocusable(false);
		m_input->addFocusListener(this);

		GuiFont* font = GuiChanGuiManager::instance()->createFont();
		font->setColor(255, 255, 255);
		setIOFont(font);
	}

	void GuiChanConsoleWidget::reLayout() {
		Image* screen = RenderBackend::instance()->getScreenImage();
		assert(screen);

		int w, h, b, input_h, bbar_h, button_w;
		w = screen->getWidth() * 4 / 5;
		h = screen->getHeight() * 4 / 5;
		b = 0;
		input_h = getFont()->getHeight();
		bbar_h = input_h;
		button_w = 80;

		gcn::Color black(0x00, 0, 0, 0xff);
		gcn::Color white(0xff, 0xff, 0xff, 0xff);
		gcn::Color dark(50, 60, 50, 0xff);

		setSize(w, h);
		setPosition((screen->getWidth() - w) / 2, -h);
		setFrameSize(0);

		setForegroundColor(white);
		setBackgroundColor(black);
		setBaseColor(dark);

		setSize(w, h);

		m_outputscrollarea->setSize(w - 2 * b, h - input_h - 3 * b - bbar_h);
		m_outputscrollarea->setPosition(b, 0);

		m_input->setPosition(b, h - input_h - b - bbar_h);
		m_input->setSize(w - 2 * b, input_h);

		m_status->setPosition(b, h - b - bbar_h);
		m_status->setSize(w - 2 * b, bbar_h);

		m_toolsbutton->setPosition(w - button_w, h - b - bbar_h);
		m_toolsbutton->setSize(button_w, bbar_h);

		m_output->setBackgroundColor(black);
		m_output->setFocusable(false);

		m_outputscrollarea->setBackgroundColor(black);
		m_outputscrollarea->setBaseColor(dark);

		m_input->setForegroundColor(white);
		m_input->setBackgroundColor(black);

		m_status->setForegroundColor(white);
		m_status->setBackgroundColor(black);

		m_toolsbutton->setForegroundColor(white);
		m_toolsbutton->setBackgroundColor(black);
		m_toolsbutton->setBaseColor(dark);

		m_hiddenPos = -h;
		m_animationDelta = h / 6;
	}

	GuiChanConsoleWidget::~GuiChanConsoleWidget() {
		doHide();

		remove(m_input);
		remove(m_outputscrollarea);
		remove(m_status);

		delete m_output;
		delete m_input;
		delete m_outputscrollarea;
		delete m_status;
		delete m_toolsbutton;
	}

	void GuiChanConsoleWidget::updateCaption() {
		std::string caption = "FIFE Console - FPS: ";
		float fps = 1e3 / double(TimeManager::instance()->getAverageFrameTime());
		caption += boost::lexical_cast<std::string>(fps);
		m_status->setCaption(caption);
	}

	void GuiChanConsoleWidget::updateAnimation() {
		if (m_hiding) {
			setPosition(getX(), getY() - m_animationDelta);
			if (getY() <= m_hiddenPos) {
				doHide();
				m_animationTimer.stop();
			}
		} else {
			setPosition(getX(), getY() + m_animationDelta);
			if (getY() >= 0) {
				setPosition(getX(), 0);
				m_animationTimer.stop();
			}
		}
	}

	void GuiChanConsoleWidget::clear() {
		m_output->setText("");
	}

	void GuiChanConsoleWidget::doShow() {
		if (m_isAttached)
			return;
		m_isAttached = true;
		GuiChanGuiManager::instance()->add(this);
		GuiChanGuiManager::instance()->getTopContainer()->moveToTop(this);
		// Assure the input field is focused when shown.
		m_input->requestFocus();

		m_fpsTimer.start();
	}

	void GuiChanConsoleWidget::doHide() {
		if (!m_isAttached)
			return;
		m_isAttached = false;
		GuiChanGuiManager::instance()->remove(this);
		m_fpsTimer.stop();
	}

	void GuiChanConsoleWidget::show() {
		if (m_hiding) {
			m_hiding = false;
			doShow();
			m_animationTimer.start();
		}
	}

	void GuiChanConsoleWidget::hide() {
		if (!m_hiding) {
			m_hiding = true;
			m_animationTimer.start();
		}
	}

	void GuiChanConsoleWidget::toggleShowHide() {
		m_hiding = !m_hiding;
		if (!m_hiding)
			doShow();
		m_animationTimer.start();
	}

	void GuiChanConsoleWidget::println(const std::string& s) {
		assert(m_output);

		// Add the text in rows
		boost::char_separator<char> separator("\n");
		typedef boost::tokenizer< boost::char_separator<char> > tokenizer;
		tokenizer tokens(s, separator);
		for (tokenizer::iterator i = tokens.begin(); i != tokens.end(); ++i) {
			m_output->addRow(*i);
		}

		// Assure the maximum number of rows
		if (m_output->getNumberOfRows() > m_maxOutputRows) {
			unsigned rows = m_output->getNumberOfRows();
			int delta_rows = rows - m_maxOutputRows;
			std::vector<std::string> rows_text;
			for (size_t i = delta_rows; i != rows; ++i) {
				rows_text.push_back(m_output->getTextRow(i));
			}
			m_output->setText("");
			for (size_t i = 0; i != rows_text.size(); ++i) {
				m_output->addRow(rows_text[i]);
			}
		}

		// Assure the new text is visible
		gcn::Rectangle rect(0, m_output->getHeight(), 0, 0);
		m_outputscrollarea->showWidgetPart(m_output, rect);
	}

	void GuiChanConsoleWidget::action(const gcn::ActionEvent& event) {
		if (m_console) {
			m_console->getConsoleExecuter()->onToolsClick();
		} else {
			FL_WARN(_log, "Console not bound, but tools button clicked");
		}
	}

	void GuiChanConsoleWidget::setIOFont(GuiFont* font) {
		m_input->setFont(font);
		m_output->setFont(font);
	}

	void GuiChanConsoleWidget::focusLost(const gcn::Event&) {
		hide();
	}


	GuiChanGuiManager::GuiChanGuiManager(ImagePool& pool) :
		GuiManager(pool),
		m_gui_graphics(0),
		m_gcn_gui(new gcn::Gui()),
		m_focushandler(0),
		m_gcn_topcontainer(new gcn::Container()),
		m_image_loader(new GuiImageLoader(pool)),
		m_input(new gcn::SDLInput()),
		m_widgets(),
		m_console_widget(0),
		m_had_mouse(false),
		m_fontpath(),
		m_fontglyphs(),
		m_fontsize(),
		m_logic_executed(false) {

		m_gcn_gui->setInput(m_input);
		gcn::Image::setImageLoader(m_image_loader);

		m_gcn_gui->setTop(m_gcn_topcontainer);
		m_focushandler = m_gcn_topcontainer->_getFocusHandler();

		m_gcn_topcontainer->setOpaque(false);
		m_gcn_topcontainer->setFocusable(false);
	}

	GuiChanGuiManager::~GuiChanGuiManager() {
		delete m_console_widget;
		delete m_gcn_topcontainer;
		delete m_image_loader;
		delete m_input;
		delete m_gcn_gui;
		delete m_gui_graphics;
	}

	bool GuiChanGuiManager::onSdlEvent(SDL_Event& evt) {
		if (!m_input) {
			FL_WARN(_log, "GuiChanGuiManager, GuichanGUI->getInput == 0 ... discarding events!");
			return false;
		}

		switch(evt.type) {
			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				if( m_gcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) ) {
					m_input->pushInput(evt);
					return true;
				}
				m_focushandler->focusNone();
				return false;

			case SDL_MOUSEMOTION:
				if( m_gcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) ) {
					m_had_mouse = true;
					m_input->pushInput(evt);
					return true;
				}
				if( m_had_mouse ) {
					// We only keep the mouse if a widget/window has requested
					// dragging.
					m_had_mouse = bool(m_focushandler->getDraggedWidget());
					m_input->pushInput(evt);
					return true;
				}
				return false;

			case SDL_KEYDOWN:
			case SDL_KEYUP:
				if(m_focushandler->getFocused()) {
					m_input->pushInput(evt);
					return true;
				}
				return false;

			case SDL_ACTIVEEVENT:
				// Actually Guichan doesn't care (it should!)
				// so at least don't swallow mouse_focus events up.
				return false;

			default:
				return false;
		}
	}

	void GuiChanGuiManager::resize(unsigned int x, unsigned int y,
			unsigned int width, unsigned int height) {
		m_gcn_topcontainer->setDimension(gcn::Rectangle(x, y, width, height));
		if (m_console_widget) {
			m_console_widget->reLayout();
		}
	}

	gcn::Gui* GuiChanGuiManager::getGuiChanGui() const {
		return m_gcn_gui;
	}

	void GuiChanGuiManager::add(gcn::Widget* widget) {
		if( !m_widgets.count(widget) ) {
			m_gcn_topcontainer->add(widget);
			m_widgets.insert(widget);
		}
	}

	void GuiChanGuiManager::remove(gcn::Widget* widget) {
		if( m_widgets.count(widget) ) {
			m_widgets.erase(widget);
			m_gcn_topcontainer->remove(widget);
		}
	}

	void GuiChanGuiManager::init(RenderBackend* render_backend, int screenWidth,
			int screenHeight) {
		GuiManager::init(render_backend, screenWidth, screenHeight);

#ifdef HAVE_OPENGL
		if(render_backend->getName() != "SDL") {
			m_gui_graphics = new OpenGLGuiGraphics(getImagePool());
		}
#endif
		if(render_backend->getName() == "SDL") {
			m_gui_graphics = new SdlGuiGraphics(getImagePool());
		}

		m_gcn_gui->setGraphics(m_gui_graphics);

		m_console_widget = new GuiChanConsoleWidget();
		resize(0, 0, screenWidth, screenHeight);
	}

	void GuiChanGuiManager::turn() {
		if (!m_logic_executed)
			m_gcn_gui->logic();
		m_logic_executed = false;
		m_gcn_gui->draw();
	}

	void GuiChanGuiManager::showConsole() {
		if (!m_console_visible) {
			m_console_widget->show();
			m_console_visible = true;
		}
	}

	void GuiChanGuiManager::hideConsole() {
		if(m_console_visible) {
			m_console_widget->hide();
			m_console_visible = false;
		}
	}

	GuiFont* GuiChanGuiManager::setDefaultFont(const std::string& path,
			unsigned int size, const std::string& glyphs) {
		printf("made it!");
		GuiFont* default_font = GuiManager::setDefaultFont(path, size, glyphs);
		gcn::Widget::setGlobalFont(default_font);
		if (m_console_widget) {
			m_console_widget->reLayout();
		}
		return default_font;
	}

	KeyEvent GuiChanGuiManager::translateKeyEvent(const gcn::KeyEvent& gcnevt) {
		KeyEvent keyevt;
		if(gcnevt.getType() == gcn::KeyEvent::PRESSED)
			keyevt.setType(KeyEvent::PRESSED);
		else if(gcnevt.getType() == gcn::KeyEvent::RELEASED)
			keyevt.setType(KeyEvent::RELEASED);
		else
			throw EventException("Invalid event type in fillKeyEvent");
		keyevt.setShiftPressed(gcnevt.isShiftPressed());
		keyevt.setControlPressed(gcnevt.isControlPressed());
		keyevt.setAltPressed(gcnevt.isAltPressed());
		keyevt.setMetaPressed(gcnevt.isMetaPressed());
		keyevt.setNumericPad(gcnevt.isNumericPad());

		// Convert from guichan keyval to FIFE keyval
		int keyval = gcnevt.getKey().getValue();
		keyval = convertGuichanKeyToFifeKey(keyval);

		keyevt.setKey(Key(static_cast<Key::KeyType>(keyval), keyval));

		return keyevt;
	}

	MouseEvent GuiChanGuiManager::translateMouseEvent(const gcn::MouseEvent& gcnevt) {
		MouseEvent mouseevt;
		mouseevt.setShiftPressed(gcnevt.isShiftPressed());
		mouseevt.setControlPressed(gcnevt.isControlPressed());
		mouseevt.setAltPressed(gcnevt.isAltPressed());
		mouseevt.setMetaPressed(gcnevt.isMetaPressed());
		mouseevt.setX(gcnevt.getX());
		mouseevt.setY(gcnevt.getY());

		switch(gcnevt.getType()) {
			case gcn::MouseEvent::PRESSED:
				mouseevt.setType(MouseEvent::PRESSED);
				break;
			case gcn::MouseEvent::RELEASED:
				mouseevt.setType(MouseEvent::RELEASED);
				break;
			case gcn::MouseEvent::MOVED:
				mouseevt.setType(MouseEvent::MOVED);
				break;
			case gcn::MouseEvent::CLICKED:
				mouseevt.setType(MouseEvent::CLICKED);
				break;
			case gcn::MouseEvent::ENTERED:
				mouseevt.setType(MouseEvent::ENTERED);
				break;
			case gcn::MouseEvent::EXITED:
				mouseevt.setType(MouseEvent::EXITED);
				break;
			case gcn::MouseEvent::DRAGGED:
				mouseevt.setType(MouseEvent::DRAGGED);
				break;
			case gcn::MouseEvent::WHEEL_MOVED_DOWN:
				mouseevt.setType(MouseEvent::WHEEL_MOVED_DOWN);
				break;
			case gcn::MouseEvent::WHEEL_MOVED_UP:
				mouseevt.setType(MouseEvent::WHEEL_MOVED_UP);
				break;
			default:
				mouseevt.setType(MouseEvent::UNKNOWN_EVENT);
		}

		switch(gcnevt.getButton()) {
			case gcn::MouseInput::LEFT:
				mouseevt.setButton(MouseEvent::LEFT);
				break;
			case gcn::MouseInput::RIGHT:
				mouseevt.setButton(MouseEvent::RIGHT);
				break;
			case gcn::MouseInput::MIDDLE:
				mouseevt.setButton(MouseEvent::MIDDLE);
				break;
			default:
				mouseevt.setButton(MouseEvent::UNKNOWN_BUTTON);
				break;
		}
		return mouseevt;
	}

	int GuiChanGuiManager::convertGuichanKeyToFifeKey(int value) {
		switch (value) {
			case gcn::Key::TAB:
				value = Key::TAB;
				break;
			case gcn::Key::LEFT_ALT:
				value = Key::LEFT_ALT;
				break;
			case gcn::Key::RIGHT_ALT:
				value = Key::RIGHT_ALT;
				break;
			case gcn::Key::LEFT_SHIFT:
				value = Key::LEFT_SHIFT;
				break;
			case gcn::Key::RIGHT_SHIFT:
				value = Key::RIGHT_SHIFT;
				break;
			case gcn::Key::LEFT_CONTROL:
				value = Key::LEFT_CONTROL;
				break;
			case gcn::Key::RIGHT_CONTROL:
				value = Key::RIGHT_CONTROL;
				break;
			case gcn::Key::BACKSPACE:
				value = Key::BACKSPACE;
				break;
			case gcn::Key::PAUSE:
				value = Key::PAUSE;
				break;
			case gcn::Key::SPACE:
				value = Key::SPACE;
				break;
			case gcn::Key::ESCAPE:
				value = Key::ESCAPE;
				break;
			case gcn::Key::DELETE:
				value = Key::DELETE;
				break;
			case gcn::Key::INSERT:
				value = Key::INSERT;
				break;
			case gcn::Key::HOME:
				value = Key::HOME;
				break;
			case gcn::Key::END:
				value = Key::END;
				break;
			case gcn::Key::PAGE_UP:
				value = Key::PAGE_UP;
				break;
			case gcn::Key::PRINT_SCREEN:
				value = Key::PRINT_SCREEN;
				break;
			case gcn::Key::PAGE_DOWN:
				value = Key::PAGE_DOWN;
				break;
			case gcn::Key::F1:
				value = Key::F1;
				break;
			case gcn::Key::F2:
				value = Key::F2;
				break;
			case gcn::Key::F3:
				value = Key::F3;
				break;
			case gcn::Key::F4:
				value = Key::F4;
				break;
			case gcn::Key::F5:
				value = Key::F5;
				break;
			case gcn::Key::F6:
				value = Key::F6;
				break;
			case gcn::Key::F7:
				value = Key::F7;
				break;
			case gcn::Key::F8:
				value = Key::F8;
				break;
			case gcn::Key::F9:
				value = Key::F9;
				break;
			case gcn::Key::F10:
				value = Key::F10;
				break;
			case gcn::Key::F11:
				value = Key::F11;
				break;
			case gcn::Key::F12:
				value = Key::F12;
				break;
			case gcn::Key::F13:
				value = Key::F13;
				break;
			case gcn::Key::F14:
				value = Key::F14;
				break;
			case gcn::Key::F15:
				value = Key::F15;
				break;
			case gcn::Key::NUM_LOCK:
				value = Key::NUM_LOCK;
				break;
			case gcn::Key::CAPS_LOCK:
				value = Key::CAPS_LOCK;
				break;
			case gcn::Key::SCROLL_LOCK:
				value = Key::SCROLL_LOCK;
				break;
			case gcn::Key::RIGHT_META:
				value = Key::RIGHT_META;
				break;
			case gcn::Key::LEFT_META:
				value = Key::LEFT_META;
				break;
			case gcn::Key::LEFT_SUPER:
				value = Key::LEFT_SUPER;
				break;
			case gcn::Key::RIGHT_SUPER:
				value = Key::RIGHT_SUPER;
				break;
			case gcn::Key::ALT_GR:
				value = Key::ALT_GR;
				break;
			case gcn::Key::UP:
				value = Key::UP;
				break;
			case gcn::Key::DOWN:
				value = Key::DOWN;
				break;
			case gcn::Key::LEFT:
				value = Key::LEFT;
				break;
			case gcn::Key::RIGHT:
				value = Key::RIGHT;
				break;
			case gcn::Key::ENTER:
				value = Key::ENTER;
				break;

			default:
				// Convert from unicode to lowercase letters
				if (value >= 1 && value <= 26) {
					// Control characters
					value = value - 1 + 'a';
				} else if (value >= 'A' && value <= 'Z') {
					value = value - 'A' + 'a';
				}

				// FIXME: Accented characters (รก) will not get converted properly.
				break;
		}

		return value;
	}
}