core/menusystem: initial work on cursor moving

This commit is contained in:
Kazhnuz 2019-02-13 22:11:04 +01:00
parent 92bea8c2cd
commit fed4f6f598

View file

@ -26,7 +26,7 @@ function GridBox:new(menusystem, name, x, y, w, h, colNumber, lineNumber)
self.slots[i] = {} self.slots[i] = {}
self.slots[i].height = 1 self.slots[i].height = 1
self.slots[i].width = 1 self.slots[i].width = 1
self.slots[i].isChild = 0 self.slots[i].parent = 0
self.slots[i].widgetID = i self.slots[i].widgetID = i
end end
end end
@ -42,10 +42,10 @@ function GridBox:getWidgetSize(id)
end end
function GridBox:getWidgetID(slot) function GridBox:getWidgetID(slot)
if (self.slots[slot].isChild == 0) then if (self.slots[slot].parent == 0) then
return self.slots[slot].widgetID return self.slots[slot].widgetID
else else
return self.slots[self.slots[slot].isChild].widgetID return self.slots[self.slots[slot].parent].widgetID
end end
end end
@ -68,8 +68,8 @@ end
function GridBox:update(dt) function GridBox:update(dt)
self.view.firstSlot = 1 self.view.firstSlot = 1
local slotID = self:getSlotbyCoord(self.cursor.x, self.cursor.y) local slotID = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
if self.slots[slotID].isChild > 0 then if self.slots[slotID].parent > 0 then
slotID = self.slots[slotID].isChild slotID = self.slots[slotID].parent
end end
self.widget.selected = self.slots[slotID].widgetID self.widget.selected = self.slots[slotID].widgetID
self.cursor.x, self.cursor.y = self:getCoord(slotID) self.cursor.x, self.cursor.y = self:getCoord(slotID)
@ -78,7 +78,7 @@ end
function GridBox:regenSlots() function GridBox:regenSlots()
local widgetID = 1 local widgetID = 1
for i,v in ipairs(self.slots) do for i,v in ipairs(self.slots) do
if v.isChild == 0 and (widgetID <= #self.widget.list) then if v.parent == 0 and (widgetID <= #self.widget.list) then
self.slots[i].widgetID = widgetID self.slots[i].widgetID = widgetID
widgetID = widgetID + 1 widgetID = widgetID + 1
end end
@ -90,7 +90,7 @@ function GridBox:addCol(slotID)
if (col + self.slots[slotID].width + 1) <= self.view.colNumber then if (col + self.slots[slotID].width + 1) <= self.view.colNumber then
slotChild = slotID + self.slots[slotID].width slotChild = slotID + self.slots[slotID].width
for i = 1, self.slots[slotID].height do for i = 1, self.slots[slotID].height do
self.slots[slotChild + ((i-1)* self.view.colNumber)].isChild = slotID self.slots[slotChild + ((i-1)* self.view.colNumber)].parent = slotID
end end
self.slots[slotID].width = self.slots[slotID].width + 1 self.slots[slotID].width = self.slots[slotID].width + 1
end end
@ -101,7 +101,7 @@ function GridBox:addLine(slotID)
if (line + self.slots[slotID].height + 1) <= self.view.colNumber then if (line + self.slots[slotID].height + 1) <= self.view.colNumber then
slotChild = slotID + (self.slots[slotID].height * self.view.colNumber) slotChild = slotID + (self.slots[slotID].height * self.view.colNumber)
for i = 1, self.slots[slotID].width do for i = 1, self.slots[slotID].width do
self.slots[slotChild + (i-1)].isChild = slotID self.slots[slotChild + (i-1)].parent = slotID
end end
self.slots[slotID].height = self.slots[slotID].height + 1 self.slots[slotID].height = self.slots[slotID].height + 1
end end
@ -131,35 +131,60 @@ end
function GridBox:moveCursor(newcol, newline) function GridBox:moveCursor(newcol, newline)
local col, line = self.cursor.x, self.cursor.y local col, line = self.cursor.x, self.cursor.y
local relcol, relline = newcol - col, newline - line self:moveCursorRelative(newcol - self.cursor.x, newline - self.cursor.y)
self.cursor.x, self.cursor.y = newcol, newline end
while self.cursor.y < 0 do function GridBox:moveCursorRelative(dx, dy)
self.cursor.y = self.cursor.y + self.view.colNumber
end
while self.cursor.x < 0 do local i = 0
self.cursor.x = self.cursor.x + self.view.colNumber self.cursor.x = self.cursor.x + dx
end self.cursor.y = self.cursor.y + dy
self:fixCursorOverflow()
while self.cursor.y >= self.view.colNumber do print(self.cursor.x, self.cursor.y, self:haveValidCursorPosition())
self.cursor.y = self.cursor.y - self.view.colNumber while not(self:haveValidCursorPosition()) do
end self.cursor.x = self.cursor.x + utils.math.sign(dx)
self.cursor.y = self.cursor.y + utils.math.sign(dy)
while self.cursor.x >= self.view.colNumber do self:fixCursorOverflow()
self.cursor.x = self.cursor.x - self.view.colNumber print(self.cursor.x, self.cursor.y, self:haveValidCursorPosition())
end i = i + 1
previousSlot = self:getSlotbyCoord(col, line) if i == 200 then
newSlot = self:getSlotbyCoord(self.cursor.x, self.cursor.y) break
if (self.slots[newSlot].isChild > 0) or (self.slots[newSlot].widgetID > #self.widget.list) then
if (self.slots[newSlot].isChild == previousSlot) or (self.slots[newSlot].widgetID > #self.widget.list) then
self:moveCursor(self.cursor.x + relcol, self.cursor.y + relline)
end end
end end
end end
function GridBox:haveValidCursorPosition()
local newSlot = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
local isValid = true
if (self.slots[newSlot].parent > 0) or (self.slots[newSlot].widgetID > #self.widget.list) then
if (self.slots[newSlot].parent == previousSlot) or (self.slots[newSlot].widgetID > #self.widget.list) then
isValid = false
end
end
return isValid
end
function GridBox:fixCursorOverflow()
if self.cursor.x < 0 then
self.cursor.x = self.view.colNumber - 1
end
if self.cursor.x > self.view.colNumber - 1 then
self.cursor.x = 0
end
if self.cursor.y < 0 then
self.cursor.y = self.view.lineNumber - 1
end
if self.cursor.y > self.view.lineNumber - 1 then
self.cursor.y = 0
end
end
function GridBox:keyreleased(key, code) function GridBox:keyreleased(key, code)
slotID = self:getSlot(self.widget.selected) slotID = self:getSlot(self.widget.selected)
local col, line = self.cursor.x, self.cursor.y local col, line = self.cursor.x, self.cursor.y
@ -225,8 +250,8 @@ function GridBox:mousepressed(x, y, button, isTouch)
self:getFocus() self:getFocus()
if self.slots[newselect].isChild > 0 then if self.slots[newselect].parent > 0 then
slotID = self.slots[newselect].isChild slotID = self.slots[newselect].parent
else else
slotID = newselect slotID = newselect
end end
@ -244,7 +269,7 @@ function GridBox:draw()
self:regenSlots() -- On reget les slots au cas où :p self:regenSlots() -- On reget les slots au cas où :p
for i,v in ipairs(self.slots) do for i,v in ipairs(self.slots) do
if (v.isChild == 0) and (v.widgetID <= #self.widget.list) then if (v.parent == 0) and (v.widgetID <= #self.widget.list) then
--self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height) --self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height)
if self.widget.selected == v.widgetID and self:haveFocus() == true then if self.widget.selected == v.widgetID and self:haveFocus() == true then
self.widget.list[v.widgetID]:drawSelected(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height) self.widget.list[v.widgetID]:drawSelected(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height)
@ -252,7 +277,7 @@ function GridBox:draw()
self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height) self.widget.list[v.widgetID]:draw(widgetx, widgety, self.widget.w * v.width, self.widget.h * v.height)
end end
end end
if (v.isChild > 0) and false then if (v.parent > 0) and false then
love.graphics.setColor(255,255,255,128) love.graphics.setColor(255,255,255,128)
love.graphics.rectangle("fill", widgetx, widgety, self.widget.w, self.widget.h) love.graphics.rectangle("fill", widgetx, widgety, self.widget.w, self.widget.h)
end end