Skip to content
Snippets Groups Projects
UseTalents.lua 6.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • -- TE4 - T-Engine 4
    -- Copyright (C) 2009, 2010 Nicolas Casalini
    --
    -- This program is free software: you can redistribute it and/or modify
    -- it under the terms of the GNU General Public License as published by
    -- the Free Software Foundation, either version 3 of the License, or
    -- (at your option) any later version.
    --
    -- This program is distributed in the hope that it will be useful,
    -- but WITHOUT ANY WARRANTY; without even the implied warranty of
    -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    -- GNU General Public License for more details.
    --
    -- You should have received a copy of the GNU General Public License
    -- along with this program.  If not, see <http://www.gnu.org/licenses/>.
    --
    -- Nicolas Casalini "DarkGod"
    -- darkgod@te4.org
    
    require "engine.class"
    
    dg's avatar
    dg committed
    local Dialog = require "engine.ui.Dialog"
    local ListColumns = require "engine.ui.ListColumns"
    local Textzone = require "engine.ui.Textzone"
    
    dg's avatar
    dg committed
    local TextzoneList = require "engine.ui.TextzoneList"
    
    dg's avatar
    dg committed
    local Separator = require "engine.ui.Separator"
    
    dg's avatar
    dg committed
    module(..., package.seeall, class.inherit(Dialog))
    
    
    function _M:init(actor)
    	self.actor = actor
    	actor.hotkey = actor.hotkey or {}
    
    dg's avatar
    dg committed
    	Dialog.init(self, "Use Talents: "..actor.name, game.w * 0.7, game.h * 0.7)
    
    	self.c_tut = Textzone.new{width=math.floor(self.iw / 2 - 10), height=1, auto_height=true, no_color_bleed=true, text=[[
    You can bind a talent to a hotkey be pressing the corresponding hotkey while selecting a talent.
    Check out the keybinding screen in the game menu to bind hotkeys to a key (default is 1-0 plus control or shift).
    ]]}
    
    dg's avatar
    dg committed
    	self.c_desc = TextzoneList.new{width=math.floor(self.iw / 2 - 10), height=self.ih - self.c_tut.h - 20, scrollbar=true, no_color_bleed=true}
    
    dg's avatar
    dg committed
    	self.c_list = ListColumns.new{width=math.floor(self.iw / 2 - 10), height=self.ih - 10, sortable=true, scrollbar=true, columns={
    
    dg's avatar
    dg committed
    		{name="", width={20,"fixed"}, display_prop="char", sort="id"},
    
    dg's avatar
    dg committed
    		{name="Talent", width=80, display_prop="name", sort="name"},
    
    dg's avatar
    dg committed
    		{name="Status", width=20, display_prop="status", sort="status"},
    
    dg's avatar
    dg committed
    	}, list=self.list, fct=function(item) self:use(item) end, select=function(item, sel) self:select(item) end}
    
    	self:loadUI{
    		{left=0, top=0, ui=self.c_list},
    		{right=0, top=self.c_tut.h + 20, ui=self.c_desc},
    		{right=0, top=0, ui=self.c_tut},
    		{hcenter=0, top=5, ui=Separator.new{dir="horizontal", size=self.ih - 10}},
    	}
    	self:setFocus(self.c_list)
    	self:setupUI()
    
    dg's avatar
    dg committed
    	self.key:addCommands{
    
    dg's avatar
    dg committed
    			if self.list and self.list.chars[c] then
    
    dg's avatar
    dg committed
    				self:use(self.list.chars[c])
    
    dg's avatar
    dg committed
    	}
    	self.key:addBinds{
    
    		HOTKEY_1 = function() self:defineHotkey(1) end,
    		HOTKEY_2 = function() self:defineHotkey(2) end,
    		HOTKEY_3 = function() self:defineHotkey(3) end,
    		HOTKEY_4 = function() self:defineHotkey(4) end,
    		HOTKEY_5 = function() self:defineHotkey(5) end,
    		HOTKEY_6 = function() self:defineHotkey(6) end,
    		HOTKEY_7 = function() self:defineHotkey(7) end,
    		HOTKEY_8 = function() self:defineHotkey(8) end,
    		HOTKEY_9 = function() self:defineHotkey(9) end,
    		HOTKEY_10 = function() self:defineHotkey(10) end,
    		HOTKEY_11 = function() self:defineHotkey(11) end,
    		HOTKEY_12 = function() self:defineHotkey(12) end,
    		HOTKEY_SECOND_1 = function() self:defineHotkey(13) end,
    		HOTKEY_SECOND_2 = function() self:defineHotkey(14) end,
    		HOTKEY_SECOND_3 = function() self:defineHotkey(15) end,
    		HOTKEY_SECOND_4 = function() self:defineHotkey(16) end,
    		HOTKEY_SECOND_5 = function() self:defineHotkey(17) end,
    		HOTKEY_SECOND_6 = function() self:defineHotkey(18) end,
    		HOTKEY_SECOND_7 = function() self:defineHotkey(19) end,
    		HOTKEY_SECOND_8 = function() self:defineHotkey(20) end,
    		HOTKEY_SECOND_9 = function() self:defineHotkey(21) end,
    		HOTKEY_SECOND_10 = function() self:defineHotkey(22) end,
    		HOTKEY_SECOND_11 = function() self:defineHotkey(23) end,
    		HOTKEY_SECOND_12 = function() self:defineHotkey(24) end,
    		HOTKEY_THIRD_1 = function() self:defineHotkey(25) end,
    		HOTKEY_THIRD_2 = function() self:defineHotkey(26) end,
    		HOTKEY_THIRD_3 = function() self:defineHotkey(27) end,
    		HOTKEY_THIRD_4 = function() self:defineHotkey(28) end,
    		HOTKEY_THIRD_5 = function() self:defineHotkey(29) end,
    		HOTKEY_THIRD_6 = function() self:defineHotkey(30) end,
    		HOTKEY_THIRD_7 = function() self:defineHotkey(31) end,
    		HOTKEY_THIRD_8 = function() self:defineHotkey(31) end,
    		HOTKEY_THIRD_9 = function() self:defineHotkey(33) end,
    		HOTKEY_THIRD_10 = function() self:defineHotkey(34) end,
    		HOTKEY_THIRD_11 = function() self:defineHotkey(35) end,
    		HOTKEY_THIRD_12 = function() self:defineHotkey(36) end,
    		EXIT = function() game:unregisterDialog(self) end,
    	}
    end
    
    function _M:defineHotkey(id)
    	if not self.actor.hotkey then return end
    
    dg's avatar
    dg committed
    	local item = self.list[self.c_list.sel]
    
    dg's avatar
    dg committed
    	if not item or not item.talent then return end
    
    dg's avatar
    dg committed
    	self.actor.hotkey[id] = {"talent", item.talent}
    	self:simplePopup("Hotkey "..id.." assigned", self.actor:getTalentFromId(item.talent).name:capitalize().." assigned to hotkey "..id)
    
    dg's avatar
    dg committed
    function _M:select(item)
    
    dg's avatar
    dg committed
    	if item then
    
    dg's avatar
    dg committed
    		self.c_desc:switchItem(item, item.desc)
    
    dg's avatar
    dg committed
    	end
    end
    
    function _M:use(item)
    
    dg's avatar
    dg committed
    	if not item or not item.talent then return end
    
    
    dg's avatar
    dg committed
    	self.actor:useTalent(item.talent)
    
    end
    
    function _M:generateList()
    	-- Makes up the list
    	local list = {}
    
    dg's avatar
    dg committed
    	list.chars = {}
    	local letter = 1
    
    	for i, tt in ipairs(self.actor.talents_types_def) do
    		local cat = tt.type:gsub("/.*", "")
    		local where = #list
    		local added = false
    
    		-- Find all talents of this school
    		for j, t in ipairs(tt.talents) do
    			if self.actor:knowTalent(t.id) and t.mode ~= "passive" then
    				local typename = "talent"
    
    dg's avatar
    dg committed
    				local status = tstring{{"color", "LIGHT_GREEN"}, "Active"}
    
    				if self.actor:isTalentCoolingDown(t) then status = tstring{{"color", "LIGHT_RED"}, self.actor:isTalentCoolingDown(t).." turns"} end
    
    dg's avatar
    dg committed
    				if t.mode == "sustained" then status = self.actor:isTalentActive(t.id) and tstring{{"color", "YELLOW"}, "Sustaining"} or tstring{{"color", "LIGHT_GREEN"}, "Sustain"} end
    
    dg's avatar
    dg committed
    				list[#list+1] = { char=self:makeKeyChar(letter), name=t.name.." ("..typename..")", status=status, talent=t.id, desc=self.actor:getTalentFullDescription(t) }
    
    dg's avatar
    dg committed
    				list.chars[self:makeKeyChar(letter)] = list[#list]
    
    				if not self.sel then self.sel = #list + 1 end
    				letter = letter + 1
    				added = true
    			end
    		end
    
    		if added then
    
    dg's avatar
    dg committed
    			table.insert(list, where+1, { char="", name=tstring{{"font","bold"}, cat:capitalize().." / "..tt.name:capitalize(), {"font","normal"}}, type=tt.type, color={0x80, 0x80, 0x80}, status="", desc=tt.description })
    
    dg's avatar
    dg committed
    	for i = 1, #list do list[i].id = i end