00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iostream>
00021 #include "graphic_context.hxx"
00022 #include "controller.hxx"
00023 #include "colors.hxx"
00024 #include "world_gui_manager.hxx"
00025 #include "gui_buttons.hxx"
00026 #include "screen_manager.hxx"
00027 #include "worldview_component.hxx"
00028
00029 #define BUTTON_POS(n) (75 + n * 30)
00030 #define BUTTON_WIDTH 75
00031 #define BUTTON_HEIGHT 25
00032
00033 GUIButton::GUIButton (const std::string& title_,
00034 int x_pos_, int y_pos_, int width_, int height_)
00035 : GUIComponent (x_pos_, y_pos_, width_, height_),
00036 title (title_)
00037 {
00038 mouse_over = false;
00039 pressed = false;
00040 }
00041
00042 void
00043 GUIButton::on_mouse_enter ()
00044 {
00045 mouse_over = true;
00046 }
00047
00048 void
00049 GUIButton::on_mouse_leave ()
00050 {
00051 mouse_over = false;
00052 }
00053
00054 void
00055 GUIButton::on_primary_button_press (int x, int y)
00056 {
00057 WorldGUIManager::instance()->grab_mouse (this);
00058 pressed = true;
00059 }
00060
00061 void
00062 GUIButton::on_primary_button_release (int x, int y)
00063 {
00064 WorldGUIManager::instance()->ungrab_mouse (this);
00065 if (is_at (x, y))
00066 on_click ();
00067 pressed = false;
00068 }
00069
00070 void
00071 GUIButton::draw (GraphicContext* gc)
00072 {
00073 if (pressed && mouse_over)
00074 {
00075 gc->draw_fill_rect (x_pos, y_pos, x_pos + width, y_pos + height, Colors::button_bg_pressed);
00076 }
00077 else if (mouse_over)
00078 {
00079 gc->draw_fill_rect (x_pos, y_pos, x_pos + width, y_pos + height, Colors::button_bg_hover);
00080 }
00081 else
00082 {
00083 gc->draw_fill_rect (x_pos, y_pos, x_pos + width, y_pos + height, Colors::button_bg_passive);
00084 }
00085
00086 draw_content (gc);
00087
00088 if (pressed && mouse_over)
00089 {
00090 draw_border_pressed (gc);
00091 }
00092 else if (mouse_over)
00093 {
00094 draw_border_hover (gc);
00095 }
00096 else
00097 {
00098 draw_border_normal (gc);
00099 }
00100 }
00101
00102 void
00103 GUIButton::draw_content (GraphicContext* gc)
00104 {
00105 gc->draw_string_centered (x_pos + width/2, y_pos + 16, title);
00106 }
00107
00108 void
00109 GUIButton::draw_border_hover(GraphicContext* gc)
00110 {
00111 gc->draw_rect (x_pos, y_pos,
00112 x_pos + width, y_pos + height, Colors::button_fg_hover);
00113 }
00114
00115 void
00116 GUIButton::draw_border_pressed(GraphicContext* gc)
00117 {
00118 gc->draw_rect (x_pos, y_pos,
00119 x_pos + width, y_pos + height, Colors::button_fg_pressed);
00120 }
00121
00122 void
00123 GUIButton::draw_border_normal(GraphicContext* gc)
00124 {
00125 gc->draw_rect (x_pos, y_pos,
00126 x_pos + width, y_pos + height, Colors::button_fg_passive);
00127 }
00128
00129 GUIRunButton::GUIRunButton ()
00130 : GUIButton ("Run", 10, BUTTON_POS(0), BUTTON_WIDTH, BUTTON_HEIGHT)
00131 {
00132 }
00133
00134 void
00135 GUIButton::on_click()
00136 {
00137 std::cout << "GUIButton: cliked (implement me)" << std::endl;
00138 }
00139
00140 void
00141 GUIRunButton::draw_content (GraphicContext* gc)
00142 {
00143 if ((!pressed || !mouse_over) && Controller::instance()->is_running ())
00144 gc->draw_fill_rect (x_pos, y_pos,
00145 x_pos + width, y_pos + height, Colors::button_bg_active) ;
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 GUIButton::draw_content (gc);
00156 }
00157
00158 void
00159 GUIRunButton::on_click()
00160 {
00161 std::cout << "Button pressed" << std::endl;
00162 Controller::instance()->start_simulation ();
00163 }
00164
00165 GUISlowMoButton::GUISlowMoButton ()
00166 : GUIButton ("SlowMotion", 10, BUTTON_POS(1), BUTTON_WIDTH, BUTTON_HEIGHT)
00167 {
00168
00169 }
00170
00171 void
00172 GUISlowMoButton::on_click()
00173 {
00174 Controller::instance()->set_slow_down (!Controller::instance()->slow_down_active ());
00175 }
00176
00177 void
00178 GUISlowMoButton::draw_content (GraphicContext* gc)
00179 {
00180 if (Controller::instance()->slow_down_active())
00181 gc->draw_fill_rect (x_pos, y_pos,
00182 x_pos + width, y_pos + height, Colors::button_bg_active);
00183
00184 GUIButton::draw_content (gc);
00185 }
00186
00187 GUIZoomInButton::GUIZoomInButton ()
00188 : GUIButton ("Zoom In", 10, BUTTON_POS(2), BUTTON_WIDTH, BUTTON_HEIGHT)
00189 {
00190 }
00191
00192 void
00193 GUIZoomInButton::on_click()
00194 {
00195 WorldViewComponent::instance()->wheel_up (400,300);
00196 }
00197
00198
00199 GUIZoomOutButton::GUIZoomOutButton ()
00200 : GUIButton ("Zoom Out", 10, BUTTON_POS(3), BUTTON_WIDTH, BUTTON_HEIGHT)
00201 {
00202 }
00203
00204 void
00205 GUIZoomOutButton::on_click()
00206 {
00207 WorldViewComponent::instance()->wheel_down (400,300);
00208 }
00209
00210 GUIQuitButton::GUIQuitButton ()
00211 : GUIButton ("Quit", 10, BUTTON_POS(11), BUTTON_WIDTH, BUTTON_HEIGHT)
00212 {
00213 }
00214
00215 void
00216 GUIQuitButton::on_click()
00217 {
00218 ScreenManager::instance()->quit();
00219 }
00220
00221
00222 GUILoadButton::GUILoadButton ()
00223 : GUIButton ("Load", 10, BUTTON_POS(8), BUTTON_WIDTH, BUTTON_HEIGHT)
00224 {
00225 }
00226
00227 void
00228 GUILoadButton::on_click()
00229 {
00230 ScreenManager::instance()->set_gui(ScreenManager::LOAD_GUI);
00231 }
00232
00233