Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/opengl/ButtonWidget.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ class ButtonWidget : public InteractiveWidget2D {
void setAction(Func func) {
this->func = func;
}

void updateCanvas(MainCanvas* canvas) { c = canvas; };
};
12 changes: 10 additions & 2 deletions src/opengl/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Canvas::Canvas(Tab* tab) : Canvas(tab->getParentWin(), tab) {}

Canvas::~Canvas() { cleanup(); }

void MainCanvas::copy(const MainCanvas& orig) {
gui = orig.gui;
guiText = orig.guiText;
menu = orig.menu;
menuText = orig.menuText;
}

void Canvas::cleanup() {
for (uint32_t i = 0; i < layers.size(); i++) delete layers[i];
layers.clear();
Expand Down Expand Up @@ -86,8 +93,9 @@ void MainCanvas::init() {
menuText->init();
tab->registerCallback(Tab::Inputs::MOUSE0_PRESS, "Widget Callback- Press",
Tab::Security::SAFE, bind(&MainCanvas::click, this));
tab->registerCallback(Tab::Inputs::MOUSE0_RELEASE, "Widget Callback- Release",
Tab::Security::SAFE, bind(&MainCanvas::click, this));
// tab->registerCallback(Tab::Inputs::MOUSE0_RELEASE, "Widget Callback-
// Release",
// Tab::Security::SAFE, bind(&MainCanvas::click, this));
}

void MainCanvas::render() {
Expand Down
3 changes: 2 additions & 1 deletion src/opengl/Canvas.hh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Canvas {
// z!=0
}
~Canvas();
Canvas(const Canvas& orig) = delete;
Canvas(const Canvas&) = delete;
Canvas& operator=(const Canvas& orig) = delete;
uint32_t getWidth() const { return vpW; }
uint32_t getHeight() const { return vpH; }
Expand Down Expand Up @@ -126,6 +126,7 @@ class MainCanvas : public Canvas {
~MainCanvas();
MainCanvas(const MainCanvas&) = delete;
MainCanvas& operator=(const MainCanvas&) = delete;
void copy(const MainCanvas&);
StyledMultiShape2D* getGui() { return gui; }
MultiText* getGuiText() { return guiText; }
StyledMultiShape2D* getMenu() { return menu; }
Expand Down
11 changes: 10 additions & 1 deletion src/opengl/GLWin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void GLWin::keyCallback(GLFWwindow *win, int key, int scancode, int action,
int mods) {
uint32_t input = (mods << 11) | (action << 9) | key;
cerr << "key: " << key << " mods: " << mods << " input=" << input << '\n';
winMap[win]->sharedTab->doit(input);
winMap[win]->currentTab()->doit(input);
}

Expand All @@ -107,13 +108,15 @@ void GLWin::mouseButtonCallback(GLFWwindow *win, int button, int action,
uint32_t input = (mods << 9) | (action << 3) | button;
fmt::print("mouse!{}, action={}, location=({}, {}), input={}\n", button,
action, w->mouseX, w->mouseY, input);
winMap[win]->sharedTab->doit(input);
winMap[win]->currentTab()->doit(input);
}

void GLWin::scrollCallback(GLFWwindow *win, double xoffset, double yoffset) {
// cout << "xoffset=" << xoffset << " yoffset=" << yoffset << '\n';
// todo: we would have to copy offsets into the object given the way this is
uint32_t input = 400;
winMap[win]->sharedTab->doit(input + int(yoffset));
winMap[win]->currentTab()->doit(input + int(yoffset));
}

Expand Down Expand Up @@ -247,6 +250,7 @@ void GLWin::startWindow() {
guiTextStyle = new Style(guiFont, 0.5, 0.5, 0.5, 0, 0, 0, 1, COMMON_SHADER);
menuStyle = new Style(menuFont, 0.5, 0.5, 0.5, 0, 0, 0, 1, COMMON_SHADER);
menuTextStyle = new Style(menuFont, 0.5, 0.5, 0.5, 0, 0, 0, 1, COMMON_SHADER);
sharedTab = new Tab(this);
tabs.add(new Tab(this));
current = 0;
hasBeenInitialized = true;
Expand All @@ -272,6 +276,7 @@ void GLWin::baseInit() {
for (int i = 0; i < tabs.size(); ++i) {
tabs[i]->init();
}
sharedTab->init();
}

int GLWin::init(GLWin *g, uint32_t w, uint32_t h, uint32_t exitAfter) {
Expand Down Expand Up @@ -300,6 +305,8 @@ void GLWin::cleanup() {
delete tabs[i]; // TODO:cw[i]->cleanup();
}
tabs.clear();
delete sharedTab;
sharedTab = nullptr;
delete defaultStyle;
defaultStyle = nullptr;
Shader::cleanAll();
Expand Down Expand Up @@ -349,6 +356,7 @@ void GLWin::mainLoop() {
bgColor.a); // Clear the colorbuffer and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
sharedTab->render();
renderTime += glfwGetTime() - startRender;
glfwSwapBuffers(win); // Swap buffer so the scene shows on screen
if (frameCount >= 150) {
Expand All @@ -367,10 +375,12 @@ void GLWin::mainLoop() {
}
currentTab()->tick(); // update time in current tab for any models using
// simulation time
sharedTab->tick();
needsUpdate = false;
glfwPollEvents(); // Check and call events
// note: any events needing a refresh should set dirty = true
if (currentTab()->checkUpdate()) setUpdate();
if (sharedTab->checkUpdate()) setUpdate();
if (needsUpdate) {
update();
needsRender = true;
Expand Down Expand Up @@ -480,7 +490,6 @@ void GLWin::prevTab() {
Tab *GLWin::addTab() {
Tab *newTab = new Tab(this);
tabs.add(newTab);
current = tabs.size() - 1;
return newTab;
}

Expand Down
3 changes: 3 additions & 0 deletions src/opengl/GLWin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class GLWin {
uint32_t frameNum;
double lastRenderTime; // Stores last time of render
char frameName[32];
Tab* sharedTab;
DynArray<Tab*> tabs; // list of web pages, ie tabs
uint32_t current; // current (active) tab
void checkUpdate();
Expand Down Expand Up @@ -124,6 +125,7 @@ class GLWin {
MainCanvas* getMainCanvas();

Tab* currentTab() { return tabs[current]; }
Tab* getSharedTab() { return sharedTab; }

void setSize(uint32_t w, uint32_t h) {
width = w;
Expand Down Expand Up @@ -187,5 +189,6 @@ Shape* pick(int x, int y, Shape*); // click on (x,y), get Shape behind
void prevTab();
Tab* addTab();
void removeTab();
void toLastTab() { current = tabs.size() - 1; }
void goToLink(const char ipaddr[], uint16_t port, uint32_t requestID);
};
71 changes: 41 additions & 30 deletions src/opengl/NavigationBar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,66 @@ NavigationBar::NavigationBar(MainCanvas* initialCanvas,
canvases.insert(initialCanvas);

this->isVertical = isVertical;

defaultButtonHeight = min(48_f32, height);
defaultButtonWidth = min(90_f32, width);
defaultEdgeWidth = min(16_f32, width / 3);

/*
addButton(defaultButtonWidth, defaultButtonHeight, axisPadding, "+",
"add tab")
.setAction(bind(&NavigationBar::addTab, this));
*/
}

NavigationBar::NavigationBar(Tab* initialTab, float x, float y, float width,
NavigationBar::NavigationBar(GLWin* w, float x, float y, float width,
float height, float axisPadding, bool isVertical)
: NavigationBar{initialTab->getMainCanvas(),
initialTab->getMainCanvas()->getStyle(),
x,
y,
width,
height,
axisPadding,
isVertical} {
parentWin = initialTab->getParentWin();
: NavigationBar(w->getSharedTab()->getMainCanvas(),
w->getSharedTab()->getMainCanvas()->getStyle(), x, y, width,
height, axisPadding, isVertical) {
parentWin = w;
}

ButtonWidget& NavigationBar::addButton(float width, float height,
ButtonWidget* NavigationBar::addButton(float width, float height,
float axisOffset, string label,
const char action[]) {
buttons.emplace_back(currentCanvas, xPos, yPos, width, height, label, action);
buttons.back().setStyledMultiShape(&m);
buttons.back().setMultiText(&t);
buttons.back().redraw();
if (isVertical) {
// TODO: emplace_back failing to copy m
// buttons.emplace_back(currentCanvas, xPos, yPos + axisOffset, width,
// height, label, action);
buttons.push_back(new ButtonWidget(currentCanvas, xPos, yPos + axisOffset,
width, height, label, action));
} else {
// TODO: ditto
// buttons.emplace_back(currentCanvas, xPos + axisOffset, yPos, width,
// height, label, action);
buttons.push_back(new ButtonWidget(currentCanvas, xPos + axisOffset, yPos,
width, height, label, action));
}
// buttons.back().setStyledMultiShape(&m);
// buttons.back().setMultiText(&t);
// buttons.back().redraw();
return buttons.back();
}

void NavigationBar::addTab() {
void NavigationBar::setButtonAction(
int buttonIndex, std::optional<std::function<void(void)>> func) {
buttons[buttonIndex]->setAction(func);
}

void NavigationBar::addNewTab() {
Tab* t = parentWin->addTab();
t->addMember(this);
reparentButtons(t->getMainCanvas());
t->setUpdate();
t->setRender();
canvases.insert(t->getMainCanvas());
parentWin->toLastTab();
// addToTab(t);
}

void NavigationBar::reparentButtons(MainCanvas* c) {
if (canvases.find(c) == canvases.end()) {
c->addLayer(&m);
c->addLayer(&t);
canvases.insert(c);
}
for (ButtonWidget& b : buttons) {
c->addClickableWidget(&b);
}
void NavigationBar::addToTab(Tab* tab) {
tab->addMember(this);
MainCanvas* newCanvas = tab->getMainCanvas();
canvases.insert(newCanvas);
newCanvas->copy(*currentCanvas);
tab->setUpdate();
tab->setRender();
}

void NavigationBar::balanceButtons(int numButtons) {}
45 changes: 26 additions & 19 deletions src/opengl/NavigationBar.hh
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once

#include <iostream>
#include <unordered_set>

#include "opengl/ButtonWidget.hh"

class NavigationBar : public Member {
private:
std::vector<ButtonWidget> buttons;
std::vector<ButtonWidget*> buttons;
std::unordered_set<MainCanvas*> canvases;
GLWin* parentWin;
MainCanvas* currentCanvas;
Expand Down Expand Up @@ -41,15 +42,15 @@ class NavigationBar : public Member {
/**
* @brief Create a new (horizontal or vertical) navigation bar
*
* @param initialTab The initial tab to draw on
* @param w The initial window to draw on
* @param x x-coordinate of top-left corner
* @param y y-coordinate of top-left corner
* @param width width of navigation bar
* @param height height of navigation bar
* @param axisPadding distance between the edges of two buttons
* @param isVertical allow vertical navigation bars (e.x. Ubuntu's task bar)
*/
NavigationBar(Tab* initialTab, float x, float y, float width, float height,
NavigationBar(GLWin* w, float x, float y, float width, float height,
float axisPadding, bool isVertical = false);

/**
Expand All @@ -62,35 +63,41 @@ class NavigationBar : public Member {
* @param action a description of the button's action
* @return ButtonWidget& a reference to the new button
*/
ButtonWidget& addButton(float width, float height, float axisOffset,
ButtonWidget* addButton(float width, float height, float axisOffset,
std::string label, const char action[]);

/**
* @brief Balance n buttons on the nav bar
*
* This is particularly useful in scenarios where a screen can fit n tabs and
* there are m tabs that could be displayed (n < m). In this case, the
* NavigationBar will determine the ideal placement of each button such that
* at most numButtons will be displayed at a given time.
* @brief Set an action for a button in the nav bar
*
* @param numButtons number of buttons to display
* @param buttonIndex index of button in buttons vector
* @param func function call for action
*/
void balanceButtons(int numButtons);
void setButtonAction(int buttonIndex,
std::optional<std::function<void(void)>> func);

/**
* @brief Allows the buttons to be reparented to a new MainCanvas
* @brief Balance n buttons on the nav bar
*
* This function uses the fact that each InteractiveWidget2D, and therefore
* each Widget2D can use a custom StyledMultiShape2D and MultiText to swap
* over to a new MainCanvas and redraw each shape.
* This is particularly useful in scenarios where a screen can fit n tabs
* and there are m tabs that could be displayed (n < m). In this case, the
* NavigationBar will determine the ideal placement of each button such
* that at most numButtons will be displayed at a given time.
*
* @param c
* @param numButtons number of buttons to display
*/
void reparentButtons(MainCanvas* c);
void balanceButtons(int numButtons);

/**
* @brief Adds a new tab to the window and adds the navigation bar to the tab
*
*/
void addTab();
void addNewTab();

void addToTab(Tab* tab);

/*
void render() {
std::cout << "printing from NavigationBar::render()" << std::endl;
}
*/
};
50 changes: 25 additions & 25 deletions src/opengl/Widget2D.hh
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#pragma once
#include "opengl/GrailGUI.hh"

/*
Represent a compound object to be drawn using StyledMultiShape2D and MultiText

*/

class StyledMultiShape2D;
class MultiText;

class Widget2D {
protected:
StyledMultiShape2D* m;
MultiText* t;
float x, y; // top left corner of the widget on the screen
float w, h; // width and height of the widget
public:
Widget2D(StyledMultiShape2D* m, MultiText* t, float x, float y, float w,
float h)
: m(m), t(t), x(x), y(y), w(w), h(h) {}
void setStyledMultiShape(StyledMultiShape2D* m) { this->m = m; }
void setMultiText(MultiText* t) { this->t = t; }
virtual void init() = 0;
};
#pragma once
#include "opengl/GrailGUI.hh"
/*
Represent a compound object to be drawn using StyledMultiShape2D and MultiText
*/
class StyledMultiShape2D;
class MultiText;
class Widget2D {
protected:
StyledMultiShape2D* m;
MultiText* t;
float x, y; // top left corner of the widget on the screen
float w, h; // width and height of the widget
public:
Widget2D(StyledMultiShape2D* m, MultiText* t, float x, float y, float w,
float h)
: m(m), t(t), x(x), y(y), w(w), h(h) {}
void setStyledMultiShape(StyledMultiShape2D* m) { this->m = m; }
void setMultiText(MultiText* t) { this->t = t; }
virtual void init() = 0;
};
Loading