2010-11-03 20:21:08 +00:00
# include "dialogue.hpp"
# include "dialogue_history.hpp"
# include "window_manager.hpp"
# include "widgets.hpp"
# include "components/esm_store/store.hpp"
2012-02-05 11:25:23 +00:00
# include "../mwworld/environment.hpp"
# include "../mwdialogue/dialoguemanager.hpp"
2010-11-03 20:21:08 +00:00
# include <assert.h>
# include <iostream>
# include <iterator>
# include <boost/algorithm/string.hpp>
# include <boost/lexical_cast.hpp>
using namespace MWGui ;
using namespace Widgets ;
2012-02-05 09:54:56 +00:00
/**
* Copied from the internet .
*/
std : : string lower_string ( const std : : string & str )
{
2012-02-10 15:21:04 +00:00
std : : string lowerCase ;
std : : transform ( str . begin ( ) , str . end ( ) , std : : back_inserter ( lowerCase ) ,
( int ( * ) ( int ) ) std : : tolower ) ;
return lowerCase ;
2012-02-05 09:54:56 +00:00
}
std : : string : : size_type find_str_ci ( const std : : string & str , const std : : string & substr , size_t pos )
{
return lower_string ( str ) . find ( lower_string ( substr ) , pos ) ;
}
2012-02-05 11:25:23 +00:00
DialogueWindow : : DialogueWindow ( WindowManager & parWindowManager , MWWorld : : Environment & environment )
: WindowBase ( " openmw_dialogue_window_layout.xml " , parWindowManager ) ,
mEnvironment ( environment )
2010-11-03 20:21:08 +00:00
{
// Centre dialog
2010-11-06 10:47:46 +00:00
center ( ) ;
2010-11-03 20:21:08 +00:00
//WindowManager *wm = environment.mWindowManager;
setText ( " NpcName " , " Name of character " ) ;
2012-01-27 13:50:13 +00:00
2010-11-03 20:21:08 +00:00
//History view
getWidget ( history , " History " ) ;
history - > setOverflowToTheLeft ( true ) ;
history - > getClient ( ) - > eventMouseButtonClick = MyGUI : : newDelegate ( this , & DialogueWindow : : onHistoryClicked ) ;
2012-02-11 11:18:47 +00:00
history - > setMaxTextLength ( 1000000 ) ;
2010-11-03 20:21:08 +00:00
//Topics list
getWidget ( topicsList , " TopicsList " ) ;
topicsList - > setScrollVisible ( true ) ;
2012-01-27 13:50:13 +00:00
//topicsList->eventListSelectAccept = MyGUI::newDelegate(this, &DialogueWindow::onSelectTopic);
2010-11-03 20:21:08 +00:00
topicsList - > eventListMouseItemActivate = MyGUI : : newDelegate ( this , & DialogueWindow : : onSelectTopic ) ;
2012-01-27 13:50:13 +00:00
//topicsList->eventListChangePosition = MyGUI::newDelegate(this, &DialogueWindow::onSelectTopic);
2010-11-03 20:21:08 +00:00
MyGUI : : ButtonPtr byeButton ;
getWidget ( byeButton , " ByeButton " ) ;
byeButton - > eventMouseButtonClick = MyGUI : : newDelegate ( this , & DialogueWindow : : onByeClicked ) ;
2012-02-05 09:54:56 +00:00
getWidget ( pDispositionBar , " Disposition " ) ;
getWidget ( pDispositionText , " DispositionText " ) ;
std : : cout < < " creation dialogue " ;
2010-11-03 20:21:08 +00:00
}
void DialogueWindow : : onHistoryClicked ( MyGUI : : Widget * _sender )
{
ISubWidgetText * t = history - > getSubWidgetText ( ) ;
if ( t = = nullptr )
return ;
const IntPoint & lastPressed = InputManager : : getInstance ( ) . getLastLeftPressed ( ) ;
size_t cursorPosition = t - > getCursorPosition ( lastPressed ) ;
2012-01-28 15:16:49 +00:00
MyGUI : : UString color = history - > getColorAtPos ( cursorPosition ) ;
if ( color ! = " #B29154 " )
2010-11-03 20:21:08 +00:00
{
UString key = history - > getColorTextAt ( cursorPosition ) ;
2012-01-28 15:16:49 +00:00
2012-01-27 13:50:13 +00:00
//std::cout << "Clicked on key: " << key << std::endl;
2012-02-05 11:25:23 +00:00
if ( color = = " #686EBA " )
{
2012-03-16 16:30:59 +00:00
//displayTopicText(lower_string(key));
2012-03-14 17:47:29 +00:00
mEnvironment . mDialogueManager - > keywordSelected ( lower_string ( key ) ) ;
2012-02-05 11:25:23 +00:00
}
2012-01-28 15:16:49 +00:00
if ( color = = " #572D21 " )
{
//TODO: send back the answere to the question!
2012-02-05 11:25:23 +00:00
mEnvironment . mDialogueManager - > questionAnswered ( key ) ;
//std::cout << "and the ansere is..."<< key;
2012-01-28 15:16:49 +00:00
}
2010-11-03 20:21:08 +00:00
}
}
void DialogueWindow : : open ( )
{
2012-01-27 13:50:13 +00:00
//updateOptions();
topicsList - > removeAllItems ( ) ;
pTopicsText . clear ( ) ;
history - > eraseText ( 0 , history - > getTextLength ( ) ) ;
2012-01-28 15:08:22 +00:00
updateOptions ( ) ;
2010-11-03 20:21:08 +00:00
setVisible ( true ) ;
}
void DialogueWindow : : onByeClicked ( MyGUI : : Widget * _sender )
{
2012-02-05 11:25:23 +00:00
//eventBye();
mEnvironment . mDialogueManager - > goodbyeSelected ( ) ;
2010-11-03 20:21:08 +00:00
}
void DialogueWindow : : onSelectTopic ( MyGUI : : List * _sender , size_t _index )
{
if ( _index = = MyGUI : : ITEM_NONE )
return ;
2012-01-27 13:50:13 +00:00
std : : string topic = _sender - > getItem ( _index ) ;
2012-03-16 16:30:59 +00:00
//displayTopicText(topic);
2012-03-14 17:47:29 +00:00
mEnvironment . mDialogueManager - > keywordSelected ( lower_string ( topic ) ) ;
2010-11-03 20:21:08 +00:00
//const std::string* theTopic = topicsList->getItemDataAt<std::string>(_index);
//std::cout << "Selected: "<< theTopic << std::endl;
//eventTopicSelected(key);
}
2012-01-27 13:50:13 +00:00
void DialogueWindow : : startDialogue ( std : : string npcName )
{
setText ( " NpcName " , npcName ) ;
}
void DialogueWindow : : addKeyword ( std : : string keyWord , std : : string topicText )
{
if ( topicsList - > findItemIndexWith ( keyWord ) = = MyGUI : : ITEM_NONE )
{
topicsList - > addItem ( keyWord ) ;
pTopicsText [ keyWord ] = topicText ;
}
}
void DialogueWindow : : removeKeyword ( std : : string keyWord )
{
if ( topicsList - > findItemIndexWith ( keyWord ) ! = MyGUI : : ITEM_NONE )
{
std : : cout < < topicsList - > findItem ( keyWord ) ;
topicsList - > removeItemAt ( topicsList - > findItem ( keyWord ) ) ;
pTopicsText . erase ( keyWord ) ;
}
}
2012-02-05 09:54:56 +00:00
void addColorInString ( std : : string & str , const std : : string & keyword , std : : string color1 , std : : string color2 )
2012-01-27 13:50:13 +00:00
{
size_t pos = 0 ;
2012-02-05 09:54:56 +00:00
while ( ( pos = find_str_ci ( str , keyword , pos ) ) ! = std : : string : : npos )
2012-01-27 13:50:13 +00:00
{
2012-02-15 12:23:59 +00:00
if ( pos = = 0 )
{
str . insert ( pos , color1 ) ;
pos + = color1 . length ( ) ;
pos + = keyword . length ( ) ;
str . insert ( pos , color2 ) ;
pos + = color2 . length ( ) ;
}
else if ( str . substr ( pos - 1 , 1 ) = = " " )
{
str . insert ( pos , color1 ) ;
pos + = color1 . length ( ) ;
pos + = keyword . length ( ) ;
str . insert ( pos , color2 ) ;
pos + = color2 . length ( ) ;
}
2012-02-05 09:54:56 +00:00
//str.replace(pos, oldStr.length(), "#686EBA"+str.get);
2012-01-27 13:50:13 +00:00
}
}
std : : string DialogueWindow : : parseText ( std : : string text )
{
//topicsList->geti
for ( int i = 0 ; i < topicsList - > getItemCount ( ) ; i + + )
{
std : : string keyWord = topicsList - > getItem ( i ) ;
2012-02-05 09:54:56 +00:00
//std::string newKeyWord = "#686EBA"+keyWord+"#B29154";
addColorInString ( text , keyWord , " #686EBA " , " #B29154 " ) ;
2012-01-27 13:50:13 +00:00
}
return text ;
}
void DialogueWindow : : displayTopicText ( std : : string topic )
{
if ( topicsList - > findItemIndexWith ( topic ) ! = MyGUI : : ITEM_NONE )
{
history - > addDialogHeading ( topic ) ;
history - > addDialogText ( parseText ( pTopicsText [ topic ] ) ) ;
}
else
{
std : : cout < < " topic not found! " ;
}
}
2010-11-03 20:21:08 +00:00
2012-02-05 09:54:56 +00:00
void DialogueWindow : : addText ( std : : string text )
{
2012-02-12 21:24:23 +00:00
history - > addDialogText ( " #B29154 " + parseText ( text ) + " #B29154 " ) ;
2012-02-05 09:54:56 +00:00
}
2012-03-16 16:30:59 +00:00
void DialogueWindow : : addTitle ( std : : string text )
{
history - > addDialogHeading ( text ) ;
}
void DialogueWindow : : askQuestion ( std : : string question )
2012-01-28 15:08:22 +00:00
{
2012-03-07 17:44:09 +00:00
history - > addDialogText ( " #572D21 " + question + " #B29154 " + " " ) ;
/*for(std::list<std::string>::iterator it = answers.begin();it!=answers.end();it++)
2012-01-28 15:08:22 +00:00
{
2012-01-28 15:16:49 +00:00
history - > addDialogText ( " #572D21 " + ( * it ) + " #B29154 " + " " ) ;
2012-03-07 17:44:09 +00:00
} */
2012-01-28 15:08:22 +00:00
}
2010-11-03 20:21:08 +00:00
void DialogueWindow : : updateOptions ( )
{
//FIXME Add this properly
2012-01-27 13:50:13 +00:00
/*history->addDialogText("Through the translucent surface of the orb, you see shifting images of distant locations...");
2010-11-03 20:21:08 +00:00
for ( int z = 0 ; z < 10 ; z + + )
{
2012-01-27 13:50:13 +00:00
history - > addDialogHeading ( " Fort Frostmoth " ) ;
history - > addDialogText ( " The image in the orb flickers, and you see.... The cold courtyard of #FF0000Fort Frostmoth#FFFFFF, battered bu werewolf attack, but still standing, still projecting Imperial might even to this distant and cold corner of the world. " ) ;
} */
2010-11-03 20:21:08 +00:00
//Clear the list of topics
topicsList - > removeAllItems ( ) ;
2012-01-27 13:50:13 +00:00
pTopicsText . clear ( ) ;
history - > eraseText ( 0 , history - > getTextLength ( ) ) ;
2012-02-05 09:54:56 +00:00
/*addKeyword("gus","gus is working on the dialogue system");
displayTopicText ( " gus " ) ; */
pDispositionBar - > setProgressRange ( 100 ) ;
pDispositionBar - > setProgressPosition ( 40 ) ;
pDispositionText - > eraseText ( 0 , pDispositionText - > getTextLength ( ) ) ;
pDispositionText - > addText ( " #B29154 " + std : : string ( " 40/100 " ) + " #B29154 " ) ;
2012-01-28 15:08:22 +00:00
2012-02-05 09:54:56 +00:00
/*std::list<std::string> test;
2012-01-28 15:08:22 +00:00
test . push_back ( " option 1 " ) ;
test . push_back ( " option 2 " ) ;
2012-02-05 09:54:56 +00:00
askQuestion ( " is gus cooking? " , test ) ; */
2012-01-27 13:50:13 +00:00
/*topicsList->addItem("Ald'ruhn", i++);
2010-11-03 20:21:08 +00:00
topicsList - > addItem ( " Balmora " , i + + ) ;
topicsList - > addItem ( " Sadrith Mora " , i + + ) ;
topicsList - > addItem ( " Vivec " , i + + ) ;
topicsList - > addItem ( " Ald Velothi " , i + + ) ;
topicsList - > addItem ( " Caldera " , i + + ) ;
topicsList - > addItem ( " Dagon Fel " , i + + ) ;
topicsList - > addItem ( " Gnaar Mok " , i + + ) ;
topicsList - > addItem ( " Gnisis " , i + + ) ;
topicsList - > addItem ( " Hla Oad " , i + + ) ;
topicsList - > addItem ( " Khuul " , i + + ) ;
topicsList - > addItem ( " Maar Gan " , i + + ) ;
topicsList - > addItem ( " Molag Mar " , i + + ) ;
topicsList - > addItem ( " Pelagiad " , i + + ) ;
topicsList - > addItem ( " Seyda Neen " , i + + ) ;
topicsList - > addItem ( " Suran " , i + + ) ;
topicsList - > addItem ( " Tel Aruhn " , i + + ) ;
topicsList - > addItem ( " Tel Branora " , i + + ) ;
topicsList - > addItem ( " Tel Fyr " , i + + ) ;
topicsList - > addItem ( " Tel Mora " , i + + ) ;
topicsList - > addItem ( " Tel Vos " , i + + ) ;
2012-01-27 13:50:13 +00:00
topicsList - > addItem ( " Vos " , i + + ) ; */
2010-11-03 20:21:08 +00:00
}