Clearing out template stuff and adding basic styles
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
#include "health_bar_element.hpp"
|
||||
#include <RmlUi/Core/Factory.h>
|
||||
#include <RmlUi/Core/ID.h>
|
||||
#include <RmlUi/Core/Math.h>
|
||||
#include <RmlUi/Core/Property.h>
|
||||
#include <RmlUi/Core/Types.h>
|
||||
#include <RmlUi/Core/Unit.h>
|
||||
|
||||
HealthBarElement::HealthBarElement(const Rml::String& tag) : Rml::Element(tag) {
|
||||
Rml::XMLAttributes attributes;
|
||||
|
||||
Rml::ElementPtr track = Rml::Factory::InstanceElement(this, "div", "div", attributes);
|
||||
track->SetClass("healthbar-track", true);
|
||||
|
||||
Rml::ElementPtr fillElement = Rml::Factory::InstanceElement(this, "div", "div", attributes);
|
||||
fillElement->SetClass("healthbar-fill", true);
|
||||
fill = fillElement.get();
|
||||
|
||||
track->AppendChild(std::move(fillElement));
|
||||
AppendChild(std::move(track));
|
||||
|
||||
UpdateFill();
|
||||
}
|
||||
|
||||
void HealthBarElement::OnAttributeChange(const Rml::ElementAttributes &changed_attributes) {
|
||||
Rml::Element::OnAttributeChange(changed_attributes);
|
||||
if (changed_attributes.find("current") != changed_attributes.end() ||
|
||||
changed_attributes.find("max") != changed_attributes.end()) {
|
||||
UpdateFill();
|
||||
}
|
||||
}
|
||||
|
||||
void HealthBarElement::UpdateFill() {
|
||||
int current = GetAttribute<int>("current", 0);
|
||||
int max = GetAttribute<int>("max", 100);
|
||||
if (max <= 0) max = 1;
|
||||
|
||||
float pct = Rml::Math::Clamp(static_cast<float>(current) / static_cast<float>(max), 0.0f, 1.0f) * 100.0f;
|
||||
|
||||
if (fill) {
|
||||
fill->SetProperty(Rml::PropertyId::Width, Rml::Property(pct, Rml::Unit::PERCENT));
|
||||
|
||||
if (current >= 90) {
|
||||
fill->SetProperty(Rml::PropertyId::BackgroundColor, Rml::Property("#33cc33", Rml::Unit::COLOUR));
|
||||
} else if (current >= 50) {
|
||||
fill->SetProperty(Rml::PropertyId::BackgroundColor, Rml::Property("#cccc33", Rml::Unit::COLOUR));
|
||||
} else if (current >= 0) {
|
||||
fill->SetProperty(Rml::PropertyId::BackgroundColor, Rml::Property("#cc3333", Rml::Unit::COLOUR));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
#include <RmlUi/Core/Element.h>
|
||||
#include <RmlUi/Core/Types.h>
|
||||
|
||||
class HealthBarElement : public Rml::Element {
|
||||
public:
|
||||
explicit HealthBarElement(const Rml::String& tag);
|
||||
|
||||
void OnAttributeChange(const Rml::ElementAttributes &changed_attributes) override;
|
||||
|
||||
private:
|
||||
void UpdateFill();
|
||||
Rml::Element* fill = nullptr;
|
||||
};
|
||||
24
src/main.cpp
24
src/main.cpp
@@ -11,13 +11,10 @@
|
||||
#include <RmlUi_Backend.h>
|
||||
#include <RmlUi/Debugger.h>
|
||||
#include <RmlUi/Lua/Lua.h>
|
||||
#include <memory>
|
||||
#include "app.hpp"
|
||||
#include "element_registry.hpp"
|
||||
#include "pages/test_page.hpp"
|
||||
#include "ButtonClickListener.hpp"
|
||||
#include "lui_app_api.hpp"
|
||||
#include "elements/health_bar_element.hpp"
|
||||
|
||||
static bool is_initalized = false;
|
||||
static bool is_running = false;
|
||||
@@ -26,7 +23,7 @@ static App *app;
|
||||
static ElementRegistry elementRegistry;
|
||||
|
||||
void RegisterElements() {
|
||||
elementRegistry.Register<HealthBarElement>("healthbar");
|
||||
|
||||
}
|
||||
|
||||
bool InitalizeApplication() {
|
||||
@@ -58,6 +55,9 @@ bool InitalizeApplication() {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string assets_dir = App::GetAssetsPath();
|
||||
Rml::LoadFontFace(assets_dir + "/fonts/PTSerif-Regular.ttf");
|
||||
|
||||
lua_State* L = Rml::Lua::Interpreter::GetLuaState();
|
||||
AppLuaApi *lua_app_api = new AppLuaApi(app, L);
|
||||
lua_app_api->RegisterLuaBindings();
|
||||
@@ -105,22 +105,6 @@ int main() {
|
||||
|
||||
std::cout << "Loading UI...\n";
|
||||
|
||||
app->RegisterPage<TestPagePage>("test-page");
|
||||
|
||||
app->SetupListeners([](Rml::ElementDocument *doc) {
|
||||
if (auto *btn = doc->GetElementById("nav-test")) {
|
||||
app->RegisterListener(btn, std::make_unique<ButtonClickListener>([](Rml::Event&){
|
||||
app->NavigateTo("test-page");
|
||||
}));
|
||||
}
|
||||
|
||||
if (auto *btn = doc->GetElementById("nav-home")) {
|
||||
app->RegisterListener(btn, std::make_unique<ButtonClickListener>([](Rml::Event&){
|
||||
app->NavigateTo("home");
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
app->LoadUi();
|
||||
app->NavigateTo("home");
|
||||
std::cout << "UI Loaded\n";
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
#include "test_page.hpp"
|
||||
#include <RmlUi/Core/DataModelHandle.h>
|
||||
#include <RmlUi/Core/Element.h>
|
||||
#include <iostream>
|
||||
#include "ButtonClickListener.hpp"
|
||||
|
||||
void TestPagePage::SetupDataModel(Rml::DataModelConstructor constructor) {
|
||||
constructor.Bind("greeting", &data.greeting);
|
||||
constructor.Bind("counter", &data.counter);
|
||||
}
|
||||
|
||||
void TestPagePage::OnEnter(Rml::Element *root) {
|
||||
if (!root) {
|
||||
std::cout << "Was not root\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (Rml::Element *btn = root->GetElementById("test-btn")) {
|
||||
RegisterListener(btn, std::make_unique<ButtonClickListener>([this](Rml::Event&) {
|
||||
data.counter++;
|
||||
std::cout << "Button clicked, counter is now: " << data.counter << "\n";
|
||||
DirtyVariable("counter");
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
void TestPagePage::OnExit() {}
|
||||
@@ -1,20 +0,0 @@
|
||||
#pragma once
|
||||
#include "page.hpp"
|
||||
#include <RmlUi/Core/DataModelHandle.h>
|
||||
|
||||
struct TestPageData {
|
||||
std::string greeting = "Hello from the test page!";
|
||||
int counter = 0;
|
||||
};
|
||||
|
||||
class TestPagePage : public PageBase {
|
||||
public:
|
||||
using PageBase::PageBase;
|
||||
|
||||
void SetupDataModel(Rml::DataModelConstructor constructor) override;
|
||||
void OnEnter(Rml::Element *root) override;
|
||||
void OnExit() override;
|
||||
|
||||
private:
|
||||
TestPageData data;
|
||||
};
|
||||
Reference in New Issue
Block a user