Skip to content

Commit

Permalink
bug fixes, improved object selection
Browse files Browse the repository at this point in the history
Engine Changes:
-changed how the cursor records highlighted objects
-cursor can tell if ui objects are clicked/not clicked
-added enable/disable functionality to clickable objects
-fixed dropdown menu buttons
-simplified "mouse_function" methods

Demo Changes:
-fixed menu button
-fixed selection box
-multiple objects can be deleted at once
-all interactive elements are disabled when game is paused
-added a "create object" button in order to expedite the process
-updated readme
  • Loading branch information
confuzedskull committed Nov 5, 2014
1 parent e7e7028 commit 2331925
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 182 deletions.
7 changes: 7 additions & 0 deletions include/clickable_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@
class clickable_object: virtual public object
{
public:
bool highlighted();//checks if the object lies within the cursor selection box
bool hovered_over();//checks if the cursor lies within the this object
bool left_clicked();//checks if the cursor left clicked inside this object
bool right_clicked();//checks if the cursor right clicked inside this object
void enable();//allow the mouse function to work
void disable();//stop running mouse function
void highlight_function();//function to perform when object is highlighted
void hover_function();//function to perform when object is hovered over
void left_click_function();//function to perform when object is left clicked
void right_click_function();//function to perform when object is right clicked
void mouse_function();//performs a variety of actions dependent on cursor interaction
void update();
clickable_object();
Expand Down
4 changes: 3 additions & 1 deletion include/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ class cursor
static bool right_click;
static bool right_dragging;
static bool left_clicked_an_object;
static bool left_clicked_ui;
static bool right_clicked_an_object;
static bool grabbed_an_object;
static bool highlighting;
static std::map<int,bool> highlighted_objects;
static bool highlighting_enabled;
static std::map<int,clickable_object*> selected_objects;
static float xmin,xmax,ymin,ymax;
static int selected_object;
static int objects_selected();
Expand Down
1 change: 1 addition & 0 deletions include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class object
bool bordered;//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
bool enabled;//whether the object should be updated or not
void rotate(float angle);//changes the object's rotation by the given angle
void set_position(float x, float y);
void set_rotation(float angle);
Expand Down
1 change: 0 additions & 1 deletion include/rts_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class rts_object: public clickable_object, public tangible_object
{
public:
static point2f origin;
bool highlighted();//checks if this object lies within the highlighting box
void mouse_function();//performs a variety of actions dependent on cursor interaction
void update();
rts_object();
Expand Down
10 changes: 10 additions & 0 deletions include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,24 @@ class scene
void hide_rts_objects();//hide all rts objects
void show_text();//show the text
void hide_text();//hide the text
void enable_objects();//enable object mouse interaction
void disable_objects();//disable object mouse interaction
void show_checkboxes();//show all checkboxes
void hide_checkboxes();//hide all checkboxes
void enable_checkboxes();
void disable_checkboxes();
void show_buttons();//show all buttons
void hide_buttons();//hide all buttons
void enable_buttons();
void disable_buttons();
void show_menus();//show all menus
void hide_menus();//hide all menus
void enable_menus();
void disable_menus();
void show_all();//show all objects and ui elements
void hide_all();//hide all objects and ui elements
void enable_all();//enable all ui elements
void disable_all();//disable all ui elements
void render();//render all objects and ui elements
void update();//update all objects and ui elements
void sync();//update clock-based items
Expand Down
6 changes: 3 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
2DWorld
=======
by James Nakano AKA confuzedskull
by James Nakano A.K.A. "Confuzed Skull"

I. Troubleshooting
A. Windows
1. If the program doesn't run, then you must right click it and select "Run as Administrator"

B. Mac
1. If the message " "2dworld" was blocked from opening because it is not from an identified developer" do the following:
1. If the message " "2dworld" was blocked from opening because it is not from an identified developer" pops up, do the following:
a. Go to System Preferences and click on Security & Privacy.
b. Somewhere near the bottom you'll see a message and then a button "Open Anyway". Click it.
c. A prompt will come up saying ""2dworld is a Unix applicaiton downloaded from the internet. Are you sure you want to open it?" Click open.
Expand Down Expand Up @@ -55,6 +55,6 @@ C. RTS Objects
Disclaimer:
This program uses the OpenGL Utility Toolkit(A.K.A."GLUT") created by Mark Kilgard.
Thanks to his work, creating cross-platform OpenGL applications is much easier.
Since glut is not open source, I have not touched any of the source files.
Since GLUT is not open source, I have not touched any of the source files.
Instead, I have linked the library to the executable and included glut32.dll in the project directory.
For more information on GLUT, visit https://www.opengl.org/resources/libraries/glut/
8 changes: 3 additions & 5 deletions src/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ void button::allign_label(std::string allignment)

void button::mouse_function()
{
if(visible)
if(visible && enabled)
{
if(hovered_over() && !fill_color.changed)
fill_color.brighten();
if(!hovered_over())
fill_color.undo();
hover_function();
if(left_clicked())
{
cursor::left_clicked_ui = true;
if(!performed_action)
{
action();
Expand Down
31 changes: 18 additions & 13 deletions src/checkbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
along with the rest of 2DWorld. If not, see <http://www.gnu.org/licenses/>.*/

#include "checkbox.h"
#include "cursor.h"
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
Expand Down Expand Up @@ -62,22 +63,26 @@ void checkbox::bind_option(int* o)

void checkbox::mouse_function()
{
if(filled)
if(visible && enabled)
{
if(hovered_over() && !fill_color.changed)
fill_color.brighten();
if(!hovered_over())
fill_color.undo();
}
if(left_clicked())
{
if(*option==1)
checked=0;
if(filled)
{
if(hovered_over() && !fill_color.changed)
fill_color.brighten();
if(!hovered_over())
fill_color.undo();
}
if(left_clicked())
{
cursor::left_clicked_ui = true;
if(*option==1)
checked=0;
else
checked=1;
}
else
checked=1;
*option=checked;
}
else
*option=checked;
}

void checkbox::render()
Expand Down
108 changes: 80 additions & 28 deletions src/clickable_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,22 @@
#include <math.h>
#include <iostream>

bool clickable_object::highlighted()
{
//if object lies within selection box boundaries, return true
if(cursor::highlighting && enabled &&
isless(position.x,cursor::xmax) &&
isgreater(position.x,cursor::xmin) &&
isgreater(position.y,cursor::ymax) &&
isless(position.y,cursor::ymin))
return true;
else
return false;
}

bool clickable_object::hovered_over()
{
if(!cursor::left_click &&
if(!cursor::left_click && enabled &&
isless(cursor::passive.x,xmax) && isgreater(cursor::passive.x,xmin) &&
isless(cursor::passive.y,ymax) && isgreater(cursor::passive.y,ymin))
return true;
Expand All @@ -32,7 +45,7 @@ bool clickable_object::hovered_over()

bool clickable_object::left_clicked()
{
if(cursor::left_click &&
if(cursor::left_click && enabled &&
isless(cursor::left_down.x,xmax) && isgreater(cursor::left_down.x,xmin) &&
isless(cursor::left_down.y,ymax) && isgreater(cursor::left_down.y,ymin))
return true;
Expand All @@ -42,48 +55,87 @@ bool clickable_object::left_clicked()

bool clickable_object::right_clicked()
{
if(cursor::right_click &&
if(cursor::right_click && enabled &&
isless(cursor::right_down.x,xmax) && isgreater(cursor::right_down.x,xmin) &&
isless(cursor::right_down.y,ymax) && isgreater(cursor::right_down.y,ymin))
return true;
else
return false;
}

void clickable_object::mouse_function()
void clickable_object::highlight_function()
{
if(visible)
if(highlighted())//object lies within selection box
{
if(left_clicked())//clicked this object
{
if(!selected)
std::clog<<"object#"<<number<<'('<<type<<')'<<" selected"<<std::endl;
cursor::left_clicked_object=this;
cursor::left_clicked_an_object = true;
cursor::selected_object=number;
selected = true;
}
cursor::selected_objects[number]=this;
selected=true;
}
}

if(cursor::left_click && cursor::left_clicked_an_object && cursor::selected_object!=number)//clicked another object
{
cursor::highlighted_objects[number]=false;
selected = false;
}
void clickable_object::hover_function()
{
if(hovered_over() && !fill_color.changed)
fill_color.brighten();
if(!hovered_over())
fill_color.undo();
}

if(cursor::left_click && !cursor::left_clicked_an_object)//clicked nothing
void clickable_object::left_click_function()
{
if(left_clicked())//clicked this object
{
if(!selected)
std::clog<<"object#"<<number<<'('<<type<<')'<<" selected"<<std::endl;
cursor::left_clicked_object=this;
cursor::left_clicked_an_object = true;
cursor::selected_object=number;
cursor::selected_objects[number]=this;
selected = true;
}
else if(selected)
{
if(cursor::left_click && !cursor::highlighting)
{
cursor::highlighted_objects[number]=false;
selected = false;
if(cursor::left_clicked_an_object && cursor::selected_object !=number)//clicked another object
{
cursor::selected_objects.erase(number);
selected = false;
}
if(!cursor::left_clicked_ui)//clicked nothing
selected = false;
}
}
}

if(right_clicked())//right clicked this object
{
cursor::right_clicked_object=this;
cursor::right_clicked_an_object=true;
}
void clickable_object::right_click_function()
{
if(right_clicked())//right clicked this object
{
cursor::right_clicked_object=this;
cursor::right_clicked_an_object=true;
}
}

void clickable_object::mouse_function()
{
if(visible && enabled)
{
highlight_function();
left_click_function();
right_click_function();
}
}

void clickable_object::enable()
{
enabled=true;
}

void clickable_object::disable()
{
enabled=false;
}

void clickable_object::update()
{
mouse_function();
Expand All @@ -92,6 +144,6 @@ void clickable_object::update()
clickable_object::clickable_object(): object()
{
type="clickable object";
selected=false;
enable();
std::clog<<"object#"<<number<<'('<<type<<')'<<" created. "<<sizeof(*this)<<" bytes"<<std::endl;
}
25 changes: 18 additions & 7 deletions src/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,27 @@ void controls::check_clicked()
left_clicked=d.second->left_clicked();
for(auto p:game::current_scene->physics_objects)
left_clicked=p.second->left_clicked();
if(!left_clicked)//at this point, no objects have been left clicked so leave the loop
break;
}
cursor::left_clicked_an_object = left_clicked;

left_clicked=false;
while(!left_clicked)
{
for(auto b:game::current_scene->buttons)
left_clicked=b->left_clicked();
for(auto m:game::current_scene->menus)
left_clicked=m->item_clicked();
for(auto cb:game::current_scene->checkboxes)
left_clicked=cb->left_clicked();
for(auto dm:game::current_scene->dropdown_menus)
left_clicked=dm->item_clicked();
{
left_clicked=dm->left_clicked();
left_clicked=dm->item_clicked()!=-1;
}
if(!left_clicked)//at this point, no objects have been left clicked so leave the loop
break;
}
cursor::left_clicked_an_object = left_clicked;
cursor::left_clicked_ui = left_clicked;

bool right_clicked=false;
while(!right_clicked)
Expand All @@ -153,7 +164,7 @@ void controls::check_clicked()
}
cursor::right_clicked_an_object = right_clicked;

bool grabbed=true;
bool grabbed=false;
for(auto d:game::current_scene->draggable_objects)
{
if(d.second->grabbed())
Expand Down Expand Up @@ -205,9 +216,9 @@ void controls::mouse_move(int x, int y)

void controls::mouse_drag(int x, int y)
{
if(cursor::left_click)
if(cursor::left_click)//mouse left button is down
{
if(!cursor::left_clicked_an_object && !cursor::grabbed_an_object)
if(cursor::highlighting_enabled && !cursor::left_clicked_an_object && !cursor::grabbed_an_object)
{
//this condition makes it so that the user has to make a rectangle larger than 10x10. That way, highlighting is less sensitive
if(isgreater(x,cursor::left_down.x+10) && isless((window::height - y),cursor::left_down.y+10))
Expand Down
Loading

0 comments on commit 2331925

Please sign in to comment.