/* Headers. */ #include #include #include "microui.h" #include "renderer.h" #include "widget.h" #include "unit.h" #include "engine.h" #include "ui.h" /* Macros. */ #define nelem(arr) (sizeof(arr)/sizeof(arr[0])) /* Constants. */ static const char TITLE[] = "volute"; enum window { WIN_OPTS = MU_OPT_NOINTERACT | MU_OPT_NOTITLE | MU_OPT_AUTOSIZE | MU_OPT_NOFRAME, }; enum layout { LABEL_WIDTH = 128, UNIT_WIDTH = 48, FIELD_WIDTH = 64, }; static const mu_Color BLACK = {0, 0, 0, 255}; static const mu_Color WHITE = {255, 255, 255, 255}; static const mu_Color LIGHT_GRAY = {222, 222, 222, 255}; static const mu_Color DARK_GRAY = {128, 128, 128, 255}; static const mu_Color COLOR_TEXT = BLACK; static const mu_Color COLOR_BORDER = BLACK; static const mu_Color COLOR_WINDOWBG = WHITE; static const mu_Color COLOR_TITLEBG = LIGHT_GRAY; static const mu_Color COLOR_TITLETEXT = COLOR_TEXT; static const mu_Color COLOR_PANELBG = COLOR_WINDOWBG; static const mu_Color COLOR_BUTTON = WHITE; static const mu_Color COLOR_BUTTONHOVER = LIGHT_GRAY; static const mu_Color COLOR_BUTTONFOCUS = DARK_GRAY; static const mu_Color COLOR_BASE = WHITE; static const mu_Color COLOR_BASEHOVER = COLOR_BASE; static const mu_Color COLOR_BASEFOCUS = COLOR_BASE; static const mu_Color COLOR_SCROLLBASE = WHITE; static const mu_Color COLOR_SCROLLTHUMB = WHITE; /* Function declarations. */ static void set_style(mu_Context *ctx); static void main_loop(mu_Context *ctx, UI *ui); static void process_frame(mu_Context *ctx, UI *ui); static void main_window(mu_Context *ctx, UI *ui); static void displacement_row(mu_Context *ctx, UI *ui); static void ambient_temperature_row(mu_Context *ctx, UI *ui); static void rpm_row(mu_Context *ctx, UI *ui); static void map_row(mu_Context *ctx, UI *ui); static void ve_row(mu_Context *ctx, UI *ui); static void dup_del_row(mu_Context *ctx, UI *ui); static void volume_flow_rate_row(mu_Context *ctx, UI *ui); static void hpad(mu_Context *ctx, int w); static void vpad(mu_Context *ctx, int h); /* Function Definitions. */ int main(void) { /* Init microui. */ static mu_Context ctx; mu_init(&ctx); r_init(&ctx, TITLE); set_style(&ctx); /* Init data structures. */ static UI ui; init_ui(&ui); main_loop(&ctx, &ui); return 0; } static void set_style(mu_Context *ctx) { ctx->style->colors[MU_COLOR_TEXT] = COLOR_TEXT; ctx->style->colors[MU_COLOR_BORDER] = COLOR_BORDER; ctx->style->colors[MU_COLOR_WINDOWBG] = COLOR_WINDOWBG; ctx->style->colors[MU_COLOR_TITLEBG] = COLOR_TITLEBG; ctx->style->colors[MU_COLOR_TITLETEXT] = COLOR_TITLETEXT; ctx->style->colors[MU_COLOR_PANELBG] = COLOR_PANELBG; ctx->style->colors[MU_COLOR_BUTTON] = COLOR_BUTTON; ctx->style->colors[MU_COLOR_BUTTONHOVER] = COLOR_BUTTONHOVER; ctx->style->colors[MU_COLOR_BUTTONFOCUS] = COLOR_BUTTONFOCUS; ctx->style->colors[MU_COLOR_BASE] = COLOR_BASE; ctx->style->colors[MU_COLOR_BASEHOVER] = COLOR_BASEHOVER; ctx->style->colors[MU_COLOR_BASEFOCUS] = COLOR_BASEFOCUS; ctx->style->colors[MU_COLOR_SCROLLBASE] = COLOR_SCROLLBASE; ctx->style->colors[MU_COLOR_SCROLLTHUMB] = COLOR_SCROLLTHUMB; } static void main_loop(mu_Context *ctx, UI *ui) { for (;;) { r_input(ctx); process_frame(ctx, ui); r_render(ctx); } } static void process_frame(mu_Context *ctx, UI *ui) { mu_begin(ctx); main_window(ctx, ui); mu_end(ctx); } static void main_window(mu_Context *ctx, UI *ui) { int w, h; r_get_window_size(&w, &h); if (!mu_begin_window_ex(ctx, TITLE, mu_rect(0, 0, w, h), WIN_OPTS)) { exit(EXIT_FAILURE); } displacement_row(ctx, ui); vpad(ctx, 0); rpm_row(ctx, ui); map_row(ctx, ui); ve_row(ctx, ui); dup_del_row(ctx, ui); vpad(ctx, 0); volume_flow_rate_row(ctx, ui); mu_end_window(ctx); } static void displacement_row(mu_Context *ctx, UI *ui) { mu_layout_row(ctx, 3, (int[]) {LABEL_WIDTH, FIELD_WIDTH, UNIT_WIDTH}, 0); mu_label(ctx, "Displacement:"); if (w_field(ctx, &ui->displacement) & MU_RES_CHANGE) { set_displacement(ui); set_all_volume_flow_rate(ui); } if (w_select(ctx, &ui->displacement_unit) & MU_RES_CHANGE) { set_displacement_unit(ui); } } static void ambient_temperature_row(mu_Context *ctx, UI *ui) { mu_layout_row(ctx, 3, (int[]) {LABEL_WIDTH, FIELD_WIDTH, UNIT_WIDTH}, 0); mu_label(ctx, "Ambient temperature:"); if (w_field(ctx, &ui->ambient_temperature) & MU_RES_CHANGE) { set_ambient_temperature(ui); set_all_volume_flow_rate(ui); } if (w_select(ctx, &ui->ambient_temperature_unit) & MU_RES_CHANGE) { set_ambient_temperature_unit(ui); } } static void rpm_row(mu_Context *ctx, UI *ui) { int i; mu_layout_row(ctx, 0, NULL, 0); mu_layout_width(ctx, LABEL_WIDTH); mu_label(ctx, "rpm:"); hpad(ctx, UNIT_WIDTH); mu_layout_width(ctx, FIELD_WIDTH); for (i = 0; i < ui->npoints; i++) { if (w_field(ctx, &ui->rpm[i])) { ui->points[i].rpm = rpm(ui->rpm[i].value); set_volume_flow_rate(ui, i); } } } static void map_row(mu_Context *ctx, UI *ui) { int i; mu_layout_row(ctx, 0, NULL, 0); mu_layout_width(ctx, LABEL_WIDTH); mu_label(ctx, "map:"); mu_layout_width(ctx, UNIT_WIDTH); if (w_select(ctx, &ui->map_unit) & MU_RES_CHANGE) { set_map_unit(ui); } mu_layout_width(ctx, FIELD_WIDTH); for (i = 0; i npoints; i++) { if (w_field(ctx, &ui->map[i])) { set_map(ui, i); set_volume_flow_rate(ui, i); } } } static void ve_row(mu_Context *ctx, UI *ui) { int i; mu_layout_row(ctx, 0, NULL, 0); mu_layout_width(ctx, LABEL_WIDTH); mu_label(ctx, "ve:"); mu_layout_width(ctx, UNIT_WIDTH); mu_label(ctx, "(%)"); mu_layout_width(ctx, FIELD_WIDTH); for (i = 0; i < ui->npoints; i++) { if (w_field(ctx, &ui->ve[i])) { set_ve(ui, i); set_volume_flow_rate(ui, i); } } } static void dup_del_row(mu_Context *ctx, UI *ui) { int i; mu_layout_row(ctx, 0, NULL, 0); hpad(ctx, LABEL_WIDTH); hpad(ctx, UNIT_WIDTH); mu_layout_width(ctx, (FIELD_WIDTH - ctx->style->spacing)/2); for (i = 0; i < ui->npoints; i++) { mu_push_id(ctx, &i, sizeof(i)); if (mu_button(ctx, "Dup")) { insert_point(ui, i); } if (mu_button(ctx, "Del")) { remove_point(ui, i); } mu_pop_id(ctx); } } static void volume_flow_rate_row(mu_Context *ctx, UI *ui) { int i; mu_layout_row(ctx, 0, NULL, 0); mu_layout_width(ctx, LABEL_WIDTH); mu_label(ctx, "Volume flow rate:"); mu_layout_width(ctx, UNIT_WIDTH); if (w_select(ctx, &ui->volume_flow_rate_unit) & MU_RES_CHANGE) { set_all_volume_flow_rate(ui); } mu_layout_width(ctx, FIELD_WIDTH); for (i = 0; i < ui->npoints; i++) { w_number(ctx, ui->volume_flow_rate[i]); } } static void hpad(mu_Context *ctx, int w) { mu_layout_width(ctx, w); mu_label(ctx, ""); } static void vpad(mu_Context *ctx, int h) { mu_layout_row(ctx, 0, NULL, h); mu_label(ctx, ""); }