mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 11:34:54 +02:00
Refactoring and flexibility changes
* Split game module into game and game_interface * Move core_lib to corelib * Move miniwindow to corelib * Introduce init.lua script for initializing the client, giving much more flexibility * OTClient is no longer Application derived and is much simpler
This commit is contained in:
36
modules/corelib/ui/effects.lua
Normal file
36
modules/corelib/ui/effects.lua
Normal file
@@ -0,0 +1,36 @@
|
||||
Effects = {}
|
||||
|
||||
function Effects.fadeIn(widget, time, elapsed)
|
||||
if not elapsed then elapsed = 0 end
|
||||
if not time then time = 300 end
|
||||
widget:setOpacity(math.min(elapsed/time, 1))
|
||||
removeEvent(widget.fadeEvent)
|
||||
if elapsed < time then
|
||||
removeEvent(widget.fadeEvent)
|
||||
widget.fadeEvent = scheduleEvent(function()
|
||||
Effects.fadeIn(widget, time, elapsed + 30)
|
||||
end, 30)
|
||||
else
|
||||
widget.fadeEvent = nil
|
||||
end
|
||||
end
|
||||
|
||||
function Effects.fadeOut(widget, time, elapsed)
|
||||
if not elapsed then elapsed = 0 end
|
||||
if not time then time = 300 end
|
||||
elapsed = math.max((1 - widget:getOpacity()) * time, elapsed)
|
||||
removeEvent(widget.fadeEvent)
|
||||
widget:setOpacity(math.max((time - elapsed)/time, 0))
|
||||
if elapsed < time then
|
||||
widget.fadeEvent = scheduleEvent(function()
|
||||
Effects.fadeOut(widget, time, elapsed + 30)
|
||||
end, 30)
|
||||
else
|
||||
widget.fadeEvent = nil
|
||||
end
|
||||
end
|
||||
|
||||
function Effects.cancelFade(widget)
|
||||
removeEvent(widget.fadeEvent)
|
||||
widget.fadeEvent = nil
|
||||
end
|
44
modules/corelib/ui/radiogroup.lua
Normal file
44
modules/corelib/ui/radiogroup.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
RadioGroup = newclass()
|
||||
|
||||
function RadioGroup.create()
|
||||
local radiogroup = RadioGroup.internalCreate()
|
||||
radiogroup.widgets = {}
|
||||
return radiogroup
|
||||
end
|
||||
|
||||
function RadioGroup:destroy()
|
||||
for k,widget in pairs(self.widgets) do
|
||||
widget.onClick = nil
|
||||
end
|
||||
self.widgets = {}
|
||||
end
|
||||
|
||||
function RadioGroup:addWidget(widget)
|
||||
table.insert(self.widgets, widget)
|
||||
widget.onClick = function(widget) self:selectWidget(widget) end
|
||||
end
|
||||
|
||||
function RadioGroup:removeWidget(widget)
|
||||
if self.selectedWidget == widget then
|
||||
self:selectWidget(nil)
|
||||
end
|
||||
widget.onClick = nil
|
||||
table.removevalue(self.widgets, widget)
|
||||
end
|
||||
|
||||
function RadioGroup:selectWidget(selectedWidget)
|
||||
if selectedWidget == self.selectedWidget then return end
|
||||
|
||||
local previousSelectedWidget = self.selectedWidget
|
||||
self.selectedWidget = selectedWidget
|
||||
|
||||
if previousSelectedWidget then
|
||||
previousSelectedWidget:setChecked(false)
|
||||
end
|
||||
|
||||
if selectedWidget then
|
||||
selectedWidget:setChecked(true)
|
||||
end
|
||||
|
||||
signalcall(self.onSelectionChange, self, selectedWidget, previousSelectedWidget)
|
||||
end
|
96
modules/corelib/ui/tooltip.lua
Normal file
96
modules/corelib/ui/tooltip.lua
Normal file
@@ -0,0 +1,96 @@
|
||||
ToolTip = {}
|
||||
|
||||
-- private variables
|
||||
local toolTipLabel
|
||||
local currentHoveredWidget
|
||||
|
||||
-- private functions
|
||||
local function moveToolTip(tooltip)
|
||||
if not tooltip:isVisible() or tooltip:getOpacity() < 0.1 then return end
|
||||
|
||||
local pos = g_window.getMousePosition()
|
||||
pos.y = pos.y + 1
|
||||
local xdif = g_window.getSize().width - (pos.x + tooltip:getWidth())
|
||||
if xdif < 2 then
|
||||
pos.x = pos.x - tooltip:getWidth() - 3
|
||||
else
|
||||
pos.x = pos.x + 10
|
||||
end
|
||||
tooltip:setPosition(pos)
|
||||
end
|
||||
|
||||
local function onWidgetHoverChange(widget, hovered)
|
||||
if hovered then
|
||||
if widget.tooltip and not Mouse.isPressed() then
|
||||
ToolTip.display(widget.tooltip)
|
||||
currentHoveredWidget = widget
|
||||
end
|
||||
else
|
||||
if widget == currentHoveredWidget then
|
||||
ToolTip:hide()
|
||||
currentHoveredWidget = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function onWidgetStyleApply(widget, styleName, styleNode)
|
||||
if styleNode.tooltip then
|
||||
widget.tooltip = styleNode.tooltip
|
||||
end
|
||||
end
|
||||
|
||||
-- public functions
|
||||
function ToolTip.init()
|
||||
connect(UIWidget, { onStyleApply = onWidgetStyleApply,
|
||||
onHoverChange = onWidgetHoverChange})
|
||||
|
||||
addEvent(function()
|
||||
toolTipLabel = createWidget('UILabel', rootWidget)
|
||||
toolTipLabel:setId('toolTip')
|
||||
toolTipLabel:setBackgroundColor('#111111cc')
|
||||
toolTipLabel:setTextAlign(AlignCenter)
|
||||
toolTipLabel:hide()
|
||||
toolTipLabel.onMouseMove = moveToolTip
|
||||
end)
|
||||
end
|
||||
|
||||
function ToolTip.terminate()
|
||||
disconnect(UIWidget, { onStyleApply = onWidgetStyleApply,
|
||||
onHoverChange = onWidgetHoverChange })
|
||||
|
||||
currentHoveredWidget = nil
|
||||
toolTipLabel:destroy()
|
||||
toolTipLabel = nil
|
||||
|
||||
ToolTip = nil
|
||||
end
|
||||
|
||||
function ToolTip.display(text)
|
||||
if text == nil then return end
|
||||
if not toolTipLabel then return end
|
||||
|
||||
toolTipLabel:setText(text)
|
||||
toolTipLabel:resizeToText()
|
||||
toolTipLabel:resize(toolTipLabel:getWidth() + 4, toolTipLabel:getHeight() + 4)
|
||||
toolTipLabel:show()
|
||||
toolTipLabel:raise()
|
||||
toolTipLabel:enable()
|
||||
Effects.fadeIn(toolTipLabel, 100)
|
||||
moveToolTip(toolTipLabel)
|
||||
end
|
||||
|
||||
function ToolTip.hide()
|
||||
Effects.fadeOut(toolTipLabel, 100)
|
||||
end
|
||||
|
||||
-- UIWidget extensions
|
||||
function UIWidget:setTooltip(text)
|
||||
self.tooltip = text
|
||||
end
|
||||
|
||||
function UIWidget:getTooltip()
|
||||
return self.tooltip
|
||||
end
|
||||
|
||||
ToolTip.init()
|
||||
connect(g_app, { onTerminate = ToolTip.terminate })
|
Reference in New Issue
Block a user