Skip to content

Commit

Permalink
gui editor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Jul 19, 2024
1 parent ba3e4a2 commit de3c66b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 15 deletions.
3 changes: 2 additions & 1 deletion data/pipelines/gui_editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ local rb_desc = createRenderbufferDesc { format = "rgba8", debug_name = "gui_edi
function main()
local color_buffer = createRenderbuffer(rb_desc)
setRenderTargets(color_buffer)
clear(CLEAR_ALL, 0, 0, 0, 1, 0)
local clear_color = GUI_EDITOR_CLEAR_COLOR or {0, 0, 0}
clear(CLEAR_ALL, clear_color[1], clear_color[2], clear_color[3], 1, 0)
render2D()
setOutput(color_buffer)
end
Expand Down
6 changes: 6 additions & 0 deletions src/editor/studio_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3582,7 +3582,12 @@ struct StudioAppImpl final : StudioApp

Span<const os::Event> getEvents() const override { return m_events; }

void setCaptureInput(bool capture) override {
m_capture_input = capture;
}

void checkShortcuts() {
if (m_capture_input) return;
u8 pressed_modifiers = 0;
if (os::isKeyDown(os::Keycode::SHIFT)) pressed_modifiers |= Action::Modifiers::SHIFT;
if (os::isKeyDown(os::Keycode::CTRL)) pressed_modifiers |= Action::Modifiers::CTRL;
Expand Down Expand Up @@ -3672,6 +3677,7 @@ struct StudioAppImpl final : StudioApp
bool m_confirm_new = false;
bool m_confirm_destroy_partition = false;
bool m_is_caption_hovered = false;
bool m_capture_input = false;

World::PartitionHandle m_partition_to_destroy;
Path m_world_to_load;
Expand Down
3 changes: 3 additions & 0 deletions src/editor/studio_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ struct LUMIX_EDITOR_API StudioApp {
virtual void setMouseClipRect(os::WindowHandle win, const os::Rect &screen_rect) = 0;
virtual void unclipMouseCursor() = 0;
virtual bool isMouseCursorClipped() const = 0;
// if true, shortcuts are not processed
// use case - when there's active game in game view, we don't want delete keypress to delete entities
virtual void setCaptureInput(bool capture) = 0;

virtual Span<const os::Event> getEvents() const = 0;
virtual ImFont* getDefaultFont() = 0;
Expand Down
22 changes: 10 additions & 12 deletions src/gui/editor/gui_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ struct GUIEditor final : StudioApp::GUIPlugin
}
if (m_resize_side != ResizeSide::NONE && ImGui::IsMouseClicked(0)) return MouseMode::RESIZE;
}
if (m_resize_side == ResizeSide::NONE) ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
if (m_resize_side == ResizeSide::NONE && module.isOver(mouse_canvas_pos, e)) ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
if (ImGui::IsMouseClicked(0)) return MouseMode::MOVE;
return MouseMode::NONE;
}
Expand Down Expand Up @@ -576,11 +576,8 @@ struct GUIEditor final : StudioApp::GUIPlugin
}
}

switch (m_mouse_mode) {
case MouseMode::RESIZE: ImGui::TextUnformatted("Resize"); break;
case MouseMode::MOVE: ImGui::TextUnformatted("Move"); break;
case MouseMode::NONE: ImGui::TextUnformatted("None"); break;
}

ImGui::ColorEdit3("Background", &m_clear_color.x);

if (!m_canvas_entity.isValid()) {
if (canvases.empty()) {
Expand Down Expand Up @@ -608,12 +605,12 @@ struct GUIEditor final : StudioApp::GUIPlugin
}

module->renderCanvas(*m_pipeline, { size.x, size.y }, false, *m_canvas_entity);

if (editor.getSelectedEntities().size() == 1) {
EntityRef e = editor.getSelectedEntities()[0];
if (isInCanvas(e, *m_canvas_entity)) {
MouseMode new_mode = drawGizmo(m_pipeline->getDraw2D(), *module, { size.x, size.y }, mouse_canvas_pos, e);
if (m_mouse_mode == MouseMode::NONE) m_mouse_mode = new_mode;
if (ImGui::IsWindowHovered() && m_mouse_mode == MouseMode::NONE) m_mouse_mode = new_mode;
}
}

Expand Down Expand Up @@ -663,7 +660,8 @@ struct GUIEditor final : StudioApp::GUIPlugin
vp.w = (int)size.x;
vp.h = (int)size.y;
m_pipeline->setViewport(vp);

m_pipeline->setLuaGlobal("GUI_EDITOR_CLEAR_COLOR", m_clear_color);

if (m_pipeline->render(true)) {
m_texture_handle = m_pipeline->getOutput();

Expand All @@ -684,13 +682,12 @@ struct GUIEditor final : StudioApp::GUIPlugin
}
}


if (ImGui::IsMouseReleased(0) && ImGui::IsItemHovered() && ImGui::GetMouseDragDelta().x == 0 && ImGui::GetMouseDragDelta().x == 0) {
if (ImGui::IsItemHovered() && ImGui::IsMouseReleased(0) && ImGui::IsItemHovered() && ImGui::GetMouseDragDelta().x == 0 && ImGui::GetMouseDragDelta().x == 0) {
const Array<EntityRef>& selected = editor.getSelectedEntities();
bool parent_selected = false;
if (!selected.empty() && isInCanvas(selected[0], *m_canvas_entity)) {
const EntityPtr parent = editor.getWorld()->getParent(selected[0]);
if (parent.isValid()) {
if (module->isOver(mouse_canvas_pos, selected[0]) && parent.isValid()) {
const GUIModule::Rect rect = module->getRect(*parent);
if (mouse_canvas_pos.x >= rect.x
&& mouse_canvas_pos.y >= rect.y
Expand Down Expand Up @@ -1116,6 +1113,7 @@ struct GUIEditor final : StudioApp::GUIPlugin
Vec2 m_top_left_start_transform;
Vec2 m_canvas_size;
EntityPtr m_canvas_entity = INVALID_ENTITY;
Vec3 m_clear_color = Vec3(0);

Action m_hcenter_action;
Action m_vcenter_action;
Expand Down
4 changes: 2 additions & 2 deletions src/gui/gui_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ struct GUIModuleImpl final : GUIModule {
Font* font = rect.text->getFont();
Vec2 text_size = measureTextA(*font, text, text_end);
draw.addLine({ pos.x + text_size.x, pos.y }
, { pos.x + text_size.x, pos.y + text_size.y }
, { pos.x + text_size.x, pos.y - text_size.y }
, *(Color*)&rect.text->color
, 1);
}
Expand Down Expand Up @@ -214,7 +214,6 @@ struct GUIModuleImpl final : GUIModule {
m_cursor_set = true;
}
img_color = (Color*)&button.hovered_color;
txt_color = (Color*)&button.hovered_color;
}
}

Expand Down Expand Up @@ -790,6 +789,7 @@ struct GUIModuleImpl final : GUIModule {
{
const GUIRect* rect = getInput(m_focused_entity);
if (!rect) return;
if (event.data.text.utf8 == '\b') return;
char tmp[5] = {};
memcpy(tmp, &event.data.text.utf8, sizeof(event.data.text.utf8));
rect->text->text.insert(rect->input_field->cursor, tmp);
Expand Down
1 change: 1 addition & 0 deletions src/renderer/editor/game_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void GameView::enableIngameCursor(bool enable)
void GameView::captureMouse(bool capture) {
if (m_is_mouse_captured == capture) return;

m_app.setCaptureInput(capture);
m_is_mouse_captured = capture;
os::showCursor(!capture || m_is_ingame_cursor);
if (capture) m_app.clipMouseCursor();
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,14 @@ struct PipelineImpl final : Pipeline
}
}

void setLuaGlobal(const char* var_name, const Vec3& value) override {
LuaWrapper::DebugGuard guard(m_lua_state);
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
LuaWrapper::setField(m_lua_state, -1, var_name, value);
lua_pop(m_lua_state, 1);

}

void define(const char* define, bool enable) override {
LuaWrapper::DebugGuard guard(m_lua_state);
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
Expand Down
1 change: 1 addition & 0 deletions src/renderer/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct LUMIX_RENDERER_API Pipeline {
virtual void callLuaFunction(const char* func) = 0;
virtual void setViewport(const Viewport& viewport) = 0;
virtual Viewport getViewport() = 0;
virtual void setLuaGlobal(const char* var_name, const Vec3& value) = 0;
virtual void define(const char* define, bool enable) = 0;
virtual void setIndirectLightMultiplier(float value) = 0;
virtual IVec2 getDisplaySize() const = 0;
Expand Down

0 comments on commit de3c66b

Please sign in to comment.