mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 11:34:54 +02:00
scrollbar, options and widgets changes
* complete scrollbar skin * implement scrollbar functionality (scrolling with mouse) * fix onMouseClick issues * add tabs in options (graphics and general tab) * add new option for limiting frame rate using scrollbar * add new widget property "clipping" that will be used on scrollable areas
This commit is contained in:
@@ -41,4 +41,5 @@ Module
|
||||
dofile 'widgets/uimessagebox'
|
||||
dofile 'widgets/uisplitter'
|
||||
dofile 'widgets/uiscrollbar'
|
||||
dofile 'widgets/uiscrollarea'
|
||||
|
||||
|
@@ -4,7 +4,10 @@ importStyle = g_ui.importStyle
|
||||
importFont = g_fonts.importFont
|
||||
setDefaultFont = g_fonts.setDefaultFont
|
||||
|
||||
loadUI = g_ui.loadUI
|
||||
function loadUI(otui, parent)
|
||||
local otuiFilePath = resolvepath(otui, 2)
|
||||
return g_ui.loadUI(otuiFilePath, parent)
|
||||
end
|
||||
|
||||
function displayUI(otui, parent)
|
||||
parent = parent or rootWidget
|
||||
@@ -17,7 +20,6 @@ function createWidget(stylename, parent)
|
||||
parent = rootWidget:recursiveGetChildById(parent)
|
||||
end
|
||||
local widget = g_ui.createWidgetFromStyle(stylename, parent)
|
||||
--widget:setStyle(stylename)
|
||||
return widget
|
||||
end
|
||||
|
||||
@@ -36,6 +38,26 @@ function addEvent(callback, front)
|
||||
return event
|
||||
end
|
||||
|
||||
|
||||
function periodicalEvent(eventFunc, conditionFunc, delay, autoRepeatDelay)
|
||||
delay = delay or 30
|
||||
autoRepeatDelay = autoRepeatDelay or delay
|
||||
|
||||
local func
|
||||
func = function()
|
||||
if conditionFunc and not conditionFunc() then
|
||||
func = nil
|
||||
return
|
||||
end
|
||||
eventFunc()
|
||||
scheduleEvent(func, delay)
|
||||
end
|
||||
|
||||
scheduleEvent(function()
|
||||
func()
|
||||
end, autoRepeatDelay)
|
||||
end
|
||||
|
||||
function removeEvent(event)
|
||||
if event then
|
||||
event:cancel()
|
||||
|
@@ -15,3 +15,24 @@ end
|
||||
function Mouse.restoreCursor()
|
||||
g_window.restoreMouseCursor()
|
||||
end
|
||||
|
||||
function Mouse.bindAutoPress(widget, callback)
|
||||
connect(widget, { onMousePress = function(widget, mousePos, mouseButton)
|
||||
callback()
|
||||
periodicalEvent(function()
|
||||
callback()
|
||||
end, function()
|
||||
return widget:isPressed()
|
||||
end, 30, 300)
|
||||
return true
|
||||
end })
|
||||
end
|
||||
|
||||
function Mouse.bindPressMove(widget, callback)
|
||||
connect(widget, { onMouseMove = function(widget, mousePos, mouseMoved)
|
||||
if widget:isPressed() then
|
||||
callback(mousePos, mouseMoved)
|
||||
end
|
||||
return true
|
||||
end })
|
||||
end
|
||||
|
15
modules/core_lib/widgets/uiscrollarea.lua
Normal file
15
modules/core_lib/widgets/uiscrollarea.lua
Normal file
@@ -0,0 +1,15 @@
|
||||
UIScrollArea = extends(UIWidget)
|
||||
|
||||
function UIScrollArea.create()
|
||||
local scrollarea = UIScrollArea.internalCreate()
|
||||
scrollarea:setClipping(true)
|
||||
return scrollarea
|
||||
end
|
||||
|
||||
function UIScrollArea:onStyleApply(styleName, styleNode)
|
||||
for name,value in pairs(styleNode) do
|
||||
if name == 'horizontal-scrollbar' then
|
||||
|
||||
end
|
||||
end
|
||||
end
|
@@ -1,30 +1,157 @@
|
||||
UIScrollBar = extends(UIWidget)
|
||||
|
||||
-- private functions
|
||||
local function calcValues(self)
|
||||
local slider = self:getChildById('sliderButton')
|
||||
local decrementButton = self:getChildById('decrementButton')
|
||||
local incrementButton = self:getChildById('incrementButton')
|
||||
|
||||
local pxrange, center
|
||||
if self.orientation == 'vertical' then
|
||||
pxrange = (self:getHeight() - decrementButton:getHeight() - decrementButton:getMarginTop() - decrementButton:getMarginBottom()
|
||||
- incrementButton:getHeight() - incrementButton:getMarginTop() - incrementButton:getMarginBottom())
|
||||
center = self:getY() + self:getHeight() / 2
|
||||
else -- horizontal
|
||||
pxrange = (self:getWidth() - decrementButton:getWidth() - decrementButton:getMarginLeft() - decrementButton:getMarginRight()
|
||||
- incrementButton:getWidth() - incrementButton:getMarginLeft() - incrementButton:getMarginRight())
|
||||
center = self:getX() + self:getWidth() / 2
|
||||
end
|
||||
|
||||
local range = self.maximum - self.minimum + 1
|
||||
local proportion = math.max(range*(self.step/range), 1)/range
|
||||
local px = math.max(math.floor(proportion * pxrange), 10)
|
||||
local offset = ((self.value / (range - 1)) - 0.5) * (pxrange - px)
|
||||
|
||||
return range, pxrange, px, offset, center
|
||||
end
|
||||
|
||||
local function updateSlider(self)
|
||||
local slider = self:getChildById('sliderButton')
|
||||
if slider == nil then return end
|
||||
|
||||
local range, pxrange, px, offset, center = calcValues(self)
|
||||
if self.orientation == 'vertical' then
|
||||
slider:setHeight(px)
|
||||
slider:setMarginTop(offset)
|
||||
else -- horizontal
|
||||
slider:setWidth(px)
|
||||
slider:setMarginLeft(offset)
|
||||
end
|
||||
|
||||
if self.maximum == self.minimum then
|
||||
self:disable()
|
||||
else
|
||||
self:enable()
|
||||
end
|
||||
end
|
||||
|
||||
local function parseSliderPos(self, pos)
|
||||
local point
|
||||
if self.orientation == 'vertical' then
|
||||
point = pos.y
|
||||
else
|
||||
point = pos.x
|
||||
end
|
||||
local range, pxrange, px, offset, center = calcValues(self)
|
||||
offset = math.min(math.max(point - center, -pxrange/2), pxrange/2)
|
||||
local newvalue = math.floor(((offset / (pxrange - px)) + 0.5) * (range - 1))
|
||||
self:setValue(newvalue)
|
||||
end
|
||||
|
||||
|
||||
-- public functions
|
||||
function UIScrollBar.create()
|
||||
local scrollbar = UIScrollBar.internalCreate()
|
||||
scrollbar:setFocusable(false)
|
||||
scrollbar.value = 0
|
||||
scrollbar.minimum = 0
|
||||
scrollbar.maximum = 0
|
||||
scrollbar.step = 1
|
||||
scrollbar.orientation = 'vertical'
|
||||
return scrollbar
|
||||
end
|
||||
|
||||
function UIScrollBar:onSetup()
|
||||
--self.getChildById('upButton').onClick = function() self.navigateUp() end
|
||||
--self.getChildById('upButton').onClick = function() self.navigateDown() end
|
||||
end
|
||||
|
||||
function UIScrollBar:attachWidget(widget)
|
||||
self.attachedWidget = widget
|
||||
addEvent(function()
|
||||
Mouse.bindAutoPress(self:getChildById('decrementButton'), function() self:decrement() end)
|
||||
Mouse.bindAutoPress(self:getChildById('incrementButton'), function() self:increment() end)
|
||||
Mouse.bindPressMove(self:getChildById('sliderButton'), function(mousePos, mouseMoved) parseSliderPos(self, mousePos) end)
|
||||
updateSlider(self)
|
||||
end)
|
||||
end
|
||||
|
||||
function UIScrollBar:onStyleApply(styleName, styleNode)
|
||||
if styleNode['attached-to'] then
|
||||
local id = styleNode['attached-to']
|
||||
local parent = self:getParent()
|
||||
local widget
|
||||
if id == 'parent' then
|
||||
widget = parent
|
||||
elseif parent then
|
||||
widget = parent:getChildById(id)
|
||||
for name,value in pairs(styleNode) do
|
||||
if name == 'maximum' then
|
||||
self:setMaximum(tonumber(value))
|
||||
elseif name == 'minimum' then
|
||||
self:setMinimum(tonumber(value))
|
||||
elseif name == 'step' then
|
||||
self:setStep(tonumber(value))
|
||||
elseif name == 'orientation' then
|
||||
self:setOrientation(value)
|
||||
elseif name == 'value' then
|
||||
self:setValue(value)
|
||||
end
|
||||
self:attachWidget(widget)
|
||||
end
|
||||
end
|
||||
|
||||
function UIScrollBar:decrement()
|
||||
self:setValue(self.value - self.step)
|
||||
end
|
||||
|
||||
function UIScrollBar:increment()
|
||||
self:setValue(self.value + self.step)
|
||||
end
|
||||
|
||||
function UIScrollBar:setMaximum(maximum)
|
||||
if maximum == self.maximum then return end
|
||||
self.maximum = maximum
|
||||
if self.value > maximum then
|
||||
self:setValue(maximum)
|
||||
else
|
||||
updateSlider(self)
|
||||
end
|
||||
end
|
||||
|
||||
function UIScrollBar:setMinimum(minimum)
|
||||
if minimum == self.minimum then return end
|
||||
self.minimum = minimum
|
||||
if self.value < minimum then
|
||||
self:setValue(minimum)
|
||||
else
|
||||
updateSlider(self)
|
||||
end
|
||||
end
|
||||
|
||||
function UIScrollBar:setRange(minimum, maximum)
|
||||
self:setMinimum(minimum)
|
||||
self:setMaximum(maximum)
|
||||
end
|
||||
|
||||
function UIScrollBar:setValue(value)
|
||||
value = math.max(math.min(value, self.maximum), self.minimum)
|
||||
if self.value == value then return end
|
||||
local delta = value - self.value
|
||||
self.value = value
|
||||
updateSlider(self)
|
||||
signalcall(self.onValueChange, self, value, delta)
|
||||
end
|
||||
|
||||
function UIScrollBar:setStep(step)
|
||||
self.step = step
|
||||
end
|
||||
|
||||
function UIScrollBar:setOrientation(orientation)
|
||||
self.orientation = orientation
|
||||
end
|
||||
|
||||
function UIScrollBar:onGeometryChange()
|
||||
updateSlider(self)
|
||||
end
|
||||
|
||||
function UIScrollBar:getMaximum() return self.maximum end
|
||||
function UIScrollBar:getMinimum() return self.minimum end
|
||||
function UIScrollBar:getValue() return self.value end
|
||||
function UIScrollBar:getStep() return self.step end
|
||||
function UIScrollBar:getOrientation() return self.orientation end
|
||||
|
Reference in New Issue
Block a user