Skip to content

Commit

Permalink
Major improvements to pointer/touch input:
Browse files Browse the repository at this point in the history
List of changes:
-Refactorized virtual scroll methods and introduced virtual GetScrollInfo.
-Added TBScroller to do slow down scroll after panning.
-Added TBScrollerSnapListener for snapped smooth scrolling.
-Added keyboard scroll support in TBScrollContainer and TBSelectList.
-Added possibility to cancel EVENT_TYPE_CLICK for current touch (stopping scroll should not cause click)
-Added flag to TBWidgetEvent to distinguish cursor base pointer events from touch events (Fixes issue capstone-engine#2).
 --Pan TBEditField on touch pointer events instead of doing selection scroll.
 --Touch events should not result in setting the hover state automatically.
  • Loading branch information
fruxo committed Jul 10, 2013
1 parent 0342c25 commit 8c25e04
Show file tree
Hide file tree
Showing 26 changed files with 988 additions and 149 deletions.
2 changes: 2 additions & 0 deletions Demo/VisualStudio/tinkerbell_static.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<ClCompile Include="..\..\tinkerbell\src\tb_object.cpp" />
<ClCompile Include="..\..\tinkerbell\src\tb_popup_window.cpp" />
<ClCompile Include="..\..\tinkerbell\src\tb_renderer.cpp" />
<ClCompile Include="..\..\tinkerbell\src\tb_scroller.cpp" />
<ClCompile Include="..\..\tinkerbell\src\tb_scroll_container.cpp" />
<ClCompile Include="..\..\tinkerbell\src\tb_select.cpp" />
<ClCompile Include="..\..\tinkerbell\src\tb_select_item.cpp" />
Expand Down Expand Up @@ -92,6 +93,7 @@
<ClInclude Include="..\..\tinkerbell\src\tb_object.h" />
<ClInclude Include="..\..\tinkerbell\src\tb_popup_window.h" />
<ClInclude Include="..\..\tinkerbell\src\tb_renderer.h" />
<ClInclude Include="..\..\tinkerbell\src\tb_scroller.h" />
<ClInclude Include="..\..\tinkerbell\src\tb_scroll_container.h" />
<ClInclude Include="..\..\tinkerbell\src\tb_select.h" />
<ClInclude Include="..\..\tinkerbell\src\tb_select_item.h" />
Expand Down
6 changes: 6 additions & 0 deletions Demo/VisualStudio/tinkerbell_static.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@
<ClCompile Include="..\..\tinkerbell\src\tb_debug.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tinkerbell\src\tb_scroller.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\tinkerbell\src\tb_language.h">
Expand Down Expand Up @@ -338,6 +341,9 @@
<ClInclude Include="..\..\tinkerbell\src\tb_config.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="..\..\tinkerbell\src\tb_scroller.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\readme.txt" />
Expand Down
9 changes: 9 additions & 0 deletions Demo/demo01/Demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "tb_widgets_listener.h"
#include "tb_message_window.h"
#include "tb_msg.h"
#include "tb_scroller.h"
#include "platform/Application.h"

using namespace tinkerbell;
Expand Down Expand Up @@ -49,6 +50,14 @@ class ImageWindow : public DemoWindow
virtual bool OnEvent(const TBWidgetEvent &ev);
};

class PageWindow : public DemoWindow, public TBScrollerSnapListener
{
public:
PageWindow();
virtual bool OnEvent(const TBWidgetEvent &ev);
virtual void OnScrollSnap(TBWidget *target_widget, int &target_x, int &target_y);
};

class AnimationsWindow : public DemoWindow
{
public:
Expand Down
28 changes: 28 additions & 0 deletions Demo/demo01/Demo01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,29 @@ bool ImageWindow::OnEvent(const TBWidgetEvent &ev)
return DemoWindow::OnEvent(ev);
}

// == PageWindow =============================================================

PageWindow::PageWindow()
{
LoadResourceFile("Demo/demo01/ui_resources/test_scroller_snap.tb.txt");

// Listen to the pagers scroller
if (TBWidget *pager = GetWidgetByID(TBIDC("page-scroller")))
pager->GetScroller()->SetSnapListener(this);
}

bool PageWindow::OnEvent(const TBWidgetEvent &ev)
{
return DemoWindow::OnEvent(ev);
}

void PageWindow::OnScrollSnap(TBWidget *target_widget, int &target_x, int &target_y)
{
int page_w = target_widget->GetPaddingRect().w;
int target_page = (target_x + page_w / 2) / page_w;
target_x = target_page * page_w;
}

// == AnimationsWindow ========================================================

AnimationsWindow::AnimationsWindow()
Expand Down Expand Up @@ -640,6 +663,11 @@ bool MainWindow::OnEvent(const TBWidgetEvent &ev)
new ImageWindow();
return true;
}
else if (ev.target->GetID() == TBIDC("test-page"))
{
new PageWindow();
return true;
}
else if (ev.target->GetID() == TBIDC("test-animations"))
{
new AnimationsWindow();
Expand Down
22 changes: 22 additions & 0 deletions Demo/demo01/ui_resources/test_scroller_snap.tb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
WindowInfo
title TBScrollerSnapListener
TBScrollContainer: id: "page-scroller"
lp: width: 200, height: 300
scroll-mode off
TBLayout
spacing 0
TBContainer
lp: width: 200, height: 300
TBTextField: text: "One - Swipe to next page"
TBContainer
lp: width: 200, height: 300
TBTextField: text: "Two"
TBContainer
lp: width: 200, height: 300
TBTextField: text: "Three"
TBContainer
lp: width: 200, height: 300
TBTextField: text: "Four"
TBContainer
lp: width: 200, height: 300
TBTextField: text: "Five - Last page"
1 change: 1 addition & 0 deletions Demo/demo01/ui_resources/test_ui.tb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ TBLayout: axis: y, distribution-position: left top
TBButton: text: "ScrollContainer & misc.", id: "test-scroll-container"
TBButton: text: "TBSelectList", id: "test-list"
TBButton: text: "TBImage", id: "test-image"
TBButton: text: "TBScrollerSnapListener", id: "test-page"
TBButton: text: "Animations", id: "test-animations"
TBButton: text: "Skin conditions", id: "test-skin-conditions"
TBButton: text: "TBToggleContainer", id: "test-toggle-container"
Expand Down
16 changes: 9 additions & 7 deletions Demo/platform/port_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ static bool InvokeShortcut(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modif
else
return false;

TBWidgetEvent ev(EVENT_TYPE_SHORTCUT, 0, 0, modifierkeys);
TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
ev.modifierkeys = modifierkeys;
ev.ref_id = id;
return TBWidget::focused_widget->InvokeEvent(ev);
}
Expand Down Expand Up @@ -161,7 +162,8 @@ static void key_callback(GLFWwindow window, int key, int action)
case GLFW_KEY_MENU:
if (TBWidget::focused_widget && !down)
{
TBWidgetEvent ev(EVENT_TYPE_CONTEXT_MENU, 0, 0, GetModifierKeys());
TBWidgetEvent ev(EVENT_TYPE_CONTEXT_MENU);
ev.modifierkeys = modifier;
TBWidget::focused_widget->InvokeEvent(ev);
}
break;
Expand Down Expand Up @@ -211,18 +213,18 @@ static void mouse_button_callback(GLFWwindow window, int button, int action)
last_y = y;
last_time = time;

g_backend->GetRoot()->InvokePointerDown(x, y, counter, GetModifierKeys());
g_backend->GetRoot()->InvokePointerDown(x, y, counter, GetModifierKeys(), false);
}
else
g_backend->GetRoot()->InvokePointerUp(x, y, GetModifierKeys());
g_backend->GetRoot()->InvokePointerUp(x, y, GetModifierKeys(), false);
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE)
{
g_backend->GetRoot()->InvokePointerMove(x, y, GetModifierKeys());
g_backend->GetRoot()->InvokePointerMove(x, y, GetModifierKeys(), false);
if (TBWidget::hovered_widget)
{
TBWidget::hovered_widget->ConvertFromRoot(x, y);
TBWidgetEvent ev(EVENT_TYPE_CONTEXT_MENU, x, y, GetModifierKeys());
TBWidgetEvent ev(EVENT_TYPE_CONTEXT_MENU, x, y, false, GetModifierKeys());
TBWidget::hovered_widget->InvokeEvent(ev);
}
}
Expand All @@ -233,7 +235,7 @@ void cursor_position_callback(GLFWwindow window, int x, int y)
mouse_x = x;
mouse_y = y;
if (g_backend->GetRoot())
g_backend->GetRoot()->InvokePointerMove(x, y, GetModifierKeys());
g_backend->GetRoot()->InvokePointerMove(x, y, GetModifierKeys(), false);
}

static void scroll_callback(GLFWwindow window, double x, double y)
Expand Down
13 changes: 13 additions & 0 deletions DemoAndroid/assets/layout/main_layout.tb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ TBLayout
styling 1
text: "<color #0794f8>Android test stuff</color>\n" \
"This is a read-only multiline textfield with styling enabled.\n"
TBEditField
multiline 1
gravity all
text: "Row 1\n" \
"Row 2\n" \
"Row 3\n" \
"Row 4\n" \
"Row 5\n" \
"Row 6\n" \
"Row 7\n" \
"Row 8\n" \
"Row 9\n" \
"Row 10"
TBLayout: axis: y
TBEditField: gravity: all, skin: 0, multiline: 1, readonly: 1, adapt-to-content: 1
text: "Some speed tests on the layout used in this demo:"
Expand Down
1 change: 1 addition & 0 deletions DemoAndroid/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ LOCAL_SRC_FILES := jni_glue.cpp \
../../tinkerbell/src/tb_msg.cpp \
../../tinkerbell/src/tb_object.cpp \
../../tinkerbell/src/tb_renderer.cpp \
../../tinkerbell/src/tb_scroller.cpp \
../../tinkerbell/src/tb_scroll_container.cpp \
../../tinkerbell/src/tb_select.cpp \
../../tinkerbell/src/tb_select_item.cpp \
Expand Down
8 changes: 4 additions & 4 deletions DemoAndroid/jni/jni_glue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ JNI_VOID_TINKERBELL(OnPointer)(JNIEnv *env, jobject obj, jfloat x, jfloat y, jin
set_jnienv(env);
//TBDebugOut("OnPointer");

int counter = 0;
int counter = 1;
if (down)
root->InvokePointerDown(x, y, counter, TB_MODIFIER_NONE);
root->InvokePointerDown(x, y, counter, TB_MODIFIER_NONE, true);
else
root->InvokePointerUp(x, y, TB_MODIFIER_NONE);
root->InvokePointerUp(x, y, TB_MODIFIER_NONE, true);
}

JNI_VOID_TINKERBELL(OnPointer2)(JNIEnv *env, jobject obj, jfloat x, jfloat y, jint down)
Expand All @@ -203,5 +203,5 @@ JNI_VOID_TINKERBELL(OnPointerMove)(JNIEnv *env, jobject obj, jfloat x, jfloat y,
set_jnienv(env);
//TBDebugOut("OnPointerMove");

root->InvokePointerMove(x, y, TB_MODIFIER_NONE);
root->InvokePointerMove(x, y, TB_MODIFIER_NONE, true);
}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ endif

TARGET = RunDemo
SRC = tinkerbell/src/tb_layout.cpp \
tinkerbell/src/tb_scroller.cpp \
tinkerbell/src/tb_scroll_container.cpp \
tinkerbell/src/tb_skin.cpp \
tinkerbell/src/tb_skin_util.cpp \
Expand Down
29 changes: 26 additions & 3 deletions tinkerbell/src/tb_editfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ bool TBEditField::GetCustomSkinCondition(const TBSkinCondition::CONDITION_INFO &
return false;
}

void TBEditField::ScrollTo(int x, int y)
{
int old_x = m_scrollbar_x.GetValue();
int old_y = m_scrollbar_y.GetValue();
m_style_edit.SetScrollPos(x, y);
if (old_x != m_scrollbar_x.GetValue() ||
old_y != m_scrollbar_y.GetValue())
TBWidget::Invalidate();
}

TBWidget::ScrollInfo TBEditField::GetScrollInfo()
{
ScrollInfo info;
info.min_x = static_cast<int>(m_scrollbar_x.GetMinValue());
info.min_y = static_cast<int>(m_scrollbar_y.GetMinValue());
info.max_x = static_cast<int>(m_scrollbar_x.GetMaxValue());
info.max_y = static_cast<int>(m_scrollbar_y.GetMaxValue());
info.x = m_scrollbar_x.GetValue();
info.y = m_scrollbar_y.GetValue();
return info;
}

bool TBEditField::OnEvent(const TBWidgetEvent &ev)
{
if (ev.type == EVENT_TYPE_CHANGED && ev.target == &m_scrollbar_x)
Expand All @@ -184,7 +206,7 @@ bool TBEditField::OnEvent(const TBWidgetEvent &ev)
TBRect padding_rect = GetPaddingRect();
if (m_style_edit.MouseDown(
TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y),
1, ev.count, TB_MODIFIER_NONE))
1, ev.count, TB_MODIFIER_NONE, ev.touch))
{
// Post a message to start selection scroll
PostMessageDelayed(TBIDC("selscroll"), nullptr, SELECTION_SCROLL_DELAY);
Expand All @@ -199,7 +221,8 @@ bool TBEditField::OnEvent(const TBWidgetEvent &ev)
else if (ev.type == EVENT_TYPE_POINTER_UP && ev.target == this)
{
TBRect padding_rect = GetPaddingRect();
return m_style_edit.MouseUp(TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y), 1, TB_MODIFIER_NONE);
return m_style_edit.MouseUp(TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y),
1, TB_MODIFIER_NONE, ev.touch);
}
else if (ev.type == EVENT_TYPE_KEY_DOWN)
{
Expand Down Expand Up @@ -407,7 +430,7 @@ void TBEditField::OnChange()

//FIX: some of theese in tinkerbell doesn't check if the widget is removed afterwards!
// it's not unlikely that it might result in the widget to be removed.
TBWidgetEvent ev(EVENT_TYPE_CHANGED, 0, 0);
TBWidgetEvent ev(EVENT_TYPE_CHANGED);
InvokeEvent(ev);
}

Expand Down
6 changes: 5 additions & 1 deletion tinkerbell/src/tb_editfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TBEditFieldScrollRoot : public TBWidget
/** TBEditField is a one line or multi line textfield that is editable or
read-only. It can also be a passwordfield by calling
SetEditType(EDIT_TYPE_PASSWORD).
It may perform styling of text and contain custom embedded content,
if enabled by SetStyling(true). Disabled by default.
*/
Expand Down Expand Up @@ -152,6 +152,10 @@ class TBEditField : public TBWidget, private TBStyleEditListener, public TBMessa
virtual bool SetPlaceholderText(const char *text) { return m_placeholder.SetText(text); }
virtual bool GetPlaceholderText(TBStr &text) { return m_placeholder.GetText(text); }

virtual void ScrollTo(int x, int y);
virtual TBWidget::ScrollInfo GetScrollInfo();
virtual TBWidget *GetScrollRoot() { return &m_root; }

virtual bool OnEvent(const TBWidgetEvent &ev);
virtual void OnPaint(const PaintProps &paint_props);
virtual void OnPaintChildren(const PaintProps &paint_props);
Expand Down
2 changes: 1 addition & 1 deletion tinkerbell/src/tb_inline_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void TBInlineSelect::SetValueInternal(int value, bool update_text)
m_editfield.SetText(strval);
}

TBWidgetEvent ev(EVENT_TYPE_CHANGED, 0, 0);
TBWidgetEvent ev(EVENT_TYPE_CHANGED);
InvokeEvent(ev);

// Warning: Do nothing here since the event might have deleted us.
Expand Down
36 changes: 13 additions & 23 deletions tinkerbell/src/tb_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,35 +483,25 @@ void TBLayout::GetChildTranslation(int &x, int &y) const
}
}

void TBLayout::ScrollIntoView(const TBRect &rect)
void TBLayout::ScrollTo(int x, int y)
{
TBRect m_rect = GetRect();
TBRect visible_rect = m_axis == AXIS_X ? TBRect(m_overflow_scroll, 0, m_rect.w, m_rect.h) :
TBRect(0, m_overflow_scroll, m_rect.w, m_rect.h);
int new_x = visible_rect.x;
int new_y = visible_rect.y;

if (rect.y <= visible_rect.y)
new_y = rect.y;
else if (rect.y + rect.h > visible_rect.y + visible_rect.h)
new_y = rect.y + rect.h - visible_rect.h;

if (rect.x <= visible_rect.x)
new_x = rect.x;
else if (rect.x + rect.w > visible_rect.x + visible_rect.w)
new_x = rect.x + rect.w - visible_rect.w;

SetOverflowScroll(m_axis == AXIS_X ? new_x : new_y);
SetOverflowScroll(m_axis == AXIS_X ? x : y);
}

void TBLayout::ScrollBy(int &dx, int &dy)
TBWidget::ScrollInfo TBLayout::GetScrollInfo()
{
int old_overflow_scroll = m_overflow_scroll;
SetOverflowScroll(m_axis == AXIS_X ? m_overflow_scroll + dx : m_overflow_scroll + dy);
ScrollInfo info;
if (m_axis == AXIS_X)
dx -= m_overflow_scroll - old_overflow_scroll;
{
info.max_x = m_overflow;
info.x = m_overflow_scroll;
}
else
dy -= m_overflow_scroll - old_overflow_scroll;
{
info.max_y = m_overflow;
info.y = m_overflow_scroll;
}
return info;
}

}; // namespace tinkerbell
4 changes: 2 additions & 2 deletions tinkerbell/src/tb_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ class TBLayout : public TBWidget
virtual void OnResized(int old_w, int old_h);
virtual void OnInflateChild(TBWidget *child);
virtual void GetChildTranslation(int &x, int &y) const;
virtual void ScrollIntoView(const TBRect &rect);
virtual void ScrollBy(int &dx, int &dy);
virtual void ScrollTo(int x, int y);
virtual TBWidget::ScrollInfo GetScrollInfo();
protected:
AXIS m_axis;
int m_spacing;
Expand Down
Loading

0 comments on commit 8c25e04

Please sign in to comment.