Skip to content

Commit

Permalink
dropdown menus and better formatting
Browse files Browse the repository at this point in the history
engine changes:
-implemented dropdown menus
-menu titles can now be alligned top, center, or bottom
-changed how key toggles work
-removed object::name
-menus now have borders
-button labels can be alligned left,middle, or right
demo changes:
-relabeled the "new object" buttons and added them to a dropdown menu
-changed the "return to menu" warning
-changed the object spawn points
-pause screen may now be toggled
-updated readme
  • Loading branch information
confuzedskull committed Oct 30, 2014
1 parent 68f6d19 commit 71f59d4
Show file tree
Hide file tree
Showing 31 changed files with 663 additions and 301 deletions.
6 changes: 3 additions & 3 deletions include/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class button : public clickable_object
{
protected:
text_object label;
std::string label_allignment;
public:
bool border;//whether or not a border should be shown
color border_color;//the color of the border
int margin;//space between text and button border
bool performed_action;//whether or not the button action executed
void (*action)();//a function pointer which will be called when clicked
void format();//adjusts the size of the button to fit the text
void set_label(std::string l);
void set_label(std::string);
void allign_label(std::string);
void mouse_function();
void render();
void update();
Expand Down
45 changes: 24 additions & 21 deletions include/controls.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,34 @@

#ifndef CONTROLS_H
#define CONTROLS_H
#include <map>

//stores all variables and functions related to peripheral input
namespace controls
{
extern bool* toggles;
extern bool* toggle_states;//stores the toggle state before it is set
extern bool* key_states; //stores each on/off state of a keyboard key
void move_forward();//moves the currently selected object forward
void move_back();//moves the currently selected object back
void move_left();//moves the currently selected object left
void move_right();//moves the currently selected object right
void turn_left();//turns the currently selected object left
void turn_right();//turns the currently selected object right
void next_item();//selects the next item
void previous_item();//selects the previous item
void choose_item();
void check_clicked();//check if objects are clicked or not
void mouse_click(int button, int state, int x, int y);//handles mouse clicks
void mouse_move(int x, int y);//handles mouse movement
void mouse_drag(int x, int y);//handles mouse drag
void key_pressed(unsigned char key, int x, int y);//marks given key as pressed
void key_released(unsigned char key, int x, int y);//marks given key as released
void key_operations(void);//handles keyboard actions
void special_input(int,int,int);
//variables
extern bool* toggles;//stores the toggle states
extern bool* toggle_states;//stores the toggle state before it is set
extern bool* key_states; //stores each on/off state of a keyboard key
//gameplay functions
void move_forward();//moves the currently selected object forward
void move_back();//moves the currently selected object back
void move_left();//moves the currently selected object left
void move_right();//moves the currently selected object right
void turn_left();//turns the currently selected object left
void turn_right();//turns the currently selected object right
void next_item();//selects the next item
void previous_item();//selects the previous item
void choose_item();//selects the current item
//mouse functions
void check_clicked();//check if objects are clicked or not
void mouse_click(int button, int state, int x, int y);//handles mouse clicks
void mouse_move(int x, int y);//handles mouse movement
void mouse_drag(int x, int y);//handles mouse drag
//keyboard functions
void key_pressed(unsigned char key, int x, int y);//marks given key as pressed
void key_released(unsigned char key, int x, int y);//marks given key as released
void key_operations(void);//handles keyboard actions
void special_keys(int,int,int);//handles special keys
};

#endif // CONTROLS_H
38 changes: 38 additions & 0 deletions include/dropdown_menu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This file is a part of 2DWorld - The Generic 2D Game Engine
Copyright (C) 2014 James Nakano
2DWorld is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2DWorld is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the rest of 2DWorld. If not, see <http://www.gnu.org/licenses/>.*/

#ifndef DROPDOWN_MENU_H
#define DROPDOWN_MENU_H
#include "menu.h"
#include "button.h"
#include "clickable_object.h"
#include <string>

class dropdown_menu: public menu, public clickable_object
{
private:
bool expanded;
bool state_toggle;
public:
void add_item(button*);
void mouse_function();
void expand();
void collapse();
void update();
dropdown_menu();
};

#endif // DROPDOWN_MENU_H
11 changes: 4 additions & 7 deletions include/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,25 @@

#ifndef GAME_H
#define GAME_H
#include "physics_object.h"
#include "draggable_object.h"
#include "rts_object.h"
#include "projectile.h"
#include "button.h"
#include "scene.h"
#include <time.h>
#include <vector>
#include <map>

//stores important functions and variables related to the game
namespace game
{
extern float time;//stores the current game time in seconds
extern clock_t time_started;//start time
extern double time_elapsed;//time since start
extern bool paused;
extern bool paused;//whether the game is active or not
extern scene* current_scene;
extern std::vector<scene*> scenes;
void initialize();//initialize the objects
void collision_detection();//handles object collision
void add_draggable_object();//add a draggable object to the current scene
void add_physics_object();//add a physics object to the current scene
void add_rts_object();//add an rts object to the current scene
void create_object();//creates an object of the previously created type
void delete_selected();//remove the selected object from the game
void play();//open game screen
void pause();//open pause screen
Expand All @@ -48,5 +43,7 @@ namespace game
void warn_return();//warn user before returning to main menu
void return_menu();//open main menu
void quit();//close the program
void update();//update variables
void sync();//update clock-based events
};
#endif // GAME_H
5 changes: 4 additions & 1 deletion include/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@
#include <vector>
#include <string>

class menu: public object
class menu: virtual public object
{
protected:
text_object title;
text_object subtitle;
std::string layout;
std::string title_allignment;
public:
std::vector<button*> items;
int spacing;//space between buttons
int margin;
button* current_item;
bool item_clicked();
void set_title(std::string t);
void set_subtitle(std::string s);
void set_layout(std::string l);
void allign_title(std::string a);
void add_item(button* b);
void format();
void render();
Expand Down
5 changes: 3 additions & 2 deletions include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class object
public:
static int total_objects;
static point2f origin;
std::string name;
int get_number();
std::string get_type();
point2f get_position();
Expand All @@ -47,8 +46,10 @@ class object
float get_width();
float get_height();
float get_radius();
color primary_color;//
color primary_color;
color marker_color;//color of marker when object is selected
color border_color;//the color of the border
bool border;//whether or not a border should be shown
bool visible;//whether the object should be shown or not
bool selected;//whether the object has been selected or not
void rotate(float angle);//changes the object's rotation by the given angle
Expand Down
32 changes: 23 additions & 9 deletions include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,60 @@
#include "draggable_object.h"
#include "physics_object.h"
#include "rts_object.h"
#include "text_object.h"
#include "button.h"
#include "menu.h"
#include "text_object.h"
#include "dropdown_menu.h"
#include <map>
#include <vector>

class scene
{
public:
public:
color background_color;
menu* current_menu;
//maps are used because we need to access the objects by referencing a common identifier (object.number)
//object pointers are used because the objects are being created in game::init_objects() and we just need to reference them
//pointers are used because everything is created in game::initialize() and we just need to reference them
std::map<int,draggable_object*> draggable_objects;
std::map<int,physics_object*> physics_objects;
std::map<int,rts_object*> rts_objects;
std::map<unsigned char, void (*)()> key_bindings;
std::map<unsigned char, void (*)()> key_toggles;
std::map<std::string, void (*)()> special_bindings;
std::map<std::string, void (*)()> special_toggles;
std::vector<text_object*> text_objects;
//button pointers are used because the buttons are being created in init_buttons() and we just need to reference them
std::vector<button*> buttons;
std::vector<menu*> menus;
std::vector<dropdown_menu*> dropdown_menus;
void add_draggable_object(draggable_object*);//add a draggable object to the scene
void add_physics_object(physics_object*);//add a physics object to the scene
void add_rts_object(rts_object*);//add an rts object to the scene
void add_text(text_object*);//add text object to the scene
void add_button(button*);//add button to the scene
void add_menu(menu*);//add menu to the scene
void bind_key(unsigned char, void (*)());//associate a key with an action
void add_menu(dropdown_menu*);//add dropdown menu to the scene
void bind_key(unsigned char, std::string, void (*)());//associate a key with an action that will be performed under given condition
void bind_key(unsigned char, void (*)(), void (*)());//associate a key that will perform 1 of 2 actions when toggled
void bind_special(std::string, void (*)());//associate a special input with an action
void bind_key(std::string, std::string, void (*)());//associate a special key with an action that will be performed under given condition
void bind_key(std::string, void (*)(), void (*)());//associate a special key that will perform 1 of 2 actions when toggled
void show_draggable_objects();//show all draggable objects
void hide_draggable_objects();//hide all draggable objects
void show_physics_objects();//show all physics objects
void hide_physics_objects();//hide all physics objects
void show_rts_objects();//show all rts objects
void hide_rts_objects();//hide all rts objects
void show_text();//show the text
void hide_text();//hide the text
void show_buttons();//show all buttons
void hide_buttons();//hide all buttons
void show_menus();//show all menus
void hide_menus();//hide all menus
void render();
void update();
void clear();
void show_all();//show all objects and ui elements
void hide_all();//hide all objects and ui elements
void render();//render all objects and ui elements
void update();//update all objects and ui elements
void sync();//update clock-based items
void clear();//delete all objects and ui elements
scene();
};

Expand Down
10 changes: 5 additions & 5 deletions include/tangible_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class tangible_object: virtual public movable_object
int touching[4];/*The number of the touching object is stored in each index. "0" is no object
Each index corresponds to a side: [1] is left, [2] is right, [3] is front, [4] is back*/
bool collided;
point2f closest_point(complex_object B);
void repel(complex_object B);//object moves away from object B
void attract(complex_object B);//object moves toward object B
void simon_says(complex_object B);//object changes color according to side touched
void identify_touched(complex_object B);//variable touching[] is updated with the number of the touched object
bool is_close(complex_object B);//check if object B is close to the center of this object
bool near_front(complex_object B);
bool near_back(complex_object B);
bool near_left(complex_object B);
bool near_right(complex_object B);
point2f closest_point(complex_object B);
void repel(complex_object B);//object moves away from object B
void attract(complex_object B);//object moves toward object B
void simon_says(complex_object B);//object changes color according to side touched
void identify_touched(complex_object B);//variable touching[] is updated with the number of the touched object
tangible_object();
};
#endif // TANGIBLE_H
4 changes: 2 additions & 2 deletions include/text_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "object.h"
#include <vector>
#include <string>
#include <sstream>

//A text object stores a string which can be formatted and displayed on screen
class text_object: public object
Expand All @@ -31,8 +32,7 @@ class text_object: public object
float font_height;
public:
int spacing;//the space between each line
void change_line(unsigned i, std::string l);
void add_line(std::string l);
void add_line(std::string str);
void set_font(std::string style, int size);
void clear();
void render();//prints the text to the screen
Expand Down
6 changes: 3 additions & 3 deletions include/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace ui
{
extern int margin;//space between window edge and text
void check_clicked();//check every clickable object to see if the cursor clicked it
void show_text();
void hide_text();
void update_text();
void show_text();//makes the text of the current scene visible
void hide_text();//makes the text of the current scene invisible
void update_text();//updates the text
}
#endif // UI_H
20 changes: 10 additions & 10 deletions include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
#define WINDOW_H
#include "point.h"

//This namespace contains variables and functions related to function
namespace window
{
extern int width;
extern int height;
extern point2i center;
extern int position_x;
extern int position_y;
extern double refresh_rate;
void change_size(int w, int h);
void initialize();
void render();
void update();
extern int width;
extern int height;
extern point2i center;
extern point2i position;//this refers to the upper left corner of the window
extern double refresh_rate;
void change_size(int w, int h);
void initialize();
void render();
void update();
};
#endif // WINDOW_H
17 changes: 9 additions & 8 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ a. left click and drag an object to move it around

B. Keyboard
1. Global
a. down or right arrow key to select next item in menu
b. up or left arrow key to select previous item in menu
c. "enter" or "return" key to choose selected item in menu
a. hold down or right arrow key to select next item in menu
b. hold up or left arrow key to select previous item in menu
c. press "enter" or "return" key to choose selected item in menu
2. In-Game
a. 'i' to toggle information overlay
b. 'esc' to open pause menu
c. "delete" to delete selected object
a. press 'i' to toggle information overlay
b. press 'esc' to open pause menu
c. press "delete" to delete selected object
d. press "insert" to create object of previous selection
3. Physics Objects (gray squares)
a. 'W','A','S','D' to move up,left,down,right, respectively
b. 'Q' and 'E' to rotate
a. hold 'W','A','S','D' to move up,left,down,right, respectively
b. hold 'Q' and 'E' to rotate

III. Known Bugs:
A. Physics Objects
Expand Down
Loading

0 comments on commit 71f59d4

Please sign in to comment.