Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions include/MenuBar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <Event.h>
#include <string>
#include <memory>

class MenuBar : public Container {
public:
MenuBar();
~MenuBar() {};

void update(float) override;
void draw() override;


WIDGET_CONSTRUCT(MenuBar);
WIDGET_CONSTRUCT_PARENT(MenuBar);
std::string m_text;
bool on_click = false;

protected:

private:
unsigned int m_bar_height = 30;// I read that MenuBar is usually a fixed height
};

21 changes: 21 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <Application.h>
#include <Window.h>
#include <MenuBar.h>
#include <Button.h>
#include <CheckBox.h>
#include <Slider.h>
Expand All @@ -20,6 +21,26 @@ int main(int argc, char** argv) {
window->set_title("Hello, from c++");
// window->set_resizable(true);

auto menu_bar = MenuBar::construct(window);

auto file_btn = Button::construct(menu_bar);
file_btn->set_text("Edit");
file_btn->on_click([](auto event) {
PopupDialog::show("Menu Action", "You clicked the Edit button on the MenuBar!");
});

auto edit_btn = Button::construct(menu_bar);
edit_btn->set_text("Edit");
edit_btn->on_click([](auto event) {
PopupDialog::show("Menu Action", "You clicked the Edit button on the MenuBar!");
});

auto help_btn = Button::construct(menu_bar);
help_btn->set_text("Help");
help_btn->on_click([](auto event) {
PopupDialog::show("Menu Action", "You clicked the Help button on the MenuBar!");
});

auto desktop = window->desktop();
desktop->on_draw([](Rectangle desktop_rect) {
auto x = desktop_rect.width / 2;
Expand Down
66 changes: 58 additions & 8 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include "nob.h"

typedef struct {
Expand All @@ -12,23 +13,28 @@ typedef struct {
} StringList;


#define SOURCE_FILE_COUNT 15 // Modify this when adding a new files to the list of files
#define SOURCE_FILE_COUNT 16 // Modify this when adding a new files to the list of files

bool to_files(const char*[SOURCE_FILE_COUNT], StringList*, const char*, const char*);
bool build_object_files(const char*[SOURCE_FILE_COUNT], StringList, StringList*);
bool build_library(StringList);
bool build_example(void);
bool generate_build_h();

int main(int argc, char** argv) {
NOB_GO_REBUILD_URSELF(argc, argv);
const char* source_file_names[SOURCE_FILE_COUNT] = {
"Application", "Button", "CheckBox", "ComboBox", "Container", "Desktop", "InputDialog",
"Label","MessageDialog", "Panel", "PopupDialog", "Slider", "Widget", "Window",
"tinyfiledialogs"
"tinyfiledialogs","MenuBar"
};

nob_mkdir_if_not_exists("./build");

if(!generate_build_h()){
return 1;
}

StringList src_files = {0};
if (!to_files(source_file_names, &src_files, "src", "cpp")) {
nob_log(NOB_ERROR, "Failed to make source files");
Expand All @@ -41,11 +47,6 @@ int main(int argc, char** argv) {
return 1;
}

if (!build_object_files(source_file_names, src_files, &object_files)) {
nob_log(NOB_ERROR, "Failed to build sources into object files");
return 1;
}

if (!build_library(object_files)) {
nob_log(NOB_ERROR, "Failed to build final library artifacts");
return 1;
Expand Down Expand Up @@ -84,9 +85,14 @@ bool build_example(void) {
nob_cmd_append(&cmd, "main.cpp");
nob_cmd_append(&cmd, "-o", "main.exe");
nob_cmd_append(&cmd, libs, "-Lbuild/");

#if defined(_WIN32) || defined (_WIN64)
nob_cmd_append(&cmd,
"-lrasen", "-lraylib", "-lm", "-lwinmm",
"-lgdi32", "-lopengl32", "-lole32", "-lcomdlg32");
#else
nob_cmd_append(&cmd, "-lrasen", "-lraylib", "-lm", "-lGL", "-lpthread", "-ldl", "-lrt", "-lX11");
#endif

nob_log(NOB_INFO, "Building example");
if (!nob_cmd_run(&cmd)) {
Expand Down Expand Up @@ -127,9 +133,13 @@ bool build_library(StringList object_files) {

nob_cmd_append(&cmd, "-o", shared_object_name);
nob_cmd_append(&cmd, libs);

#if defined(_WIN32) || defined (_WIN64)
nob_cmd_append(&cmd, "-lraylib", "-lm", "-lwinmm", "-lgdi32", "-lopengl32",
"-lgdi32", "-lopengl32", "-lole32", "-lcomdlg32");

#else
/* nob_cmd_append(&cmd, "-lraylib", "-lm", "-lGL", "-lpthread", "-ldl", "-lrt", "-lX11"); */
#endif

nob_log(NOB_INFO, "Creating .a file [%s]", static_object_name);

Expand Down Expand Up @@ -181,6 +191,9 @@ bool build_object_files(const char* names[SOURCE_FILE_COUNT], StringList source_
for (uint64_t i = 0; i < SOURCE_FILE_COUNT; ++i) {
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "g++");
#if !defined(_WIN32) || (_WIN64)
nob_cmd_append(&cmd, "-fPIC");
#endif
nob_cmd_append(&cmd, "-o");
nob_cmd_append(&cmd, object_files->items[i]);
nob_cmd_append(&cmd, "-c");
Expand Down Expand Up @@ -212,3 +225,40 @@ bool to_files(const char* names[SOURCE_FILE_COUNT], StringList* source_files, co
return true;
}

bool generate_build_h(){
time_t t = time(NULL);
struct tm tm = *localtime(&t);
nob_mkdir_if_not_exists("./include");

const char *path = "./include/build.h";
nob_log(NOB_INFO,"Generating %s", path);

FILE *f = fopen(path,"w");
if(f == NULL){
nob_log(NOB_ERROR,"Failed to open %s for writing", path);
return false;
}
Nob_String_Builder sb = {0};
nob_sb_appendf(&sb, "#ifndef __BUILD_DATE__\n\n");
nob_sb_appendf(&sb, "#define __BUILD_DATE__ \"%d-%02d-%02d %02d:%02d:%02d\"\n",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

char* os = NULL;
#if defined(__linux__) || defined(__unix__)
char* user = getenv("USER");
os = "Linux/Unix";
#elif defined(_WIN32) || defined(_WIN64)
os = "Windows";
#endif

nob_sb_appendf(&sb, "#define __BUILD_BY__ \"%s\"\n", user);
nob_sb_appendf(&sb, "#define __BUILD_OS__ \"%s\"\n", os);

nob_sb_appendf(&sb,"\n#endif // RASEN_BUILD_H\n");
fwrite(sb.items,1,sb.count,f);

nob_da_free(sb);
fclose(f);
nob_log(NOB_INFO,"Successfully generated %s", path);
return true;
}
35 changes: 35 additions & 0 deletions src/MenuBar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "MenuBar.h"

MenuBar::MenuBar() {
this->set_margin(4);
this->set_spacing(10);
}

void MenuBar::update(float dt) {
unsigned int screen_width = GetScreenWidth();

this->set_location(0, 0);
this->set_size(screen_width, m_bar_height);

unsigned int current_x = this->margin();
unsigned int center_y_offset = this->margin();

for (Widget *child : children()) {
if (child->height() < m_bar_height) {
center_y_offset = (m_bar_height - child->height()) / 2;
}

child->set_location(current_x, center_y_offset);

child->update(dt);
current_x += child->width() + this->spacing();
}
}

void MenuBar::draw() {
DrawRectangleRec(this->bounding_rect(), LIGHTGRAY);
DrawLine(0, m_bar_height, GetScreenWidth(), m_bar_height, LIGHTGRAY);
for (Widget *child : children()) {
child->draw();
}
}