diff --git a/src/LBackend.c b/src/LBackend.c index c906ca898..c75b40191 100644 --- a/src/LBackend.c +++ b/src/LBackend.c @@ -99,7 +99,35 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) { void LBackend_SetScreen(struct LScreen* s) { } void LBackend_CloseScreen(struct LScreen* s) { } -void LBackend_WidgetRepositioned(struct LWidget* w) { +static void LBackend_LayoutDimensions(struct LWidget* w) { + const struct LLayout* l = w->layouts + 2; + while (l->type) + { + switch (l->type) + { + case LLAYOUT_WIDTH: + w->width = WindowInfo.Width - w->x - Display_ScaleX(l->offset); + w->width = max(1, w->width); + break; + case LLAYOUT_HEIGHT: + w->height = WindowInfo.Height - w->y - Display_ScaleY(l->offset); + w->height = max(1, w->height); + break; + } + l++; + } +} + +void LBackend_LayoutWidget(struct LWidget* w) { + const struct LLayout* l = w->layouts; + + w->x = Gui_CalcPos(l[0].type & 0xFF, Display_ScaleX(l[0].offset), w->width, WindowInfo.Width); + w->y = Gui_CalcPos(l[1].type & 0xFF, Display_ScaleY(l[1].offset), w->height, WindowInfo.Height); + + /* e.g. Table widget needs adjusts width/height based on window */ + if (l[1].type & LLAYOUT_EXTRA) + LBackend_LayoutDimensions(w); + if (w->type != LWIDGET_TABLE) return; LBackend_TableReposition((struct LTable*)w); } diff --git a/src/LBackend.h b/src/LBackend.h index 2155b036e..806b4170f 100644 --- a/src/LBackend.h +++ b/src/LBackend.h @@ -27,7 +27,7 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title); void LBackend_Redraw(void); void LBackend_ThemeChanged(void); void LBackend_Tick(void); -void LBackend_WidgetRepositioned(struct LWidget* w); +void LBackend_LayoutWidget(struct LWidget* w); void LBackend_MarkDirty(void* widget); void LBackend_InitFramebuffer(void); diff --git a/src/LScreens.c b/src/LScreens.c index e959f05aa..18dee3c0e 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -46,7 +46,7 @@ static void LScreen_DoLayout(struct LScreen* s) { int i; for (i = 0; i < s->numWidgets; i++) { - LWidget_CalcPosition(s->widgets[i]); + LBackend_LayoutWidget(s->widgets[i]); } } diff --git a/src/LWidgets.c b/src/LWidgets.c index 2679fa31a..f4bb836ae 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -27,40 +27,6 @@ void LWidget_CalcOffsets(void) { } -static void LWidget_LayoutDimensions(struct LWidget* w) { - const struct LLayout* l = w->layouts + 2; - while (l->type) - { - switch (l->type) - { - case LLAYOUT_WIDTH: - w->width = WindowInfo.Width - w->x - Display_ScaleX(l->offset); - w->width = max(1, w->width); - break; - case LLAYOUT_HEIGHT: - w->height = WindowInfo.Height - w->y - Display_ScaleY(l->offset); - w->height = max(1, w->height); - break; - } - l++; - } -} - -void LWidget_CalcPosition(void* widget) { - struct LWidget* w = (struct LWidget*)widget; - const struct LLayout* l = w->layouts; - - w->x = Gui_CalcPos(l[0].type & 0xFF, Display_ScaleX(l[0].offset), w->width, WindowInfo.Width); - w->y = Gui_CalcPos(l[1].type & 0xFF, Display_ScaleY(l[1].offset), w->height, WindowInfo.Height); - - /* e.g. Table widget needs adjusts width/height based on window */ - if (l[1].type & LLAYOUT_EXTRA) { - LWidget_LayoutDimensions(w); - } - LBackend_WidgetRepositioned(w); -} - - /*########################################################################################################################* *------------------------------------------------------ButtonWidget-------------------------------------------------------* *#########################################################################################################################*/ @@ -417,7 +383,7 @@ void LLabel_Init(struct LLabel* w, const char* text, const struct LLayout* layou void LLabel_SetText(struct LLabel* w, const cc_string* text) { String_Copy(&w->text, text); LBackend_LabelUpdate(w); - LWidget_CalcPosition(w); + LBackend_LayoutWidget((struct LWidget*)w); } void LLabel_SetConst(struct LLabel* w, const char* text) { diff --git a/src/LWidgets.h b/src/LWidgets.h index f164e7715..c2002d25c 100644 --- a/src/LWidgets.h +++ b/src/LWidgets.h @@ -59,7 +59,6 @@ struct LWidgetVTABLE { /* Represents an individual 2D gui component in the launcher. */ struct LWidget { LWidget_Layout }; -void LWidget_CalcPosition(void* widget); void LWidget_CalcOffsets(void); struct LButton { diff --git a/src/interop_ios.m b/src/interop_ios.m index 954749e7f..57dd101fc 100644 --- a/src/interop_ios.m +++ b/src/interop_ios.m @@ -10,6 +10,7 @@ #include "LBackend.h" #include "LWidgets.h" #include "LScreens.h" +#include "Gui.h" #include "LWeb.h" #include "Funcs.h" #include @@ -18,6 +19,7 @@ #include #include #include +#include @interface CCWindow : UIWindow @end @@ -546,22 +548,37 @@ static UIImage* ToUIImage(struct Bitmap* bmp) { return img; } -static void UpdateWidgetDimensions(void* widget) { - struct LWidget* w = widget; - UIView* view = (__bridge UIView*)w->meta; - - CGRect rect = [view frame]; - w->width = (int)rect.size.width; - w->height = (int)rect.size.height; +static void LBackend_LayoutDimensions(struct LWidget* w, CGRect* r) { + const struct LLayout* l = w->layouts + 2; + while (l->type) + { + switch (l->type) + { + case LLAYOUT_WIDTH: + r->size.width = WindowInfo.Width - (int)r->origin.x - Display_ScaleX(l->offset); + break; + case LLAYOUT_HEIGHT: + r->size.height = WindowInfo.Height - (int)r->origin.y - Display_ScaleY(l->offset); + break; + } + l++; + } } -void LBackend_WidgetRepositioned(struct LWidget* w) { - UIView* view = (__bridge UIView*)w->meta; +void LBackend_LayoutWidget(struct LWidget* w) { + const struct LLayout* l = w->layouts; + UIView* view = (__bridge UIView*)w->meta; + CGRect r = [view frame]; + int width = (int)r.size.width; + int height = (int)r.size.height; - CGRect rect = [view frame]; - rect.origin.x = w->x; - rect.origin.y = w->y; - [view setFrame:rect]; + r.origin.x = Gui_CalcPos(l[0].type & 0xFF, Display_ScaleX(l[0].offset), width, WindowInfo.Width); + r.origin.y = Gui_CalcPos(l[1].type & 0xFF, Display_ScaleY(l[1].offset), height, WindowInfo.Height); + + /* e.g. Table widget needs adjusts width/height based on window */ + if (l[1].type & LLAYOUT_EXTRA) + LBackend_LayoutDimensions(w, &r); + [view setFrame:r]; } void LBackend_SetScreen(struct LScreen* s) { @@ -698,7 +715,6 @@ void LBackend_Tick(void) { } void LBackend_Free(void) { } void LBackend_UpdateLogoFont(void) { } -#include static void DrawText(NSAttributedString* str, struct Bitmap* bmp, int x, int y) { CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)str); CGRect bounds = CTLineGetImageBounds(line, win_ctx); @@ -772,15 +788,18 @@ void LBackend_ThemeChanged(void) { *#########################################################################################################################*/ static void LButton_UpdateBackground(struct LButton* w) { UIButton* btn = (__bridge UIButton*)w->meta; + CGRect rect = [btn frame]; + int width = (int)rect.size.width; + int height = (int)rect.size.height; // memory freeing deferred until UIImage is freed (see FreeContents) struct Bitmap bmp1, bmp2; - Bitmap_Allocate(&bmp1, w->width, w->height); - LButton_DrawBackground(&bmp1, 0, 0, w->width, w->height, false); + Bitmap_Allocate(&bmp1, width, height); + LButton_DrawBackground(&bmp1, 0, 0, width, height, false); [btn setBackgroundImage:ToUIImage(&bmp1) forState:UIControlStateNormal]; - Bitmap_Allocate(&bmp2, w->width, w->height); - LButton_DrawBackground(&bmp2, 0, 0, w->width, w->height, true); + Bitmap_Allocate(&bmp2, width, height); + LButton_DrawBackground(&bmp2, 0, 0, width, height, true); [btn setBackgroundImage:ToUIImage(&bmp2) forState:UIControlStateHighlighted]; } @@ -790,7 +809,6 @@ void LBackend_ButtonInit(struct LButton* w, int width, int height) { [btn addTarget:ui_controller action:@selector(handleButtonPress:) forControlEvents:UIControlEventTouchUpInside]; AssignView(w, btn); - UpdateWidgetDimensions(w); LButton_UpdateBackground(w); } @@ -799,7 +817,6 @@ void LBackend_ButtonUpdate(struct LButton* w) { NSString* str = ToNSString(&w->text); [btn setTitle:str forState:UIControlStateNormal]; - UpdateWidgetDimensions(w); } @@ -815,7 +832,6 @@ void LBackend_CheckboxInit(struct LCheckbox* w) { [swt addTarget:ui_controller action:@selector(handleValueChanged:) forControlEvents:UIControlEventValueChanged]; AssignView(w, swt); - UpdateWidgetDimensions(w); } void LBackend_CheckboxDraw(struct LCheckbox* w) { @@ -856,13 +872,11 @@ void LBackend_InputInit(struct LInput* w, int width) { LInput_SetPlaceholder(fld, w->hintText); AssignView(w, fld); - UpdateWidgetDimensions(w); } void LBackend_InputUpdate(struct LInput* w) { UITextField* fld = (__bridge UITextField*)w->meta; fld.text = ToNSString(&w->text); - UpdateWidgetDimensions(w); } void LBackend_InputDraw(struct LInput* w) { @@ -881,7 +895,6 @@ void LBackend_LabelInit(struct LLabel* w) { lbl.textColor = [UIColor whiteColor]; AssignView(w, lbl); - UpdateWidgetDimensions(w); } void LBackend_LabelUpdate(struct LLabel* w) { @@ -893,7 +906,6 @@ void LBackend_LabelUpdate(struct LLabel* w) { lbl.text = str; [lbl sizeToFit]; // adjust label to fit text - UpdateWidgetDimensions(w); } void LBackend_LabelDraw(struct LLabel* w) { @@ -911,7 +923,6 @@ void LBackend_LineInit(struct LLine* w, int width) { view.backgroundColor = ToUIColor(color, 0.5f); AssignView(w, view); - UpdateWidgetDimensions(w); } void LBackend_LineDraw(struct LLine* w) { @@ -927,13 +938,12 @@ void LBackend_SliderInit(struct LSlider* w, int width, int height) { prg.progressTintColor = ToUIColor(w->color, 1.0f); AssignView(w, prg); - UpdateWidgetDimensions(w); } void LBackend_SliderUpdate(struct LSlider* w) { - UIProgressView* lbl = (__bridge UIProgressView*)w->meta; + UIProgressView* prg = (__bridge UIProgressView*)w->meta; - lbl.progress = w->value / 100.0f; + prg.progress = w->value / 100.0f; } void LBackend_SliderDraw(struct LSlider* w) { @@ -945,17 +955,11 @@ void LBackend_SliderDraw(struct LSlider* w) { *#########################################################################################################################*/ void LBackend_TableInit(struct LTable* w) { UITableView* tbl = [[UITableView alloc] init]; - //tbl.frame = CGRectMake(0, 50, 350, 570); tbl.delegate = ui_controller; tbl.dataSource = ui_controller; //[tbl registerClass:UITableViewCell.class forCellReuseIdentifier:cellID]; - - CGRect total = [view_handle frame]; - tbl.frame = CGRectMake(0, 0, total.size.width - 10, total.size.height - 50 - 50); - AssignView(w, tbl); - UpdateWidgetDimensions(w); } void LBackend_TableUpdate(struct LTable* w) {