mirror of https://github.com/OpenMW/openmw.git
Finished console GUI window
parent
c61212202b
commit
5ff9344a87
@ -0,0 +1,146 @@
|
||||
#ifndef MWGUI_CONSOLE_H
|
||||
#define MWGUI_CONSOLE_H
|
||||
|
||||
#include <openengine/gui/layout.hpp>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
namespace MWGui
|
||||
{
|
||||
class Console : private OEngine::GUI::Layout
|
||||
{
|
||||
public:
|
||||
MyGUI::EditPtr command;
|
||||
MyGUI::EditPtr history;
|
||||
|
||||
typedef std::list<std::string> StringList;
|
||||
|
||||
// History of previous entered commands
|
||||
StringList command_history;
|
||||
StringList::iterator current;
|
||||
std::string editString;
|
||||
|
||||
Console(int w, int h)
|
||||
: Layout("openmw_console_layout.xml")
|
||||
{
|
||||
setCoord(10,10, w-10, h/2);
|
||||
|
||||
getWidget(command, "edit_Command");
|
||||
getWidget(history, "list_History");
|
||||
|
||||
// Set up the command line box
|
||||
command->eventEditSelectAccept =
|
||||
newDelegate(this, &Console::acceptCommand);
|
||||
command->eventKeyButtonPressed =
|
||||
newDelegate(this, &Console::keyPress);
|
||||
|
||||
// Set up the log window
|
||||
history->setOverflowToTheLeft(true);
|
||||
history->setEditStatic(true);
|
||||
history->setVisibleVScroll(true);
|
||||
}
|
||||
|
||||
void enable()
|
||||
{
|
||||
setVisible(true);
|
||||
|
||||
// Give keyboard focus to the combo box whenever the console is
|
||||
// turned on
|
||||
MyGUI::InputManager::getInstance().setKeyFocusWidget(command);
|
||||
}
|
||||
|
||||
void disable()
|
||||
{
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
void setFont(const std::string &fntName)
|
||||
{
|
||||
history->setFontName(fntName);
|
||||
command->setFontName(fntName);
|
||||
}
|
||||
|
||||
void clearHistory()
|
||||
{
|
||||
history->setCaption("");
|
||||
}
|
||||
|
||||
// Print a message to the console. Messages may contain color
|
||||
// code, eg. "#FFFFFF this is white".
|
||||
void print(const std::string &msg)
|
||||
{ history->addText(msg); }
|
||||
|
||||
// These are pre-colored versions that you should use.
|
||||
|
||||
/// Output from successful console command
|
||||
void printOK(const std::string &msg)
|
||||
{ print("#FF00FF" + msg + "\n"); }
|
||||
|
||||
/// Error message
|
||||
void printError(const std::string &msg)
|
||||
{ print("#FF2222" + msg + "\n"); }
|
||||
|
||||
private:
|
||||
|
||||
void keyPress(MyGUI::WidgetPtr _sender,
|
||||
MyGUI::KeyCode key,
|
||||
MyGUI::Char _char)
|
||||
{
|
||||
if(command_history.empty()) return;
|
||||
|
||||
// Traverse history with up and down arrows
|
||||
if(key == MyGUI::KeyCode::ArrowUp)
|
||||
{
|
||||
// If the user was editing a string, store it for later
|
||||
if(current == command_history.end())
|
||||
editString = command->getCaption();
|
||||
|
||||
if(current != command_history.begin())
|
||||
{
|
||||
current--;
|
||||
command->setCaption(*current);
|
||||
}
|
||||
}
|
||||
else if(key == MyGUI::KeyCode::ArrowDown)
|
||||
{
|
||||
if(current != command_history.end())
|
||||
{
|
||||
current++;
|
||||
|
||||
if(current != command_history.end())
|
||||
command->setCaption(*current);
|
||||
else
|
||||
// Restore the edit string
|
||||
command->setCaption(editString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void acceptCommand(MyGUI::EditPtr _sender)
|
||||
{
|
||||
const std::string &cm = command->getCaption();
|
||||
if(cm.empty()) return;
|
||||
|
||||
// Add the command to the history, and set the current pointer to
|
||||
// the end of the list
|
||||
command_history.push_back(cm);
|
||||
current = command_history.end();
|
||||
editString.clear();
|
||||
|
||||
// Log the command
|
||||
print("#FFFFFF> " + cm + "\n");
|
||||
|
||||
/* NOTE: This is where the console command should be
|
||||
handled.
|
||||
|
||||
The console command is in the string 'cm'. Output from the
|
||||
command should be put back into the console with the
|
||||
printOK() or printError() functions.
|
||||
*/
|
||||
printOK("OK - echoing line " + cm);
|
||||
|
||||
command->setCaption("");
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif
|
@ -1,176 +0,0 @@
|
||||
/*
|
||||
OpenMW - The completely unofficial reimplementation of Morrowind
|
||||
Copyright (C) 2008 Nicolay Korslund
|
||||
Email: < korslund@gmail.com >
|
||||
WWW: http://openmw.snaptoad.com/
|
||||
|
||||
This file (cpp_console.cpp) is part of the OpenMW package.
|
||||
|
||||
OpenMW is distributed as free software: you can redistribute it
|
||||
and/or modify it under the terms of the GNU General Public License
|
||||
version 3, as published by the Free Software Foundation.
|
||||
|
||||
This program 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
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
version 3 along with this program. If not, see
|
||||
http://www.gnu.org/licenses/ .
|
||||
|
||||
*/
|
||||
|
||||
// These are defined in gui/gui.d. At some point later we will just
|
||||
// use the C++ bindings included with Monster, but these don't cover
|
||||
// the console stuff yet.
|
||||
enum
|
||||
{
|
||||
CR_OK = 1, // Command was executed
|
||||
CR_ERROR = 2, // An error occured
|
||||
CR_MORE = 3, // More input is needed
|
||||
CR_EMPTY = 4 // The line had no effect
|
||||
};
|
||||
|
||||
#include <list>
|
||||
|
||||
extern "C" int32_t console_input(const char* command);
|
||||
extern "C" char* console_output();
|
||||
|
||||
class Console : public Layout
|
||||
{
|
||||
public:
|
||||
MyGUI::EditPtr command;
|
||||
MyGUI::EditPtr history;
|
||||
|
||||
typedef std::list<Ogre::UTFString> StringList;
|
||||
|
||||
// History of previous entered commands
|
||||
StringList command_history;
|
||||
StringList::iterator current;
|
||||
Ogre::UTFString editString;
|
||||
|
||||
Console()
|
||||
: Layout("openmw_console_layout.xml")
|
||||
{
|
||||
setCoord(10,10,
|
||||
mWindow->getWidth()*2/3, mWindow->getHeight()/2);
|
||||
|
||||
getWidget(command, "edit_Command");
|
||||
getWidget(history, "list_History");
|
||||
|
||||
// Set up the command line box
|
||||
command->eventEditSelectAccept =
|
||||
newDelegate(this, &Console::acceptCommand);
|
||||
command->eventKeyButtonPressed =
|
||||
newDelegate(this, &Console::keyPress);
|
||||
|
||||
// Set up the log window
|
||||
history->setOverflowToTheLeft(true);
|
||||
history->setEditStatic(true);
|
||||
history->setVisibleVScroll(true);
|
||||
}
|
||||
|
||||
void takeFocus()
|
||||
{
|
||||
// Give keyboard focus to the combo box whenever the console is
|
||||
// turned on
|
||||
MyGUI::InputManager::getInstance().setKeyFocusWidget(command);
|
||||
}
|
||||
|
||||
void keyPress(MyGUI::WidgetPtr _sender,
|
||||
MyGUI::KeyCode key,
|
||||
MyGUI::Char _char)
|
||||
{
|
||||
if(command_history.empty()) return;
|
||||
|
||||
// Traverse history with up and down arrows
|
||||
if(key == MyGUI::KeyCode::ArrowUp)
|
||||
{
|
||||
// If the user was editing a string, store it for later
|
||||
if(current == command_history.end())
|
||||
editString = command->getCaption();
|
||||
|
||||
if(current != command_history.begin())
|
||||
{
|
||||
current--;
|
||||
command->setCaption(*current);
|
||||
}
|
||||
}
|
||||
else if(key == MyGUI::KeyCode::ArrowDown)
|
||||
{
|
||||
if(current != command_history.end())
|
||||
{
|
||||
current++;
|
||||
|
||||
if(current != command_history.end())
|
||||
command->setCaption(*current);
|
||||
else
|
||||
// Restore the edit string
|
||||
command->setCaption(editString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void acceptCommand(MyGUI::EditPtr _sender)
|
||||
{
|
||||
const Ogre::UTFString &cm = command->getCaption();
|
||||
if(cm.empty()) return;
|
||||
|
||||
// Add the command to the history, and set the current pointer to
|
||||
// the end of the list
|
||||
command_history.push_back(cm);
|
||||
current = command_history.end();
|
||||
editString.clear();
|
||||
|
||||
// Log the command and result
|
||||
history->addText("#FFFFFF> " + cm + "\n");
|
||||
|
||||
int res = console_input(cm.asUTF8_c_str());
|
||||
Ogre::UTFString out = console_output();
|
||||
|
||||
if(res == CR_OK)
|
||||
history->addText("#FF00FF" + out);
|
||||
else if(res == CR_ERROR)
|
||||
history->addText("#FF2222" + out);
|
||||
else if(res == CR_MORE)
|
||||
history->addText("#1111FF... more input needed\n");
|
||||
|
||||
exit:
|
||||
command->setCaption("");
|
||||
}
|
||||
};
|
||||
|
||||
Console *cons;
|
||||
|
||||
extern "C" void gui_setConsoleFont(const char* fntName)
|
||||
{
|
||||
cons->history->setFontName(fntName);
|
||||
}
|
||||
|
||||
extern "C" void gui_clearConsole()
|
||||
{
|
||||
cons->history->setCaption("");
|
||||
}
|
||||
|
||||
extern "C" void gui_toggleConsole()
|
||||
{
|
||||
if(consoleMode)
|
||||
{
|
||||
leaveGui();
|
||||
if(cons)
|
||||
cons->setVisible(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
enterGui();
|
||||
if(cons)
|
||||
{
|
||||
cons->setVisible(true);
|
||||
cons->takeFocus();
|
||||
}
|
||||
}
|
||||
|
||||
consoleMode = !consoleMode;
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
bool consoleMode = false;
|
||||
bool inventoryMode = false;
|
||||
|
||||
void enterGui()
|
||||
{
|
||||
guiMode++;
|
||||
|
||||
if(guiMode == 1)
|
||||
{
|
||||
// If we just entered GUI mode, enable the pointer
|
||||
mGUI->showPointer();
|
||||
|
||||
// Restore the GUI mouse position. This is a hack because silly
|
||||
// OIS doesn't allow us to set the mouse position ourselves.
|
||||
*((OIS::MouseState*)&(mMouse->getMouseState())) = state;
|
||||
mGUI->injectMouseMove(state.X.abs, state.Y.abs, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void leaveGui()
|
||||
{
|
||||
guiMode--;
|
||||
|
||||
if(guiMode < 0)
|
||||
{
|
||||
std::cout << "WARNING: guiMode is " << guiMode << "\n";
|
||||
guiMode = 0;
|
||||
}
|
||||
|
||||
// Are we done with all GUI windows?
|
||||
if(guiMode == 0)
|
||||
{
|
||||
// If so, hide the pointer and store the mouse state for later.
|
||||
mGUI->hidePointer();
|
||||
state = mMouse->getMouseState();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void gui_toggleGui()
|
||||
{
|
||||
if(inventoryMode)
|
||||
{
|
||||
leaveGui();
|
||||
if(stats)
|
||||
stats->setVisible(false);
|
||||
if(map)
|
||||
map->setVisible(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
enterGui();
|
||||
if(stats)
|
||||
stats->setVisible(true);
|
||||
if(map)
|
||||
map->setVisible(true);
|
||||
}
|
||||
|
||||
inventoryMode = !inventoryMode;
|
||||
}
|
||||
|
||||
extern "C" void gui_setupGUI(int32_t debugOut)
|
||||
{
|
||||
guiMode = 1;
|
||||
leaveGui();
|
||||
}
|
Loading…
Reference in New Issue