Skip to content

Commit

Permalink
prevent using destroyed graphic on (legacy) FlxInputText (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli authored Oct 2, 2024
1 parent a5d07fe commit 2756177
Showing 1 changed file with 57 additions and 16 deletions.
73 changes: 57 additions & 16 deletions flixel/addons/ui/FlxInputText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,49 @@ class FlxInputText extends FlxText
}

/**
* Draw the caret in addition to the text.
* Draw the background, border and caret in addition to the text.
*/
override public function draw():Void
{
regenGraphic();
checkEmptyFrame();

if (alpha == 0 || _frame.type == EMPTY)
return;

if (dirty) // rarely
calcFrame(useFramePixels);

drawSprite(fieldBorderSprite);
drawSprite(backgroundSprite);

super.draw();


for (camera in getCamerasLegacy())
{
if (!camera.visible || !camera.exists || !isOnScreen(camera))
continue;

if (isSimpleRender(camera))
drawSimple(camera);
else
drawComplex(camera);

#if FLX_DEBUG
FlxBasic.visibleCount++;
#end
}

// In case caretColor was changed
if (caretColor != caret.color || caret.height != size + 2)
{
caret.color = caretColor;
}

drawSprite(caret);

#if FLX_DEBUG
if (FlxG.debugger.drawDebug)
drawDebug();
#end
}

/**
Expand Down Expand Up @@ -690,8 +717,9 @@ class FlxInputText extends FlxText
// Generate the properly sized caret and also draw a border that matches that of the textfield (if a border style is set)
// borderQuality can be safely ignored since the caret is always a rectangle

final caretHeight = Std.int(size + 2);
var cw:Int = caretWidth; // Basic size of the caret
var ch:Int = Std.int(size + 2);
var ch:Int = caretHeight;

// Make sure alpha channels are correctly set
var borderC:Int = (0xff000000 | (borderColor & 0x00ffffff));
Expand All @@ -705,29 +733,42 @@ class FlxInputText extends FlxText
// No border, just make the caret
caret.makeGraphic(cw, ch, caretC, false, caretKey);
caret.offset.x = caret.offset.y = 0;

case SHADOW:
// Shadow offset to the lower-right
cw += Std.int(borderSize);
ch += Std.int(borderSize); // expand canvas on one side for shadow
final absSize = Math.abs(borderSize);
cw += Std.int(absSize);
ch += Std.int(absSize); // expand canvas on one side for shadow
caret.makeGraphic(cw, ch, FlxColor.TRANSPARENT, false, caretKey); // start with transparent canvas
var r:Rectangle = new Rectangle(borderSize, borderSize, caretWidth, Std.int(size + 2));
final r:Rectangle = new Rectangle(absSize, absSize, caretWidth, caretHeight);
caret.pixels.fillRect(r, borderC); // draw shadow
r.x = r.y = 0;
caret.pixels.fillRect(r, caretC); // draw caret
caret.offset.x = caret.offset.y = 0;


case SHADOW_XY(shadowX, shadowY):
// Shadow offset to the lower-right
cw += Std.int(Math.abs(shadowX));
ch += Std.int(Math.abs(shadowY)); // expand canvas on one side for shadow
caret.makeGraphic(cw, ch, FlxColor.TRANSPARENT, false, caretKey); // start with transparent canvas
final r:Rectangle = new Rectangle(Math.max(0, shadowX), Math.max(0, shadowY), caretWidth, caretHeight);
caret.pixels.fillRect(r, borderC); // draw shadow
r.x -= shadowX;
r.y -= shadowY;
caret.pixels.fillRect(r, caretC); // draw caret
caret.offset.x = shadowX < 0 ? -shadowX : 0;
caret.offset.y = shadowY < 0 ? -shadowY : 0;

case OUTLINE_FAST, OUTLINE:
// Border all around it
cw += Std.int(borderSize * 2);
ch += Std.int(borderSize * 2); // expand canvas on both sides
final absSize = Math.abs(borderSize);
cw += Std.int(absSize * 2);
ch += Std.int(absSize * 2); // expand canvas on both sides
caret.makeGraphic(cw, ch, borderC, false, caretKey); // start with borderColor canvas
var r = new Rectangle(borderSize, borderSize, caretWidth, Std.int(size + 2));
final r = new Rectangle(absSize, absSize, caretWidth, caretHeight);
caret.pixels.fillRect(r, caretC); // draw caret
// we need to offset caret's drawing position since the caret is now larger than normal
caret.offset.x = caret.offset.y = borderSize;

case _: //temp fix for 5.9.0
caret.offset.x = caret.offset.y = absSize;
}
// Update width/height so caret's dimensions match its pixels
caret.width = cw;
Expand Down

0 comments on commit 2756177

Please sign in to comment.