[WIP] Added float, array and int support to mwvalue

This commit is contained in:
KiritoDv 2021-04-29 22:05:30 +00:00
parent 8b08b97160
commit 671af8b836
4 changed files with 90 additions and 23 deletions

View File

@ -28,11 +28,16 @@ void MoonTest::Init(){
bool b = true;
bool c = true;
bool d = true;
float e = 0;
int bIndex = 0;
vector<string> test = {"Val zero", "Val uwo", "Val owu"};
void MoonTest::Mount(){
this->widgets.clear();
this->widgets.push_back(new MWValue({.bvar = &b}, "Toggle owo", 25, 50));
this->widgets.push_back(new MWValue({.bvar = &c}, "Toggle 2 owo", 25, 70));
this->widgets.push_back(new MWValue({.index = &bIndex, .values = &test}, "Toggle 3 owo", 25, 95));
this->widgets.push_back(new MWValue({.bvar = &b}, "Toggle owo", 25, 55));
this->widgets.push_back(new MWValue({.fvar = &e, .max = 10, .min = 0, .step = 0.1f}, "Toggle 2 owo", 25, 75));
MoonScreen::Mount();
}
@ -40,7 +45,6 @@ int x = 0;
int y = 20;
void MoonTest::Draw(){
MoonDrawText(0, 0, "Test text", 1.0, {255, 255, 255, 255}, true, false);
string menuTitle = "Placeholder";

View File

@ -32,39 +32,93 @@ void MWValue::Draw(){
{0, 0, 0, 0},
};
bool isFloat = this->bind.fvar != NULL;
bool isInt = this->bind.ivar != NULL;
MoonDrawRectangle(this->x + 10, this->y, barWidth, 16, focusColors[this->selected ? 0 : this->focused ? 1 : 2], true);
if(this->bind.bvar != NULL){
bool status = *this->bind.bvar;
Color toggleColors[] = {
{255, 32, 3, 255},
{32, 255, 32, 255}
{32, 255, 3, 255},
{255, 32, 3, 255}
};
string statusText = status ? Moon_GetKey("TEXT_OPT_ENABLED") : Moon_GetKey("TEXT_OPT_DISABLED");
tmpWidth += MoonGetTextWidth(statusText, scale, false);
MoonDrawText(this->x + ( 10 + barWidth / 2 ) - tmpWidth / 2 + titleWidth, this->y, statusText, scale, toggleColors[status ? 0 : 1] , true, false);
MoonDrawText(this->x + ( 10 + barWidth / 2 ) - tmpWidth / 2 + titleWidth, this->y, statusText, scale, toggleColors[status ? 0 : 1] , true, true);
} else if(this->bind.values != NULL && this->bind.index != NULL){
int index = *this->bind.index;
string text = (*this->bind.values)[index];
tmpWidth += MoonGetTextWidth(text, scale, false);
MoonDrawText(this->x + ( 10 + barWidth / 2 ) - tmpWidth / 2 + titleWidth, this->y, text, scale, {100, 100, 255, 255}, true, true);
} else if(isFloat || isInt){
float value = isFloat ? *this->bind.fvar : *this->bind.ivar;
float max = this->bind.max;
string text = to_string((int)(100 * (value / max))) + " %";
tmpWidth += MoonGetTextWidth(text, scale, false);
MoonDrawText(this->x + ( 10 + barWidth / 2 ) - tmpWidth / 2 + titleWidth, this->y, text, scale, {100, 100, 255, 255}, true, true);
}
MoonDrawText(this->x + ( 10 + barWidth / 2 ) - tmpWidth / 2, this->y, this->title, scale, {255, 255, 255, 255}, true, false);
MoonDrawText(this->x + ( 10 + barWidth / 2 ) - tmpWidth / 2, this->y, this->title, scale, {255, 255, 255, 255}, true, true);
}
void MWValue::Update(){
float xStick = GetStickValue(MoonButtons::L_STICK, false);
if(!this->focused) return;
if(xStick > 0) {
float xStick = GetStickValue(MoonButtons::L_STICK, false);
bool isBool = this->bind.bvar != NULL;
bool isArray = this->bind.values != NULL && this->bind.index != NULL;
bool isFloat = this->bind.fvar != NULL;
bool isInt = this->bind.ivar != NULL;
float maxValue = isArray ? (*this->bind.values).size() - 1 : this->bind.max;
float minValue = isArray ? 0 : this->bind.min;
float step = isArray ? 1 : this->bind.step;
if(xStick < 0) {
if(mwvStickExecuted) return;
if(this->bind.bvar != NULL && this->focused ) {
if(isBool) {
*this->bind.bvar = !*this->bind.bvar;
std::cout << "Executed" << std::endl;
} else if(isArray || isFloat || isInt) {
float cIndex = isArray ? (int) *this->bind.index : isFloat ? *this->bind.fvar : *this->bind.ivar;
cout << "Test" << endl;
if(cIndex > minValue){
if(isArray) *this->bind.index -= (int)step;
if(isFloat) *this->bind.fvar -= step;
if(isInt) *this->bind.ivar -= (int)step;
} else {
if(isArray) *this->bind.index = (int)maxValue;
if(isFloat) *this->bind.fvar = maxValue;
if(isInt) *this->bind.ivar = (int)maxValue;
}
std::cout << "Executed x2" << std::endl;
}
mwvStickExecuted = true;
}
if(xStick < 0) {
if(xStick > 0) {
if(mwvStickExecuted) return;
if(this->bind.bvar != NULL && this->focused ) {
if(isBool) {
*this->bind.bvar = !*this->bind.bvar;
std::cout << "Executed" << std::endl;
} else if(isArray || isFloat || isInt) {
float cIndex = isArray ? (int) *this->bind.index : isFloat ? *this->bind.fvar : *this->bind.ivar;
if(cIndex < maxValue){
if(isArray) *this->bind.index += (int)step;
if(isFloat) *this->bind.fvar += step;
if(isInt) *this->bind.ivar += (int)step;
} else {
if(isArray) *this->bind.index = (int)minValue;
if(isFloat) *this->bind.fvar = minValue;
if(isInt) *this->bind.ivar = (int)minValue;
}
std::cout << "Executed x3" << std::endl;
}
mwvStickExecuted = true;
}

View File

@ -3,11 +3,19 @@
#include "moon/ui/interfaces/moon-widget.h"
#include <string>
#include <vector>
struct MWValueBind{
float *fvar;
bool *bvar;
float *fvar;
int *ivar;
float max;
float min;
float step;
int *index;
std::vector<std::string>* values;
};
class MWValue : public MoonWidget {

View File

@ -42,14 +42,15 @@ void moon_draw_colored_text(f32 x, f32 y, const u8 *str, float scale, struct Col
}
void moon_draw_text(f32 x, f32 y, const u8 *str, float scale) {
UNUSED s8 mark = DIALOG_MARK_NONE; // unused in EU
UNUSED s8 mark = DIALOG_MARK_NONE;
s32 strPos = 0;
u8 lineNum = 1;
y -= 16;
Mtx *_Matrix = (Mtx *) alloc_display_list(sizeof(Mtx));
if (!_Matrix) return;
guScale(_Matrix, scale, scale - 0.1f, 1.f);
create_dl_translation_matrix(MENU_MTX_PUSH, x, y - 15, 0.0f);
guScale(_Matrix, scale, scale, 1.f);
create_dl_translation_matrix(MENU_MTX_PUSH, x, y, 0.0f);
gSPMatrix(gDisplayListHead++, VIRTUAL_TO_PHYSICAL(_Matrix), G_MTX_MODELVIEW | G_MTX_MUL | G_MTX_NOPUSH);
while (str[strPos] != DIALOG_CHAR_TERMINATOR) {
@ -81,7 +82,7 @@ void moon_draw_text(f32 x, f32 y, const u8 *str, float scale) {
break;
case DIALOG_CHAR_SPACE:
create_dl_translation_matrix(MENU_MTX_NOPUSH, (f32)(gDialogCharWidths[DIALOG_CHAR_SPACE]), 0.0f, 0.0f);
break; // ? needed to match
break;
default:
render_generic_char(str[strPos]);
if (mark != DIALOG_MARK_NONE) {