| 63 | } |
| 64 | |
| 65 | bool TextInputNode::init(float width, float height, std::string_view placeholder, std::string_view font, int scale) |
| 66 | { |
| 67 | if (!Layer::init()) |
| 68 | return false; |
| 69 | |
| 70 | _placeholderString = placeholder; |
| 71 | |
| 72 | this->setContentSize({ width, height }); |
| 73 | |
| 74 | _textField = ui::TextField::create(placeholder, "Thonburi", scale); |
| 75 | _textField->setVisible(false); |
| 76 | this->addChild(_textField); |
| 77 | |
| 78 | if (!font.empty()) |
| 79 | { |
| 80 | _cursor = Label::createWithBMFont(font, "|"); |
| 81 | _cursor->setBMFontSize(scale); |
| 82 | _cursor->setOpacity(150); |
| 83 | _cursor->setPositionY(height / 2); |
| 84 | _cursor->setAnchorPoint({ -0.5, 0.5 }); |
| 85 | _cursor->setVisible(false); |
| 86 | this->addChild(_cursor); |
| 87 | |
| 88 | _displayedLabel = Label::createWithBMFont(font, placeholder, TextHAlignment::CENTER, 0); |
| 89 | _displayedLabel->setBMFontSize(scale); |
| 90 | _displayedLabel->setPositionY(height / 2); |
| 91 | _displayedLabel->setPositionX(width / 2); |
| 92 | _displayedLabel->setAnchorPoint({ 0.5, 0.5 }); |
| 93 | this->addChild(_displayedLabel); |
| 94 | |
| 95 | updateDisplayedLabel(); |
| 96 | } |
| 97 | |
| 98 | auto touchListener = EventListenerTouchOneByOne::create(); |
| 99 | _textField->addEventListener([&](ax::Object* re, ui::TextField::EventType event) |
| 100 | { |
| 101 | switch (event) |
| 102 | { |
| 103 | case ui::TextField::EventType::DETACH_WITH_IME: |
| 104 | onTextFieldDetachWithIME(); |
| 105 | break; |
| 106 | case ui::TextField::EventType::ATTACH_WITH_IME: |
| 107 | onTextFieldAttachWithIME(); |
| 108 | break; |
| 109 | default: // INSERT_TEXT / DELETE_BACKWARD |
| 110 | onTextFieldChanged(); |
| 111 | } |
| 112 | }); |
| 113 | |
| 114 | touchListener->onTouchBegan = AX_CALLBACK_2(TextInputNode::onTouchBegan, this); |
| 115 | |
| 116 | #ifdef AX_PLATFORM_PC |
| 117 | auto listener = EventListenerKeyboard::create(); |
| 118 | |
| 119 | listener->onKeyPressed = AX_CALLBACK_2(TextInputNode::onKeyPressed, this); |
| 120 | listener->onKeyReleased = AX_CALLBACK_2(TextInputNode::onKeyReleased, this); |
| 121 | |
| 122 | this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); |
no test coverage detected