From aa793891cc82a5e72daefd954e62fc9fdaefeb4a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 10 Apr 2022 21:14:05 +1000 Subject: [PATCH] ios: WIP on native table widget --- src/LBackend.c | 3 ++ src/LBackend.h | 2 + src/LScreens.c | 4 +- src/LWidgets.c | 29 +++++++------ src/LWidgets.h | 1 + src/Launcher.c | 2 +- src/interop_ios.m | 107 +++++++++++++++++++++++++++++++++------------- 7 files changed, 103 insertions(+), 45 deletions(-) diff --git a/src/LBackend.c b/src/LBackend.c index 560b4618e..fb2c96574 100644 --- a/src/LBackend.c +++ b/src/LBackend.c @@ -537,6 +537,9 @@ void LBackend_SliderDraw(struct LSlider* w) { /*########################################################################################################################* *-------------------------------------------------------TableWidget-------------------------------------------------------* *#########################################################################################################################*/ +void LBackend_TableInit(struct LTable* w) { } +void LBackend_TableUpdate(struct LTable* w) { } + void LBackend_TableReposition(struct LTable* w) { int rowsHeight; w->hdrHeight = Drawer2D_FontHeight(&Launcher_TextFont, true) + hdrYPadding; diff --git a/src/LBackend.h b/src/LBackend.h index 29738fa29..3e7d67dc1 100644 --- a/src/LBackend.h +++ b/src/LBackend.h @@ -46,6 +46,8 @@ void LBackend_SliderInit(struct LSlider* w, int width, int height); void LBackend_SliderUpdate(struct LSlider* w); void LBackend_SliderDraw(struct LSlider* w); +void LBackend_TableInit(struct LTable* w); +void LBackend_TableUpdate(struct LTable* w); void LBackend_TableDraw(struct LTable* w); void LBackend_TableReposition(struct LTable* w); diff --git a/src/LScreens.c b/src/LScreens.c index 9949fd74e..f8c31a4be 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -1236,9 +1236,7 @@ static void FetchResourcesScreen_UpdateProgress(struct FetchResourcesScreen* s) /* making request still, haven't started download yet */ if (progress < 0 || progress > 100) return; - if (progress == s->sdrProgress.value) return; - s->sdrProgress.value = progress; - LWidget_Draw(&s->sdrProgress); + LSlider_SetProgress(&s->sdrProgress, progress); } static void FetchResourcesScreen_Error(struct FetchResourcesScreen* s) { diff --git a/src/LWidgets.c b/src/LWidgets.c index baf87222e..c88d1a685 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -487,17 +487,7 @@ static int PlayersColumn_Sort(const struct ServerInfo* a, const struct ServerInf } static void UptimeColumn_Draw(struct ServerInfo* row, struct DrawTextArgs* args, struct LTableCell* cell) { - int uptime = row->uptime; - char unit = 's'; - - if (uptime >= SECS_PER_DAY * 7) { - uptime /= SECS_PER_DAY; unit = 'd'; - } else if (uptime >= SECS_PER_HOUR) { - uptime /= SECS_PER_HOUR; unit = 'h'; - } else if (uptime >= SECS_PER_MIN) { - uptime /= SECS_PER_MIN; unit = 'm'; - } - String_Format2(&args->text, "%i%r", &uptime, &unit); + LTable_FormatUptime(&args->text, row->uptime); } static int UptimeColumn_Sort(const struct ServerInfo* a, const struct ServerInfo* b) { return b->uptime - a->uptime; @@ -513,7 +503,7 @@ static int SoftwareColumn_Sort(const struct ServerInfo* a, const struct ServerIn return String_Compare(&b->software, &a->software); } -static struct LTableColumn tableColumns[5] = { +static struct LTableColumn tableColumns[] = { { "", 15, FlagColumn_Draw, NULL, false, false }, { "Name", 328, NameColumn_Draw, NameColumn_Sort, true, true }, { "Players", 73, PlayersColumn_Draw, PlayersColumn_Sort, true, true }, @@ -522,6 +512,19 @@ static struct LTableColumn tableColumns[5] = { }; +void LTable_FormatUptime(cc_string* dst, int uptime) { + char unit = 's'; + + if (uptime >= SECS_PER_DAY * 7) { + uptime /= SECS_PER_DAY; unit = 'd'; + } else if (uptime >= SECS_PER_HOUR) { + uptime /= SECS_PER_HOUR; unit = 'h'; + } else if (uptime >= SECS_PER_MIN) { + uptime /= SECS_PER_MIN; unit = 'm'; + } + String_Format2(dst, "%i%r", &uptime, &unit); +} + void LTable_GetScrollbarCoords(struct LTable* w, int* y, int* height) { float scale; if (!w->rowsCount) { *y = 0; *height = 0; return; } @@ -633,6 +636,7 @@ void LTable_Init(struct LTable* w, struct FontDesc* rowFont) { for (i = 0; i < w->numColumns; i++) { w->columns[i].width = Display_ScaleX(w->columns[i].width); } + LBackend_TableInit(w); } void LTable_Reset(struct LTable* w) { @@ -667,6 +671,7 @@ void LTable_ApplyFilter(struct LTable* w) { w->_lastRow = -1; LTable_ClampTopRow(w); + LBackend_TableUpdate(w); } static int sortingCol; diff --git a/src/LWidgets.h b/src/LWidgets.h index 58da23a4d..3c7aee23e 100644 --- a/src/LWidgets.h +++ b/src/LWidgets.h @@ -212,6 +212,7 @@ void LTable_Sort(struct LTable* table); /* If selected row is not visible, adjusts top row so it does show. */ void LTable_ShowSelected(struct LTable* table); +void LTable_FormatUptime(cc_string* dst, int uptime); /* Works out top and height of the scrollbar */ void LTable_GetScrollbarCoords(struct LTable* w, int* y, int* height); /* Ensures top/first visible row index lies within table */ diff --git a/src/Launcher.c b/src/Launcher.c index 4cdc5b328..d686d4e00 100644 --- a/src/Launcher.c +++ b/src/Launcher.c @@ -91,7 +91,7 @@ static CC_NOINLINE void InitFramebuffer(void) { static cc_uint64 lastJoin; cc_bool Launcher_StartGame(const cc_string* user, const cc_string* mppass, const cc_string* ip, const cc_string* port, const cc_string* server) { cc_string args[4]; int numArgs; - TimeMS now; + cc_uint64 now; cc_result res; now = Stopwatch_Measure(); diff --git a/src/interop_ios.m b/src/interop_ios.m index faf43fce0..0a51eab8b 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 "LWeb.h" #include #include #include @@ -73,19 +74,6 @@ static void RemoveTouch(UITouch* t) { } - (BOOL)isOpaque { return YES; } - -// helpers for LBackend -static void LBackend_HandleButton(id btn); -static void LBackend_HandleInput(id ipt); - -- (void)handleButtonPress:(id)sender { - LBackend_HandleButton(sender); -} - -- (void)handleTextChanged:(id)sender { - LBackend_HandleInput(sender); -} - @end static cc_bool landscape_locked; @@ -95,6 +83,10 @@ static cc_bool landscape_locked; return UIInterfaceOrientationMaskLandscape; return [super supportedInterfaceOrientations]; } + +/*- (BOOL)prefersStatusBarHidden { + +}*/ @end @implementation CCAppDelegate @@ -308,7 +300,7 @@ void Window_DrawFramebuffer(Rect2D r) { rect.origin.y = WindowInfo.Height - r.Y - r.Height; rect.size.width = r.Width; rect.size.height = r.Height; - win_handle.layer.contents = CFBridgingRelease(CGBitmapContextCreateImage(win_ctx)); + view_handle.layer.contents = CFBridgingRelease(CGBitmapContextCreateImage(win_ctx)); // TODO always redraws entire launcher which is quite terrible performance wise //[win_handle setNeedsDisplayInRect:rect]; } @@ -556,9 +548,6 @@ static UIImage* ToUIImage(struct Bitmap* bmp) { return img; } -void LBackend_Init(void) { -} - static void UpdateWidgetDimensions(void* widget) { struct LWidget* w = widget; UIView* view = (__bridge UIView*)w->meta; @@ -618,22 +607,73 @@ static struct LWidget* FindWidgetForView(id obj) { return NULL; } -/*########################################################################################################################* - *------------------------------------------------------ButtonWidget-------------------------------------------------------* - *#########################################################################################################################*/ -static void LBackend_HandleButton(id btn_obj) { - struct LWidget* w = FindWidgetForView(btn_obj); +static NSString* cellID = @"CC_Cell"; +@interface CCUIController : NSObject +@end + +@implementation CCUIController + +- (void)handleButtonPress:(id)sender { + struct LWidget* w = FindWidgetForView(sender); if (w == NULL) return; - + struct LButton* btn = (struct LButton*)w; btn->OnClick(btn); } +<<<<<<< HEAD +======= +- (void)handleTextChanged:(id)sender { + struct LWidget* w = FindWidgetForView(sender); + if (w == NULL) return; + + UITextField* src = (UITextField*)sender; + const char* str = [[src text] UTF8String]; + + struct LInput* ipt = (struct LInput*)w; + ipt->text.length = 0; + String_AppendUtf8(&ipt->text, str, String_Length(str)); + if (ipt->TextChanged) ipt->TextChanged(ipt); +} + +- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { + //UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; + UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID]; + if (cell == nil) { + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; + } + + struct ServerInfo* server = Servers_Get([indexPath row]); + char descBuffer[128]; + cc_string desc = String_FromArray(descBuffer); + String_Format2(&desc, "%i/%i players, up for ", &server->players, &server->maxPlayers); + LTable_FormatUptime(&desc, server->uptime); + + cell.textLabel.text = ToNSString(&server->name); + cell.detailTextLabel.text = ToNSString(&desc);//[ToNSString(&desc) stringByAppendingString:@"\nLine2"]; + return cell; +} + +- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return FetchServersTask.numServers; +} + +@end + +static CCUIController* ui_controller; +void LBackend_Init(void) { + ui_controller = [[CCUIController alloc] init]; + CFBridgingRetain(ui_controller); // prevent GC TODO even needed? +} + +/*########################################################################################################################* + *------------------------------------------------------ButtonWidget-------------------------------------------------------* + *#########################################################################################################################*/ +>>>>>>> b2e96605 (ios: WIP on native table widget) void LBackend_ButtonInit(struct LButton* w, int width, int height) { UIButton* btn = [[UIButton alloc] init]; btn.frame = CGRectMake(0, 0, width, height); - // TODO should be app_handle, because win_handle can change - [btn addTarget:win_handle action:@selector(handleButtonPress:) forControlEvents:UIControlEventTouchUpInside]; + [btn addTarget:ui_controller action:@selector(handleButtonPress:) forControlEvents:UIControlEventTouchUpInside]; AssignView(w, btn); UpdateWidgetDimensions(w); @@ -680,6 +720,7 @@ void LBackend_CheckboxDraw(struct LCheckbox* w) { /*########################################################################################################################* *------------------------------------------------------InputWidget--------------------------------------------------------* *#########################################################################################################################*/ +<<<<<<< HEAD static void LBackend_HandleInput(id ipt_obj) { struct LWidget* w = FindWidgetForView(ipt_obj); if (w == NULL) return; @@ -693,13 +734,14 @@ static void LBackend_HandleInput(id ipt_obj) { if (ipt->TextChanged) ipt->TextChanged(ipt); } +======= +>>>>>>> b2e96605 (ios: WIP on native table widget) void LBackend_InputInit(struct LInput* w, int width) { UITextField* fld = [[UITextField alloc] init]; fld.frame = CGRectMake(0, 0, width, 30); fld.borderStyle = UITextBorderStyleBezel; fld.backgroundColor = [UIColor whiteColor]; - // TODO should be app_handle, because win_handle can change - [fld addTarget:win_handle action:@selector(handleTextChanged:) forControlEvents:UIControlEventEditingChanged]; + [fld addTarget:ui_controller action:@selector(handleTextChanged:) forControlEvents:UIControlEventEditingChanged]; if (w->type == KEYBOARD_TYPE_INTEGER) { [fld setKeyboardType:UIKeyboardTypeNumberPad]; @@ -799,14 +841,21 @@ void LBackend_SliderDraw(struct LSlider* w) { /*########################################################################################################################* *------------------------------------------------------TableWidget-------------------------------------------------------* *#########################################################################################################################*/ -void LBackend_TableInit(struct LTableView* w) { +void LBackend_TableInit(struct LTable* w) { UITableView* tbl = [[UITableView alloc] init]; - tbl.frame = CGRectMake(0, 0, 200, 30); + tbl.frame = CGRectMake(0, 50, 350, 570); + tbl.delegate = ui_controller; + tbl.dataSource = ui_controller; AssignView(w, tbl); UpdateWidgetDimensions(w); } +void LBackend_TableUpdate(struct LTable* w) { + UITableView* tbl = (__bridge UITableView*)w->meta; + [tbl reloadData]; +} + void LBackend_TableDraw(struct LTable* w) { } void LBackend_TableReposition(struct LTable* w) { }