Skip to content

Commit

Permalink
Merge branch 'feature/menu-cleanup'
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 10, 2014
2 parents 3370e84 + 3cd485d commit fb7ec3e
Show file tree
Hide file tree
Showing 40 changed files with 776 additions and 444 deletions.
8 changes: 4 additions & 4 deletions src/control/joystick_manager.cpp
Expand Up @@ -19,10 +19,10 @@
#include <iostream>
#include <algorithm>

#include "gui/menu_manager.hpp"
#include "control/input_manager.hpp"
#include "lisp/list_iterator.hpp"
#include "supertux/menu/joystick_menu.hpp"
#include "supertux/menu/menu_storage.hpp"
#include "util/gettext.hpp"
#include "util/log.hpp"
#include "util/writer.hpp"
Expand Down Expand Up @@ -143,7 +143,7 @@ JoystickManager::process_hat_event(const SDL_JoyHatEvent& jhat)
if (changed & SDL_HAT_RIGHT && jhat.value & SDL_HAT_RIGHT)
bind_joyhat(jhat.which, SDL_HAT_RIGHT, Controller::Control(wait_for_joystick));

MenuStorage::instance().get_joystick_options_menu()->update();
MenuManager::instance().refresh();
wait_for_joystick = -1;
}
else
Expand Down Expand Up @@ -191,7 +191,7 @@ JoystickManager::process_axis_event(const SDL_JoyAxisEvent& jaxis)
else
bind_joyaxis(jaxis.which, jaxis.axis + 1, Controller::Control(wait_for_joystick));

MenuStorage::instance().get_joystick_options_menu()->update();
MenuManager::instance().refresh();
wait_for_joystick = -1;
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ JoystickManager::process_button_event(const SDL_JoyButtonEvent& jbutton)
if(jbutton.state == SDL_PRESSED)
{
bind_joybutton(jbutton.which, jbutton.button, (Controller::Control)wait_for_joystick);
MenuStorage::instance().get_joystick_options_menu()->update();
MenuManager::instance().refresh();
parent->reset();
wait_for_joystick = -1;
}
Expand Down
6 changes: 3 additions & 3 deletions src/control/keyboard_manager.cpp
Expand Up @@ -76,7 +76,7 @@ KeyboardManager::process_key_event(const SDL_KeyboardEvent& event)
// if console is open: send key there
process_console_key_event(event);
}
else if (MenuManager::instance().current())
else if (MenuManager::instance().is_active())
{
// if menu mode: send key there
process_menu_key_event(event);
Expand Down Expand Up @@ -168,7 +168,7 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
bind_key(event.keysym.sym, static_cast<Controller::Control>(wait_for_key));
}
m_parent->reset();
MenuStorage::instance().get_key_options_menu()->update();
MenuManager::instance().refresh();
wait_for_key = -1;
return;
}
Expand All @@ -178,7 +178,7 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
if (event.keysym.sym == SDLK_ESCAPE)
{
m_parent->reset();
MenuStorage::instance().get_joystick_options_menu()->update();
MenuManager::instance().refresh();
m_parent->joystick_manager->wait_for_joystick = -1;
}
return;
Expand Down
109 changes: 21 additions & 88 deletions src/gui/menu.cpp
Expand Up @@ -42,15 +42,10 @@ Menu::Menu() :
delete_character(),
mn_input_char(),
menu_repeat_time(),
close(false),
items(),
effect_progress(),
effect_start_time(),
arrange_left(),
active_item()
{
MenuManager::instance().m_all_menus.push_back(this);

hit_item = -1;
menuaction = MENU_ACTION_NONE;
delete_character = 0;
Expand All @@ -60,27 +55,17 @@ Menu::Menu() :
pos.y = SCREEN_HEIGHT/2;
arrange_left = 0;
active_item = -1;

effect_progress = 0.0f;
effect_start_time = 0.0f;
}

Menu::~Menu()
{
MenuManager::instance().m_all_menus.remove(this);

if (MenuManager::instance().current() == this)
MenuManager::instance().m_current = nullptr;

if (MenuManager::instance().get_previous() == this)
MenuManager::instance().m_previous = nullptr;
}

void
Menu::set_pos(float x, float y, float rw, float rh)
Menu::set_center_pos(float x, float y)
{
pos.x = x + get_width() * rw;
pos.y = y + get_height() * rh;
pos.x = x;
pos.y = y;
}

/* Add an item to a menu */
Expand Down Expand Up @@ -172,9 +157,9 @@ Menu::add_back(const std::string& text)
}

MenuItem*
Menu::add_submenu(const std::string& text, Menu* submenu, int id)
Menu::add_submenu(const std::string& text, int submenu)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_GOTO, id));
std::unique_ptr<MenuItem> item(new MenuItem(MN_GOTO));
item->text = text;
item->target_menu = submenu;
return add_item(std::move(item));
Expand All @@ -189,7 +174,7 @@ Menu::clear()

/* Process actions done on the menu */
void
Menu::update()
Menu::process_input()
{
int menu_height = (int) get_height();
if (menu_height > SCREEN_HEIGHT)
Expand All @@ -198,20 +183,6 @@ Menu::update()
pos.y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
}

effect_progress = (real_time - effect_start_time) * 6.0f;

if(effect_progress >= 1.0f) {
effect_progress = 1.0f;

if (close) {
MenuManager::instance().m_current = 0;
close = false;
}
}
else if (effect_progress <= 0.0f) {
effect_progress = 0.0f;
}

Controller* controller = g_input_manager->get_controller();
/** check main input controller... */
if(controller->pressed(Controller::UP)) {
Expand Down Expand Up @@ -322,7 +293,7 @@ Menu::update()
switch (items[active_item]->kind) {
case MN_GOTO:
assert(items[active_item]->target_menu != 0);
MenuManager::instance().push_current(items[active_item]->target_menu);
MenuManager::instance().push_menu(items[active_item]->target_menu);
break;

case MN_TOGGLE:
Expand Down Expand Up @@ -350,11 +321,11 @@ Menu::update()
case MN_TEXTFIELD:
case MN_NUMFIELD:
menuaction = MENU_ACTION_DOWN;
update();
process_input();
break;

case MN_BACK:
MenuManager::instance().pop_current();
MenuManager::instance().pop_menu();
break;
default:
break;
Expand Down Expand Up @@ -389,7 +360,7 @@ Menu::update()
break;

case MENU_ACTION_BACK:
MenuManager::instance().pop_current();
MenuManager::instance().pop_menu();
break;

case MENU_ACTION_NONE:
Expand Down Expand Up @@ -615,50 +586,16 @@ Menu::get_height() const
return items.size() * 24;
}

/* Draw the current menu. */
void
Menu::draw(DrawingContext& context)
Menu::on_window_resize()
{
if(MouseCursor::current()) {
MouseCursor::current()->draw(context);
}

float menu_width = get_width();
float menu_height = get_height();

if (effect_progress != 1.0f)
{
if (close)
{
menu_width = (MenuManager::instance().current()->get_width() * (1.0f - effect_progress));
menu_height = (MenuManager::instance().current()->get_height() * (1.0f - effect_progress));
}
else if (MenuManager::instance().get_previous())
{
menu_width = (menu_width * effect_progress) + (MenuManager::instance().get_previous()->get_width() * (1.0f - effect_progress));
menu_height = (menu_height * effect_progress) + (MenuManager::instance().get_previous()->get_height() * (1.0f - effect_progress));
//std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
}
else
{
menu_width *= effect_progress;
menu_height *= effect_progress;
}
}

/* Draw a transparent background */
context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2-4, pos.y - menu_height/2 - 10-4),
Vector(pos.x + menu_width/2+4, pos.y - menu_height/2 + 10 + menu_height+4)),
Color(0.2f, 0.3f, 0.4f, 0.8f),
20.0f,
LAYER_GUI-10);

context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2, pos.y - menu_height/2 - 10),
Vector(pos.x + menu_width/2, pos.y - menu_height/2 + 10 + menu_height)),
Color(0.6f, 0.7f, 0.8f, 0.5f),
16.0f,
LAYER_GUI-10);
pos.x = SCREEN_WIDTH / 2;
pos.y = SCREEN_HEIGHT / 2;
}

void
Menu::draw(DrawingContext& context)
{
if (!items[active_item]->help.empty())
{
int text_width = (int) Resources::normal_font->get_text_width(items[active_item]->help);
Expand All @@ -685,11 +622,10 @@ Menu::draw(DrawingContext& context)
ALIGN_CENTER, LAYER_GUI);
}

if (effect_progress == 1.0f)
for(unsigned int i = 0; i < items.size(); ++i)
{
draw_item(context, i);
}
for(unsigned int i = 0; i < items.size(); ++i)
{
draw_item(context, i);
}
}

MenuItem&
Expand Down Expand Up @@ -741,9 +677,6 @@ Menu::set_toggled(int id, bool toggled)
void
Menu::event(const SDL_Event& event)
{
if(effect_progress != 1.0f)
return;

switch(event.type) {
case SDL_MOUSEBUTTONDOWN:
if(event.button.button == SDL_BUTTON_LEFT)
Expand Down
23 changes: 12 additions & 11 deletions src/gui/menu.hpp
Expand Up @@ -62,14 +62,17 @@ class Menu
MenuItem* add_toggle(int id, const std::string& text, bool toggled = false);
MenuItem* add_inactive(int id, const std::string& text);
MenuItem* add_back(const std::string& text);
MenuItem* add_submenu(const std::string& text, Menu* submenu, int id = -1);
MenuItem* add_submenu(const std::string& text, int submenu);
MenuItem* add_controlfield(int id, const std::string& text,
const std::string& mapping = "");
MenuItem* add_string_select(int id, const std::string& text);

virtual void menu_action(MenuItem* item);

void update();
void process_input();

/** Perform actions to bring the menu up to date with configuration changes */
virtual void refresh() {}

/** Remove all entries from the menu */
void clear();
Expand All @@ -88,21 +91,25 @@ class Menu
void set_active_item(int id);

void draw(DrawingContext& context);
void set_pos(float x, float y, float rw = 0, float rh = 0);
Vector get_center_pos() const { return pos; }
void set_center_pos(float x, float y);

void event(const SDL_Event& event);

bool is_toggled(int id) const;
void set_toggled(int id, bool toggled);

float get_width() const;
float get_height() const;

virtual void on_window_resize();

protected:
/** Return the index of the menu item that was 'hit' (ie. the user
clicked on it) in the last event() call */
int check ();

MenuItem* add_item(std::unique_ptr<MenuItem> menu_item);
float get_width() const;
float get_height() const;

private:
void check_controlfield_change_event(const SDL_Event& event);
Expand All @@ -125,14 +132,8 @@ class Menu
float menu_repeat_time;

public:
bool close;

std::vector<std::unique_ptr<MenuItem> > items;

public:
float effect_progress;
float effect_start_time;

private:
int arrange_left;
int active_item;
Expand Down
15 changes: 7 additions & 8 deletions src/gui/menu_item.cpp
Expand Up @@ -16,28 +16,27 @@

#include "gui/menu_item.hpp"

#include <stdio.h>

#include "supertux/menu/menu_storage.hpp"
#include "supertux/resources.hpp"
#include "supertux/timer.hpp"
#include "video/font.hpp"
#include <stdio.h>

static const float FLICK_CURSOR_TIME = 0.5f;
static const float FLICK_CURSOR_TIME = 0.5f;

MenuItem::MenuItem(MenuItemKind _kind, int _id) :
kind(_kind),
id(_id),
toggled(),
toggled(false),
text(),
input(),
help(),
list(),
selected(),
target_menu(),
selected(false),
target_menu(MenuStorage::NO_MENU),
input_flickering()
{
toggled = false;
selected = false;
target_menu = 0;
}

void
Expand Down
4 changes: 2 additions & 2 deletions src/gui/menu_item.hpp
Expand Up @@ -42,7 +42,7 @@ class MenuItem
{
public:
static MenuItem* create(MenuItemKind kind, const std::string& text,
int init_toggle, Menu* target_menu, int id, int key);
int init_toggle, int target_menu, int id, int key);

public:
MenuItem(MenuItemKind kind, int id = -1);
Expand All @@ -65,7 +65,7 @@ class MenuItem
std::vector<std::string> list; // list of values for a STRINGSELECT item
size_t selected; // currently selected item

Menu* target_menu;
int target_menu;

private:
/// keyboard key or joystick button
Expand Down

0 comments on commit fb7ec3e

Please sign in to comment.