uiLitejhksdhkfj/widgets/button.cpp

106 lines
2.2 KiB
C++
Raw Normal View History

#include "core_include/api.h"
#include "core_include/rect.h"
#include "core_include/cmd_target.h"
#include "core_include/wnd.h"
2018-10-04 14:30:29 +08:00
#include "core_include/resource.h"
#include "core_include/bitmap.h"
#include "core_include/word.h"
#include "core_include/surface.h"
#include "core_include/theme.h"
#include "../widgets_include/button.h"
2017-12-06 21:43:47 +08:00
void c_button::pre_create_wnd()
{
2018-12-02 22:39:43 +08:00
m_style = GL_ATTR_VISIBLE | GL_ATTR_FOCUS | ALIGN_HCENTER | ALIGN_VCENTER;
m_font_type = c_theme::get_font(FONT_DEFAULT);
m_font_color = c_theme::get_color(COLOR_WND_FONT);
2017-12-06 21:43:47 +08:00
}
void c_button::on_focus()
{
2018-12-28 15:56:36 +08:00
m_status = STATUS_FOCUSED;
2017-12-06 21:43:47 +08:00
on_paint();
}
void c_button::on_kill_focus()
{
2018-12-28 15:56:36 +08:00
m_status = STATUS_NORMAL;
2017-12-06 21:43:47 +08:00
on_paint();
}
bool c_button::on_touch(int x, int y, TOUCH_ACTION action)
2017-12-06 21:43:47 +08:00
{
if (action == TOUCH_DOWN)
{
m_parent->set_child_focus(this);
m_status = STATUS_PUSHED;
on_paint();
}
else
2017-12-06 21:43:47 +08:00
{
2018-12-28 15:56:36 +08:00
m_status = STATUS_FOCUSED;
2017-12-06 21:43:47 +08:00
on_paint();
notify_parent(GL_BN_CLICKED, get_id(), 0);
}
2019-05-24 10:20:40 +08:00
return true;
}
2017-12-06 21:43:47 +08:00
bool c_button::on_key(KEY_TYPE key)
{
if (key == KEY_ENTER)
{
2018-12-02 22:39:43 +08:00
notify_parent(GL_BN_CLICKED, get_id(), 0);
return false;// Do not handle KEY_ENTER by other wnd.
2017-12-06 21:43:47 +08:00
}
return true;// Handle KEY_FOWARD/KEY_BACKWARD by parent wnd.
2017-12-06 21:43:47 +08:00
}
void c_button::on_paint()
{
c_rect rect;
get_screen_rect(rect);
switch(m_status)
{
2018-10-04 14:30:29 +08:00
case STATUS_NORMAL:
if (m_bitmap_normal)
2017-12-06 21:43:47 +08:00
{
2018-10-04 14:30:29 +08:00
c_bitmap::draw_bitmap_in_rect(m_surface, m_z_order, m_bitmap_normal, rect, m_style);
2017-12-06 21:43:47 +08:00
}
else
{
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_NORMAL), m_z_order);
2017-12-06 21:43:47 +08:00
}
break;
case STATUS_FOCUSED:
if (m_bitmap_focus)
{
c_bitmap::draw_bitmap_in_rect(m_surface, m_z_order, m_bitmap_focus, rect, m_style);
}
else
{
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_FOCUS), m_z_order);
2017-12-06 21:43:47 +08:00
}
break;
2018-10-04 14:30:29 +08:00
case STATUS_PUSHED:
if (m_bitmap_pushed)
2017-12-06 21:43:47 +08:00
{
2018-10-04 14:30:29 +08:00
c_bitmap::draw_bitmap_in_rect(m_surface, m_z_order, m_bitmap_pushed, rect, m_style);
2017-12-06 21:43:47 +08:00
}
else
{
m_surface->fill_rect(rect, c_theme::get_color(COLOR_WND_PUSHED), m_z_order);
m_surface->draw_rect(rect, c_theme::get_color(COLOR_WND_BORDER), 2, m_z_order);
2017-12-06 21:43:47 +08:00
}
break;
default:
ASSERT(FALSE);
break;
}
2018-10-04 14:30:29 +08:00
if (m_str)
2017-12-06 21:43:47 +08:00
{
2018-11-01 10:35:37 +08:00
c_word::draw_string_in_rect(m_surface, m_z_order, m_str, rect, m_font_type, m_font_color, GL_ARGB(0, 0, 0, 0), m_style);
2017-12-06 21:43:47 +08:00
}
}