Skip to content

Commit

Permalink
use string instead of pure chars and improved draw_control_frame
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Feb 19, 2021
1 parent b1c27d4 commit 64916a5
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions imgi_demo/imgi.asc
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ struct ImGi_Cmd
Rect* r;
int color;
int dst_idx;
int hash;
FontType font;
ImGi_Icon icon;
char str[MAX_TEXT_CMD_STR_SIZE];
String str;
};

managed struct ImGi_Layout
Expand Down Expand Up @@ -170,6 +171,8 @@ import ImGi_ID _ImGi_hash(String data, ImGi_ID id = 0);

ImGi_Cmd _ctx_stk_cmd_items[IMGI_COMMANDLIST_SIZE];
int _ctx_stk_cmd_index;
// adding this to make easier to write command stack iterations
#define CMD_IC _ctx_stk_cmd_items[ic]

struct ImGi_Context
{
Expand Down Expand Up @@ -948,10 +951,11 @@ void
ImGi_Context::Init()
{
int i;
for(i=0; i<IMGI_COMMANDLIST_SIZE; i++)
for(int ic=0; ic<IMGI_COMMANDLIST_SIZE; ic++)
{
// pre init all rects once so we can not do this when draw_text()
_ctx_stk_cmd_items[i].r = new Rect;
CMD_IC.r = new Rect;
CMD_IC.str = "";
}
/*
for(i=0; i<IMGI_ROOTLIST_SIZE; i++)
Expand Down Expand Up @@ -1146,7 +1150,6 @@ pop_clip_rect(this ImGi_Context*)
//

// <<-- Cmd /////////////////////////////////////////////////////////////
#define CMD_IC _ctx_stk_cmd_items[ic]

// Note: stk_cmd_push() operates reversely so we can avoid calling new ImGi_Cmd every frame.

Expand All @@ -1157,7 +1160,7 @@ push_jump(this ImGi_Context*, int dst_idx)
//ImGi_Cmd cmd = ;
CMD_IC.type = eImGi_CmdJump;
CMD_IC.dst_idx = dst_idx;
CMD_IC.str[0] = 0;
CMD_IC.str = "";

return _ctx_stk_cmd_index - 1;
}
Expand All @@ -1169,7 +1172,7 @@ set_clip(this ImGi_Context*, Rect* r)

CMD_IC.type = eImGi_CmdClip;
CMD_IC.r = r;
CMD_IC.str[0] = 0;
CMD_IC.str = "";
}

void
Expand All @@ -1184,7 +1187,7 @@ draw_rect(this ImGi_Context*, Rect* rect, int color)
CMD_IC.type = eImGi_CmdRect;
CMD_IC.r = r;
CMD_IC.color = color;
CMD_IC.str[0] = 0;
CMD_IC.str = "";
}
}

Expand Down Expand Up @@ -1231,11 +1234,8 @@ draw_text(this ImGi_Context*, int x, int y, int color, FontType font, String str

CMD_IC.type = eImGi_CmdText;
int len = _min(str.Length, MAX_TEXT_CMD_STR_SIZE - 1);
for (int i = 0; i < len; i++)
{
CMD_IC.str[i] = str.Chars[i];
}
CMD_IC.str[len] = 0;
CMD_IC.str = str;

//CMD_IC.r = new Rect; pre init in ::Init(), no need to call new
CMD_IC.r.x = x;
CMD_IC.r.y = y;
Expand Down Expand Up @@ -1265,7 +1265,7 @@ draw_icon(this ImGi_Context*, ImGi_Icon icon, Rect* r, int color)
CMD_IC.icon = icon;
CMD_IC.r = r;
CMD_IC.color = color;
CMD_IC.str[0] = 0;
CMD_IC.str = "";

/* reset clipping if it was set */
if (clipped)
Expand Down Expand Up @@ -1435,19 +1435,27 @@ draw_control_frame(this ImGi_Context*, ImGi_ID id, Rect* r, ImGi_Color colorid,
else if (this.hover == id)
colorid += 1;

this.draw_rect(r, this.style.colors[colorid]);
if (colorid == eImGi_Col_ScrollBase || colorid == eImGi_Col_ScrollThumb ||
colorid == eImGi_Col_TitleBG)
return;

int color = this.style.colors[eImGi_Col_Border];
if (!(colorid == eImGi_Col_ScrollBase || colorid == eImGi_Col_ScrollThumb ||
colorid == eImGi_Col_TitleBG))
{
int color = this.style.colors[eImGi_Col_Border];

/* draw border */
r = r.Expand(1);
this.draw_rect(Rect.Create(r.x + 1, r.y, r.w - 2, 1), color);
this.draw_rect(Rect.Create(r.x + 1, r.y + r.h - 1, r.w - 2, 1), color);
this.draw_rect(Rect.Create(r.x, r.y, 1, r.h), color);
this.draw_rect(Rect.Create(r.x + r.w - 1, r.y, 1, r.h), color);
/* draw border */
Rect* re = r.Expand(1);
if(r.w < 60)
{
this.draw_rect(Rect.Create(re.x, re.y, re.w, re.h), color);
}
else
{
this.draw_rect(Rect.Create(re.x + 1 , re.y , re.w - 2, 1 ), color);
this.draw_rect(Rect.Create(re.x + 1 , re.y + re.h - 1, re.w - 2, 1 ), color);
this.draw_rect(Rect.Create(re.x , re.y , 1 , re.h), color);
this.draw_rect(Rect.Create(re.x + re.w - 1, re.y , 1 , re.h), color);
}
}

this.draw_rect(r, this.style.colors[colorid]);
}

void
Expand Down Expand Up @@ -2374,13 +2382,13 @@ _ImGi_Calculate_Command_Hash()
hn = (hn ^ CMD_IC.r.h) * 16777619;
hn = (hn ^ CMD_IC.color) * 16777619;
hn = (hn ^ CMD_IC.icon) * 16777619;
if (CMD_IC.str[0] != 0)
if (CMD_IC.str.Length > 0)
{
// this is a hack but should work good enough, better than going through
// the whole string
String s = String.Format("%s", CMD_IC.str);
hn = (hn ^ s.Length) * 16777619;
hn = (hn ^ s.Chars[s.Length - 1]) * 16777619;

hn = (hn ^ CMD_IC.str.Length) * 16777619;
hn = (hn ^ CMD_IC.str.Chars[CMD_IC.str.Length - 1]) * 16777619;
// for(int k=0; k<s.Length; k++)
//{
// hn = (hn ^ s.Chars[k])* 16777619;
Expand Down

0 comments on commit 64916a5

Please sign in to comment.