@@ -12,6 +12,7 @@ type PressEvent = {||};
1212type ButtonProps = { |
1313 title : string ,
1414 onPress : ( event ? : PressEvent ) => mixed ,
15+ onClick : ( event ? : PressEvent ) => mixed ,
1516 touchSoundDisabled ?: ?boolean ,
1617 color ?: ?string ,
1718 /**
@@ -65,7 +66,7 @@ type ButtonProps = {|
6566| } ;
6667
6768function renderButton ( props : ButtonProps , apeContext , parentLayout ) {
68- const { spatialGeometry = { x : 0 , y : 0 } , relativeIndex } = parentLayout ;
69+ const { spatialGeometry = { x : 0 , y : 0 } } = parentLayout ;
6970 const { ctx} = apeContext ;
7071 // If is relative and x and y haven't be processed, don't render
7172 if ( ! spatialGeometry ) return null ;
@@ -77,10 +78,11 @@ function renderButton(props: ButtonProps, apeContext, parentLayout) {
7778 let y = spatialGeometry . y || 0 ;
7879 let width = x + y ;
7980 let height = ButtonDefaults . containerStyle . height ;
80- // if(parentLayout && parentLayout.style){
81- // width = parentLayout.style.width
82- // height = parentLayout.style.height
83- // }
81+
82+ // ctx.addEventListener('click',()=>{
83+ // alert('hi')
84+ // })
85+
8486 ctx . beginPath ( ) ;
8587 ctx . fillStyle = backgroundColor ;
8688 ctx . moveTo ( x , y ) ;
@@ -119,6 +121,50 @@ function renderButton(props: ButtonProps, apeContext, parentLayout) {
119121 ctx . fillText ( title , x + width / 2 , y + height / 2 ) ;
120122 //ctx.fillText('Start', x + height /2, y + height /2 );
121123 ctx . closePath ( ) ;
124+
125+ //const canvas = document.getElementById('root')
126+ //const context = canvas.getContext('2d')
127+ const onClick = ( event : SyntheticEvent < HTMLButtonElement > ) => {
128+ const rect = {
129+ x,
130+ y,
131+ height,
132+ width,
133+ } ;
134+ // const clientX = event.clientX - ctx.canvas.offsetLeft
135+ // const clientY = event.clientY - ctx.canvas.offsetTop
136+ const mousePosition = getMousePos ( ctx . canvas , event ) ;
137+
138+ // console.log('mouse',x,y)
139+ console . log ( 'rect' , rect ) ;
140+ console . log ( 'mouse' , mousePosition ) ;
141+ if ( isInside ( mousePosition , rect ) ) {
142+ alert ( 'hi' ) ;
143+ } else {
144+ //alert('not inside')
145+ }
146+ } ;
147+ function getMousePos ( canvas , event ) {
148+ var rect = canvas . getBoundingClientRect ( ) ;
149+ return {
150+ x : event . clientX - rect . left ,
151+ y : event . clientY - rect . top ,
152+ } ;
153+ }
154+ const isInside = ( pos , rect ) => {
155+ return (
156+ pos . x > rect . x &&
157+ pos . x < rect . x + rect . width &&
158+ pos . y < rect . y + rect . heigth &&
159+ pos . y > rect . y
160+ ) ;
161+ } ;
162+ const redrawButton = ( ) => { } ;
163+ ctx . canvas . addEventListener ( 'click' , onClick , false ) ;
164+ ctx . canvas . addEventListener ( 'focus' , redrawButton ) ;
165+ ctx . canvas . addEventListener ( 'blur' , redrawButton ) ;
166+
167+ // events
122168}
123169
124170export default function createButtonInstance ( props : ButtonProps ) : mixed {
0 commit comments