当前位置: 首页 > news >正文

TouchGFX之Button

TouchGFX中的按钮是一种感应触控事件的控件,能够在按钮被按下/释放时发送回调

代码

#ifndef TOUCHGFX_ABSTRACTBUTTON_HPP
#define TOUCHGFX_ABSTRACTBUTTON_HPP
#include <touchgfx/Callback.hpp>
#include <touchgfx/events/ClickEvent.hpp>
#include <touchgfx/widgets/Widget.hpp>namespace touchgfx
{
/* 按键抽象接口类 */
class AbstractButton : public Widget
{
public:/* 构造函数 */AbstractButton() : Widget(), action(), pressed(false){setTouchable(true);	//打开触摸功能}/* 点击事件处理函数 */virtual void handleClickEvent(const ClickEvent& event);/* 设置点击事件回调函数 */void setAction(GenericCallback<const AbstractButton&>& callback){action = &callback;}/* 执行点击事件回调函数 */virtual void executeAction(){if (action && action->isValid()){action->execute(*this);}}/* 获取按钮状态 */virtual bool getPressedState() const{return pressed;}protected:GenericCallback<const AbstractButton&>* action; //点击事件回调函数bool pressed; //按键状态(按下/松开)
};}#endif
#include <touchgfx/widgets/AbstractButton.hpp>namespace touchgfx
{
/* 点击事件回调函数 */
void AbstractButton::handleClickEvent(const ClickEvent& event)
{const bool wasPressed = pressed;pressed = (event.getType() == ClickEvent::PRESSED);/* 按钮状态改变,重绘 */if ((pressed && !wasPressed) || (!pressed && wasPressed)){invalidate();}/* 按下触发回调 */if (wasPressed && (event.getType() == ClickEvent::RELEASED)){executeAction();}
}
}

自定义按键

#ifndef BUTTONWITHCOLOR
#define BUTTONWITHCOLOR
#include <touchgfx/widgets/AbstractButton.hpp>using namespace touchgfx;class ButtonWithColor : public AbstractButton
{
public:ButtonWithColor(): AbstractButton(), upColor(0xFFFFFF), downColor(0xFFFFFF), alpha(255){}	virtual void draw(const Rect& invalidatedArea) const;virtual void setColor(const colortype colorReleased, const colortype colorPressed){upColor = colorReleased;downColor = colorPressed;}virtual Rect getSolidRect() const;void setAlpha(uint8_t newAlpha){alpha = newAlpha;}uint8_t getAlpha() const{return alpha;}colortype getCurrentlyDisplayedColor() const{return (pressed ? downColor : upColor);}virtual void invalidateContent() const{if (alpha > 0){Widget::invalidateContent();}}private:colortype upColor;colortype downColor;uint8_t alpha;
};#endif
#include <touchgfx/Drawable.hpp>
#include <touchgfx/hal/HAL.hpp>
#include <touchgfx/lcd/LCD.hpp>
#include <gui/common/ButtonWithColor.hpp>void ButtonWithColor::draw(const Rect& invalidatedArea) const
{const colortype color = pressed ? downColor : upColor;Rect dirty = invalidatedArea;translateRectToAbsolute(dirty);HAL::lcd().fillRect(dirty, color, alpha);
}Rect ButtonWithColor::getSolidRect() const
{Rect solidRect;if(alpha == 255){solidRect.width = rect.width;solidRect.height = rect.height;}return solidRect;
}
#ifndef SCREENVIEW_HPP
#define SCREENVIEW_HPP#include <gui_generated/screen_screen/screenViewBase.hpp>
#include <gui/screen_screen/screenPresenter.hpp>
#include <gui/common/ButtonWithColor.hpp>class screenView : public screenViewBase
{
public:screenView();virtual ~screenView() {}virtual void setupScreen();virtual void tearDownScreen();
protected:private:ButtonWithColor button;
};#endif // SCREENVIEW_HPP
#include <gui/screen_screen/screenView.hpp>
#include <touchgfx/Color.hpp>screenView::screenView()
{}void screenView::setupScreen()
{screenViewBase::setupScreen();button.setColor(Color::getColorFromRGB(0x00, 0xFF, 0x00), Color::getColorFromRGB(0x00, 0xFF, 0xFF));button.setPosition(100,100,100,100);add(button);
}void screenView::tearDownScreen()
{screenViewBase::tearDownScreen();
}

运行模拟器

左边松开 -> 右边按下

http://www.lryc.cn/news/327300.html

相关文章:

  • 计算机组成原理 — 指令系统
  • 使用easyYapi生成文档
  • 蓝桥杯练习题总结(三)线性dp题(摆花、数字三角形加强版)
  • Elasticsearch(15) multi_match的使用
  • nodejs的线程模型和libuv库的基本使用
  • Uni-app/Vue/Js本地模糊查询,匹配所有字段includes和some方法结合使用e
  • 深度学习pytorch——激活函数损失函数(持续更新)
  • 《苹果 iOS 应用开发与分发的关键问题解析》
  • 爱上数据结构:顺序表和链表
  • python知识点总结(十)
  • 【Python】探索 Python 编程世界:常量、变量及数据类型解析
  • vue页面实现左右div宽度,上下div高度分割线手动拖动高度或者宽度自动变化,两个div宽度或者高度拉伸调节,实现左右可拖动改变宽度的div内容显示区
  • 知攻善防应急靶场-Linux(1)
  • ffmpeg命令行
  • VMware虚拟机更换引导顺序
  • RAFT:让大型语言模型更擅长特定领域的 RAG 任务
  • Stable Diffusion 本地训练端口与云端训练端口冲突解决办法
  • C++学习day1
  • openGauss CM
  • 北斗短报文+4G应急广播系统:实时监控 自动预警 保护校园安全的新力量
  • 2024河北石家庄矿业矿山展览会|河北智慧矿山展会|河北矿博会
  • ruoyi使用笔记
  • 论文《Exploring to Prompt for Vision-Language Models》阅读
  • 科普 | Runes 预挖矿概念
  • 蓝桥杯真题Day40 倒计时19天 纯练题!
  • Android 14.0 SystemUI下拉状态栏增加响铃功能
  • docker学习笔记 二-----docker介绍
  • 螺旋矩阵的算法刷题
  • 蓝桥杯算法赛(二进制王国)
  • 7.JDK下载和安装