Improve tab navigation and scrolling

* Fix with use with over creatures when you are over force use objects
* Fix textedit wrapping
master
Eduardo Bart 11 years ago
parent bc55cbbbd1
commit 5b65088426

@ -11,17 +11,42 @@ local function updateMargins(tabBar, ignored)
local currentMargin = 0 local currentMargin = 0
for i = 1, #tabBar.tabs do for i = 1, #tabBar.tabs do
if tabBar.tabs[i] ~= ignored then tabBar.tabs[i]:setMarginLeft(currentMargin)
if i == 1 then currentMargin = currentMargin + tabBar.tabSpacing + tabBar.tabs[i]:getWidth()
tabBar.tabs[i]:setMarginLeft(0) end
else end
tabBar.tabs[i]:setMarginLeft(tabBar.tabSpacing * (i - 1) + currentMargin)
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 end
currentMargin = currentMargin + tabBar.tabs[i]:getWidth() 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
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) local function onTabMousePress(tab, mousePos, mouseButton)
if mouseButton == MouseRightButton then if mouseButton == MouseRightButton then
if tab.menuCallback then tab.menuCallback(tab, mousePos, mouseButton) end if tab.menuCallback then tab.menuCallback(tab, mousePos, mouseButton) end
@ -29,8 +54,9 @@ local function onTabMousePress(tab, mousePos, mouseButton)
end end
end end
local function onTabDragEnter(tab) local function onTabDragEnter(tab, mousePos)
tab:raise() tab:raise()
tab.hotSpot = mousePos.x - tab:getMarginLeft()
tab.tabBar.selected = tab tab.tabBar.selected = tab
return true return true
end end
@ -43,29 +69,17 @@ end
local function onTabDragMove(tab, mousePos, mouseMoved) local function onTabDragMove(tab, mousePos, mouseMoved)
if tab == tab.tabBar.selected then if tab == tab.tabBar.selected then
local newMargin = tab:getMarginLeft() + mouseMoved.x local xoff = mousePos.x - tab.hotSpot
if newMargin >= -tab.tabBar.tabSpacing and newMargin < tab.tabBar:getWidth() - tab:getWidth() then
tab:setMarginLeft(newMargin)
end
local tabs = tab.tabBar.tabs -- update indexes
local lastMargin = -tab.tabBar.tabSpacing updateIndexes(tab.tabBar, tab, xoff)
for i = 1, #tabs do updateIndexes(tab.tabBar, tab, xoff)
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 -- update margins
if tabs[i] ~= tab then updateMargins(tab.tabBar)
local newIndex = table.find(tab.tabBar.tabs, tab.tabBar.tabs[i]) xoff = math.max(xoff, 0)
table.remove(tab.tabBar.tabs, table.find(tab.tabBar.tabs, tab)) xoff = math.min(xoff, getMaxMargin(tab.tabBar, tab))
table.insert(tab.tabBar.tabs, newIndex, tab) tab:setMarginLeft(xoff)
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
end end
end end
@ -81,7 +95,7 @@ function UIMoveableTabBar.create()
tabbar:setFocusable(false) tabbar:setFocusable(false)
tabbar.tabs = {} tabbar.tabs = {}
tabbar.selected = nil -- dragged tab tabbar.selected = nil -- dragged tab
tabbar.tabSpacing = 5 tabbar.tabSpacing = 0
tabbar.tabsMoveable = false tabbar.tabsMoveable = false
return tabbar return tabbar
end end

@ -82,18 +82,30 @@ local function updateSlider(self)
end end
end end
local function parseSliderPos(self, pos, move) local function parseSliderPos(self, slider, pos, move)
local point, delta local delta, hotDistance
if self.orientation == 'vertical' then if self.orientation == 'vertical' then
point = pos.y
delta = move.y delta = move.y
hotDistance = pos.y - slider:getY()
else else
point = pos.x
delta = move.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 end
local range, pxrange, px, offset, center = calcValues(self)
local newvalue = self.value + delta * (range / (pxrange - px))
self:setValue(newvalue)
end end
-- public functions -- public functions
@ -114,10 +126,11 @@ end
function UIScrollBar:onSetup() function UIScrollBar:onSetup()
self.setupDone = true 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('decrementButton'), function() self:decrement() end, 300)
g_mouse.bindAutoPress(self:getChildById('incrementButton'), function() self:increment() 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) updateSlider(self)
end end

@ -311,6 +311,7 @@ function onUseWith(clickedWidget, mousePosition)
if clickedWidget:getClassName() == 'UIMap' then if clickedWidget:getClassName() == 'UIMap' then
local tile = clickedWidget:getTile(mousePosition) local tile = clickedWidget:getTile(mousePosition)
if tile then if tile then
print('use on' .. tile:getTopMultiUseThing():getClassName())
g_game.useWith(selectedThing, tile:getTopMultiUseThing()) g_game.useWith(selectedThing, tile:getTopMultiUseThing())
end end
elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then

@ -449,15 +449,15 @@ ThingPtr Tile::getTopMultiUseThing()
if(isEmpty()) if(isEmpty())
return nullptr; return nullptr;
if(CreaturePtr topCreature = getTopCreature())
return topCreature;
for(uint i = 0; i < m_things.size(); ++i) { for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i]; ThingPtr thing = m_things[i];
if(thing->isForceUse()) if(thing->isForceUse())
return thing; return thing;
} }
if(CreaturePtr topCreature = getTopCreature())
return topCreature;
for(uint i = 0; i < m_things.size(); ++i) { for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i]; ThingPtr thing = m_things[i];
if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnBottom() && !thing->isOnTop()) { 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) { for(uint i = 0; i < m_things.size(); ++i) {
ThingPtr thing = m_things[i]; ThingPtr thing = m_things[i];
if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnTop()) if(!thing->isGround() && !thing->isGroundBorder() && !thing->isOnTop())

@ -480,7 +480,7 @@ std::string UITextEdit::cut()
void UITextEdit::wrapText() 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) void UITextEdit::moveCursorHorizontally(bool right)
@ -552,7 +552,7 @@ std::string UITextEdit::getDisplayedText()
text = m_text; text = m_text;
if(m_textWrap && m_rect.isValid()) 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; return text;
} }

Loading…
Cancel
Save