local UIAnim = require "widgets/uianim"
local Widget = require "widgets/widget"
local Text = require "widgets/text"

local shaqi = Class(Widget, function(self, owner)
	Widget._ctor(self, "shaqi")
	self.owner = owner

    self:SetPosition(0,0,0)


    self.point = 0
    

    self.anim = self:AddChild(UIAnim())
	self.anim:GetAnimState():SetBank("hunger")
	self.anim:GetAnimState():SetBuild("shaqi")
	--self.anim:SetScale(0.14,0.14,0.14)	
	
    self.num = self:AddChild(Text(BODYTEXTFONT, 26))
    self.num:SetHAlign(ANCHOR_MIDDLE)
    self.num:SetPosition(0, -45, 0)

    self.num.shat1 = self:AddChild(Text(BODYTEXTFONT, 26))
    self.num.shat1:SetHAlign(ANCHOR_MIDDLE)
    self.num.shat1:SetPosition(-170, 0, 0)
	
	    self.num.shat1:SetString( tostring(
		"\n煞气高于160"..
		"\n进入煞气状态"..
		"\n煞气低于40"..
		"\n恢复正常"))
		self.num.shat1:SetColour(0, 0.5, 0, 1)
		self.num.shat1:Hide()	


		
	

	self:StartUpdating()
end)

function shaqi:OnUpdate(dt)
	local newnum = self.owner.components.shaqi.current
	--local oldnum = self.point
	self.shakeipercent = (200 - newnum) / 200
	self.point = newnum
	--if newnum ~= oldnum then
		--self.anim:GetAnimState():SetPercent("anim", newnum/200 * .99)
		self.anim:GetAnimState():SetPercent("anim", self.shakeipercent)
	--end

	self.num:SetString( tostring( math.ceil(newnum) ).."\0" )
	
		--设置鼠标移动到上面时的状态
		self.anim.OnGainFocus = function()
			self:OnGainFocus(self)
			self.num.shat1:Show()
		end
		self.anim.OnLoseFocus = function()
			self:OnLoseFocus(self)
			self.num.shat1:Hide()	
		end
	
end


function shaqi:OnGainFocus()
    shaqi._base.OnGainFocus(self)
end

function shaqi:OnLoseFocus()
    shaqi._base.OnLoseFocus(self)
end


return shaqi