Skip to content

Commit 365b250

Browse files
authored
Merge pull request livecode#6307 from livecodeian/feature-widget-html5button
[[ Emscripten ]] Native HTML5 Button Widget
2 parents 73a2001 + 3645b56 commit 365b250

23 files changed

+1790
-207
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# HTML5 Native Layer
2+
3+
Support has been added for widgets to use HTML5 elements as native layers.
4+
5+
The built-in emscripten module (com.livecode.emscripten) has been added
6+
to provide useful functions for interacting with JavaScript within the
7+
browser, and to convert JavaScript objects to pointers suitable for setting
8+
as the widget's native layer.

engine/engine-sources.gypi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@
560560
#'src/text-segment.cpp',
561561
#'src/text-simplebreakingengine.cpp',
562562

563+
# Group "Emscripten"
564+
'src/jsobject.h',
565+
'src/jsobject.cpp',
566+
563567
# Group "Desktop"
564568
'src/quicktime.cpp',
565569
'src/quicktime.stubs',
@@ -771,10 +775,14 @@
771775
'src/em-filehandle.cpp',
772776
'src/em-fontlist.h',
773777
'src/em-fontlist.cpp',
778+
'src/em-javascript.h',
779+
'src/em-javascript.cpp',
774780
'src/em-liburl.h',
775781
'src/em-liburl.cpp',
776782
'src/em-liburl.js',
777783
'src/em-main.cpp',
784+
'src/em-native-layer.h',
785+
'src/em-native-layer.cpp',
778786
'src/em-osspec-misc.cpp',
779787
'src/em-osspec-network.cpp',
780788
'src/em-preamble-overlay.js',
@@ -984,6 +992,7 @@
984992
'src/module-resources.cpp',
985993

986994
'src/module-browser.cpp',
995+
'src/module-emscripten.cpp',
987996
],
988997

989998
# Engine LCB files containing syntax
@@ -1005,6 +1014,7 @@
10051014
'engine_other_lcb_files':
10061015
[
10071016
'src/browser.lcb',
1017+
'src/emscripten.lcb',
10081018
],
10091019

10101020
# Engine cpptest source files

engine/src/em-dc.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1919
#include "prefix.h"
2020

2121
#include "em-dc.h"
22+
#include "em-javascript.h"
2223
#include "em-view.h"
2324
#include "em-async.h"
2425
#include "em-event.h"
@@ -99,15 +100,39 @@ bool MCEmscriptenHandleMousePress(MCStack *p_stack, uint32_t p_time, uint32_t p_
99100
if (MCnoui) return false;
100101

101102
MCScreenDC *t_dc = static_cast<MCScreenDC *>(MCscreen);
103+
t_dc->update_mouse_press_state(p_state, p_button);
102104

103-
t_dc->handle_mouse_press(p_stack, p_time, p_modifiers, p_state, p_button);
105+
MCEventQueuePostMousePress(p_stack, p_time, p_modifiers, p_state, p_button);
106+
107+
return true;
108+
}
109+
110+
static inline MCPoint MCEmscriptenWindowToGlobalLoc(MCStack *p_stack, const MCPoint &p_loc)
111+
{
112+
MCRectangle t_window_rect = p_stack->view_getrect();
113+
return MCPointOffset(p_loc, t_window_rect.x, t_window_rect.y);
114+
}
115+
116+
MC_DLLEXPORT_DEF
117+
bool MCEmscriptenHandleMousePosition(MCStack *p_stack, uint32_t p_time, uint32_t p_modifiers, int32_t p_x, int32_t p_y)
118+
{
119+
if (MCnoui) return false;
120+
121+
MCScreenDC *t_dc = static_cast<MCScreenDC *>(MCscreen);
122+
123+
MCPoint t_position = MCPointMake(p_x, p_y);
124+
if (p_stack)
125+
t_position = MCEmscriptenWindowToGlobalLoc(p_stack, t_position);
126+
if (t_dc->update_mouse_position(t_position))
127+
MCEventQueuePostMousePosition(p_stack, p_time, p_modifiers, p_x, p_y);
104128

105129
return true;
106130
}
107131

108132
MCScreenDC::MCScreenDC()
109133
: m_main_window(nil), m_mouse_button_state(0)
110134
{
135+
m_mouse_position = MCPointMake(-1,-1);
111136
}
112137

113138
MCScreenDC::~MCScreenDC()
@@ -118,6 +143,7 @@ Boolean
118143
MCScreenDC::open()
119144
{
120145
return
146+
MCEmscriptenJSInitialize() &&
121147
MCEmscriptenEventInitialize() &&
122148
MCEmscriptenViewInitialize() &&
123149
MCEmscriptenLibUrlInitialize() &&
@@ -132,6 +158,7 @@ MCScreenDC::close(Boolean force)
132158
MCEmscriptenViewFinalize();
133159
MCEmscriptenEventFinalize();
134160
MCEmscriptenLibUrlFinalize();
161+
MCEmscriptenJSFinalize();
135162

136163
return true;
137164
}
@@ -412,7 +439,7 @@ MCScreenDC::popupaskdialog(uint32_t p_type, MCStringRef p_title, MCStringRef p_m
412439

413440

414441
void
415-
MCScreenDC::handle_mouse_press(MCStack *p_stack, uint32_t p_time, uint32_t p_modifiers, MCMousePressState p_state, int32_t p_button)
442+
MCScreenDC::update_mouse_press_state(MCMousePressState p_state, int32_t p_button)
416443
{
417444
// track mouse button pressed state
418445
/* NOTE - assumes there are no more than 32 mouse buttons */
@@ -423,8 +450,17 @@ MCScreenDC::handle_mouse_press(MCStack *p_stack, uint32_t p_time, uint32_t p_mod
423450
else if (p_state == kMCMousePressStateUp)
424451
m_mouse_button_state &= ~(1UL << p_button);
425452
}
453+
}
454+
455+
bool
456+
MCScreenDC::update_mouse_position(const MCPoint &p_position)
457+
{
458+
if (MCPointIsEqual(m_mouse_position, p_position))
459+
return false;
426460

427-
MCEventQueuePostMousePress(p_stack, p_time, p_modifiers, p_state, p_button);
461+
m_mouse_position = p_position;
462+
463+
return true;
428464
}
429465

430466
Boolean
@@ -442,6 +478,6 @@ MCScreenDC::platform_querymouse(int16_t& r_x, int16_t& r_y)
442478
{
443479
// There is no asynchronous mouse position in Emscripten; just whatever the
444480
// browser has told us about.
445-
r_x = MCmousex;
446-
r_y = MCmousey;
481+
r_x = m_mouse_position.x;
482+
r_y = m_mouse_position.y;
447483
}

engine/src/em-dc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C" void MCEmscriptenGetDisplayRect(uint32_t *r_left, uint32_t *r_top, ui
3838

3939
extern "C" MCStack *MCEmscriptenGetStackForWindow(Window p_window);
4040
extern "C" bool MCEmscriptenHandleMousePress(MCStack *p_stack, uint32_t p_time, uint32_t p_modifiers, MCMousePressState p_state, int32_t p_button);
41+
extern "C" bool MCEmscriptenHandleMousePosition(MCStack *p_stack, uint32_t p_time, uint32_t p_modifiers, int32_t p_x, int32_t p_y);
4142

4243
extern "C" uint32_t MCEmscriptenCreateWindow();
4344
extern "C" void MCEmscriptenDestroyWindow(uint32_t p_window_id);
@@ -92,7 +93,9 @@ class MCScreenDC: public MCUIDC
9293
virtual bool popupaskdialog(uint32_t p_type, MCStringRef p_title, MCStringRef p_message, MCStringRef p_initial, bool p_hint, MCStringRef& r_result);
9394

9495
/* Mouse management */
95-
void handle_mouse_press(MCStack *p_stack, uint32_t p_time, uint32_t p_modifiers, MCMousePressState p_state, int32_t p_button);
96+
void update_mouse_press_state(MCMousePressState p_state, int32_t p_button);
97+
bool update_mouse_position(const MCPoint &p_position);
98+
9699
virtual Boolean getmouse(uint2 button, Boolean& r_abort);
97100
virtual void platform_querymouse(int16_t& r_x, int16_t& r_y);
98101

@@ -102,6 +105,7 @@ class MCScreenDC: public MCUIDC
102105
private:
103106
Window m_main_window;
104107
uint32_t m_mouse_button_state;
108+
MCPoint m_mouse_position;
105109
};
106110

107111
#endif /* ! __MC_EMSCRIPTEN_DC_H__ */

0 commit comments

Comments
 (0)