Skip to content

Commit

Permalink
move width internally to FancyState
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Apr 21, 2024
1 parent afc8e5d commit 6692742
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
47 changes: 23 additions & 24 deletions fancy_demo/fancy.asc
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,13 @@ int _parse_text(FancyTextToken* txttk_arr[], String text, FancyConfig* cfg)
return txttk_count;
}

int _do_word_wrapping(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, String text, int width)
int _do_word_wrapping(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, String text)
{
int line_height[MAX_LINES];
int line_width[MAX_LINES];
int line = 0;
int w = 0;
int width = fs.MaxTextWidth;

for (int i = 0; i < tk_count; i++) {
FancyTextToken* t = tk_arr[i];
Expand Down Expand Up @@ -493,7 +494,7 @@ int _get_align_y(VerticalAlignment valign, int y, FancyTextToken* t)
}

// Write text tokens on a surface
void _draw_tokens(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, DrawingSurface* surf, String text, int partial, int width, FancyConfig* cfg)
void _draw_tokens(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, DrawingSurface* surf, String text, int partial, FancyConfig* cfg)
{
if(tk_count <= 0) return;
if(fs == null) return;
Expand Down Expand Up @@ -620,21 +621,21 @@ DynamicSprite* _create_textbox_from_text_spr(DynamicSprite* text_spr, Fancy9Piec
return _create_textbox_from_textgraphic(f9p, text_spr.Width, text_spr.Height, text_spr.Graphic);
}

DynamicSprite* _create_text_sprite(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, String text, int typed_token_len, int width, FancyConfig* cfg)
DynamicSprite* _create_text_sprite(FancyTextToken* tk_arr[], int tk_count, FancyState* fs, String text, int typed_token_len, FancyConfig* cfg)
{
DynamicSprite* text_spr = DynamicSprite.Create(fs.TextWidth, fs.TextHeight, true);
DrawingSurface* surf = text_spr.GetDrawingSurface();
_draw_tokens(tk_arr, tk_count, fs, surf, text, typed_token_len, width, cfg);
_draw_tokens(tk_arr, tk_count, fs, surf, text, typed_token_len, cfg);
surf.Release();
return text_spr;
}

int _process_fancy_string(FancyTextToken* tk_arr[], FancyState* fs, String text, int width, FancyConfig* config)
int _process_fancy_string(FancyTextToken* tk_arr[], FancyState* fs, String text, FancyConfig* config)
{
if(String.IsNullOrEmpty(text))
return 0;
int tk_count = _parse_text(tk_arr, text, config);
tk_count = _do_word_wrapping(tk_arr, tk_count, fs, text, width);
tk_count = _do_word_wrapping(tk_arr, tk_count, fs, text);

_adjust_state_sizes_to_config(fs, config); // fix outline and margin
return tk_count;
Expand Down Expand Up @@ -802,14 +803,13 @@ void DrawFancyString(this DrawingSurface*, int x, int y, const string text, Fanc
FancyState* fs = NewState();
if(config == null)
config = _default_cfg.Clone();

fs.X = x; fs.Y = y; fs.MaxTextWidth = width;


fs.X = x; fs.Y = y;

int tk_count = _process_fancy_string(tk_arr, fs, text, width, config);
int tk_count = _process_fancy_string(tk_arr, fs, text, config);
if(tk_count <= 0) return;

_draw_tokens(tk_arr, tk_count, fs, this, text, -1, width, config);
_draw_tokens(tk_arr, tk_count, fs, this, text, -1, config);
}

DynamicSprite* CreateFromFancyString(static DynamicSprite, const string text, FancyConfig* config, int width)
Expand All @@ -818,11 +818,13 @@ DynamicSprite* CreateFromFancyString(static DynamicSprite, const string text, Fa
FancyState* fs = NewState();
if(config == null)
config = _default_cfg.Clone();

fs.MaxTextWidth = width;

int tk_count = _process_fancy_string(tk_arr, fs, text, width, config);
int tk_count = _process_fancy_string(tk_arr, fs, text, config);
if(tk_count <= 0) return null;

return _create_text_sprite(tk_arr, tk_count, fs, text, -1, width, config);
return _create_text_sprite(tk_arr, tk_count, fs, text, -1, config);
}

DynamicSprite* CreateFromFancyTextBox(static DynamicSprite, const string text, FancyConfig* config, int width, Fancy9Piece* f9p)
Expand Down Expand Up @@ -931,14 +933,13 @@ void FancyTextBase::SetDrawingArea(int x, int y, int width)

this._fs.X = x;
this._fs.Y = y;
this._width = width;
this._fs.MaxTextWidth = width;
}

// FIX-ME: reduce code duplication
protected void FancyTextBase::_set_text(String text)
{
if(this._cfg == null) { this._cfg = _default_cfg.Clone(); }
if(this._width <= 0) { this._width = FANCY_INFINITE_WIDTH; }
if(this._fs == null) { this._fs = NewState(); }
if(String.IsNullOrEmpty(text)) {
this._text = "";
Expand All @@ -949,10 +950,10 @@ protected void FancyTextBase::_set_text(String text)

int font = this._cfg.Font;
int color = this._cfg.TextColor;
int width = this._width;
if(this._fs.MaxTextWidth <= 0) { this._fs.MaxTextWidth = FANCY_INFINITE_WIDTH; }

FancyTextToken* tk_arr[] = _NewTxtTok();
int tk_count = _process_fancy_string(tk_arr, this._fs, text, width, this._cfg);
int tk_count = _process_fancy_string(tk_arr, this._fs, text, this._cfg);

this._tk_arr = tk_arr;
this._tk_count = tk_count;
Expand Down Expand Up @@ -983,10 +984,9 @@ void FancyTextBase::Draw(DrawingSurface* surf)
{
FancyTextToken* tk_arr[] = this._tk_arr;
int tk_count = this._tk_count;
int width = this._width;
String text = this._text;

_draw_tokens(tk_arr, tk_count, this._fs, surf, text, -1, width, this._cfg);
_draw_tokens(tk_arr, tk_count, this._fs, surf, text, -1, this._cfg);
}

Fancy9Piece* FancyTextBox::get_Fancy9Piece()
Expand All @@ -1013,7 +1013,7 @@ DynamicSprite* FancyTextBox::CreateTextBoxSprite()
if(String.IsNullOrEmpty(this._text) || this._tk_count <= 0) return null;
if(this._cfg == null) this._cfg = _default_cfg.Clone();

DynamicSprite* text_spr = _create_text_sprite(this._tk_arr, this._tk_count, this._fs, this._text, -1, this._width, this._cfg);
DynamicSprite* text_spr = _create_text_sprite(this._tk_arr, this._tk_count, this._fs, this._text, -1, this._cfg);

return _create_textbox_from_text_spr(text_spr, this._f9p);
}
Expand All @@ -1023,7 +1023,7 @@ DynamicSprite* FancyTypedText::CreateTypedSprite()
if(String.IsNullOrEmpty(this._text) || this._tk_count <= 0) return null;
if(this._cfg == null) this._cfg = _default_cfg.Clone();

DynamicSprite* text_spr = _create_text_sprite(this._tk_arr, this._typed_token_count, this._fs, this._text, this._typed_token_len, this._width, this._cfg);
DynamicSprite* text_spr = _create_text_sprite(this._tk_arr, this._typed_token_count, this._fs, this._text, this._typed_token_len, this._cfg);

return _create_textbox_from_text_spr(text_spr, this._f9p);
}
Expand Down Expand Up @@ -1061,7 +1061,7 @@ void FancyTypedText::Tick()

void FancyTypedText::DrawTyped(DrawingSurface* surf)
{
_draw_tokens(this._tk_arr, this._typed_token_count, this._fs, surf, this._text, this._typed_token_len, this._width, this._cfg);
_draw_tokens(this._tk_arr, this._typed_token_count, this._fs, surf, this._text, this._typed_token_len, this._cfg);
}

void FancyTypedText::Clear()
Expand Down Expand Up @@ -1182,7 +1182,7 @@ void _fancy_say_update()
curSpeech.Y = _adjust_speech_y(curSpeech.Y);
_say_ovr_tmp = Overlay.CreateGraphical(curSpeech.X, curSpeech.Y, _sayspr.Graphic, true, true);
_say_ovr_tmp.Transparency = 100;
}
}
}

void game_start()
Expand All @@ -1191,7 +1191,6 @@ void game_start()
_default_f9p = Fancy9Piece.CreateFromTextWindowGui(null);
}


function late_repeatedly_execute_always() {
_fancy_say_update();
}
3 changes: 1 addition & 2 deletions fancy_demo/fancy.ash
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ managed struct FancyTextToken {
};

managed struct FancyState {
int X, Y, TextWidth, TextHeight;
int X, Y, TextWidth, TextHeight, MaxTextWidth;
};

#define FANCY_INFINITE_WIDTH 65536
Expand Down Expand Up @@ -110,7 +110,6 @@ struct FancyTextBase {
protected FancyState* _fs;
protected int _tk_count;
protected FancyConfig* _cfg;
protected int _width;
protected Fancy9Piece* _f9p;
bool _is_typed_text; // $AUTOCOMPLETEIGNORE$
};
Expand Down

0 comments on commit 6692742

Please sign in to comment.