Reorganize code in widgets
This commit is contained in:
parent
837d8dc507
commit
e00dec67ba
31 changed files with 652 additions and 481 deletions
116
WidgetsNew/private/managers/DebugManager.cpp
Normal file
116
WidgetsNew/private/managers/DebugManager.cpp
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
#include "DebugManager.hpp"
|
||||
#include "RootWidget.hpp"
|
||||
|
||||
#include "imgui.h"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
bool DebugManager::update(RootWidget* rootWidget, EventHandler& events) {
|
||||
if (mDebug) {
|
||||
auto area = rootWidget->mRoot->getAreaT();
|
||||
area.size -= { 400, 0 };
|
||||
rootWidget->mRoot->setArea(area);
|
||||
|
||||
events.setEnableKeyEvents(true);
|
||||
if (events.isPressed(InputID::K)) mDebugStopProcessing = !mDebugStopProcessing;
|
||||
if (mDebugStopProcessing) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DebugManager::drawDebug(RootWidget* rootWidget, Canvas& canvas) {
|
||||
ImGui::Checkbox("Draw debug", &mDebug);
|
||||
|
||||
auto& upd = rootWidget->mUpdateManager;
|
||||
|
||||
if (mDebug) {
|
||||
recursiveDraw(canvas, rootWidget->mRoot, { 0, 0 }, 0);
|
||||
|
||||
ImGui::Text("Triggered Widgets: %i", (int) upd.mTriggeredWidgets.size());
|
||||
ImGui::Text("Widgets To Process: %i", upd.mDebugWidgetsToProcess);
|
||||
|
||||
ImGui::Text("To Toggle processing press k");
|
||||
ImGui::Checkbox("Stop processing", &mDebugStopProcessing);
|
||||
ImGui::Checkbox("Force new frames", &mDebugRedrawAlways);
|
||||
|
||||
if (ImGui::CollapsingHeader("Triggered")) {
|
||||
if (ImGui::BeginListBox("##triggered", { -FLT_MIN, 300 })) {
|
||||
for (auto widget : upd.mTriggeredWidgets) {
|
||||
widgetMenu(widget.first);
|
||||
}
|
||||
ImGui::EndListBox();
|
||||
}
|
||||
}
|
||||
|
||||
if (upd.mInFocusWidget) {
|
||||
if (ImGui::CollapsingHeader("Under cursor")) {
|
||||
if (ImGui::BeginListBox("##under_cursor", { -FLT_MIN, 300 })) {
|
||||
for (auto widget = upd.mInFocusWidget; widget && widget->mParent; widget = widget->mParent) {
|
||||
widgetMenu(widget);
|
||||
}
|
||||
ImGui::EndListBox();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DebugManager::recursiveDraw(Canvas& canvas, Widget* active, const Vec2F& pos, int depthOrder) {
|
||||
auto area = RectF{ pos, active->getAreaT().size };
|
||||
|
||||
if (active->isUpdate()) {
|
||||
RGBA color = { 1, 0, 0, 1};
|
||||
canvas.frame(area, color);
|
||||
canvas.text((active->mDebug.id + ":" + std::to_string(depthOrder)).c_str(), area, 22, Canvas::Align::LC, 2, color);
|
||||
}
|
||||
|
||||
if (active->mFlags.get(Widget::IN_FOCUS)) {
|
||||
if (active->isUpdate()) {
|
||||
canvas.circle(pos + active->mDebug.pLocal, 5, active->mDebug.col);
|
||||
canvas.circle(active->mDebug.pGlobal, 15, active->mDebug.col);
|
||||
}
|
||||
|
||||
RGBA color = { 1, 0, 0, 0.3f};
|
||||
canvas.debugCross(area, color);
|
||||
}
|
||||
|
||||
int orderIdx = 0;
|
||||
for (auto child = active->mDepthOrder.lastNode(); child; child = child->prev) {
|
||||
recursiveDraw(canvas, child->data, pos + child->data->getAreaT().pos, orderIdx++);
|
||||
}
|
||||
}
|
||||
|
||||
void DebugManager::widgetMenu(Widget* widget) {
|
||||
|
||||
ImGui::PushID(widget);
|
||||
if (ImGui::CollapsingHeader(widget->mDebug.id.c_str())) {
|
||||
|
||||
ImGui::Text("trigger reason: %s", widget->mDebug.triggerReason.c_str());
|
||||
|
||||
auto area = widget->getAreaT();
|
||||
if (ImGui::InputFloat4("rect", &area.x)) {
|
||||
widget->setArea(area);
|
||||
}
|
||||
|
||||
// ImGui::InputFloat2("min size", &widget->mMinSize.x);
|
||||
// ImGui::InputFloat2("max size", &widget->mMaxSize.x);
|
||||
|
||||
// int sizePolicyX = int(widget->mSizePolicy.x);
|
||||
// int sizePolicyY = int(widget->mSizePolicy.y);
|
||||
// int layout = int(widget->mLayoutPolicy);
|
||||
|
||||
// if (ImGui::Combo("Size Policy X", &sizePolicyX, "Fixed\0Expanding\0Minimal\0")) {
|
||||
// widget->setSizePolicy(SizePolicy(sizePolicyX), SizePolicy(sizePolicyY));
|
||||
// }
|
||||
|
||||
// if (ImGui::Combo("Size Policy Y", &sizePolicyY, "Fixed\0Expanding\0Minimal\0")) {
|
||||
// widget->setSizePolicy(SizePolicy(sizePolicyX), SizePolicy(sizePolicyY));
|
||||
// }
|
||||
|
||||
// if (ImGui::Combo("Layout", &layout, "Passive\0Vertical\0Horizontal\0")) {
|
||||
// widget->setLayoutPolicy(LayoutPolicy(layout));
|
||||
// }
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
17
WidgetsNew/private/managers/LayoutManager.cpp
Normal file
17
WidgetsNew/private/managers/LayoutManager.cpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include "LayoutManager.hpp"
|
||||
|
||||
#include "Widget.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
void LayoutManager::adjust(Widget* root) {
|
||||
Widget::dfs(
|
||||
root,
|
||||
[](Widget* widget) {
|
||||
widget->getLayout()->pickRect();
|
||||
},
|
||||
[](Widget* widget) {
|
||||
widget->getLayout()->adjustChildrenRect();
|
||||
}
|
||||
);
|
||||
}
|
||||
191
WidgetsNew/private/managers/UpdateManager.cpp
Normal file
191
WidgetsNew/private/managers/UpdateManager.cpp
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
#include "UpdateManager.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
void UpdateManager::processWidgets(Widget* root, EventHandler& events) {
|
||||
events.setEnableKeyEvents(true);
|
||||
events.setCursorOrigin({ 0, 0 });
|
||||
|
||||
processFocusItems(events);
|
||||
|
||||
events.setEnableKeyEvents(false);
|
||||
events.setCursorOrigin({ 0, 0 });
|
||||
|
||||
processActiveTree(root, events, { 0, 0 });
|
||||
}
|
||||
|
||||
void UpdateManager::clean() {
|
||||
erase_if(mTriggeredWidgets, [](auto iter) {
|
||||
auto widget = iter.first;
|
||||
auto flag = iter.second;
|
||||
|
||||
if (!flag) return false;
|
||||
|
||||
// if (mWidgetsToProcess.find(widget) == mWidgetsToProcess.end()) return false;
|
||||
widget->updateAnimations();
|
||||
auto end = !widget->needsNextFrame();
|
||||
if (end) {
|
||||
widget->endAnimations();
|
||||
widget->mDebug.triggerReason = "del";
|
||||
}
|
||||
return end;
|
||||
});
|
||||
}
|
||||
|
||||
void UpdateManager::scheduleUpdate(Widget* widget, const char* reason) {
|
||||
widget->mDebug.triggerReason = reason;
|
||||
mTriggeredWidgets.insert({ widget, false });
|
||||
}
|
||||
|
||||
void UpdateManager::updateTreeToProcess(Widget* root) {
|
||||
Widget::dfs(root, [](Widget* widget) {
|
||||
widget->mFlags.set(Widget::NEEDS_UPDATE, false);
|
||||
});
|
||||
|
||||
for (auto& [widget, flag] : mTriggeredWidgets) {
|
||||
flag = true;
|
||||
|
||||
for (auto iter = widget; iter && iter->mParent; iter = iter->mParent) {
|
||||
iter->mFlags.set(Widget::NEEDS_UPDATE, true);
|
||||
}
|
||||
}
|
||||
|
||||
mDebugWidgetsToProcess = 0;
|
||||
Widget::dfs(root, [this](Widget*) {
|
||||
mDebugWidgetsToProcess++;
|
||||
});
|
||||
}
|
||||
|
||||
void UpdateManager::getWidgetPath(Widget* widget, std::vector<Widget*>& out) {
|
||||
if (!widget) return;
|
||||
|
||||
for (auto iter = widget; iter && iter->mParent; iter = iter->mParent) {
|
||||
out.push_back(iter);
|
||||
}
|
||||
|
||||
for (auto i = 0; i < out.size() / 2; i++) {
|
||||
swap(out[i], out[out.size() - i - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateManager::handleFocusChanges(Widget* root, EventHandler& events) {
|
||||
auto prevFocus = mInFocusWidget;
|
||||
|
||||
events.setCursorOrigin({ 0, 0 });
|
||||
|
||||
findFocusWidget(root, &mInFocusWidget, events.getPointer());
|
||||
|
||||
// if (mInFocusWidget == prevFocus) return;
|
||||
if (mInFocusWidget) scheduleUpdate(mInFocusWidget, "focus entered");
|
||||
|
||||
std::vector<Widget*> path2;
|
||||
getWidgetPath(mInFocusWidget, path2);
|
||||
size_t propLen2 = path2.size();
|
||||
for (auto i = 0; i < path2.size(); i++) {
|
||||
if (!path2[i]->propagateEventsToChildren()) {
|
||||
propLen2 = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Widget*> path1;
|
||||
getWidgetPath(prevFocus, path1);
|
||||
size_t propLen1 = path1.size();
|
||||
for (auto i = 0; i < path1.size(); i++) {
|
||||
if (!path1[i]->propagateEventsToChildren()) {
|
||||
propLen1 = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
size_t mostCommonIdx = 0;
|
||||
if (!(path1.empty() || path2.empty())) {
|
||||
while (path1[mostCommonIdx] == path2[mostCommonIdx] && mostCommonIdx < min(path1.size(), path2.size())) {
|
||||
mostCommonIdx++;
|
||||
}
|
||||
}
|
||||
mostCommonIdx--;
|
||||
|
||||
for (auto i = 0; i < path1.size(); i++) {
|
||||
path1[i]->mFlags.set(Widget::IN_FOCUS, false);
|
||||
if (i > mostCommonIdx && i < propLen1) path1[i]->mouseLeave();
|
||||
}
|
||||
|
||||
for (auto i = 0; i < path2.size(); i++) {
|
||||
path2[i]->mFlags.set(Widget::IN_FOCUS, true);
|
||||
if (i > mostCommonIdx && i < propLen2) path2[i]->mouseEnter();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateManager::findFocusWidget(Widget* iter, Widget** focus, const Vec2F& pointer) {
|
||||
if (!iter->mArea.getTargetRect().isInside(pointer) || !iter->mFlags.get(Widget::ENABLED)) return;
|
||||
|
||||
*focus = iter;
|
||||
|
||||
for (auto child = iter->mDepthOrder.lastNode(); child; child = child->prev) {
|
||||
findFocusWidget(child->data, focus, pointer - iter->mArea.getTargetRect().pos);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateManager::processActiveTree(Widget* iter, EventHandler& events, Vec2F parent) {
|
||||
if (!iter || !iter->isUpdate()) return;
|
||||
|
||||
auto current = parent + iter->getAreaT().pos;
|
||||
|
||||
if (!iter->mFlags.get(Widget::IN_FOCUS)) {
|
||||
iter->mDebug.pGlobal = current;
|
||||
|
||||
events.setCursorOrigin(current);
|
||||
iter->process(events);
|
||||
}
|
||||
|
||||
for (auto child : iter->mDepthOrder) {
|
||||
processActiveTree(child.data(), events, current);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateManager::processFocusItems(EventHandler& events) {
|
||||
if (!mInFocusWidget) return;
|
||||
|
||||
// FIXME : cringe
|
||||
|
||||
std::vector<Widget*> path;
|
||||
getWidgetPath(mInFocusWidget, path);
|
||||
|
||||
size_t len = path.size();
|
||||
for (auto i = 0; i < len; i++) {
|
||||
if (!path[i]->propagateEventsToChildren()) {
|
||||
len = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Vec2F> widgetGlobalPos(path.size());
|
||||
|
||||
widgetGlobalPos[0] = 0;
|
||||
for (auto widget = 0; widget < path.size() - 1; widget++) {
|
||||
widgetGlobalPos[widget + 1] = widgetGlobalPos[widget] + path[widget + 1]->getAreaT().pos;
|
||||
// path[widget]->mGlobalPoint = widgetGlobalPos[widget];
|
||||
}
|
||||
|
||||
bool eventsProcessed = false;
|
||||
|
||||
for (int iter = (int) len - 1; iter >= 0; iter--) {
|
||||
auto widget = path[iter];
|
||||
|
||||
events.setCursorOrigin(widgetGlobalPos[iter]);
|
||||
|
||||
widget->mDebug.pGlobal = widgetGlobalPos[iter];
|
||||
|
||||
if (!eventsProcessed && widget->processesEvents()) {
|
||||
events.setEnableKeyEvents(true);
|
||||
widget->process(events);
|
||||
|
||||
events.setEnableKeyEvents(false);
|
||||
eventsProcessed = true;
|
||||
} else {
|
||||
widget->process(events);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue