Skip to content

Commit

Permalink
Merge pull request #47 from labuzm/text_input
Browse files Browse the repository at this point in the history
introduce text_input event
  • Loading branch information
maniek2332 committed Dec 10, 2019
2 parents cdf9e1a + e04ba35 commit a8ab10f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
11 changes: 9 additions & 2 deletions demos/fonts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self):

self.text_node = TextNode(font=self.my_font, content="Hello World", font_size=1.)
self.root.add_child(self.text_node)
self.text_buffer = []

def update(self, dt):
for event in self.input.events():
Expand All @@ -19,8 +20,16 @@ def update(self, dt):

keyboard = event.keyboard
if keyboard:
if keyboard.text_input:
self.text_buffer.append(keyboard.text)
print('Text: {}'.format(''.join(self.text_buffer)))

if keyboard.is_pressing(Keycode.q):
self.engine.quit()
elif keyboard.is_pressing(Keycode.backspace):
if self.text_buffer:
self.text_buffer.pop()
print('Text: {}'.format(''.join(self.text_buffer)))
elif keyboard.is_pressing(Keycode.l):
self.text_node.content += "!"
elif keyboard.is_pressing(Keycode.kp_7):
Expand Down Expand Up @@ -58,8 +67,6 @@ def update(self, dt):
elif keyboard.is_pressing(Keycode.f):
self.camera.position = self.text_node.position

print("Mouse position: {}".format(self.input.mouse.get_position()))


if __name__ == '__main__':
engine = Engine(virtual_resolution=Vector(10, 10))
Expand Down
9 changes: 9 additions & 0 deletions kaa/input.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class EventType(IntEnum):

key_down = <uint32_t>CEventType.key_down
key_up = <uint32_t>CEventType.key_up
text_input = <uint32_t>CEventType.text_input

mouse_motion = <uint32_t>CEventType.mouse_motion
mouse_button_up = <uint32_t>CEventType.mouse_button_up
Expand Down Expand Up @@ -488,6 +489,14 @@ cdef class KeyboardEvent(_BaseEvent):
cdef KeyboardEvent instance = KeyboardEvent.__new__(KeyboardEvent)
instance.c_event = c_event
return instance

@typed_property(EventType.text_input)
def text_input(self):
return self.c_event.keyboard().text_input()

@property
def text(self):
return self.c_event.keyboard().text().decode('utf-8')

def is_pressing(self, kc not None):
return self.c_event.keyboard().is_pressing(
Expand Down
5 changes: 5 additions & 0 deletions kaa/kaacore/input.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ cdef extern from "kaacore/input.h" nogil:
clipboard_updated "kaacore::EventType::clipboard_updated",
key_down "kaacore::EventType::key_down",
key_up "kaacore::EventType::key_up",
text_input "kaacore::EventType::text_input",
mouse_motion "kaacore::EventType::mouse_motion",
mouse_button_down "kaacore::EventType::mouse_button_down",
mouse_button_up "kaacore::EventType::mouse_button_up",
Expand Down Expand Up @@ -390,6 +391,10 @@ cdef extern from "kaacore/input.h" nogil:
except +raise_py_error
bint is_releasing(CKeycode kc) \
except +raise_py_error
bint text_input() \
except +raise_py_error
string text() \
except +raise_py_error

cdef cppclass CMouseEvent "kaacore::MouseEvent":
bint button() \
Expand Down
2 changes: 1 addition & 1 deletion kaacore

0 comments on commit a8ab10f

Please sign in to comment.