Permalink
Browse files

refactor Dialog

  • Loading branch information...
1 parent a6d67b7 commit f3711811a6b65bcba57f560c3f52dc67ad1ad7d4 @vilarion vilarion committed Feb 26, 2012
Showing with 81 additions and 27 deletions.
  1. +1 −1 Makefile.am
  2. +1 −1 Player.cpp
  3. +46 −0 dialog/Dialog.cpp
  4. +14 −0 dialog/Dialog.hpp
  5. +2 −20 dialog/InputDialog.cpp
  6. +0 −5 dialog/InputDialog.hpp
  7. +1 −0 script/LuaScript.cpp
  8. +16 −0 script/LuaScript.hpp
View
@@ -43,7 +43,7 @@ LongTimeEffect.cpp LongTimeAction.cpp LongTimeCharacterEffects.cpp Scheduler.cpp
Character.cpp \
Player.cpp PlayerWorkoutCommands.cpp Monster.cpp NPC.cpp PlayerManager.cpp playersave.cpp WaypointList.cpp Language.cpp \
\
-dialog/InputDialog.cpp \
+dialog/Dialog.cpp dialog/InputDialog.cpp \
\
db/ConnectionManager.cpp db/Connection.cpp db/Query.cpp \
db/SelectQuery.cpp db/InsertQuery.cpp db/UpdateQuery.cpp db/DeleteQuery.cpp \
View
@@ -3017,7 +3017,7 @@ void Player::executeInputDialog(unsigned int dialogId, bool success, std::string
if (success && (inputDialog != 0)) {
inputDialog->setInput(input);
- inputDialog->executeCallback();
+ LuaScript::executeDialogCallback(*inputDialog);
}
delete inputDialog;
View
@@ -0,0 +1,46 @@
+/*
+ * Illarionserver - server for the game Illarion
+ * Copyright 2011 Illarion e.V.
+ *
+ * This file is part of Illarionserver.
+ *
+ * Illarionserver is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option) any
+ * later version.
+ *
+ * Illarionserver 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 along with
+ * Illarionserver. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "dialog/Dialog.hpp"
+#include <luabind/luabind.hpp>
+#include "Logger.hpp"
+
+Dialog::Dialog(std::string title, std::string className, luabind::object callback)
+ : title(title), className(className), callback(callback) {
+}
+
+Dialog::Dialog(const Dialog &dialog) {
+ title = dialog.title;
+ className = dialog.className;
+ callback = dialog.callback;
+}
+
+std::string Dialog::getClassName() const {
+ return className;
+}
+
+std::string Dialog::getTitle() const {
+ return title;
+}
+
+luabind::object Dialog::getCallback() const {
+ return callback;
+}
+
View
@@ -21,7 +21,21 @@
#ifndef _DIALOG_HPP_
#define _DIALOG_HPP_
+#include <string>
+#include <luabind/object.hpp>
+
class Dialog {
+private:
+ std::string title;
+ std::string className;
+ luabind::object callback;
+
+public:
+ Dialog(std::string title, std::string className, luabind::object callback);
+ Dialog(const Dialog &dialog);
+ std::string getTitle() const;
+ std::string getClassName() const;
+ luabind::object getCallback() const;
};
#endif
View
@@ -24,20 +24,14 @@
InputDialog::InputDialog(std::string title, bool multiline,
unsigned short maxchars, luabind::object callback)
- :title(title), multiline(multiline), maxChars(maxChars), callback(callback) {
+ :Dialog(title, "InputDialog", callback), multiline(multiline), maxChars(maxChars) {
input = "";
}
-InputDialog::InputDialog(const InputDialog &inputDialog) {
- title = inputDialog.title;
+InputDialog::InputDialog(const InputDialog &inputDialog) : Dialog(inputDialog) {
multiline = inputDialog.multiline;
maxChars = inputDialog.maxChars;
input = inputDialog.input;
- callback = inputDialog.callback;
-}
-
-std::string InputDialog::getTitle() const {
- return title;
}
bool InputDialog::isMultiline() const {
@@ -64,15 +58,3 @@ void InputDialog::setInput(std::string input) {
this->input = input;
}
-void InputDialog::executeCallback() {
- try {
- callback(*this);
- } catch (luabind::error &e) {
- lua_State *L = e.state();
- std::string err = "Exception in InputDialog callback";
- err += ": " + std::string(lua_tostring(L, -1));
- lua_pop(L, 1);
- Logger::writeError("scripts", err);
- }
-}
-
View
@@ -23,25 +23,20 @@
#include "dialog/Dialog.hpp"
#include <string>
-#include <luabind/object.hpp>
class InputDialog: public Dialog {
private:
- std::string title;
bool multiline;
unsigned short maxChars;
std::string input;
- luabind::object callback;
public:
InputDialog(std::string title, bool multiline, unsigned short maxChars, luabind::object callback);
InputDialog(const InputDialog &inputDialog);
- std::string getTitle() const;
bool isMultiline() const;
unsigned short getMaxChars() const;
std::string getInput() const;
void setInput(std::string input);
- void executeCallback();
};
#endif
View
@@ -50,6 +50,7 @@ extern "C" {
#include "Logger.hpp"
#include "WaypointList.hpp"
#include "fuse_ptr.hpp"
+#include "dialog/Dialog.hpp"
#include "dialog/InputDialog.hpp"
#include <cxxabi.h>
View
@@ -28,12 +28,14 @@ extern "C" {
#include <stdexcept>
#include "globals.hpp"
#include "Item.hpp"
+#include "Logger.hpp"
#include <luabind/object.hpp>
#include "fuse_ptr.hpp"
#include <map>
class Character;
class World;
+class Dialog;
class ScriptException : public std::runtime_error {
public:
@@ -88,6 +90,20 @@ class LuaScript {
static void shutdownLua();
bool existsEntrypoint(const std::string &entrypoint);
void addQuestScript(const std::string &entrypoint, boost::shared_ptr<LuaScript> script);
+
+ template<typename T>
+ static void executeDialogCallback(T &dialog) {
+ try {
+ dialog.getCallback()(dialog);
+ } catch (luabind::error &e) {
+ lua_State *L = e.state();
+ std::string err = "Exception in " + dialog.getClassName() + " callback";
+ err += ": " + std::string(lua_tostring(L, -1));
+ lua_pop(L, 1);
+ Logger::writeError("scripts", err);
+ }
+ }
+
protected:
static lua_State *_luaState;

0 comments on commit f371181

Please sign in to comment.