From abe38e5f879e22e9bd451d2da50a51fba6cdf8ae Mon Sep 17 00:00:00 2001
From: gugus <gus_512@hotmail.com>
Date: Sun, 5 Feb 2012 10:54:56 +0100
Subject: [PATCH] Started to weire the dialogue manager and the dialogue GUI

---
 apps/openmw/mwdialogue/dialoguemanager.cpp    | 31 +++++++++-
 apps/openmw/mwgui/dialogue.cpp                | 61 ++++++++++++++-----
 apps/openmw/mwgui/dialogue.hpp                |  2 +
 apps/openmw/mwgui/window_manager.cpp          |  4 +-
 apps/openmw/mwgui/window_manager.hpp          |  2 +
 extern/mygui_3.0.1/CMakeLists.txt             |  1 +
 extern/mygui_3.0.1/openmw_resources/core.xml  |  1 +
 .../openmw_dialogue_window_layout.xml         |  7 ++-
 .../openmw_dialogue_window_skin.xml           | 18 ++++++
 9 files changed, 106 insertions(+), 21 deletions(-)
 create mode 100644 extern/mygui_3.0.1/openmw_resources/openmw_dialogue_window_skin.xml

diff --git a/apps/openmw/mwdialogue/dialoguemanager.cpp b/apps/openmw/mwdialogue/dialoguemanager.cpp
index f78160cd7..6665c30aa 100644
--- a/apps/openmw/mwdialogue/dialoguemanager.cpp
+++ b/apps/openmw/mwdialogue/dialoguemanager.cpp
@@ -16,6 +16,8 @@
 #include "../mwworld/player.hpp"
 
 #include "../mwinput/inputmanager.hpp"
+#include "../mwgui/dialogue.hpp"
+#include "../mwgui/window_manager.hpp"
 
 #include <iostream>
 
@@ -236,13 +238,13 @@ namespace MWDialogue
             if (!isMatching (actor, *iter))
                 return false;
 
-        std::cout
+        /*std::cout
             << "unchecked entries:" << std::endl
             << "    player faction: " << info.pcFaction << std::endl
             << "    disposition: " << info.data.disposition << std::endl
             << "    NPC rank: " << static_cast<int> (info.data.rank) << std::endl
             << "    gender: " << static_cast<int> (info.data.gender) << std::endl
-            << "    PC rank: " << static_cast<int> (info.data.PCrank) << std::endl;
+            << "    PC rank: " << static_cast<int> (info.data.PCrank) << std::endl;*/
 
         return true;
     }
@@ -254,7 +256,7 @@ namespace MWDialogue
         std::cout << "talking with " << MWWorld::Class::get (actor).getName (actor) << std::endl;
 
         const ESM::Dialogue *dialogue = mEnvironment.mWorld->getStore().dialogs.find ("hello");
-
+        
         for (std::vector<ESM::DialInfo>::const_iterator iter (dialogue->mInfo.begin());
             iter!=dialogue->mInfo.end(); ++iter)
         {
@@ -277,9 +279,32 @@ namespace MWDialogue
                 }
 
                 mEnvironment.mInputManager->setGuiMode(MWGui::GM_Dialogue);
+                MWGui::DialogueWindow* win = mEnvironment.mWindowManager->getDialogueWindow();
+                win->startDialogue(MWWorld::Class::get (actor).getName (actor));
+                win->addText(iter->response);
                 break;
             }
         }
+
+        ESMS::RecListT<ESM::Dialogue>::MapType dialogueList = mEnvironment.mWorld->getStore().dialogs.list;
+        for(ESMS::RecListT<ESM::Dialogue>::MapType::iterator it = dialogueList.begin(); it!=dialogueList.end();it++)
+        {
+            ESM::Dialogue ndialogue = it->second;
+            if(ndialogue.type == ESM::Dialogue::Type::Topic)
+            {
+                for (std::vector<ESM::DialInfo>::const_iterator iter (it->second.mInfo.begin());
+                    iter!=it->second.mInfo.end(); ++iter)
+                {
+                    if (isMatching (actor, *iter))
+                    {
+                        MWGui::DialogueWindow* win = mEnvironment.mWindowManager->getDialogueWindow();
+                        win->addKeyword(it->first,iter->response);
+                        //std::cout << "match found!!";
+                        break;
+                    }
+                }
+            }
+        }
     }
 
 }
diff --git a/apps/openmw/mwgui/dialogue.cpp b/apps/openmw/mwgui/dialogue.cpp
index 87eb3e9cf..acb04e53c 100644
--- a/apps/openmw/mwgui/dialogue.cpp
+++ b/apps/openmw/mwgui/dialogue.cpp
@@ -14,6 +14,23 @@
 using namespace MWGui;
 using namespace Widgets;
 
+/**
+*Copied from the internet.
+*/
+
+std::string lower_string(const std::string& str)
+{
+    std::string lower;
+    std::transform(str.begin(), str.end(), std::back_inserter(lower), std::tolower);
+    return lower;
+}
+
+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);
+}
+
+
 DialogueWindow::DialogueWindow(WindowManager& parWindowManager)
     : WindowBase("openmw_dialogue_window_layout.xml", parWindowManager)
 {
@@ -39,7 +56,9 @@ DialogueWindow::DialogueWindow(WindowManager& parWindowManager)
     getWidget(byeButton, "ByeButton");
     byeButton->eventMouseButtonClick = MyGUI::newDelegate(this, &DialogueWindow::onByeClicked);
 
-    //updateOptions();
+    getWidget(pDispositionBar, "Disposition");
+    getWidget(pDispositionText,"DispositionText");
+    std::cout << "creation dialogue";
 }
 
 void DialogueWindow::onHistoryClicked(MyGUI::Widget* _sender)
@@ -57,7 +76,7 @@ void DialogueWindow::onHistoryClicked(MyGUI::Widget* _sender)
         UString key = history->getColorTextAt(cursorPosition);
 
         //std::cout << "Clicked on key: " << key << std::endl;
-        if(color == "#686EBA") displayTopicText(key);
+        if(color == "#686EBA") displayTopicText(lower_string(key));
         if(color == "#572D21") 
         {
             //TODO: send back the answere to the question!
@@ -117,17 +136,17 @@ void DialogueWindow::removeKeyword(std::string keyWord)
     }
 }
 
-
-/**
-*Copied from the internet.
-*/
-void replaceInString(std::string& str, const std::string& oldStr, const std::string& newStr)
+void addColorInString(std::string& str, const std::string& keyword,std::string color1, std::string color2)
 {
     size_t pos = 0;
-    while((pos = str.find(oldStr, pos)) != std::string::npos)
+    while((pos = find_str_ci(str,keyword, pos)) != std::string::npos)
     {
-        str.replace(pos, oldStr.length(), newStr);
-        pos += newStr.length();
+        //str.replace(pos, oldStr.length(), "#686EBA"+str.get);
+        str.insert(pos,color1);
+        pos += color1.length();
+        pos += keyword.length();
+        str.insert(pos,color2);
+        pos+= color2.length();
     }
 }
 
@@ -137,8 +156,8 @@ std::string DialogueWindow::parseText(std::string text)
     for(int i = 0;i<topicsList->getItemCount();i++)
     {
         std::string keyWord = topicsList->getItem(i);
-        std::string newKeyWord = "#686EBA"+keyWord+"#B29154";
-        replaceInString(text,keyWord,newKeyWord);
+        //std::string newKeyWord = "#686EBA"+keyWord+"#B29154";
+        addColorInString(text,keyWord,"#686EBA","#B29154");
     }
     return text;
 }
@@ -156,6 +175,11 @@ void DialogueWindow::displayTopicText(std::string topic)
     }
 }
 
+void DialogueWindow::addText(std::string text)
+{
+    history->addDialogText(parseText(text));
+}
+
 void DialogueWindow::askQuestion(std::string question,std::list<std::string> answers)
 {
     history->addDialogText(parseText(question));
@@ -180,13 +204,18 @@ void DialogueWindow::updateOptions()
     pTopicsText.clear();
     history->eraseText(0,history->getTextLength());
 
-    addKeyword("gus","gus is working on the dialogue system");
-    displayTopicText("gus");
+    /*addKeyword("gus","gus is working on the dialogue system");
+    displayTopicText("gus");*/
 
-    std::list<std::string> test;
+    pDispositionBar->setProgressRange(100);
+    pDispositionBar->setProgressPosition(40);
+    pDispositionText->eraseText(0,pDispositionText->getTextLength());
+    pDispositionText->addText("#B29154"+std::string("40/100")+"#B29154");
+
+    /*std::list<std::string> test;
     test.push_back("option 1");
     test.push_back("option 2");
-    askQuestion("is gus cooking?",test);
+    askQuestion("is gus cooking?",test);*/
     /*topicsList->addItem("Ald'ruhn", i++);
     topicsList->addItem("Balmora", i++);
     topicsList->addItem("Sadrith Mora", i++);
diff --git a/apps/openmw/mwgui/dialogue.hpp b/apps/openmw/mwgui/dialogue.hpp
index a83334e39..9518edcde 100644
--- a/apps/openmw/mwgui/dialogue.hpp
+++ b/apps/openmw/mwgui/dialogue.hpp
@@ -57,6 +57,8 @@ namespace MWGui
 
         DialogeHistory*     history;
         MyGUI::ListPtr      topicsList;
+        MyGUI::ProgressPtr pDispositionBar;
+        MyGUI::EditPtr pDispositionText;
         std::map<std::string,std::string> pTopicsText;// this map links keyword and "real" text.
     };
 }
diff --git a/apps/openmw/mwgui/window_manager.cpp b/apps/openmw/mwgui/window_manager.cpp
index d16611794..f1db1654e 100644
--- a/apps/openmw/mwgui/window_manager.cpp
+++ b/apps/openmw/mwgui/window_manager.cpp
@@ -472,6 +472,7 @@ void WindowManager::updateSkillArea()
 
 void WindowManager::removeDialog(OEngine::GUI::Layout*dialog)
 {
+    std::cout << "dialogue a la poubelle";
     assert(dialog);
     if (!dialog)
         return;
@@ -553,7 +554,8 @@ void WindowManager::onDialogueWindowBye()
     if (dialogueWindow)
     {
         //FIXME set some state and stuff?
-        removeDialog(dialogueWindow);
+        //removeDialog(dialogueWindow);
+        dialogueWindow->setVisible(false);
     }
     setGuiMode(GM_Game);
 }
diff --git a/apps/openmw/mwgui/window_manager.hpp b/apps/openmw/mwgui/window_manager.hpp
index a47e53fed..9dbe72eab 100644
--- a/apps/openmw/mwgui/window_manager.hpp
+++ b/apps/openmw/mwgui/window_manager.hpp
@@ -207,6 +207,8 @@ namespace MWGui
       updateVisible();
     }
 
+    MWGui::DialogueWindow* getDialogueWindow() {return dialogueWindow;}
+
     MyGUI::Gui* getGui() const { return gui; }
 
     void wmUpdateFps(float fps, size_t triangleCount, size_t batchCount)
diff --git a/extern/mygui_3.0.1/CMakeLists.txt b/extern/mygui_3.0.1/CMakeLists.txt
index 2cbe8aabe..6a96bd367 100644
--- a/extern/mygui_3.0.1/CMakeLists.txt
+++ b/extern/mygui_3.0.1/CMakeLists.txt
@@ -54,6 +54,7 @@ configure_file("${SDIR}/openmw_chargen_class_description_layout.xml" "${DDIR}/op
 configure_file("${SDIR}/openmw_chargen_birth_layout.xml" "${DDIR}/openmw_chargen_birth_layout.xml" COPYONLY)
 configure_file("${SDIR}/openmw_chargen_review_layout.xml" "${DDIR}/openmw_chargen_review_layout.xml" COPYONLY)
 configure_file("${SDIR}/openmw_dialogue_window_layout.xml" "${DDIR}/openmw_dialogue_window_layout.xml" COPYONLY)
+configure_file("${SDIR}/openmw_dialogue_window_skin.xml" "${DDIR}/openmw_dialogue_window_skin.xml" COPYONLY)
 configure_file("${SDIR}/openmw_inventory_window_layout.xml" "${DDIR}/openmw_inventory_window_layout.xml" COPYONLY)
 configure_file("${SDIR}/openmw_layers.xml" "${DDIR}/openmw_layers.xml" COPYONLY)
 configure_file("${SDIR}/openmw_mainmenu_layout.xml" "${DDIR}/openmw_mainmenu_layout.xml" COPYONLY)
diff --git a/extern/mygui_3.0.1/openmw_resources/core.xml b/extern/mygui_3.0.1/openmw_resources/core.xml
index 31d409a35..e98b20d3a 100644
--- a/extern/mygui_3.0.1/openmw_resources/core.xml
+++ b/extern/mygui_3.0.1/openmw_resources/core.xml
@@ -20,6 +20,7 @@
             <List file="openmw_mainmenu_skin.xml" group="General"/>
             <List file="openmw_console.skin.xml" group="General"/>
             <List file="openmw_journal_skin.xml" group="General"/>
+			<List file="openmw_dialogue_window_skin.xml" group="General"/>
     </MyGUI>
 
 </MyGUI>
diff --git a/extern/mygui_3.0.1/openmw_resources/openmw_dialogue_window_layout.xml b/extern/mygui_3.0.1/openmw_resources/openmw_dialogue_window_layout.xml
index 2987e82be..44642167b 100644
--- a/extern/mygui_3.0.1/openmw_resources/openmw_dialogue_window_layout.xml
+++ b/extern/mygui_3.0.1/openmw_resources/openmw_dialogue_window_layout.xml
@@ -17,8 +17,13 @@
             <Property key="Edit_VisibleVScroll" value="1" />
         </Widget>
 
+		<!-- The disposition bar-->
+        <Widget type="Progress" skin="MW_EnergyBar_Blue" position="432 39 132 18"
+            align="Right Top" name="Disposition">		
+			<Widget type="Edit" skin="MW_DispositionEdit" position_real = "0.25 0 0.5 1" name = "DispositionText"/>	
+		</Widget>
         <!-- The list of topics -->
-        <Widget type="List" skin="MW_List" position="432 39 132 341" name="TopicsList">
+        <Widget type="List" skin="MW_List" position="432 62 132 318" name="TopicsList">
         </Widget>
 
         <!-- The Goodbye button -->
diff --git a/extern/mygui_3.0.1/openmw_resources/openmw_dialogue_window_skin.xml b/extern/mygui_3.0.1/openmw_resources/openmw_dialogue_window_skin.xml
new file mode 100644
index 000000000..25e0b8659
--- /dev/null
+++ b/extern/mygui_3.0.1/openmw_resources/openmw_dialogue_window_skin.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<MyGUI type="Skin">
+
+    <Skin name = "MW_DispEdit" size = "10 10">
+        <Property key="FontName" value = "MonoFont" />
+        <Property key="AlignText" value = "Left Top" />
+        <Property key="Colour" value = "0000FF" />
+        <!--Property key="Pointer" value = "beam" /-->
+        <BasisSkin type="EditText" offset = "0 0 10 10" align = "Stretch"/>
+    </Skin>
+	
+    <Skin name="MW_DispositionEdit" size="0 0 50 50">
+        <Property key="WordWrap" value = "true" />
+        <Child type="Widget" skin="MW_DispEdit" offset="0 0 35 10" align = "ALIGN_STRETCH" name = "Client"/>
+        <!--Child type="VScroll" skin="VScroll" offset = "35 0 15 50" align = "Right VStretch" name = "VScroll"/-->
+    </Skin>
+</MyGUI>
\ No newline at end of file