From 43ae62bf35bf2b3df39793c41c98e4370837e36f Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Thu, 18 Mar 2010 09:25:07 +0000
Subject: [PATCH] clickable hotkey zone

git-svn-id: http://svn.net-core.org/repos/t-engine4@437 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/HotkeysDisplay.lua         | 23 ++++++++++++++++++++++-
 game/engine/dialogs/ShowEquipInven.lua |  4 ++--
 game/modules/tome/class/Game.lua       |  4 ++++
 game/modules/tome/class/Player.lua     | 10 +++++++++-
 4 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/game/engine/HotkeysDisplay.lua b/game/engine/HotkeysDisplay.lua
index c5841d9c03..a7c2162cd6 100644
--- a/game/engine/HotkeysDisplay.lua
+++ b/game/engine/HotkeysDisplay.lua
@@ -7,6 +7,7 @@ function _M:init(actor, x, y, w, h, bgcolor)
 	self.bgcolor = bgcolor
 	self.font = core.display.newFont("/data/font/VeraMono.ttf", 10)
 	self.font_h = self.font:lineSkip()
+	self.clics = {}
 	self:resize(x, y, w, h)
 end
 
@@ -45,6 +46,7 @@ function _M:display()
 
 	local x = 0
 	local y = 0
+	self.clics = {}
 
 	for ii, ts in ipairs(hks) do
 		local s
@@ -76,8 +78,9 @@ function _M:display()
 		txt = ("%2d) %-"..(self.max_char_w-4-24).."s Key: %s"):format(i, txt, ts[4])
 		local w, h = self.font:size(txt)
 		s = core.display.newSurface(w + 4, h + 4)
-		s:alpha(255)
+		if self.cur_sel and self.cur_sel == i then s:erase(0, 50, 120) end
 		s:drawString(self.font, txt, 2, 2, color[1], color[2], color[3])
+		self.clics[i] = {x,y,w+4,h+4}
 
 		self.surface:merge(s, x, y)
 		if y + self.font_h * 2 > self.h then
@@ -90,3 +93,21 @@ function _M:display()
 
 	return self.surface
 end
+
+--- Call when a mouse event arrives in this zone
+-- This is optional, only if you need mouse support
+function _M:onMouse(button, mx, my)
+	mx, my = mx - self.display_x, my - self.display_y
+	for i, zone in pairs(self.clics) do
+		if mx >= zone[1] and mx < zone[1] + zone[3] and my >= zone[2] and my < zone[2] + zone[4] then
+			if button == "left" then
+				self.actor:activateHotkey(i)
+			else
+				self.actor.changed = true
+				self.cur_sel = i
+			end
+			return
+		end
+	end
+	self.cur_sel = nil
+end
diff --git a/game/engine/dialogs/ShowEquipInven.lua b/game/engine/dialogs/ShowEquipInven.lua
index 24f9427740..88fd6c8829 100644
--- a/game/engine/dialogs/ShowEquipInven.lua
+++ b/game/engine/dialogs/ShowEquipInven.lua
@@ -93,9 +93,9 @@ function _M:generateList()
 end
 
 function _M:drawDialog(s)
-	if not self.list[self.sel].item then
+	if self.list[self.sel] and not self.list[self.sel].item then
 		lines = self.actor.inven_def[self.list[self.sel].inven].description:splitLines(self.iw / 2 - 10, self.font)
-	elseif self.list[self.sel] and self.list[self.sel].object then
+	elseif self.list[self.sel] and self.list[self.sel] and self.list[self.sel].object then
 		lines = self.list[self.sel].object:getDesc():splitLines(self.iw - 10, self.font)
 	else
 		lines = {}
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index d485faa814..9a12c8ae9e 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -613,6 +613,10 @@ function _M:setupMouse()
 		if button == "wheelup" then self.logdisplay:scrollUp(1) end
 		if button == "wheeldown" then self.logdisplay:scrollUp(-1) end
 	end, {button=true})
+	-- Use hotkeys with mouse
+	self.mouse:registerZone(self.hotkeys_display.display_x, self.hotkeys_display.display_y, self.w, self.h, function(button, mx, my, xrel, yrel)
+		self.hotkeys_display:onMouse(button, mx, my)
+	end)
 	self.mouse:setCurrent()
 end
 
diff --git a/game/modules/tome/class/Player.lua b/game/modules/tome/class/Player.lua
index 63492ed0d8..cc91f2eb4e 100644
--- a/game/modules/tome/class/Player.lua
+++ b/game/modules/tome/class/Player.lua
@@ -216,10 +216,11 @@ function _M:runCheck()
 end
 
 function _M:doDrop(inven, item)
-	if self.zone.short_name ~= "wilderness" then game.logPlayer(self, "You can not drop on the world map.") return end
+	if game.zone.short_name ~= "wilderness" then game.logPlayer(self, "You can not drop on the world map.") return end
 	self:dropFloor(inven, item, true, true)
 	self:sortInven()
 	self:useEnergy()
+	self.changed = true
 end
 
 function _M:doWear(inven, item, o)
@@ -232,6 +233,7 @@ function _M:doWear(inven, item, o)
 	end
 	self:sortInven()
 	self:useEnergy()
+	self.changed = true
 end
 
 function _M:doTakeoff(inven, item, o)
@@ -240,6 +242,7 @@ function _M:doTakeoff(inven, item, o)
 	end
 	self:sortInven()
 	self:useEnergy()
+	self.changed = true
 end
 
 function _M:playerPickup()
@@ -248,11 +251,13 @@ function _M:playerPickup()
 		self:showPickupFloor(nil, nil, function(o, item)
 			self:pickupFloor(item, true)
 			self:sortInven()
+			self.changed = true
 		end)
 	else
 		self:pickupFloor(1, true)
 		self:sortInven()
 		self:useEnergy()
+	self.changed = true
 	end
 end
 
@@ -279,6 +284,8 @@ function _M:playerTakeoff()
 end
 
 function _M:playerUseItem(object, item)
+	if game.zone.short_name ~= "wilderness" then game.logPlayer(self, "You can not use items on the world map.") return end
+
 	local use_fct = function(o, item)
 		self.changed = true
 		local ret, no_id = o:use(self)
@@ -295,6 +302,7 @@ function _M:playerUseItem(object, item)
 			end
 		end
 		self:breakStealth()
+		self.changed = true
 	end
 
 	if object and item then return use_fct(object, item) end
-- 
GitLab