src/view.cpp in reflexion-0.1.22 vs src/view.cpp in reflexion-0.1.23
- old
+ new
@@ -7,10 +7,11 @@
#include "reflex/timer.h"
#include "reflex/filter.h"
#include "reflex/exception.h"
#include "reflex/debug.h"
#include "window.h"
+#include "event.h"
#include "selector.h"
#include "timer.h"
#include "style.h"
#include "shape.h"
#include "world.h"
@@ -875,11 +876,11 @@
assert(result && view);
result->clear();
Selector* sel = view->self->pselector.get();
- if (!sel || sel->is_empty())
+ if (!sel || sel->empty())
return false;
get_styles_for_selector(result, view, *sel);
return !result->empty();
}
@@ -1209,11 +1210,11 @@
if (!view)
argument_error(__FILE__, __LINE__);
View::Data* self = view->self.get();
- if (selector.is_empty())
+ if (selector.empty())
self->add_flag(View::Data::UPDATE_STYLE);
else
self->selectors_for_update().insert(selector);
}
@@ -1226,31 +1227,27 @@
view->self->add_flag(View::Data::UPDATE_SHAPES);
}
template <typename FUN, typename EVENT>
static void
- call_children (View* parent, FUN fun, const EVENT& e)
+ call_children (View* parent, const EVENT& event, FUN fun)
{
assert(parent);
- View::ChildList* pchildren = parent->self->pchildren.get();
- if (pchildren)
- {
- for (auto& pchild : *pchildren)
- fun(pchild.get(), e);
- }
+ auto* pchildren = parent->self->pchildren.get();
+ if (!pchildren) return;
+
+ for (auto& pchild : *pchildren)
+ fun(pchild.get(), event);
}
void
View_call_key_event (View* view, const KeyEvent& event)
{
if (!view)
argument_error(__FILE__, __LINE__);
- bool capturing = view->capture() & View::CAPTURE_KEY;
- if (capturing != event.capture) return;
-
KeyEvent e = event;
view->on_key(&e);
switch (e.type)
{
@@ -1259,75 +1256,65 @@
case KeyEvent::NONE: break;
}
}
static void
- filter_pointer_event (
- PointerEvent* to, const PointerEvent& from, const Bounds& frame)
+ update_captures (View* view, PointerEvent* event)
{
- assert(to);
+ Window* win = view->window();
+ if (!win)
+ invalid_state_error(__FILE__, __LINE__);
- const Point& offset = frame.position();
-
- to->size = 0;
- for (size_t i = 0; i < from.size; ++i) {
- const Point& pos = from.position(i);
- if (!frame.is_include(pos))
- continue;
-
- to->positions[i] = pos - offset;
- ++to->size;
- }
+ PointerEvent_each_pointer(event, [&](const auto& pointer)
+ {
+ auto action = pointer.action();
+ if (action == Pointer::DOWN)
+ {
+ Window_register_capture(win, view, pointer.id());
+ }
+ else if (action == Pointer::UP || action == Pointer::CANCEL)
+ {
+ Window_unregister_capture(win, view, pointer.id());
+ }
+ });
}
static void
- scroll_and_zoom_positions (PointerEvent* e, const Point* scroll, float zoom)
+ call_pointer_event_for_each_child (View* parent, const PointerEvent& event)
{
- static const Point ZERO = 0;
-
- assert(zoom != 0);
-
- if (!scroll) scroll = &ZERO;
- if (*scroll == 0 && zoom == 1)
- return;
-
- for (size_t i = 0; i < e->size; ++i)
- {
- e->position(i) -= *scroll;
- e->position(i) /= zoom;
- }
+ call_children(parent, event, [](View* child, const PointerEvent& e) {
+ PointerEvent e2 = e;
+ PointerEvent_update_for_child_view(&e2, child);
+ View_call_pointer_event(child, e2);
+ });
}
void
View_call_pointer_event (View* view, const PointerEvent& event)
{
if (!view)
argument_error(__FILE__, __LINE__);
- bool capturing = view->capture() & View::CAPTURE_POINTER;
- if (capturing != event.capture) return;
-
- PointerEvent e = event;
- filter_pointer_event(&e, event, view->frame());
-
- if (!capturing && e.size == 0)
+ if (event.empty())
return;
- scroll_and_zoom_positions(&e, view->self->pscroll.get(), view->zoom());
-
+ PointerEvent e = event;
view->on_pointer(&e);
- switch (e.type)
+ switch (e[0].action())
{
- case PointerEvent::DOWN: view->on_pointer_down(&e); break;
- case PointerEvent::UP: view->on_pointer_up(&e); break;
- case PointerEvent::MOVE: view->on_pointer_move(&e); break;
- case PointerEvent::NONE: break;
+ case Pointer::DOWN: view->on_pointer_down(&e); break;
+ case Pointer::UP: view->on_pointer_up(&e); break;
+ case Pointer::MOVE: view->on_pointer_move(&e); break;
+ case Pointer::CANCEL: view->on_pointer_cancel(&e); break;
+ default: break;
}
- if (!event.capture)
- call_children(view, View_call_pointer_event, e);
+ update_captures(view, &e);
+
+ if (!e.is_captured())
+ call_pointer_event_for_each_child(view, e);
}
void
View_call_wheel_event (View* view, const WheelEvent& event)
{
@@ -1342,11 +1329,11 @@
WheelEvent e = event;
e.position() -= frame.position();
view->on_wheel(&e);
- call_children(view, View_call_wheel_event, e);
+ call_children(view, e, View_call_wheel_event);
}
void
View_call_contact_event (View* view, const ContactEvent& event)
{
@@ -1478,49 +1465,67 @@
}
Point
View::from_parent (const Point& point) const
{
- not_implemented_error(__FILE__, __LINE__);
- return 0;
+ if (!parent())
+ invalid_state_error(__FILE__, __LINE__);
+
+ return point - frame().position();
}
Point
View::to_parent (const Point& point) const
{
- not_implemented_error(__FILE__, __LINE__);
- return 0;
+ if (!parent())
+ invalid_state_error(__FILE__, __LINE__);
+
+ return point + frame().position();
}
Point
View::from_window (const Point& point) const
{
+ if (!window())
+ invalid_state_error(__FILE__, __LINE__);
+
Point p = point;
- for (const View* v = parent(); v; v = v->parent())
+ for (const View* v = this; v; v = v->parent())
p -= v->frame().position();
return p;
}
Point
View::to_window (const Point& point) const
{
- not_implemented_error(__FILE__, __LINE__);
- return 0;
+ if (!window())
+ invalid_state_error(__FILE__, __LINE__);
+
+ Point p = point;
+ for (const View* v = this; v; v = v->parent())
+ p += v->frame().position();
+ return p;
}
Point
View::from_screen (const Point& point) const
{
- not_implemented_error(__FILE__, __LINE__);
- return 0;
+ const Window* w = window();
+ if (!w)
+ invalid_state_error(__FILE__, __LINE__);
+
+ return w->from_screen(from_window(point));
}
Point
View::to_screen (const Point& point) const
{
- not_implemented_error(__FILE__, __LINE__);
- return 0;
+ const Window* w = window();
+ if (!w)
+ invalid_state_error(__FILE__, __LINE__);
+
+ return w->to_screen(to_window(point));
}
static void
set_parent (View* view, View* parent)
{
@@ -1698,11 +1703,11 @@
}
Style*
View::get_style (const Selector& selector, bool create)
{
- if (selector.is_empty())
+ if (selector.empty())
return style(create);
StyleList* pstyles = self->pstyles.get();
if (pstyles)
{
@@ -2067,22 +2072,23 @@
}
void
View::set_capture (uint types)
{
- if (types == self->capture) return;
+ Window* w = window();
+ if (!w || types == self->capture) return;
uint old = self->capture;
self->capture = types;
bool registered = old != CAPTURE_NONE;
bool capture = types != CAPTURE_NONE;
if (capture && !registered)
- View_register_capture(this);
+ Window_register_capture(w, this);
else if (!capture && registered)
- View_unregister_capture(this);
+ Window_unregister_capture(w, this);
CaptureEvent e(~old & types, old & ~types);
on_capture(&e);
}
@@ -2531,9 +2537,14 @@
{
}
void
View::on_pointer_move (PointerEvent* e)
+ {
+ }
+
+ void
+ View::on_pointer_cancel (PointerEvent* e)
{
}
void
View::on_wheel (WheelEvent* e)