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].height = 1
self.slots[i].width = 1
self.slots[i].isChild = 0
self.slots[i].parent = 0
self.slots[i].widgetID = i
end
end
@ -42,10 +42,10 @@ function GridBox:getWidgetSize(id)
end
function GridBox:getWidgetID(slot)
if (self.slots[slot].isChild == 0) then
if (self.slots[slot].parent == 0) then
return self.slots[slot].widgetID
else
return self.slots[self.slots[slot].isChild].widgetID
return self.slots[self.slots[slot].parent].widgetID
end
end
@ -68,8 +68,8 @@ end
function GridBox:update(dt)
self.view.firstSlot = 1
local slotID = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
if self.slots[slotID].isChild > 0 then
slotID = self.slots[slotID].isChild
if self.slots[slotID].parent > 0 then
slotID = self.slots[slotID].parent
end
self.widget.selected = self.slots[slotID].widgetID
self.cursor.x, self.cursor.y = self:getCoord(slotID)
@ -78,7 +78,7 @@ end
function GridBox:regenSlots()
local widgetID = 1
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
widgetID = widgetID + 1
end
@ -90,7 +90,7 @@ function GridBox:addCol(slotID)
if (col + self.slots[slotID].width + 1) <= self.view.colNumber then
slotChild = slotID + self.slots[slotID].width
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
self.slots[slotID].width = self.slots[slotID].width + 1
end
@ -101,7 +101,7 @@ function GridBox:addLine(slotID)
if (line + self.slots[slotID].height + 1) <= self.view.colNumber then
slotChild = slotID + (self.slots[slotID].height * self.view.colNumber)
for i = 1, self.slots[slotID].width do
self.slots[slotChild + (i-1)].isChild = slotID
self.slots[slotChild + (i-1)].parent = slotID
end
self.slots[slotID].height = self.slots[slotID].height + 1
end
@ -131,35 +131,60 @@ end
function GridBox:moveCursor(newcol, newline)
local col, line = self.cursor.x, self.cursor.y
local relcol, relline = newcol - col, newline - line
self.cursor.x, self.cursor.y = newcol, newline
self:moveCursorRelative(newcol - self.cursor.x, newline - self.cursor.y)
end
while self.cursor.y < 0 do
self.cursor.y = self.cursor.y + self.view.colNumber
end
function GridBox:moveCursorRelative(dx, dy)
while self.cursor.x < 0 do
self.cursor.x = self.cursor.x + self.view.colNumber
end
while self.cursor.y >= self.view.colNumber do
self.cursor.y = self.cursor.y - self.view.colNumber
end
while self.cursor.x >= self.view.colNumber do
self.cursor.x = self.cursor.x - self.view.colNumber
end
previousSlot = self:getSlotbyCoord(col, line)
newSlot = self:getSlotbyCoord(self.cursor.x, self.cursor.y)
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)
local i = 0
self.cursor.x = self.cursor.x + dx
self.cursor.y = self.cursor.y + dy
self:fixCursorOverflow()
print(self.cursor.x, self.cursor.y, self:haveValidCursorPosition())
while not(self:haveValidCursorPosition()) do
self.cursor.x = self.cursor.x + utils.math.sign(dx)
self.cursor.y = self.cursor.y + utils.math.sign(dy)
self:fixCursorOverflow()
print(self.cursor.x, self.cursor.y, self:haveValidCursorPosition())
i = i + 1
if i == 200 then
break
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)
slotID = self:getSlot(self.widget.selected)
local col, line = self.cursor.x, self.cursor.y
@ -225,8 +250,8 @@ function GridBox:mousepressed(x, y, button, isTouch)
self:getFocus()
if self.slots[newselect].isChild > 0 then
slotID = self.slots[newselect].isChild
if self.slots[newselect].parent > 0 then
slotID = self.slots[newselect].parent
else
slotID = newselect
end
@ -244,7 +269,7 @@ function GridBox:draw()
self:regenSlots() -- On reget les slots au cas où :p
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)
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)
@ -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)
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.rectangle("fill", widgetx, widgety, self.widget.w, self.widget.h)
end