[WIP] Added widgets functionality and some fixes

This commit is contained in:
KiritoDv 2021-04-28 21:25:23 +00:00
parent 8e52a6caa8
commit abd0821422
16 changed files with 187 additions and 51 deletions

2
.gitignore vendored
View File

@ -57,7 +57,7 @@ build/*
*.sav
.assets-local.txt
sm64_save_file.bin
sm64config.txt
moon64config.txt
# Assets. Generally ignored, but ones with "custom" in the name are fine.
/levels/**/*.png

View File

@ -264,7 +264,7 @@ LEVEL_DIRS := $(patsubst levels/%,%,$(dir $(wildcard levels/*/header.h)))
SRC_DIRS := src src/engine src/game src/audio src/menu src/buffers actors levels bin data assets src/text src/text/libs src/pc src/pc/gfx src/pc/audio src/pc/controller src/pc/fs src/pc/fs/packtypes src/nx
# Moon64 SRC Directories
SRC_DIRS += src/moon src/moon/texts src/moon/utils src/moon/network src/moon/ui src/moon/ui/interfaces src/moon/ui/screens src/moon/ui/utils
SRC_DIRS += src/moon src/moon/texts src/moon/utils src/moon/network src/moon/ui src/moon/ui/interfaces src/moon/ui/screens src/moon/ui/utils src/moon/ui/widgets
# RapidJSON Library
SRC_DIRS += src/moon/libs/rapidjson src/moon/libs/rapidjson/error src/moon/libs/rapidjson/internal src/moon/libs/rapidjson/msinttypes

View File

@ -13,7 +13,7 @@ const LevelScript level_script_entry[] = {
#ifdef TOGGLE_GAME_DEBUG
EXECUTE(/*seg*/ 0x14, /*script*/ _introSegmentRomStart, /*scriptEnd*/ _introSegmentRomEnd, /*entry*/ level_intro_entry_4),
#else
EXECUTE(/*seg*/ 0x14, /*script*/ _introSegmentRomStart, /*scriptEnd*/ _introSegmentRomEnd, /*entry*/ level_intro_n64),
EXECUTE(/*seg*/ 0x14, /*script*/ _introSegmentRomStart, /*scriptEnd*/ _introSegmentRomEnd, /*entry*/ script_intro_L1),
#endif
JUMP(/*target*/ level_script_entry),
};

View File

@ -6,7 +6,7 @@
#include "pc/platform.h"
#include "pc/fs/fs.h"
#define FILENAME_FORMAT "%s/sm64_save_file_%d.sav"
#define FILENAME_FORMAT "%s/moon64_save_file_%d.sav"
#define NUM_COURSES 15
#define NUM_BONUS_COURSES 10
#define NUM_FLAGS 21

View File

@ -1,6 +1,9 @@
#include "moon-screen.h"
#include <algorithm>
#include <iostream>
extern "C" {
#include "engine/math_util.h"
#include "sm64.h"
#include "game/game_init.h"
#include "gfx_dimensions.h"
@ -8,27 +11,85 @@ extern "C" {
}
void MoonScreen::Init(){
for(int i = 0; i < widgets.size(); i++)
widgets[i]->Init();
this->scrollIndex = 0;
this->selected = NULL;
if(this->enabledWidgets)
for(int i = 0; i < widgets.size(); i++)
widgets[i]->Init();
}
void MoonScreen::Mount(){}
void MoonScreen::Mount(){
if(!this->widgets.empty()){
this->selected = this->widgets[0];
this->selected->selected = true;
this->selected->focused = false;
}
}
void MoonScreen::Draw(){
this->screenWidth = SCREEN_WIDTH + abs(GFX_DIMENSIONS_FROM_LEFT_EDGE(0)) * 2;
this->screenHeight = SCREEN_HEIGHT;
for(int i = 0; i < widgets.size(); i++)
widgets[i]->Draw();
if(this->enabledWidgets)
for(int i = 0; i < widgets.size(); i++)
widgets[i]->Draw();
}
bool stickExecuted;
void MoonScreen::Update(){
for(int i = 0; i < widgets.size(); i++)
widgets[i]->Update();
this->screenWidth = GFX_DIMENSIONS_ASPECT_RATIO > 1 ? SCREEN_WIDTH + abs(GFX_DIMENSIONS_FROM_LEFT_EDGE(0)) * 2 : SCREEN_WIDTH;
this->screenHeight = SCREEN_HEIGHT;
if(this->enabledWidgets) {
float xStick = this->GetValue(MoonButtons::L_STICK, false);
float yStick = this->GetValue(MoonButtons::U_STICK, false);
if(!this->widgets.empty()){
if(yStick > 0) {
if(stickExecuted) return;
if(this->selected != NULL) return;
this->widgets[this->scrollIndex]->selected = false;
if(this->scrollIndex > 0)
this->scrollIndex--;
else
this->scrollIndex = this->widgets.size() - 1;
this->widgets[this->scrollIndex]->selected = true;
stickExecuted = true;
}
if(yStick < 0) {
if(stickExecuted) return;
if(this->selected != NULL) return;
this->widgets[this->scrollIndex]->selected = false;
if(this->scrollIndex < this->widgets.size() - 1)
this->scrollIndex++;
else
this->scrollIndex = 0;
this->widgets[this->scrollIndex]->selected = true;
stickExecuted = true;
}
if(!yStick)
stickExecuted = false;
if(this->IsPressed(MoonButtons::A_BTN)) {
this->selected = this->widgets[this->scrollIndex];
this->selected->selected = false;
this->selected->focused = true;
}
if(this->IsPressed(MoonButtons::B_BTN)) {
if(this->selected != NULL){
this->selected->selected = true;
this->selected->focused = false;
this->selected = NULL;
}
}
}
for(int i = 0; i < widgets.size(); i++)
widgets[i]->Update();
}
}
void MoonScreen::Dispose(){
for(int i = 0; i < widgets.size(); i++)
widgets[i]->Dispose();
if(this->enabledWidgets)
for(int i = 0; i < widgets.size(); i++)
widgets[i]->Dispose();
}
bool MoonScreen::IsPressed(MoonButtons button){
@ -39,14 +100,14 @@ bool MoonScreen::IsDown(MoonButtons button){
return gPlayer1Controller->buttonDown & button;
}
float GetValue(MoonButtons button){
float MoonScreen::GetValue(MoonButtons button, bool absolute){
switch(button){
case MoonButtons::L_STICK:
case MoonButtons::R_STICK:
return gPlayer1Controller->stickX;
return absolute ? abs(gPlayer1Controller->stickX) : gPlayer1Controller->stickX;
case MoonButtons::U_STICK:
case MoonButtons::D_STICK:
return gPlayer1Controller->stickY;
return absolute ? abs(gPlayer1Controller->stickY) : gPlayer1Controller->stickY;
default:
return 0.f;
}

View File

@ -15,8 +15,11 @@ enum MoonButtons {
};
class MoonScreen {
private:
protected:
std::vector<MoonWidget*> widgets;
MoonWidget* selected;
bool enabledWidgets = true;
int scrollIndex = 0;
public:
virtual void Init();
virtual void Mount();
@ -25,7 +28,7 @@ public:
virtual void Dispose();
bool IsPressed(MoonButtons button);
bool IsDown(MoonButtons button);
float GetValue(MoonButtons button);
float GetValue(MoonButtons button, bool absolute);
float screenWidth;
float screenHeight;
};

View File

@ -1,8 +1,15 @@
#ifndef MoonWidgetInterface
#define MoonWidgetInterface
class MoonWidget {
class MoonWidget {
public:
float x;
float y;
bool enabled = true;
bool centered = true;
bool selectable = true;
bool selected = false;
bool focused = false;
virtual void Init(){}
virtual void Draw(){}
virtual void Update(){}

View File

@ -23,8 +23,8 @@ void MoonInitUI() {
if(screens.empty())
screens.push_back(new MoonTest());
screens[currentScreen]->Init();
screens[currentScreen]->Mount();
screens[currentScreen]->Init();
}
void MoonDrawUI() {
@ -45,8 +45,8 @@ void MoonChangeUI(int index){
}
if(!(isOpen && isRunning)) return;
currentScreen = index;
screens[currentScreen]->Init();
screens[currentScreen]->Mount();
screens[currentScreen]->Init();
}
void MoonHandleToggle(){

View File

@ -4,6 +4,8 @@
#include "moon/ui/moon-ui-manager.h"
#include "moon/network/moon-consumer.h"
#include "moon/ui/widgets/mw-value.h"
using namespace std;
extern "C" {
@ -12,7 +14,7 @@ extern "C" {
}
void MoonTest::Init(){
cout << "Screen called" << endl;
cout << "Screen called" << endl;
//MoonConsumer consumer;
//consumer.Init();
//MoonResponse res;
@ -21,6 +23,18 @@ void MoonTest::Init(){
// req.file = "Kalimba2.txt";
//consumer.Get(req, &res);
//printf("%s\n", res.body.c_str());
MoonScreen::Init();
}
void MoonTest::Mount(){
this->widgets.clear();
int a = 25;
this->widgets.push_back(new MWValue(&a, 0, 20, MWValueType::INT));
this->widgets.push_back(new MWValue(&a, 0, 40, MWValueType::INT));
this->widgets.push_back(new MWValue(&a, 0, 60, MWValueType::INT));
this->widgets.push_back(new MWValue(&a, 0, 80, MWValueType::INT));
MoonScreen::Mount();
}
int x = 0;
@ -28,23 +42,30 @@ int y = 30;
void MoonTest::Draw(){
if(this->IsDown(MoonButtons::L_CBTN)){
x -= 1;
}
//if(this->IsDown(MoonButtons::L_CBTN)){
// x -= 1;
//}
if(this->IsDown(MoonButtons::R_CBTN)){
x += 1;
}
if(this->IsDown(MoonButtons::U_CBTN)){
y -= 1;
}
//if(this->IsDown(MoonButtons::R_CBTN)){
// x += 1;
//}
//
//if(this->IsDown(MoonButtons::U_CBTN)){
// y -= 1;
//}
if(this->IsDown(MoonButtons::D_CBTN)){
y += 1;
}
//if(this->IsDown(MoonButtons::D_CBTN)){
// y += 1;
//}
MoonDrawRectangle(0, 0, this->screenWidth, this->screenHeight, {0, 0, 0, 100}, false);
MoonDrawText(x, y, "This is a test uwu", 1.0, {255, 255, 255, 255}, false);
MoonDrawColoredText(0, 50, "This is a test owo", 1.0, {255, 255, 255, 255}, false);
string test = "Placeholder";
float txtWidth = MoonGetTextWidth(test, 1.0, false);
MoonDrawRectangle(0, 0, this->screenWidth, this->screenHeight, {0, 0, 0, 255}, false);
MoonDrawText(this->screenWidth / 2 - txtWidth / 2, y, test, 1.0, {255, 255, 255, 255}, false);
// MoonDrawText(0, 40, test, 1.0, {255, 255, 255, 255}, false);
//MoonDrawColoredText(0, 50, "This is a test owo", 1.0, {255, 255, 255, 255}, false);
// std::cout << this->screenWidth << " : " << GFX_DIMENSIONS_ASPECT_RATIO << std::endl;
MoonScreen::Draw();
}

View File

@ -6,6 +6,7 @@ class MoonTest : public MoonScreen {
public:
void Init();
void Draw();
void Mount();
};

View File

@ -1,22 +1,23 @@
#include "moon-draw-utils.h"
#include <algorithm>
#include "gfx_dimensions.h"
float MoonGetTextWidth(std::string text, float scale, bool colored) {
return (float)moon_get_text_width(getTranslatedText(text.c_str()), scale, colored);
}
void MoonDrawText(float x, float y, std::string text, float scale, struct Color color, bool u4_3){
if(!u4_3){
x = GFX_DIMENSIONS_FROM_LEFT_EDGE(x);
y = SCREEN_HEIGHT - y;
}
if(!u4_3) x = GFX_DIMENSIONS_FROM_LEFT_EDGE(x);
gSPDisplayList(gDisplayListHead++, dl_ia_text_begin);
gDPSetEnvColor(gDisplayListHead++, color.r, color.g, color.b, color.a);
moon_draw_text(x, y, getTranslatedText(text.c_str()), scale);
moon_draw_text(x, SCREEN_HEIGHT - y, getTranslatedText(text.c_str()), scale);
gSPDisplayList(gDisplayListHead++, dl_ia_text_end);
gDPSetEnvColor(gDisplayListHead++, 255, 255, 255, 255);
}
void MoonDrawColoredText(float x, float y, std::string text, float scale, struct Color color, bool u4_3){
if(!u4_3){
x = GFX_DIMENSIONS_FROM_LEFT_EDGE(x);
}
if(!u4_3) x = GFX_DIMENSIONS_FROM_LEFT_EDGE(x);
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_begin);
gDPSetEnvColor(gDisplayListHead++, color.r, color.g, color.b, color.a);
std::transform(text.begin(), text.end(), text.begin(), ::toupper);

View File

@ -12,7 +12,7 @@ extern "C" {
#include "game/geo_misc.h"
#include "text/txtconv.h"
}
float MoonGetTextWidth (std::string text, float scale, bool colored);
void MoonDrawText (float x, float y, std::string text, float scale, struct Color color, bool u4_3);
void MoonDrawColoredText(float x, float y, std::string text, float scale, struct Color color, bool u4_3);
void MoonDrawRectangle (float x, float y, float w, float h, struct Color c, bool u4_3);

View File

@ -0,0 +1,21 @@
#include "mw-value.h"
#include <iostream>
#include "moon/ui/utils/moon-draw-utils.h"
// std::cout << ptr << std::endl;
// int* a = static_cast<int*>(ptr);
// int b = *a;
MWValue::MWValue(void* ptr, float x, float y, MWValueType type){
this->value = ptr;
this->x = x;
this->y = y;
}
void MWValue::Init(){}
void MWValue::Draw(){
MoonDrawText(this->x, this->y, this->focused ? "Focused" : this->selected ? "Selected" : "No selected", 1.0, {255, 255, 255, 255}, false);
}
void MWValue::Update(){}
void MWValue::Dispose(){}

View File

@ -0,0 +1,21 @@
#ifndef MoonWidgetValue
#define MoonWidgetValue
#include "moon/ui/interfaces/moon-widget.h"
enum MWValueType{
INT, FLOAT, BOOL
};
class MWValue : public MoonWidget {
private:
void* value;
public:
MWValue(void* ptr, float x, float y, MWValueType type);
void Init();
void Draw();
void Update();
void Dispose();
};
#endif

View File

@ -3,7 +3,7 @@
#include <stdbool.h>
#define CONFIGFILE_DEFAULT "sm64config.txt"
#define CONFIGFILE_DEFAULT "moon64config.txt"
#define MAX_BINDS 3
#define MAX_VOLUME 127

View File

@ -118,7 +118,7 @@ const char *sys_user_path(void) {
static char path[SYS_MAX_PATH] = { 0 };
// get the new pref path from SDL
char *sdlpath = SDL_GetPrefPath("", "sm64ex");
char *sdlpath = SDL_GetPrefPath("", "moon64");
if (sdlpath) {
const unsigned int len = strlen(sdlpath);
strncpy(path, sdlpath, sizeof(path));