Skip to content

Commit

Permalink
refactor to reduce duplication where it's complex
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Apr 21, 2024
1 parent 9f207d5 commit 545c9b6
Showing 1 changed file with 66 additions and 40 deletions.
106 changes: 66 additions & 40 deletions fancy_demo/fancy.asc
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ DynamicSprite* _create_textbox(int internal_width, int internal_height, Fancy9Pi
return spr;
}

DynamicSprite* _create_textbox_with_textspr(Fancy9Piece* f9p, int internal_width, int internal_height, int text_graphic)
DynamicSprite* _create_textbox_from_textgraphic(Fancy9Piece* f9p, int internal_width, int internal_height, int text_graphic)
{
if(f9p == null) return null;

Expand Down Expand Up @@ -621,6 +621,33 @@ int _get_speech_width(Character* c, int width)
return width;
}

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

int _process_fancy_string(FancyTextToken* tk_arr[], FancyState* fs, String text, int width, 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);

_adjust_state_sizes_to_config(fs, config); // fix outline and margin
return tk_count;
}

struct FancyStart {
FancyTextToken* tk_arr[];
FancyState* fs;
FancyConfig* config;
import void Init(FancyConfig* config);
};

// ----------------------------------------------------------------------
// ---------------- fancy module public interface -----------------------
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -655,6 +682,15 @@ FancyConfig* FancyConfig::Clone()
return cfg;
}

void FancyStart::Init(FancyConfig* config)
{
this.tk_arr = _NewTxtTok();
this.fs = NewState(0, 0);
this.config = config;
if(config == null)
this.config = _default_cfg.Clone();
}

FancyConfig* _get_speech_config(Character* c, FancyConfig* config)
{
if(config == null) {
Expand Down Expand Up @@ -779,30 +815,32 @@ Fancy9Piece* Fancy9Piece::Clone()

void DrawFancyString(this DrawingSurface*, int x, int y, const string text, FancyConfig* config, int width)
{
if(String.IsNullOrEmpty(text)) return;
if(config == null) config = _default_cfg.Clone();
FancyTextToken* tk_arr[] = _NewTxtTok();
FancyState* fs = NewState(x, y);
int tk_count = _parse_text(tk_arr, text, config);
tk_count = _do_word_wrapping(tk_arr, tk_count, fs, text, width);
FancyTextToken* tk_arr[]; FancyState* fs;
{
FancyStart ft; ft.Init(config);
config = ft.config; fs = ft.fs; tk_arr = ft.tk_arr;
}

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

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

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

DynamicSprite* CreateFromFancyString(static DynamicSprite, const string text, FancyConfig* config, int width)
{
if(String.IsNullOrEmpty(text)) return null;
if(config == null) config = _default_cfg.Clone();
FancyTextToken* tk_arr[] = _NewTxtTok();
FancyState* fs = NewState(0, 0);
int tk_count = _parse_text(tk_arr, text, config);
tk_count = _do_word_wrapping(tk_arr, tk_count, fs, text, width);
FancyTextToken* tk_arr[]; FancyState* fs;
{
FancyStart ft; ft.Init(config);
config = ft.config; fs = ft.fs; tk_arr = ft.tk_arr;
}

_adjust_state_sizes_to_config(fs, config); // fix outline and margin
int tk_count = _process_fancy_string(tk_arr, fs, text, width, config);
if(tk_count <= 0) return null;

DynamicSprite* spr = DynamicSprite.Create(fs.BoxWidth, fs.BoxHeight, true);
DrawingSurface* surf = spr.GetDrawingSurface();
_draw_tokens(tk_arr, tk_count, fs, surf, text, -1, width, config);
surf.Release();
DynamicSprite* spr = _create_text_sprite(tk_arr, tk_count, fs, text, -1, width, config);
return spr;
}

Expand Down Expand Up @@ -868,13 +906,13 @@ void Fancify(this Button*, Fancy9Piece* normal9p, Fancy9Piece* mouse_over9p, Fan
h = text_spr.Height;
}

spr_normal = _create_textbox_with_textspr(normal9p, w, h, text_spr.Graphic);
spr_normal = _create_textbox_from_textgraphic(normal9p, w, h, text_spr.Graphic);

if(spr_normal == null) {
spr_normal = text_spr;
} else {
spr_over = _create_textbox_with_textspr(mouse_over9p, w, h, text_spr.Graphic);
spr_pushed = _create_textbox_with_textspr(pushed9p, w, h, text_spr.Graphic);
spr_over = _create_textbox_from_textgraphic(mouse_over9p, w, h, text_spr.Graphic);
spr_pushed = _create_textbox_from_textgraphic(pushed9p, w, h, text_spr.Graphic);
}

_set_fancybtn(index, id, spr_normal, spr_over, spr_pushed);
Expand Down Expand Up @@ -926,7 +964,7 @@ void FancyTextBase::SetDrawingArea(int x, int y, int width)
this._width = width;
}

// FIX-ME: reduce code duplication in CreateFromFancyString and others...
// FIX-ME: reduce code duplication
protected void FancyTextBase::_set_text(String text)
{
if(this._cfg == null) { this._cfg = _default_cfg.Clone(); }
Expand All @@ -944,11 +982,7 @@ protected void FancyTextBase::_set_text(String text)
int width = this._width;

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

tk_count = _do_word_wrapping(tk_arr, tk_count, this._fs, text, width);

_adjust_state_sizes_to_config(this._fs, this._cfg); // fix outline and margin
int tk_count = _process_fancy_string(tk_arr, this._fs, text, width, this._cfg);

this._tk_arr = tk_arr;
this._tk_count = tk_count;
Expand Down Expand Up @@ -1006,22 +1040,18 @@ void FancyTextBox::set_Fancy9Piece(Fancy9Piece* value)

DynamicSprite* FancyTextBox::CreateTextBoxSprite()
{
DrawingSurface* surf;
if(String.IsNullOrEmpty(this._text) || this._tk_count <= 0) return null;
if(this._cfg == null) this._cfg = _default_cfg.Clone();

DynamicSprite* text_spr = DynamicSprite.Create(this._fs.BoxWidth, this._fs.BoxHeight, true);
surf = text_spr.GetDrawingSurface();
_draw_tokens(this._tk_arr, this._tk_count, this._fs, surf, this._text, -1, this._width, this._cfg);
surf.Release();
DynamicSprite* text_spr = _create_text_sprite(this._tk_arr, this._tk_count, this._fs, this._text, -1, this._width, this._cfg);

if(this._f9p == null) return text_spr;

int w = text_spr.Width;
int h = text_spr.Height;

DynamicSprite* box_spr = _create_textbox(w, h, this._f9p);
surf = box_spr.GetDrawingSurface();
DrawingSurface* surf = box_spr.GetDrawingSurface();

surf.DrawImage(this._f9p.BorderLeft, this._f9p.BorderTop, text_spr.Graphic);
surf.Release();
Expand All @@ -1032,22 +1062,18 @@ DynamicSprite* FancyTextBox::CreateTextBoxSprite()

DynamicSprite* FancyTypedText::CreateTypedSprite()
{
DrawingSurface* surf;
if(String.IsNullOrEmpty(this._text) || this._tk_count <= 0) return null;
if(this._cfg == null) this._cfg = _default_cfg.Clone();

DynamicSprite* text_spr = DynamicSprite.Create(this._fs.BoxWidth, this._fs.BoxHeight, true);
surf = text_spr.GetDrawingSurface();
_draw_tokens(this._tk_arr, this._typed_token_count, this._fs, surf, this._text, this._typed_token_len, this._width, this._cfg);
surf.Release();
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);

if(this._f9p == null) return text_spr;

int w = text_spr.Width;
int h = text_spr.Height;

DynamicSprite* box_spr = _create_textbox(w, h, this._f9p);
surf = box_spr.GetDrawingSurface();
DrawingSurface* surf = box_spr.GetDrawingSurface();

surf.DrawImage(this._f9p.BorderLeft, this._f9p.BorderTop, text_spr.Graphic);
surf.Release();
Expand Down Expand Up @@ -1103,7 +1129,7 @@ void FancyTypedText::Clear()

void FancyTypedText::Start(String text)
{
this.set_Text(text);
this._set_text(text);
this._typed_token_len = 0;
this._typed_token_count = 0;
this._is_typed_text = true;
Expand Down

0 comments on commit 545c9b6

Please sign in to comment.