Page 1 of 1

How to correctly read text from keyboard

Posted: Sun May 10, 2020 12:19 am
by cristoferfb
This is my class diagram:

Image

and this is my code:

Code: Select all

require "views/functionView"

function love.load ()
	functionView = FunctionView()
end

function love.draw ()
	functionView:draw()
end

function love.textinput (text)
	-- ???
end

function love.update (dt)
	functionView:update(dt)
end
My question is ¿How I can read text from my Node class? As u can see it is at three levels of abstraction inside my FuncionView Class, and the love.textinput event handle is in the top of my file tree. ¿Any scalable way for do it?

EDIT:

As the title say I want to read text from keyboard, not how to read a key, I need emulate the behaviour of a input text in Android. Then, as u can see in the example code, I read about the love.textinput event handler but the class thats need it is inside of 2 parents class, so I obviusly can break some paradigms for take it more easy, but speaking of real projects with a certain complexity that needs order and nice practices, there is a relatively clean way of do it?

Re: How to correctly read text from keyboard

Posted: Sun May 10, 2020 1:57 am
by 4vZEROv
https://www.youtube.com/watch?v=-AQfQFcXac8

What do you want to do ?
What do your classes represent ?


To respond to the title of your post :
Callback :
https://love2d.org/wiki/love.keypressed

Inside love.update:
https://love2d.org/wiki/love.keyboard.isDown

Re: How to correctly read text from keyboard

Posted: Sun May 10, 2020 4:48 am
by cristoferfb
4vZEROv wrote: Sun May 10, 2020 1:57 am https://www.youtube.com/watch?v=-AQfQFcXac8

What do you want to do ?
What do your classes represent ?


To respond to the title of your post :
Callback :
https://love2d.org/wiki/love.keypressed

Inside love.update:
https://love2d.org/wiki/love.keyboard.isDown
Sorry for not be clear I reedit my question, I think not is importat know what represent any class, but for example, with the functions u show me, with love.keyboard.isDown, I can easily use that inside any method of any class for read a speciffic key, but how I can do the same for read full words (text input)?

Re: How to correctly read text from keyboard

Posted: Sun May 10, 2020 9:55 pm
by tobiasvl
Can you explain your problem better? What exactly is the problem? How does Android do text input, since you want to do it the way Android does it? Note that if you're making this for Android, text input is disabled by default and you need to call love.keyboard.setTextInput to enable it (which will display the on-screen keyboard).

What is the Node class and what do you mean you want to read text "from" your Node class? What have you tried so far and how is it not working?

There's no callback or function to read full words in LÖVE, so you need to stitch together the input, like the example on the love.textinput wiki page says. (It might actually buffer input so you get more than one character at a time, I have no idea really, but it doesn't give you whole words, at any rate.)

Re: How to correctly read text from keyboard

Posted: Sun May 10, 2020 10:20 pm
by cristoferfb
Well the important part of the classes I mentioned is understand that FunctionView instantiate a child class called Path and that it own child class called Node (basic object oriented concepts, no more deep here) as u can see in my code example the callback 'love.textinput ()' is in a different level of abstraction vs the Node class (this is the only importance of the class diagram I showed previusly, ilustrate the position of my Node respect the main class FunctionView) so if u say me that is the only way of read full text... How does a real project handle all the 'text input' calls it might need using only that callback? Think I may have different UI components (in different .lua files) that may need use text input. I am asking more for a nice practice or advice about this.

Re: How to correctly read text from keyboard

Posted: Tue May 12, 2020 11:31 pm
by Ref
I don't know about "correct" but I've hacked a library together that allows test input without too much of a burden.
Sure you will come up with something better but might get some ideas from this.
Best

Re: How to correctly read text from keyboard

Posted: Wed May 13, 2020 6:57 am
by zorg
Since i am assuming only one UI component would ever be active, you just have an appropriate method in all relevant components, that can access whatever the textinput callback got the last frame, by storing that somewhere in the love.textinput callback. You then move that data into the active component's text buffer. The components would have their own buffer so it would not affect any other component's text field or whatever.
you can't oop-trickery yourself out of that like you wanted to.

Re: How to correctly read text from keyboard

Posted: Wed May 13, 2020 9:41 am
by pgimeno
cristoferfb wrote: Sun May 10, 2020 12:19 am and this is my code:

Code: Select all

[...]
function love.draw ()
	functionView:draw()
end
[...]
I'm quite confused by your code, because you should not be calling methods on classes, you should be calling them on objects. Class methods don't need colon syntax; only object methods do, similarly to how in C++ static methods use :: and object methods use . or ->.

cristoferfb wrote: Sun May 10, 2020 12:19 am EDIT:

As the title say I want to read text from keyboard, not how to read a key, I need emulate the behaviour of a input text in Android. Then, as u can see in the example code, I read about the love.textinput event handler but the class thats need it is inside of 2 parents class, so I obviusly can break some paradigms for take it more easy, but speaking of real projects with a certain complexity that needs order and nice practices, there is a relatively clean way of do it?
What you are asking here is a GUI input widget. Löve doesn't include a GUI; you need to implement it using the basic events. Often the GUI class hierarchy is somewhat separate from the game's; see my signature for an example of a GUI library (GSpöt) that perhaps can help orienting you.