ReiGui text input.
This commit is contained in:
7
devnotes
7
devnotes
@@ -1,10 +1,9 @@
|
|||||||
Current {
|
Current {
|
||||||
* ReiLuaGui
|
* ReiLuaGui
|
||||||
* Mouse wheel scrolling can be detected through other elements.
|
|
||||||
* Could try putting container over all of it's content elements and having it's onOver function not breaking loop.
|
|
||||||
* Text input.
|
|
||||||
* Movable window.
|
* Movable window.
|
||||||
* Grid container.
|
* Grid aling for container. Could use bounds of the first cell.
|
||||||
|
|
||||||
|
* Examples:
|
||||||
* Calculator.
|
* Calculator.
|
||||||
* Grid icon window.
|
* Grid icon window.
|
||||||
* File explorer.
|
* File explorer.
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ local container = {}
|
|||||||
-- local circleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/circle.png" )
|
-- local circleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/circle.png" )
|
||||||
local circleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/plain-circle.png" )
|
local circleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/plain-circle.png" )
|
||||||
local checkTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/check-mark.png" )
|
local checkTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/check-mark.png" )
|
||||||
|
local textInput
|
||||||
|
|
||||||
RL_GenTextureMipmaps( circleTexture )
|
RL_GenTextureMipmaps( circleTexture )
|
||||||
RL_GenTextureMipmaps( checkTexture )
|
RL_GenTextureMipmaps( checkTexture )
|
||||||
@@ -22,7 +23,8 @@ function initGui()
|
|||||||
|
|
||||||
container = Gui.container:new( {
|
container = Gui.container:new( {
|
||||||
bounds = Rect:new( 256, 120, 128, 128 ),
|
bounds = Rect:new( 256, 120, 128, 128 ),
|
||||||
-- spacing = 4,
|
-- spacing = 14,
|
||||||
|
-- drawScrollRect = true,
|
||||||
-- Haling = Gui.ALING.LEFT,
|
-- Haling = Gui.ALING.LEFT,
|
||||||
-- Haling = Gui.ALING.CENTER,
|
-- Haling = Gui.ALING.CENTER,
|
||||||
-- Valing = Gui.ALING.CENTER,
|
-- Valing = Gui.ALING.CENTER,
|
||||||
@@ -101,6 +103,21 @@ function initGui()
|
|||||||
container:add( container2 )
|
container:add( container2 )
|
||||||
|
|
||||||
panel:set2Top()
|
panel:set2Top()
|
||||||
|
|
||||||
|
-- Text input.
|
||||||
|
|
||||||
|
textInput = Gui.element:new( {
|
||||||
|
bounds = Rect:new( 64, 360, 300, 32 ),
|
||||||
|
drawBounds = true,
|
||||||
|
color = Color:new( LIGHTGRAY ),
|
||||||
|
onClicked = function() Gui.setInputFocus( textInput ) end,
|
||||||
|
inputFocus = function() textInput.color = Color:new( BLUE ) end,
|
||||||
|
-- inputFocus = function() container:delete() end,
|
||||||
|
-- inputFocus = function() panel:set2Back() end,
|
||||||
|
inputUnfocus = function() textInput.color = Color:new( LIGHTGRAY ) end,
|
||||||
|
} )
|
||||||
|
|
||||||
|
textInput:add( Gui.text:new( { text = "", maxTextLen = 16, allowLineBreak = false } ) )
|
||||||
end
|
end
|
||||||
|
|
||||||
function init()
|
function init()
|
||||||
@@ -108,7 +125,6 @@ function init()
|
|||||||
local mPos = RL_GetMonitorPosition( monitor )
|
local mPos = RL_GetMonitorPosition( monitor )
|
||||||
local mSize = RL_GetMonitorSize( monitor )
|
local mSize = RL_GetMonitorSize( monitor )
|
||||||
winSize = RL_GetWindowSize()
|
winSize = RL_GetWindowSize()
|
||||||
-- local winSize = { 1920, 1080 }
|
|
||||||
|
|
||||||
RL_SetWindowTitle( "ReiLua Gui" )
|
RL_SetWindowTitle( "ReiLua Gui" )
|
||||||
RL_SetWindowState( FLAG_WINDOW_RESIZABLE )
|
RL_SetWindowState( FLAG_WINDOW_RESIZABLE )
|
||||||
@@ -127,4 +143,5 @@ function draw()
|
|||||||
RL_ClearBackground( RAYWHITE )
|
RL_ClearBackground( RAYWHITE )
|
||||||
|
|
||||||
Gui.draw()
|
Gui.draw()
|
||||||
|
RL_DrawText( 0, "Work in progress GuiLib test.", { 10, 10 }, 30, 4, BLACK )
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,6 +5,16 @@ Color = require( "color" )
|
|||||||
|
|
||||||
-- NOTE!!! Work in progress! Do not use.
|
-- NOTE!!! Work in progress! Do not use.
|
||||||
|
|
||||||
|
--[[
|
||||||
|
To add repeat inputs to the keys pressed buffer, you could add GLFW_REPEAT in railib rcore.c in function "KeyCallback" by changing:
|
||||||
|
|
||||||
|
if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS))
|
||||||
|
To
|
||||||
|
if ((CORE.Input.Keyboard.keyPressedQueueCount < MAX_KEY_PRESSED_QUEUE) && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||||
|
|
||||||
|
Now GLFW_REPEAT can be read by "RL_GetKeyPressed".
|
||||||
|
]]
|
||||||
|
|
||||||
Gui = {
|
Gui = {
|
||||||
ALING = {
|
ALING = {
|
||||||
NONE = 0,
|
NONE = 0,
|
||||||
@@ -40,9 +50,13 @@ Gui = {
|
|||||||
|
|
||||||
_cells = {},
|
_cells = {},
|
||||||
_mousePos = Vec2:new( 0, 0 ), -- Last mouse position that was passed to Gui.process.
|
_mousePos = Vec2:new( 0, 0 ), -- Last mouse position that was passed to Gui.process.
|
||||||
heldCallback = nil,
|
_heldCallback = nil,
|
||||||
|
_inputElement = nil,
|
||||||
|
_inputItem = nil, -- Must be type Text.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local readyToUnfocusInput = false -- Guard to not unfocus input immediately.
|
||||||
|
|
||||||
local function setProperty( set, property, default )
|
local function setProperty( set, property, default )
|
||||||
if set ~= nil and set[ property ] ~= nil then
|
if set ~= nil and set[ property ] ~= nil then
|
||||||
return set[ property ]
|
return set[ property ]
|
||||||
@@ -51,27 +65,76 @@ local function setProperty( set, property, default )
|
|||||||
return default
|
return default
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Gui.setInputFocus( element, itemID )
|
||||||
|
itemID = itemID or 1
|
||||||
|
|
||||||
|
Gui._inputElement = element
|
||||||
|
Gui._inputItem = element.items[ itemID ]
|
||||||
|
|
||||||
|
if element.inputFocus ~= nil then
|
||||||
|
element.inputFocus()
|
||||||
|
end
|
||||||
|
|
||||||
|
readyToUnfocusInput = false
|
||||||
|
end
|
||||||
|
|
||||||
|
function Gui.inputUnfocus()
|
||||||
|
if Gui._inputElement ~= nil then
|
||||||
|
Gui._inputElement.inputUnfocus()
|
||||||
|
end
|
||||||
|
|
||||||
|
Gui._inputElement = nil
|
||||||
|
Gui._inputItem = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function Gui.getId( cell )
|
||||||
|
for id, c in ipairs( Gui._cells ) do
|
||||||
|
if cell == c then
|
||||||
|
return id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function Gui.delete( cell )
|
||||||
|
local id = Gui.getId( cell )
|
||||||
|
|
||||||
|
if id ~= nil then
|
||||||
|
table.remove( Gui._cells, id )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Gui.set2Top( cell )
|
||||||
|
util.tableMove( Gui._cells, Gui.getId( cell ), 1, #Gui._cells )
|
||||||
|
end
|
||||||
|
|
||||||
|
function Gui.set2Back( cell )
|
||||||
|
util.tableMove( Gui._cells, Gui.getId( cell ), 1, 1 )
|
||||||
|
end
|
||||||
|
|
||||||
function Gui.process( mousePosition )
|
function Gui.process( mousePosition )
|
||||||
local mouseWheel = RL_GetMouseWheelMove()
|
local mouseWheel = RL_GetMouseWheelMove()
|
||||||
|
|
||||||
Gui._mousePos = mousePosition
|
Gui._mousePos = mousePosition
|
||||||
|
|
||||||
if Gui.heldCallback ~= nil then
|
if Gui._heldCallback ~= nil then
|
||||||
if RL_IsMouseButtonDown( Gui.mouseButton ) then
|
if RL_IsMouseButtonDown( Gui.mouseButton ) then
|
||||||
Gui.heldCallback()
|
Gui._heldCallback()
|
||||||
else
|
else
|
||||||
Gui.heldCallback = nil
|
Gui._heldCallback = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Go backwards on process check so we trigger the top most ui first and stop there.
|
|
||||||
local foundFirst = false
|
local foundFirst = false
|
||||||
|
|
||||||
|
-- Go backwards on process check so we trigger the top most ui first and stop there.
|
||||||
for i = #Gui._cells, 1, -1 do
|
for i = #Gui._cells, 1, -1 do
|
||||||
local cell = Gui._cells[i]
|
local cell = Gui._cells[i]
|
||||||
|
|
||||||
|
if cell ~= nil then
|
||||||
if not foundFirst and cell.isMouseOver ~= nil and cell:isMouseOver( mousePosition ) and not cell.disabled then
|
if not foundFirst and cell.isMouseOver ~= nil and cell:isMouseOver( mousePosition ) and not cell.disabled then
|
||||||
-- On clicked.
|
-- On clicked.
|
||||||
if RL_IsMouseButtonPressed( Gui.mouseButton ) and cell.onClicked ~= nil then
|
if RL_IsMouseButtonPressed( Gui.mouseButton ) and cell.onClicked ~= nil then
|
||||||
@@ -106,6 +169,45 @@ function Gui.process( mousePosition )
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Text input.
|
||||||
|
if Gui._inputItem ~= nil then
|
||||||
|
repeat
|
||||||
|
local char = RL_GetCharPressed()
|
||||||
|
|
||||||
|
if 0 < char then
|
||||||
|
if utf8.len( Gui._inputItem.text ) < Gui._inputItem.maxTextLen then
|
||||||
|
Gui._inputItem.text = Gui._inputItem.text..utf8.char( char )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
until char == 0
|
||||||
|
|
||||||
|
repeat
|
||||||
|
local key = RL_GetKeyPressed()
|
||||||
|
|
||||||
|
if 0 < key then
|
||||||
|
if key == KEY_BACKSPACE then
|
||||||
|
Gui._inputItem.text = util.utf8Sub( Gui._inputItem.text, 0, utf8.len( Gui._inputItem.text ) - 1 )
|
||||||
|
elseif key == KEY_ENTER or key == KEY_KP_ENTER then
|
||||||
|
if Gui._inputItem.allowLineBreak then
|
||||||
|
Gui._inputItem.text = Gui._inputItem.text.."\n"
|
||||||
|
else
|
||||||
|
Gui.inputUnfocus()
|
||||||
|
end
|
||||||
|
elseif key == KEY_ESCAPE then
|
||||||
|
Gui.inputUnfocus()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
until key == 0
|
||||||
|
|
||||||
|
if readyToUnfocusInput and RL_IsMouseButtonPressed( Gui.mouseButton ) then
|
||||||
|
Gui.inputUnfocus()
|
||||||
|
end
|
||||||
|
|
||||||
|
readyToUnfocusInput = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Gui.draw()
|
function Gui.draw()
|
||||||
for _, element in ipairs( Gui._cells ) do
|
for _, element in ipairs( Gui._cells ) do
|
||||||
if element.draw ~= nil and element.visible then
|
if element.draw ~= nil and element.visible then
|
||||||
@@ -134,6 +236,7 @@ function Text:new( set )
|
|||||||
object.spacing = setProperty( set, "spacing", Gui.spacing )
|
object.spacing = setProperty( set, "spacing", Gui.spacing )
|
||||||
object.color = setProperty( set, "color", Color:new( BLACK ) )
|
object.color = setProperty( set, "color", Color:new( BLACK ) )
|
||||||
object.maxTextLen = setProperty( set, "maxTextLen", nil )
|
object.maxTextLen = setProperty( set, "maxTextLen", nil )
|
||||||
|
object.allowLineBreak = setProperty( set, "allowLineBreak", false )
|
||||||
|
|
||||||
object.visible = setProperty( set, "visible", true )
|
object.visible = setProperty( set, "visible", true )
|
||||||
object._parent = nil
|
object._parent = nil
|
||||||
@@ -314,7 +417,7 @@ Element.__index = Element
|
|||||||
function Element:new( set )
|
function Element:new( set )
|
||||||
local object = setmetatable( {}, Element )
|
local object = setmetatable( {}, Element )
|
||||||
|
|
||||||
object._ID = #Gui._cells + 1
|
-- object._ID = #Gui._cells + 1
|
||||||
object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) )
|
object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) )
|
||||||
object.padding = setProperty( set, "padding", Gui.padding )
|
object.padding = setProperty( set, "padding", Gui.padding )
|
||||||
object.visible = setProperty( set, "visible", true )
|
object.visible = setProperty( set, "visible", true )
|
||||||
@@ -330,6 +433,8 @@ function Element:new( set )
|
|||||||
object.notMouseOver = setProperty( set, "notMouseOver", nil )
|
object.notMouseOver = setProperty( set, "notMouseOver", nil )
|
||||||
object.onClicked = setProperty( set, "onClicked", nil )
|
object.onClicked = setProperty( set, "onClicked", nil )
|
||||||
object.onHeld = setProperty( set, "onHeld", nil )
|
object.onHeld = setProperty( set, "onHeld", nil )
|
||||||
|
object.inputFocus = setProperty( set, "inputFocus", nil )
|
||||||
|
object.inputUnfocus = setProperty( set, "inputUnfocus", nil )
|
||||||
object._parent = nil
|
object._parent = nil
|
||||||
|
|
||||||
table.insert( Gui._cells, object )
|
table.insert( Gui._cells, object )
|
||||||
@@ -415,15 +520,15 @@ function Element:draw()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Element:delete()
|
function Element:delete()
|
||||||
table.remove( Gui._cells, self._ID )
|
Gui.delete( self )
|
||||||
end
|
end
|
||||||
|
|
||||||
function Element:set2Top()
|
function Element:set2Top()
|
||||||
util.tableMove( Gui._cells, self._ID, 1, #Gui._cells )
|
Gui.set2Top( self )
|
||||||
end
|
end
|
||||||
|
|
||||||
function Element:set2Back()
|
function Element:set2Back()
|
||||||
util.tableMove( Gui._cells, self._ID, 1, 1 )
|
Gui.set2Back( self )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Container.
|
-- Container.
|
||||||
@@ -434,7 +539,7 @@ Container.__index = Container
|
|||||||
function Container:new( set )
|
function Container:new( set )
|
||||||
local object = setmetatable( {}, Container )
|
local object = setmetatable( {}, Container )
|
||||||
|
|
||||||
object._ID = #Gui._cells + 1
|
-- object._ID = #Gui._cells + 1
|
||||||
object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) )
|
object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) )
|
||||||
object.spacing = setProperty( set, "spacing", Gui.spacing )
|
object.spacing = setProperty( set, "spacing", Gui.spacing )
|
||||||
object.type = setProperty( set, "type", Gui.CONTAINER.VERTICAL )
|
object.type = setProperty( set, "type", Gui.CONTAINER.VERTICAL )
|
||||||
@@ -447,6 +552,7 @@ function Container:new( set )
|
|||||||
object.showScrollbar = setProperty( set, "showScrollbar", false )
|
object.showScrollbar = setProperty( set, "showScrollbar", false )
|
||||||
object.scrollbarWidth = setProperty( set, "scrollbarWidth", Gui.scrollbarWidth )
|
object.scrollbarWidth = setProperty( set, "scrollbarWidth", Gui.scrollbarWidth )
|
||||||
object.scrollAmount = setProperty( set, "scrollAmount", Gui.scrollAmount ) -- When using mouse scroll.
|
object.scrollAmount = setProperty( set, "scrollAmount", Gui.scrollAmount ) -- When using mouse scroll.
|
||||||
|
object.drawScrollRect = setProperty( set, "drawScrollRect", false )
|
||||||
|
|
||||||
object.cells = {}
|
object.cells = {}
|
||||||
|
|
||||||
@@ -576,7 +682,7 @@ function Container:update()
|
|||||||
padding = 0,
|
padding = 0,
|
||||||
drawBounds = true,
|
drawBounds = true,
|
||||||
color = Color:new( GRAY ),
|
color = Color:new( GRAY ),
|
||||||
onClicked = function() Gui.heldCallback = function() self:mouseScroll( Vec2:new( 0, 1 ) ) end end,
|
onClicked = function() Gui._heldCallback = function() self:mouseScroll( Vec2:new( 0, 1 ) ) end end,
|
||||||
} )
|
} )
|
||||||
|
|
||||||
self._VScrollbar:add( Gui.shape:new( {
|
self._VScrollbar:add( Gui.shape:new( {
|
||||||
@@ -593,7 +699,7 @@ function Container:update()
|
|||||||
padding = 0,
|
padding = 0,
|
||||||
drawBounds = true,
|
drawBounds = true,
|
||||||
color = Color:new( GRAY ),
|
color = Color:new( GRAY ),
|
||||||
onClicked = function() Gui.heldCallback = function() self:mouseScroll( Vec2:new( 1, 0 ) ) end end,
|
onClicked = function() Gui._heldCallback = function() self:mouseScroll( Vec2:new( 1, 0 ) ) end end,
|
||||||
} )
|
} )
|
||||||
|
|
||||||
self._HScrollbar:add( Gui.shape:new( {
|
self._HScrollbar:add( Gui.shape:new( {
|
||||||
@@ -652,7 +758,60 @@ function Container:update()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Container:delete()
|
function Container:delete()
|
||||||
table.remove( Gui._cells, self._ID )
|
for _, cell in ipairs( self.cells ) do
|
||||||
|
cell:delete()
|
||||||
|
end
|
||||||
|
|
||||||
|
if self._VScrollbar ~= nil then
|
||||||
|
Gui.delete( self._VScrollbar )
|
||||||
|
end
|
||||||
|
if self._HScrollbar ~= nil then
|
||||||
|
Gui.delete( self._HScrollbar )
|
||||||
|
end
|
||||||
|
|
||||||
|
Gui.delete( self )
|
||||||
|
end
|
||||||
|
|
||||||
|
function Container:set2Top()
|
||||||
|
Gui.set2Top( self )
|
||||||
|
|
||||||
|
for _, cell in ipairs( self.cells ) do
|
||||||
|
cell:set2Top()
|
||||||
|
end
|
||||||
|
|
||||||
|
if self._VScrollbar ~= nil then
|
||||||
|
Gui.set2Top( self._VScrollbar )
|
||||||
|
end
|
||||||
|
if self._HScrollbar ~= nil then
|
||||||
|
Gui.set2Top( self._HScrollbar )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Container:set2Back()
|
||||||
|
if self._VScrollbar ~= nil then
|
||||||
|
Gui.set2Back( self._VScrollbar )
|
||||||
|
end
|
||||||
|
if self._HScrollbar ~= nil then
|
||||||
|
Gui.set2Back( self._HScrollbar )
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, cell in ipairs( self.cells ) do
|
||||||
|
cell:set2Back()
|
||||||
|
end
|
||||||
|
|
||||||
|
Gui.set2Back( self )
|
||||||
|
end
|
||||||
|
|
||||||
|
function Container:draw()
|
||||||
|
if self.drawScrollRect then
|
||||||
|
RL_DrawRectangleLines( {
|
||||||
|
self.bounds.x - self._scrollRect.x,
|
||||||
|
self.bounds.y - self._scrollRect.y,
|
||||||
|
self._scrollRect.width,
|
||||||
|
self._scrollRect.height,
|
||||||
|
},
|
||||||
|
RED )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--Assingments.
|
--Assingments.
|
||||||
|
|||||||
Reference in New Issue
Block a user