77
88import { ButtonDefaults } from '../constants' ;
99
10+
11+
1012//TODO adjust Opacity when focus, Blur
1113type PressEvent = { || } ;
1214type ButtonProps = { |
1315 title : string ,
1416 onPress : ( event ? : PressEvent ) => mixed ,
15- onClick : ( event ? : PressEvent ) => mixed ,
17+ onClick : ( event ?: SyntheticMouseEvent < T > ) => mixed ,
1618 touchSoundDisabled ?: ?boolean ,
1719 color ?: ?string ,
1820 /**
@@ -66,8 +68,11 @@ type ButtonProps = {|
6668| } ;
6769
6870function renderButton ( props : ButtonProps , apeContext , parentLayout ) {
71+ //
72+ console . log ( '[RENDER]' )
6973 const { spatialGeometry = { x : 0 , y : 0 } } = parentLayout ;
7074 const { ctx} = apeContext ;
75+
7176 // If is relative and x and y haven't be processed, don't render
7277 if ( ! spatialGeometry ) return null ;
7378 // start drawing the canvas
@@ -78,11 +83,30 @@ function renderButton(props: ButtonProps, apeContext, parentLayout) {
7883 let y = spatialGeometry . y || 0 ;
7984 let width = x + y ;
8085 let height = ButtonDefaults . containerStyle . height ;
86+ let globalStyle = {
87+ width : width ,
88+ height :height ,
89+ color : color ,
90+ borderRadius :borderRadius ,
91+ backgroundColor,
92+ lineWidth :0 ,
93+ borderColor : 'transparent'
94+ }
95+ const resetStyle = ( newStyle ) => {
96+ globalStyle = { ...globalStyle , newStyle}
97+ console . log ( 'style)))))' , globalStyle )
98+ }
99+ const redrawButton = ( ctx ) => {
100+ // TODO reset Style on focus
101+ let newStyle = {
102+ lineWidth :2 ,
103+ borderColor :'#ccc'
104+ }
105+ //let prevStyle = globalStyle
106+ resetStyle ( newStyle )
81107
82- // ctx.addEventListener('click',()=>{
83- // alert('hi')
84- // })
85-
108+ }
109+
86110 ctx . beginPath ( ) ;
87111 ctx . fillStyle = backgroundColor ;
88112 ctx . moveTo ( x , y ) ;
@@ -113,58 +137,51 @@ function renderButton(props: ButtonProps, apeContext, parentLayout) {
113137 ctx . quadraticCurveTo ( x , y , x + borderRadius , y ) ;
114138
115139 ctx . fill ( ) ;
140+ ctx . lineWidth = globalStyle . lineWidth
141+ ctx . strokeStyle = globalStyle . borderColor
116142 ctx . stroke ( ) ;
117143 ctx . fillStyle = color || ButtonDefaults . textStyle . color ;
118144 ctx . font = `${ ButtonDefaults . textStyle . fontSize } Helvetica` ;
119145 //ctx.fillText('Start', x+ width/2 , y + height / 2);
120146 ctx . textAlign = 'center' ;
121147 ctx . fillText ( title , x + width / 2 , y + height / 2 ) ;
122- //ctx.fillText('Start', x + height /2, y + height /2 );
123148 ctx . closePath ( ) ;
124149
125- //const canvas = document.getElementById('root')
126- //const context = canvas.getContext('2d')
127- const onClick = ( event : SyntheticEvent < HTMLButtonElement > ) => {
128- const rect = {
150+ const onClick = ( event :SyntheticMouseEvent < HTMLButtonElement > ) => {
151+ const rect = {
129152 x,
130153 y,
131154 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')
155+ width
145156 }
146- } ;
147- function getMousePos ( canvas , event ) {
148- var rect = canvas . getBoundingClientRect ( ) ;
157+ const mousePosition = trackMousePosition ( ctx . canvas , event )
158+ if ( isInside ( mousePosition , rect ) ) {
159+ redrawButton ( ctx )
160+ if ( props . onClick && typeof props . onClick === "function" ) {
161+ props . onClick ( event )
162+ }
163+
164+ }
165+
166+ }
167+ function trackMousePosition ( canvas , event ) {
149168 return {
150- x : event . clientX - rect . left ,
151- y : event . clientY - rect . top ,
169+ x : event . clientX - canvas . offsetLeft ,
170+ y : event . clientY - canvas . offsetTop
152171 } ;
153172 }
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-
173+ const isInside = ( pos , rect ) => {
174+ return pos . x > rect . x && pos . x < rect . x + rect . width && pos . y < rect . y + rect . height && pos . y > rect . y
175+ }
176+
177+ ctx . canvas . addEventListener ( 'click' , onClick , false )
178+ ctx . canvas . addEventListener ( 'focus' , redrawButton )
179+ ctx . canvas . addEventListener ( 'blur' , redrawButton )
180+
167181 // events
182+
183+
184+
168185}
169186
170187export default function createButtonInstance ( props : ButtonProps ) : mixed {
0 commit comments