buttonComponent.cpp
00001
00002
00003
00004
00005
00006
00007 #include "buttonComponent.h"
00008
00009
00010 ButtonComponent::ButtonComponent(const VXV::Rect& range)
00011 : draw_mode(Auto), pressed(false), judge_rect(range),
00012 normal_surface(NULL), focused_surface(NULL), clicked_surface(NULL),
00013 border_width(0), border_color(Gray8), focused(false) {
00014
00015 w = judge_rect.w;
00016 h = judge_rect.h;
00017 position = VXV::Grid(0, 0);
00018 }
00019
00020
00021 ButtonComponent::ButtonComponent(SurfaceInterface* normal)
00022 : draw_mode(Auto), pressed(false),
00023 judge_rect(VXV::Rect(normal->w, normal->h)),
00024 normal_surface(normal), focused_surface(NULL), clicked_surface(NULL),
00025 border_width(0), border_color(Gray8), focused(false) {
00026 w = judge_rect.w;
00027 h = judge_rect.h;
00028 position = VXV::Grid(0, 0);
00029 }
00030
00031
00032 ButtonComponent::~ButtonComponent(void) {
00033 }
00034
00035
00036 bool ButtonComponent::draw(unsigned long ticks, const UserInput& ui) {
00037
00038 SurfaceInterface* surface = normal_surface;
00039
00040 focused = false;
00041 int mx = ui.mpos.x;
00042 int my = ui.mpos.y;
00043 if ((mx >= position.x + judge_rect.x) &&
00044 (mx <= position.x + judge_rect.x + judge_rect.w) &&
00045 (my >= position.y + judge_rect.y) &&
00046 (my <= position.y + judge_rect.y + judge_rect.h)) {
00047 focused = true;
00048
00049 if (ui.left_release) {
00050 pressed = true;
00051 if (clicked_surface) {
00052
00053 surface = clicked_surface;
00054 }
00055 } else {
00056 if (focused_surface) {
00057 surface = focused_surface;
00058 }
00059 }
00060 }
00061
00062
00063 if (draw_mode != Auto) {
00064 switch (draw_mode) {
00065 case Normal:
00066 surface = normal_surface;
00067 break;
00068 case Focused:
00069 if (focused_surface) {
00070 surface = focused_surface;
00071 }
00072 break;
00073 case Clicked:
00074 if (clicked_surface) {
00075 surface = clicked_surface;
00076 }
00077 break;
00078 }
00079 }
00080
00081
00082 if (!surface) {
00083 return false;
00084 } else {
00085 surface->draw(VXV::Rect(surface->w, surface->h), position, ticks);
00086
00087 return true;
00088 }
00089 }
00090
00091
00092 void ButtonComponent::setDrawSurface(int mode) {
00093 draw_mode = mode;
00094 }
00095
00096
00097 void ButtonComponent::setNormalSurface(SurfaceInterface* normal) {
00098 normal_surface = normal;
00099 }
00100
00101
00102 void ButtonComponent::setFocusedSurface(SurfaceInterface* focused) {
00103 focused_surface = focused;
00104 }
00105
00106
00107 void ButtonComponent::setClickedSurface(SurfaceInterface* clicked) {
00108 clicked_surface = clicked;
00109 }
00110
00111
00112 #if 0
00113
00114 ButtonComponent& ButtonComponent::setBorder(int width, unsigned long color) {
00115 border_width = width;
00116 border_color = color;
00117 return *this;
00118 }
00119 #endif
00120
00121
00122 bool ButtonComponent::isFocused(void) {
00123 return focused;
00124 }
00125
00126
00127 bool ButtonComponent::isPressed(void) {
00128 bool ret = pressed;
00129 pressed = false;
00130
00131 return ret;
00132 }
00133