# Changes

- Remove Scene Logic and only use Update as func
- Begin with UI7 Horizontal scrollin gimpl and add Alignment funcs (not functional yet)
- Generate a Lookup Table for Languages for faster access
This commit is contained in:
tobid7 2024-09-27 16:24:07 +02:00
parent 224daffaf7
commit eac36bcc6e
7 changed files with 134 additions and 108 deletions

View File

@ -8,8 +8,7 @@ class ThemeEditor : public Palladium::Scene {
ThemeEditor();
~ThemeEditor();
void Draw(void) const override;
void Logic() override;
void Update() override;
private:
Theme::Ref edit_theme;
@ -17,13 +16,13 @@ class ThemeEditor : public Palladium::Scene {
Theme::Ref temp_theme;
// temp vars for samples
mutable bool cm;
mutable std::string inpt;
mutable int menu = 0;
bool cm;
std::string inpt;
int menu = 0;
// Keyboard
mutable PDKeyboardState kbd_state;
mutable std::string kbd_text;
mutable std::vector<std::string> theme_list;
PDKeyboardState kbd_state;
std::string kbd_text;
std::vector<std::string> theme_list;
};
} // namespace Palladium

View File

@ -14,7 +14,9 @@ enum UI7MenuFlags_ {
UI7MenuFlags_None = 0,
UI7MenuFlags_NoTitlebar = UI7MAKEFLAG(0),
UI7MenuFlags_TitleMid = UI7MAKEFLAG(1),
UI7MenuFlags_Scrolling = MAKEFLAG(2),
UI7MenuFlags_HzScrolling = MAKEFLAG(2),
UI7MenuFlags_VtScrolling = MAKEFLAG(3),
UI7MenuFlags_Scrolling = UI7MenuFlags_HzScrolling | UI7MenuFlags_VtScrolling,
};
enum UI7Horizontal {
@ -121,6 +123,11 @@ float GetScrollingOffset();
void SetScrollingOffset(float off);
bool IsScrolling();
} // namespace Menu
namespace Next {
// Alignment in ScreenSpace
void Align(UI7Horizontal hz = UI7Horizontal_Left,
UI7Vertical vt = UI7Vertical_Top);
} // namespace Next
// DrawLists
UI7DrawList::Ref GetForegroundList();
UI7DrawList::Ref GetBackgroundList();

View File

@ -15,19 +15,19 @@
#include <3ds.h>
#include <citro3d.h>
/// Palladium Includes
#include <pd/base/Color.hpp>
#include <pd/base/FunctionTrace.hpp>
#include <pd/Hardware.hpp>
#include <pd/base/Memory.hpp>
#include <pd/Overlays.hpp>
#include <pd/Ovl.hpp>
#include <pd/ResultDecoder.hpp>
#include <pd/Sprite.hpp>
#include <pd/Tasks.hpp>
#include <pd/Time.hpp>
#include <pd/base/Color.hpp>
#include <pd/base/FunctionTrace.hpp>
#include <pd/base/Lang.hpp>
#include <pd/parameter.hpp>
#include <pd/base/Memory.hpp>
#include <pd/base/stringtool.hpp>
#include <pd/parameter.hpp>
#include <pd/thread.hpp>
#define PDVSTRING "1.0.0"
@ -49,9 +49,7 @@ class Scene {
static std::stack<std::unique_ptr<Scene>> scenes;
/// @brief Deconstructor
virtual ~Scene() {}
virtual void Logic() = 0;
/// @brief Draw Func to Override
virtual void Draw() const = 0;
virtual void Update() = 0;
/// @brief Push a Scene to Stack
/// @param scene Scene to Push
/// @param fade FadeEffect (Not Correctly Implementet yet)
@ -59,8 +57,7 @@ class Scene {
/// @brief Go Back a Scene
static void Back();
/// @brief do the Draw (Called in Palladium::MainLoop())
static void doDraw();
static void doLogic();
static void doUpdate();
};
/// @brief Integrated Setting Menu of Palladium
@ -102,12 +99,9 @@ class RSettings : public Palladium::Scene {
public:
/// @brief Constructor
RSettings();
/// @brief Override for Draw
/// @param
void Draw(void) const override;
/// @brief Deconstructor
~RSettings();
void Logic() override;
void Update() override;
};
/// @brief Show Up the Palladium-Settings Menu

View File

@ -38,7 +38,8 @@ Palladium::ThemeEditor::~ThemeEditor() {
Palladium::ThemeSet(temp_theme);
}
void Palladium::ThemeEditor::Draw() const {
void Palladium::ThemeEditor::Update() {
// Rendering / UI Logic
Palladium::LI::OnScreen(false);
if (UI7::BeginMenu("Palladium -> Theme Editor")) {
UI7::Label("Sample Text");
@ -111,9 +112,8 @@ void Palladium::ThemeEditor::Draw() const {
}
UI7::EndMenu();
}
}
void Palladium::ThemeEditor::Logic() {
// Standart Logic
if (kbd_state) {
if (kbd_state == PDKeyboardState_Confirm) {
auto path =

View File

@ -332,21 +332,30 @@ struct UI7Promt {
PD_SMART_CTOR(UI7Promt)
};
struct UI7_ItemOpts {
UI7Vertical vertical_align = UI7Vertical_Top;
UI7Horizontal horizontal_align = UI7Horizontal_Left;
};
struct UI7Menu {
UI7Menu() {}
UI7ID menuid; // menu ID
NVec2 cursor; // cursor
NVec2 cb; // backup cursor
NVec2 slc; // sameline cursor
NVec2 screen_size; // MenuScreenSize
float scrolling_offset = 0.f; // MenuScrolling Pos
bool enable_scrolling = false; // Menu Scrolling
float scrolling_mod = 0.f; // For Menu Scrolling effect
float tbh; // TabBar Height
bool show_scroolbar = true; // Show Scrollbar
bool scrolling_possible = true; // Scrolling Possible?
bool has_touch = false; // To Disable touch on Top Screen
NTCtrl::Ref ctrl; // NonTouchControl
UI7ID menuid; // menu ID
NVec2 cursor; // cursor
NVec2 cb; // backup cursor
NVec2 slc; // sameline cursor
NVec2 screen_size; // MenuScreenSize
NVec2 scrolling_offset; // MenuScrolling Pos
bool vertical_scrolling = false; // Vertical Menu Scrolling
bool horizontal_scrolling = false; // Horizontal Menu Scrolling
NVec2 scrolling_mod; // For Menu Scrolling effect
float tbh = 0.f; // TabBar Height
bool show_vt_scroolbar = true; // Show Vertical Scrollbar
bool show_hz_scroolbar = true; // Show Horizontal Scrollbar
bool vtscrolling_possible = true; // Vertical Scrolling Possible?
bool hzscrolling_possible = true; // Horizontal Scrolling Possible?
bool has_touch = false; // To Disable touch on Top Screen
NTCtrl::Ref ctrl; // NonTouchControl / Not in dev yet
UI7_ItemOpts next_item_opts; // Next Itam Options
// SubMenu
std::string submenu;
@ -472,12 +481,13 @@ bool UI7CtxBeginMenu(const std::string &lb) {
void UI7CtxEndMenu() {
if (!UI7CtxValidate()) return;
if (!UI7CtxInMenu()) return;
// Draw Scrollbar
if (ui7_ctx->cm->enable_scrolling) {
ui7_ctx->cm->scrolling_possible = (ui7_ctx->cm->ms[1] < 235 ? false : true);
ui7_ctx->cm->show_scroolbar = ui7_ctx->cm->scrolling_possible;
// Draw Vertical Scrollbar
if (ui7_ctx->cm->vertical_scrolling) {
ui7_ctx->cm->vtscrolling_possible =
(ui7_ctx->cm->ms[1] < 235 ? false : true);
ui7_ctx->cm->show_vt_scroolbar = ui7_ctx->cm->vtscrolling_possible;
if (ui7_ctx->cm->show_scroolbar) {
if (ui7_ctx->cm->show_vt_scroolbar) {
// Screen Width
int sw = Palladium::LI::GetScreenSize().x();
// Top Start Pos
@ -486,6 +496,8 @@ void UI7CtxEndMenu() {
int slider_w = 4;
// Height of Slider
int szs = 240 - tsp - 5;
// Modify if we have a horizontal scrollbar
if (ui7_ctx->cm->show_hz_scroolbar) szs -= slider_w - 2;
// Lowest Height of Slider Obj
int lszs = 20; // Lowest Slider size
// Calculate Slider Height
@ -499,30 +511,31 @@ void UI7CtxEndMenu() {
// Process MenuDragging
auto objmbg = UI7CtxGetObject("menu_bg" + ui7_ctx->cm->menuid.real_id);
// Patch that sets scrolling to 0 if max pos is not out of screen
if (ui7_ctx->cm->scrolling_offset != 0.f && ui7_ctx->cm->ms[1] < 235) {
ui7_ctx->cm->scrolling_offset = 0.f;
if (ui7_ctx->cm->scrolling_offset[1] != 0.f && ui7_ctx->cm->ms[1] < 235) {
ui7_ctx->cm->scrolling_offset[1] = 0.f;
}
/// TODO: Use Deltatime somehow here
// Auto scroll back if last object is on screen
if (ui7_ctx->cm->scrolling_offset > ui7_ctx->cm->ms[1] - 240 &&
if (ui7_ctx->cm->scrolling_offset[1] > ui7_ctx->cm->ms[1] - 240 &&
ui7_ctx->cm->ms[1] != 0 && ui7_ctx->cm->ms[1] >= 235) {
ui7_ctx->cm->scrolling_offset -= 0.3 * ui7_ctx->delta;
ui7_ctx->cm->scrolling_offset[1] -= 3.f;
// Patch to Scroll to perfect pos
if (ui7_ctx->cm->scrolling_offset < ui7_ctx->cm->ms[1] - 240) {
ui7_ctx->cm->scrolling_offset = ui7_ctx->cm->ms[1] - 240;
if (ui7_ctx->cm->scrolling_offset[1] < ui7_ctx->cm->ms[1] - 240) {
ui7_ctx->cm->scrolling_offset[1] = ui7_ctx->cm->ms[1] - 240;
}
}
// Auto Scroll back if offset gets below 0
if (ui7_ctx->cm->scrolling_offset < 0) {
ui7_ctx->cm->scrolling_offset += 0.3 * ui7_ctx->delta;
if (ui7_ctx->cm->scrolling_offset > 0)
ui7_ctx->cm->scrolling_offset = 0;
if (ui7_ctx->cm->scrolling_offset[1] < 0) {
ui7_ctx->cm->scrolling_offset[1] += 3.f;
if (ui7_ctx->cm->scrolling_offset[1] > 0)
ui7_ctx->cm->scrolling_offset[1] = 0;
}
// Zero out scrolling_mod if it goeas < -40
// or > 40 over the max size
if (ui7_ctx->cm->scrolling_offset < -40 ||
ui7_ctx->cm->scrolling_offset > ui7_ctx->cm->ms[1] - 200) {
ui7_ctx->cm->scrolling_mod = 0.f;
if (ui7_ctx->cm->scrolling_offset[1] < -40 ||
ui7_ctx->cm->scrolling_offset[1] > ui7_ctx->cm->ms[1] - 200) {
ui7_ctx->cm->scrolling_mod[1] = 0.f;
}
if (ui7_ctx->cm->has_touch) {
auto np = Palladium::Hid::GetTouchPosition();
@ -545,15 +558,15 @@ void UI7CtxEndMenu() {
NVec2(8, 240 - ui7_ctx->cm->tbh - 10))) {
objmbg->is_dragged = true;
// Check if and do nothing if the scrolling ofset goes out of screen
if (ui7_ctx->cm->scrolling_offset < ui7_ctx->cm->ms[1] - 200 &&
ui7_ctx->cm->scrolling_offset > -40) {
if (ui7_ctx->cm->scrolling_offset[1] < ui7_ctx->cm->ms[1] - 200 &&
ui7_ctx->cm->scrolling_offset[1] > -40) {
float cursor_mod = (ui7_ctx->cm->mdp[1] - np[1]);
if (ui7_ctx->cm->scrolling_mod <= 4.f &&
ui7_ctx->cm->scrolling_mod >= -4 && cursor_mod != 0.0f) {
if (ui7_ctx->cm->scrolling_mod[1] <= 4.f &&
ui7_ctx->cm->scrolling_mod[1] >= -4 && cursor_mod != 0.0f) {
if (cursor_mod > 2) {
ui7_ctx->cm->scrolling_mod = cursor_mod;
ui7_ctx->cm->scrolling_mod[1] = cursor_mod;
} else if (cursor_mod < -2) {
ui7_ctx->cm->scrolling_mod = cursor_mod;
ui7_ctx->cm->scrolling_mod[1] = cursor_mod;
}
}
}
@ -563,18 +576,18 @@ void UI7CtxEndMenu() {
}
}
// New Scrolling efect
if (ui7_ctx->cm->scrolling_mod != 0)
ui7_ctx->cm->scrolling_offset += ui7_ctx->cm->scrolling_mod;
if (ui7_ctx->cm->scrolling_mod[1] != 0)
ui7_ctx->cm->scrolling_offset[1] += ui7_ctx->cm->scrolling_mod[1];
// Slow out the effect
if (ui7_ctx->cm->scrolling_mod < 0.f) {
ui7_ctx->cm->scrolling_mod += 0.4f;
if (ui7_ctx->cm->scrolling_mod > 0.f) {
ui7_ctx->cm->scrolling_mod = 0.f;
if (ui7_ctx->cm->scrolling_mod[1] < 0.f) {
ui7_ctx->cm->scrolling_mod[1] += 0.4f;
if (ui7_ctx->cm->scrolling_mod[1] > 0.f) {
ui7_ctx->cm->scrolling_mod[1] = 0.f;
}
} else if (ui7_ctx->cm->scrolling_mod > 0.f) {
ui7_ctx->cm->scrolling_mod -= 0.4f;
if (ui7_ctx->cm->scrolling_mod < 0.f) {
ui7_ctx->cm->scrolling_mod = 0.f;
} else if (ui7_ctx->cm->scrolling_mod[1] > 0.f) {
ui7_ctx->cm->scrolling_mod[1] -= 0.4f;
if (ui7_ctx->cm->scrolling_mod[1] < 0.f) {
ui7_ctx->cm->scrolling_mod[1] = 0.f;
}
}
// Process Slider Dragging
@ -595,7 +608,7 @@ void UI7CtxEndMenu() {
(szs - slider_rh - 4)),
0.0f, 1.0f);
ui7_ctx->cm->scrolling_offset =
ui7_ctx->cm->scrolling_offset[1] =
drag_pos * (ui7_ctx->cm->ms[1] - 240.0f);
}
} else if (Palladium::Hid::IsEvent("touch", Palladium::Hid::Up) &&
@ -608,7 +621,7 @@ void UI7CtxEndMenu() {
tsp +
std::clamp(static_cast<float>(
(szs - slider_rh - 4) *
(static_cast<float>(ui7_ctx->cm->scrolling_offset) /
(static_cast<float>(ui7_ctx->cm->scrolling_offset[1]) /
static_cast<float>(ui7_ctx->cm->ms[1] - 240.f))),
0.f, static_cast<float>(szs - slider_rh - 4));
@ -619,8 +632,8 @@ void UI7CtxEndMenu() {
NVec2(slider_w, slider_rh), slider_clr);
} else {
// Set scrollingoffset and mod to 0 if not scrolling enabled
ui7_ctx->cm->scrolling_offset = 0.f;
ui7_ctx->cm->scrolling_mod = 0.f;
ui7_ctx->cm->scrolling_offset[1] = 0.f;
ui7_ctx->cm->scrolling_mod[1] = 0.f;
}
}
ui7_ctx->active_menus.push_back(ui7_ctx->cm);
@ -771,6 +784,10 @@ bool Button(const std::string &label, NVec2 size) {
}
PDColor btn = PDColor_Button;
NVec2 pos = GetCursorPos();
if (ui7_ctx->cm->next_item_opts.horizontal_align == UI7Horizontal_Center) {
pos[0] =
((ui7_ctx->cm->screen_size[0] - 10) * 0.5) - ((size[0] - pos[0]) * 0.5);
}
MoveCursor(size);
ui7_ctx->cm->ctrl->AddObj();
@ -870,7 +887,7 @@ void Progressbar(float value) {
if (!UI7CtxValidate()) return;
NVec2 pos = GetCursorPos();
NVec2 size = NVec2(Palladium::LI::GetScreenSize().x() - (pos[0] * 2), 20);
if (ui7_ctx->cm->show_scroolbar && ui7_ctx->cm->enable_scrolling)
if (ui7_ctx->cm->show_vt_scroolbar && ui7_ctx->cm->vertical_scrolling)
size[0] -= 16;
MoveCursor(size);
ui7_ctx->cm->ctrl->AddObj();
@ -1004,7 +1021,8 @@ bool BeginMenu(const std::string &title, NVec2 size, UI7MenuFlags flags) {
ui7_ctx->cm->tbh = 0.f;
}
if (flags & UI7MenuFlags_TitleMid) txtflags = PDTextFlags_AlignMid;
ui7_ctx->cm->enable_scrolling = (flags & UI7MenuFlags_Scrolling);
ui7_ctx->cm->vertical_scrolling = (flags & UI7MenuFlags_VtScrolling);
ui7_ctx->cm->horizontal_scrolling = (flags & UI7MenuFlags_HzScrolling);
// Render
ui7_ctx->cm->background->AddRectangle(NVec2(), size, PDColor_Background);
@ -1087,7 +1105,7 @@ void ColorSelector(const std::string &label, unsigned int &color) {
InBox(Palladium::Hid::GetLastTouchPosition(), pos, inp)) {
ui7_ctx->cm->submenu = id.ID();
// Nullify scrolling mod to fix freeze
ui7_ctx->cm->scrolling_mod = 0.0f;
ui7_ctx->cm->scrolling_mod[1] = 0.0f;
Palladium::Hid::Lock();
Palladium::Hid::Clear();
}
@ -1308,7 +1326,7 @@ void Separator() {
if (!UI7CtxInMenu()) return;
NVec2 pos = GetCursorPos();
NVec2 size = NVec2(
ui7_ctx->cm->screen_size[0] - (ui7_ctx->cm->enable_scrolling ? 24 : 10),
ui7_ctx->cm->screen_size[0] - (ui7_ctx->cm->vertical_scrolling ? 24 : 10),
1);
MoveCursor(size);
ui7_ctx->cm->ctrl->AddObj();
@ -1327,20 +1345,20 @@ void Debug() {
float Menu::GetScrollingOffset() {
if (!UI7CtxValidate()) return 0.f;
if (!UI7CtxInMenu()) return 0.f;
return ui7_ctx->cm->scrolling_offset;
return ui7_ctx->cm->scrolling_offset[1];
}
void Menu::SetScrollingOffset(float off) {
if (!UI7CtxValidate()) return;
if (!UI7CtxInMenu()) return;
ui7_ctx->cm->scrolling_offset = off;
ui7_ctx->cm->scrolling_mod = 0.f;
ui7_ctx->cm->scrolling_offset[1] = off;
ui7_ctx->cm->scrolling_mod[1] = 0.f;
}
bool Menu::IsScrolling() {
if (!UI7CtxValidate()) return false;
if (!UI7CtxInMenu()) return false;
return ui7_ctx->cm->scrolling_mod != 0.f;
return ui7_ctx->cm->scrolling_mod[1] != 0.f;
}
void MoveCursor(NVec2 size) {
@ -1356,12 +1374,13 @@ void MoveCursor(NVec2 size) {
ui7_ctx->cm->cursor += NVec2(0, size[1] + 5);
}
ui7_ctx->cm->ms = NVec2(ui7_ctx->cm->slc[0], ui7_ctx->cm->cursor[1]);
ui7_ctx->cm->next_item_opts = UI7_ItemOpts();
}
bool HandleScrolling(NVec2 &pos, NVec2 size) {
if (ui7_ctx->cm->enable_scrolling) {
if (ui7_ctx->cm->vertical_scrolling) {
NVec2 pb = pos;
pos -= NVec2(0, ui7_ctx->cm->scrolling_offset);
pos -= NVec2(0, ui7_ctx->cm->scrolling_offset[1]);
if (pos[1] > 240 ||
(pos[1] + size[1] < ui7_ctx->cm->tbh - 5 && pb[1] > ui7_ctx->cm->tbh))
return true;
@ -1419,4 +1438,11 @@ UI7DrawList::Ref Menu::GetForegroundList() {
if (!UI7CtxInMenu()) return ui7_ctx->bdl;
return ui7_ctx->cm->front;
}
void Next::Align(UI7Horizontal hz, UI7Vertical vt) {
if (!UI7CtxValidate()) return;
if (!UI7CtxInMenu()) return;
ui7_ctx->cm->next_item_opts.horizontal_align = hz;
ui7_ctx->cm->next_item_opts.vertical_align = vt;
}
} // namespace UI7

View File

@ -2,10 +2,14 @@
#include <filesystem>
#include <fstream>
#include <map>
#include <pd/base/Lang.hpp>
static nlohmann::json appJson;
/// Lang Map KEY STRING
static std::map<std::string, std::string> lang_table;
std::string Palladium::Lang::GetSys() {
u8 language = 1;
CFGU_GetSystemLanguage(&language);
@ -65,10 +69,9 @@ std::string Palladium::Lang::GetSys() {
}
}
std::string Palladium::Lang::Get(const std::string &key) {
if (!appJson.contains("keys")) return "ERR-01";
nlohmann::json js = appJson["keys"];
if (!js.contains(key)) return key;
return js.at(key).get<std::string>();
auto tmp = lang_table.find(key);
if (tmp == lang_table.end()) return "ERR-02";
return tmp->second;
}
void Palladium::Lang::Load(const std::string &lang) {
@ -82,7 +85,6 @@ void Palladium::Lang::Load(const std::string &lang) {
if (appJson.is_discarded()) {
appJson = {};
}
return;
} else {
values.open("romfs:/lang/en/app.json", std::ios::in);
if (values.is_open()) {
@ -92,7 +94,12 @@ void Palladium::Lang::Load(const std::string &lang) {
if (appJson.is_discarded()) {
appJson = {};
}
return;
}
lang_table.clear();
if (appJson.contains("keys")) {
for (auto &it : appJson["keys"].items()) {
lang_table[it.key()] = it.value().get<std::string>();
}
}
}

View File

@ -212,15 +212,10 @@ void Palladium::Init::NdspFirm() {
}
}
void Palladium::Scene::doDraw() {
Ftrace::ScopedTrace st("pd-core", f2s(Scene::doDraw));
if (!Palladium::Scene::scenes.empty()) Palladium::Scene::scenes.top()->Draw();
}
void Palladium::Scene::doLogic() {
Ftrace::ScopedTrace st("pd-core", f2s(Scene::doLogic));
void Palladium::Scene::doUpdate() {
Ftrace::ScopedTrace st("pd-core", f2s(Scene::doUpdate));
if (!Palladium::Scene::scenes.empty())
Palladium::Scene::scenes.top()->Logic();
Palladium::Scene::scenes.top()->Update();
}
void Palladium::Scene::Load(std::unique_ptr<Scene> scene, bool fade) {
@ -278,8 +273,7 @@ bool Palladium::MainLoop() {
C3D_RenderTargetClear(pd_bottom, C3D_CLEAR_ALL, 0x00000000, 0);
frameloop();
if (pdi_enable_scene_system) {
Palladium::Scene::doDraw();
Palladium::Scene::doLogic();
Palladium::Scene::doUpdate();
}
return pdi_running;
}
@ -470,8 +464,7 @@ void Palladium::FrameEnd() {
Ftrace::ScopedTrace st("pd-core", f2s(FrameEnd));
C3D_FrameBegin(2);
if (!pdi_enable_scene_system && pdi_settings) {
Palladium::Scene::doDraw();
Palladium::Scene::doLogic();
Palladium::Scene::doUpdate();
}
UI7::Update();
UI7::Debug();
@ -510,7 +503,9 @@ std::vector<std::string> StrHelper(std::string input) {
return test1;
}
void Palladium::RSettings::Draw(void) const {
void Palladium::RSettings::Update() {
// Rendering / UI Logic
/// TODO: Update code for new system
if (m_state == RSETTINGS) {
LI::OnScreen(false);
if (UI7::BeginMenu("Palladium -> Settings")) {
@ -793,9 +788,7 @@ void Palladium::RSettings::Draw(void) const {
UI7::EndMenu();
}
}
}
void Palladium::RSettings::Logic() {
// Standart Logic
/// Requests
for (const auto &it : shared_request) {
if (it.first == 0x00000001) {