add a plugins context to controller base / game controller

This commit is contained in:
Chris Beck 2014-12-01 18:32:30 -05:00
parent 7904578960
commit 0133fd7a81
8 changed files with 65 additions and 16 deletions

View File

@ -22,8 +22,10 @@
#include "mouse_handler_base.hpp"
#include "resources.hpp"
#include "play_controller.hpp"
#include "scripting/plugins/context.hpp"
#include <boost/foreach.hpp>
#include <boost/scoped_ptr.hpp>
static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)
@ -35,7 +37,8 @@ controller_base::controller_base(
key_(),
browse_(false),
scrolling_(false),
joystick_manager_()
joystick_manager_(),
plugins_context_(new plugins_context("Game"))
{
}
@ -211,6 +214,11 @@ bool controller_base::handle_scroll(CKey& key, int mousex, int mousey, int mouse
void controller_base::play_slice(bool is_delay_enabled)
{
CKey key;
if (plugins_context_) {
plugins_context_->play_slice();
}
events::pump();
events::raise_process_event();
events::raise_draw_event();
@ -359,3 +367,7 @@ const config& controller_base::get_theme(const config& game_config, std::string
static config empty;
return empty;
}
plugins_context * controller_base::get_plugins_context() {
return plugins_context_.get();
}

View File

@ -25,7 +25,10 @@
#include "map.hpp"
#include <boost/scoped_ptr.hpp>
class CVideo;
class plugins_context;
namespace events {
class mouse_handler_base;
@ -41,6 +44,8 @@ public:
int get_ticks();
plugins_context * get_plugins_context();
protected:
/**
* Called by play_slice after events:: calls, but before processing scroll
@ -115,6 +120,8 @@ protected:
bool browse_;
bool scrolling_;
joystick_manager joystick_manager_;
boost::scoped_ptr<plugins_context> plugins_context_;
};

View File

@ -25,20 +25,22 @@
#include "lua_jailbreak_exception.hpp"
#include "config.hpp"
#include "make_enum.hpp"
#include <string>
#include <boost/optional.hpp>
#include <boost/variant/variant.hpp>
enum LEVEL_RESULT {
NONE,
VICTORY,
DEFEAT,
QUIT,
OBSERVER_END,
SKIP_TO_LINGER
};
MAKE_ENUM(LEVEL_RESULT,
(NONE, "none")
(VICTORY, "victory")
(DEFEAT, "defeat")
(QUIT, "quit")
(OBSERVER_END, "observer_end")
(SKIP_TO_LINGER,"skip_to_linger")
)
MAKE_ENUM_STREAM_OPS1(LEVEL_RESULT)
/**
* Struct used to transmit info caught from an end_turn_exception.

View File

@ -131,10 +131,10 @@ private:
};
// Add compiler directive suppressing unused variable warning
#if defined(__GNUCC__) || defined(__clang__) || defined(__MINGW32__)
#define ATTR_UNUSED( x ) __attribute__((unused)) x
#if defined(__GNUC__) || defined(__clang__) || defined(__MINGW32__)
#define ATTR_UNUSED __attribute__((unused))
#else
#define ATTR_UNUSED( x ) x
#define ATTR_UNUSED
#endif
@ -176,24 +176,24 @@ BOOST_PP_SEQ_FOR_EACH(EXPANDENUMTYPE, , MAKEPAIRS(CONTENT)) \
#define MAKEENUMCAST( NAME, PREFIX, CONTENT, COUNT_VAR ) \
PREFIX NAME CAT3(string_to_, NAME, _default) (const std::string& str, NAME def) \
PREFIX NAME ATTR_UNUSED CAT3(string_to_, NAME, _default) (const std::string& str, NAME def) \
{ \
BOOST_PP_SEQ_FOR_EACH(EXPANDENUMFUNCTION, , MAKEPAIRS(CONTENT)) \
return def; \
} \
PREFIX NAME CAT2(string_to_,NAME) (const std::string& str) \
PREFIX NAME ATTR_UNUSED CAT2(string_to_,NAME) (const std::string& str) \
{ \
BOOST_PP_SEQ_FOR_EACH(EXPANDENUMFUNCTION, , MAKEPAIRS(CONTENT)) \
throw bad_enum_cast( #NAME , str); \
} \
PREFIX std::string CAT2(NAME,_to_string) (NAME val) \
PREFIX std::string ATTR_UNUSED CAT2(NAME,_to_string) (NAME val) \
{ \
BOOST_PP_SEQ_FOR_EACH(EXPANDENUMFUNCTIONREV, , MAKEPAIRS(CONTENT)) \
assert(false && "Corrupted enum found with identifier NAME"); \
throw 42; \
} \
\
PREFIX const size_t ATTR_UNUSED( COUNT_VAR ) = \
PREFIX const size_t ATTR_UNUSED COUNT_VAR = \
BOOST_PP_SEQ_FOR_EACH(EXPANDENUMFUNCTIONCOUNT, , MAKEPAIRS(CONTENT)) \
0;

View File

@ -48,6 +48,7 @@
#include "saved_game.hpp"
#include "save_blocker.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "scripting/plugins/context.hpp"
#include "sound.hpp"
#include "soundsource.hpp"
#include "synced_context.hpp"
@ -265,6 +266,8 @@ void play_controller::init(CVideo& video){
init_managers();
loadscreen::global_loadscreen->start_stage("start game");
loadscreen_manager->reset();
plugins_context_->set_callback("quit", boost::bind(&play_controller::force_end_level, this, QUIT), false);
}
void play_controller::init_managers(){

View File

@ -44,6 +44,7 @@
#include "formula_string_utils.hpp"
#include "events.hpp"
#include "save_blocker.hpp"
#include "scripting/plugins/context.hpp"
#include "soundsource.hpp"
#include "storyscreen/interface.hpp"
#include "unit.hpp"
@ -92,6 +93,9 @@ playsingle_controller::playsingle_controller(const config& level,
ai::game_info ai_info;
ai::manager::set_ai_info(ai_info);
ai::manager::add_observer(this) ;
plugins_context_->set_accessor_string("level_result", boost::bind(&LEVEL_RESULT_to_string, level_result_));
plugins_context_->set_accessor_int("turn", boost::bind(&play_controller::turn, this));
}
playsingle_controller::~playsingle_controller()

View File

@ -16,6 +16,8 @@
#include "scripting/plugins/manager.hpp"
#include "config_assign.hpp"
#include <assert.h>
#include <utility>
#include <boost/bind.hpp>
@ -61,6 +63,23 @@ void plugins_context::set_accessor(const std::string & name, accessor_function f
accessors_[name] = func;
}
template<typename T>
static config make_config(const std::string & name, const T & val)
{
return config_of(name, val);
}
void plugins_context::set_accessor_string(const std::string & name, boost::function<std::string(config)> func)
{
set_accessor(name, boost::bind(&make_config<std::string>, name, boost::bind(func, _1)));
}
void plugins_context::set_accessor_int(const std::string & name, boost::function<int(config)> func)
{
set_accessor(name, boost::bind(&make_config<int>, name, boost::bind(func, _1)));
}
size_t plugins_context::erase_accessor(const std::string & name)
{
return accessors_.erase(name);

View File

@ -46,6 +46,8 @@ public:
size_t clear_callbacks();
void set_accessor(const std::string & name, accessor_function);
void set_accessor_string(const std::string & name, boost::function<std::string(config)>); //helpers which create a config from a simple type
void set_accessor_int(const std::string & name, boost::function<int(config)>);
size_t erase_accessor(const std::string & name);
size_t clear_accessors();