diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d8b877..a3ca8fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,17 @@ INCLUDE_DIRECTORIES("build/include") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(SOURCE_FILES - src/main.c src/network.c src/network.h src/windows/list_window.c src/windows/list_window.h src/util.h) + src/main.c + src/network.c + src/network.h + src/windows/list/list_window.c + src/windows/list/list_window.h + src/windows/list/style.h + src/util.h + src/windows/action/action_window.c + src/windows/action/action_window.h + src/windows/action/style.h + src/layers/border_layer.c + src/layers/border_layer.h) add_executable(hello ${SOURCE_FILES}) \ No newline at end of file diff --git a/src/layers/border_layer.c b/src/layers/border_layer.c new file mode 100644 index 0000000..b0577cf --- /dev/null +++ b/src/layers/border_layer.c @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "border_layer.h" + +struct BorderLayer* get_data(const struct Layer *layer) +{ + struct BorderLayer **data_region = layer_get_data(layer); + struct BorderLayer *border = *data_region; + return border; +} + +static void on_update(struct Layer *layer, GContext *context) +{ + struct BorderLayer *border = get_data(layer); + struct GRect bounds = layer_get_bounds(layer); + struct GRect rect = { + .origin = bounds.origin, + .size.h = bounds.size.h, + .size.w = border->width + }; + graphics_context_set_fill_color(context, border->color); + graphics_fill_rect(context, rect, 0, GCornerNone); +} + +static struct Layer* create_layer(struct BorderLayer *border, + struct GRect frame) +{ + size_t data_region_size = sizeof(struct BorderLayer *); + struct Layer *layer = layer_create_with_data(frame, data_region_size); + layer_set_update_proc(layer, on_update); + + struct BorderLayer **data_region = layer_get_data(layer); + *data_region = border; + + return layer; +} + +void BORDER_LAYER_init(struct BorderLayer *border, struct GRect frame) +{ + border->raw_layer = create_layer(border, frame); + border->color = GColorWhite; + border->width = 5; +} + +void BORDER_LAYER_destroy(struct BorderLayer *border) +{ + layer_destroy(border->raw_layer); +} \ No newline at end of file diff --git a/src/layers/border_layer.h b/src/layers/border_layer.h new file mode 100644 index 0000000..ca7e652 --- /dev/null +++ b/src/layers/border_layer.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#pragma once + +#include + +struct BorderLayer +{ + int16_t width; + GColor color; + Layer *raw_layer; +}; + +void BORDER_LAYER_init(struct BorderLayer *border, struct GRect frame); + +void BORDER_LAYER_destroy(struct BorderLayer *border); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 06f3ef2..fbe73f0 100644 --- a/src/main.c +++ b/src/main.c @@ -18,7 +18,7 @@ */ #include -#include "windows/list_window.h" +#include "windows/list/list_window.h" int main(void) { diff --git a/src/network.c b/src/network.c index 899f65c..6bdba97 100644 --- a/src/network.c +++ b/src/network.c @@ -20,7 +20,7 @@ #include #include "network.h" #include "util.h" -#include "windows/list_window.h" +#include "windows/list/list_window.h" #define MAX_HABIT_COUNT 50 static int HABIT_COUNT; diff --git a/src/windows/action/action_window.c b/src/windows/action/action_window.c new file mode 100644 index 0000000..e0d2626 --- /dev/null +++ b/src/windows/action/action_window.c @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include "action_window.h" +#include "style.h" +#include "../../layers/border_layer.h" +#include "../../util.h" + +static struct Window *WINDOW = 0; +static struct MenuLayer *MENU_LAYER = 0; +static struct BorderLayer *BORDER_LAYER = 0; + +char *ACTIONS[] = { " Check", " View stats"}; + +// DRAWING +//------------------------------------------------------------------------------ + +void draw_left_border(GContext *ctx, const Layer *cell_layer) +{ + GRect bounds = layer_get_bounds(cell_layer); + GRect rect = GRect(bounds.origin.x, bounds.origin.y, 5, bounds.size.h); + graphics_context_set_fill_color(ctx, GColorFolly); + graphics_fill_rect(ctx, rect, 0, GCornerNone); +} + +// MENU LAYER CALLBACKS +//------------------------------------------------------------------------------ + +static uint16_t get_num_rows(MenuLayer *menu_layer, + uint16_t section_index, + void *context) +{ + return 2; +} + +static void draw_row(GContext *ctx, + const Layer *cell_layer, + MenuIndex *cell_index, + void *extra) +{ + int n = cell_index->row; + menu_cell_basic_draw(ctx, cell_layer, ACTIONS[n], NULL, NULL); + draw_left_border(ctx, cell_layer); +} + +static int16_t get_cell_height(struct MenuLayer *menu_layer, + MenuIndex *cell_index, + void *context) +{ + return CELL_HEIGHT; +} + +static void select_click(MenuLayer *menu_layer, MenuIndex *index, void *context) +{ +} + +// WINDOWS HANDLERS +//------------------------------------------------------------------------------ + +void add_menu_layer(Window *window, + Layer *root_layer, + GRect bounds) +{ + MenuLayerCallbacks callbacks = { + .get_num_rows = get_num_rows, + .draw_row = draw_row, + .get_cell_height = get_cell_height, + .select_click = select_click, + }; + + MENU_LAYER = menu_layer_create(bounds); + menu_layer_set_click_config_onto_window(MENU_LAYER, window); + menu_layer_set_callbacks(MENU_LAYER, NULL, callbacks); + menu_layer_set_highlight_colors(MENU_LAYER, HIGHLIGHT_BACKGROUND_COLOR, + HIGHLIGHT_FOREGROUND_COLOR); + menu_layer_set_normal_colors(MENU_LAYER, NORMAL_BACKGROUND_COLOR, + NORMAL_FOREGROUND_COLOR); + + layer_add_child(root_layer, menu_layer_get_layer(MENU_LAYER)); +} + +static int add_border_layer(Window *window, Layer *root_layer, GRect frame) +{ + int rval = 0; + + BORDER_LAYER = (struct BorderLayer*) malloc(sizeof(struct BorderLayer)); + abort_if(!BORDER_LAYER, "could not allocate BORDER_LAYER"); + + BORDER_LAYER_init(BORDER_LAYER, frame); + BORDER_LAYER->color = GColorFolly; + + layer_add_child(root_layer, BORDER_LAYER->raw_layer); + +CLEANUP: + return rval; +} + +static void on_load(Window *window) +{ + Layer *root_layer = window_get_root_layer(window); + GRect bounds = layer_get_bounds(root_layer); + add_menu_layer(window, root_layer, bounds); + add_border_layer(window, root_layer, bounds); +} + +static void on_unload(Window *window) +{ + menu_layer_destroy(MENU_LAYER); + free(BORDER_LAYER); +} + +//-------------------------------------------------------------------------------- + +void ACTION_WINDOW_push() +{ + WindowHandlers handlers = { + .load = on_load, + .unload = on_unload + }; + + WINDOW = window_create(); + window_set_window_handlers(WINDOW, handlers); + window_stack_push(WINDOW, true); +} + +void ACTION_WINDOW_destroy() +{ + window_destroy(WINDOW); +} \ No newline at end of file diff --git a/src/windows/action/action_window.h b/src/windows/action/action_window.h new file mode 100644 index 0000000..e2394be --- /dev/null +++ b/src/windows/action/action_window.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#pragma once + +void ACTION_WINDOW_push(); + +void ACTION_WINDOW_destroy(); \ No newline at end of file diff --git a/src/windows/action/style.h b/src/windows/action/style.h new file mode 100644 index 0000000..d133036 --- /dev/null +++ b/src/windows/action/style.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#pragma once + +#include + +#define HIGHLIGHT_BACKGROUND_COLOR GColorBlack + +#define HIGHLIGHT_FOREGROUND_COLOR GColorWhite + +#define NORMAL_BACKGROUND_COLOR GColorBlack + +#define NORMAL_FOREGROUND_COLOR GColorDarkGray + +#define CELL_HEIGHT 36 \ No newline at end of file diff --git a/src/windows/list_window.c b/src/windows/list/list_window.c similarity index 97% rename from src/windows/list_window.c rename to src/windows/list/list_window.c index 24fe3a6..4fd30da 100644 --- a/src/windows/list_window.c +++ b/src/windows/list/list_window.c @@ -18,9 +18,11 @@ */ #include -#include "../network.h" -#include "../util.h" +#include "../../network.h" +#include "../../util.h" #include "list_window.h" +#include "../action/action_window.h" +#include "style.h" static Window *WINDOW = 0; static MenuLayer *MENU_LAYER = 0; @@ -99,7 +101,9 @@ static void select_click(MenuLayer *menu_layer, MenuIndex *index, void *context) { int n = index->row; int id = HABIT_IDS[n]; - NETWORK_request_toggle(id); +// NETWORK_request_toggle(id); + + ACTION_WINDOW_push(); } static void request_list(void *context) diff --git a/src/windows/list_window.h b/src/windows/list/list_window.h similarity index 71% rename from src/windows/list_window.h rename to src/windows/list/list_window.h index 4739c6b..da535e9 100644 --- a/src/windows/list_window.h +++ b/src/windows/list/list_window.h @@ -19,17 +19,6 @@ #pragma once -// STYLE -//-------------------------------------------------------------------------------- - -#define HIGHLIGHT_BACKGROUND_COLOR GColorFolly -#define HIGHLIGHT_FOREGROUND_COLOR GColorWhite -#define NORMAL_BACKGROUND_COLOR GColorBlack -#define NORMAL_FOREGROUND_COLOR GColorWhite -#define CELL_HEIGHT 36 - -//-------------------------------------------------------------------------------- - void LIST_WINDOW_push(); void LIST_WINDOW_destroy(); diff --git a/src/windows/list/style.h b/src/windows/list/style.h new file mode 100644 index 0000000..b1d7d68 --- /dev/null +++ b/src/windows/list/style.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#pragma once + +#include + +#define HIGHLIGHT_BACKGROUND_COLOR GColorFolly + +#define HIGHLIGHT_FOREGROUND_COLOR GColorWhite + +#define NORMAL_BACKGROUND_COLOR GColorWhite + +#define NORMAL_FOREGROUND_COLOR GColorBlack + +#define CELL_HEIGHT 36 \ No newline at end of file