TextField¶
It’s a class and is used to create TextFields.
This is base class for IntField and FloatField
code:
from boss.ui_creator import UICreator, RectData, TextField, Boss_OT_base_ui
def onTextChanged(caller: TextField):
print('textChanged', f'value - {caller.value}, text - {caller.text}')
def onValueChanged(caller: TextField):
print('valueChanged', f'value - {caller.value}, text - {caller.text}')
def onEnterPressed(caller: TextField):
print('enterPressed', f'value - {caller.value}, text - {caller.text}')
def ui_elements(op: Boss_OT_base_ui):
# UICreator.deleteAllUi(op) # to delete all existing ui
btn_width, btn_height = 150, 40
rd = RectData(
op.uip.mouse_x,
op.uip.mouse_y - btn_height,
btn_width,
btn_height
)
textField = UICreator.textField(
op,
rectData=rd,
onTextChange=onTextChanged,
onValueChange=onValueChanged,
onEnterPress=onEnterPressed
)
# these are methods for adding callback after creation
# textField.add_onTextChange(onTextChanged)
# textField.add_onValueChange(onValueChanged)
# textField.add_onEnterPress(onEnterPressed)
TextField has two properties,
text
andvalue
.text
is always ofstr
type and is equal to displayed text. In case of TextFieldvalue
is also of typestr
.value
is committedtext
. You commit by pressing Enter.When user clicks on the TextField, input mode changes and is listening for keyboard Input instead of mouse input. You can come out of Keyboard input mode by pressing either Enter or Escape.
When you press Enter , you commit
text
tovalue
, meaning, displayed text becomes new value.When you press Escape , you cancel
text
and oldvalue
remains.
Warning
When typing in textfield, you should remember to exit out of it by pressing ESC or ENTER. (Mouse right click don’t work)
Events:
- onTextChange:
this is called whenever user types something to change text. eg. search suggestion feature, filtering the result as user types.
- onEnterPress:
when user presses Enter key, this event is called. eg. load something when user had done typing.
- onValueChange:
this is called when user presses Enter and new text value is different from old text.
Notes:
Its based on Blender’s operator events, so all the keys are checked for press. HOME, END, CTRL+V (Paste) and normal navigation like arrow keys work to some extent but there are some known bugs.
Just for curious people , Cursor blinking is implemented using wm.event_timer_add as described in python templates examples. So a Panels visibility is toggled in a interval. (There are no Threads or Timer yet)
Footnotes:
In future updates, this will be modified a lot, the way it works now, gets the Job done.