Skip to content

Commit

Permalink
TextEdit; add display_char for password type controls
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby0x1 committed Aug 23, 2015
1 parent 6023d8b commit e353f0a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
66 changes: 61 additions & 5 deletions mint/TextEdit.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ typedef TextEditOptions = {

/** The default text value */
@:optional var text: String;
/** An override display character (like * for a password entry) */
@:optional var display_char: String;
/** The text size of the text for the rendering to use */
@:optional var text_size: Float;
/** A filter function to call when text is entered.
It passes `new character`, `new text`, `old text`.
Returning false will reject the character */
It is: `filter(new_character, new_text, old_text):Bool`.
Returning false will reject the character. */
@:optional var filter: String->String->String->Bool;

} //TextEditOptions
Expand All @@ -39,12 +41,15 @@ class TextEdit extends Control {
public var filter : String->String->String->Bool;
@:isVar public var index (default, null) : Int = 0;
public var text (get, set): String;
public var display_text (get, never): String;
@:isVar public var display_char (get, set): String;

/** Emitted whenever the index is changed. */
public var onchangeindex: Signal<Int->Void>;
public var onchange: Signal<String->Void>;

var edit : String = '';
var display : String = '';
var options: TextEditOptions;

public function new( _options:TextEditOptions ) {
Expand Down Expand Up @@ -83,6 +88,10 @@ class TextEdit extends Control {

renderer = rendering.get(TextEdit, this);

if(options.display_char != null) {
display_char = options.display_char;
}

refresh(edit);

oncreate.emit();
Expand Down Expand Up @@ -137,12 +146,24 @@ class TextEdit extends Control {

} //onkeydown

inline function refresh( str:String ) {
inline function refresh( str:String, _no_change:Bool=false ) {

edit = str;

if(display_char != null) {
var _l = str.uLength();
display = '';
for(i in 0 ... _l) display += display_char;
} else {
display = edit;
}

label.text = edit = str;
label.text = display;
update_cur();

onchange.emit(edit);
if(!_no_change) {
onchange.emit(edit);
}

return edit;

Expand All @@ -152,11 +173,34 @@ class TextEdit extends Control {
return edit;
}

inline function get_display_text() {
return display;
}

inline function get_display_char() {
return display_char;
}

inline function set_text(v:String) {
index = v.uLength();
return refresh(v);
}

inline function set_display_char(v:String) {

if(v != null) {
display_char = v.uCharAt(0);
} else {
display_char = v;
}

refresh(edit, true);
update_cur();

return display_char;

} //set_display_char

function move(amount:Int = -1) {

index += amount;
Expand Down Expand Up @@ -189,6 +233,18 @@ class TextEdit extends Control {

} //before

inline function after_display( cur:Int = 0 ) {

return display.uSubstr(cur, display.length);

} //after

inline function before_display( cur:Int = 0 ) {

return display.uSubstr(0, cur);

} //before

inline function update_cur() {

onchangeindex.emit(index);
Expand Down
2 changes: 1 addition & 1 deletion mint/render/luxe/TextEdit.hx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class TextEdit extends mint.render.Render {
function update_cursor() {

var text = (cast textedit.label.renderer:mint.render.luxe.Label).text;
var _t = textedit.before(textedit.index);
var _t = textedit.before_display(textedit.index);

var _tw = text.font.width_of(textedit.edit, text.point_size, text.letter_spacing);
var _twh = _tw/2.0;
Expand Down
8 changes: 7 additions & 1 deletion tests/test_luxe/src/tests/KitchenSink.hx
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,13 @@ class KitchenSink extends State {
if(e.keycode == Key.key_2) if(window2 != null) window2.open();
if(e.keycode == Key.key_3) if(window3 != null) window3.open();
if(e.keycode == Key.key_4) if(check != null) check.visible = !check.visible;
if(e.keycode == Key.key_5) text1.text = 'fps:' + luxe.utils.Maths.fixed((1/Luxe.debug.dt_average), 2);
if(e.keycode == Key.key_5) {
if( text1.display_char == null ) {
text1.display_char = '';
} else {
text1.display_char = null;
}
}

} //onkeyup

Expand Down

0 comments on commit e353f0a

Please sign in to comment.