parent
2b92a6832d
commit
4587e1d1ab
@ -0,0 +1,66 @@ |
||||
#include "scenes.h" |
||||
#include "liquid.h" |
||||
#include <malloc.h> |
||||
#include <stdio.h> |
||||
#include "graphics/nokia.h" |
||||
#include "graphics/drawing.h" |
||||
#include "scene_number.h" |
||||
|
||||
struct NumberScene { |
||||
struct Scene base; |
||||
struct NumberSceneOpts *opts; |
||||
}; |
||||
|
||||
static struct SceneEvent onInput(struct NumberScene *self, struct InputEvent event) { |
||||
struct NumberSceneOpts *opts = self->opts; |
||||
switch (event.kind) { |
||||
case InputEventKind_Wheel: |
||||
opts->value += event.wheel.delta; |
||||
|
||||
if (opts->value < opts->min) { |
||||
opts->value = opts->min; |
||||
} |
||||
else if (opts->value > opts->max) { |
||||
opts->value = opts->max; |
||||
} |
||||
|
||||
return SceneEvent_Repaint(); |
||||
|
||||
case InputEventKind_Button: |
||||
if (event.button.state) { |
||||
return SceneEvent_Close(opts->value, NULL); |
||||
} |
||||
// fall through
|
||||
default: |
||||
return SceneEvent_None(); |
||||
} |
||||
} |
||||
|
||||
static void paint(struct NumberScene *self) |
||||
{ |
||||
struct NumberSceneOpts *opts = self->opts; |
||||
|
||||
LCD_clearDisplay(0); |
||||
LCD_setStrEx(opts->label, 1, 1, BLACK, FONT_BOLD); |
||||
|
||||
char buf[10]; |
||||
snprintf(buf, 10, "%d%s", opts->value, opts->unit); |
||||
LCD_setStrEx(buf, 1, 9, BLACK, FONT_DOUBLE); |
||||
} |
||||
|
||||
static void deinit(struct NumberScene *self) |
||||
{ |
||||
free(self->opts); |
||||
self->opts = NULL; |
||||
} |
||||
|
||||
struct Scene *NewScene_Number(struct NumberSceneOpts *opts) { |
||||
struct NumberScene *scene = calloc(1, sizeof(struct NumberScene)); |
||||
if (!scene) return NULL; |
||||
|
||||
scene->opts = opts; |
||||
scene->base.onInput = (Scene_onInput_t) onInput; |
||||
scene->base.paint = (Scene_paint_t) paint; |
||||
scene->base.deinit = (Scene_deinit_t) deinit; |
||||
return (struct Scene *) scene; |
||||
} |
@ -0,0 +1,32 @@ |
||||
/**
|
||||
* TODO file description |
||||
*
|
||||
* Created on 2020/01/05. |
||||
*/ |
||||
|
||||
#ifndef REFLOWER_SCENE_NUMBER_H |
||||
#define REFLOWER_SCENE_NUMBER_H |
||||
|
||||
#include <stdint.h> |
||||
|
||||
struct NumberSceneOpts { |
||||
char label[15]; |
||||
char unit[8]; |
||||
int32_t value; |
||||
int32_t min; |
||||
int32_t max; |
||||
}; |
||||
|
||||
/**
|
||||
* Create number picker. |
||||
* |
||||
* The result is passed back as status. |
||||
* |
||||
* "opts" must be on heap and will be internally mutated. |
||||
* |
||||
* @param opts |
||||
* @return |
||||
*/ |
||||
struct Scene *NewScene_Number(struct NumberSceneOpts *opts); |
||||
|
||||
#endif //REFLOWER_SCENE_NUMBER_H
|
Loading…
Reference in new issue