Simplify textgroupwidget's code

This commit is contained in:
UnknownShadow200 2019-06-08 08:56:40 +10:00
parent 8b24029dca
commit 8603b7f6f3

View File

@ -2161,72 +2161,54 @@ void PlayerListWidget_Create(struct PlayerListWidget* w, const FontDesc* font, b
*-----------------------------------------------------TextGroupWidget-----------------------------------------------------* *-----------------------------------------------------TextGroupWidget-----------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void TextGroupWidget_ShiftUp(struct TextGroupWidget* w) { void TextGroupWidget_ShiftUp(struct TextGroupWidget* w) {
int last, i, y; int last, i;
Gfx_DeleteTexture(&w->textures[0].ID); Gfx_DeleteTexture(&w->textures[0].ID);
last = w->lines - 1; last = w->lines - 1;
/* Move i'th line to i'th - 1 line */ for (i = 0; i < last; i++) {
for (i = 0, y = w->y; i < last; i++) {
w->textures[i] = w->textures[i + 1]; w->textures[i] = w->textures[i + 1];
w->textures[i].Y = y;
y += w->textures[i].Height;
} }
w->textures[last].ID = GFX_NULL; /* Delete() called by TextGroupWidget_Redraw otherwise */ w->textures[last].ID = GFX_NULL; /* Delete() called by TextGroupWidget_Redraw otherwise */
TextGroupWidget_Redraw(w, last); TextGroupWidget_Redraw(w, last);
} }
void TextGroupWidget_ShiftDown(struct TextGroupWidget* w) { void TextGroupWidget_ShiftDown(struct TextGroupWidget* w) {
int last, i, y; int last, i;
last = w->lines - 1; last = w->lines - 1;
Gfx_DeleteTexture(&w->textures[last].ID); Gfx_DeleteTexture(&w->textures[last].ID);
/* Move i'th line to i'th - 1 line */ for (i = last; i > 0; i--) {
for (i = last, y = w->textures[last].Y; i > 0; i--) {
w->textures[i] = w->textures[i - 1]; w->textures[i] = w->textures[i - 1];
w->textures[i].Y = y;
y -= w->textures[i].Height;
} }
w->textures[0].ID = GFX_NULL; /* Delete() called by TextGroupWidget_Redraw otherwise */ w->textures[0].ID = GFX_NULL; /* Delete() called by TextGroupWidget_Redraw otherwise */
TextGroupWidget_Redraw(w, 0); TextGroupWidget_Redraw(w, 0);
} }
static int TextGroupWidget_CalcY(struct TextGroupWidget* w, int index, int newHeight) { static void TextGroupWidget_UpdateY(struct TextGroupWidget* w) {
struct Texture* textures = w->textures; struct Texture* textures = w->textures;
int deltaY = newHeight - textures[index].Height; int i, y;
int i, y = 0;
if (w->verAnchor == ANCHOR_MIN) { if (w->verAnchor == ANCHOR_MIN) {
y = w->y; y = w->y;
for (i = 0; i < index; i++) { for (i = 0; i < w->lines; i++) {
textures[i].Y = y;
y += textures[i].Height; y += textures[i].Height;
} }
for (i = index + 1; i < w->lines; i++) {
textures[i].Y += deltaY;
}
} else { } else {
y = Window_Height - w->yOffset; y = Window_Height - w->yOffset;
for (i = index + 1; i < w->lines; i++) { for (i = w->lines- 1; i >= 0; i--) {
y -= textures[i].Height; y -= textures[i].Height;
} textures[i].Y = y;
y -= newHeight;
for (i = 0; i < index; i++) {
textures[i].Y -= deltaY;
} }
} }
return y;
} }
void TextGroupWidget_SetUsePlaceHolder(struct TextGroupWidget* w, int index, bool placeHolder) { void TextGroupWidget_SetUsePlaceHolder(struct TextGroupWidget* w, int index, bool placeHolder) {
int height;
w->placeholderHeight[index] = placeHolder; w->placeholderHeight[index] = placeHolder;
if (w->textures[index].ID) return; if (w->textures[index].ID) return;
height = placeHolder ? w->defaultHeight : 0; w->textures[index].Height = placeHolder ? w->defaultHeight : 0;
w->textures[index].Y = TextGroupWidget_CalcY(w, index, height); TextGroupWidget_UpdateY(w);
w->textures[index].Height = height;
} }
int TextGroupWidget_UsedHeight(struct TextGroupWidget* w) { int TextGroupWidget_UsedHeight(struct TextGroupWidget* w) {
@ -2530,8 +2512,8 @@ void TextGroupWidget_Redraw(struct TextGroupWidget* w, int index) {
} }
tex.X = Gui_CalcPos(w->horAnchor, w->xOffset, tex.Width, Window_Width); tex.X = Gui_CalcPos(w->horAnchor, w->xOffset, tex.Width, Window_Width);
tex.Y = TextGroupWidget_CalcY(w, index, tex.Height);
w->textures[index] = tex; w->textures[index] = tex;
TextGroupWidget_UpdateY(w);
TextGroupWidget_UpdateDimensions(w); TextGroupWidget_UpdateDimensions(w);
} }