|
@@ -1,6 +1,8 @@
|
|
local utf8 = require "utf8"
|
|
local utf8 = require "utf8"
|
|
|
|
+
|
|
local UI2D = {}
|
|
local UI2D = {}
|
|
local has_text_input = false
|
|
local has_text_input = false
|
|
|
|
+local has_mouse = false
|
|
local e_mouse_state = { clicked = 1, held = 2, released = 3, idle = 4 }
|
|
local e_mouse_state = { clicked = 1, held = 2, released = 3, idle = 4 }
|
|
local e_slider_type = { int = 1, float = 2 }
|
|
local e_slider_type = { int = 1, float = 2 }
|
|
local modal_window = nil
|
|
local modal_window = nil
|
|
@@ -318,12 +320,6 @@ local function Slider( type, name, v, v_min, v_max, width )
|
|
return result, v
|
|
return result, v
|
|
end
|
|
end
|
|
|
|
|
|
--- function utf8.sub( s, i, j )
|
|
|
|
--- i = utf8.offset( s, i )
|
|
|
|
--- j = utf8.offset( s, j + 1 ) - 1
|
|
|
|
--- return string.sub( s, i, j )
|
|
|
|
--- end
|
|
|
|
-
|
|
|
|
function utf8.sub( s, i, j )
|
|
function utf8.sub( s, i, j )
|
|
i = utf8.offset( s, i ) or 1
|
|
i = utf8.offset( s, i ) or 1
|
|
local nextOffset = utf8.offset( s, j + 1 )
|
|
local nextOffset = utf8.offset( s, j + 1 )
|
|
@@ -361,6 +357,7 @@ end
|
|
-- -------------------------------------------------------------------------- --
|
|
-- -------------------------------------------------------------------------- --
|
|
-- User --
|
|
-- User --
|
|
-- -------------------------------------------------------------------------- --
|
|
-- -------------------------------------------------------------------------- --
|
|
|
|
+
|
|
function UI2D.Init( size )
|
|
function UI2D.Init( size )
|
|
local info = debug.getinfo( 1, "S" )
|
|
local info = debug.getinfo( 1, "S" )
|
|
local lib_path = info.source:match( "@(.*[\\/])" )
|
|
local lib_path = info.source:match( "@(.*[\\/])" )
|
|
@@ -408,6 +405,7 @@ function UI2D.InputInfo()
|
|
hovers_active = true
|
|
hovers_active = true
|
|
end
|
|
end
|
|
hovers_any = true
|
|
hovers_any = true
|
|
|
|
+ has_mouse = true
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
@@ -441,6 +439,11 @@ function UI2D.InputInfo()
|
|
has_text_input = false
|
|
has_text_input = false
|
|
end
|
|
end
|
|
|
|
|
|
|
|
+ -- Give back mouse
|
|
|
|
+ if not hovers_any then
|
|
|
|
+ has_mouse = false
|
|
|
|
+ end
|
|
|
|
+
|
|
-- Handle window dragging
|
|
-- Handle window dragging
|
|
if active_window then
|
|
if active_window then
|
|
local v = active_window
|
|
local v = active_window
|
|
@@ -614,7 +617,7 @@ function UI2D.End( main_pass )
|
|
for i, v in ipairs( cur_window.cw ) do
|
|
for i, v in ipairs( cur_window.cw ) do
|
|
cur_window.pass:setColor( 1, 1, 1 )
|
|
cur_window.pass:setColor( 1, 1, 1 )
|
|
cur_window.pass:setMaterial( v.texture )
|
|
cur_window.pass:setMaterial( v.texture )
|
|
- cur_window.pass:plane( v.bbox.x + (v.bbox.w / 2), v.bbox.y + (v.bbox.h / 2), 0, v.bbox.w, v.bbox.h )
|
|
|
|
|
|
+ cur_window.pass:plane( v.bbox.x + (v.bbox.w / 2), v.bbox.y + (v.bbox.h / 2), 0, v.bbox.w, -v.bbox.h )
|
|
cur_window.pass:setMaterial()
|
|
cur_window.pass:setMaterial()
|
|
cur_window.pass:setColor( 1, 1, 1 )
|
|
cur_window.pass:setColor( 1, 1, 1 )
|
|
end
|
|
end
|
|
@@ -623,6 +626,15 @@ function UI2D.End( main_pass )
|
|
begin_end_pairs.e = begin_end_pairs.e + 1
|
|
begin_end_pairs.e = begin_end_pairs.e + 1
|
|
end
|
|
end
|
|
|
|
|
|
|
|
+function UI2D.HasMouse()
|
|
|
|
+ return has_mouse
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+function UI2D.GetMouseWheel( deltaX, deltaY )
|
|
|
|
+ mouse.wheel_x = deltaX
|
|
|
|
+ mouse.wheel_y = deltaY
|
|
|
|
+end
|
|
|
|
+
|
|
function UI2D.SetWindowPosition( id, x, y )
|
|
function UI2D.SetWindowPosition( id, x, y )
|
|
local exists, idx = WindowExists( id )
|
|
local exists, idx = WindowExists( id )
|
|
if exists then
|
|
if exists then
|
|
@@ -707,6 +719,10 @@ function UI2D.HasTextInput()
|
|
return has_text_input
|
|
return has_text_input
|
|
end
|
|
end
|
|
|
|
|
|
|
|
+function UI2D.IsModalOpen()
|
|
|
|
+ return modal_window
|
|
|
|
+end
|
|
|
|
+
|
|
function UI2D.EndModalWindow()
|
|
function UI2D.EndModalWindow()
|
|
modal_window = nil
|
|
modal_window = nil
|
|
end
|
|
end
|
|
@@ -1177,7 +1193,7 @@ function UI2D.TextBox( name, num_visible_chars, text )
|
|
end
|
|
end
|
|
|
|
|
|
if active_widget == cur_window.id .. name then
|
|
if active_widget == cur_window.id .. name then
|
|
- if lovr.system.wasKeyPressed( "tab" ) or lovr.system.wasKeyPressed( "return" ) then -- Deactivate self
|
|
|
|
|
|
+ if lovr.system.wasKeyPressed( "tab" ) or lovr.system.wasKeyPressed( "return" ) or lovr.system.wasKeyPressed( "kpenter" ) then -- Deactivate self
|
|
has_text_input = false
|
|
has_text_input = false
|
|
active_textbox = nil
|
|
active_textbox = nil
|
|
active_widget = nil
|
|
active_widget = nil
|
|
@@ -1494,11 +1510,20 @@ function UI2D.CustomWidget( name, width, height )
|
|
if not exists then
|
|
if not exists then
|
|
local new_widget = {}
|
|
local new_widget = {}
|
|
new_widget.id = cur_window.id .. name
|
|
new_widget.id = cur_window.id .. name
|
|
|
|
+ new_widget.width = width
|
|
|
|
+ new_widget.height = height
|
|
new_widget.texture = lovr.graphics.newTexture( width, height, texture_flags )
|
|
new_widget.texture = lovr.graphics.newTexture( width, height, texture_flags )
|
|
new_widget.pass = lovr.graphics.newPass( new_widget.texture )
|
|
new_widget.pass = lovr.graphics.newPass( new_widget.texture )
|
|
new_widget.pass:setProjection( 1, mat4():orthographic( new_widget.pass:getDimensions() ) )
|
|
new_widget.pass:setProjection( 1, mat4():orthographic( new_widget.pass:getDimensions() ) )
|
|
table.insert( cur_window.cw, new_widget )
|
|
table.insert( cur_window.cw, new_widget )
|
|
idx = #cur_window.cw
|
|
idx = #cur_window.cw
|
|
|
|
+ else
|
|
|
|
+ if cur_window.cw[ idx ].width ~= width or cur_window.cw[ idx ].height ~= height then
|
|
|
|
+ cur_window.cw[ idx ].width = width
|
|
|
|
+ cur_window.cw[ idx ].height = height
|
|
|
|
+ cur_window.cw[ idx ].texture = lovr.graphics.newTexture( width, height, texture_flags )
|
|
|
|
+ cur_window.cw[ idx ].pass:setCanvas( cur_window.cw[ idx ].texture )
|
|
|
|
+ end
|
|
end
|
|
end
|
|
|
|
|
|
local bbox = {}
|
|
local bbox = {}
|
|
@@ -1516,10 +1541,12 @@ function UI2D.CustomWidget( name, width, height )
|
|
local held = false
|
|
local held = false
|
|
local released = false
|
|
local released = false
|
|
local hovered = false
|
|
local hovered = false
|
|
|
|
+ local wheelx, wheely = 0, 0
|
|
|
|
|
|
if not modal_window or (modal_window and modal_window == cur_window) then
|
|
if not modal_window or (modal_window and modal_window == cur_window) then
|
|
if PointInRect( mouse.x, mouse.y, bbox.x + cur_window.x, bbox.y + cur_window.y, bbox.w, bbox.h ) and cur_window == active_window then
|
|
if PointInRect( mouse.x, mouse.y, bbox.x + cur_window.x, bbox.y + cur_window.y, bbox.w, bbox.h ) and cur_window == active_window then
|
|
hovered = true
|
|
hovered = true
|
|
|
|
+ wheelx, wheely = mouse.wheel_x, mouse.wheel_y
|
|
|
|
|
|
if mouse.state == e_mouse_state.clicked then
|
|
if mouse.state == e_mouse_state.clicked then
|
|
clicked = true
|
|
clicked = true
|
|
@@ -1540,7 +1567,7 @@ function UI2D.CustomWidget( name, width, height )
|
|
table.insert( windows[ begin_idx ].command_list, { type = "rect_wire", bbox = bbox, color = colors.button_border } )
|
|
table.insert( windows[ begin_idx ].command_list, { type = "rect_wire", bbox = bbox, color = colors.button_border } )
|
|
cur_window.cw[ idx ].pass:reset()
|
|
cur_window.cw[ idx ].pass:reset()
|
|
cur_window.cw[ idx ].pass:setProjection( 1, mat4():orthographic( cur_window.cw[ idx ].pass:getDimensions() ) )
|
|
cur_window.cw[ idx ].pass:setProjection( 1, mat4():orthographic( cur_window.cw[ idx ].pass:getDimensions() ) )
|
|
- return cur_window.cw[ idx ].pass, clicked, held, released, hovered, mouse.x - cur_window.x - bbox.x, mouse.y - cur_window.y - bbox.y
|
|
|
|
|
|
+ return cur_window.cw[ idx ].pass, clicked, held, released, hovered, mouse.x - cur_window.x - bbox.x, mouse.y - cur_window.y - bbox.y, wheelx, wheely
|
|
end
|
|
end
|
|
|
|
|
|
function UI2D.NewFrame( main_pass )
|
|
function UI2D.NewFrame( main_pass )
|