From d4dc51590d392fbca1325692e2fe02128e530bf2 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Mon, 7 Nov 2011 00:23:57 +0000
Subject: [PATCH] store converted

git-svn-id: http://svn.net-core.org/repos/t-engine4@4614 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Store.lua        |   2 +-
 game/engines/default/engine/ui/Inventory.lua |  17 ++-
 game/modules/tome/dialogs/ShowStore.lua      | 140 ++++++++++++++++++-
 3 files changed, 147 insertions(+), 12 deletions(-)

diff --git a/game/engines/default/engine/Store.lua b/game/engines/default/engine/Store.lua
index b3a426950b..041d39aa89 100644
--- a/game/engines/default/engine/Store.lua
+++ b/game/engines/default/engine/Store.lua
@@ -133,7 +133,7 @@ function _M:interact(who, name)
 		return self:descObject(who, what, o)
 	end, function(what, o)
 		return self:descObjectPrice(who, what, o)
-	end, self.allow_sell, self.allow_buy, function(item) end)
+	end, self.allow_sell, self.allow_buy, function(item) end, self, who)
 	game:registerDialog(d)
 end
 
diff --git a/game/engines/default/engine/ui/Inventory.lua b/game/engines/default/engine/ui/Inventory.lua
index 811cf6ffc8..241b0ddd81 100644
--- a/game/engines/default/engine/ui/Inventory.lua
+++ b/game/engines/default/engine/ui/Inventory.lua
@@ -33,6 +33,7 @@ function _M:init(t)
 	self.w = assert(t.width, "no inventory width")
 	self.h = assert(t.height, "no inventory height")
 	self.tabslist = t.tabslist
+	self.columns = t.columns
 	self.filter = t.filter
 	self.fct = t.fct
 	self.on_select = t.select
@@ -40,7 +41,7 @@ function _M:init(t)
 	self.on_drag = t.on_drag
 	self.on_drag_end = t.on_drag_end
 
-	if not self.tabslist and self.default_tabslist then
+	if self.tabslist == nil and self.default_tabslist then
 		if type(self.default_tabslist) == "function" then self.tabslist = self.default_tabslist(self)
 		else self.tabslist = self.default_tabslist
 		end
@@ -66,7 +67,7 @@ function _M:generate()
 		self.uis[#self.uis+1] = {x=0, y=0, ui=self.c_tabs}
 	end
 
-	self.c_inven = ListColumns.new{width=self.w, height=self.h - (self.c_tabs and self.c_tabs.h or 0), sortable=true, scrollbar=true, columns={
+	self.c_inven = ListColumns.new{width=self.w, height=self.h - (self.c_tabs and self.c_tabs.h or 0), sortable=true, scrollbar=true, columns=self.columns or {
 		{name="", width={20,"fixed"}, display_prop="char", sort="id"},
 		{name="", width={24,"fixed"}, display_prop="object", sort="sortname", direct_draw=function(item, x, y) if item.object then item.object:toScreen(nil, x+4, y, 16, 16) end end},
 		{name="Inventory", width=72, display_prop="name", sort="sortname"},
@@ -102,11 +103,13 @@ function _M:generate()
 		_TAB = function() self.c_tabs.sel_j = 1 self.c_tabs.sel_i = util.boundWrap(self.c_tabs.sel_i+1, 1, #self.tabslist) self.c_tabs:onUse("left") self.c_tabs:onSelect() end,
 		[{"_TAB","ctrl"}] = function() self.c_tabs.sel_j = 1 self.c_tabs.sel_i = util.boundWrap(self.c_tabs.sel_i-1, 1, self.tabslist) self.c_tabs:onUse("left", false) self.c_tabs:onSelect() end,
 	}
-	for i = 1, #self.tabslist do
-		self.key:addCommands{
-			['_F'..i] = function() self.c_tabs.sel_j = 1 self.c_tabs.sel_i = i self.c_tabs:onUse("left") self.c_tabs:onSelect() end,
-			[{'_F'..i,"ctrl"}] = function() self.c_tabs.sel_j = 1 self.c_tabs.sel_i = i self.c_tabs:onUse("left", true) self.c_tabs:onSelect() end,
-		}
+	if self.tabslist then
+		for i = 1, #self.tabslist do
+			self.key:addCommands{
+				['_F'..i] = function() self.c_tabs.sel_j = 1 self.c_tabs.sel_i = i self.c_tabs:onUse("left") self.c_tabs:onSelect() end,
+				[{'_F'..i,"ctrl"}] = function() self.c_tabs.sel_j = 1 self.c_tabs.sel_i = i self.c_tabs:onUse("left", true) self.c_tabs:onSelect() end,
+			}
+		end
 	end
 
 	self.c_inven:onSelect()
diff --git a/game/modules/tome/dialogs/ShowStore.lua b/game/modules/tome/dialogs/ShowStore.lua
index 64ff5962f4..51a5a0d39e 100644
--- a/game/modules/tome/dialogs/ShowStore.lua
+++ b/game/modules/tome/dialogs/ShowStore.lua
@@ -18,12 +18,73 @@
 -- darkgod@te4.org
 
 require "engine.class"
-local Base = require "engine.dialogs.ShowStore"
+local Dialog = require "engine.ui.Dialog"
+local Inventory = require "engine.ui.Inventory"
+local Separator = require "engine.ui.Separator"
 
-module(..., package.seeall, class.inherit(Base))
+module(..., package.seeall, class.inherit(Dialog))
 
-function _M:init(...)
-	Base.init(self, ...)
+function _M:init(title, store_inven, actor_inven, store_filter, actor_filter, action, desc, descprice, allow_sell, allow_buy, on_select, store_actor, actor_actor)
+	self.on_select = on_select
+	self.allow_sell, self.allow_buy = allow_sell, allow_buy
+	self.action = action
+	self.desc = desc
+	self.descprice = descprice
+	self.store_inven = store_inven
+	self.actor_inven = actor_inven
+	self.store_filter = store_filter
+	self.actor_filter = actor_filter
+	Dialog.init(self, title or "Store", math.max(800, game.w * 0.8), math.max(600, game.h * 0.8))
+
+	self.c_inven = Inventory.new{actor=actor_actor, inven=actor_inven, filter=actor_filter, width=math.floor(self.iw / 2 - 10), height=self.ih - 10,
+		columns={
+			{name="", width={20,"fixed"}, display_prop="char", sort="id"},
+			{name="", width={24,"fixed"}, display_prop="object", direct_draw=function(item, x, y) item.object:toScreen(nil, x+4, y, 16, 16) end},
+			{name="Inventory", width=80, display_prop="name", sort="name"},
+			{name="Category", width=20, display_prop="cat", sort="cat"},
+			{name="Price", width={50,"fixed"}, display_prop=function(item) return self.descprice("sell", item.object) end, sort=function(a, b) return descprice("sell", a.object) < descprice("sell", b.object) end},
+		},
+		fct=function(item, sel, button, event) self:use(item, button, event) end,
+		on_select=function(item, sel) self:select(item) end,
+		on_drag=function(item) self:onDrag(item, "store-sell") end,
+		on_drag_end=function() self:onDragTakeoff("store-buy") end,
+	}
+	self.c_store = Inventory.new{actor=store_actor, inven=store_inven, filter=store_filter, width=math.floor(self.iw / 2 - 10), height=self.ih - 10, tabslist=false,
+		columns={
+			{name="", width={20,"fixed"}, display_prop="char", sort="id"},
+			{name="", width={24,"fixed"}, display_prop="object", direct_draw=function(item, x, y) item.object:toScreen(nil, x+4, y, 16, 16) end},
+			{name="Store", width=80, display_prop="name"},
+			{name="Category", width=20, display_prop="cat"},
+			{name="Price", width={50,"fixed"}, display_prop=function(item) return self.descprice("buy", item.object) end, sort=function(a, b) return descprice("buy", a.object) < descprice("buy", b.object) end},
+		},
+		fct=function(item, sel, button, event) self:use(item, button, event) end,
+		on_select=function(item, sel) self:select(item) end,
+		on_drag=function(item) self:onDrag(item, "store-buy") end,
+		on_drag_end=function() self:onDragTakeoff("store-sell") end,
+	}
+
+	self:loadUI{
+		{left=0, top=0, ui=self.c_store},
+		{right=0, top=0, ui=self.c_inven},
+		{hcenter=0, top=5, ui=Separator.new{dir="horizontal", size=self.ih - 10}},
+	}
+	self:setFocus(self.c_inven)
+	self:setupUI()
+
+	self.key:addCommands{
+		__TEXTINPUT = function(c)
+			local list
+			if self.focus_ui and self.focus_ui.ui == self.c_inven then list = self.c_inven.c_inven.list
+			elseif self.focus_ui and self.focus_ui.ui == self.c_store then list = self.c_store.c_inven.list
+			end
+			if list and list.chars[c] then
+				self:use(list[list.chars[c]])
+			end
+		end,
+	}
+	self.key:addBinds{
+		EXIT = function() game:unregisterDialog(self) end,
+	}
 
 	-- Add tooltips
 	self.on_select = function(item)
@@ -36,3 +97,74 @@ function _M:init(...)
 		if sym == self.key._LCTRL or sym == self.key._RCTRL then local i = self.cur_item self.cur_item = nil self:select(i) end
 	end
 end
+
+function _M:on_register()
+	game:onTickEnd(function() self.key:unicodeInput(true) end)
+end
+
+function _M:updateStore()
+	self:generateList()
+end
+
+function _M:select(item)
+	if self.cur_item == item then return end
+	if item then
+		if self.on_select then self.on_select(item) end
+	end
+	self.cur_item = item
+end
+
+function _M:on_focus(id, ui)
+	if self.focus_ui and self.focus_ui.ui == self.c_inven then self:select(self.c_inven.c_inven.list[self.c_inven.c_inven.sel])
+	elseif self.focus_ui and self.focus_ui.ui == self.c_store then self:select(self.c_store.c_inven.list[self.c_store.c_inven.sel])
+	end
+end
+
+function _M:use(item, force)
+	if item and item.object then
+		if self.focus_ui and self.focus_ui.ui == self.c_store then
+			if util.getval(self.allow_buy, item.object, item.item) then
+				self.action("buy", item.object, item.item)
+			end
+		else
+			if util.getval(self.allow_sell, item.object, item.item) then
+				self.action("sell", item.object, item.item)
+			end
+		end
+	end
+end
+
+function _M:generateList()
+	self.c_inven:generateList()
+	self.c_store:generateList()
+end
+
+function _M:onDrag(item, what)
+	if item and item.object then
+		local s = item.object:getEntityFinalSurface(nil, 64, 64)
+		local x, y = core.mouse.get()
+		game.mouse:startDrag(x, y, s, {kind=what, item_idx=item.item, inven=item.inven, object=item.object, id=item.object:getName{no_add_name=true, force_id=true, no_count=true}}, function(drag, used)
+			if not used then
+				local x, y = core.mouse.get()
+				game.mouse:receiveMouse("drag-end", x, y, true, nil, {drag=drag})
+			end
+		end)
+	end
+end
+
+function _M:onDragTakeoff(what)
+	local drag = game.mouse.dragged.payload
+	if drag.kind == what then
+		if what == "store-buy" then
+			if util.getval(self.allow_buy, drag.object, drag.item_idx) then
+				self.action("buy", drag.object, drag.item_idx)
+			end
+		else
+			if util.getval(self.allow_sell, drag.object, drag.item_idx) then
+				self.action("sell", drag.object, drag.item_idx)
+			end
+		end
+
+		game.mouse:usedDrag()
+	end
+end
-- 
GitLab