Improve tab navigation and scrolling
* Fix with use with over creatures when you are over force use objects * Fix textedit wrapping
This commit is contained in:
parent
bc55cbbbd1
commit
5b65088426
|
@ -11,17 +11,42 @@ local function updateMargins(tabBar, ignored)
|
|||
|
||||
local currentMargin = 0
|
||||
for i = 1, #tabBar.tabs do
|
||||
if tabBar.tabs[i] ~= ignored then
|
||||
if i == 1 then
|
||||
tabBar.tabs[i]:setMarginLeft(0)
|
||||
else
|
||||
tabBar.tabs[i]:setMarginLeft(tabBar.tabSpacing * (i - 1) + currentMargin)
|
||||
end
|
||||
end
|
||||
currentMargin = currentMargin + tabBar.tabs[i]:getWidth()
|
||||
tabBar.tabs[i]:setMarginLeft(currentMargin)
|
||||
currentMargin = currentMargin + tabBar.tabSpacing + tabBar.tabs[i]:getWidth()
|
||||
end
|
||||
end
|
||||
|
||||
local function updateIndexes(tabBar, tab, xoff)
|
||||
local tabs = tabBar.tabs
|
||||
local currentMargin = 0
|
||||
local prevIndex = table.find(tabs, tab)
|
||||
local newIndex = prevIndex
|
||||
local xmid = xoff + tab:getWidth()/2
|
||||
for i = 1, #tabs do
|
||||
local nextTab = tabs[i]
|
||||
if xmid >= currentMargin + nextTab:getWidth()/2 then
|
||||
newIndex = table.find(tabs, nextTab)
|
||||
end
|
||||
currentMargin = currentMargin + tabBar.tabSpacing * (i - 1) + tabBar.tabs[i]:getWidth()
|
||||
end
|
||||
if newIndex ~= prevIndex then
|
||||
table.remove(tabs, table.find(tabs, tab))
|
||||
table.insert(tabs, newIndex, tab)
|
||||
end
|
||||
end
|
||||
|
||||
local function getMaxMargin(tabBar, tab)
|
||||
if #tabBar.tabs == 0 then return end
|
||||
|
||||
local maxMargin = 0
|
||||
for i = 1, #tabBar.tabs do
|
||||
if tabBar.tabs[i] ~= tab then
|
||||
maxMargin = maxMargin + tabBar.tabs[i]:getWidth()
|
||||
end
|
||||
end
|
||||
return maxMargin + tabBar.tabSpacing * (#tabBar.tabs- 1)
|
||||
end
|
||||
|
||||
local function onTabMousePress(tab, mousePos, mouseButton)
|
||||
if mouseButton == MouseRightButton then
|
||||
if tab.menuCallback then tab.menuCallback(tab, mousePos, mouseButton) end
|
||||
|
@ -29,8 +54,9 @@ local function onTabMousePress(tab, mousePos, mouseButton)
|
|||
end
|
||||
end
|
||||
|
||||
local function onTabDragEnter(tab)
|
||||
local function onTabDragEnter(tab, mousePos)
|
||||
tab:raise()
|
||||
tab.hotSpot = mousePos.x - tab:getMarginLeft()
|
||||
tab.tabBar.selected = tab
|
||||
return true
|
||||
end
|
||||
|
@ -43,29 +69,17 @@ end
|
|||
|
||||
local function onTabDragMove(tab, mousePos, mouseMoved)
|
||||
if tab == tab.tabBar.selected then
|
||||
local newMargin = tab:getMarginLeft() + mouseMoved.x
|
||||
if newMargin >= -tab.tabBar.tabSpacing and newMargin < tab.tabBar:getWidth() - tab:getWidth() then
|
||||
tab:setMarginLeft(newMargin)
|
||||
end
|
||||
local xoff = mousePos.x - tab.hotSpot
|
||||
|
||||
local tabs = tab.tabBar.tabs
|
||||
local lastMargin = -tab.tabBar.tabSpacing
|
||||
for i = 1, #tabs do
|
||||
local nextMargin = tabs[i + 1] and (tabs[i + 1] == tab and (tabs[i]:getMarginLeft() + tabs[i]:getWidth() + tab.tabBar.tabSpacing) or tabs[i + 1]:getMarginLeft()) or tab.tabBar:getWidth()
|
||||
if (tab:getMarginLeft()+(tabs[i]:getWidth()/2)) >= lastMargin and (tab:getMarginLeft()+(tabs[i]:getWidth()/2)) < nextMargin then
|
||||
if tabs[i] ~= tab then
|
||||
local newIndex = table.find(tab.tabBar.tabs, tab.tabBar.tabs[i])
|
||||
table.remove(tab.tabBar.tabs, table.find(tab.tabBar.tabs, tab))
|
||||
table.insert(tab.tabBar.tabs, newIndex, tab)
|
||||
updateMargins(tab.tabBar, tab)
|
||||
break
|
||||
else
|
||||
updateMargins(tab.tabBar, tab)
|
||||
break
|
||||
end
|
||||
end
|
||||
lastMargin = tab.tabBar.tabs[i]:getMarginLeft() == 0 and -tab.tabBar.tabSpacing or tab.tabBar.tabs[i]:getMarginLeft()
|
||||
end
|
||||
-- update indexes
|
||||
updateIndexes(tab.tabBar, tab, xoff)
|
||||
updateIndexes(tab.tabBar, tab, xoff)
|
||||
|
||||
-- update margins
|
||||
updateMargins(tab.tabBar)
|
||||
xoff = math.max(xoff, 0)
|
||||
xoff = math.min(xoff, getMaxMargin(tab.tabBar, tab))
|
||||
tab:setMarginLeft(xoff)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -81,7 +95,7 @@ function UIMoveableTabBar.create()
|
|||
tabbar:setFocusable(false)
|
||||
tabbar.tabs = {}
|
||||
tabbar.selected = nil -- dragged tab
|
||||
tabbar.tabSpacing = 5
|
||||
tabbar.tabSpacing = 0
|
||||
tabbar.tabsMoveable = false
|
||||
return tabbar
|
||||
end
|
||||
|
|
|
@ -82,18 +82,30 @@ local function updateSlider(self)
|
|||
end
|
||||
end
|
||||
|
||||
local function parseSliderPos(self, pos, move)
|
||||
local point, delta
|
||||
local function parseSliderPos(self, slider, pos, move)
|
||||
local delta, hotDistance
|
||||
if self.orientation == 'vertical' then
|
||||
point = pos.y
|
||||
delta = move.y
|
||||
hotDistance = pos.y - slider:getY()
|
||||
else
|
||||
point = pos.x
|
||||
delta = move.x
|
||||
hotDistance = pos.x - slider:getX()
|
||||
end
|
||||
|
||||
if (delta > 0 and hotDistance + delta > self.hotDistance) or
|
||||
(delta < 0 and hotDistance + delta < self.hotDistance) then
|
||||
local range, pxrange, px, offset, center = calcValues(self)
|
||||
local newvalue = self.value + delta * (range / (pxrange - px))
|
||||
self:setValue(newvalue)
|
||||
end
|
||||
end
|
||||
|
||||
local function parseSliderPress(self, slider, pos, button)
|
||||
if self.orientation == 'vertical' then
|
||||
self.hotDistance = pos.y - slider:getY()
|
||||
else
|
||||
self.hotDistance = pos.x - slider:getX()
|
||||
end
|
||||
local range, pxrange, px, offset, center = calcValues(self)
|
||||
local newvalue = self.value + delta * (range / (pxrange - px))
|
||||
self:setValue(newvalue)
|
||||
end
|
||||
|
||||
-- public functions
|
||||
|
@ -114,10 +126,11 @@ end
|
|||
|
||||
function UIScrollBar:onSetup()
|
||||
self.setupDone = true
|
||||
--signalcall(self.onValueChange, self, self.value)
|
||||
local sliderButton = self:getChildById('sliderButton')
|
||||
g_mouse.bindAutoPress(self:getChildById('decrementButton'), function() self:decrement() end, 300)
|
||||
g_mouse.bindAutoPress(self:getChildById('incrementButton'), function() self:increment() end, 300)
|
||||
g_mouse.bindPressMove(self:getChildById('sliderButton'), function(mousePos, mouseMoved) parseSliderPos(self, mousePos, mouseMoved) end)
|
||||
g_mouse.bindPressMove(sliderButton, function(mousePos, mouseMoved) parseSliderPos(self, sliderButton, mousePos, mouseMoved) end)
|
||||
g_mouse.bindPress(sliderButton, function(mousePos, mouseButton) parseSliderPress(self, sliderButton, mousePos, mouseButton) end)
|
||||
|
||||
updateSlider(self)
|
||||
end
|
||||
|
|
|
@ -311,6 +311,7 @@ function onUseWith(clickedWidget, mousePosition)
|
|||
if clickedWidget:getClassName() == 'UIMap' then
|
||||
local tile = clickedWidget:getTile(mousePosition)
|
||||
if tile then
|
||||
print('use on' .. tile:getTopMultiUseThing():getClassName())
|
||||
g_game.useWith(selectedThing, tile:getTopMultiUseThing())
|
||||
end
|
||||
elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then
|
||||
|
|
|
@ -449,15 +449,15 @@ ThingPtr Tile::getTopMultiUseThing()
|
|||
if(isEmpty())
|
||||
return nullptr;
|
||||
|
||||
if(CreaturePtr topCreature = getTopCreature())
|
||||
return topCreature;
|
||||
|
||||
for(uint i = 0; i < m_things.size(); ++i) {
|
||||
ThingPtr thing = m_things[i];
|
||||
if(thing->isForceUse())
|
||||
return thing;
|
||||
}
|
||||
|
||||
if(CreaturePtr topCreature = getTopCreature())
|
||||
return topCreature;
|
||||
|
||||
for(uint i = 0; i < m_things.size(); ++i) {
|
||||
ThingPtr thing = m_things[i];
|
||||
if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop()) {
|
||||
|
@ -467,7 +467,6 @@ ThingPtr Tile::getTopMultiUseThing()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
for(uint i = 0; i < m_things.size(); ++i) {
|
||||
ThingPtr thing = m_things[i];
|
||||
if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnTop())
|
||||
|
|
|
@ -480,7 +480,7 @@ std::string UITextEdit::cut()
|
|||
|
||||
void UITextEdit::wrapText()
|
||||
{
|
||||
setText(m_font->wrapText(m_text, getPaddingRect().width()));
|
||||
setText(m_font->wrapText(m_text, getPaddingRect().width() - m_textOffset.x));
|
||||
}
|
||||
|
||||
void UITextEdit::moveCursorHorizontally(bool right)
|
||||
|
@ -552,7 +552,7 @@ std::string UITextEdit::getDisplayedText()
|
|||
text = m_text;
|
||||
|
||||
if(m_textWrap && m_rect.isValid())
|
||||
text = m_font->wrapText(text, getWidth() - m_textOffset.x);
|
||||
text = m_font->wrapText(text, getPaddingRect().width() - m_textOffset.x);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue