2020-08-09 15:03:02 -04:00
|
|
|
#include "pebbleobject.h"
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-08-09 15:03:02 -04:00
|
|
|
#include "calc.h"
|
|
|
|
#include "object.h"
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object pebbleOther(enum PebbleType type, void* data, void (* cleanup)(Object*),
|
|
|
|
Object (* clone)(struct Other*))
|
2020-08-09 15:03:02 -04:00
|
|
|
{
|
|
|
|
struct Object o = otherObject();
|
2022-01-07 16:55:03 -05:00
|
|
|
struct PebbleObject* po = malloc(sizeof(struct PebbleObject));
|
2020-08-09 15:03:02 -04:00
|
|
|
o.other->data = po;
|
|
|
|
o.other->cleanup = cleanup;
|
|
|
|
o.other->clone = clone;
|
|
|
|
po->type = type;
|
|
|
|
po->ptr = data;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum PebbleType getPebbleType(const Object o1)
|
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (o1.type == TYPE_OTHER) {
|
|
|
|
struct PebbleObject* po = o1.other->data;
|
2020-08-09 15:03:02 -04:00
|
|
|
enum PebbleType t = po->type;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return P_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
struct PebbleObject* accessPebbleObject(Object obj)
|
|
|
|
{
|
|
|
|
if (getPebbleType(obj) != P_ERROR) {
|
2020-08-09 15:03:02 -04:00
|
|
|
return obj.other->data;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
void custom_window_load(Window* window)
|
|
|
|
{
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
void custom_window_unload(Window* window)
|
|
|
|
{
|
2020-08-09 15:03:02 -04:00
|
|
|
window_destroy(window);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object createWindow(Object o1, Object o2, struct Environment* env)
|
|
|
|
{
|
|
|
|
Window* window = window_create();
|
|
|
|
WindowHandlers wh = {.load = custom_window_load,
|
|
|
|
.unload = custom_window_unload};
|
2020-08-09 15:03:02 -04:00
|
|
|
window_set_window_handlers(window, wh);
|
|
|
|
return pebbleOther(WINDOW, window, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object deleteWindow(Object window, Object o2, struct Environment* env)
|
|
|
|
{
|
2020-08-09 15:03:02 -04:00
|
|
|
/* Maintain a list of layers to delete? */
|
2022-01-07 16:55:03 -05:00
|
|
|
if (getPebbleType(window) == WINDOW) {
|
2020-08-09 15:03:02 -04:00
|
|
|
window_stack_remove(accessPebbleObject(window)->window, true);
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return falseObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object pushWindow(Object window, Object o2, struct Environment* env)
|
|
|
|
{
|
|
|
|
if (getPebbleType(window) == WINDOW) {
|
2020-08-09 15:03:02 -04:00
|
|
|
window_stack_push(accessPebbleObject(window)->window, true);
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return falseObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object updateTextLayer(Object textLayer, Object text, struct Environment* env)
|
|
|
|
{
|
|
|
|
if (getPebbleType(textLayer) == TEXT_LAYER) {
|
|
|
|
struct PebbleObject* po = accessPebbleObject(textLayer);
|
2020-08-09 15:03:02 -04:00
|
|
|
stringObj(po->textLayer->text, &text);
|
|
|
|
text_layer_set_text(po->textLayer->layer, po->textLayer->text);
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return falseObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object addTextLayer(Object window, Object text, struct Environment* env)
|
|
|
|
{
|
|
|
|
if (getPebbleType(window) != WINDOW) {
|
2020-08-09 15:03:02 -04:00
|
|
|
return errorObject(0);
|
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
Layer* window_layer =
|
|
|
|
window_get_root_layer(accessPebbleObject(window)->window);
|
2020-08-09 15:03:02 -04:00
|
|
|
GRect bounds = layer_get_bounds(window_layer);
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
struct PLTextLayer* textLayer = malloc(sizeof(struct PLTextLayer));
|
2020-08-09 15:03:02 -04:00
|
|
|
textLayer->text = calloc(sizeof(char), 100);
|
|
|
|
|
|
|
|
textLayer->layer = text_layer_create(bounds);
|
|
|
|
stringObj(textLayer->text, &text);
|
|
|
|
text_layer_set_text(textLayer->layer, textLayer->text);
|
|
|
|
text_layer_set_font(textLayer->layer,
|
2022-01-07 16:55:03 -05:00
|
|
|
fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
|
2020-08-09 15:03:02 -04:00
|
|
|
layer_add_child(window_layer, text_layer_get_layer(textLayer->layer));
|
|
|
|
|
|
|
|
return pebbleOther(TEXT_LAYER, textLayer, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object subscription;
|
2022-01-07 16:55:03 -05:00
|
|
|
struct Environment* subscriptionEnv;
|
2020-08-09 15:03:02 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
static void subscriptionHandler(struct tm* tick_time, TimeUnits changed)
|
|
|
|
{
|
2020-08-09 15:03:02 -04:00
|
|
|
eval(&subscription, subscriptionEnv);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object subscribe(Object function, Object time, struct Environment* env)
|
|
|
|
{
|
|
|
|
if (function.type == TYPE_LAMBDA) {
|
2020-08-09 15:03:02 -04:00
|
|
|
subscription = cloneObject(function);
|
|
|
|
subscriptionEnv = env;
|
2022-01-07 16:55:03 -05:00
|
|
|
int unit = time.type != TYPE_NUMBER ? MINUTE_UNIT
|
|
|
|
: time.number == 1 ? SECOND_UNIT
|
|
|
|
: time.number == 2 ? MINUTE_UNIT
|
|
|
|
: time.number == 3 ? HOUR_UNIT
|
|
|
|
: time.number == 4
|
|
|
|
? DAY_UNIT
|
|
|
|
: time.number ==
|
|
|
|
5 ? MONTH_UNIT
|
|
|
|
:
|
|
|
|
time.number ==
|
|
|
|
6 ? YEAR_UNIT
|
|
|
|
: MINUTE_UNIT;
|
2020-08-09 15:03:02 -04:00
|
|
|
tick_timer_service_subscribe(unit, subscriptionHandler);
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return falseObject();
|
2020-08-09 15:03:02 -04:00
|
|
|
}
|