Skip to content

Instantly share code, notes, and snippets.

@stevetranby
Last active November 27, 2017 01:49
Show Gist options
  • Save stevetranby/2b68b9f6a1b27e9fdf34 to your computer and use it in GitHub Desktop.
Save stevetranby/2b68b9f6a1b27e9fdf34 to your computer and use it in GitHub Desktop.
Adding cursor support to ui::TextField
//
// STTextField.cpp
// scgamex
//
// Created by Steve Tranby on 7/11/15.
//
//
#include "stdafx.h"
#include "extensions/STTextField.h"
USING_NS_CC;
STTextField::STTextField() : _textField(nullptr) {}
STTextField::~STTextField() {}
STTextField* STTextField::create(const std::string& placeholder, const std::string&, float fontSize, const std::string &imageFileName, TextureResType texType)
{
STTextField* widget = new (std::nothrow) STTextField();
if (widget && widget->init(placeholder, kFontVisitor, fontSize, imageFileName, texType))
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
bool STTextField::init(const std::string& placeholder, const std::string&, float fontSize, const std::string &imageFileName, TextureResType texType)
{
if(! ImageView::init(imageFileName, texType)) {
return false;
}
// create textfield
_textField = TextFieldTTF::textFieldWithPlaceHolder(placeholder, cocos2d::Size(0,0), TextHAlignment::LEFT, kFontVisitor, fontSize);
_textField->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
_textField->setTextColor(Color4B::WHITE);
// _textField->setPlaceHolderColor(Color4B::WHITE);
_textField->setPlaceHolder("Touch to Edit");
// _textField->setMaxLength(20);
// _textField->setMaxLengthEnabled(true);
_textField->setCursorChar('|');
_textField->setCursorEnabled(true);
_textField->setNormalizedPosition(Vec2(.03f,.52f));
_textField->setTag(1);
addChild(_textField);
setTouchEnabled(true);
return true;
}
void STTextField::onEnter()
{
ImageView::onEnter();
}
void STTextField::onExit()
{
ImageView::onExit();
}
bool STTextField::hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p2) const
{
if (_textField) {
auto p = convertToNodeSpace(pt);
auto bboxSize = getBoundingBox().size;
log("lbl->nsp(%s) = %s, lbl->bbox = %s", CStrFromPoint(pt), CStrFromPoint(p), CStrFromSize(bboxSize));
auto fontSize = 20.f;
if(cocos2d::Rect(0, 0, bboxSize.width + fontSize, bboxSize.height).containsPoint(p))
{
// move cursor
// fraction of width
auto xrel = clampf(p.x / bboxSize.width, 0.f, 1.f);
log("xrel = %f", xrel);
float maxIndex = _textField->getStringLength();
auto letterIndex = (int)(xrel / (1.f / maxIndex) + .5f);
log("letterindex = %d", letterIndex);;
_textField->attachWithIME();
_textField->setCursorFromPoint(pt, camera);
}
else
{
_textField->detachWithIME();
}
}
return ImageView::hitTest(pt, camera, p2);
}
void STTextField::moveCursorToEnd()
{
_textField->setCursorPosition(_textField->getStringLength());
}
//
// STTextField.h
// scgamex
//
// Created by Steve Tranby on 7/11/15.
//
//
#pragma once
#include <ui/CocosGUI.h>
class STTextField : public cocos2d::ui::ImageView
{
public:
STTextField();
virtual ~STTextField();
static STTextField* create(const std::string& placeholder, const std::string& fontName, float fontSize, const std::string& imageFileName, TextureResType texType = TextureResType::LOCAL);
virtual bool init(const std::string& placeholder, const std::string& fontName, float fontSize, const std::string& imageFileName, TextureResType texType = TextureResType::LOCAL);
///////////////////
void onEnter() override;
void onExit() override;
bool hitTest(const cocos2d::Vec2 &pt, const cocos2d::Camera* camera, cocos2d::Vec3 *p2) const override;
///////////////////
void moveCursorToEnd();
// PASSTHROUGH
void attachWithIME() { if(_textField) _textField->attachWithIME(); }
std::string getString() { return _textField ? _textField->getString() : ""; }
void setString(std::string str) { if(_textField) _textField->setString(str); }
protected:
cocos2d::TextFieldTTF* _textField;
cocos2d::Label* _cursorLabel;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment