Skip to content

Commit

Permalink
Create smolui::context wrapper around mu operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ashn-dot-dev committed Oct 29, 2023
1 parent 7975aa7 commit 7bc8628
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 76 deletions.
78 changes: 51 additions & 27 deletions demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,27 @@
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
** IN THE SOFTWARE.
*/
#include <raylib.h>

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

#include <raylib.h>
#include <smolui.h>

int checked = 0;
float slider = 0.0;
float number = 0.0;
char buf[256] = {0};
char buf_a[256] = {0};
char buf_b[256] = {0};

int main(void) {
InitWindow(800, 600, "demo");
SetTargetFPS(60);

mu_Context *ctx = malloc(sizeof(mu_Context));
mu_Context* ctx = smolui_mu_context_new();
mu_init(ctx);
smolui_setup_font_ex(ctx, NULL);
smolui_setup_font(ctx, NULL);

while (!WindowShouldClose()) {
BeginDrawing();
Expand All @@ -46,41 +49,62 @@ int main(void) {
smolui_handle_input(ctx);
mu_begin(ctx);

if (mu_begin_window(ctx, "Hello", mu_rect(20, 20, 400, 300))) {
mu_text(ctx, "text");

mu_checkbox(ctx, "checkbox", &checked);

mu_slider(ctx, &slider, 0.0, 100.0);

mu_number(ctx, &number, 2.0);

mu_label(ctx, "Hello, raylib");

if (mu_header(ctx, "Header")) {
if (mu_begin_window_ex(ctx, "Hello", mu_rect(20, 20, 800 - 2 * 20, 600 - 2 * 20), MU_OPT_NOCLOSE)) {
mu_text(ctx, "some text");
mu_label(ctx, "label");
if (mu_button(ctx, "button")) {
puts("button pressed");
}
if (mu_button_ex(ctx, "button ex", MU_ICON_MAX, MU_OPT_ALIGNCENTER)) {
puts("button (ex) pressed");
}
if (mu_checkbox(ctx, "checkbox", &checked)) {
puts("checkbox interaction");
}
if (mu_textbox(ctx, buf_a, sizeof(buf_a))) {
puts("textbox interaction");
}
int textbox_ex_res = mu_textbox_ex(ctx, buf_b, sizeof(buf_b), MU_OPT_ALIGNCENTER);
if (textbox_ex_res) {
puts("textbox interaction");
if (textbox_ex_res & MU_RES_SUBMIT) {
puts("\ttextbox submit");
}
if (textbox_ex_res & MU_RES_CHANGE) {
puts("\ttextbox change");
}
}
if (mu_slider(ctx, &slider, 0.0, 100.0)) {
puts("slider interaction");
}
if (mu_number(ctx, &number, 5.0)) {
puts("number interaction");
}
if (mu_header(ctx, "header")) {
mu_text(ctx, "text under header");
}

if (mu_begin_treenode(ctx, "Treenode")) {
if (mu_header_ex(ctx, "header ex", MU_OPT_EXPANDED)) {
mu_text(ctx, "text under header (ex)");
}
if (mu_begin_treenode(ctx, "treenode")) {
mu_text(ctx, "text under treenode");
mu_end_treenode(ctx);
}

if (mu_button(ctx, "The button")) {
if (mu_begin_treenode_ex(ctx, "treenode ex", MU_OPT_EXPANDED)) {
mu_text(ctx, "text under treenode (ex)");
mu_end_treenode(ctx);
}
if (mu_button(ctx, "open popup")) {
mu_open_popup(ctx, "popup");
}

if (mu_begin_popup(ctx, "popup")) {
mu_label(ctx, "This is a popup");
mu_text(ctx, "text inside popup");
mu_end_popup(ctx);
}

mu_begin_panel(ctx, "panel");
mu_text(ctx, "text under panel");
mu_text(ctx, "text inside panel");
mu_end_panel(ctx);

mu_textbox(ctx, buf, sizeof(buf));

mu_end_window(ctx);
}

Expand All @@ -89,7 +113,7 @@ int main(void) {
EndDrawing();
}

free(ctx);
smolui_mu_context_del(ctx);
CloseWindow();
return 0;
}
109 changes: 65 additions & 44 deletions demo.sunder
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,89 @@ import "raylib";
import "microui.sunder";
import "smolui.sunder";

var checked: sint = 0;
var slider: float = 0.0;
var number: float = 0.0;
var buf = (:[256]byte)[0...];

func main() void {
InitWindow(800, 600, startof("demo"));
defer CloseWindow();
SetTargetFPS(60);

var ctx = smolui::mu_context_new();
defer smolui::mu_context_del(ctx);
mu_init(ctx);
smolui::setup_font_ex(ctx, std::ptr[[Font]]::NULL);
var ui = smolui::context::init(std::ptr[[Font]]::NULL);

var textbuf_a = std::string::init_from_str("textbox a");
var textbuf_b = std::string::init_from_str("textbox b");
defer textbuf_a.fini();
defer textbuf_b.fini();
var checked: bool = false;
var slider: float = 0.0;
var number: float = 0.0;

for not WindowShouldClose() {
BeginDrawing();
ClearBackground(BLACK);

smolui::handle_input(ctx);
mu_begin(ctx);

if mu_begin_window(ctx, startof("Hello"), (:mu_Rect){.x = 20, .y = 20, .w = 400, .h = 300}) != 0 {
mu_text(ctx, startof("text"));

mu_checkbox(ctx, startof("checkbox"), &checked);

mu_slider(ctx, &slider, 0.0, 100.0);

mu_number(ctx, &number, 2.0);
ui.handle_input();

mu_label(ctx, startof("Hello, raylib"));

if mu_header(ctx, startof("Header")) != 0 {
mu_text(ctx, startof("text under header"));
ui.begin();
if ui.begin_window_ex("window", (:smolui::rect){.x = 20, .y = 20, .w = 800 - 2 * 20, .h = 600 - 2 * 20}, smolui::OPT_NOCLOSE) {
ui.text("some text");
ui.label("label");
if ui.button("button"){
std::print_line(std::out(), "button pressed");
}

if mu_begin_treenode(ctx, startof("Treenode")) != 0 {
mu_text(ctx, startof("text under treenode"));
mu_end_treenode(ctx);
if ui.button_ex("button ex", smolui::ICON_MAX, smolui::OPT_ALIGNCENTER){
std::print_line(std::out(), "button (ex) pressed");
}

if mu_button(ctx, startof("The button")) != 0 {
mu_open_popup(ctx, startof("popup"));
if ui.checkbox("checkbox", &checked) {
std::print_line(std::out(), "checkbox interaction");
}

if mu_begin_popup(ctx, startof("popup")) != 0 {
mu_label(ctx, startof("This is a popup"));
mu_end_popup(ctx);
if ui.textbox(&textbuf_a) {
std::print_line(std::out(), "textbox interaction");
}
var textbox_ex_res = ui.textbox_ex(&textbuf_b, smolui::OPT_ALIGNCENTER);
if textbox_ex_res != 0 {
std::print_line(std::out(), "textbox (ex) interaction");
if (textbox_ex_res & smolui::RES_SUBMIT) != 0 {
std::print_line(std::out(), "\ttextbox submit");
}
if (textbox_ex_res & smolui::RES_CHANGE) != 0 {
std::print_line(std::out(), "\ttextbox change");
}
}
if ui.slider(&slider, 0.0, 100.0) {
std::print_line(std::out(), "slider interaction");
}
if ui.number(&number, 5.0) {
std::print_line(std::out(), "number interaction");
}
if ui.header("header") {
ui.text("text under header");
}
if ui.header_ex("header ex", smolui::OPT_EXPANDED) {
ui.text("text under header (ex)");
}
if ui.begin_treenode("treenode") {
ui.text("text under treenode");
ui.end_treenode();
}
if ui.begin_treenode_ex("treenode ex", smolui::OPT_EXPANDED) {
ui.text("text under treenode (ex)");
ui.end_treenode();
}
if ui.button("open popup") {
ui.open_popup("popup");
}
if ui.begin_popup("popup") {
ui.text("text inside popup");
ui.end_popup();
}
ui.begin_panel("panel");
ui.text("text inside panel");
ui.end_panel();

mu_begin_panel(ctx, startof("panel"));
mu_text(ctx, startof("text under panel"));
mu_end_panel(ctx);

mu_textbox(ctx, &buf[0], (:sint)countof(buf));

mu_end_window(ctx);
ui.end_window();
}
ui.end();

mu_end(ctx);
smolui::render(ctx);
ui.render();
EndDrawing();
}
}
4 changes: 3 additions & 1 deletion microui.sunder
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ extern func mu_init(ctx: *mu_Context) void;
extern func mu_begin(ctx: *mu_Context) void;
extern func mu_end(ctx: *mu_Context) void;

extern func mu_layout_next(ctx: *mu_Context) mu_Rect;

func mu_button(ctx: *mu_Context, label: *char) sint { return mu_button_ex(ctx, label, 0, (:sint)MU_OPT_ALIGNCENTER); }
func mu_textbox(ctx: *mu_Context, buf: *char, bufsz: sint) sint { return mu_textbox_ex(ctx, buf, bufsz, 0); }
func mu_slider(ctx: *mu_Context, value: *mu_Real, lo: mu_Real, hi: mu_Real) sint { return mu_slider_ex(ctx, value, lo, hi, 0.0, MU_SLIDER_FMT, (:sint)MU_OPT_ALIGNCENTER); }
Expand All @@ -101,7 +103,7 @@ func mu_begin_treenode(ctx: *mu_Context, label: *char) sint { return mu_begin_tr
func mu_begin_window(ctx: *mu_Context, title: *char, rect: mu_Rect) sint { return mu_begin_window_ex(ctx, title, rect, 0); }
func mu_begin_panel(ctx: *mu_Context, name: *char) void { mu_begin_panel_ex(ctx, name, 0); }

extern func mu_text(ctx: *mu_Context, text: *byte) void;
extern func mu_text(ctx: *mu_Context, text: *char) void;
extern func mu_label(ctx: *mu_Context, text: *char) void;
extern func mu_button_ex(ctx: *mu_Context, label: *char, icon: sint, opt: sint) sint;
extern func mu_checkbox(ctx: *mu_Context, label: *char, state: *sint) sint;
Expand Down
2 changes: 1 addition & 1 deletion smolui.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ smolui_mu_context_del(mu_Context* ctx)
}

void
smolui_setup_font_ex(mu_Context* ctx, Font const* font)
smolui_setup_font(mu_Context* ctx, Font const* font)
{
ctx->style->font = (mu_Font)font;
ctx->text_width = smolui_text_width;
Expand Down
4 changes: 2 additions & 2 deletions smolui.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ void smolui_mu_context_del(mu_Context* ctx);
#define SMOLUI_VECTOR2_FROM_MU(v) ((Vector2){v.x, v.y})

// Set the text height/width callbacks and the font.
void smolui_setup_font_ex(mu_Context* ctx, Font const* font);
#define smolui_setup_font(ctx) smolui_setup_font_ex(ctx, NULL)
// Providing a NULL font pointer wll use the default raylib font.
void smolui_setup_font(mu_Context* ctx, Font const* font);

// `mu_Context.text_width` callback. See `smolui_setup_font`.
int smolui_text_width(mu_Font font, char const* str, int len);
Expand Down
Loading

0 comments on commit 7bc8628

Please sign in to comment.