В Livecode я пытаюсь заставить кнопку работать с функцией «подключиться»/«отключить». Как я могу заставить кнопку узнать, была ли она уже нажата ранее, выполняя код A, а затем, при повторном нажатии, должен выполнять код B? Кнопку нужно нажимать несколько раз между "подключено" и "отключено"
Livecode: две разные функции кнопок в зависимости от того, были ли они нажаты до этого
Ответы (3)
Если вы используете стандартную кнопку и у вас есть только 2 состояния (отключено/подключено), простой способ — отключить свойство autoHilite кнопки и вручную установить hilite кнопки в ее скрипте:
on mouseUp
set the hilite of me to not the hilite of me
if the hilite of me then
-- do connecting stuff here
else
-- do disconnecting stuff here
end if
end mouseUp
Вы не объясняете, почему кнопку нужно нажимать несколько раз, но если вам нужно более двух состояний подключения, вы можете использовать настраиваемое свойство для сохранения текущего состояния. Например, вы можете использовать значения «пусто» (отключено), «рукопожатие» (запуск процесса), «подключение» (в процессе) и «связано» (подключено) для каждого состояния. Что-то типа:
# STORE CURRENT CONNECTING STATE IN connectionState
on mouseUp
switch the connectionState of me
case empty
-- start connection process, show HANDSHAKING feedback here
hilite me
set the connectionState of me to "handshaking"
break
case "handshaking"
-- if initial handshake successful, begin connecting to system
if handShakeSuccessful is true then
-- start connection process, show CONNECTING feedback here
set the connectionState of me to "connecting"
end if
break
case "connecting"
-- if initial connection is successful, show LINKED feedback here
if connectionSuccessful is true then
set the connectionState of me to "linked"
end if
break
case "linked"
-- do disconnecting stuff here
set the connectionState of me to empty
unhilite me
end switch
end mouseUp
В стеке ChatRev мы используем метку кнопки, чтобы указать ее статус.
if the label of me is "Connect" then
set the label of me to "Disconnect"
open socket gSocket
write "helo" & cr to socket gSocket
read from socket gSocket with message "connected"
else // "Disconnect"
set the label of me to "Connect"
write "disconnect" to socket gSocket
repeat for each line myLine in the openSockets
close socket myLine
end repeat
end if
Если метка «Подключено», соединение должно быть закрыто, а если метка «Подключено», соединение должно быть установлено.
Я обычно использую обработчики setProp в подобных ситуациях. Кнопка используется только как «тумблер».
# the button script
on mouseUp
set the uConnectedState of this cd to not the uConnectedState of this cd
end mouseUp
# in the card script (or wherever appropriate)
setProp uConnectedState pIsConnected
if pIsConnected then
set the label of btn "connect" to "Disconnect"
# any other interface updates or logic here
else
set the label of btn "connect" to "Connect"
# any other interface updates or logic here
end if
pass uConnectedState
end uConnectedState
Выполнение этого таким образом означает, что все, что мне нужно сделать, это установить пользовательское свойство, и все дела обрабатываются обработчиком setProp. Например, если мне нужно инициализировать соединение на openCard, все, что мне нужно сделать, это установить для uConnectedState карты значение false/true.