Skip to content

Commit

Permalink
Merge pull request #5 from tylernh10/menu-functionality
Browse files Browse the repository at this point in the history
menu-functionality
  • Loading branch information
tylernh10 authored Jan 8, 2023
2 parents fa5ff9e + 1ebc473 commit f57dee9
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 130 deletions.
48 changes: 48 additions & 0 deletions Menu.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
#include "header-files/Menu.h"

Menu::Menu(EditModeMouseFunction& e, InsertModeMouseFunction& i) : editModeMouseFunction(e), insertModeMouseFunction(i) {
for (int i = 0; i < 20; i++) {
currentHover.push_back(false);
}
}

Menu::~Menu() {
al_destroy_bitmap(divider);
al_destroy_bitmap(background);
al_destroy_font(font);
for (auto i : buttons) {
al_destroy_bitmap(i);
}
for (auto i : hoverButtons) {
al_destroy_bitmap(i);
}
for (auto i : colorButtons) {
al_destroy_bitmap(i);
}
for (auto i : colorHoverButtons) {
al_destroy_bitmap(i);
}
}

void Menu::initDivider(ALLEGRO_BITMAP* div) {
divider = div;
}
Expand Down Expand Up @@ -69,27 +93,51 @@ void Menu::detectMouse(int px, int py) {
if (py < 75 && py > 0 && px >= i*75 && px < (i+1)*75) {
al_draw_bitmap(hoverButtons[i], i*75, 0, 0);
al_draw_text(font, al_map_rgb(0, 0, 0), 3, 80, ALLEGRO_ALIGN_LEFT, hoverStrings[i]);
currentHover.at(i) = true;
}
else {
currentHover.at(i) = false;
}
}
for (unsigned int i = 0; i < 4; i++) {
if (py < 37 && py > 0 && px >= 10 * 75 + 37 * i && px < 10 * 75 + 37 * (i + 1)) {
al_draw_bitmap(colorHoverButtons.at(i), 10 * 75 + 37 * i, 0, 0);
al_draw_text(font, al_map_rgb(0, 0, 0), 3, 80, ALLEGRO_ALIGN_LEFT, hoverStrings[i+10]);
currentHover.at(i + 10) = true;
}
else {
currentHover.at(i + 10) = false;
}
}
for (unsigned int i = 4; i < 8; i++) {
if (py < 75 && py > 37 && px >= 10 * 75 + 37 * (i - 4) && px < 10 * 75 + 37 * (i - 4 + 1)) {
al_draw_bitmap(colorHoverButtons.at(i), 10 * 75 + 37 * (i - 4), 38, 0);
al_draw_text(font, al_map_rgb(0, 0, 0), 3, 80, ALLEGRO_ALIGN_LEFT, hoverStrings[i-4+14]);
currentHover.at(i + 10) = true;
}
else {
currentHover.at(i + 10) = false;
}
}

if (py > 14 && py < 69 && px >= 900 && px < 950) {
al_draw_bitmap(hoverButtons.at(10), 900, 14, 0);
al_draw_text(font, al_map_rgb(0, 0, 0), 3, 80, ALLEGRO_ALIGN_LEFT, hoverStrings[18]);
currentHover.at(18) = true;
}
else {
currentHover.at(18) = false;
}
if (py > 14 && py < 69 && px >= 950 && px < 1000) {
al_draw_bitmap(hoverButtons.at(11), 950, 14, 0);
al_draw_text(font, al_map_rgb(0, 0, 0), 3, 80, ALLEGRO_ALIGN_LEFT, hoverStrings[19]);
currentHover.at(19) = true;
}
else {
currentHover.at(19) = false;
}
}

bool Menu::checkOverButton(int x) {
return currentHover.at(x);
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ at a later time. The file will be created if it does not exist.
### Future Features:
* Documentation on shape scheme
* Makefile for easier compilation
* Menu onscreen allowing users to change colors of shapes, changes modes, etc.
* Menu onscreen allowing users to change colors of shapes, changes modes, etc. *(in progress)*
* Add support for users to add onscreen text
* Allowing users to specify which file to open in the application instead of only via command line
* Detailed documentation on design patterns used
* Detailed description on how to install Allegro

### Copyright Info

&copy; Tyler Hinrichs 2022
10 changes: 9 additions & 1 deletion header-files/ECGraphicViewImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ enum ECGVEventType
ECGV_EV_KEY_DOWN_F = 25,
ECGV_EV_KEY_UP_F = 26,
ECGV_EV_KEY_DOWN_CTRL = 27,
ECGV_EV_KEY_UP_CTRL = 28
ECGV_EV_KEY_UP_CTRL = 28,
ECGV_EV_KEY_DOWN_S = 29,
ECGV_EV_KEY_DOWN_H = 30
};

//***********************************************************
Expand Down Expand Up @@ -140,6 +142,12 @@ class ECGraphicViewImp : public ECObserverSubject
void DrawFilledCircle(int xcenter, int ycenter, double radius, ECGVColor color = ECGV_BLACK);
void DrawEllipse(int xcenter, int ycenter, double radiusx, double radiusy, int thickness = 3, ECGVColor color = ECGV_BLACK);
void DrawFilledEllipse(int xcenter, int ycenter, double radiusx, double radiusy, ECGVColor color = ECGV_BLACK);
// cursor setting
void insertCursor();
void defaultCursor();

// Access view
ALLEGRO_DISPLAY* getDisplay() { return display; }

private:
// Internal functions
Expand Down
91 changes: 68 additions & 23 deletions header-files/ECRealObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,48 @@ class ECAbstractMouseFunction;

// Spacebar observer
// used to change mode
class ECSpaceObserver : public ECObserver
class ECModeObserver : public ECObserver
{
public:
ECSpaceObserver(ECGraphicViewImp& view, Controller& ctrl) : view(view), ctrl(ctrl) {}
virtual ~ECSpaceObserver() {}
ECModeObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECModeObserver() {}
virtual void Update() override;
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu& menu;
};

// Draw observer
// draws relevant info onscreen
class ECDrawObserver : public ECObserver
{
public:
ECDrawObserver(ECGraphicViewImp& view, Controller& ctrl) : view(view), ctrl(ctrl) {}
ECDrawObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECDrawObserver() {}
virtual void Update() override;
void attachMenu(Menu* m) { menu = m; }
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu* menu;
Menu& menu;
};

// D key observer
// Delete observer
// deletes selected shape in edit mode
class ECDObserver : public ECObserver
class ECDelObserver : public ECObserver
{
public:
ECDObserver(ECGraphicViewImp& view, Controller& ctrl) : view(view), ctrl(ctrl) {}
virtual ~ECDObserver() {}
virtual void Update();
ECDelObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECDelObserver() {}
virtual void Update() override;
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu& menu;
};

// Mouse Observer
// various functionality when mouse is pressed down/up in both edit and insert mode
// various functionality when mouse is pressed down/up in both edit and insert mode (doesn't include menu clicking functionality)
class ECMouseObserver : public ECObserver
{
public:
Expand All @@ -74,39 +75,41 @@ class ECMouseObserver : public ECObserver
class ECUndoRedoObserver : public ECObserver
{
public:
ECUndoRedoObserver(ECGraphicViewImp& view, Controller& ctrl) : view(view), ctrl(ctrl) {}
ECUndoRedoObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECUndoRedoObserver() {}
virtual void Update();
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu& menu;
};

// G Key Observer
// Toggles rectangle and ellipse insertion when in insert mode
// Group Observer
// Groups/ungroups when in edit mode
class ECGObserver : public ECObserver
class ECGroupObserver : public ECObserver
{
public:
ECGObserver(ECGraphicViewImp& view, Controller& ctrl) : view(view), ctrl(ctrl) {}
virtual ~ECGObserver() {}
ECGroupObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECGroupObserver() {}
virtual void Update();
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu& menu;
};

// F Key Observer
// Toggles filled and non-filled mode
class ECFObserver : public ECObserver
// Type Insert Observer
// Determines which shape to insert
class ECTypeInsertObserver : public ECObserver
{
public:
ECFObserver(ECGraphicViewImp& view, Controller& ctrl) : view(view), ctrl(ctrl) {}
virtual ~ECFObserver() {}
ECTypeInsertObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECTypeInsertObserver() {}
virtual void Update();
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu& menu;
};

// CTRL Key Observer
Expand All @@ -122,6 +125,48 @@ class ECCtrlObserver : public ECObserver
Controller& ctrl;
};

// Color Observer
// Allows user to change color with buttons in menu
class ECColorObserver : public ECObserver
{
public:
ECColorObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECColorObserver() {}
virtual void Update();
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu& menu;
};

// Save Observer
// Allows user to save via the keyboard or menu
class ECSaveObserver : public ECObserver
{
public:
ECSaveObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECSaveObserver() {}
virtual void Update();
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu& menu;
};

// Help Observer
// Allows user to save via the keyboard or menu
class ECHelpObserver : public ECObserver
{
public:
ECHelpObserver(ECGraphicViewImp& view, Controller& ctrl, Menu& m) : view(view), ctrl(ctrl), menu(m) {}
virtual ~ECHelpObserver() {}
virtual void Update();
private:
ECGraphicViewImp& view;
Controller& ctrl;
Menu& menu;
};

// Arrow Key Observers
// When pressed, they move all selected shapes 10 by the direction specified by the key pressed
class ECGenericArrowObserver : public ECObserver
Expand Down
7 changes: 4 additions & 3 deletions header-files/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ using namespace std;

class Menu {
public:
Menu(EditModeMouseFunction& e, InsertModeMouseFunction& i): editModeMouseFunction(e), insertModeMouseFunction(i) {}
~Menu() {} // TODO: destroy all bitmaps
Menu(EditModeMouseFunction& e, InsertModeMouseFunction& i);
~Menu(); // TODO: destroy all bitmaps
void initDivider(ALLEGRO_BITMAP* div);
void initBackground(ALLEGRO_BITMAP* bg);

void initButtons(vector<ALLEGRO_BITMAP*> bts);
void initHoverButtons(vector<ALLEGRO_BITMAP*> bts);
void initColorButtons(vector<ALLEGRO_BITMAP*> bts);
void initColorHoverButtons(vector<ALLEGRO_BITMAP*> bts);
void initFont(ALLEGRO_FONT* f);
void timer(int mode); // draws all menu buttons and take care of trace shapes
void detectMouse(int px, int py);
bool checkOverButton(int x);
private:
ALLEGRO_BITMAP* divider;
ALLEGRO_BITMAP* background;
Expand Down Expand Up @@ -60,6 +60,7 @@ class Menu {
};
EditModeMouseFunction& editModeMouseFunction;
InsertModeMouseFunction& insertModeMouseFunction;
vector<bool> currentHover; // keeps track of what button is currently being hovered over
};

#endif
28 changes: 24 additions & 4 deletions header-files/ShapesModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
#include "Command.h"
#include <vector>
#include <algorithm>
#include <fstream>
#include <sstream>
using namespace std;

class ECCommandHistory; // necessary forward declaration

class ShapesModel {
public:
ShapesModel() {}
ShapesModel(string f): color(ECGV_BLACK), filename(f) {}
vector<Shape*> getListShapes() { return listShapes; }
void addShape(Shape* x) { listShapes.push_back(x); }
void removeShape(Shape* x);
Expand All @@ -26,11 +28,20 @@ class ShapesModel {
void select(int px, int py, bool ctrlIsAsserted);
vector<Shape*> getSelected(); // return currently selected shape
void removeSelected(); // clears selected vector; deselects any shape that is selected

// color
ECGVColor parseColor(int x);
ECGVColor getColor(); // retrieves current color
void setColor(int x); // sets current color

// saving
void save();

private:
vector<Shape*> listShapes;
vector<Shape*> selected;
ECGVColor color;
string filename;
};

class Controller {
Expand Down Expand Up @@ -68,13 +79,15 @@ class Controller {
void deleteShape(); // delete selected shape
void moveShape(int translateX, int translateY); // move shape to new position based on translation

// G key pressed
// G key pressed (G can be "pressed" from the menu)
bool isGAsserted() { return gIsAsserted; }
void pressGKey() { gIsAsserted = !gIsAsserted; }
void setGKey(bool x) { gIsAsserted = x; }

// F key pressed
bool isFAsserted() { return fIsAsserted; }
void pressFKey() { fIsAsserted = !fIsAsserted; }
void setFKey(bool x) { fIsAsserted = x; }

// Reset F and G assertions to false when switching modes
void resetFandGAssertions();
Expand All @@ -89,13 +102,20 @@ class Controller {
void pressLeftArrow();
void pressRightArrow();

// G key in edit mode
void pressGKeyEditMode();
// Group/Ungroup
void GroupShapes();
void removeSelected() { model->removeSelected(); }

// undo/redo operations
void Undo();
void Redo();

// color
void setColor(int x);

// save
void save() { model->save(); }

private:
ShapesModel* model; // reference to model
ECCommandHistory* history; // saves history of all commands so far
Expand Down
Loading

0 comments on commit f57dee9

Please sign in to comment.