From fa81cbde433511ef54e481d9970ebc1a7e33324e Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 28 Dec 2010 16:57:18 +0000
Subject: [PATCH] When targetting the map will scroll to follow the target
 cursor

git-svn-id: http://svn.net-core.org/repos/t-engine4@2214 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Map.lua                  |  2 ++
 game/engines/default/engine/Target.lua               |  8 ++++----
 .../default/engine/interface/GameTargeting.lua       | 12 +++++++++++-
 game/modules/tome/class/Game.lua                     |  3 ---
 src/core_lua.c                                       |  8 ++++++++
 5 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/game/engines/default/engine/Map.lua b/game/engines/default/engine/Map.lua
index 4202fb2b15..6620bc21e6 100644
--- a/game/engines/default/engine/Map.lua
+++ b/game/engines/default/engine/Map.lua
@@ -685,6 +685,7 @@ end
 
 --- Sets the current view area if x and y are out of bounds
 function _M:moveViewSurround(x, y, marginx, marginy)
+	local omx, omy = self.mx, self.my
 	if self.mx + marginx >= x or self.mx + self.viewport.mwidth - marginx <= x then
 		self.mx = x - math.floor(self.viewport.mwidth / 2)
 		self.changed = true
@@ -694,6 +695,7 @@ function _M:moveViewSurround(x, y, marginx, marginy)
 		self.changed = true
 	end
 	self:checkMapViewBounded()
+	return self.mx - omx, self.my - omy
 end
 
 --- Checks the map is bound to the screen (no "empty space" if the map is big enough)
diff --git a/game/engines/default/engine/Target.lua b/game/engines/default/engine/Target.lua
index 9401a94fa2..7bbf01d97d 100644
--- a/game/engines/default/engine/Target.lua
+++ b/game/engines/default/engine/Target.lua
@@ -189,14 +189,14 @@ function _M:freemove(dir)
 	self.target.x = self.target.x + d[1]
 	self.target.y = self.target.y + d[2]
 	self.target.entity = game.level.map(self.target.x, self.target.y, engine.Map.ACTOR)
-	if self.on_set_target then self:on_set_target() end
+	if self.on_set_target then self:on_set_target("freemove") end
 end
 
-function _M:setSpot(x, y)
+function _M:setSpot(x, y, how)
 	self.target.x = x
 	self.target.y = y
 	self.target.entity = game.level.map(self.target.x, self.target.y, engine.Map.ACTOR)
-	if self.on_set_target then self:on_set_target() end
+	if self.on_set_target then self:on_set_target(how) end
 end
 
 function _M:scan(dir, radius, sx, sy, filter)
@@ -233,7 +233,7 @@ function _M:scan(dir, radius, sx, sy, filter)
 		self.target.entity = actors[1].a
 		self.target.x = self.target.entity.x
 		self.target.y = self.target.entity.y
-		if self.on_set_target then self:on_set_target() end
+		if self.on_set_target then self:on_set_target("scan") end
 	end
 end
 
diff --git a/game/engines/default/engine/interface/GameTargeting.lua b/game/engines/default/engine/interface/GameTargeting.lua
index 0cbf440d06..c881c8e772 100644
--- a/game/engines/default/engine/interface/GameTargeting.lua
+++ b/game/engines/default/engine/interface/GameTargeting.lua
@@ -32,6 +32,16 @@ function _M:init()
 	self.target.target.entity = self.player
 	self.old_tmx, self.old_tmy = 0, 0
 	self.target_style = "lock"
+
+	-- Allow scrolling when targetting
+	self.target.on_set_target = function(self, how)
+		if self.key ~= self.targetmode_key then return end
+		local dx, dy = game.level.map:moveViewSurround(self.target.x, self.target.y, 1, 1)
+		if how == "mouse" then
+			local cx, cy = core.mouse.get()
+			core.mouse.set(cx - game.level.map.tile_w * dx, cy - game.level.map.tile_h * dy)
+		end
+	end
 end
 
 --- Maintain the current target each tick
@@ -184,7 +194,7 @@ function _M:targetMouse(button, mx, my, xrel, yrel, event)
 	if self.key == self.targetmode_key then
 		-- Target with mouse
 		if button == "none" and xrel and yrel and event == "motion" then
-			self.target:setSpot(tmx, tmy)
+			self.target:setSpot(tmx, tmy, "mouse")
 		-- Accept target
 		elseif button == "left" and not xrel and not yrel and event == "button" then
 			self:targetMode(false, false)
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index b073715703..9707e57abc 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -344,9 +344,6 @@ end
 
 function _M:initTargeting()
 	engine.interface.GameTargeting.init(self)
-	self.target.on_set_target = function(self)
---		if game.key == game.targetmode_key then game.level.map:moveViewSurround(self.target.x, self.target.y, 8, 8) end
-	end
 end
 
 
diff --git a/src/core_lua.c b/src/core_lua.c
index 1504c0cee8..cafc563d91 100644
--- a/src/core_lua.c
+++ b/src/core_lua.c
@@ -52,6 +52,13 @@ static int lua_get_mouse(lua_State *L)
 
 	return 2;
 }
+static int lua_set_mouse(lua_State *L)
+{
+	int x = luaL_checknumber(L, 1);
+	int y = luaL_checknumber(L, 2);
+	SDL_WarpMouse(x, y);
+	return 0;
+}
 extern int current_mousehandler;
 static int lua_set_current_mousehandler(lua_State *L)
 {
@@ -68,6 +75,7 @@ static int lua_set_current_mousehandler(lua_State *L)
 static const struct luaL_reg mouselib[] =
 {
 	{"get", lua_get_mouse},
+	{"set", lua_set_mouse},
 	{"set_current_handler", lua_set_current_mousehandler},
 	{NULL, NULL},
 };
-- 
GitLab