From 4250fb3752793b4fa9a9ae559e096755d99e892e Mon Sep 17 00:00:00 2001 From: yellows8 Date: Wed, 16 Jan 2019 20:34:53 -0500 Subject: [PATCH] Updated SwkbdInlineCalcArg struct. Added: swkbdInlineSetKeytopBgAlpha, swkbdInlineSetFooterBgAlpha, swkbdInlineSetKeytopScale, and swkbdInlineSetKeytopTranslate. Implemented field updating in swkbdInlineUpdate. --- nx/include/switch/applets/swkbd.h | 43 ++++++++++++++++--- nx/source/applets/swkbd.c | 69 +++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 9 deletions(-) diff --git a/nx/include/switch/applets/swkbd.h b/nx/include/switch/applets/swkbd.h index 581ddd79..dc36ade2 100644 --- a/nx/include/switch/applets/swkbd.h +++ b/nx/include/switch/applets/swkbd.h @@ -156,12 +156,12 @@ typedef struct { u8 unk_x468[5]; u16 unk_x46d; u8 unk_x46f; - float keytopScale0; ///< Flags bitmask 0x200. - float keytopScale1; ///< Flags bitmask 0x200. - float keytopTranslate0; ///< Flags bitmask 0x200. - float keytopTranslate1; ///< Flags bitmask 0x200. + float keytopScaleX; ///< Flags bitmask 0x200. + float keytopScaleY; ///< Flags bitmask 0x200. + float keytopTranslateX; ///< Flags bitmask 0x200. + float keytopTranslateY; ///< Flags bitmask 0x200. float keytopBgAlpha; ///< Flags bitmask 0x100. - float unk_x484; + float footerBgAlpha; ///< Flags bitmask 0x100. float balloonScale; ///< Flags bitmask 0x200. float unk_x48c; u8 unk_x490[0xc]; @@ -403,6 +403,39 @@ void swkbdInlineSetInputModeFadeType(SwkbdInline* s, u8 type); */ void swkbdInlineSetAlphaEnabledInInputMode(SwkbdInline* s, bool flag); +/** + * @brief Sets KeytopBgAlpha. + * @note \ref swkbdInlineUpdate must be called at some point afterwards for this to take affect. + * @param s SwkbdInline object. + * @param alpha Alpha, clamped to range 0.0f..1.0f. + */ +void swkbdInlineSetKeytopBgAlpha(SwkbdInline* s, float alpha); + +/** + * @brief Sets FooterBgAlpha. + * @note \ref swkbdInlineUpdate must be called at some point afterwards for this to take affect. + * @param s SwkbdInline object. + * @param alpha Alpha, clamped to range 0.0f..1.0f. + */ +void swkbdInlineSetFooterBgAlpha(SwkbdInline* s, float alpha); + +/** + * @brief Sets gfx scaling. Configures KeytopScale* and BalloonScale based on the input value. + * @note \ref swkbdInlineUpdate must be called at some point afterwards for this to take affect. + * @param s SwkbdInline object. + * @param scale Scale + */ +void swkbdInlineSetKeytopScale(SwkbdInline* s, float scale); + +/** + * @brief Sets gfx translation for the displayed swkbd image position. + * @note \ref swkbdInlineUpdate must be called at some point afterwards for this to take affect. + * @param s SwkbdInline object. + * @param x X + * @param y Y + */ +void swkbdInlineSetKeytopTranslate(SwkbdInline* s, float x, float y); + /** * @brief Sets KeytopAsFloating. * @note \ref swkbdInlineUpdate must be called at some point afterwards for this to take affect. diff --git a/nx/source/applets/swkbd.c b/nx/source/applets/swkbd.c index 456a2a4e..d2addfb8 100644 --- a/nx/source/applets/swkbd.c +++ b/nx/source/applets/swkbd.c @@ -1,5 +1,6 @@ #include #include +#include #include "types.h" #include "result.h" #include "kernel/detect.h" @@ -32,6 +33,16 @@ static ssize_t _swkbdConvertToUTF16ByteSize(u16* out, const char* in, size_t max return _swkbdConvertToUTF16(out, in, (max/sizeof(u16)) - 1); } +/// Clamp a float to the range 0.0f..1.0.f. +static void _swkbdClampFloat(float *val) { + float tmpval = *val; + + tmpval = fminf(tmpval, 1.0f); + tmpval = fmaxf(tmpval, 0.0f); + + *val = tmpval; +} + static void _swkbdConfigClear(SwkbdConfig* c) { memset(&c->arg.arg, 0, sizeof(c->arg.arg)); memset(c->arg.unk_x3e0, 0xff, sizeof(c->arg.unk_x3e0)); @@ -354,10 +365,10 @@ Result swkbdInlineCreate(SwkbdInline* s) { s->calcArg.footerScalable = 1; s->calcArg.inputModeFadeType = 1; - s->calcArg.keytopScale0 = 1.0f; - s->calcArg.keytopScale1 = 1.0f; + s->calcArg.keytopScaleX = 1.0f; + s->calcArg.keytopScaleY = 1.0f; s->calcArg.keytopBgAlpha = 1.0f; - s->calcArg.unk_x484 = 1.0f; + s->calcArg.footerBgAlpha = 1.0f; s->calcArg.balloonScale = 1.0f; s->calcArg.unk_x48c = 1.0f; @@ -439,7 +450,16 @@ Result swkbdInlineUpdate(SwkbdInline* s) { AppletStorage storage; u32 tmp0=0, tmp1=0; - //TODO: 'Normalize' floats. + u8 fadetype=0; + if (s->calcArg.footerScalable) { + swkbdInlineSetFooterBgAlpha(s, s->calcArg.keytopBgAlpha); + + fadetype = s->calcArg.keytopBgAlpha != 1.0f; + } + else { + fadetype = 2; + } + swkbdInlineSetInputModeFadeType(s, fadetype); if (appletHolderCheckFinished(&s->holder)) { appletHolderJoin(&s->holder); @@ -583,6 +603,20 @@ void swkbdInlineSetAlphaEnabledInInputMode(SwkbdInline* s, bool flag) { _swkbdInlineSetBoolFlag(s, &s->calcArg.alphaEnabledInInputMode, flag, 0x100); } +void swkbdInlineSetKeytopBgAlpha(SwkbdInline* s, float alpha) { + _swkbdClampFloat(&alpha); + if (s->calcArg.keytopBgAlpha == alpha) return; + s->calcArg.keytopBgAlpha = alpha; + s->calcArg.flags |= 0x100; +} + +void swkbdInlineSetFooterBgAlpha(SwkbdInline* s, float alpha) { + _swkbdClampFloat(&alpha); + if (s->calcArg.footerBgAlpha == alpha) return; + s->calcArg.footerBgAlpha = alpha; + s->calcArg.flags |= 0x100; +} + void swkbdInlineSetKeytopAsFloating(SwkbdInline* s, bool flag) { _swkbdInlineSetBoolFlag(s, &s->calcArg.keytopAsFloating, flag, 0x200); } @@ -595,6 +629,33 @@ void swkbdInlineSetTouchFlag(SwkbdInline* s, bool flag) { _swkbdInlineSetBoolDisableFlag(s, &s->calcArg.disableTouch, flag, 0x200); } +static void _swkbdInlineSetKeytopScale(SwkbdInline* s, float x, float y) { + if (s->calcArg.keytopScaleX == x && s->calcArg.keytopScaleY == y) return; + s->calcArg.keytopScaleX = x; + s->calcArg.keytopScaleY = y; + s->calcArg.flags |= 0x200; +} + +static void _swkbdInlineSetBalloonScale(SwkbdInline* s, float scale) { + if (s->calcArg.balloonScale == scale) return; + s->calcArg.balloonScale = scale; + s->calcArg.flags |= 0x200; +} + +void swkbdInlineSetKeytopScale(SwkbdInline* s, float scale) { + _swkbdInlineSetKeytopScale(s, scale, scale); + + scale = fminf(scale + 0.15f, 1.0f); + _swkbdInlineSetBalloonScale(s, scale); +} + +void swkbdInlineSetKeytopTranslate(SwkbdInline* s, float x, float y) { + if (s->calcArg.keytopTranslateX == x && s->calcArg.keytopTranslateY == y) return; + s->calcArg.keytopTranslateX = x; + s->calcArg.keytopTranslateY = y; + s->calcArg.flags |= 0x200; +} + void swkbdInlineSetUSBKeyboardFlag(SwkbdInline* s, bool flag) { _swkbdInlineSetBoolDisableFlag(s, &s->calcArg.disableUSBKeyboard, flag, 0x800); }