Searching for myself
-
Автор темы
- #1
Не работает drag в либе primordial
code_language.lua:
local DragSystem = {}
local vecScreenSize = render.get_screen_size( )
DragSystem.pList = {}
DragSystem.pWindows = {}
DragSystem.__index = DragSystem
function DragSystem:Setup( vecPosition, vecSize, strName, SelfFn )
local m_pData = {
vecPosition = vecPosition,
vecSize = vecSize,
bIsDragging = false,
vecDragPosition = vec2_t( 0.0, 0.0 ),
strName = strName,
SelfFn = SelfFn,
m_pCallbacks = {
x = vecPosition.x,
y = vecPosition.y
}
}
table.insert( DragSystem.pWindows, m_pData )
return setmetatable( m_pData, DragSystem )
end
function DragSystem:LimitPositions( )
if self.vecPosition.x <= 0.0 then
self.vecPosition.x = 0.0
end
if self.vecPosition.x + self.vecSize.x >= vecScreenSize.x - 1.0 then
self.vecPosition.x = vecScreenSize.x - self.vecSize.x - 1.0
end
if self.vecPosition.y <= 0.0 then
self.vecPosition.y = 0.0
end
if self.vecPosition.y + self.vecSize.y >= vecScreenSize.y - 1.0 then
self.vecPosition.y = vecScreenSize.y - self.vecSize.y - 1.0
end
end
function DragSystem:Update( ... )
if menu.is_open( ) then
local vecMousePosition = input.get_mouse_pos( )
local bIsInArea = input.is_mouse_in_bounds( self.vecPosition, self.vecPosition + self.vecSize )
local pList = self.pList
local bIsKeyActive = input.is_key_held( e_keys.MOUSE_LEFT )
if ( bIsKeyActive and not self.bIsDragging ) and bIsInArea and pList.strTarget == self.strName then
self.bIsDragging = true
self.vecDragPosition = vecMousePosition - self.vecPosition
self.vecPosition = vecMousePosition - self.vecDragPosition
self:LimitPositions( )
self.m_pCallbacks.x:set( math.floor( self.vecPosition.x ) )
self.m_pCallbacks.y:set( math.floor( self.vecPosition.y ) )
elseif not bIsKeyActive then
pList.strTarget = ''
self.bIsDragging = false
self.vecDragPosition = vec2_t( 0.0, 0.0 )
end
end
self.SelfFn( self, ... )
end
return DragSystem
code_language.lua:
-- ObjectSize - гетаю со слайдера
local Object = Script.m_pDragSystem:Setup( ObjectSize, vec2_t( 200, 200 ), 'Test', function( self )
render.rect_filled( self.vecPosition, self.vecPosition + self.vecSize, color_t( 255, 255, 255, 255 ) )
end ):Update( )