Skip to content

Commit

Permalink
Use more conditionally compiled code via if/else instead of via prepr…
Browse files Browse the repository at this point in the history
…ocessor
  • Loading branch information
UnknownShadow200 committed May 28, 2024
1 parent 264106c commit 245d470
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static CC_NOINLINE int GetWindowScale(void) {
/* Use larger UI scaling on mobile */
/* TODO move this DPI scaling elsewhere.,. */
#ifndef CC_BUILD_DUALSCREEN
if (!Gui.TouchUI) {
if (!Gui_TouchUI) {
#endif
widthScale /= DisplayInfo.ScaleX;
heightScale /= DisplayInfo.ScaleY;
Expand Down
6 changes: 6 additions & 0 deletions src/Gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ CC_VAR extern struct _GuiData {
cc_bool TouchUI;
} Gui;

#ifdef CC_BUILD_TOUCH
#define Gui_TouchUI Gui.TouchUI
#else
#define Gui_TouchUI false
#endif

float Gui_Scale(float value);
float Gui_GetHotbarScale(void);
float Gui_GetInventoryScale(void);
Expand Down
2 changes: 1 addition & 1 deletion src/Menus.c
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@ static void MenuOptionsScreen_Input(void* screen, void* widget) {
String_InitArray(value, valueBuffer);
btn->GetValue(&value);
desc = (struct MenuInputDesc*)btn->meta.ptr;
MenuInputOverlay_Show(desc, &value, MenuOptionsScreen_OnDone, Gui.TouchUI);
MenuInputOverlay_Show(desc, &value, MenuOptionsScreen_OnDone, Gui_TouchUI);
}

static void MenuOptionsScreen_OnHacksChanged(void* screen) {
Expand Down
8 changes: 4 additions & 4 deletions src/Screens.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,21 @@ static void HUDScreen_InputUp(void* screen, int key) {

static int HUDscreen_PointerDown(void* screen, int id, int x, int y) {
struct HUDScreen* s = (struct HUDScreen*)screen;
if (Gui.TouchUI || Gui.InputGrab) {
if (Gui_TouchUI || Gui.InputGrab) {
return Elem_HandlesPointerDown(&s->hotbar, id, x, y);
}
return false;
}

static void HUDScreen_PointerUp(void *screen, int id, int x, int y) {
struct HUDScreen* s = (struct HUDScreen*)screen;
if(!Gui.TouchUI) return;
if (!Gui_TouchUI) return;
Elem_OnPointerUp(&s->hotbar, id, x, y);
}

static int HUDScreen_PointerMove(void *screen, int id, int x, int y) {
struct HUDScreen* s = (struct HUDScreen*)screen;
if(!Gui.TouchUI) return false;
if (!Gui_TouchUI) return false;
return Elem_HandlesPointerMove(&s->hotbar, id, x, y);
}

Expand Down Expand Up @@ -1392,7 +1392,7 @@ static int ChatScreen_PointerDown(void* screen, int id, int x, int y) {
if (Game_HideGui) return false;

if (!s->grabsInput) {
if (!Gui.TouchUI) return false;
if (!Gui_TouchUI) return false;
String_InitArray(text, textBuffer);

/* Should be able to click on links with touch */
Expand Down
47 changes: 18 additions & 29 deletions src/Widgets.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ void ScrollbarWidget_Create(struct ScrollbarWidget* w, int width) {

/* It's easy to accidentally touch a bit to the right of the */
/* scrollbar with your finger, so just add some padding */
if (!Gui.TouchUI) return;
if (!Gui_TouchUI) return;
w->padding = Display_ScaleX(15);
}

Expand Down Expand Up @@ -429,9 +429,8 @@ static void HotbarWidget_BuildEntriesMesh(struct HotbarWidget* w, struct VertexT
x = HotbarWidget_TileX(w, i);
y = w->y + (w->height / 2);

#ifdef CC_BUILD_TOUCH
if (i == HOTBAR_MAX_INDEX && Gui.TouchUI) continue;
#endif
if (i == HOTBAR_MAX_INDEX && Gui_TouchUI) continue;

IsometricDrawer_AddBatch(Inventory_Get(i), scale, x, y);
}
w->verticesCount = IsometricDrawer_EndBatch();
Expand Down Expand Up @@ -467,13 +466,11 @@ static int HotbarWidget_Render2(void* widget, int offset) {
HotbarWidget_RenderOutline(w, offset );
HotbarWidget_RenderEntries(w, offset + 8);

#ifdef CC_BUILD_TOUCH
if (Gui.TouchUI) {
if (Gui_TouchUI) {
w->ellipsisTex.x = HotbarWidget_TileX(w, HOTBAR_MAX_INDEX) - w->ellipsisTex.width / 2;
w->ellipsisTex.y = w->y + (w->height / 2) - w->ellipsisTex.height / 2;
Texture_Render(&w->ellipsisTex);
}
#endif

Gfx_3DS_SetRenderScreen(TOP_SCREEN);
return HOTBAR_MAX_VERTICES;
Expand All @@ -482,9 +479,8 @@ static int HotbarWidget_Render2(void* widget, int offset) {
static int HotbarWidget_MaxVertices(void* w) { return HOTBAR_MAX_VERTICES; }

void HotbarWidget_Update(struct HotbarWidget* w, float delta) {
#ifdef CC_BUILD_TOUCH
int i;
if (!Gui.TouchUI) return;
if (!Gui_TouchUI) return;

for (i = 0; i < HOTBAR_MAX_INDEX; i++)
{
Expand All @@ -497,7 +493,6 @@ void HotbarWidget_Update(struct HotbarWidget* w, float delta) {
w->touchTime[i] = 0;
Inventory_Set(i, 0);
}
#endif
}

static int HotbarWidget_ScrolledIndex(struct HotbarWidget* w, float delta, int index, int dir) {
Expand Down Expand Up @@ -604,16 +599,15 @@ static int HotbarWidget_PointerDown(void* widget, int id, int x, int y) {
cellY = w->y;
if (!Gui_Contains(cellX, cellY, width, height, x, y)) continue;

#ifdef CC_BUILD_TOUCH
if(Gui.TouchUI) {
if (Gui_TouchUI) {
if (i == HOTBAR_MAX_INDEX) {
InventoryScreen_Show(); return TOUCH_TYPE_GUI;
} else {
w->touchId[i] = id;
w->touchId[i] = id;
w->touchTime[i] = 0;
}
}
#endif

Inventory_SetSelectedIndex(i);
return TOUCH_TYPE_GUI;
}
Expand All @@ -627,7 +621,7 @@ static void HotbarWidget_PointerUp(void* widget, int id, int x, int y) {

for (i = 0; i < HOTBAR_MAX_INDEX; i++) {
if (w->touchId[i] == id) {
w->touchId[i] = -1;
w->touchId[i] = -1;
w->touchTime[i] = 0;
}
}
Expand All @@ -639,13 +633,12 @@ static int HotbarWidget_PointerMove(void* widget, int id, int x, int y) {
struct HotbarWidget* w = (struct HotbarWidget*)widget;
int i;

for (i = 0; i < HOTBAR_MAX_INDEX; i++) {
if (w->touchId[i] == id) {
if (!Widget_Contains(w, x, y)) {
w->touchId[i] = -1;
w->touchTime[i] = 0;
return true;
}
for (i = 0; i < HOTBAR_MAX_INDEX; i++)
{
if (w->touchId[i] == id && !Widget_Contains(w, x, y)) {
w->touchId[i] = -1;
w->touchTime[i] = 0;
return true;
}
}
#endif
Expand All @@ -669,12 +662,10 @@ static int HotbarWidget_MouseScroll(void* widget, float delta) {
}

static void HotbarWidget_Free(void* widget) {
#ifdef CC_BUILD_TOUCH
struct HotbarWidget* w = (struct HotbarWidget*)widget;
if (!Gui.TouchUI) return;
if (!Gui_TouchUI) return;

Gfx_DeleteTexture(&w->ellipsisTex.ID);
#endif
}

static const struct WidgetVTABLE HotbarWidget_VTABLE = {
Expand All @@ -700,14 +691,12 @@ void HotbarWidget_Create(struct HotbarWidget* w) {
}

void HotbarWidget_SetFont(struct HotbarWidget* w, struct FontDesc* font) {
#ifdef CC_BUILD_TOUCH
static const cc_string dots = String_FromConst("...");
struct DrawTextArgs args;
if (!Gui.TouchUI) return;
if (!Gui_TouchUI) return;

DrawTextArgs_Make(&args, &dots, font, true);
Drawer2D_MakeTextTexture(&w->ellipsisTex, &args);
#endif
}


Expand Down Expand Up @@ -1717,7 +1706,7 @@ void TextInputWidget_Create(struct TextInputWidget* w, int width, const cc_strin

w->base.convertPercents = false;
w->base.padding = 3;
w->base.showCaret = !Gui.TouchUI;
w->base.showCaret = !Gui_TouchUI;
w->base.flags = WIDGET_FLAG_SELECTABLE;

w->base.GetMaxLines = TextInputWidget_GetMaxLines;
Expand Down
2 changes: 0 additions & 2 deletions src/Widgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ struct HotbarWidget {
struct Texture ellipsisTex;
int state[HOTBAR_CORE_VERTICES / 4];
int verticesCount;
#ifdef CC_BUILD_TOUCH
int touchId[HOTBAR_MAX_INDEX];
float touchTime[HOTBAR_MAX_INDEX];
#endif
};
#define HOTBAR_MAX_VERTICES (4 + 4 + HOTBAR_CORE_VERTICES)

Expand Down

0 comments on commit 245d470

Please sign in to comment.