From f45dace38829d911388e03bbfae72b266cd8576f Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Wed, 25 Nov 2009 22:54:32 +0000
Subject: [PATCH] targetting cursor window title better tooltips

git-svn-id: http://svn.net-core.org/repos/t-engine4@42 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/data/gfx/target_cursor.png               | Bin 0 -> 221 bytes
 game/engine/Game.lua                          |   4 +++
 game/engine/Target.lua                        |  11 ++++++-
 game/engine/Tooltip.lua                       |  30 ++++++++++++++++--
 game/engine/init.lua                          |   2 ++
 game/modules/tome/class/Game.lua              |  28 +++++++++++-----
 .../tome/data/zones/ancient_ruins/npcs.lua    |   2 +-
 game/modules/tome/dialogs/Quit.lua            |   1 +
 game/modules/tome/init.lua                    |   2 +-
 src/core_lua.c                                |   8 +++++
 src/main.c                                    |  20 ++++++++++++
 11 files changed, 94 insertions(+), 14 deletions(-)
 create mode 100644 game/data/gfx/target_cursor.png

diff --git a/game/data/gfx/target_cursor.png b/game/data/gfx/target_cursor.png
new file mode 100644
index 0000000000000000000000000000000000000000..847c590fb3bb26165d36ec2424e937f80e97a1c6
GIT binary patch
literal 221
zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|I14-?iy0WW
zg+Z8+Vb&Z8pdfpRr>`sfO>Rjsaf2CGm+l4%NtU=qlsM<-=BDPAFgO>bCYGe8D3oWG
zWGJ|M`UZqI@`(c#d3m}xhFF|VPC3B(@BWW|#bYZDb|@Zc;8&1tay2ewnC;Cbx!_3W
z|NKAcO1A}8$f<3LX<9O^b4`X*pm(B#K@W?=f%zioYHnVejTwZrHFn<#tYZb5%HZkh
K=d#Wzp$P!OzCt_z

literal 0
HcmV?d00001

diff --git a/game/engine/Game.lua b/game/engine/Game.lua
index 9c9b1316e9..dc207ab99e 100644
--- a/game/engine/Game.lua
+++ b/game/engine/Game.lua
@@ -49,6 +49,10 @@ end
 function _M:tick()
 end
 
+--- Called by the engine when the user tries to close the window
+function _M:onQuit()
+end
+
 --- Registers a dialog to display
 function _M:registerDialog(d)
 	table.insert(self.dialogs, d)
diff --git a/game/engine/Target.lua b/game/engine/Target.lua
index f5dae92ace..4d09264622 100644
--- a/game/engine/Target.lua
+++ b/game/engine/Target.lua
@@ -9,6 +9,8 @@ function _M:init(map, source_actor)
 	self.tile_w, self.tile_h = map.tile_w, map.tile_h
 	self.active = false
 
+	self.cursor = core.display.loadImage(engine.Tiles.prefix.."target_cursor.png")
+
 	self.sr = core.display.newSurface(map.tile_w, map.tile_h)
 	self.sr:alpha(125)
 	self.sr:erase(255, 0, 0)
@@ -17,7 +19,13 @@ function _M:init(map, source_actor)
 	self.sb:erase(0, 0, 255)
 
 	self.source_actor = source_actor
-	self.target = {x=self.source_actor.x, y=self.source_actor.y}
+
+	-- Setup the tracking target table
+	-- Notice its values are set to weak references, this has no effects on the number for x and y
+	-- but it means if the entity field is set to an entity, when it disappears this link wont prevent
+	-- the garbage collection
+	self.target = {x=self.source_actor.x, y=self.source_actor.y, entity=nil}
+	setmetatable(self.target, {__mode='v'})
 end
 
 function _M:display()
@@ -37,6 +45,7 @@ function _M:display()
 		s:toScreen(self.display_x + lx * self.tile_w, self.display_y + ly * self.tile_h)
 		lx, ly = l()
 	end
+	self.cursor:toScreen(self.display_x + self.target.x * self.tile_w, self.display_y + self.target.y * self.tile_h)
 end
 
 function _M:setActive(v)
diff --git a/game/engine/Tooltip.lua b/game/engine/Tooltip.lua
index 4cfe8248f8..59cfe76452 100644
--- a/game/engine/Tooltip.lua
+++ b/game/engine/Tooltip.lua
@@ -2,6 +2,8 @@ require "engine.class"
 
 module(..., package.seeall, class.make)
 
+tiles = engine.Tiles.new(16, 16)
+
 function _M:init(fontname, fontsize, color, bgcolor)
 	self.color = color or {255,255,255}
 	self.bgcolor = bgcolor or {0,0,0}
@@ -13,6 +15,14 @@ end
 
 function _M:set(str, ...)
 	self.text = str:format(...):splitLines(300, self.font)
+	self.w, self.h = 0, 0
+	for i, l in ipairs(self.text) do
+		local w, h = self.font:size(l)
+		if w > self.w then self.w = w end
+		self.h = self.h + self.font_h
+	end
+	self.w = self.w + 8
+	self.h = self.h + 8
 	self.changed = true
 end
 
@@ -21,13 +31,27 @@ function _M:display()
 	if not self.changed then return self.surface end
 	self.changed = false
 
-	self.surface = core.display.newSurface(300, self.font_h * #self.text)
+	self.surface = core.display.newSurface(self.w, self.h)
 	self.surface:alpha(200)
 
-	-- Erase and the display the map
+	-- Erase and the display the tooltip
 	self.surface:erase(self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
+
+	self.surface:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_7.png"), 0, 0)
+	self.surface:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_9.png"), self.w - 8, 0)
+	self.surface:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_1.png"), 0, self.h - 8)
+	self.surface:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_3.png"), self.w - 8, self.h - 8)
+	for i = 8, self.w - 9 do
+		self.surface:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_8.png"), i, 0)
+		self.surface:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_8.png"), i, self.h - 3)
+	end
+	for i = 8, self.h - 9 do
+		self.surface:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_4.png"), 0, i)
+		self.surface:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_4.png"), self.w - 3, i)
+	end
+
 	for i, l in ipairs(self.text) do
-		self.surface:drawColorString(self.font, self.text[i], 0, (i-1) * self.font_h, self.color[1], self.color[2], self.color[3])
+		self.surface:drawColorString(self.font, self.text[i], 4, 4 + (i-1) * self.font_h, self.color[1], self.color[2], self.color[3])
 	end
 	return self.surface
 end
diff --git a/game/engine/init.lua b/game/engine/init.lua
index cfa74e8254..faef9fa341 100644
--- a/game/engine/init.lua
+++ b/game/engine/init.lua
@@ -29,6 +29,8 @@ if mod_def then
 
 	if not mod.name or not mod.short_name or not mod.version or not mod.starter then os.exit() end
 
+	core.display.setWindowTitle(mod.name)
+
 	engine.Tiles.prefix = "/data/gfx/"
 	game = dofile("/"..mod.starter:gsub("%.", "/")..".lua")
 	game:run()
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index d8d8ccd3ab..25b58be804 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -72,29 +72,33 @@ function _M:display()
 	self.log:display():toScreen(self.log.display_x, self.log.display_y)
 
 	if self.level and self.level.map then
+		-- Display the map and compute FOV for the player if needed
 		if self.level.map.changed then
 			self.level.map:fov(self.player.x, self.player.y, 20)
 			self.level.map:fovLite(self.player.x, self.player.y, 4)
 		end
-		local s = self.level.map:display()
-		if s then
-			s:toScreen(self.level.map.display_x, self.level.map.display_y)
-		end
+		self.level.map:display():toScreen(self.level.map.display_x, self.level.map.display_y)
+
+		-- DIsplay the targetting system if active
+		self.target:display()
 
+		-- Display a tooltip if available
 		local mx, my = core.mouse.get()
 		local tmx, tmy = math.floor(mx / self.level.map.tile_w), math.floor(my / self.level.map.tile_h)
 		local tt = self.level.map:checkAllEntities(tmx, tmy, "tooltip")
-		if tt then
+		if tt and self.level.map.seens(tmx, tmy) then
 			self.tooltip:set(tt)
 			local t = self.tooltip:display()
+			mx = mx - self.tooltip.w
+			my = my - self.tooltip.h
+			if mx < 0 then mx = 0 end
+			if my < 0 then my = 0 end
 			if t then t:toScreen(mx, my) end
 		end
 		if self.old_tmx ~= tmx or self.old_tmy ~= tmy then
 			self.target.target.x, self.target.target.y = tmx, tmy
 		end
 		self.old_tmx, self.old_tmy = tmx, tmy
-
-		self.target:display()
 	end
 
 	engine.GameTurnBased.display(self)
@@ -201,7 +205,7 @@ function _M:setupCommands()
 		end,
 		-- Exit the game
 		[{"_x","ctrl"}] = function()
-			self:registerDialog(QuitDialog.new())
+			self:onQuit()
 		end,
 
 		-- Targeting movement
@@ -243,3 +247,11 @@ function _M:setupMouse()
 		if button == "wheeldown" then self.log:scrollUp(-1) end
 	end)
 end
+
+--- Ask if we realy want to close, if so, save the game first
+function _M:onQuit()
+	if not self.quit_dialog then
+		self.quit_dialog = QuitDialog.new()
+		self:registerDialog(self.quit_dialog)
+	end
+end
diff --git a/game/modules/tome/data/zones/ancient_ruins/npcs.lua b/game/modules/tome/data/zones/ancient_ruins/npcs.lua
index 3044269494..b76889fc03 100644
--- a/game/modules/tome/data/zones/ancient_ruins/npcs.lua
+++ b/game/modules/tome/data/zones/ancient_ruins/npcs.lua
@@ -6,7 +6,7 @@ return {
 	level = 1, exp_worth = 1,
 	life = 20,
 	mana = 1000,
-	energy = { mod=0.8 },
+	energy = { mod=0.5 },
 	has_blood = true,
 	stats = { str=15, dex=8, mag=12, },
 	combat = { dam=8, atk=10, apr=2, def=4, armor=6},
diff --git a/game/modules/tome/dialogs/Quit.lua b/game/modules/tome/dialogs/Quit.lua
index a45540fa80..b84d8bbd2d 100644
--- a/game/modules/tome/dialogs/Quit.lua
+++ b/game/modules/tome/dialogs/Quit.lua
@@ -11,6 +11,7 @@ function _M:init()
 		end,
 		__DEFAULT = function()
 			game:unregisterDialog(self)
+			game.quit_dialog = false
 		end,
 	}
 end
diff --git a/game/modules/tome/init.lua b/game/modules/tome/init.lua
index a5045a364d..332112ac6d 100644
--- a/game/modules/tome/init.lua
+++ b/game/modules/tome/init.lua
@@ -1,4 +1,4 @@
-name = "Tales of Middle Earth: 4th Age"
+name = "Tales of Middle Earth: The Fourth Age"
 short_name = "tome"
 author = { "DarkGod", "darkgod@t-o-m-e.net" }
 description = [[
diff --git a/src/core_lua.c b/src/core_lua.c
index f50aebfddd..6779b77ffe 100644
--- a/src/core_lua.c
+++ b/src/core_lua.c
@@ -445,6 +445,13 @@ static int sdl_surface_alpha(lua_State *L)
 	return 0;
 }
 
+static int sdl_set_window_title(lua_State *L)
+{
+	const char *title = luaL_checkstring(L, 1);
+	SDL_WM_SetCaption(title, NULL);
+	return 0;
+}
+
 static const struct luaL_reg displaylib[] =
 {
 	{"fullscreen", sdl_fullscreen},
@@ -452,6 +459,7 @@ static const struct luaL_reg displaylib[] =
 	{"newFont", sdl_new_font},
 	{"newSurface", sdl_new_surface},
 	{"loadImage", sdl_load_image},
+	{"setWindowTitle", sdl_set_window_title},
 	{NULL, NULL},
 };
 
diff --git a/src/main.c b/src/main.c
index ad47563c3b..8b68823a3c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -59,6 +59,23 @@ typedef struct {
 	Uint32 color;
 } MainStateData;
 
+int event_filter(const SDL_Event *event)
+{
+	// Do not allow the user to close without asking the game to know about it
+	if (event->type == SDL_QUIT && (current_game != LUA_NOREF))
+	{
+		lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
+		lua_pushstring(L, "onQuit");
+		lua_gettable(L, -2);
+		lua_remove(L, -2);
+		lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
+		docall(L, 1, 0);
+
+		return 0;
+	}
+	return 1;
+}
+
 void on_event(SDL_Event *event)
 {
 	switch (event->type) {
@@ -232,6 +249,9 @@ int main(int argc, char *argv[])
 	luaL_loadfile(L, "/engine/init.lua");
 	docall(L, 0, 0);
 
+	// Filter events, to catch the quit event
+	SDL_SetEventFilter(event_filter);
+
 	bool done = FALSE;
 	SDL_Event event;
 	while ( !done )
-- 
GitLab