Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Text Input/Editing to EventHandler #23

Closed
ldicker83 opened this issue Mar 8, 2017 · 1 comment
Closed

Add Text Input/Editing to EventHandler #23

ldicker83 opened this issue Mar 8, 2017 · 1 comment
Labels
Milestone

Comments

@ldicker83
Copy link
Member

ldicker83 commented Mar 8, 2017

The EventHandler currently has no notion of text input. This required a KeyTranslator object that would take basic keyboard events and translate them into ASCII characters.

SDL2.0 introduced text editing/input events that handle this far better than the KeyTranslator. It stands to reason then that EventHandler should adopt similar functionality.

Implementation Details

The EventHandler will need to add functions to enable and disable text input/editing and poll the current input editing state:

void EventHandler::textInputMode(bool); // true to turn on, false to turn off.
bool EventHandler::textInputMode(); // Queries the input state

Event signal:

NAS2D::Signals::Signal1<std::string> TextInputEventCallback;

Potential for Defects

The option to use a std::string by value vs. by reference here is intended to prevent inevitable mistakes. While passing by reference would work normally for typical uses (by the time the local string object is destroyed the signal observer will have already copied the string) and would certainly be more efficient, there will be times when someone will attempt to maintain a reference to the std::string either as a reference or as a pointer. In these cases the application will eventually segfault because as soon as the Signal1<std::string&>::Emit() function returns the memory for the std::stringwill be automatically deallocated resulting in dangling pointers/references.

While this is generally inefficient due to the number of c'tors and copy operations, it ensures correct behavior. Additionally, reasonably good optimizing compilers can eliminate a lot of the overhead in release builds.

@ldicker83 ldicker83 changed the title Add EventHandler::textInputMode(bool) to EventHandler Add Text Input/Editing to EventHandler Mar 8, 2017
@ldicker83 ldicker83 added the task label Mar 13, 2017
@ldicker83 ldicker83 added this to the v1.3.0 milestone Mar 13, 2017
ldicker83 added a commit that referenced this issue Mar 13, 2017
- Removed KeyTranslator object from NAS2D as implementation of #23 makes it obsolete.
- Moved KeyTranslator::shift(), KeyTranslator::control() and KeyTranslator::numlock() to EventHandler.
- Added EventHandler::query_shift(), EventHandler::query_control() and EventHandler::query_numlock() to EventHandler.
@ldicker83
Copy link
Member Author

This was surprisingly simple to implement and is also surprisingly effective. It's definitely a lot more effective than the previous method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant