All pastes #445981 Raw Edit

Chao7

public text v1 · immutable
·published 2007-04-18 06:27 UTC
rendered paste body

-- db.actor is the player character

-- ui_repair_items.uiRepairDialog( repairer, cost_modifier )
class "uiRepairDialog" (CUIScriptWnd)

local logf_console = nil

function logf( fmt, ... )
	logf_console = get_console()
	logf = logf_main
	logf_main( fmt, ... )
end

function logf_main( fmt, ... )
    local c = select( "#", ... )
    if c <= 0 then -- nothing to format
        logf_console:execute( ":" .. string.gsub( fmt, " ", "\160" ) )
    else
        logf_console:execute( ":" .. string.gsub( string.format( fmt, ... ), " ", "\160" ) )
    end
end

local color_valid = GetARGB( 255, 255, 255, 255 )
local color_invalid = GetARGB( 255, 255, 0, 0 )

-- find repairable items on someone
function find_repairable_items( owner, repair_table )
	logf( "find_repairable_items() called")

	-- the to-be-filled table of repairable items from owner
	local items_repairable = {}

	-- for find_repairable_items - what is iterated over each item
	local iterant_find_repairable_item

    if repair_table == nil or repair_table.length == 0 then
		function iterant_find_repairable_item( owner, item )
			logf( "iterant_find_repairable_item() called [no repair_table]")
			table.insert( items_repairable, item )
			logf( "iterant_find_repairable_item() complete")
		end
    else
		function iterant_find_repairable_item( owner, item )
			logf( "iterant_find_repairable_item() called [repair_table]")
			local item_section = item:section()
			local item_repairable = repair_table[item_section]
			if item_repairable ~= nil then
				local item_condition = item:condition()
				if item_repairable >= 0 then
					if item_condition < item_repairable then
						table.insert( items_repairable, item )
					end
				elseif item_repairable == -1 then
				    table.insert( items_repairable, item )
				end
			end
			logf( "iterant_find_repairable_item() complete")
		end
    end

-- fill the items_repairable table
	owner:iterate_inventory( iterant_find_repairable_item, owner )
	logf( "find_repairable_items() complete")
	return items_repairable
end

function calculate_cost_at_condition( cost, condition )
	return math.ceil( cost - ( cost * condition ) )
end


-- returns 2 values: condition repaired to, and cost of repairs
function calculate_repairable( cost_modifier, condition_current, target_condition, budget )
	logf( "calculate_repairable() called")

	-- check current condition against target condition
	if condition_current > target_condition then
	logf( "calculate_repairable() complete")
		return condition_current, 0
	end

	-- calculate amount of repair needed
	local repair_needed = target_condition - condition_current

	-- calculate maximum repair cost for fractional cost
	local repair_max_cost = math.ceil( item:cost() * cost_modifier )

	-- calculate amount repairable under budget
	local repair_budgeted = budget / repair_max_cost

	-- repair to target condition can be made under budget
	if repair_budgeted > repair_needed then
		logf( "calculate_repairable() complete")
		return repair_needed, math.ceil( repair_needed * repair_max_cost )
	end

	-- repair can only be made to budget cap
	logf( "calculate_repairable() complete")
	return repair_budgeted, budget
end

function condition_as_color( condition )
	logf( "condition_as_color() called")
	local n
	local r, g
	-- to scale down means n*v, the opposite, scale up, means (1-n)*v
	if        condition >= 0.75   then -- lime   (255,   0) -> green  (128,   0)
	    n = ( condition -  0.75 ) * 4
		-- scale down green from full to half
		r, g =          0     , ((  n)*128)+127

	elseif    condition >= 0.50   then -- green  (128,   0) -> yellow (255, 255)
	    n = ( condition -  0.50 ) * 4
	    -- scale up green from half to full, scale up red from none to full
		r, g = ((1-n)*255)    , ((1-n)*128)+127

	elseif    condition >= 0.25   then -- yellow (255, 255) -> olive  (128, 128)
	    n = ( condition -  0.25 ) * 4
	    -- scale down green from full to half, scale down red from full to half
		r, g = ((  n)*128)+127, ((  n)*128)+127

	else -- ( condition >= 0.00 ) then -- olive  (128, 128) -> red    (255,   0)
	    n =   condition          * 4
	    -- scale down green from half to none, scale up red from half to full
		r, g = ((1-n)*128)+127, ((  n)*128)

	end

	logf( "condition_as_color() complete")
	return GetARGB( 255, math.floor( r ), math.ceil( g ), 0 );
end

function translate( string )
	return game.translate_string( "ui_repair_items_" .. string )
end

function r_unpack( r )
	return r.left, r.top, r.width, r.height
end

--[[  set of registered in game fonts:

 GetFontArialN21Russian()
 GetFontGraffiti32Russian()
 GetFontDI()
 GetFontHeaderRussian()
 GetFontGraffiti19Russian()
 GetFontLetterica25()
 GetFontMedium()
 GetFontLetterica16Russian()
 GetFontSmall()
 GetFontGraffiti22Russian()
 GetFontLetterica18Russian()
 GetFontBigDigit()
 GetFontHeaderEurope()
 GetFontGraffiti50Russian()

--]]

--[[ take control by name, dependent on type

 GetCheckButton(string)
 GetFrameLineWnd(string)
 GetPropertiesBox(string)
 GetListWnd(string)
 GetStatic(string)
 GetEditBox(string)
 GetTabControl(string)
 GetRadioGroup(string)
 GetDialogWnd(string)
 GetButton(string)
 GetMessageBox(string)
 GetProgressBar(string)
 GetRadioButton(string)
 GetFrameWindow(string)

--]]

function main( repairer, cost_modifier )
	logf( "main() called")
	local dlg
	if repairer.repair_cost_modifier ~= nil and cost_modifier ~= nil then
		dlg = ui_repair_items.uiRepairDialog( repairer, repairer.repair_cost_modifier * cost_modifier )
	elseif repairer.repair_cost_modifier ~= nil then
		dlg = ui_repair_items.uiRepairDialog( repairer, repairer.repair_cost_modifier )
	elseif cost_modifier ~= nil then
	    dlg = ui_repair_items.uiRepairDialog( repairer, cost_modifier )
	else
		dlg = ui_repair_items.uiRepairDialog( repairer, (2/3) )
	end
	dlg.Open()
	while dlg.IsOpen do
		wait( 1000 )
	end
	logf( "main() complete")
end

function uiRepairDialog:__init( repairer, cost_modifier ) super()
	logf( "uiRepairDialog:__init() called")
	self.repairer = repairer
	self.cost_modifier = cost_modifier
	self.last_recalc_params = { nil, nil }
	self.valid_repair = false
	self.repairable_items = {}
	self.IsOpen = false
	-- set base font for dialog
	self:SetFont( GetFontMedium() )
	self:InitControls()
	self:InitCallBacks()
	logf( "uiRepairDialog:__init() complete")
end

function uiRepairDialog:__finalize()
	logf( "uiRepairDialog:__finalize() called")
	close( self )
	logf( "uiRepairDialog:__finalize() complete")
end


function uiRepairDialog:SetClGame( game_cl )
	logf( "uiRepairDialog:SetClGame() called")
	self.m_game_cl = game_cl
	logf( "uiRepairDialog:SetClGame() complete")
end

function uiRepairDialog:InitControls()
	logf( "uiRepairDialog:InitControls() called")
	local money = db.actor:money()
	local dev = device()
	local screen_w, screen_h = dev.width, dev.height
	local size_w, size_h = screen_w-100, screen_h-100
	local pos_x, pos_y = (screen_w/2) - (size_w/2), (screen_h/2) - (size_h/2)

	local margin_size = 10
	local top_margin, left_margin = margin_size, margin_size
	local bottom_margin, right_margin = size_h-margin_size, size_w-margin_size
	
	-- init self position and size
	--  position & size: x (from left), y (from top), width, height
	self:Init( pos_x, pos_y, size_w, size_h )

    ---------------------------------------------------------------- BACKGROUND
	logf( "constructing bkg_repair_wnd" )
	local bkg_repair_wnd = CUIStatic()
	local bkg_repair_wnd_r = {
		left   = 0,
		top    = 0,
		width  = size_w,
		height = size_h
	}
	bkg_repair_wnd:SetAutoDelete( true )
	bkg_repair_wnd:SetWindowName( "bkg_repair_wnd" )
	bkg_repair_wnd:Init( r_unpack( bkg_repair_wnd_r ) )
	bkg_repair_wnd:SetColor( GetARGB( 255, 0, 0, 0 ) )
	bkg_repair_wnd:ClipperOn()
	self:AttachChild( bkg_repair_wnd )
	
	-- Start creating controls from bottom of window up up

	------------------------------------------------------------- REPAIR BUTTON
	logf( "constructing btn_repair" )
	local btn_repair = CUIButton()
	local btn_repair_r = {
		left   = left_margin,
		top    = bottom_margin-40,
		width  = 135,
		height = 40
	}
	btn_repair:SetAutoDelete( true )
	btn_repair:SetWindowName( "btn_repair" )
	btn_repair:Init( r_unpack( btn_repair_r ) )
	btn_repair:SetText( translate( "btn_repair" ) )
	btn_repair:SetTextAlign( CGameFont.alCenter )
	btn_repair:SetTextY( -1 )
	self:Register( btn_repair )
	bkg_repair_wnd:AttachChild( btn_repair )

	------------------------------------------------------------- CANCEL BUTTON
	logf( "constructing btn_cancel" )
	local btn_cancel = CUIButton()
	local btn_cancel_r = {
		left   = right_margin-btn_repair_r.width,
		top    = btn_repair_r.top,
		width  = btn_repair_r.width,
		height = btn_repair_r.height
	}
	btn_cancel:SetAutoDelete( true )
	btn_cancel:SetWindowName( "btn_cancel" )
	btn_cancel:Init( r_unpack( btn_cancel_r ) )
	btn_cancel:SetText( translate( "btn_cancel" ) )
	btn_cancel:SetTextAlign( CGameFont.alCenter )
	btn_cancel:SetTextY( -1 )
	self:Register( btn_cancel )
	bkg_repair_wnd:AttachChild( btn_cancel )

	----------------------------------------------------------- TIME COST LABEL
	logf( "constructing lbl_repair_time" )
	local lbl_repair_time = CUIStatic()
	local lbl_repair_time_r = {
		left   = left_margin,
		top    = btn_repair_r.top-5,
		width  = size_w-(margin_size*2),
		height = btn_repair_r.height
	}
	lbl_repair_time:SetAutoDelete( true )
	lbl_repair_time:SetWindowName( "lbl_repairable_items" )
	lbl_repair_time:Init( r_unpack( lbl_repair_time_r ) )
	lbl_repair_time:SetTextColor( GetARGB(255,0,0,0) )
	lbl_repair_time:SetText( translate( "time_unknown" ) )
	lbl_repair_time:SetTextAlign( CGameFont.alCenter )
	lbl_repair_time:SetTextY( -1 )
	self:Register( lbl_repair_time )
	bkg_repair_wnd:AttachChild( lbl_repair_time )

	-------------------------------------------------- IMPROVED CONDITION LABEL
	logf( "constructing lbl_condition_result" )
	local lbl_condition_result = CUIStatic()
	local lbl_condition_result_r = {
		left   = left_margin,
		top    = lbl_repair_time_r.top-5,
		width  = 200,
		height = btn_repair_r.height
	}
	lbl_condition_result:SetAutoDelete( true )
	lbl_condition_result:SetWindowName( "lbl_condition_result" )
	lbl_condition_result:Init( r_unpack( lbl_condition_result_r ) )
	lbl_condition_result:SetText( translate( "lbl_condition_result" ) )

	lbl_condition_result:SetTextAlign( CGameFont.alRight )
	lbl_condition_result:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_condition_result )

	--------------------------------------------------- IMPROVE CONDITION VALUE
	logf( "constructing ebx_condition_result_v" )
	local ebx_condition_result_v = CUIEditBox()
	local ebx_condition_result_v_r = {
		left   = lbl_condition_result_r.left + lbl_condition_result_r.width + 5,
		top    = lbl_condition_result_r.top,
		width  = 40,
		height = lbl_condition_result_r.height
	}
	ebx_condition_result_v:SetAutoDelete( true )
	ebx_condition_result_v:SetWindowName( "ebx_condition_result_v" )
	ebx_condition_result_v:Init( r_unpack( ebx_condition_result_v_r ) )
	ebx_condition_result_v:SetText("0")
	ebx_condition_result_v:SetNumbersOnly( true )
	ebx_condition_result_v:SetTextAlignment( CGameFont.alRight )
	ebx_condition_result_v:SetTextColor( color_invalid )
	bkg_repair_wnd:AttachChild( ebx_condition_result_v )

	------------------------------------- IMPROVED CONDITION LABEL PERCENT SIGN
	logf( "constructing lbl_condition_result_pct" )
	local lbl_condition_result_pct = CUIStatic()
	local lbl_condition_result_pct_r = {
		left   = ebx_condition_result_v_r.left+ebx_condition_result_v_r.width+5,
		top    = ebx_condition_result_v_r.top,
		width  = 20,
		height = ebx_condition_result_v_r.height
	}
	lbl_condition_result_pct:SetAutoDelete( true )
	lbl_condition_result_pct:SetWindowName( "lbl_condition_result_pct" )
	lbl_condition_result_pct:Init( r_unpack( lbl_condition_result_pct_r ) )
	lbl_condition_result_pct:SetText( translate( "pct" ) )
	lbl_condition_result_pct:SetTextAlign( CGameFont.alLeft )
	lbl_condition_result_pct:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_condition_result_pct )

	--------------------------------------------------- IMPROVE CONDITION LABEL
	logf( "constructing lbl_condition_improve" )
	local lbl_condition_improve = CUIStatic()
	local lbl_condition_improve_r = {
		left   = lbl_condition_result_r.left,
		top    = (lbl_condition_result_r.top-lbl_condition_result_r.height)-5,
		width  = lbl_condition_result_r.width,
		height = lbl_condition_result_r.height
	}
	lbl_condition_improve:SetAutoDelete( true )
	lbl_condition_improve:SetWindowName( "lbl_condition_improve" )
	lbl_condition_improve:Init( r_unpack( lbl_condition_improve_r ) )
	lbl_condition_improve:SetText( translate( "lbl_condition_improve" ) )
	lbl_condition_improve:SetTextAlign( CGameFont.alRight )
	lbl_condition_improve:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_condition_improve )

	--------------------------------------------------- IMPROVE CONDITION VALUE
	logf( "constructing ebx_condition_improve_v" )
	local ebx_condition_improve_v = CUIEditBox()
	local ebx_condition_improve_v_r = {
		left   = ebx_condition_result_v_r.left,
		top    = lbl_condition_improve_r.top,
		width  = ebx_condition_result_v_r.width,
		height = ebx_condition_result_v_r.height
	}
	ebx_condition_improve_v:SetAutoDelete( true )
	ebx_condition_improve_v:SetWindowName( "ebx_condition_improve_v" )
	ebx_condition_improve_v:Init( r_unpack( ebx_condition_improve_v_r ) )
	ebx_condition_improve_v:SetText("0")
	ebx_condition_improve_v:SetNumbersOnly( true )
	ebx_condition_improve_v:SetTextAlignment( CGameFont.alRight )
	ebx_condition_improve_v:SetTextColor( GetARGB(255,255,0,0) )
	bkg_repair_wnd:AttachChild( ebx_condition_improve_v )

	-------------------------------------- IMPROVE CONDITION LABEL PERCENT SIGN
	logf( "constructing lbl_condition_improve_pct" )
	local lbl_condition_improve_pct = CUIStatic()
	local lbl_condition_improve_pct_r = {
		left   = lbl_condition_result_pct_r.left,
		top    = lbl_condition_improve_r.top,
		width  = lbl_condition_result_pct_r.width,
		height = lbl_condition_result_pct_r.height
	}
	lbl_condition_improve_pct:SetAutoDelete( true )
	lbl_condition_improve_pct:SetWindowName( "lbl_condition_improve_pct" )
	lbl_condition_improve_pct:Init( r_unpack( lbl_condition_improve_pct_r ) )
	lbl_condition_improve_pct:SetText( translate( "pct" ) )
	lbl_condition_improve_pct:SetTextAlign( CGameFont.alLeft )
	lbl_condition_improve_pct:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_condition_improve_pct )

	--------------------------------------------------- CURRENT CONDITION LABEL
	logf( "constructing lbl_condition_current" )
	local lbl_condition_current = CUIStatic()
	local lbl_condition_current_r = {
		left   = lbl_condition_improve_r.left,
		top    = (lbl_condition_improve_r.top-lbl_condition_improve_r.height)-5,
		width  = lbl_condition_improve_r.width,
		height = lbl_condition_improve_r.height
	}
	lbl_condition_current:SetAutoDelete( true )
	lbl_condition_current:SetWindowName( "lbl_condition_current" )
	lbl_condition_current:Init( r_unpack( lbl_condition_current_r ) )
	lbl_condition_current:SetText( translate( "lbl_condition_current" ) )
	lbl_condition_current:SetTextAlign( CGameFont.alRight )
	lbl_condition_current:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_condition_current )

	--------------------------------------------------- CURRENT CONDITION VALUE
	logf( "constructing ebx_condition_current_v" )
	local ebx_condition_current_v = CUIEditBox()
	local ebx_condition_current_v_r = {
		left   = ebx_condition_improve_v_r.left,
		top    = lbl_condition_current_r.top,
		width  = ebx_condition_improve_v_r.width,
		height = ebx_condition_improve_v_r.height
	}
	ebx_condition_current_v:SetAutoDelete( true )
	ebx_condition_current_v:SetWindowName( "ebx_condition_current_v" )
	ebx_condition_current_v:Init( r_unpack( ebx_condition_current_v_r ) )
	ebx_condition_current_v:SetText("0")
	ebx_condition_current_v:SetTextAlignment( CGameFont.alRight )
	ebx_condition_current_v:Enable(false) -- read only
	bkg_repair_wnd:AttachChild( ebx_condition_current_v )

	-------------------------------------- CURRENT CONDITION LABEL PERCENT SIGN
	logf( "constructing lbl_condition_current_pct" )
	local lbl_condition_current_pct = CUIStatic()
	local lbl_condition_current_pct_r = {
		left   = ebx_condition_improve_v_r.left,
		top    = lbl_condition_current_r.top,
		width  = ebx_condition_improve_v_r.width,
		height = ebx_condition_improve_v_r.height
	}
	lbl_condition_current_pct:SetAutoDelete( true )
	lbl_condition_current_pct:SetWindowName( "lbl_condition_current_pct" )
	lbl_condition_current_pct:Init( r_unpack( lbl_condition_current_pct_r ) )
	lbl_condition_current_pct:SetText( translate( "pct" ) )
	lbl_condition_current_pct:SetTextAlign( CGameFont.alLeft )
	lbl_condition_current_pct:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_condition_current_pct )

	-- right aligned, horizontally reversed control creation

	---------------------------------------------------- MONEY REMAINING RUBLES
	logf( "constructing lbl_money_remaining_ru" )
	local lbl_money_remaining_ru = CUIStatic()
	local lbl_money_remaining_ru_r = {
		left   = right_margin-30,
		top    = lbl_condition_result_r.top,
		width  = 30,
		height = lbl_condition_result_r.height
	}
	lbl_money_remaining_ru:SetAutoDelete( true )
	lbl_money_remaining_ru:SetWindowName( "lbl_money_remaining_ru" )
	lbl_money_remaining_ru:Init( r_unpack( lbl_money_remaining_ru_r ) )
	lbl_money_remaining_ru:SetText( translate( "rubles" ) )
	lbl_money_remaining_ru:SetTextAlign( CGameFont.alLeft )
	lbl_money_remaining_ru:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_money_remaining_ru )

	----------------------------------------------------- MONEY REMAINING VALUE
    logf( "constructing ebx_money_remaining_v" )
	local ebx_money_remaining_v = CUIEditBox()
	local ebx_money_remaining_v_r = {
		left   = (lbl_money_remaining_ru_r.left-lbl_money_remaining_ru_r.width)-5,
		top    = lbl_money_remaining_ru_r.top,
		width  = 120,
		height = lbl_money_remaining_ru_r.height
	}
	ebx_money_remaining_v:SetAutoDelete( true )
	ebx_money_remaining_v:SetWindowName( "ebx_money_remaining_v" )
	ebx_money_remaining_v:Init( r_unpack( ebx_money_remaining_v_r ) )
	ebx_money_remaining_v:SetText( money )
	ebx_money_remaining_v:SetNumbersOnly( true )
	ebx_money_remaining_v:SetTextAlignment( CGameFont.alRight )
	ebx_money_remaining_v:SetTextColor( GetARGB(255,255,0,0) )
	bkg_repair_wnd:AttachChild( ebx_money_remaining_v )

	----------------------------------------------------- MONEY REMAINING LABEL
    logf( "constructing lbl_money_remaining" )
	local lbl_money_remaining = CUIStatic()
	local lbl_money_remaining_r = {
		left   = (lbl_money_remaining_ru_r.left-lbl_money_remaining_ru_r.width)-5,
		top    = lbl_money_remaining_ru_r.top,
		width  = 200,
		height = lbl_money_remaining_ru_r.height
	}
	lbl_money_remaining:SetAutoDelete( true )
	lbl_money_remaining:SetWindowName( "lbl_money_remaining" )
	lbl_money_remaining:Init( r_unpack( lbl_money_remaining_r ) )
	lbl_money_remaining:SetText( translate( "lbl_money_remaining" ) )
	lbl_money_remaining:SetTextAlign( CGameFont.alRight )
	lbl_money_remaining:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_money_remaining )

	-------------------------------------------------------- SPEND MONEY RUBLES
    logf( "constructing lbl_money_spending_ru" )
	local lbl_money_spending_ru = CUIStatic()
	local lbl_money_spending_ru_r = {
		left   = lbl_money_remaining_ru_r.left,
		top    = (lbl_money_remaining_ru_r.top-lbl_money_remaining_ru_r.height)-5,
		width  = lbl_money_remaining_ru_r.width,
		height = lbl_money_remaining_ru_r.height
	}
	lbl_money_spending_ru:SetAutoDelete( true )
	lbl_money_spending_ru:SetWindowName( "lbl_money_spending_ru" )
	lbl_money_spending_ru:Init( r_unpack( lbl_money_remaining_r ) )
	lbl_money_spending_ru:SetText( translate( "rubles" ) )
	lbl_money_spending_ru:SetTextAlign( CGameFont.alLeft )
	lbl_money_spending_ru:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_money_spending_ru )

	--------------------------------------------------------- SPEND MONEY VALUE
    logf( "constructing ebx_money_spending_v" )
	local ebx_money_spending_v = CUIEditBox()
	local ebx_money_spending_v_r = {
		left   = ebx_money_remaining_v_r.left,
		top    = lbl_money_spending_ru_r.top,
		width  = ebx_money_remaining_v_r.width,
		height = ebx_money_remaining_v_r.height
	}
	ebx_money_spending_v:SetAutoDelete( true )
	ebx_money_spending_v:SetWindowName( "ebx_money_spending_v" )
	ebx_money_spending_v:Init( r_unpack( ebx_money_spending_v_r ) )
	ebx_money_spending_v:SetText( "0" )
	ebx_money_spending_v:SetNumbersOnly( true )
	ebx_money_spending_v:SetTextAlignment( CGameFont.alRight )
	ebx_money_spending_v:SetTextColor( GetARGB(255,255,0,0) )
	bkg_repair_wnd:AttachChild( ebx_money_spending_v )

	--------------------------------------------------------- SPEND MONEY LABEL
    logf( "constructing lbl_money_spending" )
	local lbl_money_spending = CUIStatic()
    logf( "constructing lbl_money_spending 1" )
	local lbl_money_spending_r = {
		left   = lbl_money_remaining_r.left,
		top    = lbl_money_spending_ru_r.top,
		width  = lbl_money_remaining_r.width,
		height = lbl_money_remaining_r.height
	}
	lbl_money_spending:SetAutoDelete( true )
	lbl_money_spending:SetWindowName( "lbl_money_spending" )
	lbl_money_spending:Init( r_unpack( lbl_money_spending_r ) )
	lbl_money_spending:SetText( translate( "lbl_money_spending" ) )
	lbl_money_spending:SetTextAlign( CGameFont.alRight )
	lbl_money_spending:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_money_spending )

	------------------------------------------------------ CURRENT MONEY RUBLES
    logf( "constructing lbl_money_current_ru" )
	local lbl_money_current_ru = CUIStatic()
	local lbl_money_current_ru_r = {
		left   = lbl_money_remaining_ru_r.left,
		top    = (lbl_money_spending_ru_r.top-lbl_money_spending_ru_r.height)-5,
		width  = lbl_money_remaining_ru_r.width,
		height = lbl_money_remaining_ru_r.height
	}
	lbl_money_current_ru:SetAutoDelete( true )
	lbl_money_current_ru:SetWindowName( "lbl_money_current_ru" )
	lbl_money_current_ru:Init( r_unpack( lbl_money_current_ru_r ) )
	lbl_money_current_ru:SetText( translate( "rubles" ) )
	lbl_money_current_ru:SetTextAlign( CGameFont.alLeft )
	lbl_money_current_ru:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_money_current_ru )

	------------------------------------------------------- CURRENT MONEY VALUE
    logf( "constructing lbl_money_current_v" )
	local lbl_money_current_v = CUIStatic()
	local lbl_money_current_v_r = {
		left   = ebx_money_remaining_v_r.left,
		top    = lbl_money_current_ru_r.top,
		width  = ebx_money_remaining_v_r.width,
		height = ebx_money_remaining_v_r.height
	}
	lbl_money_current_v:SetAutoDelete( true )
	lbl_money_current_v:SetWindowName( "lbl_money_current_v" )
	lbl_money_current_v:Init( r_unpack( lbl_money_current_v_r ) )
	lbl_money_current_v:SetText( money )
	lbl_money_current_v:SetTextAlign( CGameFont.alRight )
	lbl_money_current_v:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_money_current_v )

	------------------------------------------------------- CURRENT MONEY LABEL
    logf( "constructing lbl_money_current" )
	local lbl_money_current = CUIStatic()
	local lbl_money_current_r = {
		left   = lbl_money_remaining_r.left,
		top    = lbl_money_current_ru_r.top,
		width  = lbl_money_remaining_r.width,
		height = lbl_money_remaining_r.height
	}
	lbl_money_current:SetAutoDelete( true )
	lbl_money_current:SetWindowName( "lbl_money_current" )
	lbl_money_current:Init( r_unpack( lbl_money_current_r ) )
	lbl_money_current:SetText( translate( "lbl_money_current" ) )
	lbl_money_current:SetTextAlign( CGameFont.alRight )
	lbl_money_current:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_money_current )

	-- Switch to creating controls from top down here

	------------------------------------------------------ RUSH REPAIR CHECKBOX
    logf( "constructing chb_repair_rush" )
	local chb_repair_rush = CUICheckButton()
	local chb_repair_rush_r = {
		left   = right_margin-btn_repair_r.width,
		top    = top_margin,
		width  = btn_repair_r.width,
		height = btn_repair_r.height
	}
	chb_repair_rush:SetAutoDelete( true )
	chb_repair_rush:SetWindowName( "chb_repair_rush" )
	chb_repair_rush:Init( r_unpack( chb_repair_rush_r ) )
	chb_repair_rush:SetText( translate( "chb_repair_rush" ) )
	chb_repair_rush:SetTextAlign( CGameFont.alCenter )
	chb_repair_rush:SetTextY( -1 )
	self:Register( chb_repair_rush )
	bkg_repair_wnd:AttachChild( chb_repair_rush )

	----------------------------------------------- REPAIRABLE ITEMS LIST LABEL
    logf( "constructing lbl_repairable_items" )
	local lbl_repairable_items = CUIStatic()
	local lbl_repairable_items_r = {
		left   = left_margin,
		top    = top_margin,
		width  = (size_w-(margin_size*2))/2,
		height = btn_repair_r.height
	}
	lbl_repairable_items:SetAutoDelete( true )
	lbl_repairable_items:SetWindowName( "lbl_repairable_items" )
	lbl_repairable_items:Init( r_unpack( lbl_repairable_items_r ) )
	lbl_repairable_items:SetText( translate( "lbl_repairable_items" ) )
	lbl_repairable_items:SetTextAlign( CGameFont.alCenter )
	lbl_repairable_items:SetTextY( -1 )
	bkg_repair_wnd:AttachChild( lbl_repairable_items )

	----------------------------------------------------- REPAIRABLE ITEMS LIST
    logf( "constructing lst_repairable_items" )
	local lst_repairable_items = CUIListWnd()
	local lst_repairable_items_r = {
		left   = lbl_repairable_items_r.left,
		top    = lbl_repairable_items_r.top+lbl_repairable_items_r.height+5,
		width  = size_w-(margin_size*2),
		height = size_h-(5+lbl_money_current_ru_r.height)
	}
	lst_repairable_items:SetAutoDelete( true )
	lst_repairable_items:SetWindowName( "lst_repairable_items" )
	lst_repairable_items:SetItemHeight( 20 )
    lst_repairable_items:Init( r_unpack( lst_repairable_items_r ) )
    lst_repairable_items:EnableScrollBar( true )
	lst_repairable_items:ShowSelectedItem( true )
	self:Register( lst_repairable_items )
    bkg_repair_wnd:AttachChild( lst_repairable_items )

	self.repairable_items = find_repairable_items( db.actor )

	local lsi_item, lsi_item_name, item, item_condition, item_condition_percent

    logf( "constructing lst_repairable_items for loop" )
	for i, item in ipairs( self.repairable_items ) do
		item_condition = item:condition()
		item_condition_percent = math.floor(item_condition*100)
		lsi_item_name = string.format( "lsi_%i_%i", item:id(), item_condition_percent )
		-------------------------------------------- REPAIRABLE ITEMS LIST ITEM
	    logf( "constructing %s", lsi_item_name )
	    lsi_item = CUIListItem()
	    lsi_item:SetAutoDelete( true )
	    lsi_item:SetWindowName( lsi_item_name )
		lsi_item:Init( 0, 0, lst_repairable_items_r.width, 20 )
		lsi_item:SetText( item:name().." ("..item_condition_percent..translate( "pct" )..")" )
		lsi_item:SetTextAlign( CGameFont.alCenter )
		lsi_item:SetTextY( -1 )

		lsi_item:SetTextColor( condition_as_color( item_condition ) )

		lst_repairable_items:AddItem( lsi_item )
	end

    logf( "uiRepairDialog:InitControls() complete" )
end

function uiRepairDialog:RegisterCallbacks( registry )
    logf( "uiRepairDialog:RegisterCallbacks() called" )
	for element, callbacks in pairs( registry ) do
	    for i, callback in ipairs( callbacks ) do
	    	logf( "uiRepairDialog:RegisterCallbacks() %s callback #%i (%i) %s",
				element, i, callback.id, tostring( callback.fn ) )
	    	self:AddCallback( element, callback.id, callback.fn, self )
	    end
	end
    logf( "uiRepairDialog:RegisterCallbacks() complete" )
end

function uiRepairDialog:InitCallBacks()
    logf( "uiRepairDialog:InitCallBacks() called" )

	self:RegisterCallbacks( {
		chb_repair_rush = {
			{ fn = self.on_chb_repair_rush_set, id = ui_events.CHECK_BUTTON_SET },
			{ fn = self.on_chb_repair_rush_reset, id = ui_events.CHECK_BUTTON_RESET }
		},
		ebx_condition_improve_v = {
			{ fn = self.on_ebx_condition_improve_v_changed, id = ui_events.EDIT_TEXT_CHANGED },
			{ fn = self.on_ebx_condition_improve_v_commit, id = ui_events.EDIT_TEXT_COMMIT }
		},
		ebx_condition_result_v = {
			{ fn = self.on_ebx_condition_result_v_changed, id = ui_events.EDIT_TEXT_CHANGED },
			{ fn = self.on_ebx_condition_result_v_commit, id = ui_events.EDIT_TEXT_COMMIT }
		},
		ebx_money_spending_v = {
			{ fn = self.on_ebx_money_spending_v_changed, id = ui_events.EDIT_TEXT_CHANGED },
			{ fn = self.on_ebx_money_spending_v_commit, id = ui_events.EDIT_TEXT_COMMIT }
		},
		ebx_money_remaining_v = {
			{ fn = self.on_ebx_money_remaining_v_changed, id = ui_events.EDIT_TEXT_CHANGED },
			{ fn = self.on_ebx_money_remaining_v_commit, id = ui_events.EDIT_TEXT_COMMIT }
		},
		btn_repair = {
			{ fn = self.on_btn_repair_clicked, id = ui_events.BUTTON_CLICKED }
		},
		btn_cancel = {
			{ fn = self.on_btn_cancel_clicked, id = ui_events.BUTTON_CLICKED }
		}
	} )

    logf( "uiRepairDialog:InitCallBacks() complete" )
end

function open( wnd )
	logf( "open() called")
	if wnd == nil then
		logf( "open() wnd == nil")
		return
	end
	holder = wnd:GetHolder()
	logf( "open() 1")
	if holder == nil then 
		logf( "open() 2")
	    holder = level
		logf( "open() 3")
	end
	logf( "open() 4")
	if not wnd.isOpen then
		logf( "open() 5")
		if holder.start_stop_menu ~= nil then
	    	holder.start_stop_menu( wnd, true )
		logf( "open() 6")
	    else
	    	level.start_stop_menu( wnd, true )
		logf( "open() 7")
	    end
		logf( "open() 8")
		wnd.IsOpen = true
	end
	logf( "open() complete")
end

function close( wnd )
	logf( "close() called")
	if wnd == nil then
		logf( "close() wnd == nil")
		return
	end
	holder = wnd:GetHolder()
	logf( "close() 1")
	if holder == nil then
		logf( "close() 2")
	    holder = level
		logf( "close() 3")
	end
	logf( "close() 4")
	if wnd.isOpen then
		logf( "close() 5")
		if holder.start_stop_menu ~= nil then
	    	holder.start_stop_menu( wnd, true )
		logf( "close() 6")
	    else
	    	level.start_stop_menu( wnd, true )
		logf( "close() 7")
	    end
		logf( "close() 8")
		wnd.IsOpen = false
	end
	logf( "open() complete")
end
function uiRepairDialog:RecalcMoney( budget )
	logf( "uiRepairDialog:RecalcMoney() called")
	uiRepairDialog:Recalc( 1, budget )
	logf( "uiRepairDialog:RecalcMoney() complete")
end

function uiRepairDialog:RecalcCondition( condition_target )
	logf( "uiRepairDialog:RecalcCondition() called")
	uiRepairDialog:Recalc( condition_target, nil )
	logf( "uiRepairDialog:RecalcCondition() complete")
end

function uiRepairDialog:Recalc( condition_target, budget )
	logf( "uiRepairDialog:Recalc() called")

	-- TODO: Factor in 'goodwill' towards db.actor

	local money = db.actor:money()

	if condition_target == nil or condition_target > 1 then
		condition_target = 1
	end

	self.valid_repair = true

	if budget == nil then
		budget = money
	elseif budget > money then
		budget = money
	end

	self.last_recalc_params = { condition_target, budget }

	local lst_repairable_items = self:GetListWnd( "lst_repairable_items" )
	local lst_repairable_items_index = list:GetSelectedItem()

	if lst_repairable_items_index == -1 then
		logf( "uiRepairDialog:Recalc() complete")
		return
	end

	local repairable_item = self.repairable_items[index]
	local chb_repair_rush = self:GetCheckButton( "chb_repair_rush" )
	local ebx_condition_current_v = self:GetEditBox( "ebx_condition_current_v" )
	local ebx_condition_improve_v = self:GetEditBox( "ebx_condition_improve_v" )
	local ebx_condition_result_v = self:GetEditBox( "ebx_condition_result_v" )
	local ebx_money_spending_v = self:GetEditBox( "ebx_money_spending_v" )
	local ebx_money_remaining_v = self:GetEditBox( "ebx_money_remaining_v" )
	
	if money <= 0 then
		self.valid_repair = false
		ebx_condition_current_v:SetTextColor( color_invalid )
	end


	local rush_state = chb_repair_rush:GetCheck()
	
	local condition = repairable_item:condition()

	local condition_value = math.floor( condition * 10000 ) / 100
	local condition_target_value

    local condition_amount, expense
	if condition > condition_target then
		condition_target = condition
		condition_target_value = condition_value
		condition_ammount = 0
		expense = 0
		self.valid_repair = false
		ebx_money_spending_v:SetTextColor( color_invalid )
		ebx_condition_improve_v:SetTextColor( color_invalid )
	else
		condition_target_value = math.floor( condition_target * 10000 ) / 100
		condition_amount, expense = calculate_repairable(
			self.cost_modifier, condition_value, condition_target, budget )
		ebx_money_spending_v:SetTextColor( color_valid )
		ebx_condition_improve_v:SetTextColor( color_valid )
	end


	local condition_amount_value = math.floor( condition_amount * 10000 ) / 100
	
	ebx_condition_current_v:SetText( condition_value )

	ebx_condition_result_v:SetText( condition_target_value )
	ebx_condition_improve_v:SetText( condition_amount_value )

	local balance = money - expense
    ebx_money_spending_v:SetText( expense )
    ebx_money_remaining_v:SetText( balance )

	if balance < 0 then
		self.valid_repair = false
		ebx_money_remaining_v:SetTextColor( color_invalid )
		else
		ebx_money_remaining_v:SetTextColor( color_valid )
	end

	ebx_condition_current_v:SetTextColor( condition_as_color( condition ) )
	ebx_condition_result_v:SetTextColor( condition_as_color( condition_target ) )

	logf( "uiRepairDialog:Recalc() complete")
end

function uiRepairDialog:on_btn_cancel_clicked()
	logf( "on_btn_cancel_clicked() called")
	close( self )
	logf( "on_btn_cancel_clicked() complete")
end

function uiRepairDialog:on_chb_repair_rush_set()
	logf( "on_chb_repair_rush_set() called")
	self:Recalc( unpack( self.last_calc_value ) )
	logf( "on_chb_repair_rush_set() complete")
end

function uiRepairDialog:on_chb_repair_rush_reset()
	logf( "on_chb_repair_rush_reset() called")
	self:Recalc( unpack( self.last_calc_value ) )
	logf( "on_chb_repair_rush_reset() complete")
end

function uiRepairDialog:on_ebx_condition_improve_v_changed()
	logf( "on_ebx_condition_improve_v_changed() called")
	local ebx_condition_improve_v = self:GetEditBox( "ebx_condition_improve_v" )
	self:RecalcCondition( tonumber( ebx_condition_improve_v:GetText() ) )
	logf( "on_ebx_condition_improve_v_changed() complete")
end

function uiRepairDialog:on_ebx_condition_improve_v_commit()
	logf( "on_ebx_condition_improve_v_commit() called")
	local ebx_condition_improve_v = self:GetEditBox( "ebx_condition_improve_v" )
	self:RecalcCondition( tonumber( ebx_condition_improve_v:GetText() ) )
	logf( "on_ebx_condition_improve_v_commit() complete")
end

function uiRepairDialog:on_ebx_condition_result_v_changed()
	logf( "on_ebx_condition_result_v_changed() called")
	local ebx_condition_result_v = self:GetEditBox( "ebx_condition_result_v" )
	self:RecalcCondition( tonumber( ebx_condition_result_v:GetText() ) )
	logf( "on_ebx_condition_result_v_changed() complete")
end

function uiRepairDialog:on_ebx_condition_result_v_commit()
	logf( "on_ebx_ebx_condition_result_v_commit() called")
	local ebx_condition_result_v = self:GetEditBox( "ebx_condition_result_v" )
	self:RecalcCondition( tonumber( ebx_condition_result_v:GetText() ) )
	logf( "on_ebx_ebx_condition_result_v_commit() complete")
end

function uiRepairDialog:on_ebx_money_spending_v_changed()
	logf( "on_ebx_money_spending_v_commit() called")
	local ebx_money_spending_v = self:GetEditBox( "ebx_money_spending_v" )
	self:RecalcMoney( tonumber( ebx_money_spending_v:GetText() ) )
	logf( "on_ebx_money_spending_v_commit() complete")
end

function uiRepairDialog:on_ebx_money_spending_v_commit()
	logf( "on_ebx_money_spending_v_commit() called")
	local ebx_money_spending_v = self:GetEditBox( "ebx_money_spending_v" )
	self:RecalcMoney( tonumber( ebx_money_spending_v:GetText() ) )
	logf( "on_ebx_money_spending_v_commit() complete")
end
function uiRepairDialog:on_ebx_money_remaining_v_changed()
	logf( "on_ebx_money_remaining_v_changed() called")
	local ebx_money_remaining_v = self:GetEditBox( "ebx_money_remaining_v" )
	self:RecalcMoney( tonumber( ebx_money_remaining_v:GetText() ) )
	logf( "on_ebx_money_remaining_v_changed() complete")
end

function uiRepairDialog:on_ebx_money_remaining_v_commit()
	logf( "on_ebx_money_remaining_v_commit() called")
	local ebx_money_remaining_v = self:GetEditBox( "ebx_money_remaining_v" )
	self:RecalcMoney( tonumber( ebx_money_remaining_v:GetText() ) )
	logf( "on_ebx_money_remaining_v_commit() complete")
end

function uiRepairDialog:on_btn_repair_clicked()
	logf( "on_btn_repair_clicked() called")
	local lst_repairable_items = self:GetListWnd( "lst_repairable_items" )
	local index = list:GetSelectedItem()
	if index == -1 then return end
	local item = self.repairable_items[index]

	local ebx_condition_result_v = self:GetEditBox( "ebx_condition_result_v" )
	local ebx_money_spending_v = self:GetEditBox( "ebx_money_spending_v" )

	-- db.actor:transfer_money( ebx_money_spending_v:GetText(), self.repairer )
	
	-- TODO: Hide weapon in inaccessible virtual stash with repair and date info
	
	-- the player will be able to retrieve his weapon by asking the repairer
	-- even if the repairs aren't complete, and still get a partial repair

	-- maybe partial to full refunds depending on goodwill and time spent repairing?

	close( self )
	logf( "on_btn_repair_clicked() complete")
end

-- virtual function (overloaded?)
function uiRepairDialog:OnKeyboard( dik, keyboard_action )
	CUIScriptWnd.OnKeyboard( self, dik, keyboard_action )

	-- translate DirectInputKey to bound function (in user.ltx)
	local bind = dik_to_bind(dik)
	if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
		logf("pressed dik=%d bind=%d", dik, bind )
	elseif keyboard_action == ui_events.WINDOW_KEY_RELEASED then
		logf("released dik=%d bind=%d", dik, bind )
	end

	if bind == key_bindings.kQUIT then
		close( self )
	end
	return true -- catch all keys.
end