mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-06-25 15:20:31 -04:00
Formatted code (#1007)
* Updated style * Updated files * fixed new line * Updated spacing * File fix WIP * Updated to clang 13 * updated comment style * Removed old comment code
This commit is contained in:
parent
7aca7ce74d
commit
033c4e9a5b
599 changed files with 70746 additions and 66896 deletions
|
@ -33,39 +33,39 @@
|
|||
namespace ui {
|
||||
|
||||
Widget* FocusManager::focus_widget() const {
|
||||
return focus_widget_;
|
||||
return focus_widget_;
|
||||
}
|
||||
|
||||
void FocusManager::set_focus_widget(Widget* const new_focus_widget) {
|
||||
// Widget already has focus.
|
||||
if( new_focus_widget == focus_widget() ) {
|
||||
return;
|
||||
}
|
||||
// Widget already has focus.
|
||||
if (new_focus_widget == focus_widget()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if( new_focus_widget ) {
|
||||
// if( !new_focus_widget->visible() ) {
|
||||
// if( new_focus_widget->hidden() ) {
|
||||
// // New widget is not visible. Do nothing.
|
||||
// // TODO: Should this be a debug assertion?
|
||||
// return;
|
||||
// }
|
||||
if( !new_focus_widget->focusable() ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (new_focus_widget) {
|
||||
// if( !new_focus_widget->visible() ) {
|
||||
// if( new_focus_widget->hidden() ) {
|
||||
// // New widget is not visible. Do nothing.
|
||||
// // TODO: Should this be a debug assertion?
|
||||
// return;
|
||||
// }
|
||||
if (!new_focus_widget->focusable()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Blur old widget.
|
||||
if( focus_widget() ) {
|
||||
focus_widget()->on_blur();
|
||||
focus_widget()->set_dirty();
|
||||
}
|
||||
// Blur old widget.
|
||||
if (focus_widget()) {
|
||||
focus_widget()->on_blur();
|
||||
focus_widget()->set_dirty();
|
||||
}
|
||||
|
||||
focus_widget_ = new_focus_widget;
|
||||
focus_widget_ = new_focus_widget;
|
||||
|
||||
if( focus_widget() ) {
|
||||
focus_widget()->on_focus();
|
||||
focus_widget()->set_dirty();
|
||||
}
|
||||
if (focus_widget()) {
|
||||
focus_widget()->on_focus();
|
||||
focus_widget()->set_dirty();
|
||||
}
|
||||
}
|
||||
|
||||
using test_result_t = std::pair<Widget* const, const uint32_t>;
|
||||
|
@ -73,149 +73,143 @@ using test_fn = std::function<test_result_t(Widget* const)>;
|
|||
using test_collection_t = std::vector<test_result_t>;
|
||||
|
||||
/* Walk all visible widgets in hierarchy, collecting those that pass test */
|
||||
template<typename TestFn>
|
||||
template <typename TestFn>
|
||||
static void widget_collect_visible(Widget* const w, TestFn test, test_collection_t& collection) {
|
||||
for(auto child : w->children()) {
|
||||
if( !child->hidden() ) {
|
||||
const auto result = test(child);
|
||||
if( result.first ) {
|
||||
collection.push_back(result);
|
||||
}
|
||||
widget_collect_visible(child, test, collection);
|
||||
}
|
||||
}
|
||||
for (auto child : w->children()) {
|
||||
if (!child->hidden()) {
|
||||
const auto result = test(child);
|
||||
if (result.first) {
|
||||
collection.push_back(result);
|
||||
}
|
||||
widget_collect_visible(child, test, collection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t rect_distances(
|
||||
const KeyEvent direction,
|
||||
const Rect& rect_start,
|
||||
const Rect& rect_end
|
||||
) {
|
||||
Coord on_axis_max, on_axis_min;
|
||||
const KeyEvent direction,
|
||||
const Rect& rect_start,
|
||||
const Rect& rect_end) {
|
||||
Coord on_axis_max, on_axis_min;
|
||||
|
||||
switch(direction) {
|
||||
case KeyEvent::Right:
|
||||
on_axis_max = rect_end.left();
|
||||
on_axis_min = rect_start.right();
|
||||
break;
|
||||
switch (direction) {
|
||||
case KeyEvent::Right:
|
||||
on_axis_max = rect_end.left();
|
||||
on_axis_min = rect_start.right();
|
||||
break;
|
||||
|
||||
case KeyEvent::Left:
|
||||
on_axis_max = rect_start.left();
|
||||
on_axis_min = rect_end.right();
|
||||
break;
|
||||
case KeyEvent::Left:
|
||||
on_axis_max = rect_start.left();
|
||||
on_axis_min = rect_end.right();
|
||||
break;
|
||||
|
||||
case KeyEvent::Down:
|
||||
on_axis_max = rect_end.top();
|
||||
on_axis_min = rect_start.bottom();
|
||||
break;
|
||||
case KeyEvent::Down:
|
||||
on_axis_max = rect_end.top();
|
||||
on_axis_min = rect_start.bottom();
|
||||
break;
|
||||
|
||||
case KeyEvent::Up:
|
||||
on_axis_max = rect_start.top();
|
||||
on_axis_min = rect_end.bottom();
|
||||
break;
|
||||
case KeyEvent::Up:
|
||||
on_axis_max = rect_start.top();
|
||||
on_axis_min = rect_end.bottom();
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
Coord on_axis_distance = on_axis_max - on_axis_min;
|
||||
if( on_axis_distance < 0 ) {
|
||||
return -1;
|
||||
}
|
||||
Coord on_axis_distance = on_axis_max - on_axis_min;
|
||||
if (on_axis_distance < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Coord perpendicular_axis_start, perpendicular_axis_end;
|
||||
Coord perpendicular_axis_start, perpendicular_axis_end;
|
||||
|
||||
switch(direction) {
|
||||
case KeyEvent::Right:
|
||||
case KeyEvent::Left:
|
||||
perpendicular_axis_start = rect_start.center().y();
|
||||
perpendicular_axis_end = rect_end.center().y();
|
||||
break;
|
||||
switch (direction) {
|
||||
case KeyEvent::Right:
|
||||
case KeyEvent::Left:
|
||||
perpendicular_axis_start = rect_start.center().y();
|
||||
perpendicular_axis_end = rect_end.center().y();
|
||||
break;
|
||||
|
||||
case KeyEvent::Up:
|
||||
case KeyEvent::Down:
|
||||
perpendicular_axis_start = rect_start.center().x();
|
||||
perpendicular_axis_end = rect_end.center().x();
|
||||
break;
|
||||
case KeyEvent::Up:
|
||||
case KeyEvent::Down:
|
||||
perpendicular_axis_start = rect_start.center().x();
|
||||
perpendicular_axis_end = rect_end.center().x();
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (direction) {
|
||||
case KeyEvent::Right:
|
||||
case KeyEvent::Left:
|
||||
return ((std::abs(perpendicular_axis_end - perpendicular_axis_start) + 1) ^ 3) * sqrt((on_axis_distance + 1));
|
||||
break;
|
||||
|
||||
switch(direction) {
|
||||
case KeyEvent::Right:
|
||||
case KeyEvent::Left:
|
||||
return ((std::abs(perpendicular_axis_end - perpendicular_axis_start) + 1) ^ 3) * sqrt((on_axis_distance + 1));
|
||||
break;
|
||||
case KeyEvent::Up:
|
||||
case KeyEvent::Down:
|
||||
return (sqrt(std::abs(perpendicular_axis_end - perpendicular_axis_start) + 1)) * ((on_axis_distance + 1) ^ 3);
|
||||
break;
|
||||
|
||||
case KeyEvent::Up:
|
||||
case KeyEvent::Down:
|
||||
return (sqrt(std::abs(perpendicular_axis_end - perpendicular_axis_start) + 1)) * ((on_axis_distance + 1) ^ 3);
|
||||
break;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void FocusManager::update(
|
||||
Widget* const top_widget,
|
||||
const KeyEvent event
|
||||
) {
|
||||
if( focus_widget() ) {
|
||||
const auto focus_screen_rect = focus_widget()->screen_rect();
|
||||
Widget* const top_widget,
|
||||
const KeyEvent event) {
|
||||
if (focus_widget()) {
|
||||
const auto focus_screen_rect = focus_widget()->screen_rect();
|
||||
|
||||
const auto test_fn = [&focus_screen_rect, event](ui::Widget* const w) -> test_result_t {
|
||||
// if( w->visible() && w->focusable() ) {
|
||||
if( w->focusable() ) {
|
||||
const auto distance = rect_distances(event, focus_screen_rect, w->screen_rect());
|
||||
if( distance >= 0 ) {
|
||||
return { w, distance };
|
||||
}
|
||||
}
|
||||
const auto test_fn = [&focus_screen_rect, event](ui::Widget* const w) -> test_result_t {
|
||||
// if( w->visible() && w->focusable() ) {
|
||||
if (w->focusable()) {
|
||||
const auto distance = rect_distances(event, focus_screen_rect, w->screen_rect());
|
||||
if (distance >= 0) {
|
||||
return {w, distance};
|
||||
}
|
||||
}
|
||||
|
||||
return { nullptr, 0 };
|
||||
};
|
||||
|
||||
const auto find_back_fn = [](ui::Widget* const w) -> test_result_t {
|
||||
if( w->focusable() && (w->id == -1) )
|
||||
return { w, 0 };
|
||||
else
|
||||
return { nullptr, 0 };
|
||||
};
|
||||
return {nullptr, 0};
|
||||
};
|
||||
|
||||
test_collection_t collection;
|
||||
widget_collect_visible(top_widget, test_fn, collection);
|
||||
const auto find_back_fn = [](ui::Widget* const w) -> test_result_t {
|
||||
if (w->focusable() && (w->id == -1))
|
||||
return {w, 0};
|
||||
else
|
||||
return {nullptr, 0};
|
||||
};
|
||||
|
||||
const auto compare_fn = [](const test_result_t& a, const test_result_t& b) {
|
||||
return a.second < b.second;
|
||||
};
|
||||
test_collection_t collection;
|
||||
widget_collect_visible(top_widget, test_fn, collection);
|
||||
|
||||
const auto nearest = std::min_element(collection.cbegin(), collection.cend(), compare_fn);
|
||||
// Up and left to indicate back
|
||||
if (event == KeyEvent::Back) {
|
||||
collection.clear();
|
||||
widget_collect_visible(top_widget, find_back_fn, collection);
|
||||
if (!collection.empty())
|
||||
set_focus_widget(collection[0].first);
|
||||
}
|
||||
else if( nearest != collection.cend() ) {
|
||||
//focus->blur();
|
||||
const auto new_focus = (*nearest).first;
|
||||
set_focus_widget(new_focus);
|
||||
} else {
|
||||
if ((focus_widget()->id >= 0) && (event == KeyEvent::Left)) {
|
||||
// Stuck left, move to back button
|
||||
collection.clear();
|
||||
widget_collect_visible(top_widget, find_back_fn, collection);
|
||||
if (!collection.empty())
|
||||
set_focus_widget(collection[0].first);
|
||||
}
|
||||
}
|
||||
}
|
||||
const auto compare_fn = [](const test_result_t& a, const test_result_t& b) {
|
||||
return a.second < b.second;
|
||||
};
|
||||
|
||||
const auto nearest = std::min_element(collection.cbegin(), collection.cend(), compare_fn);
|
||||
// Up and left to indicate back
|
||||
if (event == KeyEvent::Back) {
|
||||
collection.clear();
|
||||
widget_collect_visible(top_widget, find_back_fn, collection);
|
||||
if (!collection.empty())
|
||||
set_focus_widget(collection[0].first);
|
||||
} else if (nearest != collection.cend()) {
|
||||
// focus->blur();
|
||||
const auto new_focus = (*nearest).first;
|
||||
set_focus_widget(new_focus);
|
||||
} else {
|
||||
if ((focus_widget()->id >= 0) && (event == KeyEvent::Left)) {
|
||||
// Stuck left, move to back button
|
||||
collection.clear();
|
||||
widget_collect_visible(top_widget, find_back_fn, collection);
|
||||
if (!collection.empty())
|
||||
set_focus_widget(collection[0].first);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace ui */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue