Jump to content

Round Robin


ShayF2

Recommended Posts

Posted

Hello. I'm a scripter for MTA:SA, many of you have seen me on the forums, posting helpful things on your threads. Today I need some help. This code below is code to create dx gridlists. I use arrow keys to control what I select. However when the gridlist can show 5 items at a time and I only put 3 in, it seems to overlap, thinking that there is an extra index. So long as the gridlist has at least 5 items it'll work perfectly fine. Could someone please help me fix this round robin effect?
 

function dxCreateGridlist(x,y,w,h)
    local self = dx('gridlist','default',x,y,w,h)
    if self then
        self.itemHeight = 25
        self.maxItems = math.floor(self.h/self.itemHeight)
        self.bgColor = tocolor(0,0,0,180)
        self.selectedColor = tocolor(80,80,80,180)
        self.textColor = tocolor(255,255,255,200)
        self.startPos = 1
        self.endPos = 20
        self.currentItem = 1
        self.itemSpacing = 2
        self.selected = 1
        self.items = {}
       
        self.draw = function()
            self.endPos = self.startPos+self.maxItems
            local yOff = 0
            for i=self.startPos,self.endPos do
                if self.items[i] then
                    if i == self.selected then
                        dxRect(self.x,self.y+yOff,self.w,self.itemHeight-self.itemSpacing,self.selectedColor)
                        dxText(self.items[i].text,self.x,self.y+yOff,self.w,self.itemHeight-self.itemSpacing,self.textColor)
                    else
                        dxRect(self.x,self.y+yOff,self.w,self.itemHeight-self.itemSpacing,self.bgColor)
                        dxText(self.items[i].text,self.x,self.y+yOff,self.w,self.itemHeight-self.itemSpacing,self.textColor)
                    end
                    yOff = yOff+((self.itemHeight))+self.itemSpacing
                end
            end
        end
       
        self.addItem = function(text)
            local item = {}
            item.text = text
            table.insert(self.items,item)
        end
       
        self.itemsClear = function()
            for k=1,#self.items do
                table.remove(self.items,k)
            end
        end
       
        self.onKey = function(key,state)
            if state then
                if key == 'num_8' then
                    if self.selected < self.startPos then
                        if self.selected <= 1 then
                            self.startPos = #self.items-self.maxItems
                            self.selected = #self.items
                        else
                            self.startPos = self.startPos-1
                        end
                    else
                        self.selected = self.selected-1
                    end
                elseif key == 'num_2' then
                    if self.selected > self.endPos then
                        if self.selected >= #self.items then
                            self.selected = 1
                            self.startPos = 1
                        else
                            self.startPos = self.startPos+1
                        end
                    else
                        self.selected = self.selected+1
                    end
                elseif key == 'num_enter' then
                    outputChatBox(self.items[self.selected].text)
                end
            end
        end
       
        table.insert(draw,self)
        return self
    end
end

 

  • Moderators
Posted (edited)

Please give me some context about how it overlaps, it makes it easier for me to find the problem.

 

self.endPos = self.startPos+self.maxItems

Also this should be limited to the total items in the gridlist for performance reasons. (Unless you do add a grid)

 

Debug the yOff variable for every item, just to be sure that the problem is here.(and show result)

 

 

 

Edited by IIYAMA

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

I think I figured out the issue.

Say I have 3 items in the gridlist and the maxItems is calculated to be 5. The start position would change to -2 when it's range is only 1 to 3. This is because it does the math self.startPos = #self.items-self.maxItems.

I'm not totally sure that fixing this will fix the problem, I will test it when I have the time. Today I don't really have the time.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...