FrameworkZ 4.4.2
Provides a framework for Project Zomboid with various systems.
Loading...
Searching...
No Matches
CharacterView.lua
Go to the documentation of this file.
1PFW_CharacterView = ISPanel:derive("PFW_CharacterView")
3local FONT_HEIGHT_SMALL = getTextManager():getFontHeight(UIFont.Small)
4local FONT_HEIGHT_MEDIUM = getTextManager():getFontHeight(UIFont.Medium)
5local FONT_HEIGHT_LARGE = getTextManager():getFontHeight(UIFont.Large)
7function PFW_CharacterView:initialise()
8 ISPanel.initialise(self)
9
11 self:removeChild(self.characterNameLabel)
12 end
13
15 self:removeChild(self.characterPreview)
16 end
17
19 for k, v in pairs(self.descriptionLabels) do
20 self:removeChild(v)
21 end
22 end
25 self.uiHelper = FrameworkZ.UI
26 local descriptionLines = self:getDescriptionLines(self.description)
28 local isFemale = (self.character.INFO_GENDER == "Female" and true) or (self.character.INFO_GENDER == "Male" and false)
29 local x = self.uiHelper.GetMiddle(self.width, UIFont.Medium, self.name)
30 local y = 0
32 self.survivor = SurvivorFactory:CreateSurvivor(SurvivorType.Neutral, isFemale)
34
35 self.characterNameLabel = ISLabel:new(x, 0, FONT_HEIGHT_MEDIUM, self.name, 1, 1, 1, 1, UIFont.Medium, true)
36 self.characterNameLabel:initialise()
38
42 self.characterPreview = PFW_CharacterPreview:new(0, y, self.width, previewHeight, "EventIdle", self.defaultDirection)
44 self.characterPreview:removeChild(self.characterPreview.animCombo)
46 --self.characterPreview:setSurvivorDesc(self.survivor)
47 self:updateAppearance()
48 self:addChild(self.characterPreview)
50 y = y + previewHeight
51
52 for k, v in pairs(descriptionLines) do
53 x = self.uiHelper.GetMiddle(self.width, UIFont.Small, v)
54
55 local totalLines = #descriptionLines
56 local adjustedK = k - 1
57 local alphaStart = 1.0
58 local alphaMin = 0.2
59 local decayRate = 5
60 local alpha
62 if totalLines == 1 then
63 alpha = alphaStart -- Directly set to alphaStart if there's only one line
64 else
65 alpha = alphaMin + (alphaStart - alphaMin) * ((1 - adjustedK / (totalLines - 1)) ^ decayRate)
66 alpha = math.max(alpha, alphaMin)
67 end
68
69 local descriptionLabel = ISLabel:new(x, y, FONT_HEIGHT_SMALL, v, 1, 1, 1, alpha, UIFont.Small, true)
70 descriptionLabel:initialise()
71 self:addChild(descriptionLabel)
72
73 table.insert(self.descriptionLabels, descriptionLabel)
74
75 if k <= 3 then
76 y = y + descriptionLabel.height
77 else
78 -- For more than 3 lines, the loop breaks after adding "..." to the last displayed line
79 break
80 end
81 end
82end
83
84function PFW_CharacterView:render()
85 ISPanel.prerender(self)
86
87 -- Render the character preview and any other UI elements here
88end
89
90function PFW_CharacterView:updateAppearance()
91 local survivor = self.survivor
92 local character = self.character
93
94 for k, v in ipairs (FrameworkZ.Characters.EquipmentSlots) do
95 survivor:setWornItem(v, nil)
96 end
97
98 local headItem = character.EQUIPMENT_SLOT_HEAD and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_HEAD.id) or nil
99 local faceItem = character.EQUIPMENT_SLOT_FACE and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_FACE.id) or nil
100 local earsItem = character.EQUIPMENT_SLOT_EARS and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_EARS.id) or nil
101 local backpackItem = character.EQUIPMENT_SLOT_BACKPACK and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_BACKPACK.id) or nil
102 local glovesItem = character.EQUIPMENT_SLOT_GLOVES and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_GLOVES.id) or nil
103 local undershirtItem = character.EQUIPMENT_SLOT_UNDERSHIRT and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_UNDERSHIRT.id) or nil
104 local overshirtItem = character.EQUIPMENT_SLOT_OVERSHIRT and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_OVERSHIRT.id) or nil
105 local vestItem = character.EQUIPMENT_SLOT_VEST and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_VEST.id) or nil
106 local beltItem = character.EQUIPMENT_SLOT_BELT and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_BELT.id) or nil
107 local pantsItem = character.EQUIPMENT_SLOT_PANTS and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_PANTS.id) or nil
108 local socksItem = character.EQUIPMENT_SLOT_SOCKS and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_SOCKS.id) or nil
109 local shoesItem = character.EQUIPMENT_SLOT_SHOES and InventoryItemFactory.CreateItem(character.EQUIPMENT_SLOT_SHOES.id) or nil
110
111 survivor:getHumanVisual():setSkinTextureIndex(character.INFO_SKIN_COLOR)
112 survivor:getHumanVisual():setHairModel(character.INFO_HAIR_STYLE)
113 survivor:getHumanVisual():setBeardModel(character.INFO_BEARD_STYLE)
114
115 local immutableColor = ImmutableColor.new(character.INFO_HAIR_COLOR.r, character.INFO_HAIR_COLOR.g, character.INFO_HAIR_COLOR.b, 1)
116
117 survivor:getHumanVisual():setHairColor(immutableColor)
118 survivor:getHumanVisual():setBeardColor(immutableColor)
119 survivor:getHumanVisual():setNaturalHairColor(immutableColor)
120 survivor:getHumanVisual():setNaturalBeardColor(immutableColor)
121
122 if headItem then survivor:setWornItem(EQUIPMENT_SLOT_HEAD, headItem) end
123 if faceItem then survivor:setWornItem(EQUIPMENT_SLOT_FACE, faceItem) end
124 if earsItem then survivor:setWornItem(EQUIPMENT_SLOT_EARS, earsItem) end
125 if backpackItem then survivor:setWornItem(EQUIPMENT_SLOT_BACKPACK, backpackItem) end
126 if glovesItem then survivor:setWornItem(EQUIPMENT_SLOT_GLOVES, glovesItem) end
127 if undershirtItem then survivor:setWornItem(EQUIPMENT_SLOT_UNDERSHIRT, undershirtItem) end
128 if overshirtItem then survivor:setWornItem(EQUIPMENT_SLOT_OVERSHIRT, overshirtItem) end
129 if vestItem then survivor:setWornItem(EQUIPMENT_SLOT_VEST, vestItem) end
130 if beltItem then survivor:setWornItem(EQUIPMENT_SLOT_BELT, beltItem) end
131 if pantsItem then survivor:setWornItem(EQUIPMENT_SLOT_PANTS, pantsItem) end
132 if socksItem then survivor:setWornItem(EQUIPMENT_SLOT_SOCKS, socksItem) end
133 if shoesItem then survivor:setWornItem(EQUIPMENT_SLOT_SHOES, shoesItem) end
134
135 self.characterPreview:setSurvivorDesc(survivor)
136end
137
138function PFW_CharacterView:setCharacter(character)
139 self.character = character
140end
141
142function PFW_CharacterView:setName(name)
143 self.name = name
144end
145
146function PFW_CharacterView:setDescription(description)
147 self.description = description
148end
149
150function PFW_CharacterView:reinitialize(character)
151 self:setCharacter(character)
152 self:setName(character.INFO_NAME)
153 self:setDescription(character.INFO_DESCRIPTION)
154 self:initialise()
155end
156
157function PFW_CharacterView:getDescriptionLines(description)
158 local lines = {}
159 local line = ""
160 local lineLength = 0
161 local words = {}
162
163 for word in string.gmatch(description, "%S+") do
164 table.insert(words, word)
165 end
166
167 if #words == 0 then
168 return {description}
169 end
170
171 for i = 1, #words do
172 local word = words[i]
173 local wordLength = string.len(word) + 1
174
175 if lineLength + wordLength <= 30 or lineLength == 0 then
176 line = lineLength == 0 and word or line .. " " .. word
177 lineLength = lineLength + wordLength
178 else
179 table.insert(lines, line)
180 line = word
181 lineLength = wordLength
182 end
183 end
184
185 if line ~= "" then
186 table.insert(lines, line)
187 end
188
189 return lines
190end
191
192function PFW_CharacterView:new(x, y, width, height, isoPlayer, character, name, description, defaultDirection)
193 local o = {}
194
195 o = ISPanel:new(x, y, width, height)
196 setmetatable(o, self)
197 self.__index = self
198 o.backgroundColor = {r=0, g=0, b=0, a=0}
199 o.borderColor = {r=0, g=0, b=0, a=0}
200 o.moveWithMouse = false
201 o.isoPlayer = isoPlayer
202 o.character = character
203 o.name = name
204 o.description = description
205 o.defaultDirection = defaultDirection
206 PFW_CharacterView.instance = o
207
208 return o
209end
210
211return PFW_CharacterView
void local FONT_HEIGHT_MEDIUM()
void local y()
void alpha()
void local decayRate()
void local alphaStart()
void local FONT_HEIGHT_SMALL()
void local isFemale()
void self characterPreview()
void local totalLines()
void PFW_CharacterView()
void local alphaMin()
void local FONT_HEIGHT_LARGE()
void local descriptionHeight()
void self characterNameLabel()
void self survivor()
void self uiHelper()
void self descriptionLabels()
void local descriptionLabel()
void local adjustedK()
void local previewHeight()
void local x()
void local descriptionLines()
void name()
void self isoPlayer
void for i()
void if self initialFaction and v()
void local height()
void local width
void if not description or description()
void self self
Definition MainMenu.lua:85
void local character()
void local word()
void local wordLength()
FrameworkZ global table.
void getDescriptionLines(description)
void setCharacter(character)
void setDescription(description)
void setName(name)
void reinitialize(character)