Skip to content

Commit

Permalink
+ parts of UI
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualGMQ committed May 8, 2024
1 parent 284e14e commit 3a0176d
Show file tree
Hide file tree
Showing 24 changed files with 760 additions and 311 deletions.
2 changes: 1 addition & 1 deletion 3rdlibs/gecs
4 changes: 3 additions & 1 deletion editor/src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ struct EditorContext: public nickel::Singlton<EditorContext, true> {
};

inline void InitEditorContext(
gecs::commands cmds, gecs::resource<gecs::mut<nickel::Camera>> camera) {
gecs::commands cmds, gecs::resource<gecs::mut<nickel::Camera>> camera,
gecs::resource<gecs::mut<nickel::ui::UIContext>> uiCtx) {
EditorContext::Init();
auto& target = EditorContext::Instance().texture;
camera->SetRenderTarget(target->View());
uiCtx->renderCtx.camera.SetRenderTarget(target->View());
}
3 changes: 3 additions & 0 deletions editor/src/game_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void GameWindow::update() {

auto reg = nickel::ECS::Instance().World().cur_registry();
auto camera = reg->res_mut<nickel::Camera>();
auto& uiCamera = reg->res_mut<nickel::ui::UIContext>()->renderCtx.camera;
auto mouse = reg->res<nickel::Mouse>();

// draw grid
Expand Down Expand Up @@ -61,6 +62,7 @@ void GameWindow::update() {
auto offset = mouse->Offset();
auto scale = camera->Scale();
camera->Move(nickel::cgmath::Vec2{-offset.x, offset.y} / scale);
uiCamera.Move(nickel::cgmath::Vec2{-offset.x, offset.y} / scale);
}

if (auto wheel = mouse->WheelOffset(); wheel.y != 0) {
Expand All @@ -70,6 +72,7 @@ void GameWindow::update() {
newScale.Set(0.01, 0.01);
}
camera->ScaleTo(newScale);
uiCamera.ScaleTo(newScale);
}
}

Expand Down
101 changes: 13 additions & 88 deletions editor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void EditorEnter(
gecs::resource<gecs::mut<nickel::AssetManager>> assetMgr,
gecs::resource<gecs::mut<nickel::FontManager>> fontMgr,
gecs::resource<gecs::mut<nickel::Camera>> camera,
gecs::resource<gecs::mut<nickel::ui::UIContext>> uiCtx,
gecs::event_dispatcher<ReleaseAssetEvent> releaseAsetEvent,
gecs::event_dispatcher<FileChangeEvent> fileChangeEvent,
gecs::event_dispatcher<nickel::DropFileEvent> dropFileEvent,
Expand All @@ -103,9 +104,8 @@ void EditorEnter(
const auto& halfGameWindowSize = EditorContext::Instance().projectInfo.windowData.size * 0.5;
auto& target = EditorContext::Instance().texture;
auto halfTextureSize = target->Size() * 0.5f;

camera->SetProject(nickel::cgmath::CreateOrtho(
// -halfGameWindowSize.w, halfGameWindowSize.w, halfGameWindowSize.h,
// -halfGameWindowSize.h, 1000, -1000,
-halfTextureSize.w, halfTextureSize.w, halfTextureSize.h,
-halfTextureSize.h, 1000, -1000,
adapter->RequestAdapterInfo().api == nickel::rhi::APIPreference::GL));
Expand All @@ -114,6 +114,15 @@ void EditorEnter(
target->Size()
});

uiCtx->renderCtx.camera.SetProject(nickel::cgmath::CreateOrtho(
-halfTextureSize.w, halfTextureSize.w, halfTextureSize.h,
-halfTextureSize.h, 1000, -1000,
adapter->RequestAdapterInfo().api == nickel::rhi::APIPreference::GL));
uiCtx->renderCtx.camera.SetViewport({
{0, 0},
target->Size()
});

dropFileEvent.sink().add<dropFileEventHandle>();

auto& editorCtx = EditorContext::Instance();
Expand Down Expand Up @@ -159,87 +168,6 @@ void EditorEnter(
window->SetResizable(true);
}

void DrawGuizmos() {
auto reg = nickel::ECS::Instance().World().cur_registry();
auto camera = reg->res_mut<nickel::Camera>();
auto mouse = reg->res<nickel::Mouse>();

// if (IsFocused()) {
// if (mouse->MiddleBtn().IsPressing()) {
// auto offset = mouse->Offset();
// auto scale = camera->Scale();
// camera->Move(nickel::cgmath::Vec2{-offset.x, offset.y} / scale);
// }

// if (auto wheel = mouse->WheelOffset(); wheel.y != 0) {
// auto scale = camera->Scale();
// auto newScale = scale + wheel.y * nickel::cgmath::Vec2{0.01,
// 0.01}; if (newScale.x <= 0 || newScale.y <= 0) {
// newScale.Set(0.01, 0.01);
// }
// camera->ScaleTo(newScale);
// }
// }

auto selectedEnt = EditorContext::Instance().entityListWindow.GetSelected();
if (!reg->alive(selectedEnt) || !reg->has<nickel::Transform>(selectedEnt)) {
return;
}

static auto guizmoOperation_ = ImGuizmo::OPERATION::TRANSLATE;
static auto guizmoMode_ = ImGuizmo::MODE::LOCAL;

auto& transform = reg->get_mut<nickel::Transform>(selectedEnt);

auto keyboard = reg->res<nickel::Keyboard>();
if (keyboard->Key(nickel::Key::Q).IsPressed()) {
guizmoOperation_ = ImGuizmo::TRANSLATE;
}
if (keyboard->Key(nickel::Key::W).IsPressed()) {
guizmoOperation_ = ImGuizmo::SCALE;
}
if (keyboard->Key(nickel::Key::E).IsPressed()) {
guizmoOperation_ = ImGuizmo::ROTATE;
}
if (ImGui::RadioButton("Translate",
guizmoOperation_ == ImGuizmo::TRANSLATE))
guizmoOperation_ = ImGuizmo::TRANSLATE;
ImGui::SameLine();
if (ImGui::RadioButton("Rotate", guizmoOperation_ == ImGuizmo::ROTATE))
guizmoOperation_ = ImGuizmo::ROTATE;
ImGui::SameLine();
if (ImGui::RadioButton("Scale", guizmoOperation_ == ImGuizmo::SCALE))
guizmoOperation_ = ImGuizmo::SCALE;

nickel::cgmath::Mat44 matrix;
auto windowSize = reg->res<nickel::Window>()->Size();
float matrixRot[3] = {0, 0, transform.rotation};
float matrixTrans[3] = {transform.translation.x, transform.translation.y,
0};
float matrixScale[3] = {transform.scale.x, transform.scale.y, 0};
ImGuizmo::RecomposeMatrixFromComponents(matrixTrans, matrixRot, matrixScale,
matrix.data);

if (guizmoOperation_ != ImGuizmo::SCALE) {
if (ImGui::RadioButton("Local", guizmoMode_ == ImGuizmo::LOCAL))
guizmoMode_ = ImGuizmo::LOCAL;
ImGui::SameLine();
if (ImGui::RadioButton("World", guizmoMode_ == ImGuizmo::WORLD))
guizmoMode_ = ImGuizmo::WORLD;
}
ImGuizmo::Enable(true);
ImGuiIO& io = ImGui::GetIO();
auto pos = ImGui::GetWindowPos() + ImGui::GetWindowContentRegionMin();
auto size =
ImGui::GetWindowContentRegionMax() - ImGui::GetWindowContentRegionMin();
ImGuizmo::SetRect(0, 0, 1024, 720);
auto view = nickel::cgmath::CreateScale({1, -1, 1}) * camera->View();
// ImGuizmo::DrawGrid(view.data, camera->Project().data, matrix.data,
// 100.f);
ImGuizmo::Manipulate(view.data, camera->Project().data, guizmoOperation_,
guizmoMode_, matrix.data, nullptr, nullptr);
}

void EditorImGuiUpdate() {
PROFILE_BEGIN();

Expand All @@ -252,8 +180,6 @@ void EditorImGuiUpdate() {
if (ctx.openDemoWindow) {
ImGui::ShowDemoWindow(&ctx.openDemoWindow);
}

// DrawGuizmos();
}

void EditorExit(gecs::registry reg) {
Expand Down Expand Up @@ -297,9 +223,8 @@ void RegistSystems(gecs::world& world) {
EditorScene::Editor)
.regist_update_system_to_state_after<
plugin::ImGuiGameWindowLayoutTransition, EditorImGuiUpdate>(
EditorScene::Editor);

world.cur_registry()->switch_state_immediatly(EditorScene::ProjectManager);
EditorScene::Editor)
.startup_with_state(EditorScene::ProjectManager);
}

void BootstrapSystem(gecs::world& world,
Expand Down
4 changes: 2 additions & 2 deletions editor/src/spawn_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void RegistSpawnMethods() {
instance.Regist<nickel::SoundPlayer>();
instance.Regist<nickel::SpriteMaterial>();
instance.Regist<nickel::AnimationPlayer>();
// instance.Regist<nickel::ui::Style>(GeneralSpawnMethod<nickel::ui::Style>);
// instance.Regist<nickel::ui::Button>(GeneralSpawnMethod<nickel::ui::Button>);
instance.Regist<nickel::ui::Style>();
instance.Regist<nickel::ui::Button>();
// instance.Regist<nickel::ui::Label>(GeneralSpawnMethod<nickel::ui::Label>);
}
26 changes: 13 additions & 13 deletions nickel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,18 @@ target_compile_features(Nickel.Animation PUBLIC cxx_std_17)
target_link_libraries(Nickel.Animation PUBLIC Nickel.Common)

# Nickel.UI
# aux_source_directory(src/ui nickel_ui_src)
# file(GLOB_RECURSE nickel_ui_headers ./include/ui/*.hpp)
#
# add_library(Nickel.UI STATIC)
# target_sources(Nickel.UI PRIVATE ${nickel_ui_src})
# target_sources(Nickel.UI PUBLIC ${nickel_ui_headers})
# target_include_directories(Nickel.UI PUBLIC ./include)
# set_target_properties(Nickel.UI
# PROPERTIES
# PUBLIC_HEADER "${nickel_ui_headers}")
# target_compile_features(Nickel.UI PUBLIC cxx_std_17)
# target_link_libraries(Nickel.UI PUBLIC Nickel.Common Nickel.Graphics)
aux_source_directory(src/ui nickel_ui_src)
file(GLOB_RECURSE nickel_ui_headers ./include/ui/*.hpp)

add_library(Nickel.UI STATIC)
target_sources(Nickel.UI PRIVATE ${nickel_ui_src})
target_sources(Nickel.UI PUBLIC ${nickel_ui_headers})
target_include_directories(Nickel.UI PUBLIC ./include)
set_target_properties(Nickel.UI
PROPERTIES
PUBLIC_HEADER "${nickel_ui_headers}")
target_compile_features(Nickel.UI PUBLIC cxx_std_17)
target_link_libraries(Nickel.UI PUBLIC Nickel.Common Nickel.Graphics)

# Nickel.Refl
aux_source_directory(src/refl nickel_refl_src)
Expand All @@ -152,7 +152,7 @@ target_link_libraries(Nickel.Refl PUBLIC
Nickel.Animation
Nickel.Audio
Nickel.Physics
#Nickel.UI
Nickel.UI
)

# Nickel.Misc
Expand Down
14 changes: 4 additions & 10 deletions nickel/include/graphics/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ struct Render2DContext {
rhi::SamplerAddressMode v, rhi::Filter min,
rhi::Filter mag);

void RecreatePipeline(rhi::APIPreference api, const cgmath::Vec2& size,
RenderContext& ctx);
void RecreatePipeline(rhi::APIPreference api, RenderContext& ctx);

uint32_t GenVertexSlot();
void ReuseVertexSlot(uint32_t);
Expand All @@ -64,7 +63,6 @@ struct Render2DContext {
void initPipelineShader(rhi::APIPreference);
rhi::BindGroupLayout createBindGroupLayout(RenderContext& ctx);
rhi::RenderPipeline createPipeline(rhi::APIPreference,
const nickel::cgmath::Rect& viewport,
RenderContext&);
void initSamplers();
rhi::BindGroup createDefaultBindGroup();
Expand All @@ -79,22 +77,18 @@ struct Render3DContext {
rhi::ShaderModule vertexShader;
rhi::ShaderModule fragmentShader;

Render3DContext(rhi::APIPreference, rhi::Device,
const cgmath::Rect& viewport, RenderContext&);
Render3DContext(rhi::APIPreference, rhi::Device, RenderContext&);
~Render3DContext();

void RecreatePipeline(rhi::APIPreference api, const cgmath::Vec2& size,
RenderContext& ctx);
void RecreatePipeline(rhi::APIPreference api, RenderContext& ctx);

private:
rhi::Device device_;

rhi::PipelineLayout createPipelineLayout();
void initPipelineShader(rhi::APIPreference);
rhi::BindGroupLayout createBindGroupLayout(RenderContext& ctx);
rhi::RenderPipeline createPipeline(rhi::APIPreference,
const nickel::cgmath::Rect& viewport,
RenderContext&);
rhi::RenderPipeline createPipeline(rhi::APIPreference, RenderContext&);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion nickel/include/nickel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "system/graphics.hpp"
#include "system/physics.hpp"
#include "system/video.hpp"
// #include "ui/ui.hpp"
#include "ui/ui.hpp"
#include "video/device.hpp"
#include "video/event.hpp"
#include "video/input.hpp"
Expand Down
13 changes: 4 additions & 9 deletions nickel/include/ui/button.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@

namespace nickel::ui {

class Button final {
public:
struct Button final {
cgmath::Color color = {1, 1, 1, 1};
cgmath::Color hoverColor = {1, 1, 1, 1};
cgmath::Color pressColor = {1, 1, 1, 1};
};

class TextureButton final {
public:
TextureHandle texture;
TextureHandle hoverTexture;
TextureHandle pressTexture;
TextureClip texture;
TextureClip hoverTexture;
TextureClip pressTexture;
};

} // namespace nickel::ui
Loading

0 comments on commit 3a0176d

Please sign in to comment.