4444
4545function RowListClass :BuildColumns ()
4646 wipeTable (self .colList )
47- self .colList [1 ] = { width = 50 , label = " #" , font = " FIXED" }
47+ self .colList [1 ] = { width = 50 , label = " #" , font = " FIXED" , sortable = true }
4848 for _ , specCol in ipairs (main .curDatFile .spec ) do
49- t_insert (self .colList , { width = specCol .width , label = specCol .name , font = function () return IsKeyDown (" CTRL" ) and " FIXED" or " VAR" end })
49+ t_insert (self .colList , {
50+ width = specCol .width ,
51+ label = specCol .name ,
52+ font = function () return IsKeyDown (" CTRL" ) and " FIXED" or " VAR" end ,
53+ sortable = true -- or false, as needed
54+ })
5055 end
5156 local short = main .curDatFile .rowSize - main .curDatFile .specSize
5257 if short > 0 then
53- t_insert (self .colList , { width = short * DrawStringWidth (self .rowHeight , " FIXED" , " 00 " ), font = " FIXED" })
58+ t_insert (self .colList , { width = short * DrawStringWidth (self .rowHeight , " FIXED" , " 00 " ), font = " FIXED" , sortable = true })
5459 end
5560end
5661
@@ -76,3 +81,158 @@ function RowListClass:GetRowValue(column, index, row)
7681 end
7782 end
7883end
84+
85+ function RowListClass :Draw (viewPort )
86+ local x , y = self :GetPos ()
87+ local width , height = self :GetSize ()
88+ local rowHeight = self .rowHeight
89+ local list = self .list
90+
91+ local colOffset = 0
92+ for index , column in ipairs (self .colList ) do
93+ column ._offset = colOffset
94+ column ._width = self :GetColumnProperty (column , " width" ) or (index == # self .colList and self .scroll and width - 20 or width - colOffset ) or 0
95+ colOffset = colOffset + column ._width
96+ end
97+
98+ local scrollBarV = self .controls .scrollBarV
99+ local rowRegion = self :GetRowRegion ()
100+ scrollBarV :SetContentDimension (# list * rowHeight , rowRegion .height )
101+ local scrollOffsetV = scrollBarV .offset
102+ local scrollBarH = self .controls .scrollBarH
103+ local lastCol = self .colList [# self .colList ]
104+ scrollBarH :SetContentDimension (lastCol ._offset + lastCol ._width , rowRegion .width )
105+ local scrollOffsetH = scrollBarH .offset
106+
107+ local cursorX , cursorY = GetCursorPos ()
108+
109+ local label = self :GetProperty (" label" )
110+ if label then
111+ DrawString (x + self .labelPositionOffset [1 ], y - 20 + self .labelPositionOffset [2 ], " LEFT" , 16 , self .font , label )
112+ end
113+ if self .hasFocus then
114+ SetDrawColor (1 , 1 , 1 )
115+ else
116+ SetDrawColor (0.5 , 0.5 , 0.5 )
117+ end
118+ DrawImage (nil , x , y , width , height )
119+ SetDrawColor (0 , 0 , 0 )
120+ DrawImage (nil , x + 1 , y + 1 , width - 2 , height - 2 )
121+ self :DrawControls (viewPort )
122+
123+ SetViewport (x + 2 , y + 2 , self .scroll and width - 20 or width , height - 4 - (self .scroll and self .scrollH and 16 or 0 ))
124+ local textOffsetY = self .showRowSeparators and 2 or 0
125+ local textHeight = rowHeight - textOffsetY * 2
126+ local minIndex = math.floor (scrollOffsetV / rowHeight + 1 )
127+ local maxIndex = math.min (math.floor ((scrollOffsetV + height ) / rowHeight + 1 ), # list )
128+ for colIndex , column in ipairs (self .colList ) do
129+ local colFont = self :GetColumnProperty (column , " font" ) or " VAR"
130+ local clipWidth = DrawStringWidth (textHeight , colFont , " ..." )
131+ colOffset = column ._offset - scrollOffsetH
132+ local colWidth = column ._width
133+ local relX = cursorX - (x + 2 )
134+ local relY = cursorY - (y + 2 )
135+ for index = minIndex , maxIndex do
136+ local lineY = rowHeight * (index - 1 ) - scrollOffsetV + (self .colLabels and 18 or 0 )
137+ local value = list [index ]
138+ local text = self :GetRowValue (colIndex , index , value )
139+ local textWidth = DrawStringWidth (textHeight , colFont , text )
140+ if textWidth > colWidth - 2 then
141+ local clipIndex = DrawStringCursorIndex (textHeight , colFont , text , colWidth - clipWidth - 2 , 0 )
142+ text = text :sub (1 , clipIndex - 1 ) .. " ..."
143+ textWidth = DrawStringWidth (textHeight , colFont , text )
144+ end
145+ if self .showRowSeparators then
146+ if self .hasFocus and value == self .selValue then
147+ SetDrawColor (1 , 1 , 1 )
148+ else
149+ SetDrawColor (0.5 , 0.5 , 0.5 )
150+ end
151+ DrawImage (nil , colOffset , lineY , not self .scroll and colWidth - 4 or colWidth , rowHeight )
152+ if index % 2 == 0 then
153+ SetDrawColor (0.05 , 0.05 , 0.05 )
154+ else
155+ SetDrawColor (0 , 0 , 0 )
156+ end
157+ DrawImage (nil , colOffset , lineY + 1 , not self .scroll and colWidth - 4 or colWidth , rowHeight - 2 )
158+ elseif value == self .selValue then
159+ if self .hasFocus and value == self .selValue then
160+ SetDrawColor (1 , 1 , 1 )
161+ else
162+ SetDrawColor (0.5 , 0.5 , 0.5 )
163+ end
164+ DrawImage (nil , colOffset , lineY , not self .scroll and colWidth - 4 or colWidth , rowHeight )
165+ SetDrawColor (0.15 , 0.15 , 0.15 )
166+ DrawImage (nil , colOffset , lineY + 1 , not self .scroll and colWidth - 4 or colWidth , rowHeight - 2 )
167+ end
168+ if not self .SetHighlightColor or not self :SetHighlightColor (index , value ) then
169+ SetDrawColor (1 , 1 , 1 )
170+ end
171+ DrawString (colOffset , lineY + textOffsetY , " LEFT" , textHeight , colFont , text )
172+ end
173+ if self .colLabels then
174+ local mOver = relX >= colOffset and relX <= colOffset + colWidth and relY >= 0 and relY <= 18
175+
176+ local isSelected = (colIndex - 1 ) == main .curSpecColIndex
177+ local outerColor
178+ if mOver then
179+ outerColor = {1 , 1 , 1 }
180+ elseif isSelected then
181+ outerColor = {1 , 0.3 , 0.2 }
182+ else
183+ outerColor = {0.5 , 0.5 , 0.5 }
184+ end
185+ local innerColor = isSelected and {0.6 , 0.25 , 0.2 } or (mOver and self :GetColumnProperty (column , " sortable" ) and {0.33 , 0.33 , 0.33 } or {0.15 , 0.15 , 0.15 })
186+
187+ SetDrawColor (unpack (outerColor ))
188+ DrawImage (nil , colOffset , 1 , colWidth , 18 )
189+ SetDrawColor (unpack (innerColor ))
190+ DrawImage (nil , colOffset + 1 , 2 , colWidth - 2 , 16 )
191+
192+ local label = self :GetColumnProperty (column , " label" )
193+ if label and # label > 0 then
194+ SetDrawColor (1 , 1 , 1 )
195+ DrawString (colOffset + colWidth / 2 , 4 , " CENTER_X" , 12 , " VAR" , label )
196+ end
197+ end
198+ end
199+ if # self .list == 0 and self .defaultText then
200+ SetDrawColor (1 , 1 , 1 )
201+ DrawString (2 , 2 , " LEFT" , 14 , self .font , self .defaultText )
202+ end
203+ SetViewport ()
204+ end
205+
206+ function RowListClass :ReSort (colIndex )
207+ local asc = true
208+ if self .lastSortedCol == colIndex then
209+ asc = not self .sortAsc
210+ end
211+
212+ table.sort (self .list , function (a , b )
213+ local valA = self :GetRowValue (colIndex , nil , a )
214+ local valB = self :GetRowValue (colIndex , nil , b )
215+
216+ local numA = tonumber (valA )
217+ local numB = tonumber (valB )
218+
219+ if numA and numB then
220+ if asc then
221+ return numA < numB
222+ else
223+ return numA > numB
224+ end
225+ else
226+ valA = tostring (valA or " " )
227+ valB = tostring (valB or " " )
228+ if asc then
229+ return valA < valB
230+ else
231+ return valA > valB
232+ end
233+ end
234+ end )
235+
236+ self .lastSortedCol = colIndex
237+ self .sortAsc = asc
238+ end
0 commit comments