Add wesnoth.game_events.on_mouse_move/on_mouse_actions

This attempts to implement http://gna.org/bugs/?22635 and also allows
users to implement a 'queries a location from the user' (as suggested
from the easycosing page) in lua.
This commit is contained in:
gfgtdf 2016-03-24 00:41:07 +01:00
parent fa1c1f033c
commit b017a2b6b2
3 changed files with 40 additions and 1 deletions

View file

@ -48,6 +48,7 @@
#include "tstring.hpp" // for t_string
#include "units/unit.hpp" // for unit, intrusive_ptr_add_ref
#include "units/animation_component.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "units/ptr.hpp" // for unit_const_ptr
#include "whiteboard/manager.hpp" // for manager, etc
#include "whiteboard/typedefs.hpp" // for whiteboard_lock
@ -125,6 +126,9 @@ void mouse_handler::mouse_motion(int x, int y, const bool browse, bool update, m
new_hex = gui().hex_clicked_on(x,y);
if(new_hex != last_hex_) {
if(game_lua_kernel* lk = pc_.gamestate().lua_kernel_.get()) {
lk->mouse_over_hex_callback(new_hex);
}
update = true;
if ( pc_.get_map_const().on_board(last_hex_) ) {
// we store the previous hexes used to propose attack direction
@ -554,7 +558,9 @@ void mouse_handler::move_action(bool browse)
// deselect_hex();
// return false;
// }
if(game_lua_kernel* lk = pc_.gamestate().lua_kernel_.get()) {
lk->select_hex_callback(last_hex_);
}
unit_map::iterator u;
unit_map::iterator clicked_u;
map_location src;
@ -721,6 +727,9 @@ void mouse_handler::move_action(bool browse)
void mouse_handler::select_hex(const map_location& hex, const bool browse, const bool highlight, const bool fire_event) {
selected_hex_ = hex;
if(game_lua_kernel* lk = pc_.gamestate().lua_kernel_.get()) {
lk->select_hex_callback(last_hex_);
}
gui().select_hex(selected_hex_);
gui().clear_attack_indicator();

View file

@ -4984,3 +4984,29 @@ ai::lua_ai_action_handler* game_lua_kernel::create_lua_ai_action_handler(char co
{
return ai::lua_ai_action_handler::create(mState,code,context);
}
void game_lua_kernel::mouse_over_hex_callback(const map_location& loc)
{
lua_State *L = mState;
if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_move", NULL)) {
return;
}
lua_push(L, loc.x + 1);
lua_push(L, loc.y + 1);
luaW_pcall(L, 2, 0, false);
return;
}
void game_lua_kernel::select_hex_callback(const map_location& loc)
{
lua_State *L = mState;
if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_action", NULL)) {
return;
}
lua_push(L, loc.x + 1);
lua_push(L, loc.y + 1);
luaW_pcall(L, 2, 0, false);
return;
}

View file

@ -42,6 +42,7 @@ class tod_manager;
class play_controller;
class reports;
struct map_location;
typedef int (*lua_CFunction) (lua_State *L);
class game_lua_kernel : public lua_kernel_base
@ -202,6 +203,9 @@ public:
ai::lua_ai_context* create_lua_ai_context(char const *code, ai::engine_lua *engine);
ai::lua_ai_action_handler* create_lua_ai_action_handler(char const *code, ai::lua_ai_context &context);
int return_unit_method(lua_State *L, char const *m);
void mouse_over_hex_callback(const map_location& loc);
void select_hex_callback(const map_location& loc);
};
#endif